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( const char* hostname, const char* configfile,
85 const char* contextNode ) {
86 if( !(hostname && configfile && contextNode) ) return -1;
88 /* first we grab the settings */
89 if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
90 osrfLogError( OSRF_LOG_MARK,
91 "Unable to bootstrap for host %s from configuration file %s",
92 hostname, configfile );
96 int retcode = osrf_settings_retrieve(hostname);
97 osrf_system_disconnect_client();
100 osrfLogError( OSRF_LOG_MARK,
101 "Unable to retrieve settings for host %s from configuration file %s",
102 hostname, configfile );
107 /* background and let our children do their thing */
108 /* NOTE: This has been moved from below the 'if (apps)' block below ... move it back if things go crazy */
111 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
112 osrfStringArray* arr = osrfNewStringArray(8);
117 if(apps->type == JSON_STRING) {
118 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
121 const jsonObject* app;
122 while( (app = jsonObjectGetIndex(apps, i++)) )
123 osrfStringArrayAdd(arr, jsonObjectGetString(app));
126 char* appname = NULL;
128 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
130 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
132 if(lang && !strcasecmp(lang,"c")) {
134 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
136 if(! (appname && libfile) ) {
137 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
141 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
145 if( (pid = fork()) ) {
146 // store pid in local list for re-launching dead children...
147 add_child( pid, appname, libfile );
148 osrfLogInfo( OSRF_LOG_MARK, "Running application child %s: process id %ld",
149 appname, (long) pid );
153 osrfLogInfo( OSRF_LOG_MARK, " * Running application %s\n", appname);
154 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
155 osrf_prefork_run(appname);
157 osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
162 } // should we do something if there are no apps? does the wait(NULL) below do that for us?
164 osrfStringArrayFree(arr);
166 signal(SIGTERM, handleKillSignal);
167 signal(SIGINT, handleKillSignal);
172 pid_t pid = wait( &status );
175 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
177 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
180 report_child_status( pid, status );
184 delete_all_children();
189 static void report_child_status( pid_t pid, int status )
193 ChildNode* node = seek_child( pid );
196 app = node->app ? node->app : "[unknown]";
197 libfile = node->libfile ? node->libfile : "[none]";
199 app = libfile = NULL;
201 if( WIFEXITED( status ) )
203 int rc = WEXITSTATUS( status ); // return code of child process
205 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) exited with return code %d",
206 (long) pid, app, rc );
208 osrfLogInfo( OSRF_LOG_MARK, "Child process %ld (app %s) exited normally",
211 else if( WIFSIGNALED( status ) )
213 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) killed by signal %d",
214 (long) pid, app, WTERMSIG( status) );
216 else if( WIFSTOPPED( status ) )
218 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) stopped by signal %d",
219 (long) pid, app, (int) WSTOPSIG( status ) );
222 delete_child( node );
225 /*----------- Routines to manage list of children --*/
227 static void add_child( pid_t pid, const char* app, const char* libfile )
229 /* Construct new child node */
231 ChildNode* node = safe_malloc( sizeof( ChildNode ) );
236 node->app = strdup( app );
241 node->libfile = strdup( libfile );
243 node->libfile = NULL;
245 /* Add new child node to the head of the list */
247 node->pNext = child_list;
251 child_list->pPrev = node;
256 static void delete_child( ChildNode* node ) {
263 /* Detach the node from the list */
266 node->pPrev->pNext = node->pNext;
268 child_list = node->pNext;
271 node->pNext->pPrev = node->pPrev;
273 /* Deallocate the node and its payload */
276 free( node->libfile );
280 static void delete_all_children( void ) {
283 delete_child( child_list );
286 static ChildNode* seek_child( pid_t pid ) {
288 /* Return a pointer to the child node for the */
289 /* specified process ID, or NULL if not found */
291 ChildNode* node = child_list;
293 if( node->pid == pid )
302 /*----------- End of routines to manage list of children --*/
305 int osrfSystemBootstrapClientResc( const char* config_file,
306 const char* contextnode, const char* resource ) {
310 if(osrfSystemGetTransportClient()) {
311 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
312 return 1; /* we already have a client connection */
315 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
316 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
321 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
323 osrfConfigSetDefaultConfig(cfg);
325 return 0; /* Can't load configuration? Bail out */
329 char* log_file = osrfConfigGetValue( NULL, "/logfile");
331 fprintf(stderr, "No log file specified in configuration file %s\n",
336 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
337 osrfStringArray* arr = osrfNewStringArray(8);
338 osrfConfigGetValueList(NULL, arr, "/domain");
340 char* username = osrfConfigGetValue( NULL, "/username" );
341 char* password = osrfConfigGetValue( NULL, "/passwd" );
342 char* port = osrfConfigGetValue( NULL, "/port" );
343 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
344 char* facility = osrfConfigGetValue( NULL, "/syslog" );
345 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
347 /* if we're a source-client, tell the logger */
348 char* isclient = osrfConfigGetValue(NULL, "/client");
349 if( isclient && !strcasecmp(isclient,"true") )
350 osrfLogSetIsClient(1);
355 if(port) iport = atoi(port);
356 if(log_level) llevel = atoi(log_level);
358 if(!strcmp(log_file, "syslog")) {
359 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
360 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
361 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
364 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
365 osrfLogSetFile( log_file );
369 /* Get a domain, if one is specified */
370 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
372 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
373 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
378 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
379 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
384 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
385 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
389 if((iport <= 0) && !unixpath) {
390 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
391 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
397 osrfStringArrayFree(arr);
409 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
410 domain, iport, unixpath ? unixpath : "(none)" );
411 transport_client* client = client_init( domain, iport, unixpath, 0 );
414 host = getenv("HOSTNAME");
418 snprintf(tbuf, 32, "%f", get_timestamp_millis());
421 if(!resource) resource = "";
423 int len = strlen(resource) + 256;
426 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
428 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
429 /* child nodes will leak the parents client... but we can't free
430 it without disconnecting the parents client :( */
431 osrfGlobalTransportClient = client;
434 osrfStringArrayFree(arr);
444 if(osrfGlobalTransportClient)
450 int osrf_system_disconnect_client( void ) {
451 client_disconnect( osrfGlobalTransportClient );
452 client_free( osrfGlobalTransportClient );
453 osrfGlobalTransportClient = NULL;
457 int osrf_system_shutdown( void ) {
460 osrf_system_disconnect_client();
461 osrf_settings_free_host_config(NULL);
462 osrfAppSessionCleanup();