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