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