Work on Authentication System(s)
[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 #include "pin_test.h"
13 #include "bbb_pin_defines.h"
14
15 // --- Standard headers --- //
16 #include <syslog.h> // for system logging
17 #include <signal.h> // for signal handling
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
32
33         g_options.program = argv[0]; // program name
34         g_options.verbosity = LOGDEBUG; // default log level
35         gettimeofday(&(g_options.start_time), NULL); // Start time
36
37
38         g_options.auth_method = AUTH_NONE;  // Don't use authentication
39         g_options.auth_uri = ""; // 
40         g_options.ldap_base_dn = "";
41         
42
43
44         for (int i = 1; i < argc; ++i)
45         {
46                 if (argv[i][0] != '-')
47                         Fatal("Unexpected argv[%d] - %s", i, argv[i]);
48
49                 if (i+1 >= argc || argv[i+1][0] == '-')
50                         Fatal("No argument following switch %s", argv[i]);
51                 
52                 if (strlen(argv[i]) > 2)
53                         Fatal("Human readable switches are not supported.");
54
55                 char * end = NULL;
56                 switch (argv[i][1])
57                 {
58                         // Set program verbosity
59                         case 'v':
60                                 g_options.verbosity = strtol(argv[++i], &end, 10);
61                                 break;
62                         // Enable/Disable pin test
63                         case 'p':
64                                 g_options.enable_pin = !(strtol(argv[++i], &end, 10));
65                                 break;
66                         // LDAP URI
67                         case 'A':
68                                 g_options.auth_uri = argv[++i];
69                                 break;
70                         // LDAP DN
71                         case 'd':
72                                 g_options.ldap_base_dn = argv[++i];
73                                 break;
74                         default:
75                                 Fatal("Unrecognised switch %s", argv[i]);
76                                 break;
77                 }
78
79                 if (end != NULL && *end != '\0')
80                         Fatal("argv[%d] -%c requires an integer (got \"%s\" instead)", i-1, argv[i-1][0], argv[i]);
81         }       
82
83         Log(LOGDEBUG, "Verbosity: %d", g_options.verbosity);
84         Log(LOGDEBUG, "Pin Module Enabled: %d", g_options.enable_pin);
85         Log(LOGDEBUG, "Auth URI: %s", g_options.auth_uri);
86         Log(LOGDEBUG, "LDAP Base DN: %s", g_options.ldap_base_dn);
87
88         if (g_options.auth_uri[0] != '\0')
89         {
90                 //HACK...
91                 if (PathExists(g_options.auth_uri))
92                         g_options.auth_method = AUTH_SHADOW;
93                 else
94                         g_options.auth_method = AUTH_LDAP;
95         }
96         
97 }
98
99 /**
100  * Handle a signal
101  * @param signal - The signal number
102  */
103 //TODO: Something that gets massively annoying with threads is that you can't predict which one gets the signal
104 // There are ways to deal with this, but I can't remember them
105 // Probably sufficient to just call Thread_QuitProgram here
106 void SignalHandler(int signal)
107 {
108         // At the moment just always exit.
109         // Call `exit` so that Cleanup will be called to... clean up.
110         Log(LOGWARN, "Got signal %d (%s). Exiting.", signal, strsignal(signal));
111
112         //exit(signal);
113 }
114
115 /**
116  * Cleanup before the program exits
117  */
118 void Cleanup()
119 {
120         Log(LOGDEBUG, "Begin cleanup.");
121         Log(LOGDEBUG, "Finish cleanup.");
122
123 }
124
125 /**
126  * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
127  * @param argc - Num args
128  * @param argv - Args
129  * @returns 0 on success, error code on failure
130  * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
131  */
132 int main(int argc, char ** argv)
133 {
134         // Open log before calling ParseArguments (since ParseArguments may call the Log functions)
135         openlog("mctxserv", LOG_PID | LOG_PERROR, LOG_USER);
136         Log(LOGINFO, "Server started");
137
138         ParseArguments(argc, argv);
139
140         //Open the system log
141
142         // signal handler
143         //TODO: Make this work
144         /*
145         int signals[] = {SIGINT, SIGSEGV, SIGTERM};
146         for (int i = 0; i < sizeof(signals)/sizeof(int); ++i)
147         {
148                 signal(signals[i], SignalHandler);
149         }
150         */
151         Sensor_Init();
152         Actuator_Init();
153         Pin_Init();
154         //Sensor_StartAll("test");
155         //Actuator_StartAll("test");
156         const char *ret;
157         if ((ret = Control_SetMode(CONTROL_START, "test")) != NULL)
158                 Fatal("Control_SetMode failed with '%s'", ret);
159
160         // run request thread in the main thread
161         FCGI_RequestLoop(NULL);
162
163         if ((ret = Control_SetMode(CONTROL_STOP, "test")) != NULL)
164                 Fatal("Control_SetMode failed with '%s'", ret);
165         //Sensor_StopAll();
166         //Actuator_StopAll();
167
168         Pin_Close();
169
170         Cleanup();
171         return 0;
172 }
173
174

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