1 | // ============================================================= // |
---|
2 | // // |
---|
3 | // File : servercntrl.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // ============================================================= // |
---|
10 | |
---|
11 | #include <servercntrl.h> |
---|
12 | |
---|
13 | #include <client_privat.h> |
---|
14 | #include <client.h> |
---|
15 | |
---|
16 | #include <arbdb.h> |
---|
17 | #include <arb_file.h> |
---|
18 | #include <arb_sleep.h> |
---|
19 | #include <ut_valgrinded.h> |
---|
20 | |
---|
21 | /* The following lines go to servercntrl.h |
---|
22 | * edit here, not there!! |
---|
23 | * call 'make proto' to update |
---|
24 | */ |
---|
25 | |
---|
26 | // AISC_MKPT_PROMOTE:#ifndef ARBDB_BASE_H |
---|
27 | // AISC_MKPT_PROMOTE:#include <arbdb_base.h> |
---|
28 | // AISC_MKPT_PROMOTE:#endif |
---|
29 | // AISC_MKPT_PROMOTE: |
---|
30 | // AISC_MKPT_PROMOTE:struct arb_params { |
---|
31 | // AISC_MKPT_PROMOTE: char *species_name; |
---|
32 | // AISC_MKPT_PROMOTE: char *extended_name; |
---|
33 | // AISC_MKPT_PROMOTE: char *alignment; |
---|
34 | // AISC_MKPT_PROMOTE: char *default_file; |
---|
35 | // AISC_MKPT_PROMOTE: char *field; |
---|
36 | // AISC_MKPT_PROMOTE: const char *field_default; |
---|
37 | // AISC_MKPT_PROMOTE: |
---|
38 | // AISC_MKPT_PROMOTE: int read_only; |
---|
39 | // AISC_MKPT_PROMOTE: |
---|
40 | // AISC_MKPT_PROMOTE: char *job_server; |
---|
41 | // AISC_MKPT_PROMOTE: char *db_server; |
---|
42 | // AISC_MKPT_PROMOTE: char *mgr_server; |
---|
43 | // AISC_MKPT_PROMOTE: char *pt_server; |
---|
44 | // AISC_MKPT_PROMOTE: |
---|
45 | // AISC_MKPT_PROMOTE: char *tcp; |
---|
46 | // AISC_MKPT_PROMOTE:}; |
---|
47 | |
---|
48 | #define TRIES 1 |
---|
49 | |
---|
50 | static struct gl_struct { |
---|
51 | aisc_com *link; |
---|
52 | AISC_Object com; |
---|
53 | |
---|
54 | gl_struct() |
---|
55 | : link(0), com(0x10000*1) // faked type id (matches main object) |
---|
56 | { } |
---|
57 | } glservercntrl; |
---|
58 | |
---|
59 | |
---|
60 | char *prefixSSH(const char *host, const char *command, int async) { |
---|
61 | /* 'host' is a hostname or 'hostname:port' (where hostname may be an IP) |
---|
62 | 'command' is the command to be executed |
---|
63 | if 'async' is 1 -> append '&' |
---|
64 | |
---|
65 | returns a SSH system call for foreign host or |
---|
66 | a direct system call for the local machine |
---|
67 | */ |
---|
68 | |
---|
69 | char *result = 0; |
---|
70 | char asyncChar = " &"[!!async]; |
---|
71 | |
---|
72 | if (host && host[0]) { |
---|
73 | const char *hostPort = strchr(host, ':'); |
---|
74 | char *hostOnly = GB_strpartdup(host, hostPort ? hostPort-1 : 0); |
---|
75 | |
---|
76 | if (!GB_host_is_local(hostOnly)) { |
---|
77 | result = GBS_global_string_copy("ssh %s -n '%s' %c", hostOnly, command, asyncChar); |
---|
78 | } |
---|
79 | free(hostOnly); |
---|
80 | } |
---|
81 | |
---|
82 | if (!result) { |
---|
83 | result = GBS_global_string_copy("(%s) %c", command, asyncChar); |
---|
84 | } |
---|
85 | |
---|
86 | return result; |
---|
87 | } |
---|
88 | |
---|
89 | GB_ERROR arb_start_server(const char *arb_tcp_env, int do_sleep) |
---|
90 | { |
---|
91 | const char *tcp_id; |
---|
92 | GB_ERROR error = 0; |
---|
93 | |
---|
94 | if (!(tcp_id = GBS_read_arb_tcp(arb_tcp_env))) { |
---|
95 | error = GB_await_error(); |
---|
96 | } |
---|
97 | else { |
---|
98 | const char *server = strchr(tcp_id, 0) + 1; |
---|
99 | char *serverparams = 0; |
---|
100 | |
---|
101 | /* concatenate all params behind server |
---|
102 | Note : changed behavior on 2007/Mar/09 -- ralf |
---|
103 | serverparams now is one space if nothing defined in arb_tcp.dat |
---|
104 | (previously was same as 'server' - most likely a bug) |
---|
105 | */ |
---|
106 | { |
---|
107 | const char *param = strchr(server, 0)+1; |
---|
108 | size_t plen = strlen(param); |
---|
109 | size_t alllen = 0; |
---|
110 | |
---|
111 | while (plen) { |
---|
112 | param += plen+1; |
---|
113 | alllen += plen+1; |
---|
114 | plen = strlen(param); |
---|
115 | } |
---|
116 | |
---|
117 | serverparams = (char*)malloc(alllen+1); |
---|
118 | { |
---|
119 | char *sp = serverparams; |
---|
120 | |
---|
121 | param = strchr(server, 0)+1; |
---|
122 | plen = strlen(param); |
---|
123 | if (!plen) sp++; |
---|
124 | else do { |
---|
125 | memcpy(sp, param, plen); |
---|
126 | sp[plen] = ' '; |
---|
127 | sp += plen+1; |
---|
128 | param += plen+1; |
---|
129 | plen = strlen(param); |
---|
130 | } while (plen); |
---|
131 | sp[-1] = 0; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | { |
---|
136 | char *command = 0; |
---|
137 | |
---|
138 | if (*tcp_id == ':') { // local mode |
---|
139 | command = GBS_global_string_copy("%s %s -T%s &", server, serverparams, tcp_id); |
---|
140 | make_valgrinded_call(command); |
---|
141 | } |
---|
142 | else { |
---|
143 | const char *port = strchr(tcp_id, ':'); |
---|
144 | |
---|
145 | if (!port) { |
---|
146 | error = GB_export_errorf("Error: Missing ':' in line '%s' file $(ARBHOME)/lib/arb_tcp.dat", arb_tcp_env); |
---|
147 | } |
---|
148 | else { |
---|
149 | char *remoteCommand = GBS_global_string_copy("$ARBHOME/bin/%s %s -T%s", server, serverparams, port); |
---|
150 | make_valgrinded_call(remoteCommand); |
---|
151 | command = prefixSSH(tcp_id, remoteCommand, 1); |
---|
152 | free(remoteCommand); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | if (!error) { |
---|
157 | error = GBK_system(command); |
---|
158 | if (do_sleep) GB_sleep(5, SEC); |
---|
159 | } |
---|
160 | free(command); |
---|
161 | } |
---|
162 | free(serverparams); |
---|
163 | } |
---|
164 | return error; |
---|
165 | } |
---|
166 | |
---|
167 | static GB_ERROR arb_wait_for_server(const char *arb_tcp_env, const char *tcp_id, int magic_number, struct gl_struct *serverctrl, int wait) { |
---|
168 | GB_ERROR error = NULL; |
---|
169 | serverctrl->link = aisc_open(tcp_id, serverctrl->com, magic_number, &error); |
---|
170 | |
---|
171 | if (!error && !serverctrl->link) { // no server running -> start one |
---|
172 | error = arb_start_server(arb_tcp_env, 0); |
---|
173 | while (!error && !serverctrl->link && wait) { |
---|
174 | GB_sleep(1, SEC); |
---|
175 | wait--; |
---|
176 | if ((wait%10) == 0 && wait>0) { |
---|
177 | printf("Waiting for server '%s' to come up (%i seconds left)\n", arb_tcp_env, wait); |
---|
178 | } |
---|
179 | serverctrl->link = aisc_open(tcp_id, serverctrl->com, magic_number, &error); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | return error; |
---|
184 | } |
---|
185 | |
---|
186 | GB_ERROR arb_look_and_start_server(long magic_number, const char *arb_tcp_env) { |
---|
187 | arb_assert(!GB_have_error()); |
---|
188 | |
---|
189 | GB_ERROR error = 0; |
---|
190 | const char *tcp_id = GBS_read_arb_tcp(arb_tcp_env); |
---|
191 | const char *arb_tcp_dat = "$(ARBHOME)/lib/arb_tcp.dat"; |
---|
192 | |
---|
193 | if (!tcp_id) { |
---|
194 | error = GBS_global_string("Entry '%s' not found in %s (%s)", arb_tcp_env, arb_tcp_dat, GB_await_error()); |
---|
195 | } |
---|
196 | else { |
---|
197 | const char *file = GBS_scan_arb_tcp_param(tcp_id, "-d"); // find parameter behind '-d' |
---|
198 | |
---|
199 | if (!file) { |
---|
200 | error = GBS_global_string("Parameter -d missing for entry '%s' in %s", arb_tcp_env, arb_tcp_dat); |
---|
201 | } |
---|
202 | else { |
---|
203 | if (strcmp(file, "!ASSUME_RUNNING") == 0) { |
---|
204 | // assume pt-server is running on a host, w/o access to common network drive |
---|
205 | // i.e. we cannot check for the existence of the database file |
---|
206 | } |
---|
207 | else if (GB_size_of_file(file) <= 0) { |
---|
208 | if (strncmp(arb_tcp_env, "ARB_NAME_SERVER", 15) == 0) { |
---|
209 | char *dir = strdup(file); |
---|
210 | char *lastSlash = strrchr(dir, '/'); |
---|
211 | |
---|
212 | if (lastSlash) { |
---|
213 | lastSlash[0] = 0; // cut off file |
---|
214 | { |
---|
215 | const char *copy_cmd = GBS_global_string("cp %s/names.dat.template %s", dir, file); |
---|
216 | error = GBK_system(copy_cmd); |
---|
217 | } |
---|
218 | if (!error && GB_size_of_file(file) <= 0) { |
---|
219 | error = GBS_global_string("Cannot copy nameserver template (%s/names.dat.template missing?)", dir); |
---|
220 | } |
---|
221 | } |
---|
222 | else { |
---|
223 | error = GBS_global_string("Can't determine directory from '%s'", dir); |
---|
224 | } |
---|
225 | free(dir); |
---|
226 | } |
---|
227 | else if (strncmp(arb_tcp_env, "ARB_PT_SERVER", 13) == 0) { |
---|
228 | const char *nameOnly = strrchr(file, '/'); |
---|
229 | if (!nameOnly) nameOnly = file; |
---|
230 | |
---|
231 | error = GBS_global_string("PT_server '%s' has not been created yet.\n" |
---|
232 | " To create it follow these steps:\n" |
---|
233 | " 1. Start ARB on the whole database you want to use for probe match/design\n" |
---|
234 | " 2. Go to ARB_NTREE/Probes/PT_SERVER Admin\n" |
---|
235 | " 3. Select '%s' and press BUILD SERVER\n" |
---|
236 | " 4. Wait (up to hours, depending on your DB size)\n" |
---|
237 | " 5. Meanwhile read the help file: PT_SERVER: What Why and How", |
---|
238 | file, nameOnly); |
---|
239 | } |
---|
240 | else { |
---|
241 | error = GBS_global_string("The file '%s' is missing. \nUnable to start %s", file, arb_tcp_env); |
---|
242 | } |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | if (!error) { |
---|
247 | error = arb_wait_for_server(arb_tcp_env, tcp_id, magic_number, &glservercntrl, 20); |
---|
248 | |
---|
249 | if (!error) { |
---|
250 | if (!glservercntrl.link) { // couldn't start server |
---|
251 | error = // | |
---|
252 | "ARB has problems to start a server! Possible reasons may be one\n" |
---|
253 | "or several of the following list:\n" |
---|
254 | "- the tcp_id (socket number) is already used by another program\n" |
---|
255 | " (doesnt apply to user-specific PTSERVERs; check $ARBHOME/lib/arb_tcp.dat versus /etc/services)\n" |
---|
256 | "- the server exited with error or has crashed.\n" |
---|
257 | " In case of PTSERVER, the failure might be caused by:\n" |
---|
258 | " - missing database in $ARBHOME/lib/pts/* (solution: update ptserver database)\n" |
---|
259 | " - wrong permissions of $ARBHOME/lib/pts/* (no read access)\n" |
---|
260 | " If you recently installed a new arb version, arb will continue\n" |
---|
261 | " to use your previous 'arb_tcp.dat', which might be out-of-date.\n" |
---|
262 | " Backup and remove it, then restart ARB. If it works now,\n" |
---|
263 | " compare your old 'arb_tcp.dat' with the new one for changes.\n" |
---|
264 | "- When using remote servers: login or network problems\n" |
---|
265 | ; |
---|
266 | } |
---|
267 | else { |
---|
268 | aisc_close(glservercntrl.link, glservercntrl.com); |
---|
269 | glservercntrl.link = 0; |
---|
270 | } |
---|
271 | } |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | arb_assert(!GB_have_error()); |
---|
276 | return error; |
---|
277 | } |
---|
278 | |
---|
279 | GB_ERROR arb_look_and_kill_server(int magic_number, const char *arb_tcp_env) { |
---|
280 | const char *tcp_id; |
---|
281 | GB_ERROR error = 0; |
---|
282 | |
---|
283 | if (!(tcp_id = GBS_read_arb_tcp(arb_tcp_env))) { |
---|
284 | error = GB_await_error(); |
---|
285 | } |
---|
286 | else { |
---|
287 | const char *server = strchr(tcp_id, 0)+1; |
---|
288 | |
---|
289 | glservercntrl.link = aisc_open(tcp_id, glservercntrl.com, magic_number, &error); |
---|
290 | if (glservercntrl.link) { |
---|
291 | aisc_close(glservercntrl.link, glservercntrl.com); |
---|
292 | glservercntrl.link = 0; |
---|
293 | |
---|
294 | const char *command = GBS_global_string("%s -kill -T%s &", server, tcp_id); |
---|
295 | if (system(command) != 0) { |
---|
296 | error = GBS_global_string("Failed to execute '%s'", command); |
---|
297 | } |
---|
298 | } |
---|
299 | else { |
---|
300 | error = "Server is not running"; |
---|
301 | } |
---|
302 | } |
---|
303 | return error; |
---|
304 | } |
---|
305 | |
---|
306 | void arb_print_server_params() { |
---|
307 | printf("General server parameters (some maybe unused by this server):\n" |
---|
308 | " -s<name> sets species name to '<name>'\n" |
---|
309 | " -e<name> sets extended name to '<name>'\n" |
---|
310 | " -a<ali> sets alignment to '<ali>'\n" |
---|
311 | " -d<file> sets default file to '<file>'\n" |
---|
312 | " -f<field>=<def> sets DB field to '<field>' (using <def> as default)\n" |
---|
313 | " -r read-only mode\n" |
---|
314 | " -D<server> sets DB-server to '<server>' [default = ':']\n" |
---|
315 | " -J<server> sets job-server to '<server>' [default = 'ARB_JOB_SERVER']\n" |
---|
316 | " -M<server> sets MGR-server to '<server>' [default = 'ARB_MGR_SERVER']\n" |
---|
317 | " -P<server> sets PT-server to '<server>' [default = 'ARB_PT_SERVER']\n" |
---|
318 | " -T<[host]:port> sets TCP connection to '<[host]:port>'\n" |
---|
319 | ); |
---|
320 | } |
---|
321 | |
---|
322 | arb_params *arb_trace_argv(int *argc, const char **argv) |
---|
323 | { |
---|
324 | int s, d; |
---|
325 | |
---|
326 | arb_params *erg = (arb_params *)calloc(sizeof(arb_params), 1); |
---|
327 | erg->db_server = strdup(":"); |
---|
328 | erg->job_server = strdup("ARB_JOB_SERVER"); |
---|
329 | erg->mgr_server = strdup("ARB_MGR_SERVER"); |
---|
330 | erg->pt_server = strdup("ARB_PT_SERVER"); |
---|
331 | |
---|
332 | for (s=d=0; s<*argc; s++) { |
---|
333 | if (argv[s][0] == '-') { |
---|
334 | switch (argv[s][1]) { |
---|
335 | case 's': erg->species_name = strdup(argv[s]+2); break; |
---|
336 | case 'e': erg->extended_name = strdup(argv[s]+2); break; |
---|
337 | case 'a': erg->alignment = strdup(argv[s]+2); break; |
---|
338 | case 'd': erg->default_file = strdup(argv[s]+2); break; |
---|
339 | case 'f': { |
---|
340 | char *eq; |
---|
341 | erg->field = strdup(argv[s]+2); |
---|
342 | |
---|
343 | eq = strchr(erg->field, '='); |
---|
344 | if (eq) { |
---|
345 | erg->field_default = eq+1; |
---|
346 | eq[0] = 0; |
---|
347 | } |
---|
348 | else { |
---|
349 | erg->field_default = 0; // this is illegal - error handling done in caller |
---|
350 | } |
---|
351 | break; |
---|
352 | } |
---|
353 | case 'r': erg->read_only = 1; break; |
---|
354 | case 'J': freedup(erg->job_server, argv[s]+2); break; |
---|
355 | case 'D': freedup(erg->db_server, argv[s]+2); break; |
---|
356 | case 'M': freedup(erg->mgr_server, argv[s]+2); break; |
---|
357 | case 'P': freedup(erg->pt_server, argv[s]+2); break; |
---|
358 | case 'T': { |
---|
359 | const char *ipport = argv[s]+2; |
---|
360 | if (ipport[0] == ':' && |
---|
361 | ipport[1] >= '0' && ipport[1] <= '9') { // port only -> assume localhost |
---|
362 | erg->tcp = GBS_global_string_copy("localhost%s", ipport); |
---|
363 | } |
---|
364 | else { |
---|
365 | erg->tcp = strdup(ipport); |
---|
366 | } |
---|
367 | break; |
---|
368 | } |
---|
369 | default: |
---|
370 | argv[d++] = argv[s]; |
---|
371 | break; |
---|
372 | } |
---|
373 | } |
---|
374 | else { |
---|
375 | argv[d++] = argv[s]; |
---|
376 | } |
---|
377 | } |
---|
378 | *argc = d; |
---|
379 | return erg; |
---|
380 | } |
---|
381 | |
---|
382 | void free_arb_params(arb_params *params) { |
---|
383 | free(params->species_name); |
---|
384 | free(params->extended_name); |
---|
385 | free(params->alignment); |
---|
386 | free(params->default_file); |
---|
387 | free(params->field); |
---|
388 | free(params->job_server); |
---|
389 | free(params->db_server); |
---|
390 | free(params->mgr_server); |
---|
391 | free(params->pt_server); |
---|
392 | free(params->tcp); |
---|
393 | |
---|
394 | free(params); |
---|
395 | } |
---|
396 | |
---|
397 | // -------------------------------------------------------------------------------- |
---|
398 | |
---|
399 | #if defined(UNIT_TESTS) |
---|
400 | |
---|
401 | // If you need tests in AISC_COM/C/client.c, put them here instead. |
---|
402 | |
---|
403 | #include <test_unit.h> |
---|
404 | |
---|
405 | void TEST_servercntrl() { |
---|
406 | // TEST_EXPECT(0); |
---|
407 | } |
---|
408 | |
---|
409 | #endif // UNIT_TESTS |
---|
410 | |
---|