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