runsvdir.c 9.5 KB

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