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 | |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED |
Denis Vlasenko | 8a16405 | 2007-03-12 23:34:52 +0000 | [diff] [blame] | 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 |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 31 | //config: bool "runsvdir (6.3 kb)" |
Denys Vlasenko | 0863e1a | 2015-10-19 00:41:28 +0200 | [diff] [blame] | 32 | //config: default y |
| 33 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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. |
Denys Vlasenko | 0863e1a | 2015-10-19 00:41:28 +0200 | [diff] [blame] | 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 |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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. |
Denys Vlasenko | 0863e1a | 2015-10-19 00:41:28 +0200 | [diff] [blame] | 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++) { |
Denys Vlasenko | ef15970 | 2016-09-01 11:16:22 +0200 | [diff] [blame] | 184 | if (sv[i].ino == s.st_ino |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 185 | #if CHECK_DEVNO_TOO |
Denys Vlasenko | ef15970 | 2016-09-01 11:16:22 +0200 | [diff] [blame] | 186 | && sv[i].dev == s.st_dev |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 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 | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 251 | opt_s_argv[0] = NULL; |
| 252 | opt_s_argv[2] = NULL; |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 253 | getopt32(argv, "^" "Ps:" "\0" "-1", &opt_s_argv[0]); |
Denis Vlasenko | 08ea11a | 2008-09-11 19:51:11 +0000 | [diff] [blame] | 254 | argv += optind; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 255 | |
Denys Vlasenko | 7db2a7c | 2015-03-02 17:39:13 +0100 | [diff] [blame] | 256 | i_am_init = (getpid() == 1); |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 257 | bb_signals(0 |
| 258 | | (1 << SIGTERM) |
| 259 | | (1 << SIGHUP) |
| 260 | /* For busybox's init, SIGTERM == reboot, |
Denys Vlasenko | 7db2a7c | 2015-03-02 17:39:13 +0100 | [diff] [blame] | 261 | * SIGUSR1 == halt, |
| 262 | * SIGUSR2 == poweroff, |
| 263 | * Ctlr-ALt-Del sends SIGINT to init, |
| 264 | * so we need to intercept SIGUSRn and SIGINT too. |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 265 | * Note that we do not implement actual reboot |
| 266 | * (killall(TERM) + umount, etc), we just pause |
| 267 | * respawing and avoid exiting (-> making kernel oops). |
Denys Vlasenko | 7db2a7c | 2015-03-02 17:39:13 +0100 | [diff] [blame] | 268 | * The user is responsible for the rest. |
| 269 | */ |
| 270 | | (i_am_init ? ((1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGINT)) : 0) |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 271 | , record_signo); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 272 | svdir = *argv++; |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 273 | |
| 274 | #if ENABLE_FEATURE_RUNSVDIR_LOG |
| 275 | /* setup log */ |
| 276 | if (*argv) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 277 | rplog = *argv; |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 278 | if (strlen(rplog) < 7) { |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 279 | warnx("log must have at least seven characters"); |
| 280 | } else if (piped_pair(logpipe)) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 281 | warnx("can't create pipe for log"); |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 282 | } else { |
| 283 | close_on_exec_on(logpipe.rd); |
| 284 | close_on_exec_on(logpipe.wr); |
| 285 | ndelay_on(logpipe.rd); |
| 286 | ndelay_on(logpipe.wr); |
| 287 | if (dup2(logpipe.wr, 2) == -1) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 288 | warnx("can't set filedescriptor for log"); |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 289 | } else { |
| 290 | pfd[0].fd = logpipe.rd; |
| 291 | pfd[0].events = POLLIN; |
| 292 | stamplog = monotonic_sec(); |
| 293 | goto run; |
| 294 | } |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 295 | } |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 296 | rplog = NULL; |
| 297 | warnx("log service disabled"); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 298 | } |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 299 | run: |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 300 | #endif |
Denys Vlasenko | 05e8605 | 2010-10-13 12:53:27 +0200 | [diff] [blame] | 301 | curdir = open(".", O_RDONLY|O_NDELAY); |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 302 | if (curdir == -1) |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 303 | fatal2_cannot("open current directory", ""); |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 304 | close_on_exec_on(curdir); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 305 | |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 306 | stampcheck = monotonic_sec(); |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 307 | need_rescan = 1; |
| 308 | last_mtime = 0; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 309 | |
| 310 | for (;;) { |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 311 | unsigned now; |
| 312 | unsigned sig; |
| 313 | |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 314 | /* collect children */ |
| 315 | for (;;) { |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 316 | pid_t pid = wait_any_nohang(NULL); |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 317 | if (pid <= 0) |
| 318 | break; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 319 | for (i = 0; i < svnum; i++) { |
| 320 | if (pid == sv[i].pid) { |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 321 | /* runsv has died */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 322 | sv[i].pid = 0; |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 323 | need_rescan = 1; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 328 | now = monotonic_sec(); |
| 329 | if ((int)(now - stampcheck) >= 0) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 330 | /* wait at least a second */ |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 331 | stampcheck = now + 1; |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 332 | |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 333 | if (stat(svdir, &s) != -1) { |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 334 | if (need_rescan || s.st_mtime != last_mtime |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 335 | || s.st_ino != last_ino || s.st_dev != last_dev |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 336 | ) { |
| 337 | /* svdir modified */ |
| 338 | if (chdir(svdir) != -1) { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 339 | last_mtime = s.st_mtime; |
| 340 | last_dev = s.st_dev; |
| 341 | last_ino = s.st_ino; |
Eric Lammerts | 31905f9 | 2010-12-06 01:26:26 +0100 | [diff] [blame] | 342 | /* if the svdir changed this very second, wait until the |
| 343 | * next second, because we won't be able to detect more |
| 344 | * changes within this second */ |
| 345 | while (time(NULL) == last_mtime) |
| 346 | usleep(100000); |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 347 | need_rescan = do_rescan(); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 348 | while (fchdir(curdir) == -1) { |
| 349 | warn2_cannot("change directory, pausing", ""); |
| 350 | sleep(5); |
| 351 | } |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 352 | } else { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 353 | warn2_cannot("change directory to ", svdir); |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 354 | } |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 355 | } |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 356 | } else { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 357 | warn2_cannot("stat ", svdir); |
Denis Vlasenko | 6a2c2cf | 2008-10-29 12:04:45 +0000 | [diff] [blame] | 358 | } |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 361 | #if ENABLE_FEATURE_RUNSVDIR_LOG |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 362 | if (rplog) { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 363 | if ((int)(now - stamplog) >= 0) { |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 364 | write(logpipe.wr, ".", 1); |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 365 | stamplog = now + 900; |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 368 | pfd[0].revents = 0; |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 369 | #endif |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 370 | { |
| 371 | unsigned deadline = (need_rescan ? 1 : 5); |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 372 | #if ENABLE_FEATURE_RUNSVDIR_LOG |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 373 | if (rplog) |
| 374 | poll(pfd, 1, deadline*1000); |
| 375 | else |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 376 | #endif |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 377 | sleep(deadline); |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 378 | } |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 379 | |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 380 | #if ENABLE_FEATURE_RUNSVDIR_LOG |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 381 | if (pfd[0].revents & POLLIN) { |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 382 | char ch; |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 383 | while (read(logpipe.rd, &ch, 1) > 0) { |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 384 | if (ch < ' ') |
| 385 | ch = ' '; |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 386 | for (i = 6; rplog[i] != '\0'; i++) |
Denis Vlasenko | 9657596 | 2008-10-29 03:42:44 +0000 | [diff] [blame] | 387 | rplog[i-1] = rplog[i]; |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 388 | rplog[i-1] = ch; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
Denis Vlasenko | 923a79a | 2008-09-22 21:34:24 +0000 | [diff] [blame] | 391 | #endif |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 392 | sig = bb_got_signal; |
| 393 | if (!sig) |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 394 | continue; |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 395 | bb_got_signal = 0; |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 396 | |
| 397 | /* -s SCRIPT: useful if we are init. |
| 398 | * In this case typically script never returns, |
| 399 | * it halts/powers off/reboots the system. */ |
| 400 | if (opt_s_argv[0]) { |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 401 | pid_t pid; |
| 402 | |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 403 | /* Single parameter: signal# */ |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 404 | opt_s_argv[1] = utoa(sig); |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 405 | pid = spawn(opt_s_argv); |
| 406 | if (pid > 0) { |
Denis Vlasenko | 93b3820 | 2009-02-14 20:58:13 +0000 | [diff] [blame] | 407 | /* Remembering to wait for _any_ children, |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 408 | * not just pid */ |
| 409 | while (wait(NULL) != pid) |
| 410 | continue; |
| 411 | } |
| 412 | } |
| 413 | |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 414 | if (sig == SIGHUP) { |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 415 | for (i = 0; i < svnum; i++) |
| 416 | if (sv[i].pid) |
| 417 | kill(sv[i].pid, SIGTERM); |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 418 | } |
Denis Vlasenko | 3b35fbb | 2008-11-11 09:33:24 +0000 | [diff] [blame] | 419 | /* SIGHUP or SIGTERM (or SIGUSRn if we are init) */ |
| 420 | /* Exit unless we are init */ |
Denys Vlasenko | 7db2a7c | 2015-03-02 17:39:13 +0100 | [diff] [blame] | 421 | if (!i_am_init) |
Denys Vlasenko | fa535f3 | 2015-03-02 17:37:31 +0100 | [diff] [blame] | 422 | return (SIGHUP == sig) ? 111 : EXIT_SUCCESS; |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 423 | |
Denis Vlasenko | 3b35fbb | 2008-11-11 09:33:24 +0000 | [diff] [blame] | 424 | /* init continues to monitor services forever */ |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 425 | } /* for (;;) */ |
Denis Vlasenko | 04c6386 | 2006-11-17 18:58:49 +0000 | [diff] [blame] | 426 | } |