1 #include "osrf_system.h"
3 #include "osrf_application.h"
4 #include "osrf_prefork.h"
6 void __osrfSystemSignalHandler( int sig );
8 transport_client* __osrfGlobalTransportClient;
10 transport_client* osrfSystemGetTransportClient() {
11 return __osrfGlobalTransportClient;
14 transport_client* osrf_system_get_transport_client() {
15 return __osrfGlobalTransportClient;
18 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
19 return osrf_system_bootstrap_client_resc(config_file, contextnode, NULL);
22 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
23 return osrf_system_bootstrap_client_resc( config_file, contextnode, resource );
27 int _osrfSystemInitCache() {
29 jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
30 char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
32 if( cacheServers && maxCache) {
34 if( cacheServers->type == JSON_ARRAY ) {
36 char* servers[cacheServers->size];
37 for( i = 0; i != cacheServers->size; i++ ) {
38 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
39 osrfLogInfo("Adding cache server %s", servers[i]);
41 osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
44 char* servers[] = { jsonObjectGetString(cacheServers) };
45 osrfLogInfo("Adding cache server %s", servers[0]);
46 osrfCacheInit( servers, 1, atoi(maxCache) );
50 osrfLogError( "Missing config value for /cache/global/servers/server _or_ "
51 "/cache/global/max_cache_time");
58 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
59 if( !(hostname && configfile && contextNode) ) return -1;
61 /* first we grab the settings */
62 if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
63 osrfLogError("Unable to bootstrap");
67 osrf_settings_retrieve(hostname);
68 osrf_system_disconnect_client();
70 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
71 osrfStringArray* arr = osrfNewStringArray(8);
73 _osrfSystemInitCache();
78 if(apps->type == JSON_STRING) {
79 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
83 while( (app = jsonObjectGetIndex(apps, i++)) )
84 osrfStringArrayAdd(arr, jsonObjectGetString(app));
89 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
91 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
93 if(lang && !strcasecmp(lang,"c")) {
95 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
97 if(! (appname && libfile) ) {
98 osrfLogWarning("Missing appname / libfile in settings config");
102 osrfLogInfo("Launching application %s with implementation %s", appname, libfile);
106 if( (pid = fork()) ) {
107 // storage pid in local table for re-launching dead children...
108 osrfLogInfo("Launched application child %d", pid);
112 fprintf(stderr, " * Running application %s\n", appname);
113 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
114 osrf_prefork_run(appname);
116 osrfLogDebug("Server exiting for app %s and library %s", appname, libfile );
125 /* background and let our children do their thing */
128 signal(SIGCHLD, __osrfSystemSignalHandler);
135 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
137 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
138 osrfLogError("No Config File Specified\n" );
144 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
145 osrfConfigSetDefaultConfig(cfg);
149 char* log_file = osrfConfigGetValue( NULL, "/logfile");
150 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
151 osrfStringArray* arr = osrfNewStringArray(8);
152 osrfConfigGetValueList(NULL, arr, "/domains/domain");
153 char* username = osrfConfigGetValue( NULL, "/username" );
154 char* password = osrfConfigGetValue( NULL, "/passwd" );
155 char* port = osrfConfigGetValue( NULL, "/port" );
156 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
157 char* facility = osrfConfigGetValue( NULL, "/syslog" );
159 char* domain = strdup(osrfStringArrayGetString( arr, 0 )); /* just the first for now */
160 osrfStringArrayFree(arr);
165 if(port) iport = atoi(port);
166 if(log_level) llevel = atoi(log_level);
168 if(!log_file) { fprintf(stderr, "Log file needed\n"); return -1; }
170 if(!strcmp(log_file, "syslog")) {
171 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
172 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
175 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
176 osrfLogSetFile( log_file );
179 osrfLogInfo("Bootstrapping system with domain %s, port %d, and unixpath %s", domain, iport, unixpath );
181 transport_client* client = client_init( domain, iport, unixpath, 0 );
184 host = getenv("HOSTNAME");
187 if(!resource) resource = "";
189 int len = strlen(resource) + 256;
192 snprintf(buf, len - 1, "%s_%s_%d", resource, host, getpid() );
194 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
195 /* child nodes will leak the parents client... but we can't free
196 it without disconnecting the parents client :( */
197 __osrfGlobalTransportClient = client;
208 if(__osrfGlobalTransportClient)
214 int osrf_system_disconnect_client() {
215 client_disconnect( __osrfGlobalTransportClient );
216 client_free( __osrfGlobalTransportClient );
217 __osrfGlobalTransportClient = NULL;
221 int osrf_system_shutdown() {
223 osrf_system_disconnect_client();
224 osrf_settings_free_host_config(NULL);
231 void __osrfSystemSignalHandler( int sig ) {
236 while( (pid = waitpid(-1, &status, WNOHANG)) > 0) {
237 osrfLogWarning("We lost child %d", pid);
240 /** relaunch the server **/