2 /* send commands to metad's
3 * -V display meta version and metad versions
5 * -u send datagrams, don't use stream connection
6 * -b broadcast datagrams on all interfaces
8 * -r service run the service
9 * -a args pass args with previous run command
10 * -k service kick(run) the service
11 * -l service list the service
12 * -L list all services
13 * -D service disable service
14 * -E service enable service
15 * -K pid/service kill process/service (Sig 15)
17 * -X tell metad to exit
19 * -C command send the given command
21 * -B actually send broadcast requests
23 * other arguments are host names
35 void usage(char *fmt, char *arg)
38 sprintf(buf, fmt, arg);
39 fprintf(stderr,"%s: %s\n", progname, buf);
40 fprintf(stderr,"Usage: %s -[h?Vubv] -[LRXB] {-[rklDEK] service} [-C command] hostlist ...\n", progname);
46 "Usage: %s -[h?Vubv] -[LRXB] {-[rklDEK] service} [-C command] hostlist ...\n"
47 " -h This help message\n"
49 " -V Print version number of metac and the metad running on any\n"
51 " -u Send commands in udp datagrams. This is faster than the\n"
53 " avoids delays on dead machines.\n"
54 " -b Send each request as a broadcast on any available network\n"
56 " -v Be verbose when listing services.\n"
57 " -L List all services supported by the metad on the remote host.\n"
58 " -R Tell metad to reread it's configuration file.\n"
59 " -X Tell metad to eXit.\n"
60 " -B Tell the metad to rebroadcast the request on it's\n"
62 " -r service Try to start a process running for the service.\n"
63 " -k service Kick a service into action, same as above.\n"
64 " -l service List details of the named service.\n"
65 " -D service Disable a service. No more processes will be started for\n"
67 " -E service Enable a service. Metad will start processes as\n"
69 " -K pid Of metad is managing a process with number pid, then it\n"
70 " is sent a SIGTERM.\n"
71 " -K service Every process running for the given service is sent a\n"
73 " -C command Send the given command (which will usually be quoted).\n"
75 " run service args like -r, but allows args to be passed to\n"
77 " kill signum pid-or-service like -K, but allows a signal number\n"
79 " restart tell metad to re-exec itself, for use when a\n"
80 " new version is installed\n"
81 " (plus others that duplicate the above flags)\n"
85 int main(int argc, char *argv[])
87 void *cmds = dl_head();
88 void *hosts = dl_head();
96 void *opts = args_init(argc, argv, "??h?V?u?b?r:a:k:l:L?D:E:K:R?X?v?C:B?");
100 progname = strrchr(argv[0], '/');
101 if (progname) progname++; else progname = argv[0];
103 while ((opt = args_next(opts, &pos, &inv, &arg))!= END_ARGS)
107 usage("Bad option: %s", argv[pos]);
117 case 'V': /* my version number */
118 printf("%s: version %s\n", progname, version);
129 c = dl_strndup("kick ", 5+strlen(arg)+1);
135 if (c == cmds || (strncmp(c, "kick ", 5)!= 0 && strncmp(c, "run ", 4)!= 0))
137 usage("%s: -a only valid after -r or -k", NULL);
140 c2 = dl_strndup(c, strlen(c)+1+strlen(arg)+1);
143 dl_del(c); dl_free(c);
147 c = dl_strndup("list ", 5+strlen(arg)+1);
152 c = dl_strdup("list");
156 c = dl_strndup("disable ", 8+strlen(arg)+1);
161 c = dl_strndup("enable ", 7+strlen(arg)+1);
166 c = dl_strndup("kill 15 ", 8+strlen(arg)+1);
171 c = dl_strdup("reread");
175 c = dl_strdup("die");
194 if (dl_next(hosts) == hosts && local_broad == 0) {
195 /* no where to send to... */
196 if (dl_next(cmds) != cmds) {
197 fprintf(stderr,"%s: commands were specified with no where to send them!\n", progname);
200 /* nothing to do, though might has displayed version.
201 * Should probably give usage...
203 if (show_version == 0)
204 usage("Nothing to do", NULL), exit(1);
208 if (dl_next(hosts) != hosts && local_broad) {
209 fprintf(stderr, "%s: you probably don't want to broadcast AND list hosts...\n", progname);
214 c = dl_strdup("version");
217 for (c= dl_next(cmds) ; c != cmds ; c = dl_next(c)) {
218 char *cmd = (char*)malloc(10 + strlen(c)); /* make sure there is room for port and broad */
223 strcat(cmd, "BROAD ");
227 else for (h=dl_next(hosts) ; h != hosts ; h = dl_next(h))
229 if (strcmp(h, ".")== 0)
230 send_cmd(cmd, use_dgram, "localhost", verbose);
232 send_cmd(cmd, use_dgram, h, verbose);
235 exit(0); /* FIXME what exit status */