3 * @brief main and its helper functions, signal handling and cleanup functions
6 // --- Custom headers --- //
13 #include "bbb_pin_defines.h"
15 // --- Standard headers --- //
16 #include <syslog.h> // for system logging
17 #include <signal.h> // for signal handling
20 #ifdef REALTIME_VERSION
24 #include <sys/utsname.h>
25 #endif //REALTIME_VERSION
27 // --- Variable definitions --- //
28 Options g_options; // options passed to program through command line arguments
30 // --- Function definitions --- //
33 * Parse command line arguments, initialise g_options
34 * @param argc - Number of arguments
35 * @param argv - Array of argument strings
37 void ParseArguments(int argc, char ** argv)
39 g_options.program = argv[0]; // program name
40 g_options.verbosity = LOGDEBUG; // default log level
41 // Set the main directory
42 //if (getcwd(g_options.root_dir, sizeof(g_options.root_dir)) == NULL)
43 // Fatal("Couldn't get current working directory - %s", strerror(errno));
45 clock_gettime(CLOCK_MONOTONIC, &(g_options.start_time)); // Start time
48 g_options.auth_method = AUTH_NONE; // Don't use authentication
49 g_options.auth_uri = ""; //
50 g_options.ldap_base_dn = "";
51 g_options.experiment_dir = ".";
53 for (int i = 1; i < argc; ++i)
55 if (argv[i][0] != '-')
56 Fatal("Unexpected argv[%d] - %s", i, argv[i]);
58 if (i+1 >= argc || argv[i+1][0] == '-')
59 Fatal("No argument following switch %s", argv[i]);
61 if (strlen(argv[i]) > 2)
62 Fatal("Human readable switches are not supported.");
67 // Set program verbosity
69 g_options.verbosity = strtol(argv[++i], &end, 10);
71 // Enable/Disable pin test
73 g_options.enable_pin = !(strtol(argv[++i], &end, 10));
77 g_options.auth_uri = argv[++i];
81 g_options.ldap_base_dn = argv[++i];
84 // Experiments directory
85 g_options.experiment_dir = argv[++i];
88 Fatal("Unrecognised switch %s", argv[i]);
92 if (end != NULL && *end != '\0')
93 Fatal("argv[%d] -%c requires an integer (got \"%s\" instead)", i-1, argv[i-1][0], argv[i]);
96 Log(LOGDEBUG, "Verbosity: %d", g_options.verbosity);
97 Log(LOGDEBUG, "Pin Module Enabled: %d", g_options.enable_pin);
98 Log(LOGDEBUG, "Auth URI: %s", g_options.auth_uri);
99 Log(LOGDEBUG, "LDAP Base DN: %s", g_options.ldap_base_dn);
100 //Log(LOGDEBUG, "Root directory: %s", g_options.root_dir);
101 Log(LOGDEBUG, "Experiment directory: %s", g_options.experiment_dir);
103 if (!DirExists(g_options.experiment_dir))
105 Fatal("Experiment directory '%s' does not exist.", g_options.experiment_dir);
108 if (g_options.auth_uri[0] != '\0')
111 if (PathExists(g_options.auth_uri))
112 g_options.auth_method = AUTH_SHADOW;
114 g_options.auth_method = AUTH_LDAP;
120 * Cleanup before the program exits
124 Log(LOGDEBUG, "Begin cleanup.");
126 //Actuator_Cleanup();
127 Log(LOGDEBUG, "Finish cleanup.");
131 #ifdef REALTIME_VERSION
133 #define MAX_SAFE_STACK (8*1024)
134 #define NSEC_PER_SEC (1000000000)
136 void stack_prefault()
138 unsigned char dummy[MAX_SAFE_STACK];
139 memset(dummy, 0, MAX_SAFE_STACK);
150 crit1 = (strcasestr(u.version, "PREEMPT RT") != NULL);
151 if (crit1 && ((f = fopen("/sys/kernel/realtime", "r")) != NULL))
154 crit2 = ((fscanf(f, "%d", &flag) == 1) && (flag == 1));
157 return (crit1 && crit2);
160 #endif //REALTIME_VERSION
163 * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
164 * @param argc - Num args
166 * @returns 0 on success, error code on failure
167 * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
169 int main(int argc, char ** argv)
172 // Open log before calling ParseArguments (since ParseArguments may call the Log functions)
173 openlog("mctxserv", LOG_PID | LOG_PERROR, LOG_USER);
175 ParseArguments(argc, argv); // Setup the g_options structure from program arguments
177 Log(LOGINFO, "Server started");
181 #ifdef REALTIME_VERSION
185 Log(LOGDEBUG, "Running under realtime kernel");
189 Fatal("Not running under realtime kernel");
191 struct sched_param param;
192 param.sched_priority = 49;
193 if (sched_setscheduler(0, SCHED_FIFO, ¶m) < 0)
194 Fatal("sched_setscheduler failed - %s", strerror(errno));
195 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
196 Fatal("mlockall failed - %s", strerror(errno));
198 #endif //REALTIME_VERSION
206 // Try and start things
209 if ((ret = Control_SetMode(CONTROL_START, "test")) != NULL)
210 Fatal("Control_SetMode failed with '%s'", ret);
213 // run request thread in the main thread
214 FCGI_RequestLoop(NULL);
217 if ((ret = Control_SetMode(CONTROL_STOP, "test")) != NULL)
218 Fatal("Control_SetMode failed with '%s'", ret);
221 //Actuator_StopAll();