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?
153 osrfStringArrayFree(arr);
158 pid_t pid = wait( &status );
161 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
163 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
166 report_child_status( pid, status );
170 delete_all_children();
175 static void report_child_status( pid_t pid, int status )
179 ChildNode* node = seek_child( pid );
182 app = node->app ? node->app : "[unknown]";
183 libfile = node->libfile ? node->libfile : "[none]";
185 app = libfile = NULL;
187 if( WIFEXITED( status ) )
189 int rc = WEXITSTATUS( status ); // return code of child process
191 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) exited with return code %d",
192 (long) pid, app, rc );
194 osrfLogInfo( OSRF_LOG_MARK, "Child process %ld (app %s) exited normally",
197 else if( WIFSIGNALED( status ) )
199 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) killed by signal %d",
200 (long) pid, app, WTERMSIG( status) );
202 else if( WIFSTOPPED( status ) )
204 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) stopped by signal %d",
205 (long) pid, app, (int) WSTOPSIG( status ) );
208 delete_child( node );
211 /*----------- Routines to manage list of children --*/
213 static void add_child( pid_t pid, const char* app, const char* libfile )
215 /* Construct new child node */
217 ChildNode* node = safe_malloc( sizeof( ChildNode ) );
222 node->app = strdup( app );
227 node->libfile = strdup( libfile );
229 node->libfile = NULL;
231 /* Add new child node to the head of the list */
233 node->pNext = child_list;
237 child_list->pPrev = node;
242 static void delete_child( ChildNode* node ) {
249 /* Detach the node from the list */
252 node->pPrev->pNext = node->pNext;
254 child_list = node->pNext;
257 node->pNext->pPrev = node->pPrev;
259 /* Deallocate the node and its payload */
262 free( node->libfile );
266 static void delete_all_children( void ) {
269 delete_child( child_list );
272 static ChildNode* seek_child( pid_t pid ) {
274 /* Return a pointer to the child node for the */
275 /* specified process ID, or NULL if not found */
277 ChildNode* node = child_list;
279 if( node->pid == pid )
288 /*----------- End of routines to manage list of children --*/
291 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
295 if(osrfSystemGetTransportClient()) {
296 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
297 return 1; /* we already have a client connection */
300 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
301 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
306 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
308 osrfConfigSetDefaultConfig(cfg);
310 return 0; /* Can't load configuration? Bail out */
314 char* log_file = osrfConfigGetValue( NULL, "/logfile");
316 fprintf(stderr, "No log file specified in configuration file %s\n",
321 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
322 osrfStringArray* arr = osrfNewStringArray(8);
323 osrfConfigGetValueList(NULL, arr, "/domains/domain");
325 char* username = osrfConfigGetValue( NULL, "/username" );
326 char* password = osrfConfigGetValue( NULL, "/passwd" );
327 char* port = osrfConfigGetValue( NULL, "/port" );
328 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
329 char* facility = osrfConfigGetValue( NULL, "/syslog" );
330 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
332 /* if we're a source-client, tell the logger */
333 char* isclient = osrfConfigGetValue(NULL, "/client");
334 if( isclient && !strcasecmp(isclient,"true") )
335 osrfLogSetIsClient(1);
340 if(port) iport = atoi(port);
341 if(log_level) llevel = atoi(log_level);
343 if(!strcmp(log_file, "syslog")) {
344 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
345 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
346 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
349 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
350 osrfLogSetFile( log_file );
354 /* Get a domain, if one is specified */
355 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
357 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
358 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
363 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
364 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
369 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
370 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
374 if((iport <= 0) && !unixpath) {
375 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
376 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
382 osrfStringArrayFree(arr);
393 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
394 domain, iport, unixpath ? unixpath : "(none)" );
395 transport_client* client = client_init( domain, iport, unixpath, 0 );
398 host = getenv("HOSTNAME");
402 snprintf(tbuf, 32, "%f", get_timestamp_millis());
405 if(!resource) resource = "";
407 int len = strlen(resource) + 256;
410 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
412 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
413 /* child nodes will leak the parents client... but we can't free
414 it without disconnecting the parents client :( */
415 osrfGlobalTransportClient = client;
418 osrfStringArrayFree(arr);
428 if(osrfGlobalTransportClient)
434 int osrf_system_disconnect_client( void ) {
435 client_disconnect( osrfGlobalTransportClient );
436 client_free( osrfGlobalTransportClient );
437 osrfGlobalTransportClient = NULL;
441 int osrf_system_shutdown( void ) {
444 osrf_system_disconnect_client();
445 osrf_settings_free_host_config(NULL);
446 osrfAppSessionCleanup();