3
0

runsvdir.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. struct service {
  32. dev_t dev;
  33. ino_t ino;
  34. pid_t pid;
  35. smallint isgone;
  36. };
  37. struct globals {
  38. struct service *sv;
  39. char *svdir;
  40. char *rplog;
  41. int svnum;
  42. int rploglen;
  43. struct fd_pair logpipe;
  44. struct pollfd pfd[1];
  45. unsigned stamplog;
  46. smallint check; /* = 1; */
  47. smallint exitsoon;
  48. smallint set_pgrp;
  49. };
  50. #define G (*(struct globals*)&bb_common_bufsiz1)
  51. #define sv (G.sv )
  52. #define svdir (G.svdir )
  53. #define rplog (G.rplog )
  54. #define svnum (G.svnum )
  55. #define rploglen (G.rploglen )
  56. #define logpipe (G.logpipe )
  57. #define pfd (G.pfd )
  58. #define stamplog (G.stamplog )
  59. #define check (G.check )
  60. #define exitsoon (G.exitsoon )
  61. #define set_pgrp (G.set_pgrp )
  62. #define INIT_G() do { \
  63. check = 1; \
  64. } while (0)
  65. static void fatal2_cannot(const char *m1, const char *m2)
  66. {
  67. bb_perror_msg_and_die("%s: fatal: cannot %s%s", svdir, m1, m2);
  68. /* was exiting 100 */
  69. }
  70. static void warn3x(const char *m1, const char *m2, const char *m3)
  71. {
  72. bb_error_msg("%s: warning: %s%s%s", svdir, m1, m2, m3);
  73. }
  74. static void warn2_cannot(const char *m1, const char *m2)
  75. {
  76. warn3x("cannot ", m1, m2);
  77. }
  78. static void warnx(const char *m1)
  79. {
  80. warn3x(m1, "", "");
  81. }
  82. static void s_term(int sig_no ATTRIBUTE_UNUSED)
  83. {
  84. exitsoon = 1;
  85. }
  86. static void s_hangup(int sig_no ATTRIBUTE_UNUSED)
  87. {
  88. exitsoon = 2;
  89. }
  90. static void runsv(int no, const char *name)
  91. {
  92. pid_t pid;
  93. char *prog[3];
  94. prog[0] = (char*)"runsv";
  95. prog[1] = (char*)name;
  96. prog[2] = NULL;
  97. pid = vfork();
  98. if (pid == -1) {
  99. warn2_cannot("vfork", "");
  100. return;
  101. }
  102. if (pid == 0) {
  103. /* child */
  104. if (set_pgrp)
  105. setsid();
  106. bb_signals(0
  107. + (1 << SIGHUP)
  108. + (1 << SIGTERM)
  109. , SIG_DFL);
  110. execvp(prog[0], prog);
  111. fatal2_cannot("start runsv ", name);
  112. }
  113. sv[no].pid = pid;
  114. }
  115. static void runsvdir(void)
  116. {
  117. DIR *dir;
  118. direntry *d;
  119. int i;
  120. struct stat s;
  121. dir = opendir(".");
  122. if (!dir) {
  123. warn2_cannot("open directory ", svdir);
  124. return;
  125. }
  126. for (i = 0; i < svnum; i++)
  127. sv[i].isgone = 1;
  128. while (1) {
  129. errno = 0;
  130. d = readdir(dir);
  131. if (!d)
  132. break;
  133. if (d->d_name[0] == '.')
  134. continue;
  135. if (stat(d->d_name, &s) == -1) {
  136. warn2_cannot("stat ", d->d_name);
  137. errno = 0;
  138. continue;
  139. }
  140. if (!S_ISDIR(s.st_mode))
  141. continue;
  142. for (i = 0; i < svnum; i++) {
  143. if ((sv[i].ino == s.st_ino) && (sv[i].dev == s.st_dev)) {
  144. sv[i].isgone = 0;
  145. if (!sv[i].pid)
  146. runsv(i, d->d_name);
  147. break;
  148. }
  149. }
  150. if (i == svnum) {
  151. /* new service */
  152. struct service *svnew = realloc(sv, (i+1) * sizeof(*sv));
  153. if (!svnew) {
  154. warn3x("cannot start runsv ", d->d_name,
  155. " too many services");
  156. continue;
  157. }
  158. sv = svnew;
  159. svnum++;
  160. memset(&sv[i], 0, sizeof(sv[i]));
  161. sv[i].ino = s.st_ino;
  162. sv[i].dev = s.st_dev;
  163. /*sv[i].pid = 0;*/
  164. /*sv[i].isgone = 0;*/
  165. runsv(i, d->d_name);
  166. check = 1;
  167. }
  168. }
  169. if (errno) {
  170. warn2_cannot("read directory ", svdir);
  171. closedir(dir);
  172. check = 1;
  173. return;
  174. }
  175. closedir(dir);
  176. /* SIGTERM removed runsv's */
  177. for (i = 0; i < svnum; i++) {
  178. if (!sv[i].isgone)
  179. continue;
  180. if (sv[i].pid)
  181. kill(sv[i].pid, SIGTERM);
  182. sv[i] = sv[--svnum];
  183. /* BUG? we deleted sv[i] by copying over sv[last], but we will not check this newly-copied one! */
  184. check = 1;
  185. }
  186. }
  187. static int setup_log(void)
  188. {
  189. rploglen = strlen(rplog);
  190. if (rploglen < 7) {
  191. warnx("log must have at least seven characters");
  192. return 0;
  193. }
  194. if (piped_pair(logpipe)) {
  195. warnx("cannot create pipe for log");
  196. return -1;
  197. }
  198. close_on_exec_on(logpipe.rd);
  199. close_on_exec_on(logpipe.wr);
  200. ndelay_on(logpipe.rd);
  201. ndelay_on(logpipe.wr);
  202. if (dup2(logpipe.wr, 2) == -1) {
  203. warnx("cannot set filedescriptor for log");
  204. return -1;
  205. }
  206. pfd[0].fd = logpipe.rd;
  207. pfd[0].events = POLLIN;
  208. stamplog = monotonic_sec();
  209. return 1;
  210. }
  211. int runsvdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  212. int runsvdir_main(int argc ATTRIBUTE_UNUSED, char **argv)
  213. {
  214. struct stat s;
  215. dev_t last_dev = last_dev; /* for gcc */
  216. ino_t last_ino = last_ino; /* for gcc */
  217. time_t last_mtime = 0;
  218. int wstat;
  219. int curdir;
  220. int pid;
  221. unsigned deadline;
  222. unsigned now;
  223. unsigned stampcheck;
  224. char ch;
  225. int i;
  226. INIT_G();
  227. argv++;
  228. if (!*argv)
  229. bb_show_usage();
  230. if (argv[0][0] == '-') {
  231. switch (argv[0][1]) {
  232. case 'P': set_pgrp = 1;
  233. case '-': ++argv;
  234. }
  235. if (!*argv)
  236. bb_show_usage();
  237. }
  238. bb_signals_recursive(1 << SIGTERM, s_term);
  239. bb_signals_recursive(1 << SIGHUP, s_hangup);
  240. svdir = *argv++;
  241. if (argv && *argv) {
  242. rplog = *argv;
  243. if (setup_log() != 1) {
  244. rplog = 0;
  245. warnx("log service disabled");
  246. }
  247. }
  248. curdir = open_read(".");
  249. if (curdir == -1)
  250. fatal2_cannot("open current directory", "");
  251. close_on_exec_on(curdir);
  252. stampcheck = monotonic_sec();
  253. for (;;) {
  254. /* collect children */
  255. for (;;) {
  256. pid = wait_any_nohang(&wstat);
  257. if (pid <= 0)
  258. break;
  259. for (i = 0; i < svnum; i++) {
  260. if (pid == sv[i].pid) {
  261. /* runsv has gone */
  262. sv[i].pid = 0;
  263. check = 1;
  264. break;
  265. }
  266. }
  267. }
  268. now = monotonic_sec();
  269. if ((int)(now - stampcheck) >= 0) {
  270. /* wait at least a second */
  271. stampcheck = now + 1;
  272. if (stat(svdir, &s) != -1) {
  273. if (check || s.st_mtime != last_mtime
  274. || s.st_ino != last_ino || s.st_dev != last_dev
  275. ) {
  276. /* svdir modified */
  277. if (chdir(svdir) != -1) {
  278. last_mtime = s.st_mtime;
  279. last_dev = s.st_dev;
  280. last_ino = s.st_ino;
  281. check = 0;
  282. //if (now <= mtime)
  283. // sleep(1);
  284. runsvdir();
  285. while (fchdir(curdir) == -1) {
  286. warn2_cannot("change directory, pausing", "");
  287. sleep(5);
  288. }
  289. } else
  290. warn2_cannot("change directory to ", svdir);
  291. }
  292. } else
  293. warn2_cannot("stat ", svdir);
  294. }
  295. if (rplog) {
  296. if ((int)(now - stamplog) >= 0) {
  297. write(logpipe.wr, ".", 1);
  298. stamplog = now + 900;
  299. }
  300. }
  301. pfd[0].revents = 0;
  302. sig_block(SIGCHLD);
  303. deadline = (check ? 1 : 5);
  304. if (rplog)
  305. poll(pfd, 1, deadline*1000);
  306. else
  307. sleep(deadline);
  308. sig_unblock(SIGCHLD);
  309. if (pfd[0].revents & POLLIN) {
  310. while (read(logpipe.rd, &ch, 1) > 0) {
  311. if (ch) {
  312. for (i = 6; i < rploglen; i++)
  313. rplog[i-1] = rplog[i];
  314. rplog[rploglen-1] = ch;
  315. }
  316. }
  317. }
  318. switch (exitsoon) {
  319. case 1:
  320. _exit(EXIT_SUCCESS);
  321. case 2:
  322. for (i = 0; i < svnum; i++)
  323. if (sv[i].pid)
  324. kill(sv[i].pid, SIGTERM);
  325. _exit(111);
  326. }
  327. }
  328. /* not reached */
  329. return 0;
  330. }