3 * @purpose main and its helper functions, signal handling and cleanup functions
8 // --- Standard headers --- //
9 #include <signal.h> // for signal handling
11 // --- Custom headers --- //
17 // --- Variable definitions --- //
18 Options g_options; // options passed to program through command line arguments
19 Sensor g_sensors[NUMSENSORS]; // sensors array
21 // --- Function definitions --- //
24 * Parse command line arguments, initialise g_options
25 * @param argc - Number of arguments
26 * @param argv - Array of argument strings
28 void ParseArguments(int argc, char ** argv)
30 g_options.program = argv[0]; // program name
31 g_options.verbosity = LOGDEBUG; // default log level
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 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));
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
65 int main(int argc, char ** argv)
67 ParseArguments(argc, argv);
69 // start sensor threads
70 for (int i = 0; i < NUMSENSORS; ++i)
72 Sensor_Init(g_sensors+i, i);
73 pthread_create(&(g_sensors[i].thread), NULL, Sensor_Main, (void*)(g_sensors+i));
76 // run request thread in the main thread
77 Query_Request(NULL); //TODO: Replace with FastCGI code