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);
46 static ChildNode* child_list;
48 /** Pointer to the global transport_client; i.e. our connection to Jabber. */
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 Launch a collection of servers, as defined by the settings server.
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'.
105 @param contextNode Name of an aggregate within the configuration file, containing the
106 relevant subset of configuration stuff.
107 @return - Zero if successful, or -1 if not.
109 int osrfSystemBootstrap( const char* hostname, const char* configfile,
110 const char* contextNode ) {
111 if( !(hostname && configfile && contextNode) )
114 // Load the conguration, open the log, open a connection to Jabber
115 if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
116 osrfLogError( OSRF_LOG_MARK,
117 "Unable to bootstrap for host %s from configuration file %s",
118 hostname, configfile );
122 // Get a list of applications to launch from the settings server
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.
138 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
139 osrfStringArray* arr = osrfNewStringArray(8);
144 if(apps->type == JSON_STRING) {
145 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
148 const jsonObject* app;
149 while( (app = jsonObjectGetIndex(apps, i++)) )
150 osrfStringArrayAdd(arr, jsonObjectGetString(app));
152 jsonObjectFree(apps);
154 const char* appname = NULL;
156 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
158 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
160 if(lang && !strcasecmp(lang,"c")) {
162 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
164 if(! (appname && libfile) ) {
165 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
169 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s",
174 if( (pid = fork()) ) { // if parent
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 );
180 } else { // if child, run the application
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",
192 } // should we do something if there are no apps? does the wait(NULL) below do that for us?
194 osrfStringArrayFree(arr);
196 signal(SIGTERM, handleKillSignal);
197 signal(SIGINT, handleKillSignal);
202 pid_t pid = wait( &status );
205 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
207 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s",
211 report_child_status( pid, status );
215 delete_all_children();
220 static void report_child_status( pid_t pid, int status )
224 ChildNode* node = seek_child( pid );
227 app = node->app ? node->app : "[unknown]";
228 libfile = node->libfile ? node->libfile : "[none]";
230 app = libfile = NULL;
232 if( WIFEXITED( status ) )
234 int rc = WEXITSTATUS( status ); // return code of child process
236 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) exited with return code %d",
237 (long) pid, app, rc );
239 osrfLogInfo( OSRF_LOG_MARK, "Child process %ld (app %s) exited normally",
242 else if( WIFSIGNALED( status ) )
244 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) killed by signal %d",
245 (long) pid, app, WTERMSIG( status) );
247 else if( WIFSTOPPED( status ) )
249 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) stopped by signal %d",
250 (long) pid, app, (int) WSTOPSIG( status ) );
253 delete_child( node );
256 /*----------- Routines to manage list of children --*/
258 static void add_child( pid_t pid, const char* app, const char* libfile )
260 /* Construct new child node */
262 ChildNode* node = safe_malloc( sizeof( ChildNode ) );
267 node->app = strdup( app );
272 node->libfile = strdup( libfile );
274 node->libfile = NULL;
276 /* Add new child node to the head of the list */
278 node->pNext = child_list;
282 child_list->pPrev = node;
287 static void delete_child( ChildNode* node ) {
294 /* Detach the node from the list */
297 node->pPrev->pNext = node->pNext;
299 child_list = node->pNext;
302 node->pNext->pPrev = node->pPrev;
304 /* Deallocate the node and its payload */
307 free( node->libfile );
311 static void delete_all_children( void ) {
314 delete_child( child_list );
317 static ChildNode* seek_child( pid_t pid ) {
319 /* Return a pointer to the child node for the */
320 /* specified process ID, or NULL if not found */
322 ChildNode* node = child_list;
324 if( node->pid == pid )
333 /*----------- End of routines to manage list of children --*/
336 @brief Bootstrap a generic application from info in the configuration file.
337 @param config_file Name of the configuration file.
338 @param context_node Name of an aggregate within the configuration file, containing the
339 relevant subset of configuration stuff.
340 @param resource Used to construct a Jabber resource name; may be NULL.
341 @return 1 if successful; zero or -1 if error.
343 - Load the configuration file.
345 - Open a connection to Jabber.
347 int osrfSystemBootstrapClientResc( const char* config_file,
348 const char* contextnode, const char* resource ) {
352 if(osrfSystemGetTransportClient()) {
353 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
354 return 1; /* we already have a client connection */
357 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
358 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
363 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
365 osrfConfigSetDefaultConfig(cfg);
367 return 0; /* Can't load configuration? Bail out */
370 char* log_file = osrfConfigGetValue( NULL, "/logfile");
372 fprintf(stderr, "No log file specified in configuration file %s\n",
377 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
378 osrfStringArray* arr = osrfNewStringArray(8);
379 osrfConfigGetValueList(NULL, arr, "/domain");
381 char* username = osrfConfigGetValue( NULL, "/username" );
382 char* password = osrfConfigGetValue( NULL, "/passwd" );
383 char* port = osrfConfigGetValue( NULL, "/port" );
384 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
385 char* facility = osrfConfigGetValue( NULL, "/syslog" );
386 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
388 /* if we're a source-client, tell the logger */
389 char* isclient = osrfConfigGetValue(NULL, "/client");
390 if( isclient && !strcasecmp(isclient,"true") )
391 osrfLogSetIsClient(1);
396 if(port) iport = atoi(port);
397 if(log_level) llevel = atoi(log_level);
399 if(!strcmp(log_file, "syslog")) {
400 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
401 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
402 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
405 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
406 osrfLogSetFile( log_file );
410 /* Get a domain, if one is specified */
411 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
413 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
414 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n",
420 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
421 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n",
427 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
428 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n",
433 if((iport <= 0) && !unixpath) {
434 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
435 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
441 osrfStringArrayFree(arr);
453 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
454 domain, iport, unixpath ? unixpath : "(none)" );
455 transport_client* client = client_init( domain, iport, unixpath, 0 );
457 char host[HOST_NAME_MAX + 1] = "";
458 gethostname(host, sizeof(host) );
459 host[HOST_NAME_MAX] = '\0';
463 snprintf(tbuf, 32, "%f", get_timestamp_millis());
465 if(!resource) resource = "";
467 int len = strlen(resource) + 256;
470 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
472 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
473 /* child nodes will leak the parents client... but we can't free
474 it without disconnecting the parents client :( */
475 osrfGlobalTransportClient = client;
478 osrfStringArrayFree(arr);
488 if(osrfGlobalTransportClient)
495 @brief Disconnect from Jabber.
496 @return Zero in all cases.
498 int osrf_system_disconnect_client( void ) {
499 client_disconnect( osrfGlobalTransportClient );
500 client_free( osrfGlobalTransportClient );
501 osrfGlobalTransportClient = NULL;
505 static int shutdownComplete = 0;
506 int osrf_system_shutdown( void ) {
507 if(shutdownComplete) return 0;
510 osrf_system_disconnect_client();
511 osrf_settings_free_host_config(NULL);
512 osrfAppSessionCleanup();
514 shutdownComplete = 1;