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