1 #include "osrf_system.h"
3 #include "osrf_application.h"
4 #include "osrf_prefork.h"
6 char* __osrfSystemHostname = NULL;
8 void __osrfSystemSignalHandler( int sig );
10 transport_client* __osrfGlobalTransportClient;
12 transport_client* osrfSystemGetTransportClient() {
13 return __osrfGlobalTransportClient;
16 transport_client* osrf_system_get_transport_client() {
17 return __osrfGlobalTransportClient;
20 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
21 return osrf_system_bootstrap_client_resc(config_file, contextnode, NULL);
24 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
25 return osrf_system_bootstrap_client_resc( config_file, contextnode, resource );
29 int _osrfSystemInitCache() {
31 jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
32 char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
34 if( cacheServers && maxCache) {
36 if( cacheServers->type == JSON_ARRAY ) {
38 char* servers[cacheServers->size];
39 for( i = 0; i != cacheServers->size; i++ ) {
40 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
41 info_handler("Adding cache server %s", servers[i]);
43 osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
46 char* servers[] = { jsonObjectGetString(cacheServers) };
47 info_handler("Adding cache server %s", servers[0]);
48 osrfCacheInit( servers, 1, atoi(maxCache) );
52 fatal_handler( "Missing config value for /cache/global/servers/server _or_ "
53 "/cache/global/max_cache_time");
60 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
61 if( !(hostname && configfile && contextNode) ) return -1;
63 __osrfSystemHostname = strdup(hostname);
65 /* first we grab the settings */
66 if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
67 return fatal_handler("Unable to bootstrap");
70 osrf_settings_retrieve(hostname);
71 osrf_system_disconnect_client();
73 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
74 osrfStringArray* arr = osrfNewStringArray(8);
76 _osrfSystemInitCache();
81 if(apps->type == JSON_STRING) {
82 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
86 while( (app = jsonObjectGetIndex(apps, i++)) )
87 osrfStringArrayAdd(arr, jsonObjectGetString(app));
92 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
94 char* lang = osrf_settings_host_value("/apps/%s/language", appname);
96 if(lang && !strcasecmp(lang,"c")) {
98 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
100 if(! (appname && libfile) ) {
101 warning_handler("Missing appname / libfile in settings config");
105 info_handler("Launching application %s with implementation %s", appname, libfile);
109 if( (pid = fork()) ) {
110 // storage pid in local table for re-launching dead children...
111 info_handler("Launched application child %d", pid);
115 fprintf(stderr, " * Running application %s\n", appname);
116 if( osrfAppRegisterApplication( appname, libfile ) == 0 )
117 osrf_prefork_run(appname);
119 debug_handler("Server exiting for app %s and library %s", appname, libfile );
128 /* background and let our children do their thing */
131 signal(SIGCHLD, __osrfSystemSignalHandler);
138 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
140 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() )
141 return fatal_handler("No Config File Specified\n" );
145 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
146 osrfConfigSetDefaultConfig(cfg);
150 char* log_file = osrfConfigGetValue( NULL, "/logfile");
151 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
152 osrfStringArray* arr = osrfNewStringArray(8);
153 osrfConfigGetValueList(NULL, arr, "/domains/domain");
154 char* username = osrfConfigGetValue( NULL, "/username" );
155 char* password = osrfConfigGetValue( NULL, "/passwd" );
156 char* port = osrfConfigGetValue( NULL, "/port" );
157 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
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 log_init( llevel, log_file );
170 info_handler("Bootstrapping system with domain %s, port %d, and unixpath %s", domain, iport, unixpath );
172 transport_client* client = client_init( domain, iport, unixpath, 0 );
175 if(__osrfSystemHostname) host = __osrfSystemHostname;
176 else host = getenv("HOSTNAME");
177 if( host == NULL ) host = "";
179 if(!resource) resource = "";
180 int len = strlen(resource) + 256;
183 snprintf(buf, len - 1, "opensrf_%s_%s_%d", resource, host, getpid() );
185 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
186 /* child nodes will leak the parents client... but we can't free
187 it without disconnecting the parents client :( */
188 __osrfGlobalTransportClient = client;
198 if(__osrfGlobalTransportClient)
204 int osrf_system_disconnect_client() {
205 client_disconnect( __osrfGlobalTransportClient );
206 client_free( __osrfGlobalTransportClient );
207 __osrfGlobalTransportClient = NULL;
211 int osrf_system_shutdown() {
213 osrf_system_disconnect_client();
214 osrf_settings_free_host_config(NULL);
222 void __osrfSystemSignalHandler( int sig ) {
227 while( (pid = waitpid(-1, &status, WNOHANG)) > 0) {
228 warning_handler("We lost child %d", pid);
231 /** relaunch the server **/