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 static transport_client* osrfGlobalTransportClient = NULL;
11 transport_client* osrfSystemGetTransportClient( void ) {
12 return osrfGlobalTransportClient;
15 void osrfSystemIgnoreTransportClient() {
16 osrfGlobalTransportClient = NULL;
19 transport_client* osrf_system_get_transport_client( void ) {
20 return osrfGlobalTransportClient;
23 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
24 return osrf_system_bootstrap_client_resc(config_file, contextnode, NULL);
27 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
28 return osrf_system_bootstrap_client_resc( config_file, contextnode, resource );
32 static int _osrfSystemInitCache( void ) {
34 jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
35 char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
37 if( cacheServers && maxCache) {
39 if( cacheServers->type == JSON_ARRAY ) {
41 char* servers[cacheServers->size];
42 for( i = 0; i != cacheServers->size; i++ ) {
43 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
44 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[i]);
46 osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
49 char* servers[] = { jsonObjectGetString(cacheServers) };
50 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
51 osrfCacheInit( servers, 1, atoi(maxCache) );
55 osrfLogError( OSRF_LOG_MARK, "Missing config value for /cache/global/servers/server _or_ "
56 "/cache/global/max_cache_time");
63 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
64 if( !(hostname && configfile && contextNode) ) return -1;
66 /* first we grab the settings */
67 if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
68 osrfLogError( OSRF_LOG_MARK,
69 "Unable to bootstrap for host %s from configuration file %s",
70 hostname, configfile );
74 int retcode = osrf_settings_retrieve(hostname);
75 osrf_system_disconnect_client();
78 osrfLogError( OSRF_LOG_MARK,
79 "Unable to retrieve settings for host %s from configuration file %s",
80 hostname, configfile );
85 /* background and let our children do their thing */
86 /* NOTE: This has been moved from below the 'if (apps)' block below ... move it back if things go crazy */
89 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
90 osrfStringArray* arr = osrfNewStringArray(8);
92 _osrfSystemInitCache();
97 if(apps->type == JSON_STRING) {
98 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
102 while( (app = jsonObjectGetIndex(apps, i++)) )
103 osrfStringArrayAdd(arr, jsonObjectGetString(app));
106 char* appname = NULL;
108 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
110 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
112 if(lang && !strcasecmp(lang,"c")) {
114 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
116 if(! (appname && libfile) ) {
117 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
121 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
125 if( (pid = fork()) ) {
126 // storage pid in local table for re-launching dead children...
127 osrfLogInfo( OSRF_LOG_MARK, "Launched application child %ld", (long) pid);
131 osrfLogError( OSRF_LOG_MARK, " * Running application %s\n", appname);
132 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
133 osrf_prefork_run(appname);
135 osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
140 } // should we do something if there are no apps? does the wait(NULL) below do that for us?
145 pid_t pid = wait( &status );
148 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
150 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
153 report_child_status( pid, status );
161 static void report_child_status( pid_t pid, int status ) {
163 if( WIFEXITED( status ) )
165 int rc = WEXITSTATUS( status ); // return code of child process
167 osrfLogError( OSRF_LOG_MARK, "Child process %ld exited with return code %d",
170 osrfLogError( OSRF_LOG_MARK, "Child process %ld exited normally", (long) pid );
172 else if( WIFSIGNALED( status ) )
174 osrfLogError( OSRF_LOG_MARK, "Child process %ld killed by signal %d",
175 (long) pid, WTERMSIG( status) );
177 else if( WIFSTOPPED( status ) )
179 osrfLogError( OSRF_LOG_MARK, "Child process %ld stopped by signal %d",
180 (long) pid, (int) WSTOPSIG( status ) );
185 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
189 if(osrfSystemGetTransportClient()) {
190 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
191 return 1; /* we already have a client connection */
194 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
195 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
200 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
202 osrfConfigSetDefaultConfig(cfg);
204 return 0; /* Can't load configuration? Bail out */
208 char* log_file = osrfConfigGetValue( NULL, "/logfile");
209 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
210 osrfStringArray* arr = osrfNewStringArray(8);
211 osrfConfigGetValueList(NULL, arr, "/domains/domain");
213 char* username = osrfConfigGetValue( NULL, "/username" );
214 char* password = osrfConfigGetValue( NULL, "/passwd" );
215 char* port = osrfConfigGetValue( NULL, "/port" );
216 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
217 char* facility = osrfConfigGetValue( NULL, "/syslog" );
218 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
221 fprintf(stderr, "No log file specified in configuration file %s\n",
233 /* if we're a source-client, tell the logger */
234 char* isclient = osrfConfigGetValue(NULL, "/client");
235 if( isclient && !strcasecmp(isclient,"true") )
236 osrfLogSetIsClient(1);
241 if(port) iport = atoi(port);
242 if(log_level) llevel = atoi(log_level);
244 if(!strcmp(log_file, "syslog")) {
245 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
246 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
247 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
250 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
251 osrfLogSetFile( log_file );
255 /* Get a domain, if one is specified */
256 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
258 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
259 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
264 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
265 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
270 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
271 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
275 if((iport <= 0) && !unixpath) {
276 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
277 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
283 osrfStringArrayFree(arr);
294 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
295 domain, iport, unixpath ? unixpath : "(none)" );
296 transport_client* client = client_init( domain, iport, unixpath, 0 );
299 host = getenv("HOSTNAME");
303 snprintf(tbuf, 32, "%f", get_timestamp_millis());
306 if(!resource) resource = "";
308 int len = strlen(resource) + 256;
311 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
313 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
314 /* child nodes will leak the parents client... but we can't free
315 it without disconnecting the parents client :( */
316 osrfGlobalTransportClient = client;
319 osrfStringArrayFree(arr);
329 if(osrfGlobalTransportClient)
335 int osrf_system_disconnect_client( void ) {
336 client_disconnect( osrfGlobalTransportClient );
337 client_free( osrfGlobalTransportClient );
338 osrfGlobalTransportClient = NULL;
342 int osrf_system_shutdown( void ) {
344 osrf_system_disconnect_client();
345 osrf_settings_free_host_config(NULL);
346 osrfAppSessionCleanup();