eac1c692d665ce8ea15922059cfefd33c55fccea
[Evergreen.git] / OpenSRF / src / libstack / osrf_system.c
1 #include "osrf_system.h"
2 #include <signal.h>
3 #include "osrf_application.h"
4 #include "osrf_prefork.h"
5
6 void __osrfSystemSignalHandler( int sig );
7
8 transport_client* __osrfGlobalTransportClient;
9
10 transport_client* osrfSystemGetTransportClient() {
11         return __osrfGlobalTransportClient;
12 }
13
14 transport_client* osrf_system_get_transport_client() {
15         return __osrfGlobalTransportClient;
16 }
17
18 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
19         return osrf_system_bootstrap_client_resc(config_file, contextnode, NULL);
20 }
21
22 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
23         return osrf_system_bootstrap_client_resc( config_file, contextnode, resource );
24 }
25
26
27 int _osrfSystemInitCache() {
28
29         jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
30         char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
31
32         if( cacheServers && maxCache) {
33
34                 if( cacheServers->type == JSON_ARRAY ) {
35                         int i;
36                         char* servers[cacheServers->size];
37                         for( i = 0; i != cacheServers->size; i++ ) {
38                                 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
39                                 info_handler("Adding cache server %s", servers[i]);
40                         }
41                         osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
42
43                 } else {
44                         char* servers[] = { jsonObjectGetString(cacheServers) };                
45                         info_handler("Adding cache server %s", servers[0]);
46                         osrfCacheInit( servers, 1, atoi(maxCache) );
47                 }
48
49         } else {
50                 fatal_handler( "Missing config value for /cache/global/servers/server _or_ "
51                         "/cache/global/max_cache_time");
52         }
53
54         return 0;
55 }
56
57
58 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
59         if( !(configfile && contextNode) ) return -1;
60
61         /* first we grab the settings */
62         if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
63                 return fatal_handler("Unable to bootstrap");
64         }
65
66         osrf_settings_retrieve(hostname);
67         osrf_system_disconnect_client();
68
69         jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
70         osrfStringArray* arr = osrfNewStringArray(8);
71         
72         _osrfSystemInitCache();
73
74         if(apps) {
75                 int i = 0;
76
77                 if(apps->type == JSON_STRING) {
78                         osrfStringArrayAdd(arr, jsonObjectGetString(apps));
79
80                 } else {
81                         jsonObject* app;
82                         while( (app = jsonObjectGetIndex(apps, i++)) ) 
83                                 osrfStringArrayAdd(arr, jsonObjectGetString(app));
84                 }
85
86                 char* appname = NULL;
87                 i = 0;
88                 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
89
90                         char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
91                         info_handler("Launching application %s with implementation %s", appname, libfile);
92         
93                         if(! (appname && libfile) ) {
94                                 warning_handler("Missing appname / libfile in settings config");
95                                 continue;
96                         }
97         
98                         int pid;
99         
100                         if( (pid = fork()) ) { 
101                                 // storage pid in local table for re-launching dead children...
102                                 info_handler("Launched application child %d", pid);
103
104                         } else {
105         
106                                 fprintf(stderr, " * Running application %s\n", appname);
107                                 if( osrfAppRegisterApplication( appname, libfile ) == 0 ) 
108                                         osrf_prefork_run(appname);
109
110                                 debug_handler("Server exiting for app %s and library %s", appname, libfile );
111                                 exit(0);
112                         }
113                 }
114         }
115
116         /** daemonize me **/
117
118         /* background and let our children do their thing */
119         daemonize();
120         while(1) {
121                 signal(SIGCHLD, __osrfSystemSignalHandler);
122                 sleep(10000);
123         }
124         
125         return 0;
126 }
127
128 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
129
130         if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() )
131                 return fatal_handler("No Config File Specified\n" );
132
133         if( config_file ) {
134                 osrfConfigCleanup();
135                 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
136                 osrfConfigSetDefaultConfig(cfg);
137         }
138
139
140         char* log_file          = osrfConfigGetValue( NULL, "/logfile");
141         char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
142         osrfStringArray* arr = osrfNewStringArray(8);
143         osrfConfigGetValueList(NULL, arr, "/domains/domain");
144         char* username          = osrfConfigGetValue( NULL, "/username" );
145         char* password          = osrfConfigGetValue( NULL, "/passwd" );
146         char* port                      = osrfConfigGetValue( NULL, "/port" );
147         char* unixpath          = osrfConfigGetValue( NULL, "/unixpath" );
148
149         char* domain = strdup(osrfStringArrayGetString( arr, 0 )); /* just the first for now */
150         osrfStringArrayFree(arr);
151
152
153         int llevel = 0;
154         int iport = 0;
155         if(port) iport = atoi(port);
156         if(log_level) llevel = atoi(log_level);
157
158         log_init( llevel, log_file );
159
160         info_handler("Bootstrapping system with domain %s, port %d, and unixpath %s", domain, iport, unixpath );
161
162         transport_client* client = client_init( domain, iport, unixpath, 0 );
163
164         char* host = getenv("HOSTNAME");
165
166         if(!resource) resource = "";
167         int len = strlen(resource) + 256;
168         char buf[len];
169         memset(buf,0,len);
170         snprintf(buf, len - 1, "opensrf_%s_%s_%d", resource, host, getpid() );
171         
172         if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
173                 /* child nodes will leak the parents client... but we can't free
174                         it without disconnecting the parents client :( */
175                 __osrfGlobalTransportClient = client;
176         }
177
178         free(log_level);
179         free(log_file);
180         free(username);
181         free(password);
182         free(port);     
183         free(unixpath);
184
185         if(__osrfGlobalTransportClient)
186                 return 1;
187
188         return 0;
189 }
190
191 int osrf_system_disconnect_client() {
192         client_disconnect( __osrfGlobalTransportClient );
193         client_free( __osrfGlobalTransportClient );
194         __osrfGlobalTransportClient = NULL;
195         return 0;
196 }
197
198 int osrf_system_shutdown() {
199         osrfConfigCleanup();
200         osrf_system_disconnect_client();
201         osrf_settings_free_host_config(NULL);
202         log_free();
203         return 1;
204 }
205
206
207
208
209 void __osrfSystemSignalHandler( int sig ) {
210
211         pid_t pid;
212         int status;
213
214         while( (pid = waitpid(-1, &status, WNOHANG)) > 0) {
215                 warning_handler("We lost child %d", pid);
216         }
217
218         /** relaunch the server **/
219 }
220
221