source: tags/cvs_2_svn/SERVERCNTRL/servercntrl.c

Last change on this file was 5390, checked in by westram, 16 years ago
  • TAB-Ex
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <string.h>
5#include <ctype.h>
6/* #include <malloc.h> */
7#include <client_privat.h>
8#include <client.h>
9#include <arbdb.h>
10#include <servercntrl.h>
11
12/*
13 * The following lines go to servercntrl.h
14 * edit here, not there!!
15 * call 'make proto' to update
16 */
17
18/* AISC_MKPT_PROMOTE:struct arb_params {*/
19/* AISC_MKPT_PROMOTE:    char *species_name;*/
20/* AISC_MKPT_PROMOTE:    char *extended_name;*/
21/* AISC_MKPT_PROMOTE:    char *alignment;*/
22/* AISC_MKPT_PROMOTE:    char *default_file;*/
23/* AISC_MKPT_PROMOTE:    char *field;*/
24/* AISC_MKPT_PROMOTE:*/
25/* AISC_MKPT_PROMOTE:    int  read_only;*/
26/* AISC_MKPT_PROMOTE:*/
27/* AISC_MKPT_PROMOTE:    char *job_server;*/
28/* AISC_MKPT_PROMOTE:    char *db_server;*/
29/* AISC_MKPT_PROMOTE:    char *mgr_server;*/
30/* AISC_MKPT_PROMOTE:    char *pt_server;*/
31/* AISC_MKPT_PROMOTE:*/
32/* AISC_MKPT_PROMOTE:    char *tcp;*/
33/* AISC_MKPT_PROMOTE:};*/
34
35#define TRIES 1
36
37struct gl_struct {
38    aisc_com *link;
39    long      locs;
40    long      com;
41} glservercntrl;
42
43
44char *prefixSSH(const char *host, const char *command, int async) {
45    /* 'host' is a hostname or 'hostname:port' (where hostname may be an IP)
46       'command' is the command to be executed
47       if 'async' is 1 -> append '&'
48
49       returns a SSH system call for foreign host or
50       a direct system call for the local machine
51    */
52
53    char *result    = 0;
54    char  asyncChar = "&"[!async];
55
56    if (host && host[0]) {
57        const char *hostPort = strchr(host, ':');
58        char       *hostOnly = GB_strpartdup(host, hostPort ? hostPort-1 : 0);
59
60        if (!GB_host_is_local(hostOnly)) {
61            result = GBS_global_string_copy("ssh %s -n '%s' %c", hostOnly, command, asyncChar);
62        }
63        free(hostOnly);
64    }
65
66    if (!result) {
67        result = GBS_global_string_copy("(%s) %c", command, asyncChar);
68    }
69
70    return result;
71}
72
73GB_ERROR arb_start_server(const char *arb_tcp_env, GBDATA *gbmain, int do_sleep)
74{
75    const char *tcp_id;
76    GB_ERROR error = 0;
77
78    if (!(tcp_id = GBS_read_arb_tcp(arb_tcp_env))) {
79        error = GB_export_error("Entry '%s' in $(ARBHOME)/lib/arb_tcp.dat not found", arb_tcp_env);
80    }
81    else {
82        const char *server       = strchr(tcp_id, 0) + 1;
83        char       *serverparams = 0;
84
85        /* concatenate all params behind server
86           Note :  changed behavior on 2007/Mar/09 -- ralf
87           serverparams now is one space if nothing defined in arb_tcp.dat
88           (previously was same as 'server' - most likely a bug)
89        */
90        {
91            const char *param  = strchr(server, 0)+1;
92            size_t      plen   = strlen(param);
93            size_t      alllen = 0;
94
95            while (plen) {
96                param  += plen+1;
97                alllen += plen+1;
98                plen    = strlen(param);
99            }
100
101            serverparams = (char*)malloc(alllen+1);
102            {
103                char *sp = serverparams;
104
105                param = strchr(server, 0)+1;
106                plen  = strlen(param);
107                if (!plen) sp++;
108                else do {
109                    memcpy(sp, param, plen);
110                    sp[plen]  = ' ';
111                    sp       += plen+1;
112                    param    += plen+1;
113                    plen      = strlen(param);
114                } while (plen);
115                sp[-1] = 0;
116            }
117        }
118
119        {
120            char *command = 0;
121            int   delay   = 5;
122
123            if (*tcp_id == ':') { /* local mode */
124                command = GBS_global_string_copy("%s %s -T%s &",server, serverparams, tcp_id);
125            }
126            else {
127                const char *port = strchr(tcp_id, ':');
128
129                if (!port) {
130                    error = GB_export_error("Error: Missing ':' in line '%s' file $(ARBHOME)/lib/arb_tcp.dat", arb_tcp_env);
131                }
132                else {
133                    char *remoteCommand = GBS_global_string_copy("$ARBHOME/bin/%s %s -T%s", server, serverparams, port);
134
135                    command = prefixSSH(tcp_id, remoteCommand, 1);
136                    free(remoteCommand);
137                }
138            }
139
140            if (!error) {
141#if defined(DEBUG)
142                printf("Starting server (cmd='%s')\n", command);
143#endif /* DEBUG */
144                if (!gbmain || GBCMC_system(gbmain,command)) system(command);
145                if (do_sleep) sleep(delay);
146            }
147            free(command);
148        }
149        free(serverparams);
150    }
151    return error;
152}
153
154static GB_ERROR arb_wait_for_server(const char *arb_tcp_env, GBDATA *gbmain, const char *tcp_id, int magic_number, struct gl_struct *serverctrl, int wait) {
155    serverctrl->link = aisc_open(tcp_id, &(serverctrl->com), magic_number);
156    if (!serverctrl->link) { // no server running -> start one
157        GB_ERROR error = arb_start_server(arb_tcp_env, gbmain, 0);
158        if (error) return error;
159
160        while (!serverctrl->link && wait) {
161            sleep(1);
162            wait--;
163            if ((wait%10) == 0 && wait>0) {
164                printf("Waiting for server '%s' to come up (%i seconds left)\n", arb_tcp_env, wait);
165            }
166            serverctrl->link  = aisc_open(tcp_id, &(serverctrl->com), magic_number);
167        }
168    }
169
170    return 0;
171}
172
173GB_ERROR arb_look_and_start_server(long magic_number, const char *arb_tcp_env, GBDATA *gbmain) {
174    GB_ERROR    error       = 0;
175    const char *tcp_id      = GBS_read_arb_tcp(arb_tcp_env);
176    const char *arb_tcp_dat = "$(ARBHOME)/lib/arb_tcp.dat";
177
178    if (!tcp_id) {
179        error = GBS_global_string("Entry '%s' not found in %s", arb_tcp_env, arb_tcp_dat);
180    }
181    else {
182        const char *file = GBS_scan_arb_tcp_param(tcp_id, "-d"); // find parameter behind '-d'
183
184        if (!file) {
185            error = GBS_global_string("Parameter -d missing for entry '%s' in %s", arb_tcp_env, arb_tcp_dat);
186        }
187        else {
188            if (strcmp(file, "!ASSUME_RUNNING") == 0) {
189                // assume pt-server is running on a host,  w/o access to common network drive
190                // i.e. we cannot check for the existance of the database file
191            }
192            else if (GB_size_of_file(file) <= 0) {
193                if (strncmp(arb_tcp_env, "ARB_NAME_SERVER", 15) == 0) {
194                    char *dir       = strdup(file);
195                    char *lastSlash = strrchr(dir, '/');
196
197                    if (lastSlash) {
198                        lastSlash[0]         = 0; // cut off file
199                        {
200                            const char *copy_cmd = GBS_global_string("cp %s/names.dat.template %s", dir, file);
201                            system(copy_cmd);
202                        }
203                        if (GB_size_of_file(file) <= 0) {
204                            error = GBS_global_string("Cannot copy nameserver template (%s/names.dat.template missing?)", dir);
205                        }
206                    }
207                    else {
208                        error = GBS_global_string("Can't determine directory from '%s'", dir);
209                    }
210                    free(dir);
211                }
212                else if (strncmp(arb_tcp_env, "ARB_PT_SERVER", 13) == 0) {
213                    const char *nameOnly    = strrchr(file, '/');
214                    if (!nameOnly) nameOnly = file;
215
216                    error = GBS_global_string("PT_server '%s' has not been created yet.\n"
217                                              " To create it follow these steps:\n"
218                                              " 1. Start ARB on the whole database you want to use for probe match/design\n"
219                                              " 2. Go to ARB_NTREE/Probes/PT_SERVER Admin\n"
220                                              " 3. Select '%s' and press BUILD SERVER\n"
221                                              " 4. Wait (up to hours, depending on your DB size)\n"
222                                              " 5. Meanwhile read the help file: PT_SERVER: What Why and How",
223                                              file, nameOnly);
224                }
225                else {
226                    error = GBS_global_string("The file '%s' is missing. \nUnable to start %s", file, arb_tcp_env);
227                }
228            }
229        }
230
231        if (!error) {
232            error = arb_wait_for_server(arb_tcp_env, gbmain, tcp_id, magic_number, &glservercntrl, 20);
233
234            if (!error) {
235                if (!glservercntrl.link) { // couldn't start server
236                    error =
237                        "I got some problems to start your server:\n"
238                        "   Possible Reasons may be one or more of the following list:\n"
239                        "   - there is no database in $ARBHOME/lib/pts/*\n"
240                        "     update server <ARB_NTREE/Probes/PT_SERVER Admin/BUILD SERVER>\n"
241                        "   - you are not allowed to run 'ssh host pt_server ....&'\n"
242                        "     check file '/etc/hosts.equiv' (read man pages for help)\n"
243                        "   - the permissions of $ARBHOME/lib/pts/* do not allow read access\n"
244                        "   - the PT_SERVER host is not up\n"
245                        "   - the tcp_id is already used by another program\n"
246                        "     check $ARBHOME/lib/arb_tcp.dat and /etc/services\n";
247                }
248                else {
249                    aisc_close(glservercntrl.link);
250                    glservercntrl.link = 0;
251                }
252            }
253        }
254    }
255
256    return error;
257}
258
259GB_ERROR arb_look_and_kill_server(int magic_number, const char *arb_tcp_env)
260{
261    const char *tcp_id;
262    GB_ERROR    error = 0;
263
264    if (!(tcp_id = GBS_read_arb_tcp(arb_tcp_env))) {
265        error = GB_export_error("Missing line '%s' in $(ARBHOME)/lib/arb_tcp.dat:",arb_tcp_env);
266    }
267    else {
268        const char *server = strchr(tcp_id, 0)+1;
269
270        glservercntrl.link = (aisc_com *) aisc_open(tcp_id, &glservercntrl.com, magic_number);
271        if (glservercntrl.link) {
272            const char *command = GBS_global_string("%s -kill -T%s &", server, tcp_id);
273            /* sprintf(command, "%s -kill -T%s &", server, tcp_id); */
274            if (system(command)) {
275                error = GB_export_error("Cannot execute '%s'",command);
276            }
277            aisc_close(glservercntrl.link);
278            glservercntrl.link = 0;
279        }
280        else {
281            error= GB_export_error("I cannot kill your server because I cannot find it");
282        }
283    }
284    return error;
285}
286
287void arb_print_server_params() {
288    printf("General server parameters (some maybe unused by this server):\n"
289           "    -s<name>        sets species name to '<name>'\n"
290           "    -e<name>        sets extended name to '<name>'\n"
291           "    -a<ali>         sets alignment to '<ali>'\n"
292           "    -d<file>        sets default file to '<file>'\n"
293           "    -f<field>       sets DB field to '<field>'\n"
294           "    -r              read-only mode\n"
295           "    -D<server>      sets DB-server to '<server>'  [default = ':']\n"
296           "    -J<server>      sets job-server to '<server>' [default = 'ARB_JOB_SERVER']\n"
297           "    -M<server>      sets MGR-server to '<server>' [default = 'ARB_MGR_SERVER']\n"
298           "    -P<server>      sets PT-server to '<server>'  [default = 'ARB_PT_SERVER']\n"
299           "    -T<[host]:port>   sets TCP connection to '<[host]:port>'\n"
300           );
301}
302
303struct arb_params *arb_trace_argv(int *argc, char **argv)
304{
305    struct arb_params *erg;
306    int s,d;
307
308    erg             = (struct arb_params *)calloc(sizeof(struct arb_params),1);
309    erg->db_server  = GB_strdup(":");
310    erg->job_server = GB_strdup("ARB_JOB_SERVER");
311    erg->mgr_server = GB_strdup("ARB_MGR_SERVER");
312    erg->pt_server  = GB_strdup("ARB_PT_SERVER");
313
314    for (s=d=0; s<*argc; s++) {
315        if (argv[s][0] == '-') {
316            switch (argv[s][1]) {
317                case 's': erg->species_name  = strdup(argv[s]+2);break;
318                case 'e': erg->extended_name = strdup(argv[s]+2);break;
319                case 'a': erg->alignment     = strdup(argv[s]+2);break;
320                case 'd': erg->default_file  = strdup(argv[s]+2);break;
321                case 'f': erg->field         = strdup(argv[s]+2);break;
322                case 'r': erg->read_only     = 1;break;
323                case 'J': erg->job_server    = strdup(argv[s]+2);break;
324                case 'D': erg->db_server     = strdup(argv[s]+2);break;
325                case 'M': erg->mgr_server    = strdup(argv[s]+2);break;
326                case 'P': erg->pt_server     = strdup(argv[s]+2);break;
327                case 'T': {
328                    char *ipport = argv[s]+2;
329                    if (ipport[0] == ':') { /* port only -> assume localhost */
330                        erg->tcp = GBS_global_string_copy("localhost%s", ipport);
331                    }
332                    else {
333                        erg->tcp = strdup(ipport);
334                    }
335                    break;
336                }
337                default:
338                    argv[d++] = argv[s];
339                    break;
340            }
341        }
342        else {
343            argv[d++] = argv[s];
344        }
345    }
346    *argc = d;
347    return erg;
348}
Note: See TracBrowser for help on using the repository browser.