Switch to syslog for logging messages.
[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 #include "actuator.h"
11 #include "control.h"
12
13 // --- Standard headers --- //
14 #include <syslog.h> // for system logging
15 #include <signal.h> // for signal handling
16
17 // --- Variable definitions --- //
18 Options g_options; // options passed to program through command line arguments
19
20 // --- Function definitions --- //
21
22 /**
23  * Parse command line arguments, initialise g_options
24  * @param argc - Number of arguments
25  * @param argv - Array of argument strings
26  */
27 void ParseArguments(int argc, char ** argv)
28 {
29         g_options.program = argv[0]; // program name
30         g_options.verbosity = LOGDEBUG; // default log level
31         gettimeofday(&(g_options.start_time), NULL); // Start time
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 // Probably sufficient to just call Thread_QuitProgram here
42 void SignalHandler(int signal)
43 {
44         // At the moment just always exit.
45         // Call `exit` so that Cleanup will be called to... clean up.
46         Log(LOGWARN, "Got signal %d (%s). Exiting.", signal, strsignal(signal));
47
48         //exit(signal);
49 }
50
51 /**
52  * Cleanup before the program exits
53  */
54 void Cleanup()
55 {
56         Log(LOGDEBUG, "Begin cleanup.");
57         Log(LOGDEBUG, "Finish cleanup.");
58
59 }
60
61 /**
62  * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
63  * @param argc - Num args
64  * @param argv - Args
65  * @returns 0 on success, error code on failure
66  * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
67  */
68 int main(int argc, char ** argv)
69 {
70         ParseArguments(argc, argv);
71
72         //Open the system log
73         openlog("mctxserv", LOG_PID | LOG_PERROR, LOG_USER);
74         Log(LOGINFO, "Server started");
75         // signal handler
76         //TODO: Make this work
77         /*
78         int signals[] = {SIGINT, SIGSEGV, SIGTERM};
79         for (int i = 0; i < sizeof(signals)/sizeof(int); ++i)
80         {
81                 signal(signals[i], SignalHandler);
82         }
83         */
84         Sensor_Init();
85         Actuator_Init();
86         //Sensor_StartAll("test");
87         //Actuator_StartAll("test");
88         const char *ret;
89         if ((ret = Control_SetMode(CONTROL_START, "test")) != NULL)
90                 Fatal("Control_SetMode failed with '%s'", ret);
91
92         // run request thread in the main thread
93         FCGI_RequestLoop(NULL);
94
95         if ((ret = Control_SetMode(CONTROL_STOP, "test")) != NULL)
96                 Fatal("Control_SetMode failed with '%s'", ret);
97         //Sensor_StopAll();
98         //Actuator_StopAll();
99
100         Cleanup();
101         return 0;
102 }
103
104

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