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