patch from Scott McKellar to examine and report the exit status of child processes...
[OpenSRF.git] / src / libopensrf / osrf_system.c
1 #include <opensrf/osrf_system.h>
2 #include <opensrf/osrf_application.h>
3 #include <opensrf/osrf_prefork.h>
4 #include <signal.h>
5
6 static int _osrfSystemInitCache( void );
7 static void report_child_status( pid_t pid, int status );
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,
69                         "Unable to bootstrap for host %s from configuration file %s",
70                         hostname, configfile );
71                 return -1;
72         }
73
74         int retcode = osrf_settings_retrieve(hostname);
75         osrf_system_disconnect_client();
76
77         if( retcode ) {
78                 osrfLogError( OSRF_LOG_MARK,
79                         "Unable to retrieve settings for host %s from configuration file %s",
80                         hostname, configfile );
81                 return -1;
82         }
83
84         /** daemonize me **/
85         /* background and let our children do their thing */
86         /* NOTE: This has been moved from below the 'if (apps)' block below ... move it back if things go crazy */
87         daemonize();
88
89         jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
90         osrfStringArray* arr = osrfNewStringArray(8);
91         
92         _osrfSystemInitCache();
93
94         if(apps) {
95                 int i = 0;
96
97                 if(apps->type == JSON_STRING) {
98                         osrfStringArrayAdd(arr, jsonObjectGetString(apps));
99
100                 } else {
101                         jsonObject* app;
102                         while( (app = jsonObjectGetIndex(apps, i++)) ) 
103                                 osrfStringArrayAdd(arr, jsonObjectGetString(app));
104                 }
105
106                 char* appname = NULL;
107                 i = 0;
108                 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
109
110                         char* lang = osrf_settings_host_value("/apps/%s/language", appname);
111
112                         if(lang && !strcasecmp(lang,"c"))  {
113
114                                 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
115                 
116                                 if(! (appname && libfile) ) {
117                                         osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
118                                         continue;
119                                 }
120
121                                 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
122                 
123                                 pid_t pid;
124                 
125                                 if( (pid = fork()) ) { 
126                                         // storage pid in local table for re-launching dead children...
127                                         osrfLogInfo( OSRF_LOG_MARK, "Launched application child %ld", (long) pid);
128         
129                                 } else {
130                 
131                                         osrfLogError( OSRF_LOG_MARK, " * Running application %s\n", appname);
132                                         if( osrfAppRegisterApplication( appname, libfile ) == 0 ) 
133                                                 osrf_prefork_run(appname);
134         
135                                         osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
136                                         exit(0);
137                                 }
138                         } // language == c
139                 } 
140         } // should we do something if there are no apps? does the wait(NULL) below do that for us?
141
142         while(1) {
143                 errno = 0;
144                 int status;
145                 pid_t pid = wait( &status );
146                 if(-1 == pid) {
147                         if(errno == ECHILD)
148                                 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
149                         else
150                                 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
151                         break;
152                 } else {
153                         report_child_status( pid, status );
154                 }
155         }
156
157         return 0;
158 }
159
160
161 static void report_child_status( pid_t pid, int status ) {
162         
163         if( WIFEXITED( status ) )
164         {
165                 int rc = WEXITSTATUS( status );  // return code of child process
166                 if( rc )
167                         osrfLogError( OSRF_LOG_MARK, "Child process %ld exited with return code %d",
168                                                   (long) pid, rc );
169                 else
170                         osrfLogError( OSRF_LOG_MARK, "Child process %ld exited normally", (long) pid );
171         }
172         else if( WIFSIGNALED( status ) )
173         {
174                 osrfLogError( OSRF_LOG_MARK, "Child process %ld killed by signal %d",
175                                           (long) pid, WTERMSIG( status) );
176         }
177         else if( WIFSTOPPED( status ) )
178         {
179                 osrfLogError( OSRF_LOG_MARK, "Child process %ld stopped by signal %d",
180                                           (long) pid, (int) WSTOPSIG( status ) );
181         }
182 }
183
184
185 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
186
187         int failure = 0;
188
189         if(osrfSystemGetTransportClient()) {
190                 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
191                 return 1; /* we already have a client connection */
192         }
193
194         if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
195                 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
196                 return -1;
197         }
198
199         if( config_file ) {
200                 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
201                 if(cfg)
202                         osrfConfigSetDefaultConfig(cfg);
203                 else
204                         return 0;   /* Can't load configuration?  Bail out */
205         }
206
207
208         char* log_file          = osrfConfigGetValue( NULL, "/logfile");
209         char* log_level         = osrfConfigGetValue( NULL, "/loglevel" );
210         osrfStringArray* arr    = osrfNewStringArray(8);
211         osrfConfigGetValueList(NULL, arr, "/domains/domain");
212
213         char* username          = osrfConfigGetValue( NULL, "/username" );
214         char* password          = osrfConfigGetValue( NULL, "/passwd" );
215         char* port              = osrfConfigGetValue( NULL, "/port" );
216         char* unixpath          = osrfConfigGetValue( NULL, "/unixpath" );
217         char* facility          = osrfConfigGetValue( NULL, "/syslog" );
218         char* actlog            = osrfConfigGetValue( NULL, "/actlog" );
219
220         if(!log_file) {
221                 fprintf(stderr, "No log file specified in configuration file %s\n",
222                            config_file);
223                 free(log_level);
224                 free(username);
225                 free(password);
226                 free(port);
227                 free(unixpath);
228                 free(facility);
229                 free(actlog);
230                 return -1;
231         }
232
233         /* if we're a source-client, tell the logger */
234         char* isclient = osrfConfigGetValue(NULL, "/client");
235         if( isclient && !strcasecmp(isclient,"true") )
236                 osrfLogSetIsClient(1);
237         free(isclient);
238
239         int llevel = 0;
240         int iport = 0;
241         if(port) iport = atoi(port);
242         if(log_level) llevel = atoi(log_level);
243
244         if(!strcmp(log_file, "syslog")) {
245                 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
246                 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
247                 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
248
249         } else {
250                 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
251                 osrfLogSetFile( log_file );
252         }
253
254
255         /* Get a domain, if one is specified */
256         const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
257         if(!domain) {
258                 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
259                 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
260                 failure = 1;
261         }
262
263         if(!username) {
264                 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
265                 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
266                 failure = 1;
267         }
268
269         if(!password) {
270                 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
271                 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
272                 failure = 1;
273         }
274
275         if((iport <= 0) && !unixpath) {
276                 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
277                 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
278                         config_file);
279                 failure = 1;
280         }
281
282         if (failure) {
283                 osrfStringArrayFree(arr);
284                 free(log_level);
285                 free(username);
286                 free(password);
287                 free(port);
288                 free(unixpath);
289                 free(facility);
290                 free(actlog);
291                 return 0;
292         }
293
294         osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
295                 domain, iport, unixpath ? unixpath : "(none)" );
296         transport_client* client = client_init( domain, iport, unixpath, 0 );
297
298         const char* host;
299         host = getenv("HOSTNAME");
300
301         char tbuf[32];
302         tbuf[0] = '\0';
303         snprintf(tbuf, 32, "%f", get_timestamp_millis());
304
305         if(!host) host = "";
306         if(!resource) resource = "";
307
308         int len = strlen(resource) + 256;
309         char buf[len];
310         buf[0] = '\0';
311         snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
312
313         if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
314                 /* child nodes will leak the parents client... but we can't free
315                         it without disconnecting the parents client :( */
316                 osrfGlobalTransportClient = client;
317         }
318
319         osrfStringArrayFree(arr);
320         free(actlog);
321         free(facility);
322         free(log_level);
323         free(log_file);
324         free(username);
325         free(password);
326         free(port);     
327         free(unixpath);
328
329         if(osrfGlobalTransportClient)
330                 return 1;
331
332         return 0;
333 }
334
335 int osrf_system_disconnect_client( void ) {
336         client_disconnect( osrfGlobalTransportClient );
337         client_free( osrfGlobalTransportClient );
338         osrfGlobalTransportClient = NULL;
339         return 0;
340 }
341
342 int osrf_system_shutdown( void ) {
343         osrfConfigCleanup();
344         osrf_system_disconnect_client();
345         osrf_settings_free_host_config(NULL);
346         osrfAppSessionCleanup();
347         osrfLogCleanup();
348         return 1;
349 }
350
351
352
353