3 * @brief main and its helper functions, signal handling and cleanup functions
6 // --- Custom headers --- //
13 // --- Standard headers --- //
14 #include <signal.h> // for signal handling
16 // --- Variable definitions --- //
17 Options g_options; // options passed to program through command line arguments
19 // --- Function definitions --- //
22 * Parse command line arguments, initialise g_options
23 * @param argc - Number of arguments
24 * @param argv - Array of argument strings
26 void ParseArguments(int argc, char ** argv)
28 g_options.program = argv[0]; // program name
29 g_options.verbosity = LOGDEBUG; // default log level
30 gettimeofday(&(g_options.start_time), NULL); // Start time
31 Log(LOGDEBUG, "Called as %s with %d arguments.", g_options.program, argc);
36 * @param signal - The signal number
38 //TODO: Something that gets massively annoying with threads is that you can't predict which one gets the signal
39 // There are ways to deal with this, but I can't remember them
40 // Probably sufficient to just call Thread_QuitProgram here
41 void SignalHandler(int signal)
43 // At the moment just always exit.
44 // Call `exit` so that Cleanup will be called to... clean up.
45 Log(LOGWARN, "Got signal %d (%s). Exiting.", signal, strsignal(signal));
51 * Cleanup before the program exits
55 Log(LOGDEBUG, "Begin cleanup.");
56 Log(LOGDEBUG, "Finish cleanup.");
61 * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
62 * @param argc - Num args
64 * @returns 0 on success, error code on failure
65 * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
67 int main(int argc, char ** argv)
69 ParseArguments(argc, argv);
72 //TODO: Make this work
74 int signals[] = {SIGINT, SIGSEGV, SIGTERM};
75 for (int i = 0; i < sizeof(signals)/sizeof(int); ++i)
77 signal(signals[i], SignalHandler);
82 //Sensor_StartAll("test");
83 //Actuator_StartAll("test");
85 if ((ret = Control_SetMode(CONTROL_START, "test")) != NULL)
86 Fatal("Control_SetMode failed with '%s'", ret);
88 // run request thread in the main thread
89 FCGI_RequestLoop(NULL);
91 if ((ret = Control_SetMode(CONTROL_STOP, "test")) != NULL)
92 Fatal("Control_SetMode failed with '%s'", ret);