summary refs log tree commit diff
path: root/src/runsvstat.c
blob: c17bbd3eda6f3c57027dfb47ff676bdd3fefff9f (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "strerr.h"
#include "error.h"
#include "sgetopt.h"
#include "open.h"
#include "buffer.h"
#include "tai.h"
#include "fmt.h"

#define USAGE " [ -l ] service ..."

#define VERSION "$Id$"

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

const char *progname;
unsigned int rc =0;
struct stat s;
int showlog =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 show_status(char *name) {
  char status[20];
  int pid;
  int fd;
  int normallyup =0;
  char sulong[FMT_ULONG];
  struct tai when;
  struct tai now;

  if (stat("down", &s) == -1) {
    if (errno != error_noent) {
      warn(name, "unable to stat down");
      return(-1);
    }
    normallyup = 1;
  }
  if ((fd =open_write("supervise/ok")) == -1) {
    if (errno == error_nodevice)
      warnx(name, "runsv not running.");
    else
      warn(name, "unable to open supervise/ok");
    return(-1);
  }
  close(fd);
  if ((fd =open_read("supervise/status")) == -1) {
    warn(name, "unable to open supervise/status");
    return(-1);
  }
  switch(read(fd, status, 20)) {
  case 20: break;
  case -1:
    warn(name, "unable to read supervise/status");
    return(-1);
  default:
    warnx(name, "unable to read supervise/status: bad format.");
    return(-1);
  }
  pid =(unsigned char) status[15];
  pid <<=8; pid +=(unsigned char)status[14];
  pid <<=8; pid +=(unsigned char)status[13];
  pid <<=8; pid +=(unsigned char)status[12];

  tai_unpack(status,&when);
  tai_now(&now);
  if (tai_less(&now,&when)) when =now;
  tai_sub(&when,&now,&when);

  buffer_puts(buffer_1, name);
  buffer_puts(buffer_1, ": ");
  if (pid) {
    switch (status[19]) {
    case 1: buffer_puts(buffer_1, "run "); break;
    case 2: buffer_puts(buffer_1, "finish "); break;
    }
    buffer_puts(buffer_1, "(pid ");
    buffer_put(buffer_1, sulong, fmt_ulong(sulong, pid));
    buffer_puts(buffer_1, ") ");
  }
  else
    buffer_puts(buffer_1, "down ");
  buffer_put(buffer_1, sulong, fmt_ulong(sulong, tai_approx(&when)));
  buffer_puts(buffer_1, " seconds");
  if (pid && !normallyup) buffer_puts(buffer_1,", normally down");
  if (!pid && normallyup) buffer_puts(buffer_1,", normally up");
  if (pid && status[16]) buffer_puts(buffer_1,", paused");
  if (!pid && (status[17] == 'u')) buffer_puts(buffer_1,", want up");
  if (pid && (status[17] == 'd')) buffer_puts(buffer_1,", want down");
  if (pid && status[18]) buffer_puts(buffer_1, ", got TERM");
  /* buffer_putsflush(buffer_1, "\n"); */
  return(1);
}

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

  progname =*argv;

  while ((opt =getopt(argc, (const char * const *)argv, "lV")) != opteof) {
    switch(opt) {
    case 'l':
      showlog =1;
      break;
    case 'V':
      strerr_warn1(VERSION, 0);
    case '?':
      usage();
    }
  }
  argv +=optind;

  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;
    }
    if (show_status(*dir) == 1) {
      if (showlog) {
        if (stat("log", &s) == -1) {
          if (errno != error_noent)
            warn("unable to stat()", "./log");
        }
        else {
          if (! S_ISDIR(s.st_mode))
            warnx("./log", "not a directory.");
          else {
            if (chdir("log") == -1) {
              warn(*dir, "unable to change directory");
              continue;
            }
            show_status("\n  log");
          }
        }
      }
      buffer_puts(buffer_1, "\n"); buffer_flush(buffer_1);
    }
    if (fchdir(curdir) == -1) {
      rc =100;
      fatal("unable to change directory");
    }
  }
  if (rc > 100) rc =100;
  _exit(rc);
}