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", appname, libfile );
130 /* background and let our children do their thing */
133 signal(SIGCHLD, osrfSystemSignalHandler);
140 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
144 if(osrfSystemGetTransportClient()) {
145 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
146 return 1; /* we already have a client connection */
149 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
150 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
155 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
157 osrfConfigSetDefaultConfig(cfg);
159 return 0; /* Can't load configuration? Bail out */
163 char* log_file = osrfConfigGetValue( NULL, "/logfile");
164 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
165 osrfStringArray* arr = osrfNewStringArray(8);
166 osrfConfigGetValueList(NULL, arr, "/domains/domain");
168 char* username = osrfConfigGetValue( NULL, "/username" );
169 char* password = osrfConfigGetValue( NULL, "/passwd" );
170 char* port = osrfConfigGetValue( NULL, "/port" );
171 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
172 char* facility = osrfConfigGetValue( NULL, "/syslog" );
173 char* actlog = osrfConfigGetValue( NULL, "/actlog" );
176 fprintf(stderr, "No log file specified in configuration file %s\n",
188 /* if we're a source-client, tell the logger */
189 char* isclient = osrfConfigGetValue(NULL, "/client");
190 if( isclient && !strcasecmp(isclient,"true") )
191 osrfLogSetIsClient(1);
196 if(port) iport = atoi(port);
197 if(log_level) llevel = atoi(log_level);
199 if(!strcmp(log_file, "syslog")) {
200 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
201 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
202 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
205 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
206 osrfLogSetFile( log_file );
210 /* Get a domain, if one is specified */
211 const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
213 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
214 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
219 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
220 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
225 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
226 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
230 if((iport <= 0) && !unixpath) {
231 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
232 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
238 osrfStringArrayFree(arr);
249 osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
250 domain, iport, unixpath ? unixpath : "(none)" );
251 transport_client* client = client_init( domain, iport, unixpath, 0 );
254 host = getenv("HOSTNAME");
258 snprintf(tbuf, 32, "%f", get_timestamp_millis());
261 if(!resource) resource = "";
263 int len = strlen(resource) + 256;
266 snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
268 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
269 /* child nodes will leak the parents client... but we can't free
270 it without disconnecting the parents client :( */
271 osrfGlobalTransportClient = client;
274 osrfStringArrayFree(arr);
284 if(osrfGlobalTransportClient)
290 int osrf_system_disconnect_client( void ) {
291 client_disconnect( osrfGlobalTransportClient );
292 client_free( osrfGlobalTransportClient );
293 osrfGlobalTransportClient = NULL;
297 int osrf_system_shutdown( void ) {
299 osrf_system_disconnect_client();
300 osrf_settings_free_host_config(NULL);
301 osrfAppSessionCleanup();
309 static void osrfSystemSignalHandler( int sig ) {
314 while( (pid = waitpid(-1, &status, WNOHANG)) > 0) {
315 osrfLogWarning( OSRF_LOG_MARK, "We lost child %d", pid);
318 /** relaunch the server **/