]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrf_system.c
Change suggested by Scott McKellar; handle c-app children death without a
[OpenSRF.git] / 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 static void osrfSystemSignalHandler( int sig );
7 static int _osrfSystemInitCache( void );
8
9 static transport_client* osrfGlobalTransportClient = NULL;
10
11 transport_client* osrfSystemGetTransportClient( void ) {
12         return osrfGlobalTransportClient;
13 }
14
15 void osrfSystemIgnoreTransportClient() {
16         osrfGlobalTransportClient = NULL;
17 }
18
19 transport_client* osrf_system_get_transport_client( void ) {
20         return osrfGlobalTransportClient;
21 }
22
23 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
24         return osrf_system_bootstrap_client_resc(config_file, contextnode, NULL);
25 }
26
27 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
28         return osrf_system_bootstrap_client_resc( config_file, contextnode, resource );
29 }
30
31
32 static int _osrfSystemInitCache( void ) {
33
34         jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
35         char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
36
37         if( cacheServers && maxCache) {
38
39                 if( cacheServers->type == JSON_ARRAY ) {
40                         int i;
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]);
45                         }
46                         osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
47
48                 } else {
49                         char* servers[] = { jsonObjectGetString(cacheServers) };                
50                         osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
51                         osrfCacheInit( servers, 1, atoi(maxCache) );
52                 }
53
54         } else {
55                 osrfLogError( OSRF_LOG_MARK,  "Missing config value for /cache/global/servers/server _or_ "
56                         "/cache/global/max_cache_time");
57         }
58
59         return 0;
60 }
61
62
63 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
64         if( !(hostname && configfile && contextNode) ) return -1;
65
66         /* first we grab the settings */
67         if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
68                 osrfLogError( OSRF_LOG_MARK, "Unable to bootstrap");
69                 return -1;
70         }
71
72         osrf_settings_retrieve(hostname);
73         osrf_system_disconnect_client();
74
75         jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
76         osrfStringArray* arr = osrfNewStringArray(8);
77         
78         _osrfSystemInitCache();
79
80         if(apps) {
81                 int i = 0;
82
83                 if(apps->type == JSON_STRING) {
84                         osrfStringArrayAdd(arr, jsonObjectGetString(apps));
85
86                 } else {
87                         jsonObject* app;
88                         while( (app = jsonObjectGetIndex(apps, i++)) ) 
89                                 osrfStringArrayAdd(arr, jsonObjectGetString(app));
90                 }
91
92                 char* appname = NULL;
93                 i = 0;
94                 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
95
96                         char* lang = osrf_settings_host_value("/apps/%s/language", appname);
97
98                         if(lang && !strcasecmp(lang,"c"))  {
99
100                                 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
101                 
102                                 if(! (appname && libfile) ) {
103                                         osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
104                                         continue;
105                                 }
106
107                                 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
108                 
109                                 int pid;
110                 
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);
114         
115                                 } else {
116                 
117                                         fprintf(stderr, " * Running application %s\n", appname);
118                                         if( osrfAppRegisterApplication( appname, libfile ) == 0 ) 
119                                                 osrf_prefork_run(appname);
120         
121                                         osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
122                                         exit(0);
123                                 }
124                         } // language == c
125                 } 
126         }
127
128         /** daemonize me **/
129
130         /* background and let our children do their thing */
131         daemonize();
132         while(1) {
133                 errno = 0;
134                 pid_t pid = wait( NULL );
135                 if ( -1 == pid ) {
136                         if ( errno != ECHILD ) {
137                                 char* err_str = strerror( errno );
138                                 osrfLogWarning( OSRF_LOG_MARK, "Exiting on singal or error: %s\n", err_str );
139                                 free(err_str);
140                         }
141                         break;
142                 } else {
143                         osrfLogWarning( OSRF_LOG_MARK, "We lost child %d", pid);
144                 }
145         }
146
147         return 0;
148 }
149
150 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
151
152         int failure = 0;
153
154         if(osrfSystemGetTransportClient()) {
155                 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
156                 return 1; /* we already have a client connection */
157         }
158
159         if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
160                 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
161                 return -1;
162         }
163
164         if( config_file ) {
165                 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
166                 if(cfg)
167                         osrfConfigSetDefaultConfig(cfg);
168                 else
169                         return 0;   /* Can't load configuration?  Bail out */
170         }
171
172
173         char* log_file          = osrfConfigGetValue( NULL, "/logfile");
174         char* log_level         = osrfConfigGetValue( NULL, "/loglevel" );
175         osrfStringArray* arr    = osrfNewStringArray(8);
176         osrfConfigGetValueList(NULL, arr, "/domains/domain");
177
178         char* username          = osrfConfigGetValue( NULL, "/username" );
179         char* password          = osrfConfigGetValue( NULL, "/passwd" );
180         char* port              = osrfConfigGetValue( NULL, "/port" );
181         char* unixpath          = osrfConfigGetValue( NULL, "/unixpath" );
182         char* facility          = osrfConfigGetValue( NULL, "/syslog" );
183         char* actlog            = osrfConfigGetValue( NULL, "/actlog" );
184
185         if(!log_file) {
186                 fprintf(stderr, "No log file specified in configuration file %s\n",
187                            config_file);
188                 free(log_level);
189                 free(username);
190                 free(password);
191                 free(port);
192                 free(unixpath);
193                 free(facility);
194                 free(actlog);
195                 return -1;
196         }
197
198         /* if we're a source-client, tell the logger */
199         char* isclient = osrfConfigGetValue(NULL, "/client");
200         if( isclient && !strcasecmp(isclient,"true") )
201                 osrfLogSetIsClient(1);
202         free(isclient);
203
204         int llevel = 0;
205         int iport = 0;
206         if(port) iport = atoi(port);
207         if(log_level) llevel = atoi(log_level);
208
209         if(!strcmp(log_file, "syslog")) {
210                 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
211                 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
212                 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
213
214         } else {
215                 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
216                 osrfLogSetFile( log_file );
217         }
218
219
220         /* Get a domain, if one is specified */
221         const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
222         if(!domain) {
223                 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
224                 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
225                 failure = 1;
226         }
227
228         if(!username) {
229                 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
230                 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
231                 failure = 1;
232         }
233
234         if(!password) {
235                 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
236                 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
237                 failure = 1;
238         }
239
240         if((iport <= 0) && !unixpath) {
241                 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
242                 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
243                         config_file);
244                 failure = 1;
245         }
246
247         if (failure) {
248                 osrfStringArrayFree(arr);
249                 free(log_level);
250                 free(username);
251                 free(password);
252                 free(port);
253                 free(unixpath);
254                 free(facility);
255                 free(actlog);
256                 return 0;
257         }
258
259         osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
260                 domain, iport, unixpath ? unixpath : "(none)" );
261         transport_client* client = client_init( domain, iport, unixpath, 0 );
262
263         const char* host;
264         host = getenv("HOSTNAME");
265
266         char tbuf[32];
267         tbuf[0] = '\0';
268         snprintf(tbuf, 32, "%f", get_timestamp_millis());
269
270         if(!host) host = "";
271         if(!resource) resource = "";
272
273         int len = strlen(resource) + 256;
274         char buf[len];
275         buf[0] = '\0';
276         snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
277
278         if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
279                 /* child nodes will leak the parents client... but we can't free
280                         it without disconnecting the parents client :( */
281                 osrfGlobalTransportClient = client;
282         }
283
284         osrfStringArrayFree(arr);
285         free(actlog);
286         free(facility);
287         free(log_level);
288         free(log_file);
289         free(username);
290         free(password);
291         free(port);     
292         free(unixpath);
293
294         if(osrfGlobalTransportClient)
295                 return 1;
296
297         return 0;
298 }
299
300 int osrf_system_disconnect_client( void ) {
301         client_disconnect( osrfGlobalTransportClient );
302         client_free( osrfGlobalTransportClient );
303         osrfGlobalTransportClient = NULL;
304         return 0;
305 }
306
307 int osrf_system_shutdown( void ) {
308         osrfConfigCleanup();
309         osrf_system_disconnect_client();
310         osrf_settings_free_host_config(NULL);
311         osrfAppSessionCleanup();
312         osrfLogCleanup();
313         return 1;
314 }
315
316
317
318
319 static void osrfSystemSignalHandler( int sig ) {
320
321         pid_t pid;
322         int status;
323
324         while( (pid = waitpid(-1, &status, WNOHANG)) > 0) {
325                 osrfLogWarning( OSRF_LOG_MARK, "We lost child %d", pid);
326         }
327
328         /** relaunch the server **/
329 }
330
331