3 * @brief 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
17 // --- Function definitions --- //
20 * Parse command line arguments, initialise g_options
21 * @param argc - Number of arguments
22 * @param argv - Array of argument strings
24 void ParseArguments(int argc, char ** argv)
26 g_options.program = argv[0]; // program name
27 g_options.verbosity = LOGDEBUG; // default log level
28 gettimeofday(&(g_options.start_time), NULL); // Start time
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 // Probably sufficient to just call Thread_QuitProgram here
39 void SignalHandler(int signal)
41 // At the moment just always exit.
42 // Call `exit` so that Cleanup will be called to... clean up.
43 Log(LOGWARN, "Got signal %d (%s). Exiting.", signal, strsignal(signal));
49 * Cleanup before the program exits
53 Log(LOGDEBUG, "Begin cleanup.");
54 Log(LOGDEBUG, "Finish cleanup.");
59 * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
60 * @param argc - Num args
62 * @returns 0 on success, error code on failure
63 * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
65 int main(int argc, char ** argv)
67 ParseArguments(argc, argv);
70 //TODO: Make this work
72 int signals[] = {SIGINT, SIGSEGV, SIGTERM};
73 for (int i = 0; i < sizeof(signals)/sizeof(int); ++i)
75 signal(signals[i], SignalHandler);
79 Sensor_StartAll("test");
81 // run request thread in the main thread
82 FCGI_RequestLoop(NULL);