]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_system.c
a8609e76bd9ca64c32d20a0ddd501e3fc343a9d3
[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 osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
28         if( !(configfile && contextNode) ) return -1;
29
30         /* first we grab the settings */
31         if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
32                 return fatal_handler("Unable to bootstrap");
33         }
34
35         osrf_settings_retrieve(hostname);
36         osrf_system_disconnect_client();
37
38         jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
39         osrfStringArray* arr = osrfNewStringArray(8);
40
41         if(apps) {
42                 int i = 0;
43
44                 if(apps->type == JSON_STRING) {
45                         osrfStringArrayAdd(arr, jsonObjectGetString(apps));
46
47                 } else {
48                         jsonObject* app;
49                         while( (app = jsonObjectGetIndex(apps, i++)) ) 
50                                 osrfStringArrayAdd(arr, jsonObjectGetString(app));
51                 }
52
53                 char* appname = NULL;
54                 i = 0;
55                 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
56
57                         char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
58                         info_handler("Launching application %s with implementation %s", appname, libfile);
59         
60                         if(! (appname && libfile) ) {
61                                 warning_handler("Missing appname / libfile in settings config");
62                                 continue;
63                         }
64         
65                         int pid;
66         
67                         if( (pid = fork()) ) { 
68                                 // storage pid in local table for re-launching dead children...
69                                 info_handler("Launched application child %d", pid);
70
71                         } else {
72         
73                                 if( osrfAppRegisterApplication( appname, libfile ) == 0 ) 
74                                         osrf_prefork_run(appname);
75
76                                 debug_handler("Server exiting for app %s and library %s", appname, libfile );
77                                 exit(0);
78                         }
79                 }
80         }
81
82         /** daemonize me **/
83
84         /* let our children do their thing */
85         while(1) {
86                 signal(SIGCHLD, __osrfSystemSignalHandler);
87                 sleep(10000);
88         }
89         
90         return 0;
91 }
92
93 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
94
95         if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() )
96                 return fatal_handler("No Config File Specified\n" );
97
98         if( config_file ) {
99                 osrfConfigCleanup();
100                 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
101                 osrfConfigSetDefaultConfig(cfg);
102         }
103
104
105         char* log_file          = osrfConfigGetValue( NULL, "/logfile");
106         char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
107         osrfStringArray* arr = osrfNewStringArray(8);
108         osrfConfigGetValueList(NULL, arr, "/domains/domain");
109         char* username          = osrfConfigGetValue( NULL, "/username" );
110         char* password          = osrfConfigGetValue( NULL, "/passwd" );
111         char* port                      = osrfConfigGetValue( NULL, "/port" );
112         char* unixpath          = osrfConfigGetValue( NULL, "/unixpath" );
113
114         char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
115         osrfStringArrayFree(arr);
116
117
118         int llevel = 0;
119         int iport = 0;
120         if(port) iport = atoi(port);
121         if(log_level) llevel = atoi(log_level);
122
123         log_init( llevel, log_file );
124
125         info_handler("Bootstrapping system with domain %s, port %d, and unixpath %s", domain, iport, unixpath );
126
127         transport_client* client = client_init( domain, iport, unixpath, 0 );
128
129         char* host = getenv("HOSTNAME");
130
131         if(!resource) resource = "";
132         int len = strlen(resource) + 256;
133         char buf[len];
134         memset(buf,0,len);
135         snprintf(buf, len - 1, "opensrf_%s_%s_%d", resource, host, getpid() );
136         
137         if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
138                 /* child nodes will leak the parents client... but we can't free
139                         it without disconnecting the parents client :( */
140                 __osrfGlobalTransportClient = client;
141         }
142
143         free(log_level);
144         free(log_file);
145         free(username);
146         free(password);
147         free(port);     
148         free(unixpath);
149
150         if(__osrfGlobalTransportClient)
151                 return 1;
152
153         return 0;
154 }
155
156 int osrf_system_disconnect_client() {
157         client_disconnect( __osrfGlobalTransportClient );
158         client_free( __osrfGlobalTransportClient );
159         __osrfGlobalTransportClient = NULL;
160         return 0;
161 }
162
163 int osrf_system_shutdown() {
164         osrfConfigCleanup();
165         osrf_system_disconnect_client();
166         osrf_settings_free_host_config(NULL);
167         log_free();
168         return 1;
169 }
170
171
172
173
174 void __osrfSystemSignalHandler( int sig ) {
175
176         pid_t pid;
177         int status;
178
179         while( (pid = waitpid(-1, &status, WNOHANG)) > 0) {
180                 warning_handler("We lost child %d", pid);
181         }
182
183         /** relaunch the server **/
184 }