3 @brief Launch a collection of servers.
12 #include <sys/select.h>
16 #include "opensrf/utils.h"
17 #include "opensrf/log.h"
18 #include "opensrf/osrf_system.h"
19 #include "opensrf/osrf_application.h"
20 #include "opensrf/osrf_prefork.h"
23 #define HOST_NAME_MAX 256
26 osrfStringArray* log_protect_arr = NULL;
28 /** Pointer to the global transport_client; i.e. our connection to Jabber. */
29 static transport_client* osrfGlobalTransportClient = NULL;
31 /** Boolean: set to true when we finish shutting down. */
32 static int shutdownComplete = 0;
34 /** Returns the full path to the pid file for the service */
35 static char* get_pid_file(const char* path, const char* service);
37 static int stop_service(const char* path, const char* service);
40 @brief Return a pointer to the global transport_client.
41 @return Pointer to the global transport_client, or NULL.
43 A given process needs only one connection to Jabber, so we keep it a pointer to it at
44 file scope. This function returns that pointer.
46 If the connection has been opened by a previous call to osrfSystemBootstrapClientResc(),
47 return the pointer. Otherwise return NULL.
49 transport_client* osrfSystemGetTransportClient( void ) {
50 return osrfGlobalTransportClient;
54 @brief Discard the global transport_client, but without disconnecting from Jabber.
56 To be called by a child process in order to disregard the parent's connection without
57 disconnecting it, since disconnecting would disconnect the parent as well.
59 void osrfSystemIgnoreTransportClient() {
60 client_discard( osrfGlobalTransportClient );
61 osrfGlobalTransportClient = NULL;
65 @brief Bootstrap a generic application from info in the configuration file.
66 @param config_file Name of the configuration file.
67 @param contextnode Name of an aggregate within the configuration file, containing the
68 relevant subset of configuration stuff.
69 @return 1 if successful; zero or -1 if error.
71 - Load the configuration file.
73 - Open a connection to Jabber.
75 A thin wrapper for osrfSystemBootstrapClientResc, passing it NULL for a resource.
77 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
78 return osrfSystemBootstrapClientResc(config_file, contextnode, NULL);
82 @brief Connect to one or more cache servers.
83 @return Zero in all cases.
85 int osrfSystemInitCache( void ) {
87 jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
88 char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
90 if( cacheServers && maxCache) {
92 if( cacheServers->type == JSON_ARRAY ) {
94 const char* servers[cacheServers->size];
95 for( i = 0; i != cacheServers->size; i++ ) {
96 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
97 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[i]);
99 osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
102 const char* servers[] = { jsonObjectGetString(cacheServers) };
103 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
104 osrfCacheInit( servers, 1, atoi(maxCache) );
108 osrfLogError( OSRF_LOG_MARK, "Missing config value for /cache/global/servers/server _or_ "
109 "/cache/global/max_cache_time");
112 jsonObjectFree( cacheServers );
116 static char* get_pid_file(const char* piddir, const char* service) {
117 int nsize = strlen(piddir) + strlen(service) + 6;
119 snprintf(pfname, nsize, "%s/%s.pid", piddir, service);
120 pfname[nsize-1] = '\0';
121 return strdup(pfname);
124 // TERM the process and delete the PID file
125 static int stop_service(const char* piddir, const char* service) {
127 char* pidfile_name = get_pid_file(piddir, service);
128 FILE* pidfile = fopen(pidfile_name, "r");
130 osrfLogInfo(OSRF_LOG_MARK, "Stopping service %s", service);
134 if (fgets(pidstr, 16, pidfile) != NULL) {
135 long pid = atol(pidstr);
138 // we have a PID, now send the TERM signal the process
140 "* stopping service pid=%ld %s\n", pid, service);
145 osrfLogWarning(OSRF_LOG_MARK,
146 "Unable to read pid file %s", pidfile_name);
151 if (unlink(pidfile_name) != 0) {
152 osrfLogError(OSRF_LOG_MARK,
153 "Unable to delete pid file %s", pidfile_name);
157 osrfLogWarning(OSRF_LOG_MARK,
158 "Unable to open pidfile %s for reading", pidfile_name);
166 @brief Launch one or more opensrf services
167 @param hostname Full network name of the host where the process is
168 running; or 'localhost' will do.
169 @param config Name of the configuration file;
170 normally '/openils/conf/opensrf_core.xml'.
171 @param context Name of an aggregate within the configuration file,
172 containing the relevant subset of configuration stuff.
173 @param piddir Name of the PID path the PID file directory
174 @param action Name of action. Options include start, start_all, stop,
176 @param service Name of the service to start/stop. If no value is
177 specified, all C-based services are affected
178 @return - Zero if successful, or -1 if not.
181 // if service is null, all services are started
182 int osrf_system_service_ctrl(
183 const char* hostname, const char* config,
184 const char* context, const char* piddir,
185 const char* action, const char* service) {
187 // Load the conguration, open the log, open a connection to Jabber
188 if (!osrfSystemBootstrapClientResc(config, context, "c_launcher")) {
189 osrfLogError(OSRF_LOG_MARK,
190 "Unable to bootstrap for host %s from configuration file %s",
195 // Get the list of applications from the settings server
196 // sometimes the network / settings server is slow to get going, s
197 // so give it a few tries before giving up.
200 for (j = 0; j < 3; j++) {
201 retcode = osrf_settings_retrieve(hostname);
202 if (retcode == 0) break; // success
203 osrfLogInfo(OSRF_LOG_MARK,
204 "Unable to retrieve settings from settings server, retrying..");
208 // all done talking to the network
209 osrf_system_disconnect_client();
212 osrfLogWarning(OSRF_LOG_MARK, "Unable to retrieve settings for "
213 "host %s from configuration file %s", hostname, config);
214 // this usually means settings server isn't running, which can happen
215 // for a variety of reasons. Log the problem then exit cleanly.
219 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
222 osrfLogInfo(OSRF_LOG_MARK, "OpenSRF-C found no apps to run");
224 osrf_settings_free_host_config(NULL);
227 osrfStringArray* arr = osrfNewStringArray(8);
230 if(apps->type == JSON_STRING) {
231 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
234 const jsonObject* app;
235 while( (app = jsonObjectGetIndex(apps, i++)) )
236 osrfStringArrayAdd(arr, jsonObjectGetString(app));
238 jsonObjectFree(apps);
241 const char* appname = NULL;
242 while ((appname = osrfStringArrayGetString(arr, i++))) {
245 osrfLogWarning(OSRF_LOG_MARK,
246 "Invalid service name at index %d", i);
250 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
252 // this is not a C service, skip it.
253 if (!lang || strcasecmp(lang, "c")) continue;
255 // caller requested a specific service, but not this one
256 if (service && strcmp(service, appname))
260 if (!strncmp(action, "stop", 4)) {
261 stop_service(piddir, appname);
266 if ((pid = fork())) {
267 // parent process forks the Listener, logs the PID to stdout,
268 // then goes about its business
270 "* starting service pid=%ld %s\n", (long) pid, appname);
274 // this is the top-level Listener process. It's responsible
275 // for managing all of the processes related to a given service.
278 char* libfile = osrf_settings_host_value(
279 "/apps/%s/implementation", appname);
282 osrfLogError(OSRF_LOG_MARK,
283 "Service %s has no implemention", appname);
287 osrfLogInfo(OSRF_LOG_MARK,
288 "Launching application %s with implementation %s",
291 // write the PID of our newly detached process to the PID file
292 // pid file name is /path/to/dir/<service>.pid
293 char* pidfile_name = get_pid_file(piddir, appname);
294 FILE* pidfile = fopen(pidfile_name, "w");
296 osrfLogDebug(OSRF_LOG_MARK,
297 "Writing PID %ld for service %s", (long) getpid(), appname);
298 fprintf(pidfile, "%ld\n", (long) getpid());
301 osrfLogError(OSRF_LOG_MARK,
302 "Unable to open PID file '%s': %s",
303 pidfile_name, strerror(errno));
308 if (osrfAppRegisterApplication(appname, libfile) == 0)
309 osrf_prefork_run(appname);
311 osrfLogInfo(OSRF_LOG_MARK,
312 "Prefork Server exiting for service %s and implementation %s\n",
317 } // service name loop
319 // main process can now go away
320 osrfStringArrayFree(arr);
322 osrf_settings_free_host_config(NULL);
328 @brief Bootstrap a generic application from info in the configuration file.
329 @param config_file Name of the configuration file.
330 @param contextnode Name of an aggregate within the configuration file, containing the
331 relevant subset of configuration stuff.
332 @param resource Used to construct a Jabber resource name; may be NULL.
333 @return 1 if successful; zero or -1 if error.
335 - Load the configuration file.
337 - Open a connection to Jabber.
339 int osrfSystemBootstrapClientResc( const char* config_file,
340 const char* contextnode, const char* resource ) {
344 if(osrfSystemGetTransportClient()) {
345 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
346 return 1; /* we already have a client connection */
349 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
350 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
355 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
357 osrfConfigSetDefaultConfig(cfg);
359 return 0; /* Can't load configuration? Bail out */
361 // fetch list of configured log redaction marker strings
362 log_protect_arr = osrfNewStringArray(8);
363 osrfConfig* cfg_shared = osrfConfigInit(config_file, "shared");
364 osrfConfigGetValueList( cfg_shared, log_protect_arr, "/log_protect/match_string" );
367 char* log_file = osrfConfigGetValue( NULL, "/logfile");
369 fprintf(stderr, "No log file specified in configuration file %s\n",
374 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
375 osrfStringArray* arr = osrfNewStringArray(8);
376 osrfConfigGetValueList(NULL, arr, "/domain");
378 char* username = osrfConfigGetValue( NULL, "/username" );
379 char* password = osrfConfigGetValue( NULL, "/passwd" );
380 char* port = osrfConfigGetValue( NULL, "/port" );
381 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
382 char* facility = osrfConfigGetValue( NULL, "/syslog" );
383 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
385 /* if we're a source-client, tell the logger */
386 char* isclient = osrfConfigGetValue(NULL, "/client");
387 if( isclient && !strcasecmp(isclient,"true") )
388 osrfLogSetIsClient(1);
393 if(port) iport = atoi(port);
394 if(log_level) llevel = atoi(log_level);
396 if(!strcmp(log_file, "syslog")) {
397 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
398 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
399 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
402 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
403 osrfLogSetFile( log_file );
407 /* Get a domain, if one is specified */
408 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
410 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
411 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n",
417 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
418 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n",
424 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
425 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n",
430 if((iport <= 0) && !unixpath) {
431 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
432 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
438 osrfStringArrayFree(arr);
450 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
451 domain, iport, unixpath ? unixpath : "(none)" );
452 transport_client* client = client_init( domain, iport, unixpath, 0 );
454 char host[HOST_NAME_MAX + 1] = "";
455 gethostname(host, sizeof(host) );
456 host[HOST_NAME_MAX] = '\0';
460 snprintf(tbuf, 32, "%f", get_timestamp_millis());
462 if(!resource) resource = "";
464 int len = strlen(resource) + 256;
467 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
469 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
470 osrfGlobalTransportClient = client;
473 osrfStringArrayFree(arr);
483 if(osrfGlobalTransportClient)
490 @brief Disconnect from Jabber.
491 @return Zero in all cases.
493 int osrf_system_disconnect_client( void ) {
494 client_disconnect( osrfGlobalTransportClient );
495 client_free( osrfGlobalTransportClient );
496 osrfGlobalTransportClient = NULL;
501 @brief Shut down a laundry list of facilities typically used by servers.
504 - Settings from configuration file
506 - Connection to Jabber
507 - Settings from settings server
508 - Application sessions
511 int osrf_system_shutdown( void ) {
517 osrf_system_disconnect_client();
518 osrf_settings_free_host_config(NULL);
519 osrfAppSessionCleanup();
521 shutdownComplete = 1;