Use experiment folders instead of chdir
[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 // --- Variable definitions --- //
21 Options g_options; // options passed to program through command line arguments
22
23 // --- Function definitions --- //
24
25 /**
26  * Parse command line arguments, initialise g_options
27  * @param argc - Number of arguments
28  * @param argv - Array of argument strings
29  */
30 void ParseArguments(int argc, char ** argv)
31 {
32         g_options.program = argv[0]; // program name
33         g_options.verbosity = LOGDEBUG; // default log level
34         // Set the main directory
35         //if (getcwd(g_options.root_dir, sizeof(g_options.root_dir)) == NULL)
36         //      Fatal("Couldn't get current working directory - %s", strerror(errno));
37
38         gettimeofday(&(g_options.start_time), NULL); // Start time
39
40
41         g_options.auth_method = AUTH_NONE;  // Don't use authentication
42         g_options.auth_uri = ""; // 
43         g_options.ldap_base_dn = "";
44         g_options.experiment_dir = ".";
45         
46         for (int i = 1; i < argc; ++i)
47         {
48                 if (argv[i][0] != '-')
49                         Fatal("Unexpected argv[%d] - %s", i, argv[i]);
50
51                 if (i+1 >= argc || argv[i+1][0] == '-')
52                         Fatal("No argument following switch %s", argv[i]);
53                 
54                 if (strlen(argv[i]) > 2)
55                         Fatal("Human readable switches are not supported.");
56
57                 char * end = NULL;
58                 switch (argv[i][1])
59                 {
60                         // Set program verbosity
61                         case 'v':
62                                 g_options.verbosity = strtol(argv[++i], &end, 10);
63                                 break;
64                         // Enable/Disable pin test
65                         case 'p':
66                                 g_options.enable_pin = !(strtol(argv[++i], &end, 10));
67                                 break;
68                         // LDAP URI
69                         case 'A':
70                                 g_options.auth_uri = argv[++i];
71                                 break;
72                         // LDAP DN
73                         case 'd':
74                                 g_options.ldap_base_dn = argv[++i];
75                                 break;
76                         case 'e':
77                         // Experiments directory
78                                 g_options.experiment_dir = argv[++i];
79                                 break;
80                         default:
81                                 Fatal("Unrecognised switch %s", argv[i]);
82                                 break;
83                 }
84
85                 if (end != NULL && *end != '\0')
86                         Fatal("argv[%d] -%c requires an integer (got \"%s\" instead)", i-1, argv[i-1][0], argv[i]);
87         }       
88
89         Log(LOGDEBUG, "Verbosity: %d", g_options.verbosity);
90         Log(LOGDEBUG, "Pin Module Enabled: %d", g_options.enable_pin);
91         Log(LOGDEBUG, "Auth URI: %s", g_options.auth_uri);
92         Log(LOGDEBUG, "LDAP Base DN: %s", g_options.ldap_base_dn);
93         //Log(LOGDEBUG, "Root directory: %s", g_options.root_dir);
94         Log(LOGDEBUG, "Experiment directory: %s", g_options.experiment_dir);
95
96         if (!DirExists(g_options.experiment_dir))
97         {
98                 Fatal("Experiment directory '%s' does not exist.", g_options.experiment_dir);
99         }
100
101         if (g_options.auth_uri[0] != '\0')
102         {
103                 //HACK...
104                 if (PathExists(g_options.auth_uri))
105                         g_options.auth_method = AUTH_SHADOW;
106                 else
107                         g_options.auth_method = AUTH_LDAP;
108         }
109         
110 }
111
112 /**
113  * Cleanup before the program exits
114  */
115 void Cleanup()
116 {
117         Log(LOGDEBUG, "Begin cleanup.");
118         Sensor_Cleanup();
119         //Actuator_Cleanup();
120         Log(LOGDEBUG, "Finish cleanup.");
121 }
122
123 /**
124  * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
125  * @param argc - Num args
126  * @param argv - Args
127  * @returns 0 on success, error code on failure
128  * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
129  */
130 int main(int argc, char ** argv)
131 {
132         // Open log before calling ParseArguments (since ParseArguments may call the Log functions)
133         openlog("mctxserv", LOG_PID | LOG_PERROR, LOG_USER);
134         Log(LOGINFO, "Server started");
135
136         ParseArguments(argc, argv); // Setup the g_options structure from program arguments
137
138         Sensor_Init();
139         Actuator_Init();
140         Pin_Init();
141         
142         // Try and start things
143         /*
144         const char *ret;
145         if ((ret = Control_SetMode(CONTROL_START, "test")) != NULL)
146                 Fatal("Control_SetMode failed with '%s'", ret);
147         */
148
149         // run request thread in the main thread
150         FCGI_RequestLoop(NULL);
151
152         /*
153         if ((ret = Control_SetMode(CONTROL_STOP, "test")) != NULL)
154                 Fatal("Control_SetMode failed with '%s'", ret);
155         */
156         //Sensor_StopAll();
157         //Actuator_StopAll();
158
159         Pin_Close();
160
161         Cleanup();
162         return 0;
163 }
164
165

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