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.auth_options = "";
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));
75 // Authentication URI and options
77 g_options.auth_uri = argv[++i];
80 // Experiments directory
81 g_options.experiment_dir = argv[++i];
84 Fatal("Unrecognised switch %s", argv[i]);
88 if (end != NULL && *end != '\0')
89 Fatal("argv[%d] -%c requires an integer (got \"%s\" instead)", i-1, argv[i-1][0], argv[i]);
94 if (!DirExists(g_options.experiment_dir))
96 Fatal("Experiment directory '%s' does not exist.", g_options.experiment_dir);
99 if (g_options.auth_uri[0] != '\0')
101 // Get the options part of the URI if it exists
102 char * c = (char*)g_options.auth_uri;
103 while (*(++c) != '\0' && *c != '#');
108 g_options.auth_options = c;
111 // Use the first part of the URI to identify the protocol:
112 c = (char*)g_options.auth_uri;
113 while (*(++c) != '\0' && *c != ':');
115 if (*c == '\0') // No ':' means no protocol; use plaintext file
117 g_options.auth_method = AUTH_SHADOW;
119 else if (*c == ':' && *(c+1) == '/' && *(c+2) == '/')
123 if (strcmp(g_options.auth_uri, "ldap") == 0 || strcmp(g_options.auth_uri, "ldaps") == 0)
125 *c = ':'; // LDAP URI's require the prodocol as part of the string
126 g_options.auth_method = AUTH_LDAP;
128 else if (strcmp(g_options.auth_uri, "mysql") == 0)
130 g_options.auth_uri = c+3; // MySQL doesn't (just a hostname)
131 g_options.auth_method = AUTH_MYSQL;
135 Fatal("Unsupported authentication method %s", g_options.auth_uri);
141 Log(LOGWARN, "No authentication method.");
144 Log(LOGDEBUG, "Verbosity: %d", g_options.verbosity);
145 Log(LOGDEBUG, "Pin Module Enabled: %d", g_options.enable_pin);
146 Log(LOGDEBUG, "Auth method: %d", g_options.auth_method);
147 Log(LOGDEBUG, "Auth URI: %s", g_options.auth_uri);
148 Log(LOGDEBUG, "Auth Options: %s", g_options.auth_options);
149 //Log(LOGDEBUG, "Root directory: %s", g_options.root_dir);
150 Log(LOGDEBUG, "Experiment directory: %s", g_options.experiment_dir);
157 * Cleanup before the program exits
161 Log(LOGDEBUG, "Begin cleanup.");
164 Log(LOGDEBUG, "Finish cleanup.");
168 #ifdef REALTIME_VERSION
170 #define MAX_SAFE_STACK (8*1024)
171 #define NSEC_PER_SEC (1000000000)
173 void stack_prefault()
175 unsigned char dummy[MAX_SAFE_STACK];
176 memset(dummy, 0, MAX_SAFE_STACK);
187 crit1 = (strcasestr(u.version, "PREEMPT RT") != NULL);
188 if (crit1 && ((f = fopen("/sys/kernel/realtime", "r")) != NULL))
191 crit2 = ((fscanf(f, "%d", &flag) == 1) && (flag == 1));
194 return (crit1 && crit2);
197 #endif //REALTIME_VERSION
200 * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
201 * @param argc - Num args
203 * @returns 0 on success, error code on failure
204 * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
206 int main(int argc, char ** argv)
209 // Open log before calling ParseArguments (since ParseArguments may call the Log functions)
210 openlog("mctxserv", LOG_PID | LOG_PERROR, LOG_USER);
212 ParseArguments(argc, argv); // Setup the g_options structure from program arguments
214 Log(LOGINFO, "Server started");
218 #ifdef REALTIME_VERSION
222 Log(LOGDEBUG, "Running under realtime kernel");
226 Fatal("Not running under realtime kernel");
228 struct sched_param param;
229 param.sched_priority = 49;
230 if (sched_setscheduler(0, SCHED_FIFO, ¶m) < 0)
231 Fatal("sched_setscheduler failed - %s", strerror(errno));
232 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
233 Fatal("mlockall failed - %s", strerror(errno));
235 #endif //REALTIME_VERSION
241 // Try and start things
244 //if ((ret = Control_SetMode(CONTROL_START, "test")) != NULL)
245 // Fatal("Control_SetMode failed with '%s'", ret);
248 // run request thread in the main thread
249 FCGI_RequestLoop(NULL);
252 Control_SetMode(CONTROL_STOP, NULL);
253 //if ((ret = Control_SetMode(CONTROL_STOP, "test")) != NULL)
254 // Fatal("Control_SetMode failed with '%s'", ret);
257 //Actuator_StopAll();