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;
19 static ChildNode* child_list;
21 static transport_client* osrfGlobalTransportClient = NULL;
23 static void add_child( pid_t pid, const char* app, const char* libfile );
24 static void delete_child( ChildNode* node );
25 static void delete_all_children( void );
26 static ChildNode* seek_child( pid_t pid );
28 transport_client* osrfSystemGetTransportClient( void ) {
29 return osrfGlobalTransportClient;
32 void osrfSystemIgnoreTransportClient() {
33 osrfGlobalTransportClient = NULL;
36 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
37 return osrfSystemBootstrapClientResc(config_file, contextnode, NULL);
40 int osrfSystemInitCache( void ) {
42 jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
43 char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
45 if( cacheServers && maxCache) {
47 if( cacheServers->type == JSON_ARRAY ) {
49 const char* servers[cacheServers->size];
50 for( i = 0; i != cacheServers->size; i++ ) {
51 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
52 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[i]);
54 osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
57 const char* servers[] = { jsonObjectGetString(cacheServers) };
58 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
59 osrfCacheInit( servers, 1, atoi(maxCache) );
63 osrfLogError( OSRF_LOG_MARK, "Missing config value for /cache/global/servers/server _or_ "
64 "/cache/global/max_cache_time");
71 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
72 if( !(hostname && configfile && contextNode) ) return -1;
74 /* first we grab the settings */
75 if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
76 osrfLogError( OSRF_LOG_MARK,
77 "Unable to bootstrap for host %s from configuration file %s",
78 hostname, configfile );
82 int retcode = osrf_settings_retrieve(hostname);
83 osrf_system_disconnect_client();
86 osrfLogError( OSRF_LOG_MARK,
87 "Unable to retrieve settings for host %s from configuration file %s",
88 hostname, configfile );
93 /* background and let our children do their thing */
94 /* NOTE: This has been moved from below the 'if (apps)' block below ... move it back if things go crazy */
97 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
98 osrfStringArray* arr = osrfNewStringArray(8);
103 if(apps->type == JSON_STRING) {
104 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
107 const jsonObject* app;
108 while( (app = jsonObjectGetIndex(apps, i++)) )
109 osrfStringArrayAdd(arr, jsonObjectGetString(app));
112 char* appname = NULL;
114 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
116 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
118 if(lang && !strcasecmp(lang,"c")) {
120 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
122 if(! (appname && libfile) ) {
123 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
127 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
131 if( (pid = fork()) ) {
132 // store pid in local list for re-launching dead children...
133 add_child( pid, appname, libfile );
134 osrfLogInfo( OSRF_LOG_MARK, "Running application child %s: process id %ld",
135 appname, (long) pid );
139 osrfLogInfo( OSRF_LOG_MARK, " * Running application %s\n", appname);
140 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
141 osrf_prefork_run(appname);
143 osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
148 } // should we do something if there are no apps? does the wait(NULL) below do that for us?
150 osrfStringArrayFree(arr);
155 pid_t pid = wait( &status );
158 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
160 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
163 report_child_status( pid, status );
167 delete_all_children();
172 static void report_child_status( pid_t pid, int status )
176 ChildNode* node = seek_child( pid );
179 app = node->app ? node->app : "[unknown]";
180 libfile = node->libfile ? node->libfile : "[none]";
182 app = libfile = NULL;
184 if( WIFEXITED( status ) )
186 int rc = WEXITSTATUS( status ); // return code of child process
188 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) exited with return code %d",
189 (long) pid, app, rc );
191 osrfLogInfo( OSRF_LOG_MARK, "Child process %ld (app %s) exited normally",
194 else if( WIFSIGNALED( status ) )
196 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) killed by signal %d",
197 (long) pid, app, WTERMSIG( status) );
199 else if( WIFSTOPPED( status ) )
201 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) stopped by signal %d",
202 (long) pid, app, (int) WSTOPSIG( status ) );
205 delete_child( node );
208 /*----------- Routines to manage list of children --*/
210 static void add_child( pid_t pid, const char* app, const char* libfile )
212 /* Construct new child node */
214 ChildNode* node = safe_malloc( sizeof( ChildNode ) );
219 node->app = strdup( app );
224 node->libfile = strdup( libfile );
226 node->libfile = NULL;
228 /* Add new child node to the head of the list */
230 node->pNext = child_list;
234 child_list->pPrev = node;
239 static void delete_child( ChildNode* node ) {
246 /* Detach the node from the list */
249 node->pPrev->pNext = node->pNext;
251 child_list = node->pNext;
254 node->pNext->pPrev = node->pPrev;
256 /* Deallocate the node and its payload */
259 free( node->libfile );
263 static void delete_all_children( void ) {
266 delete_child( child_list );
269 static ChildNode* seek_child( pid_t pid ) {
271 /* Return a pointer to the child node for the */
272 /* specified process ID, or NULL if not found */
274 ChildNode* node = child_list;
276 if( node->pid == pid )
285 /*----------- End of routines to manage list of children --*/
288 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
292 if(osrfSystemGetTransportClient()) {
293 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
294 return 1; /* we already have a client connection */
297 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
298 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
303 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
305 osrfConfigSetDefaultConfig(cfg);
307 return 0; /* Can't load configuration? Bail out */
311 char* log_file = osrfConfigGetValue( NULL, "/logfile");
313 fprintf(stderr, "No log file specified in configuration file %s\n",
318 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
319 osrfStringArray* arr = osrfNewStringArray(8);
320 osrfConfigGetValueList(NULL, arr, "/domain");
322 char* username = osrfConfigGetValue( NULL, "/username" );
323 char* password = osrfConfigGetValue( NULL, "/passwd" );
324 char* port = osrfConfigGetValue( NULL, "/port" );
325 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
326 char* facility = osrfConfigGetValue( NULL, "/syslog" );
327 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
329 /* if we're a source-client, tell the logger */
330 char* isclient = osrfConfigGetValue(NULL, "/client");
331 if( isclient && !strcasecmp(isclient,"true") )
332 osrfLogSetIsClient(1);
337 if(port) iport = atoi(port);
338 if(log_level) llevel = atoi(log_level);
340 if(!strcmp(log_file, "syslog")) {
341 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
342 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
343 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
346 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
347 osrfLogSetFile( log_file );
351 /* Get a domain, if one is specified */
352 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
354 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
355 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
360 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
361 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
366 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
367 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
371 if((iport <= 0) && !unixpath) {
372 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
373 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
379 osrfStringArrayFree(arr);
390 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
391 domain, iport, unixpath ? unixpath : "(none)" );
392 transport_client* client = client_init( domain, iport, unixpath, 0 );
395 host = getenv("HOSTNAME");
399 snprintf(tbuf, 32, "%f", get_timestamp_millis());
402 if(!resource) resource = "";
404 int len = strlen(resource) + 256;
407 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
409 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
410 /* child nodes will leak the parents client... but we can't free
411 it without disconnecting the parents client :( */
412 osrfGlobalTransportClient = client;
415 osrfStringArrayFree(arr);
425 if(osrfGlobalTransportClient)
431 int osrf_system_disconnect_client( void ) {
432 client_disconnect( osrfGlobalTransportClient );
433 client_free( osrfGlobalTransportClient );
434 osrfGlobalTransportClient = NULL;
438 int osrf_system_shutdown( void ) {
441 osrf_system_disconnect_client();
442 osrf_settings_free_host_config(NULL);
443 osrfAppSessionCleanup();