3
0

runsvdir.c 9.9 KB

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