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 transport_client* osrf_system_get_transport_client( void ) {
38 return osrfGlobalTransportClient;
41 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
42 return osrf_system_bootstrap_client_resc(config_file, contextnode, NULL);
45 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
46 return osrf_system_bootstrap_client_resc( config_file, contextnode, resource );
50 static int _osrfSystemInitCache( void ) {
52 jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
53 char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
55 if( cacheServers && maxCache) {
57 if( cacheServers->type == JSON_ARRAY ) {
59 char* servers[cacheServers->size];
60 for( i = 0; i != cacheServers->size; i++ ) {
61 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
62 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[i]);
64 osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
67 char* servers[] = { jsonObjectGetString(cacheServers) };
68 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
69 osrfCacheInit( servers, 1, atoi(maxCache) );
73 osrfLogError( OSRF_LOG_MARK, "Missing config value for /cache/global/servers/server _or_ "
74 "/cache/global/max_cache_time");
81 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
82 if( !(hostname && configfile && contextNode) ) return -1;
84 /* first we grab the settings */
85 if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
86 osrfLogError( OSRF_LOG_MARK,
87 "Unable to bootstrap for host %s from configuration file %s",
88 hostname, configfile );
92 int retcode = osrf_settings_retrieve(hostname);
93 osrf_system_disconnect_client();
96 osrfLogError( OSRF_LOG_MARK,
97 "Unable to retrieve settings for host %s from configuration file %s",
98 hostname, configfile );
103 /* background and let our children do their thing */
104 /* NOTE: This has been moved from below the 'if (apps)' block below ... move it back if things go crazy */
107 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
108 osrfStringArray* arr = osrfNewStringArray(8);
110 _osrfSystemInitCache();
115 if(apps->type == JSON_STRING) {
116 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
120 while( (app = jsonObjectGetIndex(apps, i++)) )
121 osrfStringArrayAdd(arr, jsonObjectGetString(app));
124 char* appname = NULL;
126 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
128 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
130 if(lang && !strcasecmp(lang,"c")) {
132 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
134 if(! (appname && libfile) ) {
135 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
139 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
143 if( (pid = fork()) ) {
144 // store pid in local list for re-launching dead children...
145 add_child( pid, appname, libfile );
146 osrfLogInfo( OSRF_LOG_MARK, "Running application child %s: process id %ld",
147 appname, (long) pid );
151 osrfLogInfo( OSRF_LOG_MARK, " * Running application %s\n", appname);
152 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
153 osrf_prefork_run(appname);
155 osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
160 } // should we do something if there are no apps? does the wait(NULL) below do that for us?
165 pid_t pid = wait( &status );
168 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
170 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
173 report_child_status( pid, status );
177 delete_all_children();
182 static void report_child_status( pid_t pid, int status )
186 ChildNode* node = seek_child( pid );
189 app = node->app ? node->app : "[unknown]";
190 libfile = node->libfile ? node->libfile : "[none]";
192 app = libfile = NULL;
194 if( WIFEXITED( status ) )
196 int rc = WEXITSTATUS( status ); // return code of child process
198 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) exited with return code %d",
199 (long) pid, app, rc );
201 osrfLogInfo( OSRF_LOG_MARK, "Child process %ld (app %s) exited normally",
204 else if( WIFSIGNALED( status ) )
206 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) killed by signal %d",
207 (long) pid, app, WTERMSIG( status) );
209 else if( WIFSTOPPED( status ) )
211 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) stopped by signal %d",
212 (long) pid, app, (int) WSTOPSIG( status ) );
215 delete_child( node );
218 /*----------- Routines to manage list of children --*/
220 static void add_child( pid_t pid, const char* app, const char* libfile )
222 /* Construct new child node */
224 ChildNode* node = safe_malloc( sizeof( ChildNode ) );
229 node->app = strdup( app );
234 node->libfile = strdup( libfile );
236 node->libfile = NULL;
238 /* Add new child node to the head of the list */
240 node->pNext = child_list;
244 child_list->pPrev = node;
249 static void delete_child( ChildNode* node ) {
256 /* Detach the node from the list */
259 node->pPrev->pNext = node->pNext;
261 child_list = node->pNext;
264 node->pNext->pPrev = node->pPrev;
266 /* Deallocate the node and its payload */
269 free( node->libfile );
273 static void delete_all_children( void ) {
276 delete_child( child_list );
279 static ChildNode* seek_child( pid_t pid ) {
281 /* Return a pointer to the child node for the */
282 /* specified process ID, or NULL if not found */
284 ChildNode* node = child_list;
286 if( node->pid == pid )
295 /*----------- End of routines to manage list of children --*/
298 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
302 if(osrfSystemGetTransportClient()) {
303 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
304 return 1; /* we already have a client connection */
307 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
308 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
313 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
315 osrfConfigSetDefaultConfig(cfg);
317 return 0; /* Can't load configuration? Bail out */
321 char* log_file = osrfConfigGetValue( NULL, "/logfile");
322 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
323 osrfStringArray* arr = osrfNewStringArray(8);
324 osrfConfigGetValueList(NULL, arr, "/domains/domain");
326 char* username = osrfConfigGetValue( NULL, "/username" );
327 char* password = osrfConfigGetValue( NULL, "/passwd" );
328 char* port = osrfConfigGetValue( NULL, "/port" );
329 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
330 char* facility = osrfConfigGetValue( NULL, "/syslog" );
331 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
334 fprintf(stderr, "No log file specified in configuration file %s\n",
346 /* if we're a source-client, tell the logger */
347 char* isclient = osrfConfigGetValue(NULL, "/client");
348 if( isclient && !strcasecmp(isclient,"true") )
349 osrfLogSetIsClient(1);
354 if(port) iport = atoi(port);
355 if(log_level) llevel = atoi(log_level);
357 if(!strcmp(log_file, "syslog")) {
358 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
359 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
360 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
363 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
364 osrfLogSetFile( log_file );
368 /* Get a domain, if one is specified */
369 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
371 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
372 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
377 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
378 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
383 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
384 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
388 if((iport <= 0) && !unixpath) {
389 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
390 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
396 osrfStringArrayFree(arr);
407 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
408 domain, iport, unixpath ? unixpath : "(none)" );
409 transport_client* client = client_init( domain, iport, unixpath, 0 );
412 host = getenv("HOSTNAME");
416 snprintf(tbuf, 32, "%f", get_timestamp_millis());
419 if(!resource) resource = "";
421 int len = strlen(resource) + 256;
424 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
426 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
427 /* child nodes will leak the parents client... but we can't free
428 it without disconnecting the parents client :( */
429 osrfGlobalTransportClient = client;
432 osrfStringArrayFree(arr);
442 if(osrfGlobalTransportClient)
448 int osrf_system_disconnect_client( void ) {
449 client_disconnect( osrfGlobalTransportClient );
450 client_free( osrfGlobalTransportClient );
451 osrfGlobalTransportClient = NULL;
455 int osrf_system_shutdown( void ) {
458 osrf_system_disconnect_client();
459 osrf_settings_free_host_config(NULL);
460 osrfAppSessionCleanup();