Merge remote-tracking branch 'upstream/master'
[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 #include "common.h"
6
7
8 // --- Standard headers --- //
9 #include <signal.h> // for signal handling
10
11 // --- Custom headers --- //
12 #include "query.h"
13 #include "log.h"
14 #include "options.h"
15 #include "sensor.h"
16
17 // --- Variable definitions --- //
18 Options g_options; // options passed to program through command line arguments
19 Sensor g_sensors[NUMSENSORS]; // sensors array
20
21 // --- Function definitions --- //
22
23 /**
24  * Parse command line arguments, initialise g_options
25  * @param argc - Number of arguments
26  * @param argv - Array of argument strings
27  */
28 void ParseArguments(int argc, char ** argv)
29 {
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);
33 }
34
35 /**
36  * Handle a signal
37  * @param signal - The signal number
38  */
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)
42 {
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));
46         exit(signal);
47 }
48
49 /**
50  * Cleanup before the program exits
51  */
52 void Cleanup()
53 {
54         Log(LOGDEBUG, "Begin cleanup.");
55         Log(LOGDEBUG, "Finish cleanup.");
56
57 }
58
59 /**
60  * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
61  * @param argc - Num args
62  * @param argv - Args
63  * @returns 0 on success, error code on failure
64  */
65 int main(int argc, char ** argv)
66 {
67         ParseArguments(argc, argv);
68
69         // start sensor threads
70         for (int i = 0; i < NUMSENSORS; ++i)
71         {
72                 Sensor_Init(g_sensors+i, i);
73                 pthread_create(&(g_sensors[i].thread), NULL, Sensor_Main, (void*)(g_sensors+i));
74         }
75
76         // run request thread in the main thread
77         Query_Main(NULL); //TODO: Replace with FastCGI code
78         return 0;
79 }
80
81

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