1 #include "osrf_system.h"
3 #include "osrf_application.h"
4 #include "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, "Unable to bootstrap");
71 osrf_settings_retrieve(hostname);
72 osrf_system_disconnect_client();
74 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
75 osrfStringArray* arr = osrfNewStringArray(8);
77 _osrfSystemInitCache();
82 if(apps->type == JSON_STRING) {
83 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
87 while( (app = jsonObjectGetIndex(apps, i++)) )
88 osrfStringArrayAdd(arr, jsonObjectGetString(app));
93 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
95 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
97 if(lang && !strcasecmp(lang,"c")) {
99 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
101 if(! (appname && libfile) ) {
102 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
106 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
110 if( (pid = fork()) ) {
111 // storage pid in local table for re-launching dead children...
112 osrfLogInfo( OSRF_LOG_MARK, "Launched application child %ld", (long) pid);
116 fprintf(stderr, " * Running application %s\n", appname);
117 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
118 osrf_prefork_run(appname);
120 osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
129 /* background and let our children do their thing */
133 pid_t pid = wait(NULL);
136 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
138 char* err_str = strerror(errno);
139 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", err_str);
143 osrfLogError(OSRF_LOG_MARK, "We lost a top-level service process with PID %ld", pid);
151 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
155 if(osrfSystemGetTransportClient()) {
156 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
157 return 1; /* we already have a client connection */
160 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
161 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
166 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
168 osrfConfigSetDefaultConfig(cfg);
170 return 0; /* Can't load configuration? Bail out */
174 char* log_file = osrfConfigGetValue( NULL, "/logfile");
175 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
176 osrfStringArray* arr = osrfNewStringArray(8);
177 osrfConfigGetValueList(NULL, arr, "/domains/domain");
179 char* username = osrfConfigGetValue( NULL, "/username" );
180 char* password = osrfConfigGetValue( NULL, "/passwd" );
181 char* port = osrfConfigGetValue( NULL, "/port" );
182 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
183 char* facility = osrfConfigGetValue( NULL, "/syslog" );
184 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
187 fprintf(stderr, "No log file specified in configuration file %s\n",
199 /* if we're a source-client, tell the logger */
200 char* isclient = osrfConfigGetValue(NULL, "/client");
201 if( isclient && !strcasecmp(isclient,"true") )
202 osrfLogSetIsClient(1);
207 if(port) iport = atoi(port);
208 if(log_level) llevel = atoi(log_level);
210 if(!strcmp(log_file, "syslog")) {
211 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
212 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
213 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
216 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
217 osrfLogSetFile( log_file );
221 /* Get a domain, if one is specified */
222 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
224 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
225 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
230 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
231 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
236 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
237 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
241 if((iport <= 0) && !unixpath) {
242 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
243 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
249 osrfStringArrayFree(arr);
260 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
261 domain, iport, unixpath ? unixpath : "(none)" );
262 transport_client* client = client_init( domain, iport, unixpath, 0 );
265 host = getenv("HOSTNAME");
269 snprintf(tbuf, 32, "%f", get_timestamp_millis());
272 if(!resource) resource = "";
274 int len = strlen(resource) + 256;
277 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
279 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
280 /* child nodes will leak the parents client... but we can't free
281 it without disconnecting the parents client :( */
282 osrfGlobalTransportClient = client;
285 osrfStringArrayFree(arr);
295 if(osrfGlobalTransportClient)
301 int osrf_system_disconnect_client( void ) {
302 client_disconnect( osrfGlobalTransportClient );
303 client_free( osrfGlobalTransportClient );
304 osrfGlobalTransportClient = NULL;
308 int osrf_system_shutdown( void ) {
310 osrf_system_disconnect_client();
311 osrf_settings_free_host_config(NULL);
312 osrfAppSessionCleanup();