Change @purpose to @brief (Doxygen warning) and work on unit tests
[matches/MCTX3420.git] / server / main.c
1 /**
2  * @file main.c
3  * @brief main and its helper functions, signal handling and cleanup functions
4  */
5
6 // --- Custom headers --- //
7 #include "common.h"
8 #include "options.h"
9 #include "sensor.h"
10
11 // --- Standard headers --- //
12 #include <signal.h> // for signal handling
13
14 // --- Variable definitions --- //
15 Options g_options; // options passed to program through command line arguments
16
17 // --- Function definitions --- //
18
19 /**
20  * Parse command line arguments, initialise g_options
21  * @param argc - Number of arguments
22  * @param argv - Array of argument strings
23  */
24 void ParseArguments(int argc, char ** argv)
25 {
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);
30 }
31
32 /**
33  * Handle a signal
34  * @param signal - The signal number
35  */
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)
40 {
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));
44         Thread_QuitProgram(false);
45         //exit(signal);
46 }
47
48 /**
49  * Cleanup before the program exits
50  */
51 void Cleanup()
52 {
53         Log(LOGDEBUG, "Begin cleanup.");
54         Log(LOGDEBUG, "Finish cleanup.");
55
56 }
57
58 /**
59  * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
60  * @param argc - Num args
61  * @param argv - Args
62  * @returns 0 on success, error code on failure
63  * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
64  */
65 int main(int argc, char ** argv)
66 {
67         ParseArguments(argc, argv);
68
69         // signal handler
70         //TODO: Make this work
71         /*
72         int signals[] = {SIGINT, SIGSEGV, SIGTERM};
73         for (int i = 0; i < sizeof(signals)/sizeof(int); ++i)
74         {
75                 signal(signals[i], SignalHandler);
76         }
77         */
78         Sensor_Spawn();
79
80         // run request thread in the main thread
81         FCGI_RequestLoop(NULL);
82
83         // Join the dark side, Luke
84         // *cough*
85         // Join the sensor threads
86         Sensor_Join();
87         Cleanup();
88         return 0;
89 }
90
91

UCC git Repository :: git.ucc.asn.au