Added support for multi-router registration
[OpenSRF.git] / src / libopensrf / osrf_system.c
1 #include <opensrf/osrf_system.h>
2 #include <opensrf/osrf_application.h>
3 #include <opensrf/osrf_prefork.h>
4 #include <signal.h>
5
6 static void report_child_status( pid_t pid, int status );
7 struct child_node;
8 typedef struct child_node ChildNode;
9
10 struct child_node
11 {
12         ChildNode* pNext;
13         ChildNode* pPrev;
14         pid_t pid;
15         char* app;
16         char* libfile;
17 };
18
19 static ChildNode* child_list;
20
21 static transport_client* osrfGlobalTransportClient = NULL;
22
23 static void add_child( pid_t pid, const char* app, const char* libfile );
24 static void delete_child( ChildNode* node );
25 static void delete_all_children( void );
26 static ChildNode* seek_child( pid_t pid );
27
28 transport_client* osrfSystemGetTransportClient( void ) {
29         return osrfGlobalTransportClient;
30 }
31
32 void osrfSystemIgnoreTransportClient() {
33         osrfGlobalTransportClient = NULL;
34 }
35
36 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
37         return osrfSystemBootstrapClientResc(config_file, contextnode, NULL);
38 }
39
40 int osrfSystemInitCache( void ) {
41
42         jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
43         char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
44
45         if( cacheServers && maxCache) {
46
47                 if( cacheServers->type == JSON_ARRAY ) {
48                         int i;
49                         const char* servers[cacheServers->size];
50                         for( i = 0; i != cacheServers->size; i++ ) {
51                                 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
52                                 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[i]);
53                         }
54                         osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
55
56                 } else {
57                         const char* servers[] = { jsonObjectGetString(cacheServers) };          
58                         osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
59                         osrfCacheInit( servers, 1, atoi(maxCache) );
60                 }
61
62         } else {
63                 osrfLogError( OSRF_LOG_MARK,  "Missing config value for /cache/global/servers/server _or_ "
64                         "/cache/global/max_cache_time");
65         }
66
67         return 0;
68 }
69
70
71 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
72         if( !(hostname && configfile && contextNode) ) return -1;
73
74         /* first we grab the settings */
75         if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
76                 osrfLogError( OSRF_LOG_MARK,
77                         "Unable to bootstrap for host %s from configuration file %s",
78                         hostname, configfile );
79                 return -1;
80         }
81
82         int retcode = osrf_settings_retrieve(hostname);
83         osrf_system_disconnect_client();
84
85         if( retcode ) {
86                 osrfLogError( OSRF_LOG_MARK,
87                         "Unable to retrieve settings for host %s from configuration file %s",
88                         hostname, configfile );
89                 return -1;
90         }
91
92         /** daemonize me **/
93         /* background and let our children do their thing */
94         /* NOTE: This has been moved from below the 'if (apps)' block below ... move it back if things go crazy */
95         daemonize();
96
97         jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
98         osrfStringArray* arr = osrfNewStringArray(8);
99         
100         if(apps) {
101                 int i = 0;
102
103                 if(apps->type == JSON_STRING) {
104                         osrfStringArrayAdd(arr, jsonObjectGetString(apps));
105
106                 } else {
107                         const jsonObject* app;
108                         while( (app = jsonObjectGetIndex(apps, i++)) ) 
109                                 osrfStringArrayAdd(arr, jsonObjectGetString(app));
110                 }
111
112                 char* appname = NULL;
113                 i = 0;
114                 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
115
116                         char* lang = osrf_settings_host_value("/apps/%s/language", appname);
117
118                         if(lang && !strcasecmp(lang,"c"))  {
119
120                                 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
121                 
122                                 if(! (appname && libfile) ) {
123                                         osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
124                                         continue;
125                                 }
126
127                                 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
128                 
129                                 pid_t pid;
130                 
131                                 if( (pid = fork()) ) { 
132                                         // store pid in local list for re-launching dead children...
133                                         add_child( pid, appname, libfile );
134                                         osrfLogInfo( OSRF_LOG_MARK, "Running application child %s: process id %ld",
135                                                                  appname, (long) pid );
136         
137                                 } else {
138                 
139                                         osrfLogInfo( OSRF_LOG_MARK, " * Running application %s\n", appname);
140                                         if( osrfAppRegisterApplication( appname, libfile ) == 0 ) 
141                                                 osrf_prefork_run(appname);
142         
143                                         osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
144                                         exit(0);
145                                 }
146                         } // language == c
147                 } 
148         } // should we do something if there are no apps? does the wait(NULL) below do that for us?
149
150         osrfStringArrayFree(arr);
151         
152         while(1) {
153                 errno = 0;
154                 int status;
155                 pid_t pid = wait( &status );
156                 if(-1 == pid) {
157                         if(errno == ECHILD)
158                                 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
159                         else
160                                 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
161                         break;
162                 } else {
163                         report_child_status( pid, status );
164                 }
165         }
166
167         delete_all_children();
168         return 0;
169 }
170
171
172 static void report_child_status( pid_t pid, int status )
173 {
174         const char* app;
175         const char* libfile;
176         ChildNode* node = seek_child( pid );
177
178         if( node ) {
179                 app     = node->app     ? node->app     : "[unknown]";
180                 libfile = node->libfile ? node->libfile : "[none]";
181         } else
182                 app = libfile = NULL;
183         
184         if( WIFEXITED( status ) )
185         {
186                 int rc = WEXITSTATUS( status );  // return code of child process
187                 if( rc )
188                         osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) exited with return code %d",
189                                                   (long) pid, app, rc );
190                 else
191                         osrfLogInfo( OSRF_LOG_MARK, "Child process %ld (app %s) exited normally",
192                                                   (long) pid, app );
193         }
194         else if( WIFSIGNALED( status ) )
195         {
196                 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) killed by signal %d",
197                                           (long) pid, app, WTERMSIG( status) );
198         }
199         else if( WIFSTOPPED( status ) )
200         {
201                 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) stopped by signal %d",
202                                           (long) pid, app, (int) WSTOPSIG( status ) );
203         }
204
205         delete_child( node );
206 }
207
208 /*----------- Routines to manage list of children --*/
209
210 static void add_child( pid_t pid, const char* app, const char* libfile )
211 {
212         /* Construct new child node */
213         
214         ChildNode* node = safe_malloc( sizeof( ChildNode ) );
215
216         node->pid = pid;
217
218         if( app )
219                 node->app = strdup( app );
220         else
221                 node->app = NULL;
222
223         if( libfile )
224                 node->libfile = strdup( libfile );
225         else
226                 node->libfile = NULL;
227         
228         /* Add new child node to the head of the list */
229
230         node->pNext = child_list;
231         node->pPrev = NULL;
232
233         if( child_list )
234                 child_list->pPrev = node;
235
236         child_list = node;
237 }
238
239 static void delete_child( ChildNode* node ) {
240
241         /* Sanity check */
242
243         if( ! node )
244                 return;
245         
246         /* Detach the node from the list */
247
248         if( node->pPrev )
249                 node->pPrev->pNext = node->pNext;
250         else
251                 child_list = node->pNext;
252
253         if( node->pNext )
254                 node->pNext->pPrev = node->pPrev;
255
256         /* Deallocate the node and its payload */
257
258         free( node->app );
259         free( node->libfile );
260         free( node );
261 }
262
263 static void delete_all_children( void ) {
264
265         while( child_list )
266                 delete_child( child_list );
267 }
268
269 static ChildNode* seek_child( pid_t pid ) {
270
271         /* Return a pointer to the child node for the */
272         /* specified process ID, or NULL if not found */
273         
274         ChildNode* node = child_list;
275         while( node ) {
276                 if( node->pid == pid )
277                         break;
278                 else
279                         node = node->pNext;
280         }
281
282         return node;
283 }
284
285 /*----------- End of routines to manage list of children --*/
286
287
288 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
289
290         int failure = 0;
291
292         if(osrfSystemGetTransportClient()) {
293                 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
294                 return 1; /* we already have a client connection */
295         }
296
297         if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
298                 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
299                 return -1;
300         }
301
302         if( config_file ) {
303                 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
304                 if(cfg)
305                         osrfConfigSetDefaultConfig(cfg);
306                 else
307                         return 0;   /* Can't load configuration?  Bail out */
308         }
309
310
311         char* log_file          = osrfConfigGetValue( NULL, "/logfile");
312         if(!log_file) {
313                 fprintf(stderr, "No log file specified in configuration file %s\n",
314                                 config_file);
315                 return -1;
316         }
317
318         char* log_level         = osrfConfigGetValue( NULL, "/loglevel" );
319         osrfStringArray* arr    = osrfNewStringArray(8);
320         osrfConfigGetValueList(NULL, arr, "/domain");
321
322         char* username          = osrfConfigGetValue( NULL, "/username" );
323         char* password          = osrfConfigGetValue( NULL, "/passwd" );
324         char* port              = osrfConfigGetValue( NULL, "/port" );
325         char* unixpath          = osrfConfigGetValue( NULL, "/unixpath" );
326         char* facility          = osrfConfigGetValue( NULL, "/syslog" );
327         char* actlog            = osrfConfigGetValue( NULL, "/actlog" );
328
329         /* if we're a source-client, tell the logger */
330         char* isclient = osrfConfigGetValue(NULL, "/client");
331         if( isclient && !strcasecmp(isclient,"true") )
332                 osrfLogSetIsClient(1);
333         free(isclient);
334
335         int llevel = 0;
336         int iport = 0;
337         if(port) iport = atoi(port);
338         if(log_level) llevel = atoi(log_level);
339
340         if(!strcmp(log_file, "syslog")) {
341                 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
342                 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
343                 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
344
345         } else {
346                 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
347                 osrfLogSetFile( log_file );
348         }
349
350
351         /* Get a domain, if one is specified */
352         const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
353         if(!domain) {
354                 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
355                 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
356                 failure = 1;
357         }
358
359         if(!username) {
360                 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
361                 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
362                 failure = 1;
363         }
364
365         if(!password) {
366                 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
367                 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
368                 failure = 1;
369         }
370
371         if((iport <= 0) && !unixpath) {
372                 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
373                 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
374                         config_file);
375                 failure = 1;
376         }
377
378         if (failure) {
379                 osrfStringArrayFree(arr);
380                 free(log_level);
381                 free(username);
382                 free(password);
383                 free(port);
384                 free(unixpath);
385                 free(facility);
386                 free(actlog);
387                 return 0;
388         }
389
390         osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
391                 domain, iport, unixpath ? unixpath : "(none)" );
392         transport_client* client = client_init( domain, iport, unixpath, 0 );
393
394         const char* host;
395         host = getenv("HOSTNAME");
396
397         char tbuf[32];
398         tbuf[0] = '\0';
399         snprintf(tbuf, 32, "%f", get_timestamp_millis());
400
401         if(!host) host = "";
402         if(!resource) resource = "";
403
404         int len = strlen(resource) + 256;
405         char buf[len];
406         buf[0] = '\0';
407         snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
408
409         if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
410                 /* child nodes will leak the parents client... but we can't free
411                         it without disconnecting the parents client :( */
412                 osrfGlobalTransportClient = client;
413         }
414
415         osrfStringArrayFree(arr);
416         free(actlog);
417         free(facility);
418         free(log_level);
419         free(log_file);
420         free(username);
421         free(password);
422         free(port);     
423         free(unixpath);
424
425         if(osrfGlobalTransportClient)
426                 return 1;
427
428         return 0;
429 }
430
431 int osrf_system_disconnect_client( void ) {
432         client_disconnect( osrfGlobalTransportClient );
433         client_free( osrfGlobalTransportClient );
434         osrfGlobalTransportClient = NULL;
435         return 0;
436 }
437
438 int osrf_system_shutdown( void ) {
439         osrfConfigCleanup();
440     osrfCacheCleanup();
441         osrf_system_disconnect_client();
442         osrf_settings_free_host_config(NULL);
443         osrfAppSessionCleanup();
444         osrfLogCleanup();
445         return 1;
446 }
447
448
449
450