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 | d18f52b | 2008-03-02 12:53:15 +0000 | [diff] [blame] | 28 | /* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 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 | #define MAXSERVICES 1000 |
| 37 | |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 38 | struct service { |
| 39 | dev_t dev; |
| 40 | ino_t ino; |
| 41 | pid_t pid; |
| 42 | smallint isgone; |
| 43 | }; |
| 44 | |
Denis Vlasenko | c9dc2ac | 2007-09-27 10:08:02 +0000 | [diff] [blame] | 45 | static struct service *sv; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 46 | static char *svdir; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 47 | static int svnum; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 48 | static char *rplog; |
| 49 | static int rploglen; |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 50 | static struct fd_pair logpipe; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 51 | static struct pollfd pfd[1]; |
| 52 | static unsigned stamplog; |
| 53 | static smallint check = 1; |
| 54 | static smallint exitsoon; |
| 55 | static smallint set_pgrp; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 56 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 57 | static void fatal2_cannot(const char *m1, const char *m2) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 58 | { |
| 59 | bb_perror_msg_and_die("%s: fatal: cannot %s%s", svdir, m1, m2); |
| 60 | /* was exiting 100 */ |
| 61 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 62 | static void warn3x(const char *m1, const char *m2, const char *m3) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 63 | { |
| 64 | bb_error_msg("%s: warning: %s%s%s", svdir, m1, m2, m3); |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 65 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 66 | static void warn2_cannot(const char *m1, const char *m2) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 67 | { |
| 68 | warn3x("cannot ", m1, m2); |
| 69 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 70 | static void warnx(const char *m1) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 71 | { |
| 72 | warn3x(m1, "", ""); |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 73 | } |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 74 | |
| 75 | static void s_term(int sig_no) |
| 76 | { |
| 77 | exitsoon = 1; |
| 78 | } |
| 79 | static void s_hangup(int sig_no) |
| 80 | { |
| 81 | exitsoon = 2; |
| 82 | } |
| 83 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 84 | static void runsv(int no, const char *name) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 85 | { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 86 | pid_t pid; |
| 87 | char *prog[3]; |
| 88 | |
| 89 | prog[0] = (char*)"runsv"; |
| 90 | prog[1] = (char*)name; |
| 91 | prog[2] = NULL; |
| 92 | |
| 93 | pid = vfork(); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 94 | |
| 95 | if (pid == -1) { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 96 | warn2_cannot("vfork", ""); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | if (pid == 0) { |
| 100 | /* child */ |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 101 | if (set_pgrp) |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 102 | setsid(); |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 103 | bb_signals(0 |
| 104 | + (1 << SIGHUP) |
| 105 | + (1 << SIGTERM) |
| 106 | , SIG_DFL); |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 107 | execvp(prog[0], prog); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 108 | fatal2_cannot("start runsv ", name); |
| 109 | } |
| 110 | sv[no].pid = pid; |
| 111 | } |
| 112 | |
| 113 | static void runsvdir(void) |
| 114 | { |
| 115 | DIR *dir; |
| 116 | direntry *d; |
| 117 | int i; |
| 118 | struct stat s; |
| 119 | |
| 120 | dir = opendir("."); |
| 121 | if (!dir) { |
| 122 | warn2_cannot("open directory ", svdir); |
| 123 | return; |
| 124 | } |
| 125 | for (i = 0; i < svnum; i++) |
| 126 | sv[i].isgone = 1; |
| 127 | errno = 0; |
| 128 | while ((d = readdir(dir))) { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 129 | if (d->d_name[0] == '.') |
| 130 | continue; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 131 | if (stat(d->d_name, &s) == -1) { |
| 132 | warn2_cannot("stat ", d->d_name); |
| 133 | errno = 0; |
| 134 | continue; |
| 135 | } |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 136 | if (!S_ISDIR(s.st_mode)) |
| 137 | continue; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 138 | for (i = 0; i < svnum; i++) { |
| 139 | if ((sv[i].ino == s.st_ino) && (sv[i].dev == s.st_dev)) { |
| 140 | sv[i].isgone = 0; |
| 141 | if (!sv[i].pid) |
| 142 | runsv(i, d->d_name); |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | if (i == svnum) { |
| 147 | /* new service */ |
| 148 | struct service *svnew = realloc(sv, (i+1) * sizeof(*sv)); |
| 149 | if (!svnew) { |
| 150 | warn3x("cannot start runsv ", d->d_name, |
| 151 | " too many services"); |
| 152 | continue; |
| 153 | } |
| 154 | sv = svnew; |
| 155 | svnum++; |
| 156 | memset(&sv[i], 0, sizeof(sv[i])); |
| 157 | sv[i].ino = s.st_ino; |
| 158 | sv[i].dev = s.st_dev; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 159 | /*sv[i].pid = 0;*/ |
| 160 | /*sv[i].isgone = 0;*/ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 161 | runsv(i, d->d_name); |
| 162 | check = 1; |
| 163 | } |
| 164 | } |
| 165 | if (errno) { |
| 166 | warn2_cannot("read directory ", svdir); |
| 167 | closedir(dir); |
| 168 | check = 1; |
| 169 | return; |
| 170 | } |
| 171 | closedir(dir); |
| 172 | |
| 173 | /* SIGTERM removed runsv's */ |
| 174 | for (i = 0; i < svnum; i++) { |
| 175 | if (!sv[i].isgone) |
| 176 | continue; |
| 177 | if (sv[i].pid) |
| 178 | kill(sv[i].pid, SIGTERM); |
| 179 | sv[i] = sv[--svnum]; |
| 180 | check = 1; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static int setup_log(void) |
| 185 | { |
| 186 | rploglen = strlen(rplog); |
| 187 | if (rploglen < 7) { |
| 188 | warnx("log must have at least seven characters"); |
| 189 | return 0; |
| 190 | } |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 191 | if (piped_pair(logpipe)) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 192 | warnx("cannot create pipe for log"); |
| 193 | return -1; |
| 194 | } |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 195 | close_on_exec_on(logpipe.rd); |
| 196 | close_on_exec_on(logpipe.wr); |
| 197 | ndelay_on(logpipe.rd); |
| 198 | ndelay_on(logpipe.wr); |
| 199 | if (dup2(logpipe.wr, 2) == -1) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 200 | warnx("cannot set filedescriptor for log"); |
| 201 | return -1; |
| 202 | } |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 203 | pfd[0].fd = logpipe.rd; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 204 | pfd[0].events = POLLIN; |
| 205 | stamplog = monotonic_sec(); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 206 | return 1; |
| 207 | } |
| 208 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 209 | int runsvdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 210 | int runsvdir_main(int argc, char **argv) |
| 211 | { |
| 212 | struct stat s; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 213 | dev_t last_dev = last_dev; /* for gcc */ |
| 214 | ino_t last_ino = last_ino; /* for gcc */ |
| 215 | time_t last_mtime = 0; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 216 | int wstat; |
| 217 | int curdir; |
| 218 | int pid; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 219 | unsigned deadline; |
| 220 | unsigned now; |
| 221 | unsigned stampcheck; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 222 | char ch; |
| 223 | int i; |
| 224 | |
| 225 | argv++; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 226 | if (!*argv) |
| 227 | bb_show_usage(); |
| 228 | if (argv[0][0] == '-') { |
| 229 | switch (argv[0][1]) { |
| 230 | case 'P': set_pgrp = 1; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 231 | case '-': ++argv; |
| 232 | } |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 233 | if (!*argv) |
| 234 | bb_show_usage(); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 237 | bb_signals_recursive(1 << SIGTERM, s_term); |
| 238 | bb_signals_recursive(1 << SIGHUP, s_hangup); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 239 | svdir = *argv++; |
| 240 | if (argv && *argv) { |
| 241 | rplog = *argv; |
| 242 | if (setup_log() != 1) { |
| 243 | rplog = 0; |
| 244 | warnx("log service disabled"); |
| 245 | } |
| 246 | } |
| 247 | curdir = open_read("."); |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 248 | if (curdir == -1) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 249 | fatal2_cannot("open current directory", ""); |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 250 | close_on_exec_on(curdir); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 251 | |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 252 | stampcheck = monotonic_sec(); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 253 | |
| 254 | for (;;) { |
| 255 | /* collect children */ |
| 256 | for (;;) { |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 257 | pid = wait_any_nohang(&wstat); |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 258 | if (pid <= 0) |
| 259 | break; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 260 | for (i = 0; i < svnum; i++) { |
| 261 | if (pid == sv[i].pid) { |
| 262 | /* runsv has gone */ |
| 263 | sv[i].pid = 0; |
| 264 | check = 1; |
| 265 | break; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 270 | now = monotonic_sec(); |
| 271 | if ((int)(now - stampcheck) >= 0) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 272 | /* wait at least a second */ |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 273 | stampcheck = now + 1; |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 274 | |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 275 | if (stat(svdir, &s) != -1) { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 276 | if (check || s.st_mtime != last_mtime |
| 277 | || s.st_ino != last_ino || s.st_dev != last_dev |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 278 | ) { |
| 279 | /* svdir modified */ |
| 280 | if (chdir(svdir) != -1) { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 281 | last_mtime = s.st_mtime; |
| 282 | last_dev = s.st_dev; |
| 283 | last_ino = s.st_ino; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 284 | check = 0; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 285 | //if (now <= mtime) |
| 286 | // sleep(1); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 287 | runsvdir(); |
| 288 | while (fchdir(curdir) == -1) { |
| 289 | warn2_cannot("change directory, pausing", ""); |
| 290 | sleep(5); |
| 291 | } |
| 292 | } else |
| 293 | warn2_cannot("change directory to ", svdir); |
| 294 | } |
| 295 | } else |
| 296 | warn2_cannot("stat ", svdir); |
| 297 | } |
| 298 | |
| 299 | if (rplog) { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 300 | if ((int)(now - stamplog) >= 0) { |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 301 | write(logpipe.wr, ".", 1); |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 302 | stamplog = now + 900; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 305 | |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 306 | pfd[0].revents = 0; |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 307 | sig_block(SIGCHLD); |
Denis Vlasenko | 137fbe4 | 2007-09-26 12:18:07 +0000 | [diff] [blame] | 308 | deadline = (check ? 1 : 5); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 309 | if (rplog) |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 310 | poll(pfd, 1, deadline*1000); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 311 | else |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 312 | sleep(deadline); |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 313 | sig_unblock(SIGCHLD); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 314 | |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 315 | if (pfd[0].revents & POLLIN) { |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 316 | while (read(logpipe.rd, &ch, 1) > 0) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 317 | if (ch) { |
| 318 | for (i = 6; i < rploglen; i++) |
| 319 | rplog[i-1] = rplog[i]; |
| 320 | rplog[rploglen-1] = ch; |
| 321 | } |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 324 | |
| 325 | switch (exitsoon) { |
| 326 | case 1: |
| 327 | _exit(0); |
| 328 | case 2: |
| 329 | for (i = 0; i < svnum; i++) |
| 330 | if (sv[i].pid) |
| 331 | kill(sv[i].pid, SIGTERM); |
| 332 | _exit(111); |
| 333 | } |
| 334 | } |
| 335 | /* not reached */ |
| 336 | return 0; |
| 337 | } |