Patch from Scott McKellar implementing cleaner daemonization; moved daemonizing code...
[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         /** daemonize me **/
84         /* background and let our children do their thing */
85         /* NOTE: This has been moved from below the 'if (apps)' block below ... move it back if things go crazy */
86         daemonize();
87
88         jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
89         osrfStringArray* arr = osrfNewStringArray(8);
90         
91         _osrfSystemInitCache();
92
93         if(apps) {
94                 int i = 0;
95
96                 if(apps->type == JSON_STRING) {
97                         osrfStringArrayAdd(arr, jsonObjectGetString(apps));
98
99                 } else {
100                         jsonObject* app;
101                         while( (app = jsonObjectGetIndex(apps, i++)) ) 
102                                 osrfStringArrayAdd(arr, jsonObjectGetString(app));
103                 }
104
105                 char* appname = NULL;
106                 i = 0;
107                 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
108
109                         char* lang = osrf_settings_host_value("/apps/%s/language", appname);
110
111                         if(lang && !strcasecmp(lang,"c"))  {
112
113                                 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
114                 
115                                 if(! (appname && libfile) ) {
116                                         osrfLogWarning( OSRF_LOG_MARK, "Missing appname / libfile in settings config");
117                                         continue;
118                                 }
119
120                                 osrfLogInfo( OSRF_LOG_MARK, "Launching application %s with implementation %s", appname, libfile);
121                 
122                                 pid_t pid;
123                 
124                                 if( (pid = fork()) ) { 
125                                         // storage pid in local table for re-launching dead children...
126                                         osrfLogInfo( OSRF_LOG_MARK, "Launched application child %ld", (long) pid);
127         
128                                 } else {
129                 
130                                         osrfLogError( OSRF_LOG_MARK, " * Running application %s\n", appname);
131                                         if( osrfAppRegisterApplication( appname, libfile ) == 0 ) 
132                                                 osrf_prefork_run(appname);
133         
134                                         osrfLogDebug( OSRF_LOG_MARK, "Server exiting for app %s and library %s\n", appname, libfile );
135                                         exit(0);
136                                 }
137                         } // language == c
138                 } 
139         } // should we do something if there are no apps? does the wait(NULL) below do that for us?
140
141         while(1) {
142                 errno = 0;
143                 pid_t pid = wait(NULL);
144                 if(-1 == pid) {
145                         if(errno == ECHILD)
146                                 osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
147                         else
148                                 osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
149                         break;
150                 } else {
151                         osrfLogError(OSRF_LOG_MARK, "We lost a top-level service process with PID %ld", pid);
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