1 #include <opensrf/osrf_system.h>
2 #include <opensrf/osrf_application.h>
3 #include <opensrf/osrf_prefork.h>
6 static void report_child_status( pid_t pid, int status );
8 typedef struct child_node ChildNode;
10 static void handleKillSignal(int signo) {
11 /* we are the top-level process and we've been
12 * killed. Kill all of our children */
14 sleep(1); /* give the children a chance to die before we reap them */
17 while( (child_pid=waitpid(-1,&status,WNOHANG)) > 0)
18 osrfLogInfo(OSRF_LOG_MARK, "Killed child %d", child_pid);
32 static ChildNode* child_list;
34 static transport_client* osrfGlobalTransportClient = NULL;
36 static void add_child( pid_t pid, const char* app, const char* libfile );
37 static void delete_child( ChildNode* node );
38 static void delete_all_children( void );
39 static ChildNode* seek_child( pid_t pid );
41 transport_client* osrfSystemGetTransportClient( void ) {
42 return osrfGlobalTransportClient;
45 void osrfSystemIgnoreTransportClient() {
46 osrfGlobalTransportClient = NULL;
49 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
50 return osrfSystemBootstrapClientResc(config_file, contextnode, NULL);
53 int osrfSystemInitCache( void ) {
55 jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
56 char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
58 if( cacheServers && maxCache) {
60 if( cacheServers->type == JSON_ARRAY ) {
62 const char* servers[cacheServers->size];
63 for( i = 0; i != cacheServers->size; i++ ) {
64 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
65 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[i]);
67 osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
70 const char* servers[] = { jsonObjectGetString(cacheServers) };
71 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
72 osrfCacheInit( servers, 1, atoi(maxCache) );
76 osrfLogError( OSRF_LOG_MARK, "Missing config value for /cache/global/servers/server _or_ "
77 "/cache/global/max_cache_time");
84 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
85 if( !(hostname && configfile && contextNode) ) return -1;
87 /* first we grab the settings */
88 if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
89 osrfLogError( OSRF_LOG_MARK,
90 "Unable to bootstrap for host %s from configuration file %s",
91 hostname, configfile );
95 int retcode = osrf_settings_retrieve(hostname);
96 osrf_system_disconnect_client();
99 osrfLogError( OSRF_LOG_MARK,
100 "Unable to retrieve settings for host %s from configuration file %s",
101 hostname, configfile );
106 /* background and let our children do their thing */
107 /* NOTE: This has been moved from below the 'if (apps)' block below ... move it back if things go crazy */
110 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
111 osrfStringArray* arr = osrfNewStringArray(8);
116 if(apps->type == JSON_STRING) {
117 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
120 const jsonObject* app;
121 while( (app = jsonObjectGetIndex(apps, i++)) )
122 osrfStringArrayAdd(arr, jsonObjectGetString(app));
125 char* appname = NULL;
127 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
129 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
131 if(lang && !strcasecmp(lang,"c")) {
133 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
135 if(! (appname && libfile) ) {
136 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
140 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
144 if( (pid = fork()) ) {
145 // store pid in local list for re-launching dead children...
146 add_child( pid, appname, libfile );
147 osrfLogInfo( OSRF_LOG_MARK, "Running application child %s: process id %ld",
148 appname, (long) pid );
152 osrfLogInfo( OSRF_LOG_MARK, " * Running application %s\n", appname);
153 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
154 osrf_prefork_run(appname);
156 osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
161 } // should we do something if there are no apps? does the wait(NULL) below do that for us?
163 osrfStringArrayFree(arr);
165 signal(SIGTERM, handleKillSignal);
166 signal(SIGINT, handleKillSignal);
171 pid_t pid = wait( &status );
174 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
176 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
179 report_child_status( pid, status );
183 delete_all_children();
188 static void report_child_status( pid_t pid, int status )
192 ChildNode* node = seek_child( pid );
195 app = node->app ? node->app : "[unknown]";
196 libfile = node->libfile ? node->libfile : "[none]";
198 app = libfile = NULL;
200 if( WIFEXITED( status ) )
202 int rc = WEXITSTATUS( status ); // return code of child process
204 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) exited with return code %d",
205 (long) pid, app, rc );
207 osrfLogInfo( OSRF_LOG_MARK, "Child process %ld (app %s) exited normally",
210 else if( WIFSIGNALED( status ) )
212 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) killed by signal %d",
213 (long) pid, app, WTERMSIG( status) );
215 else if( WIFSTOPPED( status ) )
217 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) stopped by signal %d",
218 (long) pid, app, (int) WSTOPSIG( status ) );
221 delete_child( node );
224 /*----------- Routines to manage list of children --*/
226 static void add_child( pid_t pid, const char* app, const char* libfile )
228 /* Construct new child node */
230 ChildNode* node = safe_malloc( sizeof( ChildNode ) );
235 node->app = strdup( app );
240 node->libfile = strdup( libfile );
242 node->libfile = NULL;
244 /* Add new child node to the head of the list */
246 node->pNext = child_list;
250 child_list->pPrev = node;
255 static void delete_child( ChildNode* node ) {
262 /* Detach the node from the list */
265 node->pPrev->pNext = node->pNext;
267 child_list = node->pNext;
270 node->pNext->pPrev = node->pPrev;
272 /* Deallocate the node and its payload */
275 free( node->libfile );
279 static void delete_all_children( void ) {
282 delete_child( child_list );
285 static ChildNode* seek_child( pid_t pid ) {
287 /* Return a pointer to the child node for the */
288 /* specified process ID, or NULL if not found */
290 ChildNode* node = child_list;
292 if( node->pid == pid )
301 /*----------- End of routines to manage list of children --*/
304 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
308 if(osrfSystemGetTransportClient()) {
309 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
310 return 1; /* we already have a client connection */
313 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
314 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
319 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
321 osrfConfigSetDefaultConfig(cfg);
323 return 0; /* Can't load configuration? Bail out */
327 char* log_file = osrfConfigGetValue( NULL, "/logfile");
329 fprintf(stderr, "No log file specified in configuration file %s\n",
334 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
335 osrfStringArray* arr = osrfNewStringArray(8);
336 osrfConfigGetValueList(NULL, arr, "/domain");
338 char* username = osrfConfigGetValue( NULL, "/username" );
339 char* password = osrfConfigGetValue( NULL, "/passwd" );
340 char* port = osrfConfigGetValue( NULL, "/port" );
341 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
342 char* facility = osrfConfigGetValue( NULL, "/syslog" );
343 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
345 /* if we're a source-client, tell the logger */
346 char* isclient = osrfConfigGetValue(NULL, "/client");
347 if( isclient && !strcasecmp(isclient,"true") )
348 osrfLogSetIsClient(1);
353 if(port) iport = atoi(port);
354 if(log_level) llevel = atoi(log_level);
356 if(!strcmp(log_file, "syslog")) {
357 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
358 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
359 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
362 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
363 osrfLogSetFile( log_file );
367 /* Get a domain, if one is specified */
368 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
370 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
371 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
376 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
377 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
382 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
383 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
387 if((iport <= 0) && !unixpath) {
388 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
389 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
395 osrfStringArrayFree(arr);
407 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
408 domain, iport, unixpath ? unixpath : "(none)" );
409 transport_client* client = client_init( domain, iport, unixpath, 0 );
412 host = getenv("HOSTNAME");
416 snprintf(tbuf, 32, "%f", get_timestamp_millis());
419 if(!resource) resource = "";
421 int len = strlen(resource) + 256;
424 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
426 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
427 /* child nodes will leak the parents client... but we can't free
428 it without disconnecting the parents client :( */
429 osrfGlobalTransportClient = client;
432 osrfStringArrayFree(arr);
442 if(osrfGlobalTransportClient)
448 int osrf_system_disconnect_client( void ) {
449 client_disconnect( osrfGlobalTransportClient );
450 client_free( osrfGlobalTransportClient );
451 osrfGlobalTransportClient = NULL;
455 int osrf_system_shutdown( void ) {
458 osrf_system_disconnect_client();
459 osrf_settings_free_host_config(NULL);
460 osrfAppSessionCleanup();