3
0

runsvdir.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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/file.h>
  33. #include "libbb.h"
  34. #include "runit_lib.h"
  35. #define MAXSERVICES 1000
  36. /* Should be not needed - all dirs are on same FS, right? */
  37. #define CHECK_DEVNO_TOO 0
  38. struct service {
  39. #if CHECK_DEVNO_TOO
  40. dev_t dev;
  41. #endif
  42. ino_t ino;
  43. pid_t pid;
  44. smallint isgone;
  45. };
  46. struct globals {
  47. struct service *sv;
  48. char *svdir;
  49. int svnum;
  50. #if ENABLE_FEATURE_RUNSVDIR_LOG
  51. char *rplog;
  52. struct fd_pair logpipe;
  53. struct pollfd pfd[1];
  54. unsigned stamplog;
  55. #endif
  56. } FIX_ALIASING;
  57. #define G (*(struct globals*)&bb_common_bufsiz1)
  58. #define sv (G.sv )
  59. #define svdir (G.svdir )
  60. #define svnum (G.svnum )
  61. #define rplog (G.rplog )
  62. #define logpipe (G.logpipe )
  63. #define pfd (G.pfd )
  64. #define stamplog (G.stamplog )
  65. #define INIT_G() do { } while (0)
  66. static void fatal2_cannot(const char *m1, const char *m2)
  67. {
  68. bb_perror_msg_and_die("%s: fatal: can't %s%s", svdir, m1, m2);
  69. /* was exiting 100 */
  70. }
  71. static void warn3x(const char *m1, const char *m2, const char *m3)
  72. {
  73. bb_error_msg("%s: warning: %s%s%s", svdir, m1, m2, m3);
  74. }
  75. static void warn2_cannot(const char *m1, const char *m2)
  76. {
  77. warn3x("can't ", m1, m2);
  78. }
  79. #if ENABLE_FEATURE_RUNSVDIR_LOG
  80. static void warnx(const char *m1)
  81. {
  82. warn3x(m1, "", "");
  83. }
  84. #endif
  85. /* inlining + vfork -> bigger code */
  86. static NOINLINE pid_t runsv(const char *name)
  87. {
  88. pid_t pid;
  89. /* If we got signaled, stop spawning children at once! */
  90. if (bb_got_signal)
  91. return 0;
  92. pid = vfork();
  93. if (pid == -1) {
  94. warn2_cannot("vfork", "");
  95. return 0;
  96. }
  97. if (pid == 0) {
  98. /* child */
  99. if (option_mask32 & 1) /* -P option? */
  100. setsid();
  101. /* man execv:
  102. * "Signals set to be caught by the calling process image
  103. * shall be set to the default action in the new process image."
  104. * Therefore, we do not need this: */
  105. #if 0
  106. bb_signals(0
  107. | (1 << SIGHUP)
  108. | (1 << SIGTERM)
  109. , SIG_DFL);
  110. #endif
  111. execlp("runsv", "runsv", name, (char *) NULL);
  112. fatal2_cannot("start runsv ", name);
  113. }
  114. return pid;
  115. }
  116. /* gcc 4.3.0 does better with NOINLINE */
  117. static NOINLINE int do_rescan(void)
  118. {
  119. DIR *dir;
  120. struct dirent *d;
  121. int i;
  122. struct stat s;
  123. int need_rescan = 0;
  124. dir = opendir(".");
  125. if (!dir) {
  126. warn2_cannot("open directory ", svdir);
  127. return 1; /* need to rescan again soon */
  128. }
  129. for (i = 0; i < svnum; i++)
  130. sv[i].isgone = 1;
  131. while (1) {
  132. errno = 0;
  133. d = readdir(dir);
  134. if (!d)
  135. break;
  136. if (d->d_name[0] == '.')
  137. continue;
  138. if (stat(d->d_name, &s) == -1) {
  139. warn2_cannot("stat ", d->d_name);
  140. continue;
  141. }
  142. if (!S_ISDIR(s.st_mode))
  143. continue;
  144. /* Do we have this service listed already? */
  145. for (i = 0; i < svnum; i++) {
  146. if ((sv[i].ino == s.st_ino)
  147. #if CHECK_DEVNO_TOO
  148. && (sv[i].dev == s.st_dev)
  149. #endif
  150. ) {
  151. if (sv[i].pid == 0) /* restart if it has died */
  152. goto run_ith_sv;
  153. sv[i].isgone = 0; /* "we still see you" */
  154. goto next_dentry;
  155. }
  156. }
  157. { /* Not found, make new service */
  158. struct service *svnew = realloc(sv, (i+1) * sizeof(*sv));
  159. if (!svnew) {
  160. warn2_cannot("start runsv ", d->d_name);
  161. need_rescan = 1;
  162. continue;
  163. }
  164. sv = svnew;
  165. svnum++;
  166. #if CHECK_DEVNO_TOO
  167. sv[i].dev = s.st_dev;
  168. #endif
  169. sv[i].ino = s.st_ino;
  170. run_ith_sv:
  171. sv[i].pid = runsv(d->d_name);
  172. sv[i].isgone = 0;
  173. }
  174. next_dentry: ;
  175. }
  176. i = errno;
  177. closedir(dir);
  178. if (i) { /* readdir failed */
  179. warn2_cannot("read directory ", svdir);
  180. return 1; /* need to rescan again soon */
  181. }
  182. /* Send SIGTERM to runsv whose directories
  183. * were no longer found (-> must have been removed) */
  184. for (i = 0; i < svnum; i++) {
  185. if (!sv[i].isgone)
  186. continue;
  187. if (sv[i].pid)
  188. kill(sv[i].pid, SIGTERM);
  189. svnum--;
  190. sv[i] = sv[svnum];
  191. i--; /* so that we don't skip new sv[i] (bug was here!) */
  192. }
  193. return need_rescan;
  194. }
  195. int runsvdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  196. int runsvdir_main(int argc UNUSED_PARAM, char **argv)
  197. {
  198. struct stat s;
  199. dev_t last_dev = last_dev; /* for gcc */
  200. ino_t last_ino = last_ino; /* for gcc */
  201. time_t last_mtime;
  202. int curdir;
  203. unsigned stampcheck;
  204. int i;
  205. int need_rescan;
  206. bool i_am_init;
  207. char *opt_s_argv[3];
  208. INIT_G();
  209. opt_complementary = "-1";
  210. opt_s_argv[0] = NULL;
  211. opt_s_argv[2] = NULL;
  212. getopt32(argv, "Ps:", &opt_s_argv[0]);
  213. argv += optind;
  214. i_am_init = (getpid() == 1);
  215. bb_signals(0
  216. | (1 << SIGTERM)
  217. | (1 << SIGHUP)
  218. /* For busybox's init, SIGTERM == reboot,
  219. * SIGUSR1 == halt,
  220. * SIGUSR2 == poweroff,
  221. * Ctlr-ALt-Del sends SIGINT to init,
  222. * so we need to intercept SIGUSRn and SIGINT too.
  223. * Note that we do not implement actual reboot
  224. * (killall(TERM) + umount, etc), we just pause
  225. * respawing and avoid exiting (-> making kernel oops).
  226. * The user is responsible for the rest.
  227. */
  228. | (i_am_init ? ((1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGINT)) : 0)
  229. , record_signo);
  230. svdir = *argv++;
  231. #if ENABLE_FEATURE_RUNSVDIR_LOG
  232. /* setup log */
  233. if (*argv) {
  234. rplog = *argv;
  235. if (strlen(rplog) < 7) {
  236. warnx("log must have at least seven characters");
  237. } else if (piped_pair(logpipe)) {
  238. warnx("can't create pipe for log");
  239. } else {
  240. close_on_exec_on(logpipe.rd);
  241. close_on_exec_on(logpipe.wr);
  242. ndelay_on(logpipe.rd);
  243. ndelay_on(logpipe.wr);
  244. if (dup2(logpipe.wr, 2) == -1) {
  245. warnx("can't set filedescriptor for log");
  246. } else {
  247. pfd[0].fd = logpipe.rd;
  248. pfd[0].events = POLLIN;
  249. stamplog = monotonic_sec();
  250. goto run;
  251. }
  252. }
  253. rplog = NULL;
  254. warnx("log service disabled");
  255. }
  256. run:
  257. #endif
  258. curdir = open(".", O_RDONLY|O_NDELAY);
  259. if (curdir == -1)
  260. fatal2_cannot("open current directory", "");
  261. close_on_exec_on(curdir);
  262. stampcheck = monotonic_sec();
  263. need_rescan = 1;
  264. last_mtime = 0;
  265. for (;;) {
  266. unsigned now;
  267. unsigned sig;
  268. /* collect children */
  269. for (;;) {
  270. pid_t pid = wait_any_nohang(NULL);
  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. {
  322. unsigned deadline = (need_rescan ? 1 : 5);
  323. #if ENABLE_FEATURE_RUNSVDIR_LOG
  324. if (rplog)
  325. poll(pfd, 1, deadline*1000);
  326. else
  327. #endif
  328. sleep(deadline);
  329. }
  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; rplog[i] != '\0'; i++)
  337. rplog[i-1] = rplog[i];
  338. rplog[i-1] = ch;
  339. }
  340. }
  341. #endif
  342. sig = bb_got_signal;
  343. if (!sig)
  344. continue;
  345. bb_got_signal = 0;
  346. /* -s SCRIPT: useful if we are init.
  347. * In this case typically script never returns,
  348. * it halts/powers off/reboots the system. */
  349. if (opt_s_argv[0]) {
  350. pid_t pid;
  351. /* Single parameter: signal# */
  352. opt_s_argv[1] = utoa(sig);
  353. pid = spawn(opt_s_argv);
  354. if (pid > 0) {
  355. /* Remembering to wait for _any_ children,
  356. * not just pid */
  357. while (wait(NULL) != pid)
  358. continue;
  359. }
  360. }
  361. if (sig == SIGHUP) {
  362. for (i = 0; i < svnum; i++)
  363. if (sv[i].pid)
  364. kill(sv[i].pid, SIGTERM);
  365. }
  366. /* SIGHUP or SIGTERM (or SIGUSRn if we are init) */
  367. /* Exit unless we are init */
  368. if (!i_am_init)
  369. return (SIGHUP == sig) ? 111 : EXIT_SUCCESS;
  370. /* init continues to monitor services forever */
  371. } /* for (;;) */
  372. }