2 * minit version 0.9.1 by Felix von Leitner
3 * ported to busybox by Glenn McGrath <bug1@optushome.com.au>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <sys/fcntl.h>
30 static int infd, outfd;
32 static char buf[1500];
34 static unsigned int fmt_ulong(char *dest, unsigned long i)
36 register unsigned long len, tmp, len2;
38 /* first count the number of bytes needed */
39 for (len = 1, tmp = i; tmp > 9; ++len)
42 for (tmp = i, dest += len, len2 = len + 1; --len2; tmp /= 10)
43 *--dest = (tmp % 10) + '0';
47 static void addservice(char *service)
51 if (strncmp(service, "/etc/minit/", 11) == 0) {
55 while ((service_ptr = last_char_is(service, '/')) != NULL) {
58 strncpy(buf + 1, service, 1400);
62 static int addreadwrite(char *service)
65 write(infd, buf, strlen(buf));
66 return read(outfd, buf, 1500);
69 /* return PID, 0 if error */
70 static pid_t __readpid(char *service)
75 len = addreadwrite(service);
82 /* return nonzero if error */
83 static int respawn(char *service, int yesno)
87 buf[0] = yesno ? 'R' : 'r';
88 len = addreadwrite(service);
89 return (len != 1 || buf[0] == '0');
92 /* return nonzero if error */
93 static int startservice(char *service)
98 len = addreadwrite(service);
99 return (len != 1 || buf[0] == '0');
102 extern int msvc_main(int argc, char **argv)
107 infd = bb_xopen("/etc/minit/in", O_WRONLY);
108 outfd = bb_xopen("/etc/minit/out", O_RDONLY);
110 while (lockf(infd, F_LOCK, 1)) {
111 bb_perror_msg("could not aquire lock!\n");
116 pid_t pid = __readpid(argv[1]);
122 printf("%s: ", argv[1]);
128 printf("up (pid %d) ", pid);
132 len = addreadwrite(argv[1]);
139 printf("%d seconds\n", up_time);
148 bb_error_msg_and_die("no such service");
156 if (argv[1][0] == '-') {
157 switch (argv[1][1]) {
159 for (i = 2; i < argc; ++i) {
160 pid = __readpid(argv[i]);
163 bb_error_msg("%s, service termination", argv[i]);
165 bb_error_msg("%s, no such service", argv[i]);
201 for (i = 2; i < argc; ++i)
202 if (startservice(argv[i]) || respawn(argv[i], 0)) {
203 bb_error_msg("Couldnt not start %s\n", argv[i]);
208 for (i = 2; i < argc; ++i) {
209 pid = __readpid(argv[i]);
211 bb_error_msg("%s, no such service\n", argv[i]);
215 if (respawn(argv[i], 0) || kill(pid, SIGTERM)
216 || kill(pid, SIGCONT));
220 for (i = 2; i < argc; ++i) {
221 if (startservice(argv[i]) || respawn(argv[i], 1)) {
222 bb_error_msg("Couldnt not start %s\n", argv[i]);
228 for (i = 2; i < argc; ++i) {
232 len = addreadwrite(argv[i]);
233 if (len != 1 || buf[0] == '0') {
234 bb_error_msg("%s has terminated or was killed\n",
241 pid = atoi(argv[1] + 2);
248 tmp = buf + strlen(buf) + 1;
249 tmp[fmt_ulong(tmp, pid)] = 0;
250 write(infd, buf, strlen(buf) + strlen(tmp) + 2);
251 len = read(outfd, buf, 1500);
252 if (len != 1 || buf[0] == '0') {
253 bb_error_msg_and_die("Couldnt not set pid of service %s\n", argv[2]);
265 for (i = 2; i < argc; i++) {
266 pid = __readpid(argv[i]);
268 bb_error_msg("%s no such service\n", argv[i]);
271 if (kill(pid, sig)) {
272 bb_error_msg("%s, could not send signal %d to PID %d\n",
282 -u Up. If the service is not running, start it. If the service stops,
284 -d Down. If the service is running, send it a TERM signal and then a CONT
285 signal. After it stops, do not restart it.
286 -o Once. If the service is not running, start it. Do not restart it if it
288 -r Tell supervise that the service is normally running; this affects status
290 -s Tell supervise that the service is normally stopped; this affects status
292 -p Pause. Send the service a STOP signal.
293 -c Continue. Send the service a CONT signal.
294 -h Hangup. Send the service a HUP signal.
295 -a Alarm. Send the service an ALRM signal.
296 -i Interrupt. Send the service an INT signal.
297 -t Terminate. Send the service a TERM signal.
298 -k Kill. Send the service a KILL signal.
299 -x Exit. supervise will quit as soon as the service is down.