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);
38 @brief Represents a child process.
42 ChildNode* pNext; /**< Linkage pointer for doubly linked list. */
43 ChildNode* pPrev; /**< Linkage pointer for doubly linked list. */
44 pid_t pid; /**< Process ID of the child process. */
49 /** List of child processes. */
50 static ChildNode* child_list;
52 /** Pointer to the global transport_client; i.e. our connection to Jabber. */
53 static transport_client* osrfGlobalTransportClient = NULL;
55 static void add_child( pid_t pid, const char* app, const char* libfile );
56 static void delete_child( ChildNode* node );
57 static void delete_all_children( void );
58 static ChildNode* seek_child( pid_t pid );
61 @brief Return a pointer to the global transport_client.
62 @return Pointer to the global transport_client, or NULL.
64 A given process needs only one connection to Jabber, so we keep it a pointer to it at
65 file scope. This function returns that pointer.
67 If the connection has been opened by a previous call to osrfSystemBootstrapClientResc(),
68 Return the pointer. Otherwise return NULL.
70 transport_client* osrfSystemGetTransportClient( void ) {
71 return osrfGlobalTransportClient;
75 @brief Discard the global transport_client, but without disconnecting from Jabber.
77 To be called by a child process in order to disregard the parent's connection without
78 disconnecting it, since disconnecting would disconnect the parent as well.
80 void osrfSystemIgnoreTransportClient() {
81 client_discard( osrfGlobalTransportClient );
82 osrfGlobalTransportClient = NULL;
85 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
86 return osrfSystemBootstrapClientResc(config_file, contextnode, NULL);
89 int osrfSystemInitCache( void ) {
91 jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
92 char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
94 if( cacheServers && maxCache) {
96 if( cacheServers->type == JSON_ARRAY ) {
98 const char* servers[cacheServers->size];
99 for( i = 0; i != cacheServers->size; i++ ) {
100 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
101 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[i]);
103 osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
106 const char* servers[] = { jsonObjectGetString(cacheServers) };
107 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
108 osrfCacheInit( servers, 1, atoi(maxCache) );
112 osrfLogError( OSRF_LOG_MARK, "Missing config value for /cache/global/servers/server _or_ "
113 "/cache/global/max_cache_time");
116 jsonObjectFree( cacheServers );
122 @brief Launch a collection of servers, as defined by the settings server.
123 @param hostname Full network name of the host where the process is running; or
125 @param configfile Name of the configuration file; normally '/openils/conf/opensrf_core.xml'.
126 @param contextNode Name of an aggregate within the configuration file, containing the
127 relevant subset of configuration stuff.
128 @return - Zero if successful, or -1 if not.
130 int osrfSystemBootstrap( const char* hostname, const char* configfile,
131 const char* contextNode ) {
132 if( !(hostname && configfile && contextNode) )
135 // Load the conguration, open the log, open a connection to Jabber
136 if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
137 osrfLogError( OSRF_LOG_MARK,
138 "Unable to bootstrap for host %s from configuration file %s",
139 hostname, configfile );
143 // Get a list of applications to launch from the settings server
144 int retcode = osrf_settings_retrieve(hostname);
145 osrf_system_disconnect_client();
148 osrfLogError( OSRF_LOG_MARK,
149 "Unable to retrieve settings for host %s from configuration file %s",
150 hostname, configfile );
154 // Turn into a daemon. The parent forks and exits. Only the
155 // child returns, with the standard streams (stdin, stdout, and
156 // stderr) redirected to /dev/null.
159 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
160 osrfStringArray* arr = osrfNewStringArray(8);
165 if(apps->type == JSON_STRING) {
166 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
169 const jsonObject* app;
170 while( (app = jsonObjectGetIndex(apps, i++)) )
171 osrfStringArrayAdd(arr, jsonObjectGetString(app));
173 jsonObjectFree(apps);
175 const char* appname = NULL;
177 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
179 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
181 if(lang && !strcasecmp(lang,"c")) {
183 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
185 if(! (appname && libfile) ) {
186 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
190 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s",
195 if( (pid = fork()) ) { // if parent
196 // store pid in local list for re-launching dead children...
197 add_child( pid, appname, libfile );
198 osrfLogInfo( OSRF_LOG_MARK, "Running application child %s: process id %ld",
199 appname, (long) pid );
201 } else { // if child, run the application
203 osrfLogInfo( OSRF_LOG_MARK, " * Running application %s\n", appname);
204 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
205 osrf_prefork_run(appname);
207 osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n",
213 } // should we do something if there are no apps? does the wait(NULL) below do that for us?
215 osrfStringArrayFree(arr);
217 signal(SIGTERM, handleKillSignal);
218 signal(SIGINT, handleKillSignal);
223 pid_t pid = wait( &status );
226 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
228 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s",
232 report_child_status( pid, status );
236 delete_all_children();
241 static void report_child_status( pid_t pid, int status )
245 ChildNode* node = seek_child( pid );
248 app = node->app ? node->app : "[unknown]";
249 libfile = node->libfile ? node->libfile : "[none]";
251 app = libfile = NULL;
253 if( WIFEXITED( status ) )
255 int rc = WEXITSTATUS( status ); // return code of child process
257 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) exited with return code %d",
258 (long) pid, app, rc );
260 osrfLogInfo( OSRF_LOG_MARK, "Child process %ld (app %s) exited normally",
263 else if( WIFSIGNALED( status ) )
265 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) killed by signal %d",
266 (long) pid, app, WTERMSIG( status) );
268 else if( WIFSTOPPED( status ) )
270 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) stopped by signal %d",
271 (long) pid, app, (int) WSTOPSIG( status ) );
274 delete_child( node );
277 /*----------- Routines to manage list of children --*/
279 static void add_child( pid_t pid, const char* app, const char* libfile )
281 /* Construct new child node */
283 ChildNode* node = safe_malloc( sizeof( ChildNode ) );
288 node->app = strdup( app );
293 node->libfile = strdup( libfile );
295 node->libfile = NULL;
297 /* Add new child node to the head of the list */
299 node->pNext = child_list;
303 child_list->pPrev = node;
308 static void delete_child( ChildNode* node ) {
315 /* Detach the node from the list */
318 node->pPrev->pNext = node->pNext;
320 child_list = node->pNext;
323 node->pNext->pPrev = node->pPrev;
325 /* Deallocate the node and its payload */
328 free( node->libfile );
332 static void delete_all_children( void ) {
335 delete_child( child_list );
338 static ChildNode* seek_child( pid_t pid ) {
340 /* Return a pointer to the child node for the */
341 /* specified process ID, or NULL if not found */
343 ChildNode* node = child_list;
345 if( node->pid == pid )
354 /*----------- End of routines to manage list of children --*/
357 @brief Bootstrap a generic application from info in the configuration file.
358 @param config_file Name of the configuration file.
359 @param context_node Name of an aggregate within the configuration file, containing the
360 relevant subset of configuration stuff.
361 @param resource Used to construct a Jabber resource name; may be NULL.
362 @return 1 if successful; zero or -1 if error.
364 - Load the configuration file.
366 - Open a connection to Jabber.
368 int osrfSystemBootstrapClientResc( const char* config_file,
369 const char* contextnode, const char* resource ) {
373 if(osrfSystemGetTransportClient()) {
374 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
375 return 1; /* we already have a client connection */
378 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
379 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
384 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
386 osrfConfigSetDefaultConfig(cfg);
388 return 0; /* Can't load configuration? Bail out */
391 char* log_file = osrfConfigGetValue( NULL, "/logfile");
393 fprintf(stderr, "No log file specified in configuration file %s\n",
398 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
399 osrfStringArray* arr = osrfNewStringArray(8);
400 osrfConfigGetValueList(NULL, arr, "/domain");
402 char* username = osrfConfigGetValue( NULL, "/username" );
403 char* password = osrfConfigGetValue( NULL, "/passwd" );
404 char* port = osrfConfigGetValue( NULL, "/port" );
405 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
406 char* facility = osrfConfigGetValue( NULL, "/syslog" );
407 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
409 /* if we're a source-client, tell the logger */
410 char* isclient = osrfConfigGetValue(NULL, "/client");
411 if( isclient && !strcasecmp(isclient,"true") )
412 osrfLogSetIsClient(1);
417 if(port) iport = atoi(port);
418 if(log_level) llevel = atoi(log_level);
420 if(!strcmp(log_file, "syslog")) {
421 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
422 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
423 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
426 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
427 osrfLogSetFile( log_file );
431 /* Get a domain, if one is specified */
432 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
434 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
435 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n",
441 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
442 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n",
448 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
449 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n",
454 if((iport <= 0) && !unixpath) {
455 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
456 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
462 osrfStringArrayFree(arr);
474 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
475 domain, iport, unixpath ? unixpath : "(none)" );
476 transport_client* client = client_init( domain, iport, unixpath, 0 );
478 char host[HOST_NAME_MAX + 1] = "";
479 gethostname(host, sizeof(host) );
480 host[HOST_NAME_MAX] = '\0';
484 snprintf(tbuf, 32, "%f", get_timestamp_millis());
486 if(!resource) resource = "";
488 int len = strlen(resource) + 256;
491 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
493 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
494 /* child nodes will leak the parents client... but we can't free
495 it without disconnecting the parents client :( */
496 osrfGlobalTransportClient = client;
499 osrfStringArrayFree(arr);
509 if(osrfGlobalTransportClient)
516 @brief Disconnect from Jabber.
517 @return Zero in all cases.
519 int osrf_system_disconnect_client( void ) {
520 client_disconnect( osrfGlobalTransportClient );
521 client_free( osrfGlobalTransportClient );
522 osrfGlobalTransportClient = NULL;
526 static int shutdownComplete = 0;
527 int osrf_system_shutdown( void ) {
528 if(shutdownComplete) return 0;
531 osrf_system_disconnect_client();
532 osrf_settings_free_host_config(NULL);
533 osrfAppSessionCleanup();
535 shutdownComplete = 1;