3 * @brief main and its helper functions, signal handling and cleanup functions
6 // --- Custom headers --- //
13 // --- Standard headers --- //
14 #include <syslog.h> // for system logging
15 #include <signal.h> // for signal handling
17 // --- Variable definitions --- //
18 Options g_options; // options passed to program through command line arguments
20 // --- Function definitions --- //
23 * Parse command line arguments, initialise g_options
24 * @param argc - Number of arguments
25 * @param argv - Array of argument strings
27 void ParseArguments(int argc, char ** argv)
29 g_options.program = argv[0]; // program name
30 g_options.verbosity = LOGDEBUG; // default log level
31 gettimeofday(&(g_options.start_time), NULL); // Start time
32 Log(LOGDEBUG, "Called as %s with %d arguments.", g_options.program, argc);
37 * @param signal - The signal number
39 //TODO: Something that gets massively annoying with threads is that you can't predict which one gets the signal
40 // There are ways to deal with this, but I can't remember them
41 // Probably sufficient to just call Thread_QuitProgram here
42 void SignalHandler(int signal)
44 // At the moment just always exit.
45 // Call `exit` so that Cleanup will be called to... clean up.
46 Log(LOGWARN, "Got signal %d (%s). Exiting.", signal, strsignal(signal));
52 * Cleanup before the program exits
56 Log(LOGDEBUG, "Begin cleanup.");
57 Log(LOGDEBUG, "Finish cleanup.");
62 * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
63 * @param argc - Num args
65 * @returns 0 on success, error code on failure
66 * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
68 int main(int argc, char ** argv)
70 ParseArguments(argc, argv);
73 openlog("mctxserv", LOG_PID | LOG_PERROR, LOG_USER);
74 Log(LOGINFO, "Server started");
76 //TODO: Make this work
78 int signals[] = {SIGINT, SIGSEGV, SIGTERM};
79 for (int i = 0; i < sizeof(signals)/sizeof(int); ++i)
81 signal(signals[i], SignalHandler);
86 //Sensor_StartAll("test");
87 //Actuator_StartAll("test");
89 if ((ret = Control_SetMode(CONTROL_START, "test")) != NULL)
90 Fatal("Control_SetMode failed with '%s'", ret);
92 // run request thread in the main thread
93 FCGI_RequestLoop(NULL);
95 if ((ret = Control_SetMode(CONTROL_STOP, "test")) != NULL)
96 Fatal("Control_SetMode failed with '%s'", ret);