3
0

runsvdir.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. errno = 0;
  129. while ((d = readdir(dir))) {
  130. if (d->d_name[0] == '.')
  131. continue;
  132. if (stat(d->d_name, &s) == -1) {
  133. warn2_cannot("stat ", d->d_name);
  134. errno = 0;
  135. continue;
  136. }
  137. if (!S_ISDIR(s.st_mode))
  138. continue;
  139. for (i = 0; i < svnum; i++) {
  140. if ((sv[i].ino == s.st_ino) && (sv[i].dev == s.st_dev)) {
  141. sv[i].isgone = 0;
  142. if (!sv[i].pid)
  143. runsv(i, d->d_name);
  144. break;
  145. }
  146. }
  147. if (i == svnum) {
  148. /* new service */
  149. struct service *svnew = realloc(sv, (i+1) * sizeof(*sv));
  150. if (!svnew) {
  151. warn3x("cannot start runsv ", d->d_name,
  152. " too many services");
  153. continue;
  154. }
  155. sv = svnew;
  156. svnum++;
  157. memset(&sv[i], 0, sizeof(sv[i]));
  158. sv[i].ino = s.st_ino;
  159. sv[i].dev = s.st_dev;
  160. /*sv[i].pid = 0;*/
  161. /*sv[i].isgone = 0;*/
  162. runsv(i, d->d_name);
  163. check = 1;
  164. }
  165. }
  166. if (errno) {
  167. warn2_cannot("read directory ", svdir);
  168. closedir(dir);
  169. check = 1;
  170. return;
  171. }
  172. closedir(dir);
  173. /* SIGTERM removed runsv's */
  174. for (i = 0; i < svnum; i++) {
  175. if (!sv[i].isgone)
  176. continue;
  177. if (sv[i].pid)
  178. kill(sv[i].pid, SIGTERM);
  179. sv[i] = sv[--svnum];
  180. check = 1;
  181. }
  182. }
  183. static int setup_log(void)
  184. {
  185. rploglen = strlen(rplog);
  186. if (rploglen < 7) {
  187. warnx("log must have at least seven characters");
  188. return 0;
  189. }
  190. if (piped_pair(logpipe)) {
  191. warnx("cannot create pipe for log");
  192. return -1;
  193. }
  194. close_on_exec_on(logpipe.rd);
  195. close_on_exec_on(logpipe.wr);
  196. ndelay_on(logpipe.rd);
  197. ndelay_on(logpipe.wr);
  198. if (dup2(logpipe.wr, 2) == -1) {
  199. warnx("cannot set filedescriptor for log");
  200. return -1;
  201. }
  202. pfd[0].fd = logpipe.rd;
  203. pfd[0].events = POLLIN;
  204. stamplog = monotonic_sec();
  205. return 1;
  206. }
  207. int runsvdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  208. int runsvdir_main(int argc ATTRIBUTE_UNUSED, char **argv)
  209. {
  210. struct stat s;
  211. dev_t last_dev = last_dev; /* for gcc */
  212. ino_t last_ino = last_ino; /* for gcc */
  213. time_t last_mtime = 0;
  214. int wstat;
  215. int curdir;
  216. int pid;
  217. unsigned deadline;
  218. unsigned now;
  219. unsigned stampcheck;
  220. char ch;
  221. int i;
  222. INIT_G();
  223. argv++;
  224. if (!*argv)
  225. bb_show_usage();
  226. if (argv[0][0] == '-') {
  227. switch (argv[0][1]) {
  228. case 'P': set_pgrp = 1;
  229. case '-': ++argv;
  230. }
  231. if (!*argv)
  232. bb_show_usage();
  233. }
  234. bb_signals_recursive(1 << SIGTERM, s_term);
  235. bb_signals_recursive(1 << SIGHUP, s_hangup);
  236. svdir = *argv++;
  237. if (argv && *argv) {
  238. rplog = *argv;
  239. if (setup_log() != 1) {
  240. rplog = 0;
  241. warnx("log service disabled");
  242. }
  243. }
  244. curdir = open_read(".");
  245. if (curdir == -1)
  246. fatal2_cannot("open current directory", "");
  247. close_on_exec_on(curdir);
  248. stampcheck = monotonic_sec();
  249. for (;;) {
  250. /* collect children */
  251. for (;;) {
  252. pid = wait_any_nohang(&wstat);
  253. if (pid <= 0)
  254. break;
  255. for (i = 0; i < svnum; i++) {
  256. if (pid == sv[i].pid) {
  257. /* runsv has gone */
  258. sv[i].pid = 0;
  259. check = 1;
  260. break;
  261. }
  262. }
  263. }
  264. now = monotonic_sec();
  265. if ((int)(now - stampcheck) >= 0) {
  266. /* wait at least a second */
  267. stampcheck = now + 1;
  268. if (stat(svdir, &s) != -1) {
  269. if (check || s.st_mtime != last_mtime
  270. || s.st_ino != last_ino || s.st_dev != last_dev
  271. ) {
  272. /* svdir modified */
  273. if (chdir(svdir) != -1) {
  274. last_mtime = s.st_mtime;
  275. last_dev = s.st_dev;
  276. last_ino = s.st_ino;
  277. check = 0;
  278. //if (now <= mtime)
  279. // sleep(1);
  280. runsvdir();
  281. while (fchdir(curdir) == -1) {
  282. warn2_cannot("change directory, pausing", "");
  283. sleep(5);
  284. }
  285. } else
  286. warn2_cannot("change directory to ", svdir);
  287. }
  288. } else
  289. warn2_cannot("stat ", svdir);
  290. }
  291. if (rplog) {
  292. if ((int)(now - stamplog) >= 0) {
  293. write(logpipe.wr, ".", 1);
  294. stamplog = now + 900;
  295. }
  296. }
  297. pfd[0].revents = 0;
  298. sig_block(SIGCHLD);
  299. deadline = (check ? 1 : 5);
  300. if (rplog)
  301. poll(pfd, 1, deadline*1000);
  302. else
  303. sleep(deadline);
  304. sig_unblock(SIGCHLD);
  305. if (pfd[0].revents & POLLIN) {
  306. while (read(logpipe.rd, &ch, 1) > 0) {
  307. if (ch) {
  308. for (i = 6; i < rploglen; i++)
  309. rplog[i-1] = rplog[i];
  310. rplog[rploglen-1] = ch;
  311. }
  312. }
  313. }
  314. switch (exitsoon) {
  315. case 1:
  316. _exit(0);
  317. case 2:
  318. for (i = 0; i < svnum; i++)
  319. if (sv[i].pid)
  320. kill(sv[i].pid, SIGTERM);
  321. _exit(111);
  322. }
  323. }
  324. /* not reached */
  325. return 0;
  326. }