5 /* the commands are handled here */
7 static char *gather_arg(char **args)
12 for (a=0 ; args[a] ; a++) len += strlen(args[a])+1;
13 line = (char*)malloc(len+1);
15 for (a=0 ; args[a] ; a++)
17 strcpy(line+len, args[a]);
18 len += strlen(args[a]);
28 static void do_version(char **args, char *host, void *con)
33 rv = (char*)malloc(strlen(version)+2);
34 strcpy(rv+1, version);
36 set_reply(con, rv, strlen(version)+2);
41 static void do_broad(char **args, char *host, void *con)
43 /* remaining args are packaged up and broadcast on all interfaces */
45 line = gather_arg(args+1);
46 logmsg(LOG_INFO, "Broadcast request from %s for %s", host, line);
51 static void do_die(char **args, char *host, void *con)
53 logmsg(LOG_WARNING, "Die request from %s", host);
57 static void do_disable(char **args, char *host, void *con)
61 return_error(con, "No service given for disabling");
62 else if ((sv=find_service(args[1]))==NULL)
63 return_error(con, "Cannot find service %s to disable it", args[1]);
64 else for ( ; sv ; sv=find_service(NULL))
66 logmsg(LOG_INFO, "Disable request from %s for %s", host, sv->service);
67 (sv->class->disable_service)(sv);
72 static void do_enable(char **args, char *host, void *con)
76 return_error(con, "No service given for enabling");
77 else if ((sv=find_service(args[1]))==NULL)
78 return_error(con, "Cannot find service %s to enable it", args[1]);
79 else for ( ; sv ; sv=find_service(NULL))
81 logmsg(LOG_INFO, "Enable request from %s for %s", host, sv->service);
82 (sv->class->register_service)(sv);
88 static void do_run(char **args, char *host, void *con)
90 /* attempt to run service called args[1] with METAD_ARG set to remaining args and METAD_HOST set to host
94 return_error(con, "No service given to run");
95 else if ((sv=find_service(args[1]))==NULL)
96 return_error(con, "Cannot find service %s to run", args[1]);
99 char *arg = gather_arg(args+2);
100 env[0] = strcat(strcpy((char*)malloc(20+strlen(host)), "METAD_REASON=run:"), host);
101 env[1] = strcat(strcpy((char*)malloc(11+strlen(arg)), "METAD_ARG="), arg);
103 for ( ; sv ; sv = find_service(NULL))
105 /* first clear all hold-times */
107 for (pp=skip_first(sv->proc_list) ; pp ; pp=skip_next(pp))
108 if ((*pp)->hold_time != 0)
109 (*pp)->hold_time = 1;
111 logmsg(LOG_INFO,"starting %s for %s : arg = %s", sv->service, host, arg);
120 static void do_kill(char **args, char *host, void *con)
123 * args[2] = pid or service name
129 return_error(con, "No signal number given for kill");
130 else if ((sig=atoi(args[1]))<= 0)
131 return_error(con, "Bad signal number given for kill: %s", args[1]);
132 else if (args[2] == NULL)
133 return_error(con, "No process id or service name given for kill");
134 else if ((pid = atoi(args[2]))>0) {
135 proc_t *pp = skip_search(allprocs, &pid);
137 logmsg(LOG_INFO, "killing %s for %s", args[2], host);
138 if ((*pp)->exit_time == 0)
139 kill((*pp)->pid, sig);
140 else if ((*pp)->it_forked > 1)
141 kill((*pp)->it_forked, sig);
143 return_error(con, "Cannot find process %s to kill", args[2]);
144 } else if ((sv = find_service(args[2]))!= NULL) {
145 for ( ; sv ; sv = find_service(NULL)) {
147 for (pp = skip_first(sv->proc_list) ; pp ; pp = skip_next(pp))
148 if ((*pp)->exit_time == 0 || (*pp)->it_forked) {
150 "signalling %s:%d with %d for %s", sv->service,
151 (*pp)->exit_time?(*pp)->it_forked:(*pp)->pid,
153 if ((*pp)->exit_time == 0)
154 kill((*pp)->pid, sig);
155 else if ((*pp)->it_forked > 1)
156 kill((*pp)->it_forked, sig);
161 return_error(con, "Cannot find service %s to kill", args[2]);
165 static void do_reread(char **args, char *host, void *con)
169 logmsg(LOG_INFO, "Rereading config file for %s", host);
170 old= errors_to(ERROR_STRING, &errs);
171 if (read_config(services, NULL) != 0)
172 return_error(con, "%s", errs);
173 errors_to(old, NULL);
174 if (errs) free(errs);
177 static void do_list(char **args, char *host, void *con)
181 send_byte(3); /* listing */
182 if (args[1] == NULL) {
183 for (svp = skip_first(services) ; svp ; svp = skip_next(svp))
186 send_byte(0); /* finished */
189 else if ((sv=find_service(args[1])) != NULL) {
190 for ( ; sv ; sv = find_service(NULL))
195 return_error(con, "Cannot find service %s to list", args[1]);
198 void do_restart(char **args, char *host, void *con)
200 logmsg(LOG_INFO, "About to restart for %s", host);
207 static struct commands cmds[] = {
209 { "version", do_version},
210 { "broad", do_broad},
212 { "disable", do_disable},
213 { "enable", do_enable},
217 { "reread", do_reread},
218 { "restart", do_restart},
222 int do_command(char **args, char *host, void *con)
225 for (cmd = 0; cmds[cmd].name ; cmd++)
226 if (strcasecmp(cmds[cmd].name, args[0])==0)
228 if (cmds[cmd].name) {
229 (cmds[cmd].proc)(args, host, con);