runsvdir.c 7.2 KB

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