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

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