1 #include "osrf_system.h"
3 #include "osrf_application.h"
4 #include "osrf_prefork.h"
6 static void osrfSystemSignalHandler( int sig );
7 static int _osrfSystemInitCache( void );
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, "Unable to bootstrap");
72 osrf_settings_retrieve(hostname);
73 osrf_system_disconnect_client();
75 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
76 osrfStringArray* arr = osrfNewStringArray(8);
78 _osrfSystemInitCache();
83 if(apps->type == JSON_STRING) {
84 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
88 while( (app = jsonObjectGetIndex(apps, i++)) )
89 osrfStringArrayAdd(arr, jsonObjectGetString(app));
94 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
96 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
98 if(lang && !strcasecmp(lang,"c")) {
100 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
102 if(! (appname && libfile) ) {
103 osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
107 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
111 if( (pid = fork()) ) {
112 // storage pid in local table for re-launching dead children...
113 osrfLogInfo( OSRF_LOG_MARK, "Launched application child %d", pid);
117 fprintf(stderr, " * Running application %s\n", appname);
118 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
119 osrf_prefork_run(appname);
121 osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
130 /* background and let our children do their thing */
134 pid_t pid = wait( NULL );
136 if ( errno != ECHILD ) {
137 char* err_str = strerror( errno );
138 osrfLogWarning( OSRF_LOG_MARK, "Exiting on singal or error: %s\n", err_str );
143 osrfLogWarning( OSRF_LOG_MARK, "We lost child %d", pid);
150 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
154 if(osrfSystemGetTransportClient()) {
155 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
156 return 1; /* we already have a client connection */
159 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
160 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
165 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
167 osrfConfigSetDefaultConfig(cfg);
169 return 0; /* Can't load configuration? Bail out */
173 char* log_file = osrfConfigGetValue( NULL, "/logfile");
174 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
175 osrfStringArray* arr = osrfNewStringArray(8);
176 osrfConfigGetValueList(NULL, arr, "/domains/domain");
178 char* username = osrfConfigGetValue( NULL, "/username" );
179 char* password = osrfConfigGetValue( NULL, "/passwd" );
180 char* port = osrfConfigGetValue( NULL, "/port" );
181 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
182 char* facility = osrfConfigGetValue( NULL, "/syslog" );
183 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
186 fprintf(stderr, "No log file specified in configuration file %s\n",
198 /* if we're a source-client, tell the logger */
199 char* isclient = osrfConfigGetValue(NULL, "/client");
200 if( isclient && !strcasecmp(isclient,"true") )
201 osrfLogSetIsClient(1);
206 if(port) iport = atoi(port);
207 if(log_level) llevel = atoi(log_level);
209 if(!strcmp(log_file, "syslog")) {
210 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
211 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
212 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
215 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
216 osrfLogSetFile( log_file );
220 /* Get a domain, if one is specified */
221 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
223 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
224 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
229 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
230 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
235 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
236 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
240 if((iport <= 0) && !unixpath) {
241 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
242 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
248 osrfStringArrayFree(arr);
259 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
260 domain, iport, unixpath ? unixpath : "(none)" );
261 transport_client* client = client_init( domain, iport, unixpath, 0 );
264 host = getenv("HOSTNAME");
268 snprintf(tbuf, 32, "%f", get_timestamp_millis());
271 if(!resource) resource = "";
273 int len = strlen(resource) + 256;
276 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
278 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
279 /* child nodes will leak the parents client... but we can't free
280 it without disconnecting the parents client :( */
281 osrfGlobalTransportClient = client;
284 osrfStringArrayFree(arr);
294 if(osrfGlobalTransportClient)
300 int osrf_system_disconnect_client( void ) {
301 client_disconnect( osrfGlobalTransportClient );
302 client_free( osrfGlobalTransportClient );
303 osrfGlobalTransportClient = NULL;
307 int osrf_system_shutdown( void ) {
309 osrf_system_disconnect_client();
310 osrf_settings_free_host_config(NULL);
311 osrfAppSessionCleanup();
319 static void osrfSystemSignalHandler( int sig ) {
324 while( (pid = waitpid(-1, &status, WNOHANG)) > 0) {
325 osrfLogWarning( OSRF_LOG_MARK, "We lost child %d", pid);
328 /** relaunch the server **/