]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrf_system.c
de25a29ae5e8eb7d5f8aef97ffdc0e4e4c199a82
[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 struct child_node;
9 typedef struct child_node ChildNode;
10
11 struct child_node
12 {
13         ChildNode* pNext;
14         ChildNode* pPrev;
15         pid_t pid;
16         char* app;
17         char* libfile;
18 };
19
20 static ChildNode* child_list;
21
22 static transport_client* osrfGlobalTransportClient = NULL;
23
24 static void add_child( pid_t pid, const char* app, const char* libfile );
25 static void delete_child( ChildNode* node );
26 static void delete_all_children( void );
27 static ChildNode* seek_child( pid_t pid );
28
29 transport_client* osrfSystemGetTransportClient( void ) {
30         return osrfGlobalTransportClient;
31 }
32
33 void osrfSystemIgnoreTransportClient() {
34         osrfGlobalTransportClient = NULL;
35 }
36
37 transport_client* osrf_system_get_transport_client( void ) {
38         return osrfGlobalTransportClient;
39 }
40
41 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
42         return osrf_system_bootstrap_client_resc(config_file, contextnode, NULL);
43 }
44
45 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
46         return osrf_system_bootstrap_client_resc( config_file, contextnode, resource );
47 }
48
49
50 static int _osrfSystemInitCache( void ) {
51
52         jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
53         char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
54
55         if( cacheServers && maxCache) {
56
57                 if( cacheServers->type == JSON_ARRAY ) {
58                         int i;
59                         char* servers[cacheServers->size];
60                         for( i = 0; i != cacheServers->size; i++ ) {
61                                 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
62                                 osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[i]);
63                         }
64                         osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
65
66                 } else {
67                         char* servers[] = { jsonObjectGetString(cacheServers) };                
68                         osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]);
69                         osrfCacheInit( servers, 1, atoi(maxCache) );
70                 }
71
72         } else {
73                 osrfLogError( OSRF_LOG_MARK,  "Missing config value for /cache/global/servers/server _or_ "
74                         "/cache/global/max_cache_time");
75         }
76
77         return 0;
78 }
79
80
81 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
82         if( !(hostname && configfile && contextNode) ) return -1;
83
84         /* first we grab the settings */
85         if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
86                 osrfLogError( OSRF_LOG_MARK,
87                         "Unable to bootstrap for host %s from configuration file %s",
88                         hostname, configfile );
89                 return -1;
90         }
91
92         int retcode = osrf_settings_retrieve(hostname);
93         osrf_system_disconnect_client();
94
95         if( retcode ) {
96                 osrfLogError( OSRF_LOG_MARK,
97                         "Unable to retrieve settings for host %s from configuration file %s",
98                         hostname, configfile );
99                 return -1;
100         }
101
102         /** daemonize me **/
103         /* background and let our children do their thing */
104         /* NOTE: This has been moved from below the 'if (apps)' block below ... move it back if things go crazy */
105         daemonize();
106
107         jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
108         osrfStringArray* arr = osrfNewStringArray(8);
109         
110         _osrfSystemInitCache();
111
112         if(apps) {
113                 int i = 0;
114
115                 if(apps->type == JSON_STRING) {
116                         osrfStringArrayAdd(arr, jsonObjectGetString(apps));
117
118                 } else {
119                         jsonObject* app;
120                         while( (app = jsonObjectGetIndex(apps, i++)) ) 
121                                 osrfStringArrayAdd(arr, jsonObjectGetString(app));
122                 }
123
124                 char* appname = NULL;
125                 i = 0;
126                 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
127
128                         char* lang = osrf_settings_host_value("/apps/%s/language", appname);
129
130                         if(lang && !strcasecmp(lang,"c"))  {
131
132                                 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
133                 
134                                 if(! (appname && libfile) ) {
135                                         osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
136                                         continue;
137                                 }
138
139                                 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
140                 
141                                 pid_t pid;
142                 
143                                 if( (pid = fork()) ) { 
144                                         // store pid in local list for re-launching dead children...
145                                         add_child( pid, appname, libfile );
146                                         osrfLogInfo( OSRF_LOG_MARK, "Running application child %s: process id %ld",
147                                                                  appname, (long) pid );
148         
149                                 } else {
150                 
151                                         osrfLogInfo( OSRF_LOG_MARK, " * Running application %s\n", appname);
152                                         if( osrfAppRegisterApplication( appname, libfile ) == 0 ) 
153                                                 osrf_prefork_run(appname);
154         
155                                         osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
156                                         exit(0);
157                                 }
158                         } // language == c
159                 } 
160         } // should we do something if there are no apps? does the wait(NULL) below do that for us?
161
162         while(1) {
163                 errno = 0;
164                 int status;
165                 pid_t pid = wait( &status );
166                 if(-1 == pid) {
167                         if(errno == ECHILD)
168                                 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
169                         else
170                                 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
171                         break;
172                 } else {
173                         report_child_status( pid, status );
174                 }
175         }
176
177         delete_all_children();
178         return 0;
179 }
180
181
182 static void report_child_status( pid_t pid, int status )
183 {
184         const char* app;
185         const char* libfile;
186         ChildNode* node = seek_child( pid );
187
188         if( node ) {
189                 app     = node->app     ? node->app     : "[unknown]";
190                 libfile = node->libfile ? node->libfile : "[none]";
191         } else
192                 app = libfile = NULL;
193         
194         if( WIFEXITED( status ) )
195         {
196                 int rc = WEXITSTATUS( status );  // return code of child process
197                 if( rc )
198                         osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) exited with return code %d",
199                                                   (long) pid, app, rc );
200                 else
201                         osrfLogInfo( OSRF_LOG_MARK, "Child process %ld (app %s) exited normally",
202                                                   (long) pid, app );
203         }
204         else if( WIFSIGNALED( status ) )
205         {
206                 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) killed by signal %d",
207                                           (long) pid, app, WTERMSIG( status) );
208         }
209         else if( WIFSTOPPED( status ) )
210         {
211                 osrfLogError( OSRF_LOG_MARK, "Child process %ld (app %s) stopped by signal %d",
212                                           (long) pid, app, (int) WSTOPSIG( status ) );
213         }
214
215         delete_child( node );
216 }
217
218 /*----------- Routines to manage list of children --*/
219
220 static void add_child( pid_t pid, const char* app, const char* libfile )
221 {
222         /* Construct new child node */
223         
224         ChildNode* node = safe_malloc( sizeof( ChildNode ) );
225
226         node->pid = pid;
227
228         if( app )
229                 node->app = strdup( app );
230         else
231                 node->app = NULL;
232
233         if( libfile )
234                 node->libfile = strdup( libfile );
235         else
236                 node->libfile = NULL;
237         
238         /* Add new child node to the head of the list */
239
240         node->pNext = child_list;
241         node->pPrev = NULL;
242
243         if( child_list )
244                 child_list->pPrev = node;
245
246         child_list = node;
247 }
248
249 static void delete_child( ChildNode* node ) {
250
251         /* Sanity check */
252
253         if( ! node )
254                 return;
255         
256         /* Detach the node from the list */
257
258         if( node->pPrev )
259                 node->pPrev->pNext = node->pNext;
260         else
261                 child_list = node->pNext;
262
263         if( node->pNext )
264                 node->pNext->pPrev = node->pPrev;
265
266         /* Deallocate the node and its payload */
267
268         free( node->app );
269         free( node->libfile );
270         free( node );
271 }
272
273 static void delete_all_children( void ) {
274
275         while( child_list )
276                 delete_child( child_list );
277 }
278
279 static ChildNode* seek_child( pid_t pid ) {
280
281         /* Return a pointer to the child node for the */
282         /* specified process ID, or NULL if not found */
283         
284         ChildNode* node = child_list;
285         while( node ) {
286                 if( node->pid == pid )
287                         break;
288                 else
289                         node = node->pNext;
290         }
291
292         return node;
293 }
294
295 /*----------- End of routines to manage list of children --*/
296
297
298 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
299
300         int failure = 0;
301
302         if(osrfSystemGetTransportClient()) {
303                 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
304                 return 1; /* we already have a client connection */
305         }
306
307         if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
308                 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
309                 return -1;
310         }
311
312         if( config_file ) {
313                 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
314                 if(cfg)
315                         osrfConfigSetDefaultConfig(cfg);
316                 else
317                         return 0;   /* Can't load configuration?  Bail out */
318         }
319
320
321         char* log_file          = osrfConfigGetValue( NULL, "/logfile");
322         char* log_level         = osrfConfigGetValue( NULL, "/loglevel" );
323         osrfStringArray* arr    = osrfNewStringArray(8);
324         osrfConfigGetValueList(NULL, arr, "/domains/domain");
325
326         char* username          = osrfConfigGetValue( NULL, "/username" );
327         char* password          = osrfConfigGetValue( NULL, "/passwd" );
328         char* port              = osrfConfigGetValue( NULL, "/port" );
329         char* unixpath          = osrfConfigGetValue( NULL, "/unixpath" );
330         char* facility          = osrfConfigGetValue( NULL, "/syslog" );
331         char* actlog            = osrfConfigGetValue( NULL, "/actlog" );
332
333         if(!log_file) {
334                 fprintf(stderr, "No log file specified in configuration file %s\n",
335                            config_file);
336                 free(log_level);
337                 free(username);
338                 free(password);
339                 free(port);
340                 free(unixpath);
341                 free(facility);
342                 free(actlog);
343                 return -1;
344         }
345
346         /* if we're a source-client, tell the logger */
347         char* isclient = osrfConfigGetValue(NULL, "/client");
348         if( isclient && !strcasecmp(isclient,"true") )
349                 osrfLogSetIsClient(1);
350         free(isclient);
351
352         int llevel = 0;
353         int iport = 0;
354         if(port) iport = atoi(port);
355         if(log_level) llevel = atoi(log_level);
356
357         if(!strcmp(log_file, "syslog")) {
358                 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
359                 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
360                 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
361
362         } else {
363                 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
364                 osrfLogSetFile( log_file );
365         }
366
367
368         /* Get a domain, if one is specified */
369         const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
370         if(!domain) {
371                 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
372                 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
373                 failure = 1;
374         }
375
376         if(!username) {
377                 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
378                 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
379                 failure = 1;
380         }
381
382         if(!password) {
383                 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
384                 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
385                 failure = 1;
386         }
387
388         if((iport <= 0) && !unixpath) {
389                 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
390                 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
391                         config_file);
392                 failure = 1;
393         }
394
395         if (failure) {
396                 osrfStringArrayFree(arr);
397                 free(log_level);
398                 free(username);
399                 free(password);
400                 free(port);
401                 free(unixpath);
402                 free(facility);
403                 free(actlog);
404                 return 0;
405         }
406
407         osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
408                 domain, iport, unixpath ? unixpath : "(none)" );
409         transport_client* client = client_init( domain, iport, unixpath, 0 );
410
411         const char* host;
412         host = getenv("HOSTNAME");
413
414         char tbuf[32];
415         tbuf[0] = '\0';
416         snprintf(tbuf, 32, "%f", get_timestamp_millis());
417
418         if(!host) host = "";
419         if(!resource) resource = "";
420
421         int len = strlen(resource) + 256;
422         char buf[len];
423         buf[0] = '\0';
424         snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
425
426         if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
427                 /* child nodes will leak the parents client... but we can't free
428                         it without disconnecting the parents client :( */
429                 osrfGlobalTransportClient = client;
430         }
431
432         osrfStringArrayFree(arr);
433         free(actlog);
434         free(facility);
435         free(log_level);
436         free(log_file);
437         free(username);
438         free(password);
439         free(port);     
440         free(unixpath);
441
442         if(osrfGlobalTransportClient)
443                 return 1;
444
445         return 0;
446 }
447
448 int osrf_system_disconnect_client( void ) {
449         client_disconnect( osrfGlobalTransportClient );
450         client_free( osrfGlobalTransportClient );
451         osrfGlobalTransportClient = NULL;
452         return 0;
453 }
454
455 int osrf_system_shutdown( void ) {
456         osrfConfigCleanup();
457     osrfCacheCleanup();
458         osrf_system_disconnect_client();
459         osrf_settings_free_host_config(NULL);
460         osrfAppSessionCleanup();
461         osrfLogCleanup();
462         return 1;
463 }
464
465
466
467