| 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_export_errorf("Entry '%s' in $(ARBHOME)/lib/arb_tcp.dat not found", arb_tcp_env); |
|---|
| 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 | GB_ERROR error = 0; |
|---|
| 188 | const char *tcp_id = GBS_read_arb_tcp(arb_tcp_env); |
|---|
| 189 | const char *arb_tcp_dat = "$(ARBHOME)/lib/arb_tcp.dat"; |
|---|
| 190 | |
|---|
| 191 | if (!tcp_id) { |
|---|
| 192 | error = GBS_global_string("Entry '%s' not found in %s", arb_tcp_env, arb_tcp_dat); |
|---|
| 193 | } |
|---|
| 194 | else { |
|---|
| 195 | const char *file = GBS_scan_arb_tcp_param(tcp_id, "-d"); // find parameter behind '-d' |
|---|
| 196 | |
|---|
| 197 | if (!file) { |
|---|
| 198 | error = GBS_global_string("Parameter -d missing for entry '%s' in %s", arb_tcp_env, arb_tcp_dat); |
|---|
| 199 | } |
|---|
| 200 | else { |
|---|
| 201 | if (strcmp(file, "!ASSUME_RUNNING") == 0) { |
|---|
| 202 | // assume pt-server is running on a host, w/o access to common network drive |
|---|
| 203 | // i.e. we cannot check for the existence of the database file |
|---|
| 204 | } |
|---|
| 205 | else if (GB_size_of_file(file) <= 0) { |
|---|
| 206 | if (strncmp(arb_tcp_env, "ARB_NAME_SERVER", 15) == 0) { |
|---|
| 207 | char *dir = strdup(file); |
|---|
| 208 | char *lastSlash = strrchr(dir, '/'); |
|---|
| 209 | |
|---|
| 210 | if (lastSlash) { |
|---|
| 211 | lastSlash[0] = 0; // cut off file |
|---|
| 212 | { |
|---|
| 213 | const char *copy_cmd = GBS_global_string("cp %s/names.dat.template %s", dir, file); |
|---|
| 214 | error = GBK_system(copy_cmd); |
|---|
| 215 | } |
|---|
| 216 | if (!error && GB_size_of_file(file) <= 0) { |
|---|
| 217 | error = GBS_global_string("Cannot copy nameserver template (%s/names.dat.template missing?)", dir); |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | else { |
|---|
| 221 | error = GBS_global_string("Can't determine directory from '%s'", dir); |
|---|
| 222 | } |
|---|
| 223 | free(dir); |
|---|
| 224 | } |
|---|
| 225 | else if (strncmp(arb_tcp_env, "ARB_PT_SERVER", 13) == 0) { |
|---|
| 226 | const char *nameOnly = strrchr(file, '/'); |
|---|
| 227 | if (!nameOnly) nameOnly = file; |
|---|
| 228 | |
|---|
| 229 | error = GBS_global_string("PT_server '%s' has not been created yet.\n" |
|---|
| 230 | " To create it follow these steps:\n" |
|---|
| 231 | " 1. Start ARB on the whole database you want to use for probe match/design\n" |
|---|
| 232 | " 2. Go to ARB_NTREE/Probes/PT_SERVER Admin\n" |
|---|
| 233 | " 3. Select '%s' and press BUILD SERVER\n" |
|---|
| 234 | " 4. Wait (up to hours, depending on your DB size)\n" |
|---|
| 235 | " 5. Meanwhile read the help file: PT_SERVER: What Why and How", |
|---|
| 236 | file, nameOnly); |
|---|
| 237 | } |
|---|
| 238 | else { |
|---|
| 239 | error = GBS_global_string("The file '%s' is missing. \nUnable to start %s", file, arb_tcp_env); |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | if (!error) { |
|---|
| 245 | error = arb_wait_for_server(arb_tcp_env, tcp_id, magic_number, &glservercntrl, 20); |
|---|
| 246 | |
|---|
| 247 | if (!error) { |
|---|
| 248 | if (!glservercntrl.link) { // couldn't start server |
|---|
| 249 | error = // | |
|---|
| 250 | "ARB has problems to start a server! Possible reasons may be one\n" |
|---|
| 251 | "or several of the following list:\n" |
|---|
| 252 | "- the tcp_id (socket number) is already used by another program\n" |
|---|
| 253 | " (doesnt apply to user-specific PTSERVERs; check $ARBHOME/lib/arb_tcp.dat versus /etc/services)\n" |
|---|
| 254 | "- the server exited with error or has crashed.\n" |
|---|
| 255 | " In case of PTSERVER, the failure might be caused by:\n" |
|---|
| 256 | " - missing database in $ARBHOME/lib/pts/* (solution: update ptserver database)\n" |
|---|
| 257 | " - wrong permissions of $ARBHOME/lib/pts/* (no read access)\n" |
|---|
| 258 | " If you recently installed a new arb version, arb will continue\n" |
|---|
| 259 | " to use your previous 'arb_tcp.dat', which might be out-of-date.\n" |
|---|
| 260 | " Backup and remove it, then restart ARB. If it works now,\n" |
|---|
| 261 | " compare your old 'arb_tcp.dat' with the new one for changes.\n" |
|---|
| 262 | "- When using remote servers: login or network problems\n" |
|---|
| 263 | ; |
|---|
| 264 | } |
|---|
| 265 | else { |
|---|
| 266 | aisc_close(glservercntrl.link, glservercntrl.com); |
|---|
| 267 | glservercntrl.link = 0; |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | return error; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | GB_ERROR arb_look_and_kill_server(int magic_number, const char *arb_tcp_env) { |
|---|
| 277 | const char *tcp_id; |
|---|
| 278 | GB_ERROR error = 0; |
|---|
| 279 | |
|---|
| 280 | if (!(tcp_id = GBS_read_arb_tcp(arb_tcp_env))) { |
|---|
| 281 | error = GB_export_errorf("Missing line '%s' in $(ARBHOME)/lib/arb_tcp.dat:", arb_tcp_env); |
|---|
| 282 | } |
|---|
| 283 | else { |
|---|
| 284 | const char *server = strchr(tcp_id, 0)+1; |
|---|
| 285 | |
|---|
| 286 | glservercntrl.link = aisc_open(tcp_id, glservercntrl.com, magic_number, &error); |
|---|
| 287 | if (glservercntrl.link) { |
|---|
| 288 | aisc_close(glservercntrl.link, glservercntrl.com); |
|---|
| 289 | glservercntrl.link = 0; |
|---|
| 290 | |
|---|
| 291 | const char *command = GBS_global_string("%s -kill -T%s &", server, tcp_id); |
|---|
| 292 | if (system(command) != 0) { |
|---|
| 293 | error = GBS_global_string("Failed to execute '%s'", command); |
|---|
| 294 | } |
|---|
| 295 | } |
|---|
| 296 | else { |
|---|
| 297 | error = "Server is not running"; |
|---|
| 298 | } |
|---|
| 299 | } |
|---|
| 300 | return error; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | void arb_print_server_params() { |
|---|
| 304 | printf("General server parameters (some maybe unused by this server):\n" |
|---|
| 305 | " -s<name> sets species name to '<name>'\n" |
|---|
| 306 | " -e<name> sets extended name to '<name>'\n" |
|---|
| 307 | " -a<ali> sets alignment to '<ali>'\n" |
|---|
| 308 | " -d<file> sets default file to '<file>'\n" |
|---|
| 309 | " -f<field>=<def> sets DB field to '<field>' (using <def> as default)\n" |
|---|
| 310 | " -r read-only mode\n" |
|---|
| 311 | " -D<server> sets DB-server to '<server>' [default = ':']\n" |
|---|
| 312 | " -J<server> sets job-server to '<server>' [default = 'ARB_JOB_SERVER']\n" |
|---|
| 313 | " -M<server> sets MGR-server to '<server>' [default = 'ARB_MGR_SERVER']\n" |
|---|
| 314 | " -P<server> sets PT-server to '<server>' [default = 'ARB_PT_SERVER']\n" |
|---|
| 315 | " -T<[host]:port> sets TCP connection to '<[host]:port>'\n" |
|---|
| 316 | ); |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | arb_params *arb_trace_argv(int *argc, const char **argv) |
|---|
| 320 | { |
|---|
| 321 | int s, d; |
|---|
| 322 | |
|---|
| 323 | arb_params *erg = (arb_params *)calloc(sizeof(arb_params), 1); |
|---|
| 324 | erg->db_server = strdup(":"); |
|---|
| 325 | erg->job_server = strdup("ARB_JOB_SERVER"); |
|---|
| 326 | erg->mgr_server = strdup("ARB_MGR_SERVER"); |
|---|
| 327 | erg->pt_server = strdup("ARB_PT_SERVER"); |
|---|
| 328 | |
|---|
| 329 | for (s=d=0; s<*argc; s++) { |
|---|
| 330 | if (argv[s][0] == '-') { |
|---|
| 331 | switch (argv[s][1]) { |
|---|
| 332 | case 's': erg->species_name = strdup(argv[s]+2); break; |
|---|
| 333 | case 'e': erg->extended_name = strdup(argv[s]+2); break; |
|---|
| 334 | case 'a': erg->alignment = strdup(argv[s]+2); break; |
|---|
| 335 | case 'd': erg->default_file = strdup(argv[s]+2); break; |
|---|
| 336 | case 'f': { |
|---|
| 337 | char *eq; |
|---|
| 338 | erg->field = strdup(argv[s]+2); |
|---|
| 339 | |
|---|
| 340 | eq = strchr(erg->field, '='); |
|---|
| 341 | if (eq) { |
|---|
| 342 | erg->field_default = eq+1; |
|---|
| 343 | eq[0] = 0; |
|---|
| 344 | } |
|---|
| 345 | else { |
|---|
| 346 | erg->field_default = 0; // this is illegal - error handling done in caller |
|---|
| 347 | } |
|---|
| 348 | break; |
|---|
| 349 | } |
|---|
| 350 | case 'r': erg->read_only = 1; break; |
|---|
| 351 | case 'J': freedup(erg->job_server, argv[s]+2); break; |
|---|
| 352 | case 'D': freedup(erg->db_server, argv[s]+2); break; |
|---|
| 353 | case 'M': freedup(erg->mgr_server, argv[s]+2); break; |
|---|
| 354 | case 'P': freedup(erg->pt_server, argv[s]+2); break; |
|---|
| 355 | case 'T': { |
|---|
| 356 | const char *ipport = argv[s]+2; |
|---|
| 357 | if (ipport[0] == ':' && |
|---|
| 358 | ipport[1] >= '0' && ipport[1] <= '9') { // port only -> assume localhost |
|---|
| 359 | erg->tcp = GBS_global_string_copy("localhost%s", ipport); |
|---|
| 360 | } |
|---|
| 361 | else { |
|---|
| 362 | erg->tcp = strdup(ipport); |
|---|
| 363 | } |
|---|
| 364 | break; |
|---|
| 365 | } |
|---|
| 366 | default: |
|---|
| 367 | argv[d++] = argv[s]; |
|---|
| 368 | break; |
|---|
| 369 | } |
|---|
| 370 | } |
|---|
| 371 | else { |
|---|
| 372 | argv[d++] = argv[s]; |
|---|
| 373 | } |
|---|
| 374 | } |
|---|
| 375 | *argc = d; |
|---|
| 376 | return erg; |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | void free_arb_params(arb_params *params) { |
|---|
| 380 | free(params->species_name); |
|---|
| 381 | free(params->extended_name); |
|---|
| 382 | free(params->alignment); |
|---|
| 383 | free(params->default_file); |
|---|
| 384 | free(params->field); |
|---|
| 385 | free(params->job_server); |
|---|
| 386 | free(params->db_server); |
|---|
| 387 | free(params->mgr_server); |
|---|
| 388 | free(params->pt_server); |
|---|
| 389 | free(params->tcp); |
|---|
| 390 | |
|---|
| 391 | free(params); |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | // -------------------------------------------------------------------------------- |
|---|
| 395 | |
|---|
| 396 | #if defined(UNIT_TESTS) |
|---|
| 397 | |
|---|
| 398 | // If you need tests in AISC_COM/C/client.c, put them here instead. |
|---|
| 399 | |
|---|
| 400 | #include <test_unit.h> |
|---|
| 401 | |
|---|
| 402 | void TEST_servercntrl() { |
|---|
| 403 | // TEST_EXPECT(0); |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | #endif // UNIT_TESTS |
|---|
| 407 | |
|---|