]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrf_system.c
Several combined cleanup patches from Scott McKellar:
[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", 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                 signal(SIGCHLD, osrfSystemSignalHandler);
134                 sleep(10000);
135         }
136         
137         return 0;
138 }
139
140 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
141
142         int failure = 0;
143
144         if(osrfSystemGetTransportClient()) {
145                 osrfLogInfo(OSRF_LOG_MARK, "Client is already bootstrapped");
146                 return 1; /* we already have a client connection */
147         }
148
149         if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
150                 osrfLogError( OSRF_LOG_MARK, "No Config File Specified\n" );
151                 return -1;
152         }
153
154         if( config_file ) {
155                 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
156                 if(cfg)
157                         osrfConfigSetDefaultConfig(cfg);
158                 else
159                         return 0;   /* Can't load configuration?  Bail out */
160         }
161
162
163         char* log_file          = osrfConfigGetValue( NULL, "/logfile");
164         char* log_level         = osrfConfigGetValue( NULL, "/loglevel" );
165         osrfStringArray* arr    = osrfNewStringArray(8);
166         osrfConfigGetValueList(NULL, arr, "/domains/domain");
167
168         char* username          = osrfConfigGetValue( NULL, "/username" );
169         char* password          = osrfConfigGetValue( NULL, "/passwd" );
170         char* port              = osrfConfigGetValue( NULL, "/port" );
171         char* unixpath          = osrfConfigGetValue( NULL, "/unixpath" );
172         char* facility          = osrfConfigGetValue( NULL, "/syslog" );
173         char* actlog            = osrfConfigGetValue( NULL, "/actlog" );
174
175         if(!log_file) {
176                 fprintf(stderr, "No log file specified in configuration file %s\n",
177                            config_file);
178                 free(log_level);
179                 free(username);
180                 free(password);
181                 free(port);
182                 free(unixpath);
183                 free(facility);
184                 free(actlog);
185                 return -1;
186         }
187
188         /* if we're a source-client, tell the logger */
189         char* isclient = osrfConfigGetValue(NULL, "/client");
190         if( isclient && !strcasecmp(isclient,"true") )
191                 osrfLogSetIsClient(1);
192         free(isclient);
193
194         int llevel = 0;
195         int iport = 0;
196         if(port) iport = atoi(port);
197         if(log_level) llevel = atoi(log_level);
198
199         if(!strcmp(log_file, "syslog")) {
200                 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
201                 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
202                 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
203
204         } else {
205                 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
206                 osrfLogSetFile( log_file );
207         }
208
209
210         /* Get a domain, if one is specified */
211         const char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
212         if(!domain) {
213                 fprintf(stderr, "No domain specified in configuration file %s\n", config_file);
214                 osrfLogError( OSRF_LOG_MARK, "No domain specified in configuration file %s\n", config_file);
215                 failure = 1;
216         }
217
218         if(!username) {
219                 fprintf(stderr, "No username specified in configuration file %s\n", config_file);
220                 osrfLogError( OSRF_LOG_MARK, "No username specified in configuration file %s\n", config_file);
221                 failure = 1;
222         }
223
224         if(!password) {
225                 fprintf(stderr, "No password specified in configuration file %s\n", config_file);
226                 osrfLogError( OSRF_LOG_MARK, "No password specified in configuration file %s\n", config_file);
227                 failure = 1;
228         }
229
230         if((iport <= 0) && !unixpath) {
231                 fprintf(stderr, "No unixpath or valid port in configuration file %s\n", config_file);
232                 osrfLogError( OSRF_LOG_MARK, "No unixpath or valid port in configuration file %s\n",
233                         config_file);
234                 failure = 1;
235         }
236
237         if (failure) {
238                 osrfStringArrayFree(arr);
239                 free(log_level);
240                 free(username);
241                 free(password);
242                 free(port);
243                 free(unixpath);
244                 free(facility);
245                 free(actlog);
246                 return 0;
247         }
248
249         osrfLogInfo( OSRF_LOG_MARK, "Bootstrapping system with domain %s, port %d, and unixpath %s",
250                 domain, iport, unixpath ? unixpath : "(none)" );
251         transport_client* client = client_init( domain, iport, unixpath, 0 );
252
253         const char* host;
254         host = getenv("HOSTNAME");
255
256         char tbuf[32];
257         tbuf[0] = '\0';
258         snprintf(tbuf, 32, "%f", get_timestamp_millis());
259
260         if(!host) host = "";
261         if(!resource) resource = "";
262
263         int len = strlen(resource) + 256;
264         char buf[len];
265         buf[0] = '\0';
266         snprintf(buf, len - 1, "%s_%s_%s_%ld", resource, host, tbuf, (long) getpid() );
267
268         if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
269                 /* child nodes will leak the parents client... but we can't free
270                         it without disconnecting the parents client :( */
271                 osrfGlobalTransportClient = client;
272         }
273
274         osrfStringArrayFree(arr);
275         free(actlog);
276         free(facility);
277         free(log_level);
278         free(log_file);
279         free(username);
280         free(password);
281         free(port);     
282         free(unixpath);
283
284         if(osrfGlobalTransportClient)
285                 return 1;
286
287         return 0;
288 }
289
290 int osrf_system_disconnect_client( void ) {
291         client_disconnect( osrfGlobalTransportClient );
292         client_free( osrfGlobalTransportClient );
293         osrfGlobalTransportClient = NULL;
294         return 0;
295 }
296
297 int osrf_system_shutdown( void ) {
298         osrfConfigCleanup();
299         osrf_system_disconnect_client();
300         osrf_settings_free_host_config(NULL);
301         osrfAppSessionCleanup();
302         osrfLogCleanup();
303         return 1;
304 }
305
306
307
308
309 static void osrfSystemSignalHandler( int sig ) {
310
311         pid_t pid;
312         int status;
313
314         while( (pid = waitpid(-1, &status, WNOHANG)) > 0) {
315                 osrfLogWarning( OSRF_LOG_MARK, "We lost child %d", pid);
316         }
317
318         /** relaunch the server **/
319 }
320
321