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