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 |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 61 | }; |
| 62 | #define G (*(struct globals*)&bb_common_bufsiz1) |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 63 | #define sv (G.sv ) |
| 64 | #define svdir (G.svdir ) |
| 65 | #define svnum (G.svnum ) |
| 66 | #define rplog (G.rplog ) |
| 67 | #define rploglen (G.rploglen ) |
| 68 | #define logpipe (G.logpipe ) |
| 69 | #define pfd (G.pfd ) |
| 70 | #define stamplog (G.stamplog ) |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 71 | #define INIT_G() do { \ |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 72 | } while (0) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 74 | static void fatal2_cannot(const char *m1, const char *m2) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 75 | { |
| 76 | bb_perror_msg_and_die("%s: fatal: cannot %s%s", svdir, m1, m2); |
| 77 | /* was exiting 100 */ |
| 78 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 79 | static void warn3x(const char *m1, const char *m2, const char *m3) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 80 | { |
| 81 | bb_error_msg("%s: warning: %s%s%s", svdir, m1, m2, m3); |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 82 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 83 | static void warn2_cannot(const char *m1, const char *m2) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 84 | { |
| 85 | warn3x("cannot ", m1, m2); |
| 86 | } |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 87 | #if ENABLE_FEATURE_RUNSVDIR_LOG |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 88 | static void warnx(const char *m1) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 89 | { |
| 90 | warn3x(m1, "", ""); |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 91 | } |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 92 | #endif |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 93 | |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 94 | /* inlining + vfork -> bigger code */ |
| 95 | static NOINLINE pid_t runsv(const char *name) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 96 | { |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 97 | pid_t pid; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 98 | |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 99 | /* If we got signaled, stop spawning children at once! */ |
| 100 | if (bb_got_signal) |
| 101 | return 0; |
| 102 | |
| 103 | pid = vfork(); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 104 | if (pid == -1) { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 105 | warn2_cannot("vfork", ""); |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 106 | return 0; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 107 | } |
| 108 | if (pid == 0) { |
| 109 | /* child */ |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 110 | if (option_mask32 & 1) /* -P option? */ |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 111 | setsid(); |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 112 | /* man execv: |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 113 | * "Signals set to be caught by the calling process image |
| 114 | * shall be set to the default action in the new process image." |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 115 | * Therefore, we do not need this: */ |
| 116 | #if 0 |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 117 | bb_signals(0 |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 118 | | (1 << SIGHUP) |
| 119 | | (1 << SIGTERM) |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 120 | , SIG_DFL); |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 121 | #endif |
Denis Vlasenko | f09f4e0 | 2009-02-26 12:29:59 +0000 | [diff] [blame] | 122 | execlp("runsv", "runsv", name, (char *) NULL); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 123 | fatal2_cannot("start runsv ", name); |
| 124 | } |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 125 | return pid; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 128 | /* gcc 4.3.0 does better with NOINLINE */ |
| 129 | static NOINLINE int do_rescan(void) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 130 | { |
| 131 | DIR *dir; |
| 132 | direntry *d; |
| 133 | int i; |
| 134 | struct stat s; |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 135 | int need_rescan = 0; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 136 | |
| 137 | dir = opendir("."); |
| 138 | if (!dir) { |
| 139 | warn2_cannot("open directory ", svdir); |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 140 | return 1; /* need to rescan again soon */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 141 | } |
| 142 | for (i = 0; i < svnum; i++) |
| 143 | sv[i].isgone = 1; |
Denis Vlasenko | 8f74094 | 2008-05-31 07:00:33 +0000 | [diff] [blame] | 144 | |
| 145 | while (1) { |
| 146 | errno = 0; |
| 147 | d = readdir(dir); |
| 148 | if (!d) |
| 149 | break; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 150 | if (d->d_name[0] == '.') |
| 151 | continue; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 152 | if (stat(d->d_name, &s) == -1) { |
| 153 | warn2_cannot("stat ", d->d_name); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 154 | continue; |
| 155 | } |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 156 | if (!S_ISDIR(s.st_mode)) |
| 157 | continue; |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 158 | /* Do we have this service listed already? */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 159 | for (i = 0; i < svnum; i++) { |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 160 | if ((sv[i].ino == s.st_ino) |
| 161 | #if CHECK_DEVNO_TOO |
| 162 | && (sv[i].dev == s.st_dev) |
| 163 | #endif |
| 164 | ) { |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 165 | if (sv[i].pid == 0) /* restart if it has died */ |
| 166 | goto run_ith_sv; |
| 167 | sv[i].isgone = 0; /* "we still see you" */ |
| 168 | goto next_dentry; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 171 | { /* Not found, make new service */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 172 | struct service *svnew = realloc(sv, (i+1) * sizeof(*sv)); |
| 173 | if (!svnew) { |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 174 | warn2_cannot("start runsv ", d->d_name); |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 175 | need_rescan = 1; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 176 | continue; |
| 177 | } |
| 178 | sv = svnew; |
| 179 | svnum++; |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 180 | #if CHECK_DEVNO_TOO |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 181 | sv[i].dev = s.st_dev; |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 182 | #endif |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 183 | sv[i].ino = s.st_ino; |
| 184 | run_ith_sv: |
| 185 | sv[i].pid = runsv(d->d_name); |
| 186 | sv[i].isgone = 0; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 187 | } |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 188 | next_dentry: ; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 189 | } |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 190 | i = errno; |
| 191 | closedir(dir); |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 192 | if (i) { /* readdir failed */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 193 | warn2_cannot("read directory ", svdir); |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 194 | return 1; /* need to rescan again soon */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 195 | } |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 196 | |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 197 | /* Send SIGTERM to runsv whose directories |
| 198 | * were no longer found (-> must have been removed) */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 199 | for (i = 0; i < svnum; i++) { |
| 200 | if (!sv[i].isgone) |
| 201 | continue; |
| 202 | if (sv[i].pid) |
| 203 | kill(sv[i].pid, SIGTERM); |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 204 | svnum--; |
| 205 | sv[i] = sv[svnum]; |
| 206 | i--; /* so that we don't skip new sv[i] (bug was here!) */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 207 | } |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 208 | return need_rescan; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 211 | int runsvdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 212 | int runsvdir_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 213 | { |
| 214 | struct stat s; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 215 | dev_t last_dev = last_dev; /* for gcc */ |
| 216 | ino_t last_ino = last_ino; /* for gcc */ |
| 217 | time_t last_mtime = 0; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 218 | int wstat; |
| 219 | int curdir; |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 220 | pid_t pid; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 221 | unsigned deadline; |
| 222 | unsigned now; |
| 223 | unsigned stampcheck; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 224 | int i; |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 225 | int need_rescan = 1; |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 226 | char *opt_s_argv[3]; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 227 | |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 228 | INIT_G(); |
| 229 | |
Denis Vlasenko | 08ea11a | 2008-09-11 19:51:11 +0000 | [diff] [blame] | 230 | opt_complementary = "-1"; |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 231 | opt_s_argv[0] = NULL; |
| 232 | opt_s_argv[2] = NULL; |
| 233 | getopt32(argv, "Ps:", &opt_s_argv[0]); |
Denis Vlasenko | 08ea11a | 2008-09-11 19:51:11 +0000 | [diff] [blame] | 234 | argv += optind; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 235 | |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 236 | bb_signals(0 |
| 237 | | (1 << SIGTERM) |
| 238 | | (1 << SIGHUP) |
| 239 | /* For busybox's init, SIGTERM == reboot, |
| 240 | * SIGUSR1 == halt |
| 241 | * SIGUSR2 == poweroff |
| 242 | * so we need to intercept SIGUSRn too. |
| 243 | * Note that we do not implement actual reboot |
| 244 | * (killall(TERM) + umount, etc), we just pause |
| 245 | * respawing and avoid exiting (-> making kernel oops). |
| 246 | * The user is responsible for the rest. */ |
| 247 | | (getpid() == 1 ? ((1 << SIGUSR1) | (1 << SIGUSR2)) : 0) |
| 248 | , record_signo); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 249 | svdir = *argv++; |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 250 | |
| 251 | #if ENABLE_FEATURE_RUNSVDIR_LOG |
| 252 | /* setup log */ |
| 253 | if (*argv) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 254 | rplog = *argv; |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 255 | rploglen = strlen(rplog); |
| 256 | if (rploglen < 7) { |
| 257 | warnx("log must have at least seven characters"); |
| 258 | } else if (piped_pair(logpipe)) { |
| 259 | warnx("cannot create pipe for log"); |
| 260 | } else { |
| 261 | close_on_exec_on(logpipe.rd); |
| 262 | close_on_exec_on(logpipe.wr); |
| 263 | ndelay_on(logpipe.rd); |
| 264 | ndelay_on(logpipe.wr); |
| 265 | if (dup2(logpipe.wr, 2) == -1) { |
| 266 | warnx("cannot set filedescriptor for log"); |
| 267 | } else { |
| 268 | pfd[0].fd = logpipe.rd; |
| 269 | pfd[0].events = POLLIN; |
| 270 | stamplog = monotonic_sec(); |
| 271 | goto run; |
| 272 | } |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 273 | } |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 274 | rplog = NULL; |
| 275 | warnx("log service disabled"); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 276 | } |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 277 | run: |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 278 | #endif |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 279 | curdir = open_read("."); |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 280 | if (curdir == -1) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 281 | fatal2_cannot("open current directory", ""); |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 282 | close_on_exec_on(curdir); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 283 | |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 284 | stampcheck = monotonic_sec(); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 285 | |
| 286 | for (;;) { |
| 287 | /* collect children */ |
| 288 | for (;;) { |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 289 | pid = wait_any_nohang(&wstat); |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 290 | if (pid <= 0) |
| 291 | break; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 292 | for (i = 0; i < svnum; i++) { |
| 293 | if (pid == sv[i].pid) { |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 294 | /* runsv has died */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 295 | sv[i].pid = 0; |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 296 | need_rescan = 1; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 301 | now = monotonic_sec(); |
| 302 | if ((int)(now - stampcheck) >= 0) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 303 | /* wait at least a second */ |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 304 | stampcheck = now + 1; |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 305 | |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 306 | if (stat(svdir, &s) != -1) { |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 307 | if (need_rescan || s.st_mtime != last_mtime |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 308 | || s.st_ino != last_ino || s.st_dev != last_dev |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 309 | ) { |
| 310 | /* svdir modified */ |
| 311 | if (chdir(svdir) != -1) { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 312 | last_mtime = s.st_mtime; |
| 313 | last_dev = s.st_dev; |
| 314 | last_ino = s.st_ino; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 315 | //if (now <= mtime) |
| 316 | // sleep(1); |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 317 | need_rescan = do_rescan(); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 318 | while (fchdir(curdir) == -1) { |
| 319 | warn2_cannot("change directory, pausing", ""); |
| 320 | sleep(5); |
| 321 | } |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 322 | } else { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 323 | warn2_cannot("change directory to ", svdir); |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 324 | } |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 325 | } |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 326 | } else { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 327 | warn2_cannot("stat ", svdir); |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 328 | } |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 331 | #if ENABLE_FEATURE_RUNSVDIR_LOG |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 332 | if (rplog) { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 333 | if ((int)(now - stamplog) >= 0) { |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 334 | write(logpipe.wr, ".", 1); |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 335 | stamplog = now + 900; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 338 | pfd[0].revents = 0; |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 339 | #endif |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 340 | deadline = (need_rescan ? 1 : 5); |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 341 | sig_block(SIGCHLD); |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 342 | #if ENABLE_FEATURE_RUNSVDIR_LOG |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 343 | if (rplog) |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 344 | poll(pfd, 1, deadline*1000); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 345 | else |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 346 | #endif |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 347 | sleep(deadline); |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 348 | sig_unblock(SIGCHLD); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 349 | |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 350 | #if ENABLE_FEATURE_RUNSVDIR_LOG |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 351 | if (pfd[0].revents & POLLIN) { |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 352 | char ch; |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 353 | while (read(logpipe.rd, &ch, 1) > 0) { |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 354 | if (ch < ' ') |
| 355 | ch = ' '; |
| 356 | for (i = 6; i < rploglen; i++) |
| 357 | rplog[i-1] = rplog[i]; |
| 358 | rplog[rploglen-1] = ch; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 361 | #endif |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 362 | if (!bb_got_signal) |
| 363 | continue; |
| 364 | |
| 365 | /* -s SCRIPT: useful if we are init. |
| 366 | * In this case typically script never returns, |
| 367 | * it halts/powers off/reboots the system. */ |
| 368 | if (opt_s_argv[0]) { |
| 369 | /* Single parameter: signal# */ |
| 370 | opt_s_argv[1] = utoa(bb_got_signal); |
| 371 | pid = spawn(opt_s_argv); |
| 372 | if (pid > 0) { |
Denis Vlasenko | 93b3820 | 2009-02-14 20:58:13 +0000 | [diff] [blame] | 373 | /* Remembering to wait for _any_ children, |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 374 | * not just pid */ |
| 375 | while (wait(NULL) != pid) |
| 376 | continue; |
| 377 | } |
| 378 | } |
| 379 | |
Denis Vlasenko | 3b35fbb | 2008-11-11 09:33:24 +0000 | [diff] [blame] | 380 | if (bb_got_signal == SIGHUP) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 381 | for (i = 0; i < svnum; i++) |
| 382 | if (sv[i].pid) |
| 383 | kill(sv[i].pid, SIGTERM); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 384 | } |
Denis Vlasenko | 3b35fbb | 2008-11-11 09:33:24 +0000 | [diff] [blame] | 385 | /* SIGHUP or SIGTERM (or SIGUSRn if we are init) */ |
| 386 | /* Exit unless we are init */ |
| 387 | if (getpid() != 1) |
| 388 | return (SIGHUP == bb_got_signal) ? 111 : EXIT_SUCCESS; |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 389 | |
Denis Vlasenko | 3b35fbb | 2008-11-11 09:33:24 +0000 | [diff] [blame] | 390 | /* init continues to monitor services forever */ |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 391 | bb_got_signal = 0; |
| 392 | } /* for (;;) */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 393 | } |