3 * @brief main and its helper functions, signal handling and cleanup functions
6 // --- Custom headers --- //
12 // --- Standard headers --- //
13 #include <signal.h> // for signal handling
15 // --- Variable definitions --- //
16 Options g_options; // options passed to program through command line arguments
18 // --- Function definitions --- //
21 * Parse command line arguments, initialise g_options
22 * @param argc - Number of arguments
23 * @param argv - Array of argument strings
25 void ParseArguments(int argc, char ** argv)
27 g_options.program = argv[0]; // program name
28 g_options.verbosity = LOGDEBUG; // default log level
29 gettimeofday(&(g_options.start_time), NULL); // Start time
30 Log(LOGDEBUG, "Called as %s with %d arguments.", g_options.program, argc);
35 * @param signal - The signal number
37 //TODO: Something that gets massively annoying with threads is that you can't predict which one gets the signal
38 // There are ways to deal with this, but I can't remember them
39 // Probably sufficient to just call Thread_QuitProgram here
40 void SignalHandler(int signal)
42 // At the moment just always exit.
43 // Call `exit` so that Cleanup will be called to... clean up.
44 Log(LOGWARN, "Got signal %d (%s). Exiting.", signal, strsignal(signal));
50 * Cleanup before the program exits
54 Log(LOGDEBUG, "Begin cleanup.");
55 Log(LOGDEBUG, "Finish cleanup.");
60 * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
61 * @param argc - Num args
63 * @returns 0 on success, error code on failure
64 * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
66 int main(int argc, char ** argv)
68 ParseArguments(argc, argv);
71 //TODO: Make this work
73 int signals[] = {SIGINT, SIGSEGV, SIGTERM};
74 for (int i = 0; i < sizeof(signals)/sizeof(int); ++i)
76 signal(signals[i], SignalHandler);
81 Sensor_StartAll("test");
82 Actuator_StartAll("test");
84 // run request thread in the main thread
85 FCGI_RequestLoop(NULL);