edited dilatometer
[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
20 #ifdef REALTIME_VERSION
21 #include <time.h>
22 #include <sched.h>
23 #include <sys/mman.h>
24 #include <sys/utsname.h>
25 #endif //REALTIME_VERSION
26
27 // --- Variable definitions --- //
28 Options g_options; // options passed to program through command line arguments
29
30 // --- Function definitions --- //
31
32 /**
33  * Parse command line arguments, initialise g_options
34  * @param argc - Number of arguments
35  * @param argv - Array of argument strings
36  */
37 void ParseArguments(int argc, char ** argv)
38 {
39         g_options.program = argv[0]; // program name
40         g_options.verbosity = LOGDEBUG; // default log level
41         // Set the main directory
42         //if (getcwd(g_options.root_dir, sizeof(g_options.root_dir)) == NULL)
43         //      Fatal("Couldn't get current working directory - %s", strerror(errno));
44
45         clock_gettime(CLOCK_MONOTONIC, &(g_options.start_time)); // Start time
46
47
48         g_options.auth_method = AUTH_NONE;  // Don't use authentication
49         g_options.auth_uri = ""; // 
50         g_options.auth_options = "";
51         g_options.experiment_dir = ".";
52         
53         for (int i = 1; i < argc; ++i)
54         {
55                 if (argv[i][0] != '-')
56                         Fatal("Unexpected argv[%d] - %s", i, argv[i]);
57
58                 if (i+1 >= argc || argv[i+1][0] == '-')
59                         Fatal("No argument following switch %s", argv[i]);
60                 
61                 if (strlen(argv[i]) > 2)
62                         Fatal("Human readable switches are not supported.");
63
64                 char * end = NULL;
65                 switch (argv[i][1])
66                 {
67                         // Set program verbosity
68                         case 'v':
69                                 g_options.verbosity = strtol(argv[++i], &end, 10);
70                                 break;
71                         // Enable/Disable pin test
72                         case 'p':
73                                 g_options.enable_pin = !(strtol(argv[++i], &end, 10));
74                                 break;
75                         // Authentication URI and options
76                         case 'A':
77                                 g_options.auth_uri = argv[++i];
78                                 break;
79                         case 'e':
80                         // Experiments directory
81                                 g_options.experiment_dir = argv[++i];
82                                 break;
83                         default:
84                                 Fatal("Unrecognised switch %s", argv[i]);
85                                 break;
86                 }
87
88                 if (end != NULL && *end != '\0')
89                         Fatal("argv[%d] -%c requires an integer (got \"%s\" instead)", i-1, argv[i-1][0], argv[i]);
90         }       
91
92
93
94         if (!DirExists(g_options.experiment_dir))
95         {
96                 Fatal("Experiment directory '%s' does not exist.", g_options.experiment_dir);
97         }
98
99         if (g_options.auth_uri[0] != '\0')
100         {
101                 // Get the options part of the URI if it exists
102                 char * c = (char*)g_options.auth_uri;
103                 while (*(++c) != '\0' && *c != '#');
104                 
105                 if (*(c++) == '#')
106                 {
107                         *(c-1) = '\0';
108                         g_options.auth_options = c;
109                 }
110
111                 // Use the first part of the URI to identify the protocol:
112                 c = (char*)g_options.auth_uri;
113                 while (*(++c) != '\0' && *c != ':');
114
115                 if (*c == '\0') // No ':' means no protocol; use plaintext file
116                 {
117                         g_options.auth_method = AUTH_SHADOW;                    
118                 }
119                 else if (*c == ':' && *(c+1) == '/' && *(c+2) == '/')
120                 {
121                         
122                         *c = '\0';
123                         if (strcmp(g_options.auth_uri, "ldap") == 0 || strcmp(g_options.auth_uri, "ldaps") == 0)
124                         {
125                                 *c = ':'; // LDAP URI's require the prodocol as part of the string
126                                 g_options.auth_method = AUTH_LDAP;
127                         }
128                         else if (strcmp(g_options.auth_uri, "mysql") == 0)
129                         {
130                                 g_options.auth_uri = c+3; // MySQL doesn't (just a hostname)
131                                 g_options.auth_method = AUTH_MYSQL;
132                         }
133                         else
134                         {
135                                 Fatal("Unsupported authentication method %s", g_options.auth_uri);
136                         }
137                 }
138         }
139         else
140         {
141                 Log(LOGWARN, "No authentication method.");
142         }
143
144         Log(LOGDEBUG, "Verbosity: %d", g_options.verbosity);
145         Log(LOGDEBUG, "Pin Module Enabled: %d", g_options.enable_pin);
146         Log(LOGDEBUG, "Auth method: %d", g_options.auth_method);
147         Log(LOGDEBUG, "Auth URI: %s", g_options.auth_uri);
148         Log(LOGDEBUG, "Auth Options: %s", g_options.auth_options);
149         //Log(LOGDEBUG, "Root directory: %s", g_options.root_dir);
150         Log(LOGDEBUG, "Experiment directory: %s", g_options.experiment_dir);
151
152
153         
154 }
155
156 /**
157  * Cleanup before the program exits
158  */
159 void Cleanup()
160 {
161         Log(LOGDEBUG, "Begin cleanup.");
162         Sensor_Cleanup();
163         Actuator_Cleanup();
164         Log(LOGDEBUG, "Finish cleanup.");
165 }
166
167
168 #ifdef REALTIME_VERSION
169
170 #define MAX_SAFE_STACK (8*1024)
171 #define NSEC_PER_SEC (1000000000)
172
173 void stack_prefault()
174 {
175         unsigned char dummy[MAX_SAFE_STACK];
176         memset(dummy, 0, MAX_SAFE_STACK);
177         return;
178 }
179
180 bool is_realtime()
181 {
182         struct utsname u;
183         bool crit1 = 0;
184         bool crit2 = 0;
185         FILE * f;
186         uname(&u);
187         crit1 = (strcasestr(u.version, "PREEMPT RT") != NULL);
188         if (crit1 && ((f = fopen("/sys/kernel/realtime", "r")) != NULL))
189         {
190                 int flag;
191                 crit2 = ((fscanf(f, "%d", &flag) == 1) && (flag == 1));
192                 fclose(f);
193         }
194         return (crit1 && crit2);
195 }
196
197 #endif //REALTIME_VERSION
198
199 /**
200  * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
201  * @param argc - Num args
202  * @param argv - Args
203  * @returns 0 on success, error code on failure
204  * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
205  */
206 int main(int argc, char ** argv)
207 {
208
209         // Open log before calling ParseArguments (since ParseArguments may call the Log functions)
210         openlog("mctxserv", LOG_PID | LOG_PERROR, LOG_USER);
211
212         ParseArguments(argc, argv); // Setup the g_options structure from program arguments
213
214         Log(LOGINFO, "Server started");
215
216
217         
218         #ifdef REALTIME_VERSION
219         
220         if (is_realtime())
221         {
222                 Log(LOGDEBUG, "Running under realtime kernel");
223         }
224         else
225         {
226                 Fatal("Not running under realtime kernel");
227         }
228         struct sched_param param;
229         param.sched_priority = 49;
230         if (sched_setscheduler(0, SCHED_FIFO, &param) < 0)
231                 Fatal("sched_setscheduler failed - %s", strerror(errno));
232         if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
233                 Fatal("mlockall failed - %s", strerror(errno));
234         stack_prefault();
235         #endif //REALTIME_VERSION
236
237         
238
239         Pin_Init();
240         
241         // Try and start things
242         
243         //const char *ret;
244         //if ((ret = Control_SetMode(CONTROL_START, "test")) != NULL)
245         //      Fatal("Control_SetMode failed with '%s'", ret);
246         
247
248         // run request thread in the main thread
249         FCGI_RequestLoop(NULL);
250
251         
252         Control_SetMode(CONTROL_STOP, NULL);
253         //if ((ret = Control_SetMode(CONTROL_STOP, "test")) != NULL)
254         //      Fatal("Control_SetMode failed with '%s'", ret);
255         
256         //Sensor_StopAll();
257         //Actuator_StopAll();
258
259         Pin_Close();
260
261         Cleanup();
262         return 0;
263 }
264
265

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