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