7 #include <sys/select.h>
11 #include "opensrf/utils.h"
12 #include "opensrf/log.h"
13 #include "opensrf/osrf_system.h"
14 #include "opensrf/osrf_application.h"
15 #include "opensrf/osrf_prefork.h"
18 #define HOST_NAME_MAX 256
21 static void report_child_status( pid_t pid, int status );
23 typedef struct child_node ChildNode;
25 static void handleKillSignal(int signo) {
26 /* we are the top-level process and we've been
27 * killed. Kill all of our children */
29 sleep(1); /* give the children a chance to die before we reap them */
32 while( (child_pid=waitpid(-1,&status,WNOHANG)) > 0)
33 osrfLogInfo(OSRF_LOG_MARK, "Killed child %d", child_pid);
47 static ChildNode* child_list;
49 static transport_client* osrfGlobalTransportClient = NULL;
51 static void add_child( pid_t pid, const char* app, const char* libfile );
52 static void delete_child( ChildNode* node );
53 static void delete_all_children( void );
54 static ChildNode* seek_child( pid_t pid );
56 transport_client* osrfSystemGetTransportClient( void ) {
57 return osrfGlobalTransportClient;
60 void osrfSystemIgnoreTransportClient() {
61 osrfGlobalTransportClient = NULL;
64 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
65 return osrfSystemBootstrapClientResc(config_file, contextnode, NULL);
68 int osrfSystemInitCache( void ) {
70 jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
71 char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
73 if( cacheServers && maxCache) {
75 if( cacheServers->type == JSON_ARRAY ) {
77 const char* servers[cacheServers->size];
78 for( i = 0; i != cacheServers->size; i++ ) {
79 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
80 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[i]);
82 osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
85 const char* servers[] = { jsonObjectGetString(cacheServers) };
86 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
87 osrfCacheInit( servers, 1, atoi(maxCache) );
91 osrfLogError( OSRF_LOG_MARK, "Missing config value for /cache/global/servers/server _or_ "
92 "/cache/global/max_cache_time");
95 jsonObjectFree( cacheServers );
101 @brief Set yourself up for business.
102 @param hostname Full network name of the host where the process is running; or
104 @param configfile Name of the configuration file; normally '/openils/conf/opensrf_core.xml'.
106 @return - Name of an aggregate within the configuration file, containing the relevant
107 subset of configuration stuff.
111 int osrfSystemBootstrap( const char* hostname, const char* configfile,
112 const char* contextNode ) {
113 if( !(hostname && configfile && contextNode) ) return -1;
115 /* first we grab the settings */
116 if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
117 osrfLogError( OSRF_LOG_MARK,
118 "Unable to bootstrap for host %s from configuration file %s",
119 hostname, configfile );
123 int retcode = osrf_settings_retrieve(hostname);
124 osrf_system_disconnect_client();
127 osrfLogError( OSRF_LOG_MARK,
128 "Unable to retrieve settings for host %s from configuration file %s",
129 hostname, configfile );
133 // Turn into a daemon. The parent forks and exits. Only the
134 // child returns, with the standard streams (stdin, stdout, and
135 // stderr) redirected to /dev/null.
136 /* NOTE: This has been moved from below the 'if (apps)' block below ... move it back if things go crazy */
139 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
140 osrfStringArray* arr = osrfNewStringArray(8);
145 if(apps->type == JSON_STRING) {
146 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
149 const jsonObject* app;
150 while( (app = jsonObjectGetIndex(apps, i++)) )
151 osrfStringArrayAdd(arr, jsonObjectGetString(app));
153 jsonObjectFree(apps);
155 const char* appname = NULL;
157 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
159 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
161 if(lang && !strcasecmp(lang,"c")) {
163 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
165 if(! (appname && libfile) ) {
166 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
170 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
174 if( (pid = fork()) ) {
175 // store pid in local list for re-launching dead children...
176 add_child( pid, appname, libfile );
177 osrfLogInfo( OSRF_LOG_MARK, "Running application child %s: process id %ld",
178 appname, (long) pid );
182 osrfLogInfo( OSRF_LOG_MARK, " * Running application %s\n", appname);
183 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
184 osrf_prefork_run(appname);
186 osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
191 } // should we do something if there are no apps? does the wait(NULL) below do that for us?
193 osrfStringArrayFree(arr);
195 signal(SIGTERM, handleKillSignal);
196 signal(SIGINT, handleKillSignal);
201 pid_t pid = wait( &status );
204 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
206 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
209 report_child_status( pid, status );
213 delete_all_children();
218 static void report_child_status( pid_t pid, int status )
222 ChildNode* node = seek_child( pid );
225 app = node->app ? node->app : "[unknown]";
226 libfile = node->libfile ? node->libfile : "[none]";
228 app = libfile = NULL;
230 if( WIFEXITED( status ) )
232 int rc = WEXITSTATUS( status ); // return code of child process
234 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) exited with return code %d",
235 (long) pid, app, rc );
237 osrfLogInfo( OSRF_LOG_MARK, "Child process %ld (app %s) exited normally",
240 else if( WIFSIGNALED( status ) )
242 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) killed by signal %d",
243 (long) pid, app, WTERMSIG( status) );
245 else if( WIFSTOPPED( status ) )
247 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) stopped by signal %d",
248 (long) pid, app, (int) WSTOPSIG( status ) );
251 delete_child( node );
254 /*----------- Routines to manage list of children --*/
256 static void add_child( pid_t pid, const char* app, const char* libfile )
258 /* Construct new child node */
260 ChildNode* node = safe_malloc( sizeof( ChildNode ) );
265 node->app = strdup( app );
270 node->libfile = strdup( libfile );
272 node->libfile = NULL;
274 /* Add new child node to the head of the list */
276 node->pNext = child_list;
280 child_list->pPrev = node;
285 static void delete_child( ChildNode* node ) {
292 /* Detach the node from the list */
295 node->pPrev->pNext = node->pNext;
297 child_list = node->pNext;
300 node->pNext->pPrev = node->pPrev;
302 /* Deallocate the node and its payload */
305 free( node->libfile );
309 static void delete_all_children( void ) {
312 delete_child( child_list );
315 static ChildNode* seek_child( pid_t pid ) {
317 /* Return a pointer to the child node for the */
318 /* specified process ID, or NULL if not found */
320 ChildNode* node = child_list;
322 if( node->pid == pid )
331 /*----------- End of routines to manage list of children --*/
334 int osrfSystemBootstrapClientResc( const char* config_file,
335 const char* contextnode, const char* resource ) {
339 if(osrfSystemGetTransportClient()) {
340 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
341 return 1; /* we already have a client connection */
344 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
345 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
350 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
352 osrfConfigSetDefaultConfig(cfg);
354 return 0; /* Can't load configuration? Bail out */
358 char* log_file = osrfConfigGetValue( NULL, "/logfile");
360 fprintf(stderr, "No log file specified in configuration file %s\n",
365 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
366 osrfStringArray* arr = osrfNewStringArray(8);
367 osrfConfigGetValueList(NULL, arr, "/domain");
369 char* username = osrfConfigGetValue( NULL, "/username" );
370 char* password = osrfConfigGetValue( NULL, "/passwd" );
371 char* port = osrfConfigGetValue( NULL, "/port" );
372 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
373 char* facility = osrfConfigGetValue( NULL, "/syslog" );
374 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
376 /* if we're a source-client, tell the logger */
377 char* isclient = osrfConfigGetValue(NULL, "/client");
378 if( isclient && !strcasecmp(isclient,"true") )
379 osrfLogSetIsClient(1);
384 if(port) iport = atoi(port);
385 if(log_level) llevel = atoi(log_level);
387 if(!strcmp(log_file, "syslog")) {
388 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
389 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
390 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
393 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
394 osrfLogSetFile( log_file );
398 /* Get a domain, if one is specified */
399 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
401 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
402 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
407 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
408 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
413 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
414 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
418 if((iport <= 0) && !unixpath) {
419 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
420 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
426 osrfStringArrayFree(arr);
438 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
439 domain, iport, unixpath ? unixpath : "(none)" );
440 transport_client* client = client_init( domain, iport, unixpath, 0 );
442 char host[HOST_NAME_MAX + 1] = "";
443 gethostname(host, sizeof(host) );
444 host[HOST_NAME_MAX] = '\0';
448 snprintf(tbuf, 32, "%f", get_timestamp_millis());
450 if(!resource) resource = "";
452 int len = strlen(resource) + 256;
455 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
457 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
458 /* child nodes will leak the parents client... but we can't free
459 it without disconnecting the parents client :( */
460 osrfGlobalTransportClient = client;
463 osrfStringArrayFree(arr);
473 if(osrfGlobalTransportClient)
479 int osrf_system_disconnect_client( void ) {
480 client_disconnect( osrfGlobalTransportClient );
481 client_free( osrfGlobalTransportClient );
482 osrfGlobalTransportClient = NULL;
486 static int shutdownComplete = 0;
487 int osrf_system_shutdown( void ) {
488 if(shutdownComplete) return 0;
491 osrf_system_disconnect_client();
492 osrf_settings_free_host_config(NULL);
493 osrfAppSessionCleanup();
495 shutdownComplete = 1;