1 #include <opensrf/osrf_system.h>
2 #include <opensrf/osrf_application.h>
3 #include <opensrf/osrf_prefork.h>
7 #define HOST_NAME_MAX 256
10 static void report_child_status( pid_t pid, int status );
12 typedef struct child_node ChildNode;
14 static void handleKillSignal(int signo) {
15 /* we are the top-level process and we've been
16 * killed. Kill all of our children */
18 sleep(1); /* give the children a chance to die before we reap them */
21 while( (child_pid=waitpid(-1,&status,WNOHANG)) > 0)
22 osrfLogInfo(OSRF_LOG_MARK, "Killed child %d", child_pid);
36 static ChildNode* child_list;
38 static transport_client* osrfGlobalTransportClient = NULL;
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 );
45 transport_client* osrfSystemGetTransportClient( void ) {
46 return osrfGlobalTransportClient;
49 void osrfSystemIgnoreTransportClient() {
50 osrfGlobalTransportClient = NULL;
53 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
54 return osrfSystemBootstrapClientResc(config_file, contextnode, NULL);
57 int osrfSystemInitCache( void ) {
59 jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
60 char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
62 if( cacheServers && maxCache) {
64 if( cacheServers->type == JSON_ARRAY ) {
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]);
71 osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
74 const char* servers[] = { jsonObjectGetString(cacheServers) };
75 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
76 osrfCacheInit( servers, 1, atoi(maxCache) );
80 osrfLogError( OSRF_LOG_MARK, "Missing config value for /cache/global/servers/server _or_ "
81 "/cache/global/max_cache_time");
84 jsonObjectFree( cacheServers );
89 int osrfSystemBootstrap( const char* hostname, const char* configfile,
90 const char* contextNode ) {
91 if( !(hostname && configfile && contextNode) ) return -1;
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 );
101 int retcode = osrf_settings_retrieve(hostname);
102 osrf_system_disconnect_client();
105 osrfLogError( OSRF_LOG_MARK,
106 "Unable to retrieve settings for host %s from configuration file %s",
107 hostname, configfile );
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 */
116 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
117 osrfStringArray* arr = osrfNewStringArray(8);
122 if(apps->type == JSON_STRING) {
123 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
126 const jsonObject* app;
127 while( (app = jsonObjectGetIndex(apps, i++)) )
128 osrfStringArrayAdd(arr, jsonObjectGetString(app));
130 jsonObjectFree(apps);
132 char* appname = NULL;
134 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
136 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
138 if(lang && !strcasecmp(lang,"c")) {
140 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
142 if(! (appname && libfile) ) {
143 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
147 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
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 );
159 osrfLogInfo( OSRF_LOG_MARK, " * Running application %s\n", appname);
160 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
161 osrf_prefork_run(appname);
163 osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
168 } // should we do something if there are no apps? does the wait(NULL) below do that for us?
170 osrfStringArrayFree(arr);
172 signal(SIGTERM, handleKillSignal);
173 signal(SIGINT, handleKillSignal);
178 pid_t pid = wait( &status );
181 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
183 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
186 report_child_status( pid, status );
190 delete_all_children();
195 static void report_child_status( pid_t pid, int status )
199 ChildNode* node = seek_child( pid );
202 app = node->app ? node->app : "[unknown]";
203 libfile = node->libfile ? node->libfile : "[none]";
205 app = libfile = NULL;
207 if( WIFEXITED( status ) )
209 int rc = WEXITSTATUS( status ); // return code of child process
211 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) exited with return code %d",
212 (long) pid, app, rc );
214 osrfLogInfo( OSRF_LOG_MARK, "Child process %ld (app %s) exited normally",
217 else if( WIFSIGNALED( status ) )
219 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) killed by signal %d",
220 (long) pid, app, WTERMSIG( status) );
222 else if( WIFSTOPPED( status ) )
224 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) stopped by signal %d",
225 (long) pid, app, (int) WSTOPSIG( status ) );
228 delete_child( node );
231 /*----------- Routines to manage list of children --*/
233 static void add_child( pid_t pid, const char* app, const char* libfile )
235 /* Construct new child node */
237 ChildNode* node = safe_malloc( sizeof( ChildNode ) );
242 node->app = strdup( app );
247 node->libfile = strdup( libfile );
249 node->libfile = NULL;
251 /* Add new child node to the head of the list */
253 node->pNext = child_list;
257 child_list->pPrev = node;
262 static void delete_child( ChildNode* node ) {
269 /* Detach the node from the list */
272 node->pPrev->pNext = node->pNext;
274 child_list = node->pNext;
277 node->pNext->pPrev = node->pPrev;
279 /* Deallocate the node and its payload */
282 free( node->libfile );
286 static void delete_all_children( void ) {
289 delete_child( child_list );
292 static ChildNode* seek_child( pid_t pid ) {
294 /* Return a pointer to the child node for the */
295 /* specified process ID, or NULL if not found */
297 ChildNode* node = child_list;
299 if( node->pid == pid )
308 /*----------- End of routines to manage list of children --*/
311 int osrfSystemBootstrapClientResc( const char* config_file,
312 const char* contextnode, const char* resource ) {
316 if(osrfSystemGetTransportClient()) {
317 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
318 return 1; /* we already have a client connection */
321 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
322 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
327 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
329 osrfConfigSetDefaultConfig(cfg);
331 return 0; /* Can't load configuration? Bail out */
335 char* log_file = osrfConfigGetValue( NULL, "/logfile");
337 fprintf(stderr, "No log file specified in configuration file %s\n",
342 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
343 osrfStringArray* arr = osrfNewStringArray(8);
344 osrfConfigGetValueList(NULL, arr, "/domain");
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" );
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);
361 if(port) iport = atoi(port);
362 if(log_level) llevel = atoi(log_level);
364 if(!strcmp(log_file, "syslog")) {
365 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
366 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
367 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
370 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
371 osrfLogSetFile( log_file );
375 /* Get a domain, if one is specified */
376 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
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);
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);
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);
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",
403 osrfStringArrayFree(arr);
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 );
419 char host[HOST_NAME_MAX + 1] = "";
420 gethostname(host, sizeof(host) );
421 host[HOST_NAME_MAX] = '\0';
425 snprintf(tbuf, 32, "%f", get_timestamp_millis());
427 if(!resource) resource = "";
429 int len = strlen(resource) + 256;
432 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
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;
440 osrfStringArrayFree(arr);
450 if(osrfGlobalTransportClient)
456 int osrf_system_disconnect_client( void ) {
457 client_disconnect( osrfGlobalTransportClient );
458 client_free( osrfGlobalTransportClient );
459 osrfGlobalTransportClient = NULL;
463 static int shutdownComplete = 0;
464 int osrf_system_shutdown( void ) {
465 if(shutdownComplete) return 0;
468 osrf_system_disconnect_client();
469 osrf_settings_free_host_config(NULL);
470 osrfAppSessionCleanup();
472 shutdownComplete = 1;