3
0

runsvdir.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
  2. /* TODO: depends on runit_lib.c - review and reduce/eliminate */
  3. #include <sys/poll.h>
  4. #include <sys/file.h>
  5. #include "busybox.h"
  6. #include "runit_lib.h"
  7. #define MAXSERVICES 1000
  8. static char *svdir;
  9. static unsigned long dev;
  10. static unsigned long ino;
  11. static struct service {
  12. unsigned long dev;
  13. unsigned long ino;
  14. int pid;
  15. int isgone;
  16. } *sv;
  17. static int svnum;
  18. static int check = 1;
  19. static char *rplog;
  20. static int rploglen;
  21. static int logpipe[2];
  22. static iopause_fd io[1];
  23. static struct taia stamplog;
  24. static int exitsoon;
  25. static int pgrp;
  26. #define usage() bb_show_usage()
  27. static void fatal2_cannot(char *m1, char *m2)
  28. {
  29. bb_perror_msg_and_die("%s: fatal: cannot %s%s", svdir, m1, m2);
  30. /* was exiting 100 */
  31. }
  32. static void warn3x(char *m1, char *m2, char *m3)
  33. {
  34. bb_error_msg("%s: warning: %s%s%s", svdir, m1, m2, m3);
  35. }
  36. static void warn2_cannot(char *m1, char *m2)
  37. {
  38. warn3x("cannot ", m1, m2);
  39. }
  40. static void warnx(char *m1)
  41. {
  42. warn3x(m1, "", "");
  43. }
  44. static void s_term(int sig_no)
  45. {
  46. exitsoon = 1;
  47. }
  48. static void s_hangup(int sig_no)
  49. {
  50. exitsoon = 2;
  51. }
  52. static void runsv(int no, char *name)
  53. {
  54. int pid = fork();
  55. if (pid == -1) {
  56. warn2_cannot("fork for ", name);
  57. return;
  58. }
  59. if (pid == 0) {
  60. /* child */
  61. char *prog[3];
  62. prog[0] = "runsv";
  63. prog[1] = name;
  64. prog[2] = 0;
  65. sig_uncatch(sig_hangup);
  66. sig_uncatch(sig_term);
  67. if (pgrp) setsid();
  68. execvp(prog[0], prog);
  69. //pathexec_run(*prog, prog, (char* const*)environ);
  70. fatal2_cannot("start runsv ", name);
  71. }
  72. sv[no].pid = pid;
  73. }
  74. static void runsvdir(void)
  75. {
  76. DIR *dir;
  77. direntry *d;
  78. int i;
  79. struct stat s;
  80. dir = opendir(".");
  81. if (!dir) {
  82. warn2_cannot("open directory ", svdir);
  83. return;
  84. }
  85. for (i = 0; i < svnum; i++)
  86. sv[i].isgone = 1;
  87. errno = 0;
  88. while ((d = readdir(dir))) {
  89. if (d->d_name[0] == '.') continue;
  90. if (stat(d->d_name, &s) == -1) {
  91. warn2_cannot("stat ", d->d_name);
  92. errno = 0;
  93. continue;
  94. }
  95. if (!S_ISDIR(s.st_mode)) continue;
  96. for (i = 0; i < svnum; i++) {
  97. if ((sv[i].ino == s.st_ino) && (sv[i].dev == s.st_dev)) {
  98. sv[i].isgone = 0;
  99. if (!sv[i].pid)
  100. runsv(i, d->d_name);
  101. break;
  102. }
  103. }
  104. if (i == svnum) {
  105. /* new service */
  106. struct service *svnew = realloc(sv, (i+1) * sizeof(*sv));
  107. if (!svnew) {
  108. warn3x("cannot start runsv ", d->d_name,
  109. " too many services");
  110. continue;
  111. }
  112. sv = svnew;
  113. svnum++;
  114. memset(&sv[i], 0, sizeof(sv[i]));
  115. sv[i].ino = s.st_ino;
  116. sv[i].dev = s.st_dev;
  117. //sv[i].pid = 0;
  118. //sv[i].isgone = 0;
  119. runsv(i, d->d_name);
  120. check = 1;
  121. }
  122. }
  123. if (errno) {
  124. warn2_cannot("read directory ", svdir);
  125. closedir(dir);
  126. check = 1;
  127. return;
  128. }
  129. closedir(dir);
  130. /* SIGTERM removed runsv's */
  131. for (i = 0; i < svnum; i++) {
  132. if (!sv[i].isgone)
  133. continue;
  134. if (sv[i].pid)
  135. kill(sv[i].pid, SIGTERM);
  136. sv[i] = sv[--svnum];
  137. check = 1;
  138. }
  139. }
  140. static int setup_log(void)
  141. {
  142. rploglen = strlen(rplog);
  143. if (rploglen < 7) {
  144. warnx("log must have at least seven characters");
  145. return 0;
  146. }
  147. if (pipe(logpipe) == -1) {
  148. warnx("cannot create pipe for log");
  149. return -1;
  150. }
  151. coe(logpipe[1]);
  152. coe(logpipe[0]);
  153. ndelay_on(logpipe[0]);
  154. ndelay_on(logpipe[1]);
  155. if (fd_copy(2, logpipe[1]) == -1) {
  156. warnx("cannot set filedescriptor for log");
  157. return -1;
  158. }
  159. io[0].fd = logpipe[0];
  160. io[0].events = IOPAUSE_READ;
  161. taia_now(&stamplog);
  162. return 1;
  163. }
  164. int runsvdir_main(int argc, char **argv)
  165. {
  166. struct stat s;
  167. time_t mtime = 0;
  168. int wstat;
  169. int curdir;
  170. int pid;
  171. struct taia deadline;
  172. struct taia now;
  173. struct taia stampcheck;
  174. char ch;
  175. int i;
  176. argv++;
  177. if (!argv || !*argv) usage();
  178. if (**argv == '-') {
  179. switch (*(*argv + 1)) {
  180. case 'P': pgrp = 1;
  181. case '-': ++argv;
  182. }
  183. if (!argv || !*argv) usage();
  184. }
  185. sig_catch(sig_term, s_term);
  186. sig_catch(sig_hangup, s_hangup);
  187. svdir = *argv++;
  188. if (argv && *argv) {
  189. rplog = *argv;
  190. if (setup_log() != 1) {
  191. rplog = 0;
  192. warnx("log service disabled");
  193. }
  194. }
  195. curdir = open_read(".");
  196. if (curdir == -1)
  197. fatal2_cannot("open current directory", "");
  198. coe(curdir);
  199. taia_now(&stampcheck);
  200. for (;;) {
  201. /* collect children */
  202. for (;;) {
  203. pid = wait_nohang(&wstat);
  204. if (pid <= 0) break;
  205. for (i = 0; i < svnum; i++) {
  206. if (pid == sv[i].pid) {
  207. /* runsv has gone */
  208. sv[i].pid = 0;
  209. check = 1;
  210. break;
  211. }
  212. }
  213. }
  214. taia_now(&now);
  215. if (now.sec.x < (stampcheck.sec.x - 3)) {
  216. /* time warp */
  217. warnx("time warp: resetting time stamp");
  218. taia_now(&stampcheck);
  219. taia_now(&now);
  220. if (rplog) taia_now(&stamplog);
  221. }
  222. if (taia_less(&now, &stampcheck) == 0) {
  223. /* wait at least a second */
  224. taia_uint(&deadline, 1);
  225. taia_add(&stampcheck, &now, &deadline);
  226. if (stat(svdir, &s) != -1) {
  227. if (check || s.st_mtime != mtime
  228. || s.st_ino != ino || s.st_dev != dev
  229. ) {
  230. /* svdir modified */
  231. if (chdir(svdir) != -1) {
  232. mtime = s.st_mtime;
  233. dev = s.st_dev;
  234. ino = s.st_ino;
  235. check = 0;
  236. if (now.sec.x <= (4611686018427387914ULL + (uint64_t)mtime))
  237. sleep(1);
  238. runsvdir();
  239. while (fchdir(curdir) == -1) {
  240. warn2_cannot("change directory, pausing", "");
  241. sleep(5);
  242. }
  243. } else
  244. warn2_cannot("change directory to ", svdir);
  245. }
  246. } else
  247. warn2_cannot("stat ", svdir);
  248. }
  249. if (rplog) {
  250. if (taia_less(&now, &stamplog) == 0) {
  251. write(logpipe[1], ".", 1);
  252. taia_uint(&deadline, 900);
  253. taia_add(&stamplog, &now, &deadline);
  254. }
  255. }
  256. taia_uint(&deadline, check ? 1 : 5);
  257. taia_add(&deadline, &now, &deadline);
  258. sig_block(sig_child);
  259. if (rplog)
  260. iopause(io, 1, &deadline, &now);
  261. else
  262. iopause(0, 0, &deadline, &now);
  263. sig_unblock(sig_child);
  264. if (rplog && (io[0].revents | IOPAUSE_READ))
  265. while (read(logpipe[0], &ch, 1) > 0)
  266. if (ch) {
  267. for (i = 6; i < rploglen; i++)
  268. rplog[i-1] = rplog[i];
  269. rplog[rploglen-1] = ch;
  270. }
  271. switch (exitsoon) {
  272. case 1:
  273. _exit(0);
  274. case 2:
  275. for (i = 0; i < svnum; i++)
  276. if (sv[i].pid)
  277. kill(sv[i].pid, SIGTERM);
  278. _exit(111);
  279. }
  280. }
  281. /* not reached */
  282. return 0;
  283. }