3 * @purpose main and its helper functions, signal handling and cleanup functions
6 // --- Custom headers --- //
11 // --- Standard headers --- //
12 #include <signal.h> // for signal handling
14 // --- Variable definitions --- //
15 Options g_options; // options passed to program through command line arguments
16 Sensor g_sensors[NUMSENSORS]; // sensors array
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 Log(LOGDEBUG, "Called as %s with %d arguments.", g_options.program, argc);
34 * @param signal - The signal number
36 //TODO: Something that gets massively annoying with threads is that you can't predict which one gets the signal
37 // There are ways to deal with this, but I can't remember them
38 void SignalHandler(int signal)
40 // At the moment just always exit.
41 // Call `exit` so that Cleanup will be called to... clean up.
42 Log(LOGWARN, "Got signal %d (%s). Exiting.", signal, strsignal(signal));
47 * Cleanup before the program exits
51 Log(LOGDEBUG, "Begin cleanup.");
52 Log(LOGDEBUG, "Finish cleanup.");
57 * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
58 * @param argc - Num args
60 * @returns 0 on success, error code on failure
62 int main(int argc, char ** argv)
64 ParseArguments(argc, argv);
66 // start sensor threads
67 for (int i = 0; i < NUMSENSORS; ++i)
69 Sensor_Init(g_sensors+i, i);
70 pthread_create(&(g_sensors[i].thread), NULL, Sensor_Main, (void*)(g_sensors+i));
73 // run request thread in the main thread
74 FCGI_RequestLoop(NULL);