Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 1 | /* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */ |
| 2 | /* TODO: depends on runit_lib.c - review and reduce/eliminate */ |
| 3 | |
| 4 | #include <sys/poll.h> |
| 5 | #include <sys/file.h> |
| 6 | #include "busybox.h" |
| 7 | #include "runit_lib.h" |
| 8 | |
| 9 | static int selfpipe[2]; |
| 10 | |
| 11 | /* state */ |
| 12 | #define S_DOWN 0 |
| 13 | #define S_RUN 1 |
| 14 | #define S_FINISH 2 |
| 15 | /* ctrl */ |
| 16 | #define C_NOOP 0 |
| 17 | #define C_TERM 1 |
| 18 | #define C_PAUSE 2 |
| 19 | /* want */ |
| 20 | #define W_UP 0 |
| 21 | #define W_DOWN 1 |
| 22 | #define W_EXIT 2 |
| 23 | |
| 24 | struct svdir { |
| 25 | int pid; |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 26 | smallint state; |
| 27 | smallint ctrl; |
| 28 | smallint want; |
| 29 | smallint islog; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 30 | struct taia start; |
| 31 | int fdlock; |
| 32 | int fdcontrol; |
| 33 | int fdcontrolwrite; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 34 | }; |
| 35 | static struct svdir svd[2]; |
| 36 | |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 37 | static smallint sigterm; |
| 38 | static smallint haslog; |
| 39 | static smallint pidchanged = 1; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 40 | static int logpipe[2]; |
| 41 | static char *dir; |
| 42 | |
| 43 | #define usage() bb_show_usage() |
| 44 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 45 | static void fatal2_cannot(const char *m1, const char *m2) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 46 | { |
| 47 | bb_perror_msg_and_die("%s: fatal: cannot %s%s", dir, m1, m2); |
| 48 | /* was exiting 111 */ |
| 49 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 50 | static void fatal_cannot(const char *m) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 51 | { |
| 52 | fatal2_cannot(m, ""); |
| 53 | /* was exiting 111 */ |
| 54 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 55 | static void fatal2x_cannot(const char *m1, const char *m2) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 56 | { |
| 57 | bb_error_msg_and_die("%s: fatal: cannot %s%s", dir, m1, m2); |
| 58 | /* was exiting 111 */ |
| 59 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 60 | static void warn_cannot(const char *m) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 61 | { |
| 62 | bb_perror_msg("%s: warning: cannot %s", dir, m); |
| 63 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 64 | static void warnx_cannot(const char *m) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 65 | { |
| 66 | bb_error_msg("%s: warning: cannot %s", dir, m); |
| 67 | } |
| 68 | |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 69 | static void s_child(int sig_no) |
| 70 | { |
| 71 | write(selfpipe[1], "", 1); |
| 72 | } |
| 73 | |
| 74 | static void s_term(int sig_no) |
| 75 | { |
| 76 | sigterm = 1; |
| 77 | write(selfpipe[1], "", 1); /* XXX */ |
| 78 | } |
| 79 | |
| 80 | static char *add_str(char *p, const char *to_add) |
| 81 | { |
| 82 | while ((*p = *to_add) != '\0') { |
| 83 | p++; |
| 84 | to_add++; |
| 85 | } |
| 86 | return p; |
| 87 | } |
| 88 | |
| 89 | static int open_trunc_or_warn(const char *name) |
| 90 | { |
| 91 | int fd = open_trunc(name); |
| 92 | if (fd < 0) |
| 93 | bb_perror_msg("%s: warning: cannot open %s", |
| 94 | dir, name); |
| 95 | return fd; |
| 96 | } |
| 97 | |
| 98 | static int rename_or_warn(const char *old, const char *new) |
| 99 | { |
| 100 | if (rename(old, new) == -1) { |
| 101 | bb_perror_msg("%s: warning: cannot rename %s to %s", |
| 102 | dir, old, new); |
| 103 | return -1; |
| 104 | } |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static void update_status(struct svdir *s) |
| 109 | { |
| 110 | unsigned long l; |
| 111 | int fd; |
| 112 | char status[20]; |
| 113 | |
| 114 | /* pid */ |
| 115 | if (pidchanged) { |
| 116 | fd = open_trunc_or_warn("supervise/pid.new"); |
| 117 | if (fd < 0) |
| 118 | return; |
| 119 | if (s->pid) { |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 120 | char spid[sizeof(int)*3 + 2]; |
| 121 | int size = sprintf(spid, "%u\n", (unsigned)s->pid); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 122 | write(fd, spid, size); |
| 123 | } |
| 124 | close(fd); |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 125 | if (rename_or_warn("supervise/pid.new", |
| 126 | s->islog ? "log/supervise/pid" : "log/supervise/pid"+4)) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 127 | return; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 128 | pidchanged = 0; |
| 129 | } |
| 130 | |
| 131 | /* stat */ |
| 132 | fd = open_trunc_or_warn("supervise/stat.new"); |
| 133 | if (fd < -1) |
| 134 | return; |
| 135 | |
| 136 | { |
| 137 | char stat_buf[sizeof("finish, paused, got TERM, want down\n")]; |
| 138 | char *p = stat_buf; |
| 139 | switch (s->state) { |
| 140 | case S_DOWN: |
| 141 | p = add_str(p, "down"); |
| 142 | break; |
| 143 | case S_RUN: |
| 144 | p = add_str(p, "run"); |
| 145 | break; |
| 146 | case S_FINISH: |
| 147 | p = add_str(p, "finish"); |
| 148 | break; |
| 149 | } |
| 150 | if (s->ctrl & C_PAUSE) p = add_str(p, ", paused"); |
| 151 | if (s->ctrl & C_TERM) p = add_str(p, ", got TERM"); |
| 152 | if (s->state != S_DOWN) |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 153 | switch (s->want) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 154 | case W_DOWN: |
| 155 | p = add_str(p, ", want down"); |
| 156 | break; |
| 157 | case W_EXIT: |
| 158 | p = add_str(p, ", want exit"); |
| 159 | break; |
| 160 | } |
| 161 | *p++ = '\n'; |
| 162 | write(fd, stat_buf, p - stat_buf); |
| 163 | close(fd); |
| 164 | } |
| 165 | |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 166 | rename_or_warn("supervise/stat.new", |
| 167 | s->islog ? "log/supervise/stat" : "log/supervise/stat"+4); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 168 | |
| 169 | /* supervise compatibility */ |
| 170 | taia_pack(status, &s->start); |
| 171 | l = (unsigned long)s->pid; |
| 172 | status[12] = l; l >>=8; |
| 173 | status[13] = l; l >>=8; |
| 174 | status[14] = l; l >>=8; |
| 175 | status[15] = l; |
| 176 | if (s->ctrl & C_PAUSE) |
| 177 | status[16] = 1; |
| 178 | else |
| 179 | status[16] = 0; |
| 180 | if (s->want == W_UP) |
| 181 | status[17] = 'u'; |
| 182 | else |
| 183 | status[17] = 'd'; |
| 184 | if (s->ctrl & C_TERM) |
| 185 | status[18] = 1; |
| 186 | else |
| 187 | status[18] = 0; |
| 188 | status[19] = s->state; |
| 189 | fd = open_trunc_or_warn("supervise/status.new"); |
| 190 | if (fd < 0) |
| 191 | return; |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 192 | l = write(fd, status, sizeof(status)); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 193 | if (l < 0) { |
| 194 | warn_cannot("write supervise/status.new"); |
| 195 | close(fd); |
| 196 | unlink("supervise/status.new"); |
| 197 | return; |
| 198 | } |
| 199 | close(fd); |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 200 | if (l < sizeof(status)) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 201 | warnx_cannot("write supervise/status.new: partial write"); |
| 202 | return; |
| 203 | } |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 204 | rename_or_warn("supervise/status.new", |
| 205 | s->islog ? "log/supervise/status" : "log/supervise/status"+4); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static unsigned custom(struct svdir *s, char c) |
| 209 | { |
| 210 | int pid; |
| 211 | int w; |
| 212 | char a[10]; |
| 213 | struct stat st; |
| 214 | char *prog[2]; |
| 215 | |
| 216 | if (s->islog) return 0; |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 217 | strcpy(a, "control/?"); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 218 | a[8] = c; |
| 219 | if (stat(a, &st) == 0) { |
| 220 | if (st.st_mode & S_IXUSR) { |
| 221 | pid = fork(); |
| 222 | if (pid == -1) { |
| 223 | warn_cannot("fork for control/?"); |
| 224 | return 0; |
| 225 | } |
| 226 | if (!pid) { |
| 227 | if (haslog && fd_copy(1, logpipe[1]) == -1) |
| 228 | warn_cannot("setup stdout for control/?"); |
| 229 | prog[0] = a; |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 230 | prog[1] = NULL; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 231 | execve(a, prog, environ); |
| 232 | fatal_cannot("run control/?"); |
| 233 | } |
| 234 | while (wait_pid(&w, pid) == -1) { |
| 235 | if (errno == EINTR) continue; |
| 236 | warn_cannot("wait for child control/?"); |
| 237 | return 0; |
| 238 | } |
| 239 | return !wait_exitcode(w); |
| 240 | } |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 241 | } else { |
| 242 | if (errno != ENOENT) |
| 243 | warn_cannot("stat control/?"); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static void stopservice(struct svdir *s) |
| 249 | { |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 250 | if (s->pid && !custom(s, 't')) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 251 | kill(s->pid, SIGTERM); |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 252 | s->ctrl |= C_TERM; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 253 | update_status(s); |
| 254 | } |
| 255 | if (s->want == W_DOWN) { |
| 256 | kill(s->pid, SIGCONT); |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 257 | custom(s, 'd'); |
| 258 | return; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 259 | } |
| 260 | if (s->want == W_EXIT) { |
| 261 | kill(s->pid, SIGCONT); |
| 262 | custom(s, 'x'); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static void startservice(struct svdir *s) |
| 267 | { |
| 268 | int p; |
| 269 | char *run[2]; |
| 270 | |
| 271 | if (s->state == S_FINISH) |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 272 | run[0] = (char*)"./finish"; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 273 | else { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 274 | run[0] = (char*)"./run"; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 275 | custom(s, 'u'); |
| 276 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 277 | run[1] = NULL; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 278 | |
| 279 | if (s->pid != 0) stopservice(s); /* should never happen */ |
| 280 | while ((p = fork()) == -1) { |
| 281 | warn_cannot("fork, sleeping"); |
| 282 | sleep(5); |
| 283 | } |
| 284 | if (p == 0) { |
| 285 | /* child */ |
| 286 | if (haslog) { |
| 287 | if (s->islog) { |
| 288 | if (fd_copy(0, logpipe[0]) == -1) |
| 289 | fatal_cannot("setup filedescriptor for ./log/run"); |
| 290 | close(logpipe[1]); |
| 291 | if (chdir("./log") == -1) |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 292 | fatal_cannot("change directory to ./log"); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 293 | } else { |
| 294 | if (fd_copy(1, logpipe[1]) == -1) |
| 295 | fatal_cannot("setup filedescriptor for ./run"); |
| 296 | close(logpipe[0]); |
| 297 | } |
| 298 | } |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 299 | sig_uncatch(SIGCHLD); |
| 300 | sig_unblock(SIGCHLD); |
| 301 | sig_uncatch(SIGTERM); |
| 302 | sig_unblock(SIGTERM); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 303 | execve(*run, run, environ); |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 304 | fatal2_cannot(s->islog ? "start log/" : "start ", *run); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 305 | } |
| 306 | if (s->state != S_FINISH) { |
| 307 | taia_now(&s->start); |
| 308 | s->state = S_RUN; |
| 309 | } |
| 310 | s->pid = p; |
| 311 | pidchanged = 1; |
| 312 | s->ctrl = C_NOOP; |
| 313 | update_status(s); |
| 314 | } |
| 315 | |
| 316 | static int ctrl(struct svdir *s, char c) |
| 317 | { |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 318 | int sig; |
| 319 | |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 320 | switch (c) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 321 | case 'd': /* down */ |
| 322 | s->want = W_DOWN; |
| 323 | update_status(s); |
| 324 | if (s->pid && s->state != S_FINISH) stopservice(s); |
| 325 | break; |
| 326 | case 'u': /* up */ |
| 327 | s->want = W_UP; |
| 328 | update_status(s); |
| 329 | if (s->pid == 0) startservice(s); |
| 330 | break; |
| 331 | case 'x': /* exit */ |
| 332 | if (s->islog) break; |
| 333 | s->want = W_EXIT; |
| 334 | update_status(s); |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 335 | /* FALLTHROUGH */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 336 | case 't': /* sig term */ |
| 337 | if (s->pid && s->state != S_FINISH) stopservice(s); |
| 338 | break; |
| 339 | case 'k': /* sig kill */ |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 340 | if (s->pid && !custom(s, c)) kill(s->pid, SIGKILL); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 341 | s->state = S_DOWN; |
| 342 | break; |
| 343 | case 'p': /* sig pause */ |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 344 | if (s->pid && !custom(s, c)) kill(s->pid, SIGSTOP); |
| 345 | s->ctrl |= C_PAUSE; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 346 | update_status(s); |
| 347 | break; |
| 348 | case 'c': /* sig cont */ |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 349 | if (s->pid && !custom(s, c)) kill(s->pid, SIGCONT); |
| 350 | if (s->ctrl & C_PAUSE) s->ctrl &= ~C_PAUSE; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 351 | update_status(s); |
| 352 | break; |
| 353 | case 'o': /* once */ |
| 354 | s->want = W_DOWN; |
| 355 | update_status(s); |
| 356 | if (!s->pid) startservice(s); |
| 357 | break; |
| 358 | case 'a': /* sig alarm */ |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 359 | sig = SIGALRM; |
| 360 | goto sendsig; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 361 | case 'h': /* sig hup */ |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 362 | sig = SIGHUP; |
| 363 | goto sendsig; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 364 | case 'i': /* sig int */ |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 365 | sig = SIGINT; |
| 366 | goto sendsig; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 367 | case 'q': /* sig quit */ |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 368 | sig = SIGQUIT; |
| 369 | goto sendsig; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 370 | case '1': /* sig usr1 */ |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 371 | sig = SIGUSR1; |
| 372 | goto sendsig; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 373 | case '2': /* sig usr2 */ |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 374 | sig = SIGUSR2; |
| 375 | goto sendsig; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 376 | } |
| 377 | return 1; |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 378 | sendsig: |
| 379 | if (s->pid && !custom(s, c)) |
| 380 | kill(s->pid, sig); |
| 381 | return 1; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 384 | int runsv_main(int argc, char **argv); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 385 | int runsv_main(int argc, char **argv) |
| 386 | { |
| 387 | struct stat s; |
| 388 | int fd; |
| 389 | int r; |
| 390 | char buf[256]; |
| 391 | |
| 392 | if (!argv[1] || argv[2]) usage(); |
| 393 | dir = argv[1]; |
| 394 | |
| 395 | if (pipe(selfpipe) == -1) fatal_cannot("create selfpipe"); |
| 396 | coe(selfpipe[0]); |
| 397 | coe(selfpipe[1]); |
| 398 | ndelay_on(selfpipe[0]); |
| 399 | ndelay_on(selfpipe[1]); |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 400 | |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 401 | sig_block(SIGCHLD); |
| 402 | sig_catch(SIGCHLD, s_child); |
| 403 | sig_block(SIGTERM); |
| 404 | sig_catch(SIGTERM, s_term); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 405 | |
| 406 | xchdir(dir); |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 407 | /* bss: svd[0].pid = 0; */ |
| 408 | if (S_DOWN) svd[0].state = S_DOWN; /* otherwise already 0 (bss) */ |
| 409 | if (C_NOOP) svd[0].ctrl = C_NOOP; |
| 410 | if (W_UP) svd[0].want = W_UP; |
| 411 | /* bss: svd[0].islog = 0; */ |
| 412 | /* bss: svd[1].pid = 0; */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 413 | taia_now(&svd[0].start); |
| 414 | if (stat("down", &s) != -1) svd[0].want = W_DOWN; |
| 415 | |
| 416 | if (stat("log", &s) == -1) { |
| 417 | if (errno != ENOENT) |
| 418 | warn_cannot("stat ./log"); |
| 419 | } else { |
| 420 | if (!S_ISDIR(s.st_mode)) |
| 421 | warnx_cannot("stat log/down: log is not a directory"); |
| 422 | else { |
| 423 | haslog = 1; |
| 424 | svd[1].state = S_DOWN; |
| 425 | svd[1].ctrl = C_NOOP; |
| 426 | svd[1].want = W_UP; |
| 427 | svd[1].islog = 1; |
| 428 | taia_now(&svd[1].start); |
| 429 | if (stat("log/down", &s) != -1) |
| 430 | svd[1].want = W_DOWN; |
| 431 | if (pipe(logpipe) == -1) |
| 432 | fatal_cannot("create log pipe"); |
| 433 | coe(logpipe[0]); |
| 434 | coe(logpipe[1]); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | if (mkdir("supervise", 0700) == -1) { |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 439 | r = readlink("supervise", buf, sizeof(buf)); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 440 | if (r != -1) { |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 441 | if (r == sizeof(buf)) |
| 442 | fatal2x_cannot("readlink ./supervise", ": name too long"); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 443 | buf[r] = 0; |
| 444 | mkdir(buf, 0700); |
| 445 | } else { |
| 446 | if ((errno != ENOENT) && (errno != EINVAL)) |
| 447 | fatal_cannot("readlink ./supervise"); |
| 448 | } |
| 449 | } |
| 450 | svd[0].fdlock = xopen3("log/supervise/lock"+4, |
| 451 | O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600); |
| 452 | if (lock_exnb(svd[0].fdlock) == -1) |
| 453 | fatal_cannot("lock supervise/lock"); |
| 454 | coe(svd[0].fdlock); |
| 455 | if (haslog) { |
| 456 | if (mkdir("log/supervise", 0700) == -1) { |
| 457 | r = readlink("log/supervise", buf, 256); |
| 458 | if (r != -1) { |
| 459 | if (r == 256) |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 460 | fatal2x_cannot("readlink ./log/supervise", ": name too long"); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 461 | buf[r] = 0; |
| 462 | fd = xopen(".", O_RDONLY|O_NDELAY); |
| 463 | xchdir("./log"); |
| 464 | mkdir(buf, 0700); |
| 465 | if (fchdir(fd) == -1) |
| 466 | fatal_cannot("change back to service directory"); |
| 467 | close(fd); |
| 468 | } |
| 469 | else { |
| 470 | if ((errno != ENOENT) && (errno != EINVAL)) |
| 471 | fatal_cannot("readlink ./log/supervise"); |
| 472 | } |
| 473 | } |
| 474 | svd[1].fdlock = xopen3("log/supervise/lock", |
| 475 | O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600); |
| 476 | if (lock_ex(svd[1].fdlock) == -1) |
| 477 | fatal_cannot("lock log/supervise/lock"); |
| 478 | coe(svd[1].fdlock); |
| 479 | } |
| 480 | |
Denis Vlasenko | e2473f8 | 2007-01-27 22:22:17 +0000 | [diff] [blame] | 481 | mkfifo("log/supervise/control"+4, 0600); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 482 | svd[0].fdcontrol = xopen("log/supervise/control"+4, O_RDONLY|O_NDELAY); |
| 483 | coe(svd[0].fdcontrol); |
| 484 | svd[0].fdcontrolwrite = xopen("log/supervise/control"+4, O_WRONLY|O_NDELAY); |
| 485 | coe(svd[0].fdcontrolwrite); |
| 486 | update_status(&svd[0]); |
| 487 | if (haslog) { |
Denis Vlasenko | e2473f8 | 2007-01-27 22:22:17 +0000 | [diff] [blame] | 488 | mkfifo("log/supervise/control", 0600); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 489 | svd[1].fdcontrol = xopen("log/supervise/control", O_RDONLY|O_NDELAY); |
| 490 | coe(svd[1].fdcontrol); |
| 491 | svd[1].fdcontrolwrite = xopen("log/supervise/control", O_WRONLY|O_NDELAY); |
| 492 | coe(svd[1].fdcontrolwrite); |
| 493 | update_status(&svd[1]); |
| 494 | } |
Denis Vlasenko | e2473f8 | 2007-01-27 22:22:17 +0000 | [diff] [blame] | 495 | mkfifo("log/supervise/ok"+4, 0600); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 496 | fd = xopen("log/supervise/ok"+4, O_RDONLY|O_NDELAY); |
| 497 | coe(fd); |
| 498 | if (haslog) { |
Denis Vlasenko | e2473f8 | 2007-01-27 22:22:17 +0000 | [diff] [blame] | 499 | mkfifo("log/supervise/ok", 0600); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 500 | fd = xopen("log/supervise/ok", O_RDONLY|O_NDELAY); |
| 501 | coe(fd); |
| 502 | } |
| 503 | for (;;) { |
| 504 | iopause_fd x[3]; |
| 505 | struct taia deadline; |
| 506 | struct taia now; |
| 507 | char ch; |
| 508 | |
| 509 | if (haslog) |
| 510 | if (!svd[1].pid && svd[1].want == W_UP) |
| 511 | startservice(&svd[1]); |
| 512 | if (!svd[0].pid) |
| 513 | if (svd[0].want == W_UP || svd[0].state == S_FINISH) |
| 514 | startservice(&svd[0]); |
| 515 | |
| 516 | x[0].fd = selfpipe[0]; |
| 517 | x[0].events = IOPAUSE_READ; |
| 518 | x[1].fd = svd[0].fdcontrol; |
| 519 | x[1].events = IOPAUSE_READ; |
| 520 | if (haslog) { |
| 521 | x[2].fd = svd[1].fdcontrol; |
| 522 | x[2].events = IOPAUSE_READ; |
| 523 | } |
| 524 | taia_now(&now); |
| 525 | taia_uint(&deadline, 3600); |
| 526 | taia_add(&deadline, &now, &deadline); |
| 527 | |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 528 | sig_unblock(SIGTERM); |
| 529 | sig_unblock(SIGCHLD); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 530 | iopause(x, 2+haslog, &deadline, &now); |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 531 | sig_block(SIGTERM); |
| 532 | sig_block(SIGCHLD); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 533 | |
| 534 | while (read(selfpipe[0], &ch, 1) == 1) |
| 535 | ; |
| 536 | for (;;) { |
| 537 | int child; |
| 538 | int wstat; |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 539 | |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 540 | child = wait_nohang(&wstat); |
| 541 | if (!child) break; |
| 542 | if ((child == -1) && (errno != EINTR)) break; |
| 543 | if (child == svd[0].pid) { |
| 544 | svd[0].pid = 0; |
| 545 | pidchanged = 1; |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 546 | svd[0].ctrl &=~ C_TERM; |
Denis Vlasenko | d0762e3 | 2007-02-18 11:07:43 +0000 | [diff] [blame] | 547 | if (svd[0].state != S_FINISH) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 548 | fd = open_read("finish"); |
| 549 | if (fd != -1) { |
| 550 | close(fd); |
| 551 | svd[0].state = S_FINISH; |
| 552 | update_status(&svd[0]); |
| 553 | continue; |
| 554 | } |
Denis Vlasenko | d0762e3 | 2007-02-18 11:07:43 +0000 | [diff] [blame] | 555 | } |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 556 | svd[0].state = S_DOWN; |
| 557 | taia_uint(&deadline, 1); |
| 558 | taia_add(&deadline, &svd[0].start, &deadline); |
| 559 | taia_now(&svd[0].start); |
| 560 | update_status(&svd[0]); |
| 561 | if (taia_less(&svd[0].start, &deadline)) sleep(1); |
| 562 | } |
| 563 | if (haslog) { |
| 564 | if (child == svd[1].pid) { |
| 565 | svd[1].pid = 0; |
| 566 | pidchanged = 1; |
| 567 | svd[1].state = S_DOWN; |
Denis Vlasenko | 3aba666 | 2007-03-09 22:46:06 +0000 | [diff] [blame^] | 568 | svd[1].ctrl &= ~C_TERM; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 569 | taia_uint(&deadline, 1); |
| 570 | taia_add(&deadline, &svd[1].start, &deadline); |
| 571 | taia_now(&svd[1].start); |
| 572 | update_status(&svd[1]); |
| 573 | if (taia_less(&svd[1].start, &deadline)) sleep(1); |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | if (read(svd[0].fdcontrol, &ch, 1) == 1) |
| 578 | ctrl(&svd[0], ch); |
| 579 | if (haslog) |
| 580 | if (read(svd[1].fdcontrol, &ch, 1) == 1) |
| 581 | ctrl(&svd[1], ch); |
| 582 | |
| 583 | if (sigterm) { |
| 584 | ctrl(&svd[0], 'x'); |
| 585 | sigterm = 0; |
| 586 | } |
| 587 | |
| 588 | if (svd[0].want == W_EXIT && svd[0].state == S_DOWN) { |
| 589 | if (svd[1].pid == 0) |
| 590 | _exit(0); |
| 591 | if (svd[1].want != W_EXIT) { |
| 592 | svd[1].want = W_EXIT; |
| 593 | /* stopservice(&svd[1]); */ |
| 594 | update_status(&svd[1]); |
| 595 | close(logpipe[1]); |
| 596 | close(logpipe[0]); |
| 597 | //if (close(logpipe[1]) == -1) |
| 598 | // warn_cannot("close logpipe[1]"); |
| 599 | //if (close(logpipe[0]) == -1) |
| 600 | // warn_cannot("close logpipe[0]"); |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | /* not reached */ |
| 605 | return 0; |
| 606 | } |