]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_system.c
rolling back hostname change because of mysterious performance problem
[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( !(hostname && 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* lang = osrf_settings_host_value("/apps/%s/language", appname);
91
92                         if(lang && !strcasecmp(lang,"c"))  {
93
94                                 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
95                 
96                                 if(! (appname && libfile) ) {
97                                         warning_handler("Missing appname / libfile in settings config");
98                                         continue;
99                                 }
100
101                                 info_handler("Launching application %s with implementation %s", appname, libfile);
102                 
103                                 int pid;
104                 
105                                 if( (pid = fork()) ) { 
106                                         // storage pid in local table for re-launching dead children...
107                                         info_handler("Launched application child %d", pid);
108         
109                                 } else {
110                 
111                                         fprintf(stderr, " * Running application %s\n", appname);
112                                         if( osrfAppRegisterApplication( appname, libfile ) == 0 ) 
113                                                 osrf_prefork_run(appname);
114         
115                                         debug_handler("Server exiting for app %s and library %s", appname, libfile );
116                                         exit(0);
117                                 }
118                         } // language == c
119                 } 
120         }
121
122         /** daemonize me **/
123
124         /* background and let our children do their thing */
125         daemonize();
126         while(1) {
127                 signal(SIGCHLD, __osrfSystemSignalHandler);
128                 sleep(10000);
129         }
130         
131         return 0;
132 }
133
134 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
135
136         if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() )
137                 return fatal_handler("No Config File Specified\n" );
138
139         if( config_file ) {
140                 osrfConfigCleanup();
141                 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
142                 osrfConfigSetDefaultConfig(cfg);
143         }
144
145
146         char* log_file          = osrfConfigGetValue( NULL, "/logfile");
147         char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
148         osrfStringArray* arr = osrfNewStringArray(8);
149         osrfConfigGetValueList(NULL, arr, "/domains/domain");
150         char* username          = osrfConfigGetValue( NULL, "/username" );
151         char* password          = osrfConfigGetValue( NULL, "/passwd" );
152         char* port                      = osrfConfigGetValue( NULL, "/port" );
153         char* unixpath          = osrfConfigGetValue( NULL, "/unixpath" );
154
155         char* domain = strdup(osrfStringArrayGetString( arr, 0 )); /* just the first for now */
156         osrfStringArrayFree(arr);
157
158
159         int llevel = 0;
160         int iport = 0;
161         if(port) iport = atoi(port);
162         if(log_level) llevel = atoi(log_level);
163
164         log_init( llevel, log_file );
165
166         info_handler("Bootstrapping system with domain %s, port %d, and unixpath %s", domain, iport, unixpath );
167
168         transport_client* client = client_init( domain, iport, unixpath, 0 );
169
170         char* host;
171         host = getenv("HOSTNAME");
172
173         if(!host) host = "";
174         if(!resource) resource = "";
175
176         int len = strlen(resource) + 256;
177         char buf[len];
178         memset(buf,0,len);
179         snprintf(buf, len - 1, "%s_%s_%d", resource, host, getpid() );
180         
181         if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
182                 /* child nodes will leak the parents client... but we can't free
183                         it without disconnecting the parents client :( */
184                 __osrfGlobalTransportClient = client;
185         }
186
187         free(log_level);
188         free(log_file);
189         free(username);
190         free(password);
191         free(port);     
192         free(unixpath);
193
194         if(__osrfGlobalTransportClient)
195                 return 1;
196
197         return 0;
198 }
199
200 int osrf_system_disconnect_client() {
201         client_disconnect( __osrfGlobalTransportClient );
202         client_free( __osrfGlobalTransportClient );
203         __osrfGlobalTransportClient = NULL;
204         return 0;
205 }
206
207 int osrf_system_shutdown() {
208         osrfConfigCleanup();
209         osrf_system_disconnect_client();
210         osrf_settings_free_host_config(NULL);
211         log_free();
212         return 1;
213 }
214
215
216
217
218 void __osrfSystemSignalHandler( int sig ) {
219
220         pid_t pid;
221         int status;
222
223         while( (pid = waitpid(-1, &status, WNOHANG)) > 0) {
224                 warning_handler("We lost child %d", pid);
225         }
226
227         /** relaunch the server **/
228 }
229
230