]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_system.c
ac698f74cc78ea3c402dbf8787bc728e11b22c43
[Evergreen.git] / OpenSRF / 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;
9
10 transport_client* osrfSystemGetTransportClient() {
11         return __osrfGlobalTransportClient;
12 }
13
14 transport_client* osrf_system_get_transport_client() {
15         return __osrfGlobalTransportClient;
16 }
17
18 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
19         return osrf_system_bootstrap_client_resc(config_file, contextnode, NULL);
20 }
21
22 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
23         return osrf_system_bootstrap_client_resc( config_file, contextnode, resource );
24 }
25
26
27 int _osrfSystemInitCache() {
28
29         jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
30         char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
31
32         if( cacheServers && maxCache) {
33
34                 if( cacheServers->type == JSON_ARRAY ) {
35                         int i;
36                         char* servers[cacheServers->size];
37                         for( i = 0; i != cacheServers->size; i++ ) {
38                                 servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) );
39                                 osrfLogInfo("Adding cache server %s", servers[i]);
40                         }
41                         osrfCacheInit( servers, cacheServers->size, atoi(maxCache) );
42
43                 } else {
44                         char* servers[] = { jsonObjectGetString(cacheServers) };                
45                         osrfLogInfo("Adding cache server %s", servers[0]);
46                         osrfCacheInit( servers, 1, atoi(maxCache) );
47                 }
48
49         } else {
50                 osrfLogError( "Missing config value for /cache/global/servers/server _or_ "
51                         "/cache/global/max_cache_time");
52         }
53
54         return 0;
55 }
56
57
58 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
59         if( !(hostname && configfile && contextNode) ) return -1;
60
61         /* first we grab the settings */
62         if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
63                 osrfLogError("Unable to bootstrap");
64                 return -1;
65         }
66
67         osrf_settings_retrieve(hostname);
68         osrf_system_disconnect_client();
69
70         jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
71         osrfStringArray* arr = osrfNewStringArray(8);
72         
73         _osrfSystemInitCache();
74
75         if(apps) {
76                 int i = 0;
77
78                 if(apps->type == JSON_STRING) {
79                         osrfStringArrayAdd(arr, jsonObjectGetString(apps));
80
81                 } else {
82                         jsonObject* app;
83                         while( (app = jsonObjectGetIndex(apps, i++)) ) 
84                                 osrfStringArrayAdd(arr, jsonObjectGetString(app));
85                 }
86
87                 char* appname = NULL;
88                 i = 0;
89                 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
90
91                         char* lang = osrf_settings_host_value("/apps/%s/language", appname);
92
93                         if(lang && !strcasecmp(lang,"c"))  {
94
95                                 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
96                 
97                                 if(! (appname && libfile) ) {
98                                         osrfLogWarning("Missing appname / libfile in settings config");
99                                         continue;
100                                 }
101
102                                 osrfLogInfo("Launching application %s with implementation %s", appname, libfile);
103                 
104                                 int pid;
105                 
106                                 if( (pid = fork()) ) { 
107                                         // storage pid in local table for re-launching dead children...
108                                         osrfLogInfo("Launched application child %d", pid);
109         
110                                 } else {
111                 
112                                         fprintf(stderr, " * Running application %s\n", appname);
113                                         if( osrfAppRegisterApplication( appname, libfile ) == 0 ) 
114                                                 osrf_prefork_run(appname);
115         
116                                         osrfLogDebug("Server exiting for app %s and library %s", appname, libfile );
117                                         exit(0);
118                                 }
119                         } // language == c
120                 } 
121         }
122
123         /** daemonize me **/
124
125         /* background and let our children do their thing */
126         daemonize();
127         while(1) {
128                 signal(SIGCHLD, __osrfSystemSignalHandler);
129                 sleep(10000);
130         }
131         
132         return 0;
133 }
134
135 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
136
137         if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) {
138                 osrfLogError("No Config File Specified\n" );
139                 return -1;
140         }
141
142         if( config_file ) {
143                 osrfConfigCleanup();
144                 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
145                 osrfConfigSetDefaultConfig(cfg);
146         }
147
148
149         char* log_file          = osrfConfigGetValue( NULL, "/logfile");
150         char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
151         osrfStringArray* arr = osrfNewStringArray(8);
152         osrfConfigGetValueList(NULL, arr, "/domains/domain");
153         char* username          = osrfConfigGetValue( NULL, "/username" );
154         char* password          = osrfConfigGetValue( NULL, "/passwd" );
155         char* port                      = osrfConfigGetValue( NULL, "/port" );
156         char* unixpath          = osrfConfigGetValue( NULL, "/unixpath" );
157         char* facility          = osrfConfigGetValue( NULL, "/syslog" );
158         char* actlog            = osrfConfigGetValue( NULL, "/actlog" );
159
160         char* domain = strdup(osrfStringArrayGetString( arr, 0 )); /* just the first for now */
161         osrfStringArrayFree(arr);
162
163
164         int llevel = 0;
165         int iport = 0;
166         if(port) iport = atoi(port);
167         if(log_level) llevel = atoi(log_level);
168
169         if(!log_file) { fprintf(stderr, "Log file needed\n"); return -1; }
170
171         if(!strcmp(log_file, "syslog")) {
172                 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel );
173                 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
174                 if(actlog) osrfLogSetSyslogActFacility(osrfLogFacilityToInt(actlog));
175
176         } else {
177                 osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel );
178                 osrfLogSetFile( log_file );
179         }
180
181         osrfLogInfo("Bootstrapping system with domain %s, port %d, and unixpath %s", domain, iport, unixpath );
182
183         transport_client* client = client_init( domain, iport, unixpath, 0 );
184
185         char* host;
186         host = getenv("HOSTNAME");
187
188         if(!host) host = "";
189         if(!resource) resource = "";
190
191         int len = strlen(resource) + 256;
192         char buf[len];
193         memset(buf,0,len);
194         snprintf(buf, len - 1, "%s_%s_%d", resource, host, getpid() );
195         
196         if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
197                 /* child nodes will leak the parents client... but we can't free
198                         it without disconnecting the parents client :( */
199                 __osrfGlobalTransportClient = client;
200         }
201
202         free(actlog);
203         free(facility);
204         free(log_level);
205         free(log_file);
206         free(username);
207         free(password);
208         free(port);     
209         free(unixpath);
210
211         if(__osrfGlobalTransportClient)
212                 return 1;
213
214         return 0;
215 }
216
217 int osrf_system_disconnect_client() {
218         client_disconnect( __osrfGlobalTransportClient );
219         client_free( __osrfGlobalTransportClient );
220         __osrfGlobalTransportClient = NULL;
221         return 0;
222 }
223
224 int osrf_system_shutdown() {
225         osrfConfigCleanup();
226         osrf_system_disconnect_client();
227         osrf_settings_free_host_config(NULL);
228         return 1;
229 }
230
231
232
233
234 void __osrfSystemSignalHandler( int sig ) {
235
236         pid_t pid;
237         int status;
238
239         while( (pid = waitpid(-1, &status, WNOHANG)) > 0) {
240                 osrfLogWarning("We lost child %d", pid);
241         }
242
243         /** relaunch the server **/
244 }
245
246