1 #include <opensrf/osrf_system.h>
2 #include <opensrf/osrf_application.h>
3 #include <opensrf/osrf_prefork.h>
6 static int _osrfSystemInitCache( void );
8 static transport_client* osrfGlobalTransportClient = NULL;
10 transport_client* osrfSystemGetTransportClient( void ) {
11 return osrfGlobalTransportClient;
14 void osrfSystemIgnoreTransportClient() {
15 osrfGlobalTransportClient = NULL;
18 transport_client* osrf_system_get_transport_client( void ) {
19 return osrfGlobalTransportClient;
22 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
23 return osrf_system_bootstrap_client_resc(config_file, contextnode, NULL);
26 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
27 return osrf_system_bootstrap_client_resc( config_file, contextnode, resource );
31 static int _osrfSystemInitCache( void ) {
33 jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
34 char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
36 if( cacheServers && maxCache) {
38 if( cacheServers->type == JSON_ARRAY ) {
40 char* servers[cacheServers->size];
41 for( i = 0; i != cacheServers->size; i++ ) {
42 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
43 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[i]);
45 osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
48 char* servers[] = { jsonObjectGetString(cacheServers) };
49 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
50 osrfCacheInit( servers, 1, atoi(maxCache) );
54 osrfLogError( OSRF_LOG_MARK, "Missing config value for /cache/global/servers/server _or_ "
55 "/cache/global/max_cache_time");
62 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
63 if( !(hostname && configfile && contextNode) ) return -1;
65 /* first we grab the settings */
66 if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
67 osrfLogError( OSRF_LOG_MARK,
68 "Unable to bootstrap for host %s from configuration file %s",
69 hostname, configfile );
73 int retcode = osrf_settings_retrieve(hostname);
74 osrf_system_disconnect_client();
77 osrfLogError( OSRF_LOG_MARK,
78 "Unable to retrieve settings for host %s from configuration file %s",
79 hostname, configfile );
83 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
84 osrfStringArray* arr = osrfNewStringArray(8);
86 _osrfSystemInitCache();
91 if(apps->type == JSON_STRING) {
92 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
96 while( (app = jsonObjectGetIndex(apps, i++)) )
97 osrfStringArrayAdd(arr, jsonObjectGetString(app));
100 char* appname = NULL;
102 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
104 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
106 if(lang && !strcasecmp(lang,"c")) {
108 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
110 if(! (appname && libfile) ) {
111 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
115 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
119 if( (pid = fork()) ) {
120 // storage pid in local table for re-launching dead children...
121 osrfLogInfo( OSRF_LOG_MARK, "Launched application child %ld", (long) pid);
125 fprintf(stderr, " * Running application %s\n", appname);
126 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
127 osrf_prefork_run(appname);
129 osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
138 /* background and let our children do their thing */
142 pid_t pid = wait(NULL);
145 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
147 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
150 osrfLogError(OSRF_LOG_MARK, "We lost a top-level service process with PID %ld", pid);
158 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
162 if(osrfSystemGetTransportClient()) {
163 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
164 return 1; /* we already have a client connection */
167 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
168 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
173 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
175 osrfConfigSetDefaultConfig(cfg);
177 return 0; /* Can't load configuration? Bail out */
181 char* log_file = osrfConfigGetValue( NULL, "/logfile");
182 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
183 osrfStringArray* arr = osrfNewStringArray(8);
184 osrfConfigGetValueList(NULL, arr, "/domains/domain");
186 char* username = osrfConfigGetValue( NULL, "/username" );
187 char* password = osrfConfigGetValue( NULL, "/passwd" );
188 char* port = osrfConfigGetValue( NULL, "/port" );
189 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
190 char* facility = osrfConfigGetValue( NULL, "/syslog" );
191 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
194 fprintf(stderr, "No log file specified in configuration file %s\n",
206 /* if we're a source-client, tell the logger */
207 char* isclient = osrfConfigGetValue(NULL, "/client");
208 if( isclient && !strcasecmp(isclient,"true") )
209 osrfLogSetIsClient(1);
214 if(port) iport = atoi(port);
215 if(log_level) llevel = atoi(log_level);
217 if(!strcmp(log_file, "syslog")) {
218 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
219 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
220 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
223 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
224 osrfLogSetFile( log_file );
228 /* Get a domain, if one is specified */
229 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
231 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
232 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
237 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
238 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
243 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
244 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
248 if((iport <= 0) && !unixpath) {
249 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
250 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
256 osrfStringArrayFree(arr);
267 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
268 domain, iport, unixpath ? unixpath : "(none)" );
269 transport_client* client = client_init( domain, iport, unixpath, 0 );
272 host = getenv("HOSTNAME");
276 snprintf(tbuf, 32, "%f", get_timestamp_millis());
279 if(!resource) resource = "";
281 int len = strlen(resource) + 256;
284 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
286 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
287 /* child nodes will leak the parents client... but we can't free
288 it without disconnecting the parents client :( */
289 osrfGlobalTransportClient = client;
292 osrfStringArrayFree(arr);
302 if(osrfGlobalTransportClient)
308 int osrf_system_disconnect_client( void ) {
309 client_disconnect( osrfGlobalTransportClient );
310 client_free( osrfGlobalTransportClient );
311 osrfGlobalTransportClient = NULL;
315 int osrf_system_shutdown( void ) {
317 osrf_system_disconnect_client();
318 osrf_settings_free_host_config(NULL);
319 osrfAppSessionCleanup();