runsvdir.c 10.0 KB

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