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");
80 jsonObjectFree( cacheServers );
85 int osrfSystemBootstrap( const char* hostname, const char* configfile,
86 const char* contextNode ) {
87 if( !(hostname && configfile && contextNode) ) return -1;
89 /* first we grab the settings */
90 if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
91 osrfLogError( OSRF_LOG_MARK,
92 "Unable to bootstrap for host %s from configuration file %s",
93 hostname, configfile );
97 int retcode = osrf_settings_retrieve(hostname);
98 osrf_system_disconnect_client();
101 osrfLogError( OSRF_LOG_MARK,
102 "Unable to retrieve settings for host %s from configuration file %s",
103 hostname, configfile );
108 /* background and let our children do their thing */
109 /* NOTE: This has been moved from below the 'if (apps)' block below ... move it back if things go crazy */
112 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
113 osrfStringArray* arr = osrfNewStringArray(8);
118 if(apps->type == JSON_STRING) {
119 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
122 const jsonObject* app;
123 while( (app = jsonObjectGetIndex(apps, i++)) )
124 osrfStringArrayAdd(arr, jsonObjectGetString(app));
126 jsonObjectFree(apps);
128 char* appname = NULL;
130 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
132 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
134 if(lang && !strcasecmp(lang,"c")) {
136 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
138 if(! (appname && libfile) ) {
139 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
143 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
147 if( (pid = fork()) ) {
148 // store pid in local list for re-launching dead children...
149 add_child( pid, appname, libfile );
150 osrfLogInfo( OSRF_LOG_MARK, "Running application child %s: process id %ld",
151 appname, (long) pid );
155 osrfLogInfo( OSRF_LOG_MARK, " * Running application %s\n", appname);
156 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
157 osrf_prefork_run(appname);
159 osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
164 } // should we do something if there are no apps? does the wait(NULL) below do that for us?
166 osrfStringArrayFree(arr);
168 signal(SIGTERM, handleKillSignal);
169 signal(SIGINT, handleKillSignal);
174 pid_t pid = wait( &status );
177 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
179 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
182 report_child_status( pid, status );
186 delete_all_children();
191 static void report_child_status( pid_t pid, int status )
195 ChildNode* node = seek_child( pid );
198 app = node->app ? node->app : "[unknown]";
199 libfile = node->libfile ? node->libfile : "[none]";
201 app = libfile = NULL;
203 if( WIFEXITED( status ) )
205 int rc = WEXITSTATUS( status ); // return code of child process
207 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) exited with return code %d",
208 (long) pid, app, rc );
210 osrfLogInfo( OSRF_LOG_MARK, "Child process %ld (app %s) exited normally",
213 else if( WIFSIGNALED( status ) )
215 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) killed by signal %d",
216 (long) pid, app, WTERMSIG( status) );
218 else if( WIFSTOPPED( status ) )
220 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) stopped by signal %d",
221 (long) pid, app, (int) WSTOPSIG( status ) );
224 delete_child( node );
227 /*----------- Routines to manage list of children --*/
229 static void add_child( pid_t pid, const char* app, const char* libfile )
231 /* Construct new child node */
233 ChildNode* node = safe_malloc( sizeof( ChildNode ) );
238 node->app = strdup( app );
243 node->libfile = strdup( libfile );
245 node->libfile = NULL;
247 /* Add new child node to the head of the list */
249 node->pNext = child_list;
253 child_list->pPrev = node;
258 static void delete_child( ChildNode* node ) {
265 /* Detach the node from the list */
268 node->pPrev->pNext = node->pNext;
270 child_list = node->pNext;
273 node->pNext->pPrev = node->pPrev;
275 /* Deallocate the node and its payload */
278 free( node->libfile );
282 static void delete_all_children( void ) {
285 delete_child( child_list );
288 static ChildNode* seek_child( pid_t pid ) {
290 /* Return a pointer to the child node for the */
291 /* specified process ID, or NULL if not found */
293 ChildNode* node = child_list;
295 if( node->pid == pid )
304 /*----------- End of routines to manage list of children --*/
307 int osrfSystemBootstrapClientResc( const char* config_file,
308 const char* contextnode, const char* resource ) {
312 if(osrfSystemGetTransportClient()) {
313 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
314 return 1; /* we already have a client connection */
317 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
318 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
323 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
325 osrfConfigSetDefaultConfig(cfg);
327 return 0; /* Can't load configuration? Bail out */
331 char* log_file = osrfConfigGetValue( NULL, "/logfile");
333 fprintf(stderr, "No log file specified in configuration file %s\n",
338 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
339 osrfStringArray* arr = osrfNewStringArray(8);
340 osrfConfigGetValueList(NULL, arr, "/domain");
342 char* username = osrfConfigGetValue( NULL, "/username" );
343 char* password = osrfConfigGetValue( NULL, "/passwd" );
344 char* port = osrfConfigGetValue( NULL, "/port" );
345 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
346 char* facility = osrfConfigGetValue( NULL, "/syslog" );
347 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
349 /* if we're a source-client, tell the logger */
350 char* isclient = osrfConfigGetValue(NULL, "/client");
351 if( isclient && !strcasecmp(isclient,"true") )
352 osrfLogSetIsClient(1);
357 if(port) iport = atoi(port);
358 if(log_level) llevel = atoi(log_level);
360 if(!strcmp(log_file, "syslog")) {
361 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
362 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
363 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
366 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
367 osrfLogSetFile( log_file );
371 /* Get a domain, if one is specified */
372 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
374 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
375 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
380 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
381 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
386 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
387 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
391 if((iport <= 0) && !unixpath) {
392 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
393 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
399 osrfStringArrayFree(arr);
411 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
412 domain, iport, unixpath ? unixpath : "(none)" );
413 transport_client* client = client_init( domain, iport, unixpath, 0 );
415 char host[HOST_NAME_MAX + 1] = "";
416 gethostname(host, sizeof(host) );
417 host[HOST_NAME_MAX] = '\0';
421 snprintf(tbuf, 32, "%f", get_timestamp_millis());
423 if(!resource) resource = "";
425 int len = strlen(resource) + 256;
428 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
430 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
431 /* child nodes will leak the parents client... but we can't free
432 it without disconnecting the parents client :( */
433 osrfGlobalTransportClient = client;
436 osrfStringArrayFree(arr);
446 if(osrfGlobalTransportClient)
452 int osrf_system_disconnect_client( void ) {
453 client_disconnect( osrfGlobalTransportClient );
454 client_free( osrfGlobalTransportClient );
455 osrfGlobalTransportClient = NULL;
459 int osrf_system_shutdown( void ) {
462 osrf_system_disconnect_client();
463 osrf_settings_free_host_config(NULL);
464 osrfAppSessionCleanup();