Update FastCGI code and restructure includes a bit
[matches/MCTX3420.git] / server / main.c
1 /**
2  * @file main.c
3  * @purpose 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 Sensor g_sensors[NUMSENSORS]; // sensors array
17
18 // --- Function definitions --- //
19
20 /**
21  * Parse command line arguments, initialise g_options
22  * @param argc - Number of arguments
23  * @param argv - Array of argument strings
24  */
25 void ParseArguments(int argc, char ** argv)
26 {
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);
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 void SignalHandler(int signal)
39 {
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));
43         exit(signal);
44 }
45
46 /**
47  * Cleanup before the program exits
48  */
49 void Cleanup()
50 {
51         Log(LOGDEBUG, "Begin cleanup.");
52         Log(LOGDEBUG, "Finish cleanup.");
53
54 }
55
56 /**
57  * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
58  * @param argc - Num args
59  * @param argv - Args
60  * @returns 0 on success, error code on failure
61  */
62 int main(int argc, char ** argv)
63 {
64         ParseArguments(argc, argv);
65
66         // start sensor threads
67         for (int i = 0; i < NUMSENSORS; ++i)
68         {
69                 Sensor_Init(g_sensors+i, i);
70                 pthread_create(&(g_sensors[i].thread), NULL, Sensor_Main, (void*)(g_sensors+i));
71         }
72
73         // run request thread in the main thread
74         FCGI_RequestLoop(NULL);
75         return 0;
76 }
77
78

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