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