Merge branch 'master' of https://github.com/jtanx/MCTX3420 into jtanx-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
6 #define _POSIX_C_SOURCE 200809L // For strsignal to work
7
8 // --- Standard headers --- //
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <signal.h> // for signal handling
12 #include <string.h> // string functions
13 #include <pthread.h>
14
15 // --- Custom headers --- //
16 #include "log.h"
17 #include "options.h"
18
19 // --- Variable definitions --- //
20 Options g_options; // options passed to program through command line arguments
21
22 // --- Function definitions --- //
23
24 /**
25  * Parse command line arguments, initialise g_options
26  * @param argc - Number of arguments
27  * @param argv - Array of argument strings
28  */
29 void ParseArguments(int argc, char ** argv)
30 {
31         g_options.program = argv[0]; // program name
32         g_options.verbosity = LOGDEBUG; // default log level
33         Log(LOGDEBUG, "Called as %s with %d arguments.", g_options.program, argc);
34 }
35
36 /**
37  * Handle a signal
38  * @param signal - The signal number
39  */
40 void SignalHandler(int signal)
41 {
42         // At the moment just always exit.
43         // Call `exit` so that Cleanup will be called to... clean up.
44         Log(LOGWARN, "Got signal %d (%s). Exiting.", sig, strsignal(sig));
45         exit(sig);
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  */
64 int main(int argc, char ** argv)
65 {
66         ParseArguments(argc, argv, &g_options);
67         return 0;
68 }
69
70

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