1 #include <opensrf/osrf_system.h>
2 #include <opensrf/osrf_application.h>
3 #include <opensrf/osrf_prefork.h>
6 static int _osrfSystemInitCache( void );
7 static void report_child_status( pid_t pid, int status );
9 typedef struct child_node ChildNode;
20 static ChildNode* child_list;
22 static transport_client* osrfGlobalTransportClient = NULL;
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 );
29 transport_client* osrfSystemGetTransportClient( void ) {
30 return osrfGlobalTransportClient;
33 void osrfSystemIgnoreTransportClient() {
34 osrfGlobalTransportClient = NULL;
37 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
38 return osrfSystemBootstrapClientResc(config_file, contextnode, NULL);
41 static int _osrfSystemInitCache( void ) {
43 jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
44 char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
46 if( cacheServers && maxCache) {
48 if( cacheServers->type == JSON_ARRAY ) {
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]);
55 osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
58 const char* servers[] = { jsonObjectGetString(cacheServers) };
59 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
60 osrfCacheInit( servers, 1, atoi(maxCache) );
64 osrfLogError( OSRF_LOG_MARK, "Missing config value for /cache/global/servers/server _or_ "
65 "/cache/global/max_cache_time");
72 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
73 if( !(hostname && configfile && contextNode) ) return -1;
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 );
83 int retcode = osrf_settings_retrieve(hostname);
84 osrf_system_disconnect_client();
87 osrfLogError( OSRF_LOG_MARK,
88 "Unable to retrieve settings for host %s from configuration file %s",
89 hostname, configfile );
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 */
98 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
99 osrfStringArray* arr = osrfNewStringArray(8);
101 _osrfSystemInitCache();
106 if(apps->type == JSON_STRING) {
107 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
110 const jsonObject* app;
111 while( (app = jsonObjectGetIndex(apps, i++)) )
112 osrfStringArrayAdd(arr, jsonObjectGetString(app));
115 char* appname = NULL;
117 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
119 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
121 if(lang && !strcasecmp(lang,"c")) {
123 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
125 if(! (appname && libfile) ) {
126 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
130 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
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 );
142 osrfLogInfo( OSRF_LOG_MARK, " * Running application %s\n", appname);
143 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
144 osrf_prefork_run(appname);
146 osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
151 } // should we do something if there are no apps? does the wait(NULL) below do that for us?
156 pid_t pid = wait( &status );
159 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
161 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
164 report_child_status( pid, status );
168 delete_all_children();
173 static void report_child_status( pid_t pid, int status )
177 ChildNode* node = seek_child( pid );
180 app = node->app ? node->app : "[unknown]";
181 libfile = node->libfile ? node->libfile : "[none]";
183 app = libfile = NULL;
185 if( WIFEXITED( status ) )
187 int rc = WEXITSTATUS( status ); // return code of child process
189 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) exited with return code %d",
190 (long) pid, app, rc );
192 osrfLogInfo( OSRF_LOG_MARK, "Child process %ld (app %s) exited normally",
195 else if( WIFSIGNALED( status ) )
197 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) killed by signal %d",
198 (long) pid, app, WTERMSIG( status) );
200 else if( WIFSTOPPED( status ) )
202 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) stopped by signal %d",
203 (long) pid, app, (int) WSTOPSIG( status ) );
206 delete_child( node );
209 /*----------- Routines to manage list of children --*/
211 static void add_child( pid_t pid, const char* app, const char* libfile )
213 /* Construct new child node */
215 ChildNode* node = safe_malloc( sizeof( ChildNode ) );
220 node->app = strdup( app );
225 node->libfile = strdup( libfile );
227 node->libfile = NULL;
229 /* Add new child node to the head of the list */
231 node->pNext = child_list;
235 child_list->pPrev = node;
240 static void delete_child( ChildNode* node ) {
247 /* Detach the node from the list */
250 node->pPrev->pNext = node->pNext;
252 child_list = node->pNext;
255 node->pNext->pPrev = node->pPrev;
257 /* Deallocate the node and its payload */
260 free( node->libfile );
264 static void delete_all_children( void ) {
267 delete_child( child_list );
270 static ChildNode* seek_child( pid_t pid ) {
272 /* Return a pointer to the child node for the */
273 /* specified process ID, or NULL if not found */
275 ChildNode* node = child_list;
277 if( node->pid == pid )
286 /*----------- End of routines to manage list of children --*/
289 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
293 if(osrfSystemGetTransportClient()) {
294 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
295 return 1; /* we already have a client connection */
298 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
299 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
304 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
306 osrfConfigSetDefaultConfig(cfg);
308 return 0; /* Can't load configuration? Bail out */
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");
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" );
325 fprintf(stderr, "No log file specified in configuration file %s\n",
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);
345 if(port) iport = atoi(port);
346 if(log_level) llevel = atoi(log_level);
348 if(!strcmp(log_file, "syslog")) {
349 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
350 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
351 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
354 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
355 osrfLogSetFile( log_file );
359 /* Get a domain, if one is specified */
360 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
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);
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);
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);
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",
387 osrfStringArrayFree(arr);
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 );
403 host = getenv("HOSTNAME");
407 snprintf(tbuf, 32, "%f", get_timestamp_millis());
410 if(!resource) resource = "";
412 int len = strlen(resource) + 256;
415 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
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;
423 osrfStringArrayFree(arr);
433 if(osrfGlobalTransportClient)
439 int osrf_system_disconnect_client( void ) {
440 client_disconnect( osrfGlobalTransportClient );
441 client_free( osrfGlobalTransportClient );
442 osrfGlobalTransportClient = NULL;
446 int osrf_system_shutdown( void ) {
449 osrf_system_disconnect_client();
450 osrf_settings_free_host_config(NULL);
451 osrfAppSessionCleanup();