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)
41 g_options.program = argv[0]; // program name
42 g_options.verbosity = LOGDEBUG; // default log level
43 // Set the main directory
44 if (getcwd(g_options.root_dir, sizeof(g_options.root_dir)) == NULL)
45 Fatal("Couldn't get current working directory - %s", strerror(errno));
47 clock_gettime(CLOCK_MONOTONIC, &(g_options.start_time)); // Start time
50 g_options.auth_method = AUTH_NONE; // Don't use authentication
51 g_options.auth_uri = ""; //
52 g_options.ldap_base_dn = "";
56 for (int i = 1; i < argc; ++i)
58 if (argv[i][0] != '-')
59 Fatal("Unexpected argv[%d] - %s", i, argv[i]);
61 if (i+1 >= argc || argv[i+1][0] == '-')
62 Fatal("No argument following switch %s", argv[i]);
64 if (strlen(argv[i]) > 2)
65 Fatal("Human readable switches are not supported.");
70 // Set program verbosity
72 g_options.verbosity = strtol(argv[++i], &end, 10);
74 // Enable/Disable pin test
76 g_options.enable_pin = !(strtol(argv[++i], &end, 10));
80 g_options.auth_uri = argv[++i];
84 g_options.ldap_base_dn = argv[++i];
87 Fatal("Unrecognised switch %s", argv[i]);
91 if (end != NULL && *end != '\0')
92 Fatal("argv[%d] -%c requires an integer (got \"%s\" instead)", i-1, argv[i-1][0], argv[i]);
95 Log(LOGDEBUG, "Verbosity: %d", g_options.verbosity);
96 Log(LOGDEBUG, "Pin Module Enabled: %d", g_options.enable_pin);
97 Log(LOGDEBUG, "Auth URI: %s", g_options.auth_uri);
98 Log(LOGDEBUG, "LDAP Base DN: %s", g_options.ldap_base_dn);
99 Log(LOGDEBUG, "Root directory: %s", g_options.root_dir);
101 if (g_options.auth_uri[0] != '\0')
104 if (PathExists(g_options.auth_uri))
105 g_options.auth_method = AUTH_SHADOW;
107 g_options.auth_method = AUTH_LDAP;
113 * Cleanup before the program exits
117 Log(LOGDEBUG, "Begin cleanup.");
119 //Actuator_Cleanup();
120 Log(LOGDEBUG, "Finish cleanup.");
124 #ifdef REALTIME_VERSION
126 #define MAX_SAFE_STACK (8*1024)
127 #define NSEC_PER_SEC (1000000000)
129 void stack_prefault()
131 unsigned char dummy[MAX_SAFE_STACK];
132 memset(dummy, 0, MAX_SAFE_STACK);
143 crit1 = (strcasestr(u.version, "PREEMPT RT") != NULL);
144 if (crit1 && ((f = fopen("/sys/kernel/realtime", "r")) != NULL))
147 crit2 = ((fscanf(f, "%d", &flag) == 1) && (flag == 1));
150 return (crit1 && crit2);
153 #endif //REALTIME_VERSION
156 * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
157 * @param argc - Num args
159 * @returns 0 on success, error code on failure
160 * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
162 int main(int argc, char ** argv)
165 // Open log before calling ParseArguments (since ParseArguments may call the Log functions)
166 openlog("mctxserv", LOG_PID | LOG_PERROR, LOG_USER);
168 ParseArguments(argc, argv); // Setup the g_options structure from program arguments
170 Log(LOGINFO, "Server started");
174 #ifdef REALTIME_VERSION
178 Log(LOGDEBUG, "Running under realtime kernel");
182 Fatal("Not running under realtime kernel");
184 struct sched_param param;
185 param.sched_priority = 49;
186 if (sched_setscheduler(0, SCHED_FIFO, ¶m) < 0)
187 Fatal("sched_setscheduler failed - %s", strerror(errno));
188 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
189 Fatal("mlockall failed - %s", strerror(errno));
191 #endif //REALTIME_VERSION
199 // Try and start things
202 if ((ret = Control_SetMode(CONTROL_START, "test")) != NULL)
203 Fatal("Control_SetMode failed with '%s'", ret);
206 // run request thread in the main thread
207 FCGI_RequestLoop(NULL);
210 if ((ret = Control_SetMode(CONTROL_STOP, "test")) != NULL)
211 Fatal("Control_SetMode failed with '%s'", ret);
214 //Actuator_StopAll();