3
0

runsvdir.c 7.4 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. static char *svdir;
  32. static unsigned long dev;
  33. static unsigned long ino;
  34. static struct service {
  35. unsigned long dev;
  36. unsigned long ino;
  37. int pid;
  38. int isgone;
  39. } *sv;
  40. static int svnum;
  41. static int check = 1;
  42. static char *rplog;
  43. static int rploglen;
  44. static int logpipe[2];
  45. static iopause_fd io[1];
  46. static struct taia stamplog;
  47. static int exitsoon;
  48. static int pgrp;
  49. #define usage() bb_show_usage()
  50. static void fatal2_cannot(const char *m1, const char *m2)
  51. {
  52. bb_perror_msg_and_die("%s: fatal: cannot %s%s", svdir, m1, m2);
  53. /* was exiting 100 */
  54. }
  55. static void warn3x(const char *m1, const char *m2, const char *m3)
  56. {
  57. bb_error_msg("%s: warning: %s%s%s", svdir, m1, m2, m3);
  58. }
  59. static void warn2_cannot(const char *m1, const char *m2)
  60. {
  61. warn3x("cannot ", m1, m2);
  62. }
  63. static void warnx(const char *m1)
  64. {
  65. warn3x(m1, "", "");
  66. }
  67. static void s_term(int sig_no)
  68. {
  69. exitsoon = 1;
  70. }
  71. static void s_hangup(int sig_no)
  72. {
  73. exitsoon = 2;
  74. }
  75. static void runsv(int no, const char *name)
  76. {
  77. int pid = fork();
  78. if (pid == -1) {
  79. warn2_cannot("fork for ", name);
  80. return;
  81. }
  82. if (pid == 0) {
  83. /* child */
  84. char *prog[3];
  85. prog[0] = (char*)"runsv";
  86. prog[1] = (char*)name;
  87. prog[2] = NULL;
  88. if (pgrp)
  89. setsid();
  90. signal(SIGHUP, SIG_DFL);
  91. signal(SIGTERM, SIG_DFL);
  92. BB_EXECVP(prog[0], prog);
  93. //pathexec_run(*prog, prog, (char* const*)environ);
  94. fatal2_cannot("start runsv ", name);
  95. }
  96. sv[no].pid = pid;
  97. }
  98. static void runsvdir(void)
  99. {
  100. DIR *dir;
  101. direntry *d;
  102. int i;
  103. struct stat s;
  104. dir = opendir(".");
  105. if (!dir) {
  106. warn2_cannot("open directory ", svdir);
  107. return;
  108. }
  109. for (i = 0; i < svnum; i++)
  110. sv[i].isgone = 1;
  111. errno = 0;
  112. while ((d = readdir(dir))) {
  113. if (d->d_name[0] == '.') continue;
  114. if (stat(d->d_name, &s) == -1) {
  115. warn2_cannot("stat ", d->d_name);
  116. errno = 0;
  117. continue;
  118. }
  119. if (!S_ISDIR(s.st_mode)) 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. coe(logpipe[1]);
  176. coe(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. io[0].fd = logpipe[0];
  184. io[0].events = IOPAUSE_READ;
  185. taia_now(&stamplog);
  186. return 1;
  187. }
  188. int runsvdir_main(int argc, char **argv);
  189. int runsvdir_main(int argc, char **argv)
  190. {
  191. struct stat s;
  192. time_t mtime = 0;
  193. int wstat;
  194. int curdir;
  195. int pid;
  196. struct taia deadline;
  197. struct taia now;
  198. struct taia stampcheck;
  199. char ch;
  200. int i;
  201. argv++;
  202. if (!argv || !*argv) usage();
  203. if (**argv == '-') {
  204. switch (*(*argv + 1)) {
  205. case 'P': pgrp = 1;
  206. case '-': ++argv;
  207. }
  208. if (!argv || !*argv) usage();
  209. }
  210. sig_catch(SIGTERM, s_term);
  211. sig_catch(SIGHUP, s_hangup);
  212. svdir = *argv++;
  213. if (argv && *argv) {
  214. rplog = *argv;
  215. if (setup_log() != 1) {
  216. rplog = 0;
  217. warnx("log service disabled");
  218. }
  219. }
  220. curdir = open_read(".");
  221. if (curdir == -1)
  222. fatal2_cannot("open current directory", "");
  223. coe(curdir);
  224. taia_now(&stampcheck);
  225. for (;;) {
  226. /* collect children */
  227. for (;;) {
  228. pid = wait_nohang(&wstat);
  229. if (pid <= 0) break;
  230. for (i = 0; i < svnum; i++) {
  231. if (pid == sv[i].pid) {
  232. /* runsv has gone */
  233. sv[i].pid = 0;
  234. check = 1;
  235. break;
  236. }
  237. }
  238. }
  239. taia_now(&now);
  240. if (now.sec.x < (stampcheck.sec.x - 3)) {
  241. /* time warp */
  242. warnx("time warp: resetting time stamp");
  243. taia_now(&stampcheck);
  244. taia_now(&now);
  245. if (rplog) taia_now(&stamplog);
  246. }
  247. if (taia_less(&now, &stampcheck) == 0) {
  248. /* wait at least a second */
  249. taia_uint(&deadline, 1);
  250. taia_add(&stampcheck, &now, &deadline);
  251. if (stat(svdir, &s) != -1) {
  252. if (check || s.st_mtime != mtime
  253. || s.st_ino != ino || s.st_dev != dev
  254. ) {
  255. /* svdir modified */
  256. if (chdir(svdir) != -1) {
  257. mtime = s.st_mtime;
  258. dev = s.st_dev;
  259. ino = s.st_ino;
  260. check = 0;
  261. if (now.sec.x <= (4611686018427387914ULL + (uint64_t)mtime))
  262. sleep(1);
  263. runsvdir();
  264. while (fchdir(curdir) == -1) {
  265. warn2_cannot("change directory, pausing", "");
  266. sleep(5);
  267. }
  268. } else
  269. warn2_cannot("change directory to ", svdir);
  270. }
  271. } else
  272. warn2_cannot("stat ", svdir);
  273. }
  274. if (rplog) {
  275. if (taia_less(&now, &stamplog) == 0) {
  276. write(logpipe[1], ".", 1);
  277. taia_uint(&deadline, 900);
  278. taia_add(&stamplog, &now, &deadline);
  279. }
  280. }
  281. taia_uint(&deadline, check ? 1 : 5);
  282. taia_add(&deadline, &now, &deadline);
  283. sig_block(SIGCHLD);
  284. if (rplog)
  285. iopause(io, 1, &deadline, &now);
  286. else
  287. iopause(0, 0, &deadline, &now);
  288. sig_unblock(SIGCHLD);
  289. if (rplog && (io[0].revents | IOPAUSE_READ))
  290. while (read(logpipe[0], &ch, 1) > 0)
  291. if (ch) {
  292. for (i = 6; i < rploglen; i++)
  293. rplog[i-1] = rplog[i];
  294. rplog[rploglen-1] = ch;
  295. }
  296. switch (exitsoon) {
  297. case 1:
  298. _exit(0);
  299. case 2:
  300. for (i = 0; i < svnum; i++)
  301. if (sv[i].pid)
  302. kill(sv[i].pid, SIGTERM);
  303. _exit(111);
  304. }
  305. }
  306. /* not reached */
  307. return 0;
  308. }