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