sv.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. /* Taken from http://smarden.org/runit/sv.8.html:
  25. sv - control and manage services monitored by runsv
  26. sv [-v] [-w sec] command services
  27. /etc/init.d/service [-w sec] command
  28. The sv program reports the current status and controls the state of services
  29. monitored by the runsv(8) supervisor.
  30. services consists of one or more arguments, each argument naming a directory
  31. service used by runsv(8). If service doesn't start with a dot or slash and
  32. doesn't end with a slash, it is searched in the default services directory
  33. /var/service/, otherwise relative to the current directory.
  34. command is one of up, down, status, once, pause, cont, hup, alarm, interrupt,
  35. 1, 2, term, kill, or exit, or start, stop, reload, restart, shutdown,
  36. force-stop, force-reload, force-restart, force-shutdown, try-restart.
  37. status
  38. Report the current status of the service, and the appendant log service
  39. if available, to standard output.
  40. up
  41. If the service is not running, start it. If the service stops, restart it.
  42. down
  43. If the service is running, send it the TERM signal, and the CONT signal.
  44. If ./run exits, start ./finish if it exists. After it stops, do not
  45. restart service.
  46. once
  47. If the service is not running, start it. Do not restart it if it stops.
  48. pause cont hup alarm interrupt quit 1 2 term kill
  49. If the service is running, send it the STOP, CONT, HUP, ALRM, INT, QUIT,
  50. USR1, USR2, TERM, or KILL signal respectively.
  51. exit
  52. If the service is running, send it the TERM signal, and the CONT signal.
  53. Do not restart the service. If the service is down, and no log service
  54. exists, runsv(8) exits. If the service is down and a log service exists,
  55. runsv(8) closes the standard input of the log service and waits for it to
  56. terminate. If the log service is down, runsv(8) exits. This command is
  57. ignored if it is given to an appendant log service.
  58. sv actually looks only at the first character of above commands.
  59. Commands compatible to LSB init script actions:
  60. status
  61. Same as status.
  62. start
  63. Same as up, but wait up to 7 seconds for the command to take effect.
  64. Then report the status or timeout. If the script ./check exists in
  65. the service directory, sv runs this script to check whether the service
  66. is up and available; it's considered to be available if ./check exits
  67. with 0.
  68. stop
  69. Same as down, but wait up to 7 seconds for the service to become down.
  70. Then report the status or timeout.
  71. reload
  72. Same as hup, and additionally report the status afterwards.
  73. restart
  74. Send the commands term, cont, and up to the service, and wait up to
  75. 7 seconds for the service to restart. Then report the status or timeout.
  76. If the script ./check exists in the service directory, sv runs this script
  77. to check whether the service is up and available again; it's considered
  78. to be available if ./check exits with 0.
  79. shutdown
  80. Same as exit, but wait up to 7 seconds for the runsv(8) process
  81. to terminate. Then report the status or timeout.
  82. force-stop
  83. Same as down, but wait up to 7 seconds for the service to become down.
  84. Then report the status, and on timeout send the service the kill command.
  85. force-reload
  86. Send the service the term and cont commands, and wait up to
  87. 7 seconds for the service to restart. Then report the status,
  88. and on timeout send the service the kill command.
  89. force-restart
  90. Send the service the term, cont and up commands, and wait up to
  91. 7 seconds for the service to restart. Then report the status, and
  92. on timeout send the service the kill command. If the script ./check
  93. exists in the service directory, sv runs this script to check whether
  94. the service is up and available again; it's considered to be available
  95. if ./check exits with 0.
  96. force-shutdown
  97. Same as exit, but wait up to 7 seconds for the runsv(8) process to
  98. terminate. Then report the status, and on timeout send the service
  99. the kill command.
  100. try-restart
  101. if the service is running, send it the term and cont commands, and wait up to
  102. 7 seconds for the service to restart. Then report the status or timeout.
  103. Additional Commands
  104. check
  105. Check for the service to be in the state that's been requested. Wait up to
  106. 7 seconds for the service to reach the requested state, then report
  107. the status or timeout. If the requested state of the service is up,
  108. and the script ./check exists in the service directory, sv runs
  109. this script to check whether the service is up and running;
  110. it's considered to be up if ./check exits with 0.
  111. Options
  112. -v
  113. If the command is up, down, term, once, cont, or exit, then wait up to 7
  114. seconds for the command to take effect. Then report the status or timeout.
  115. -w sec
  116. Override the default timeout of 7 seconds with sec seconds. Implies -v.
  117. Environment
  118. SVDIR
  119. The environment variable $SVDIR overrides the default services directory
  120. /var/service.
  121. SVWAIT
  122. The environment variable $SVWAIT overrides the default 7 seconds to wait
  123. for a command to take effect. It is overridden by the -w option.
  124. Exit Codes
  125. sv exits 0, if the command was successfully sent to all services, and,
  126. if it was told to wait, the command has taken effect to all services.
  127. For each service that caused an error (e.g. the directory is not
  128. controlled by a runsv(8) process, or sv timed out while waiting),
  129. sv increases the exit code by one and exits non zero. The maximum
  130. is 99. sv exits 100 on error.
  131. */
  132. /* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
  133. //config:config SV
  134. //config: bool "sv (8.5 kb)"
  135. //config: default y
  136. //config: help
  137. //config: sv reports the current status and controls the state of services
  138. //config: monitored by the runsv supervisor.
  139. //config:
  140. //config:config SV_DEFAULT_SERVICE_DIR
  141. //config: string "Default directory for services"
  142. //config: default "/var/service"
  143. //config: depends on SV || SVC || SVOK
  144. //config: help
  145. //config: Default directory for services.
  146. //config: Defaults to "/var/service"
  147. //config:
  148. //config:config SVC
  149. //config: bool "svc (8.4 kb)"
  150. //config: default y
  151. //config: help
  152. //config: svc controls the state of services monitored by the runsv supervisor.
  153. //config: It is compatible with daemontools command with the same name.
  154. //config:
  155. //config:config SVOK
  156. //config: bool "svok (1.5 kb)"
  157. //config: default y
  158. //config: help
  159. //config: svok checks whether runsv supervisor is running.
  160. //config: It is compatible with daemontools command with the same name.
  161. //applet:IF_SV( APPLET_NOEXEC(sv, sv, BB_DIR_USR_BIN, BB_SUID_DROP, sv ))
  162. //applet:IF_SVC( APPLET_NOEXEC(svc, svc, BB_DIR_USR_BIN, BB_SUID_DROP, svc ))
  163. //applet:IF_SVOK(APPLET_NOEXEC(svok, svok, BB_DIR_USR_BIN, BB_SUID_DROP, svok))
  164. //kbuild:lib-$(CONFIG_SV) += sv.o
  165. //kbuild:lib-$(CONFIG_SVC) += sv.o
  166. //kbuild:lib-$(CONFIG_SVOK) += sv.o
  167. #include <sys/file.h>
  168. #include "libbb.h"
  169. #include "common_bufsiz.h"
  170. #include "runit_lib.h"
  171. struct globals {
  172. const char *acts;
  173. char **service;
  174. unsigned rc;
  175. /* "Bernstein" time format: unix + 0x400000000000000aULL */
  176. uint64_t tstart, tnow;
  177. svstatus_t svstatus;
  178. smallint islog;
  179. } FIX_ALIASING;
  180. #define G (*(struct globals*)bb_common_bufsiz1)
  181. #define acts (G.acts )
  182. #define service (G.service )
  183. #define rc (G.rc )
  184. #define tstart (G.tstart )
  185. #define tnow (G.tnow )
  186. #define svstatus (G.svstatus )
  187. #define islog (G.islog )
  188. #define INIT_G() do { \
  189. setup_common_bufsiz(); \
  190. /* need to zero out, svc calls sv() repeatedly */ \
  191. memset(&G, 0, sizeof(G)); \
  192. } while (0)
  193. #define str_equal(s,t) (strcmp((s), (t)) == 0)
  194. #if ENABLE_SV || ENABLE_SVC
  195. static void fatal_cannot(const char *m1) NORETURN;
  196. static void fatal_cannot(const char *m1)
  197. {
  198. bb_perror_msg("fatal: can't %s", m1);
  199. _exit(151);
  200. }
  201. static void out(const char *p, const char *m1)
  202. {
  203. printf("%s%s%s: %s", p, *service, islog ? "/log" : "", m1);
  204. if (errno) {
  205. printf(": "STRERROR_FMT STRERROR_ERRNO);
  206. }
  207. bb_putchar('\n'); /* will also flush the output */
  208. }
  209. #define WARN "warning: "
  210. #define OK "ok: "
  211. static void fail(const char *m1)
  212. {
  213. ++rc;
  214. out("fail: ", m1);
  215. }
  216. static void failx(const char *m1)
  217. {
  218. errno = 0;
  219. fail(m1);
  220. }
  221. static void warn(const char *m1)
  222. {
  223. ++rc;
  224. /* "warning: <service>: <m1>\n" */
  225. out("warning: ", m1);
  226. }
  227. static void ok(const char *m1)
  228. {
  229. errno = 0;
  230. out(OK, m1);
  231. }
  232. static int svstatus_get(void)
  233. {
  234. int fd, r;
  235. fd = open("supervise/ok", O_WRONLY|O_NDELAY);
  236. if (fd == -1) {
  237. if (errno == ENODEV) {
  238. *acts == 'x' ? ok("runsv not running")
  239. : failx("runsv not running");
  240. return 0;
  241. }
  242. warn("can't open supervise/ok");
  243. return -1;
  244. }
  245. close(fd);
  246. fd = open("supervise/status", O_RDONLY|O_NDELAY);
  247. if (fd == -1) {
  248. warn("can't open supervise/status");
  249. return -1;
  250. }
  251. r = read(fd, &svstatus, 20);
  252. close(fd);
  253. switch (r) {
  254. case 20:
  255. break;
  256. case -1:
  257. warn("can't read supervise/status");
  258. return -1;
  259. default:
  260. errno = 0;
  261. warn("can't read supervise/status: bad format");
  262. return -1;
  263. }
  264. return 1;
  265. }
  266. static unsigned svstatus_print(const char *m)
  267. {
  268. int diff;
  269. int pid;
  270. int normallyup = 0;
  271. struct stat s;
  272. uint64_t timestamp;
  273. if (stat("down", &s) == -1) {
  274. if (errno != ENOENT) {
  275. bb_perror_msg(WARN"can't stat %s/down", *service);
  276. return 0;
  277. }
  278. normallyup = 1;
  279. }
  280. pid = SWAP_LE32(svstatus.pid_le32);
  281. timestamp = SWAP_BE64(svstatus.time_be64);
  282. switch (svstatus.run_or_finish) {
  283. case 0: printf("down: "); break;
  284. case 1: printf("run: "); break;
  285. case 2: printf("finish: "); break;
  286. }
  287. printf("%s: ", m);
  288. if (svstatus.run_or_finish)
  289. printf("(pid %d) ", pid);
  290. diff = tnow - timestamp;
  291. printf("%us", (diff < 0 ? 0 : diff));
  292. if (pid) {
  293. if (!normallyup) printf(", normally down");
  294. if (svstatus.paused) printf(", paused");
  295. if (svstatus.want == 'd') printf(", want down");
  296. if (svstatus.got_term) printf(", got TERM");
  297. } else {
  298. if (normallyup) printf(", normally up");
  299. if (svstatus.want == 'u') printf(", want up");
  300. }
  301. return pid ? 1 : 2;
  302. }
  303. static int status(const char *unused UNUSED_PARAM)
  304. {
  305. int r;
  306. if (svstatus_get() <= 0)
  307. return 0;
  308. r = svstatus_print(*service);
  309. islog = 1;
  310. if (chdir("log") == -1) {
  311. if (errno != ENOENT) {
  312. printf("; ");
  313. warn("can't change directory");
  314. } else
  315. bb_putchar('\n');
  316. } else {
  317. printf("; ");
  318. if (svstatus_get()) {
  319. r = svstatus_print("log");
  320. bb_putchar('\n');
  321. }
  322. }
  323. islog = 0;
  324. return r;
  325. }
  326. static int checkscript(void)
  327. {
  328. char *prog[2];
  329. struct stat s;
  330. int pid, w;
  331. if (stat("check", &s) == -1) {
  332. if (errno == ENOENT) return 1;
  333. bb_perror_msg(WARN"can't stat %s/check", *service);
  334. return 0;
  335. }
  336. /* if (!(s.st_mode & S_IXUSR)) return 1; */
  337. prog[0] = (char*)"./check";
  338. prog[1] = NULL;
  339. pid = spawn(prog);
  340. if (pid <= 0) {
  341. bb_perror_msg(WARN"can't %s child %s/check", "run", *service);
  342. return 0;
  343. }
  344. while (safe_waitpid(pid, &w, 0) == -1) {
  345. bb_perror_msg(WARN"can't %s child %s/check", "wait for", *service);
  346. return 0;
  347. }
  348. return WEXITSTATUS(w) == 0;
  349. }
  350. static int check(const char *a)
  351. {
  352. int r;
  353. unsigned pid_le32;
  354. uint64_t timestamp;
  355. r = svstatus_get();
  356. if (r == -1)
  357. return -1;
  358. while (*a) {
  359. if (r == 0) {
  360. if (*a == 'x')
  361. return 1;
  362. return -1;
  363. }
  364. pid_le32 = svstatus.pid_le32;
  365. switch (*a) {
  366. case 'x':
  367. return 0;
  368. case 'u':
  369. if (!pid_le32 || svstatus.run_or_finish != 1)
  370. return 0;
  371. if (!checkscript())
  372. return 0;
  373. break;
  374. case 'd':
  375. if (pid_le32 || svstatus.run_or_finish != 0)
  376. return 0;
  377. break;
  378. case 'C':
  379. if (pid_le32 && !checkscript())
  380. return 0;
  381. break;
  382. case 't':
  383. case 'k':
  384. if (!pid_le32 && svstatus.want == 'd')
  385. break;
  386. timestamp = SWAP_BE64(svstatus.time_be64);
  387. if ((tstart > timestamp) || !pid_le32 || svstatus.got_term || !checkscript())
  388. return 0;
  389. break;
  390. case 'o':
  391. timestamp = SWAP_BE64(svstatus.time_be64);
  392. if ((!pid_le32 && tstart > timestamp) || (pid_le32 && svstatus.want != 'd'))
  393. return 0;
  394. break;
  395. case 'p':
  396. if (pid_le32 && !svstatus.paused)
  397. return 0;
  398. break;
  399. case 'c':
  400. if (pid_le32 && svstatus.paused)
  401. return 0;
  402. break;
  403. }
  404. ++a;
  405. }
  406. printf(OK);
  407. svstatus_print(*service);
  408. bb_putchar('\n'); /* will also flush the output */
  409. return 1;
  410. }
  411. static int control(const char *a)
  412. {
  413. int fd, r, l;
  414. if (svstatus_get() <= 0)
  415. return -1;
  416. if (svstatus.want == *a && (*a != 'd' || svstatus.got_term == 1))
  417. return 0;
  418. fd = open("supervise/control", O_WRONLY|O_NDELAY);
  419. if (fd == -1) {
  420. if (errno != ENODEV)
  421. warn("can't open supervise/control");
  422. else
  423. *a == 'x' ? ok("runsv not running") : failx("runsv not running");
  424. return -1;
  425. }
  426. l = strlen(a);
  427. r = write(fd, a, l);
  428. close(fd);
  429. if (r != l) {
  430. warn("can't write to supervise/control");
  431. return -1;
  432. }
  433. return 1;
  434. }
  435. //usage:#define sv_trivial_usage
  436. //usage: "[-v] [-w SEC] CMD SERVICE_DIR..."
  437. //usage:#define sv_full_usage "\n\n"
  438. //usage: "Control services monitored by runsv supervisor.\n"
  439. //usage: "Commands (only first character is enough):\n"
  440. //usage: "\n"
  441. //usage: "status: query service status\n"
  442. //usage: "up: if service isn't running, start it. If service stops, restart it\n"
  443. //usage: "once: like 'up', but if service stops, don't restart it\n"
  444. //usage: "down: send TERM and CONT signals. If ./run exits, start ./finish\n"
  445. //usage: " if it exists. After it stops, don't restart service\n"
  446. //usage: "exit: send TERM and CONT signals to service and log service. If they exit,\n"
  447. //usage: " runsv exits too\n"
  448. //usage: "pause, cont, hup, alarm, interrupt, quit, 1, 2, term, kill: send\n"
  449. //usage: "STOP, CONT, HUP, ALRM, INT, QUIT, USR1, USR2, TERM, KILL signal to service"
  450. static int sv(char **argv)
  451. {
  452. char *x;
  453. char *action;
  454. const char *varservice = CONFIG_SV_DEFAULT_SERVICE_DIR;
  455. unsigned waitsec = 7;
  456. smallint kll = 0;
  457. int verbose = 0;
  458. int (*act)(const char*);
  459. int (*cbk)(const char*);
  460. int curdir;
  461. INIT_G();
  462. xfunc_error_retval = 100;
  463. x = getenv("SVDIR");
  464. if (x) varservice = x;
  465. x = getenv("SVWAIT");
  466. if (x) waitsec = xatou(x);
  467. getopt32(argv, "^" "w:+v" "\0" "vv" /* -w N, -v is a counter */,
  468. &waitsec, &verbose
  469. );
  470. argv += optind;
  471. action = *argv++;
  472. if (!action || !*argv) bb_show_usage();
  473. tnow = time(NULL) + 0x400000000000000aULL;
  474. tstart = tnow;
  475. curdir = open(".", O_RDONLY|O_NDELAY);
  476. if (curdir == -1)
  477. fatal_cannot("open current directory");
  478. act = &control;
  479. acts = "s";
  480. cbk = &check;
  481. switch (*action) {
  482. case 'x':
  483. case 'e':
  484. acts = "x";
  485. if (!verbose) cbk = NULL;
  486. break;
  487. case 'X':
  488. case 'E':
  489. acts = "x";
  490. kll = 1;
  491. break;
  492. case 'D':
  493. acts = "d";
  494. kll = 1;
  495. break;
  496. case 'T':
  497. acts = "tc";
  498. kll = 1;
  499. break;
  500. case 't':
  501. if (str_equal(action, "try-restart")) {
  502. acts = "tc";
  503. break;
  504. }
  505. case 'c':
  506. if (str_equal(action, "check")) {
  507. act = NULL;
  508. acts = "C";
  509. break;
  510. }
  511. case 'u': case 'd': case 'o': case 'p': case 'h':
  512. case 'a': case 'i': case 'k': case 'q': case '1': case '2':
  513. action[1] = '\0';
  514. acts = action;
  515. if (!verbose)
  516. cbk = NULL;
  517. break;
  518. case 's':
  519. if (str_equal(action, "shutdown")) {
  520. acts = "x";
  521. break;
  522. }
  523. if (str_equal(action, "start")) {
  524. acts = "u";
  525. break;
  526. }
  527. if (str_equal(action, "stop")) {
  528. acts = "d";
  529. break;
  530. }
  531. /* "status" */
  532. act = &status;
  533. cbk = NULL;
  534. break;
  535. case 'r':
  536. if (str_equal(action, "restart")) {
  537. acts = "tcu";
  538. break;
  539. }
  540. if (str_equal(action, "reload")) {
  541. acts = "h";
  542. break;
  543. }
  544. bb_show_usage();
  545. case 'f':
  546. if (str_equal(action, "force-reload")) {
  547. acts = "tc";
  548. kll = 1;
  549. break;
  550. }
  551. if (str_equal(action, "force-restart")) {
  552. acts = "tcu";
  553. kll = 1;
  554. break;
  555. }
  556. if (str_equal(action, "force-shutdown")) {
  557. acts = "x";
  558. kll = 1;
  559. break;
  560. }
  561. if (str_equal(action, "force-stop")) {
  562. acts = "d";
  563. kll = 1;
  564. break;
  565. }
  566. default:
  567. bb_show_usage();
  568. }
  569. service = argv;
  570. while ((x = *service) != NULL) {
  571. if (x[0] != '/' && x[0] != '.'
  572. && !last_char_is(x, '/')
  573. ) {
  574. if (chdir(varservice) == -1)
  575. goto chdir_failed_0;
  576. }
  577. if (chdir(x) == -1) {
  578. chdir_failed_0:
  579. fail("can't change to service directory");
  580. goto nullify_service_0;
  581. }
  582. if (act && (act(acts) == -1)) {
  583. nullify_service_0:
  584. *service = (char*) -1L; /* "dead" */
  585. }
  586. if (fchdir(curdir) == -1)
  587. fatal_cannot("change to original directory");
  588. service++;
  589. }
  590. if (cbk) while (1) {
  591. int want_exit;
  592. int diff;
  593. diff = tnow - tstart;
  594. service = argv;
  595. want_exit = 1;
  596. while ((x = *service) != NULL) {
  597. if (x == (char*) -1L) /* "dead" */
  598. goto next;
  599. if (x[0] != '/' && x[0] != '.') {
  600. if (chdir(varservice) == -1)
  601. goto chdir_failed;
  602. }
  603. if (chdir(x) == -1) {
  604. chdir_failed:
  605. fail("can't change to service directory");
  606. goto nullify_service;
  607. }
  608. if (cbk(acts) != 0)
  609. goto nullify_service;
  610. want_exit = 0;
  611. if (diff >= waitsec) {
  612. printf(kll ? "kill: " : "timeout: ");
  613. if (svstatus_get() > 0) {
  614. svstatus_print(x);
  615. ++rc;
  616. }
  617. bb_putchar('\n'); /* will also flush the output */
  618. if (kll)
  619. control("k");
  620. nullify_service:
  621. *service = (char*) -1L; /* "dead" */
  622. }
  623. if (fchdir(curdir) == -1)
  624. fatal_cannot("change to original directory");
  625. next:
  626. service++;
  627. }
  628. if (want_exit) break;
  629. usleep(420000);
  630. tnow = time(NULL) + 0x400000000000000aULL;
  631. }
  632. return rc > 99 ? 99 : rc;
  633. }
  634. #endif
  635. #if ENABLE_SV
  636. int sv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  637. int sv_main(int argc UNUSED_PARAM, char **argv)
  638. {
  639. return sv(argv);
  640. }
  641. #endif
  642. //usage:#define svc_trivial_usage
  643. //usage: "[-udopchaitkx] SERVICE_DIR..."
  644. //usage:#define svc_full_usage "\n\n"
  645. //usage: "Control services monitored by runsv supervisor"
  646. //usage: "\n"
  647. //usage: "\n"" -u If service is not running, start it; restart if it stops"
  648. //usage: "\n"" -d If service is running, send TERM+CONT signals; do not restart it"
  649. //usage: "\n"" -o Once: if service is not running, start it; do not restart it"
  650. //usage: "\n"" -pchaitk Send STOP, CONT, HUP, ALRM, INT, TERM, KILL signal to service"
  651. //usage: "\n"" -x Exit: runsv will exit as soon as the service is down"
  652. #if ENABLE_SVC
  653. int svc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  654. int svc_main(int argc UNUSED_PARAM, char **argv)
  655. {
  656. char command[2];
  657. const char *optstring;
  658. unsigned opts;
  659. optstring = "udopchaitkx";
  660. opts = getopt32(argv, optstring);
  661. argv += optind;
  662. if (!argv[0] || !opts)
  663. bb_show_usage();
  664. argv -= 2;
  665. if (optind > 2) {
  666. argv--;
  667. argv[2] = (char*)"--";
  668. }
  669. argv[0] = (char*)"sv";
  670. argv[1] = command;
  671. command[1] = '\0';
  672. do {
  673. if (opts & 1) {
  674. int r;
  675. command[0] = *optstring;
  676. /* getopt() was already called by getopt32():
  677. * reset the libc getopt() function's internal state.
  678. */
  679. GETOPT_RESET();
  680. r = sv(argv);
  681. if (r)
  682. return 1;
  683. }
  684. optstring++;
  685. opts >>= 1;
  686. } while (opts);
  687. return 0;
  688. }
  689. #endif
  690. //usage:#define svok_trivial_usage
  691. //usage: "SERVICE_DIR"
  692. //usage:#define svok_full_usage "\n\n"
  693. //usage: "Check whether runsv supervisor is running.\n"
  694. //usage: "Exit code is 0 if it does, 100 if it does not,\n"
  695. //usage: "111 (with error message) if SERVICE_DIR does not exist."
  696. #if ENABLE_SVOK
  697. int svok_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  698. int svok_main(int argc UNUSED_PARAM, char **argv)
  699. {
  700. const char *dir = argv[1];
  701. if (!dir)
  702. bb_show_usage();
  703. xfunc_error_retval = 111;
  704. /*
  705. * daemontools has no concept of "default service dir", runit does.
  706. * Let's act as runit.
  707. */
  708. if (dir[0] != '/' && dir[0] != '.'
  709. && !last_char_is(dir, '/')
  710. ) {
  711. xchdir(CONFIG_SV_DEFAULT_SERVICE_DIR);
  712. }
  713. xchdir(dir);
  714. if (open("supervise/ok", O_WRONLY) < 0) {
  715. if (errno == ENOENT || errno == ENXIO)
  716. return 100;
  717. bb_perror_msg_and_die("can't open '%s'", "supervise/ok");
  718. }
  719. return 0;
  720. }
  721. #endif