3
0

sv.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. static char *action;
  8. static char *acts;
  9. static char *varservice = "/var/service/";
  10. static char **service;
  11. static char **servicex;
  12. static unsigned services;
  13. static unsigned rc = 0;
  14. static unsigned verbose = 0;
  15. static unsigned long waitsec = 7;
  16. static unsigned kll = 0;
  17. static struct taia tstart, tnow, tdiff;
  18. static struct tai tstatus;
  19. static int (*act)(char*) = 0;
  20. static int (*cbk)(char*) = 0;
  21. static int curdir, fd, r;
  22. static char svstatus[20];
  23. #define usage() bb_show_usage()
  24. static void fatal_cannot(char *m1)
  25. {
  26. bb_perror_msg("fatal: cannot %s", m1);
  27. _exit(151);
  28. }
  29. static void out(char *p, char *m1)
  30. {
  31. printf("%s%s: %s", p, *service, m1);
  32. if (errno) {
  33. printf(": %s", strerror(errno));
  34. }
  35. puts(""); /* will also flush the output */
  36. }
  37. #define FAIL "fail: "
  38. #define WARN "warning: "
  39. #define OK "ok: "
  40. #define RUN "run: "
  41. #define FINISH "finish: "
  42. #define DOWN "down: "
  43. #define TIMEOUT "timeout: "
  44. #define KILL "kill: "
  45. static void fail(char *m1) { ++rc; out(FAIL, m1); }
  46. static void failx(char *m1) { errno = 0; fail(m1); }
  47. static void warn_cannot(char *m1) { ++rc; out("warning: cannot ", m1); }
  48. static void warnx_cannot(char *m1) { errno = 0; warn_cannot(m1); }
  49. static void ok(char *m1) { errno = 0; out(OK, m1); }
  50. static int svstatus_get(void)
  51. {
  52. if ((fd = open_write("supervise/ok")) == -1) {
  53. if (errno == ENODEV) {
  54. *acts == 'x' ? ok("runsv not running")
  55. : failx("runsv not running");
  56. return 0;
  57. }
  58. warn_cannot("open supervise/ok");
  59. return -1;
  60. }
  61. close(fd);
  62. if ((fd = open_read("supervise/status")) == -1) {
  63. warn_cannot("open supervise/status");
  64. return -1;
  65. }
  66. r = read(fd, svstatus, 20);
  67. close(fd);
  68. switch(r) {
  69. case 20: break;
  70. case -1: warn_cannot("read supervise/status"); return -1;
  71. default: warnx_cannot("read supervise/status: bad format"); return -1;
  72. }
  73. return 1;
  74. }
  75. static unsigned svstatus_print(char *m)
  76. {
  77. int pid;
  78. int normallyup = 0;
  79. struct stat s;
  80. if (stat("down", &s) == -1) {
  81. if (errno != ENOENT) {
  82. bb_perror_msg(WARN"cannot stat %s/down", *service);
  83. return 0;
  84. }
  85. normallyup = 1;
  86. }
  87. pid = (unsigned char) svstatus[15];
  88. pid <<= 8; pid += (unsigned char)svstatus[14];
  89. pid <<= 8; pid += (unsigned char)svstatus[13];
  90. pid <<= 8; pid += (unsigned char)svstatus[12];
  91. tai_unpack(svstatus, &tstatus);
  92. if (pid) {
  93. switch (svstatus[19]) {
  94. case 1: printf(RUN); break;
  95. case 2: printf(FINISH); break;
  96. }
  97. printf("%s: (pid %d) ", m, pid);
  98. }
  99. else {
  100. printf(DOWN"%s: ", m);
  101. }
  102. printf("%lus", (unsigned long)(tnow.sec.x < tstatus.x ? 0 : tnow.sec.x-tstatus.x));
  103. if (pid && !normallyup) printf(", normally down");
  104. if (!pid && normallyup) printf(", normally up");
  105. if (pid && svstatus[16]) printf(", paused");
  106. if (!pid && (svstatus[17] == 'u')) printf(", want up");
  107. if (pid && (svstatus[17] == 'd')) printf(", want down");
  108. if (pid && svstatus[18]) printf(", got TERM");
  109. return pid ? 1 : 2;
  110. }
  111. static int status(char *unused)
  112. {
  113. r = svstatus_get();
  114. switch(r) { case -1: case 0: return 0; }
  115. r = svstatus_print(*service);
  116. if (chdir("log") == -1) {
  117. if (errno != ENOENT) {
  118. printf("; log: "WARN"cannot change to log service directory: %s",
  119. strerror(errno));
  120. }
  121. } else if (svstatus_get()) {
  122. printf("; ");
  123. svstatus_print("log");
  124. }
  125. puts(""); /* will also flush the output */
  126. return r;
  127. }
  128. static int checkscript(void)
  129. {
  130. char *prog[2];
  131. struct stat s;
  132. int pid, w;
  133. if (stat("check", &s) == -1) {
  134. if (errno == ENOENT) return 1;
  135. bb_perror_msg(WARN"cannot stat %s/check", *service);
  136. return 0;
  137. }
  138. /* if (!(s.st_mode & S_IXUSR)) return 1; */
  139. if ((pid = fork()) == -1) {
  140. bb_perror_msg(WARN"cannot fork for %s/check", *service);
  141. return 0;
  142. }
  143. if (!pid) {
  144. prog[0] = "./check";
  145. prog[1] = 0;
  146. close(1);
  147. execve("check", prog, environ);
  148. bb_perror_msg(WARN"cannot run %s/check", *service);
  149. _exit(0);
  150. }
  151. while (wait_pid(&w, pid) == -1) {
  152. if (errno == EINTR) continue;
  153. bb_perror_msg(WARN"cannot wait for child %s/check", *service);
  154. return 0;
  155. }
  156. return !wait_exitcode(w);
  157. }
  158. static int check(char *a)
  159. {
  160. unsigned pid;
  161. if ((r = svstatus_get()) == -1) return -1;
  162. if (r == 0) { if (*a == 'x') return 1; return -1; }
  163. pid = (unsigned char)svstatus[15];
  164. pid <<= 8; pid += (unsigned char)svstatus[14];
  165. pid <<= 8; pid += (unsigned char)svstatus[13];
  166. pid <<= 8; pid += (unsigned char)svstatus[12];
  167. switch (*a) {
  168. case 'x': return 0;
  169. case 'u':
  170. if (!pid || svstatus[19] != 1) return 0;
  171. if (!checkscript()) return 0;
  172. break;
  173. case 'd': if (pid) return 0; break;
  174. case 'c': if (pid) if (!checkscript()) return 0; break;
  175. case 't':
  176. if (!pid && svstatus[17] == 'd') break;
  177. tai_unpack(svstatus, &tstatus);
  178. if ((tstart.sec.x > tstatus.x) || !pid || svstatus[18] || !checkscript())
  179. return 0;
  180. break;
  181. case 'o':
  182. tai_unpack(svstatus, &tstatus);
  183. if ((!pid && tstart.sec.x > tstatus.x) || (pid && svstatus[17] != 'd'))
  184. return 0;
  185. }
  186. printf(OK); svstatus_print(*service); puts(""); /* will also flush the output */
  187. return 1;
  188. }
  189. static int control(char *a)
  190. {
  191. if (svstatus_get() <= 0) return -1;
  192. if (svstatus[17] == *a) return 0;
  193. if ((fd = open_write("supervise/control")) == -1) {
  194. if (errno != ENODEV)
  195. warn_cannot("open supervise/control");
  196. else
  197. *a == 'x' ? ok("runsv not running") : failx("runsv not running");
  198. return -1;
  199. }
  200. r = write(fd, a, strlen(a));
  201. close(fd);
  202. if (r != strlen(a)) {
  203. warn_cannot("write to supervise/control");
  204. return -1;
  205. }
  206. return 1;
  207. }
  208. int sv_main(int argc, char **argv)
  209. {
  210. unsigned opt;
  211. unsigned i, want_exit;
  212. char *x;
  213. for (i = strlen(*argv); i; --i)
  214. if ((*argv)[i-1] == '/')
  215. break;
  216. *argv += i;
  217. service = argv;
  218. services = 1;
  219. if ((x = getenv("SVDIR"))) varservice = x;
  220. if ((x = getenv("SVWAIT"))) waitsec = xatoul(x);
  221. /* TODO: V can be handled internally by getopt_ulflags */
  222. opt = getopt32(argc, argv, "w:vV", &x);
  223. if (opt & 1) waitsec = xatoul(x);
  224. if (opt & 2) verbose = 1;
  225. if (opt & 4) usage();
  226. if (!(action = *argv++)) usage();
  227. --argc;
  228. service = argv; services = argc;
  229. if (!*service) usage();
  230. taia_now(&tnow); tstart = tnow;
  231. if ((curdir = open_read(".")) == -1)
  232. fatal_cannot("open current directory");
  233. act = &control; acts = "s";
  234. if (verbose) cbk = &check;
  235. switch (*action) {
  236. case 'x': case 'e':
  237. acts = "x"; break;
  238. case 'X': case 'E':
  239. acts = "x"; kll = 1; cbk = &check; break;
  240. case 'D':
  241. acts = "d"; kll = 1; cbk = &check; break;
  242. case 'T':
  243. acts = "tc"; kll = 1; cbk = &check; break;
  244. case 'c':
  245. if (!str_diff(action, "check")) {
  246. act = 0;
  247. acts = "c";
  248. cbk = &check;
  249. break;
  250. }
  251. case 'u': case 'd': case 'o': case 't': case 'p': case 'h':
  252. case 'a': case 'i': case 'k': case 'q': case '1': case '2':
  253. action[1] = 0; acts = action; break;
  254. case 's':
  255. if (!str_diff(action, "shutdown")) {
  256. acts = "x";
  257. cbk = &check;
  258. break;
  259. }
  260. if (!str_diff(action, "start")) {
  261. acts = "u";
  262. cbk = &check;
  263. break;
  264. }
  265. if (!str_diff(action, "stop")) {
  266. acts = "d";
  267. cbk = &check;
  268. break;
  269. }
  270. act = &status; cbk = 0; break;
  271. case 'r':
  272. if (!str_diff(action, "restart")) {
  273. acts = "tcu";
  274. cbk = &check;
  275. break;
  276. }
  277. usage();
  278. case 'f':
  279. if (!str_diff(action, "force-reload"))
  280. { acts = "tc"; kll = 1; cbk = &check; break; }
  281. if (!str_diff(action, "force-restart"))
  282. { acts = "tcu"; kll = 1; cbk = &check; break; }
  283. if (!str_diff(action, "force-shutdown"))
  284. { acts = "x"; kll = 1; cbk = &check; break; }
  285. if (!str_diff(action, "force-stop"))
  286. { acts = "d"; kll = 1; cbk = &check; break; }
  287. default:
  288. usage();
  289. }
  290. servicex = service;
  291. for (i = 0; i < services; ++i) {
  292. if ((**service != '/') && (**service != '.')) {
  293. if ((chdir(varservice) == -1) || (chdir(*service) == -1)) {
  294. fail("cannot change to service directory");
  295. *service = 0;
  296. }
  297. } else if (chdir(*service) == -1) {
  298. fail("cannot change to service directory");
  299. *service = 0;
  300. }
  301. if (*service) if (act && (act(acts) == -1)) *service = 0;
  302. if (fchdir(curdir) == -1) fatal_cannot("change to original directory");
  303. service++;
  304. }
  305. if (*cbk)
  306. for (;;) {
  307. taia_sub(&tdiff, &tnow, &tstart);
  308. service = servicex; want_exit = 1;
  309. for (i = 0; i < services; ++i, ++service) {
  310. if (!*service) continue;
  311. if ((**service != '/') && (**service != '.')) {
  312. if ((chdir(varservice) == -1) || (chdir(*service) == -1)) {
  313. fail("cannot change to service directory");
  314. *service = 0;
  315. }
  316. } else if (chdir(*service) == -1) {
  317. fail("cannot change to service directory");
  318. *service = 0;
  319. }
  320. if (*service) { if (cbk(acts) != 0) *service = 0; else want_exit = 0; }
  321. if (*service && taia_approx(&tdiff) > waitsec) {
  322. kll ? printf(KILL) : printf(TIMEOUT);
  323. if (svstatus_get() > 0) { svstatus_print(*service); ++rc; }
  324. puts(""); /* will also flush the output */
  325. if (kll) control("k");
  326. *service = 0;
  327. }
  328. if (fchdir(curdir) == -1)
  329. fatal_cannot("change to original directory");
  330. }
  331. if (want_exit) break;
  332. usleep(420000);
  333. taia_now(&tnow);
  334. }
  335. return rc > 99 ? 99 : rc;
  336. }