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