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