runsvdir.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. Copyright (c) 2001-2006, Gerrit Pape
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. 3. The name of the author may not be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  14. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  16. EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  19. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  20. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  21. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  22. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
  25. /* TODO: depends on runit_lib.c - review and reduce/eliminate */
  26. //usage:#define runsvdir_trivial_usage
  27. //usage: "[-P] [-s SCRIPT] DIR"
  28. //usage:#define runsvdir_full_usage "\n\n"
  29. //usage: "Start a runsv process for each subdirectory. If it exits, restart it.\n"
  30. //usage: "\n -P Put each runsv in a new session"
  31. //usage: "\n -s SCRIPT Run SCRIPT <signo> after signal is processed"
  32. #include <sys/poll.h>
  33. #include <sys/file.h>
  34. #include "libbb.h"
  35. #include "runit_lib.h"
  36. #define MAXSERVICES 1000
  37. /* Should be not needed - all dirs are on same FS, right? */
  38. #define CHECK_DEVNO_TOO 0
  39. struct service {
  40. #if CHECK_DEVNO_TOO
  41. dev_t dev;
  42. #endif
  43. ino_t ino;
  44. pid_t pid;
  45. smallint isgone;
  46. };
  47. struct globals {
  48. struct service *sv;
  49. char *svdir;
  50. int svnum;
  51. #if ENABLE_FEATURE_RUNSVDIR_LOG
  52. char *rplog;
  53. int rploglen;
  54. struct fd_pair logpipe;
  55. struct pollfd pfd[1];
  56. unsigned stamplog;
  57. #endif
  58. } FIX_ALIASING;
  59. #define G (*(struct globals*)&bb_common_bufsiz1)
  60. #define sv (G.sv )
  61. #define svdir (G.svdir )
  62. #define svnum (G.svnum )
  63. #define rplog (G.rplog )
  64. #define rploglen (G.rploglen )
  65. #define logpipe (G.logpipe )
  66. #define pfd (G.pfd )
  67. #define stamplog (G.stamplog )
  68. #define INIT_G() do { \
  69. } while (0)
  70. static void fatal2_cannot(const char *m1, const char *m2)
  71. {
  72. bb_perror_msg_and_die("%s: fatal: can't %s%s", svdir, m1, m2);
  73. /* was exiting 100 */
  74. }
  75. static void warn3x(const char *m1, const char *m2, const char *m3)
  76. {
  77. bb_error_msg("%s: warning: %s%s%s", svdir, m1, m2, m3);
  78. }
  79. static void warn2_cannot(const char *m1, const char *m2)
  80. {
  81. warn3x("can't ", m1, m2);
  82. }
  83. #if ENABLE_FEATURE_RUNSVDIR_LOG
  84. static void warnx(const char *m1)
  85. {
  86. warn3x(m1, "", "");
  87. }
  88. #endif
  89. /* inlining + vfork -> bigger code */
  90. static NOINLINE pid_t runsv(const char *name)
  91. {
  92. pid_t pid;
  93. /* If we got signaled, stop spawning children at once! */
  94. if (bb_got_signal)
  95. return 0;
  96. pid = vfork();
  97. if (pid == -1) {
  98. warn2_cannot("vfork", "");
  99. return 0;
  100. }
  101. if (pid == 0) {
  102. /* child */
  103. if (option_mask32 & 1) /* -P option? */
  104. setsid();
  105. /* man execv:
  106. * "Signals set to be caught by the calling process image
  107. * shall be set to the default action in the new process image."
  108. * Therefore, we do not need this: */
  109. #if 0
  110. bb_signals(0
  111. | (1 << SIGHUP)
  112. | (1 << SIGTERM)
  113. , SIG_DFL);
  114. #endif
  115. execlp("runsv", "runsv", name, (char *) NULL);
  116. fatal2_cannot("start runsv ", name);
  117. }
  118. return pid;
  119. }
  120. /* gcc 4.3.0 does better with NOINLINE */
  121. static NOINLINE int do_rescan(void)
  122. {
  123. DIR *dir;
  124. struct dirent *d;
  125. int i;
  126. struct stat s;
  127. int need_rescan = 0;
  128. dir = opendir(".");
  129. if (!dir) {
  130. warn2_cannot("open directory ", svdir);
  131. return 1; /* need to rescan again soon */
  132. }
  133. for (i = 0; i < svnum; i++)
  134. sv[i].isgone = 1;
  135. while (1) {
  136. errno = 0;
  137. d = readdir(dir);
  138. if (!d)
  139. break;
  140. if (d->d_name[0] == '.')
  141. continue;
  142. if (stat(d->d_name, &s) == -1) {
  143. warn2_cannot("stat ", d->d_name);
  144. continue;
  145. }
  146. if (!S_ISDIR(s.st_mode))
  147. continue;
  148. /* Do we have this service listed already? */
  149. for (i = 0; i < svnum; i++) {
  150. if ((sv[i].ino == s.st_ino)
  151. #if CHECK_DEVNO_TOO
  152. && (sv[i].dev == s.st_dev)
  153. #endif
  154. ) {
  155. if (sv[i].pid == 0) /* restart if it has died */
  156. goto run_ith_sv;
  157. sv[i].isgone = 0; /* "we still see you" */
  158. goto next_dentry;
  159. }
  160. }
  161. { /* Not found, make new service */
  162. struct service *svnew = realloc(sv, (i+1) * sizeof(*sv));
  163. if (!svnew) {
  164. warn2_cannot("start runsv ", d->d_name);
  165. need_rescan = 1;
  166. continue;
  167. }
  168. sv = svnew;
  169. svnum++;
  170. #if CHECK_DEVNO_TOO
  171. sv[i].dev = s.st_dev;
  172. #endif
  173. sv[i].ino = s.st_ino;
  174. run_ith_sv:
  175. sv[i].pid = runsv(d->d_name);
  176. sv[i].isgone = 0;
  177. }
  178. next_dentry: ;
  179. }
  180. i = errno;
  181. closedir(dir);
  182. if (i) { /* readdir failed */
  183. warn2_cannot("read directory ", svdir);
  184. return 1; /* need to rescan again soon */
  185. }
  186. /* Send SIGTERM to runsv whose directories
  187. * were no longer found (-> must have been removed) */
  188. for (i = 0; i < svnum; i++) {
  189. if (!sv[i].isgone)
  190. continue;
  191. if (sv[i].pid)
  192. kill(sv[i].pid, SIGTERM);
  193. svnum--;
  194. sv[i] = sv[svnum];
  195. i--; /* so that we don't skip new sv[i] (bug was here!) */
  196. }
  197. return need_rescan;
  198. }
  199. int runsvdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  200. int runsvdir_main(int argc UNUSED_PARAM, char **argv)
  201. {
  202. struct stat s;
  203. dev_t last_dev = last_dev; /* for gcc */
  204. ino_t last_ino = last_ino; /* for gcc */
  205. time_t last_mtime = 0;
  206. int wstat;
  207. int curdir;
  208. pid_t pid;
  209. unsigned deadline;
  210. unsigned now;
  211. unsigned stampcheck;
  212. int i;
  213. int need_rescan = 1;
  214. char *opt_s_argv[3];
  215. INIT_G();
  216. opt_complementary = "-1";
  217. opt_s_argv[0] = NULL;
  218. opt_s_argv[2] = NULL;
  219. getopt32(argv, "Ps:", &opt_s_argv[0]);
  220. argv += optind;
  221. bb_signals(0
  222. | (1 << SIGTERM)
  223. | (1 << SIGHUP)
  224. /* For busybox's init, SIGTERM == reboot,
  225. * SIGUSR1 == halt
  226. * SIGUSR2 == poweroff
  227. * so we need to intercept SIGUSRn too.
  228. * Note that we do not implement actual reboot
  229. * (killall(TERM) + umount, etc), we just pause
  230. * respawing and avoid exiting (-> making kernel oops).
  231. * The user is responsible for the rest. */
  232. | (getpid() == 1 ? ((1 << SIGUSR1) | (1 << SIGUSR2)) : 0)
  233. , record_signo);
  234. svdir = *argv++;
  235. #if ENABLE_FEATURE_RUNSVDIR_LOG
  236. /* setup log */
  237. if (*argv) {
  238. rplog = *argv;
  239. rploglen = strlen(rplog);
  240. if (rploglen < 7) {
  241. warnx("log must have at least seven characters");
  242. } else if (piped_pair(logpipe)) {
  243. warnx("can't create pipe for log");
  244. } else {
  245. close_on_exec_on(logpipe.rd);
  246. close_on_exec_on(logpipe.wr);
  247. ndelay_on(logpipe.rd);
  248. ndelay_on(logpipe.wr);
  249. if (dup2(logpipe.wr, 2) == -1) {
  250. warnx("can't set filedescriptor for log");
  251. } else {
  252. pfd[0].fd = logpipe.rd;
  253. pfd[0].events = POLLIN;
  254. stamplog = monotonic_sec();
  255. goto run;
  256. }
  257. }
  258. rplog = NULL;
  259. warnx("log service disabled");
  260. }
  261. run:
  262. #endif
  263. curdir = open(".", O_RDONLY|O_NDELAY);
  264. if (curdir == -1)
  265. fatal2_cannot("open current directory", "");
  266. close_on_exec_on(curdir);
  267. stampcheck = monotonic_sec();
  268. for (;;) {
  269. /* collect children */
  270. for (;;) {
  271. pid = wait_any_nohang(&wstat);
  272. if (pid <= 0)
  273. break;
  274. for (i = 0; i < svnum; i++) {
  275. if (pid == sv[i].pid) {
  276. /* runsv has died */
  277. sv[i].pid = 0;
  278. need_rescan = 1;
  279. }
  280. }
  281. }
  282. now = monotonic_sec();
  283. if ((int)(now - stampcheck) >= 0) {
  284. /* wait at least a second */
  285. stampcheck = now + 1;
  286. if (stat(svdir, &s) != -1) {
  287. if (need_rescan || s.st_mtime != last_mtime
  288. || s.st_ino != last_ino || s.st_dev != last_dev
  289. ) {
  290. /* svdir modified */
  291. if (chdir(svdir) != -1) {
  292. last_mtime = s.st_mtime;
  293. last_dev = s.st_dev;
  294. last_ino = s.st_ino;
  295. /* if the svdir changed this very second, wait until the
  296. * next second, because we won't be able to detect more
  297. * changes within this second */
  298. while (time(NULL) == last_mtime)
  299. usleep(100000);
  300. need_rescan = do_rescan();
  301. while (fchdir(curdir) == -1) {
  302. warn2_cannot("change directory, pausing", "");
  303. sleep(5);
  304. }
  305. } else {
  306. warn2_cannot("change directory to ", svdir);
  307. }
  308. }
  309. } else {
  310. warn2_cannot("stat ", svdir);
  311. }
  312. }
  313. #if ENABLE_FEATURE_RUNSVDIR_LOG
  314. if (rplog) {
  315. if ((int)(now - stamplog) >= 0) {
  316. write(logpipe.wr, ".", 1);
  317. stamplog = now + 900;
  318. }
  319. }
  320. pfd[0].revents = 0;
  321. #endif
  322. deadline = (need_rescan ? 1 : 5);
  323. sig_block(SIGCHLD);
  324. #if ENABLE_FEATURE_RUNSVDIR_LOG
  325. if (rplog)
  326. poll(pfd, 1, deadline*1000);
  327. else
  328. #endif
  329. sleep(deadline);
  330. sig_unblock(SIGCHLD);
  331. #if ENABLE_FEATURE_RUNSVDIR_LOG
  332. if (pfd[0].revents & POLLIN) {
  333. char ch;
  334. while (read(logpipe.rd, &ch, 1) > 0) {
  335. if (ch < ' ')
  336. ch = ' ';
  337. for (i = 6; i < rploglen; i++)
  338. rplog[i-1] = rplog[i];
  339. rplog[rploglen-1] = ch;
  340. }
  341. }
  342. #endif
  343. if (!bb_got_signal)
  344. continue;
  345. /* -s SCRIPT: useful if we are init.
  346. * In this case typically script never returns,
  347. * it halts/powers off/reboots the system. */
  348. if (opt_s_argv[0]) {
  349. /* Single parameter: signal# */
  350. opt_s_argv[1] = utoa(bb_got_signal);
  351. pid = spawn(opt_s_argv);
  352. if (pid > 0) {
  353. /* Remembering to wait for _any_ children,
  354. * not just pid */
  355. while (wait(NULL) != pid)
  356. continue;
  357. }
  358. }
  359. if (bb_got_signal == SIGHUP) {
  360. for (i = 0; i < svnum; i++)
  361. if (sv[i].pid)
  362. kill(sv[i].pid, SIGTERM);
  363. }
  364. /* SIGHUP or SIGTERM (or SIGUSRn if we are init) */
  365. /* Exit unless we are init */
  366. if (getpid() != 1)
  367. return (SIGHUP == bb_got_signal) ? 111 : EXIT_SUCCESS;
  368. /* init continues to monitor services forever */
  369. bb_got_signal = 0;
  370. } /* for (;;) */
  371. }