summary refs log tree commit diff
path: root/src/runsvctrl.c
blob: 21fe00d6a262c44d81def7916ee7cae234011495 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <unistd.h>
#include "strerr.h"
#include "error.h"
#include "open.h"

#define USAGE " u|d|o|p|c|h|a|i|1|2|t|k|x|e service ..."

#define VERSION "$Id$"

#define FATAL "runsvctrl: fatal: "
#define WARNING "runsvctrl: warning: "

char *progname;
unsigned int rc =0;

void usage() { strerr_die4x(1, "usage: ", progname, USAGE, "\n"); }

void fatal(char *m1) { strerr_die3sys(111, FATAL, m1, ": "); }
void warn(char *m1, char *m2) {
  rc++;
  strerr_warn5(WARNING, m1, ": ", m2, ": ", &strerr_sys);
}
void warnx(char *m1, char *m2) {
  rc++;
  strerr_warn4(WARNING, m1, ": ", m2, 0);
}

int ctrl(char *name, char c) {
  int fd;

  if ((fd =open_write("supervise/control")) == -1) {
    if (errno == error_nodevice)
      warnx(name, "supervise not running.");
    else
      warn(name, "unable to open supervise/control");
    return(-1);
  }
  if (write(fd, &c, 1) != 1) {
    warn(name, "unable to write to supervise/control");
    return(-1);
  }
  return(1);
}

int main(int argc, char **argv) {
  char **dir;
  int curdir;
  char c;

  progname =*argv++;

  if (! argv || ! *argv) usage();

  switch ((c =**argv)) {
  case 'u': case 'd': case 'o': case 'x': case 'e': case 'p': case 'c':
  case 'h': case 'a': case 'i': case 't': case 'k': case '1': case '2':
    break;
  default:
    usage();
  }
  dir =++argv;
  if (! dir || ! *dir) usage();

  if ((curdir =open_read(".")) == -1) {
    rc =100;
    fatal("unable to open current directory");
  }
  for (; dir && *dir; dir++) {
    if (chdir(*dir) == -1) {
      warn(*dir, "unable to change directory");
      continue;
    }
    ctrl(*dir, c);
    if (fchdir(curdir) == -1) {
      rc =100;
      fatal("unable to change directory");
    }
  }
  if (rc > 100) rc =100;
  _exit(rc);
}