sv.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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 (7.8 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
  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 (7.8 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 comaptible with daemontools command with the same name.
  154. //applet:IF_SV( APPLET_NOEXEC(sv, sv, BB_DIR_USR_BIN, BB_SUID_DROP, sv ))
  155. //applet:IF_SVC(APPLET_NOEXEC(svc, svc, BB_DIR_USR_BIN, BB_SUID_DROP, svc))
  156. //kbuild:lib-$(CONFIG_SV) += sv.o
  157. //kbuild:lib-$(CONFIG_SVC) += sv.o
  158. #include <sys/file.h>
  159. #include "libbb.h"
  160. #include "common_bufsiz.h"
  161. #include "runit_lib.h"
  162. struct globals {
  163. const char *acts;
  164. char **service;
  165. unsigned rc;
  166. /* "Bernstein" time format: unix + 0x400000000000000aULL */
  167. uint64_t tstart, tnow;
  168. svstatus_t svstatus;
  169. smallint islog;
  170. } FIX_ALIASING;
  171. #define G (*(struct globals*)bb_common_bufsiz1)
  172. #define acts (G.acts )
  173. #define service (G.service )
  174. #define rc (G.rc )
  175. #define tstart (G.tstart )
  176. #define tnow (G.tnow )
  177. #define svstatus (G.svstatus )
  178. #define islog (G.islog )
  179. #define INIT_G() do { \
  180. setup_common_bufsiz(); \
  181. /* need to zero out, svc calls sv() repeatedly */ \
  182. memset(&G, 0, sizeof(G)); \
  183. } while (0)
  184. #define str_equal(s,t) (strcmp((s), (t)) == 0)
  185. static void fatal_cannot(const char *m1) NORETURN;
  186. static void fatal_cannot(const char *m1)
  187. {
  188. bb_perror_msg("fatal: can't %s", m1);
  189. _exit(151);
  190. }
  191. static void out(const char *p, const char *m1)
  192. {
  193. printf("%s%s%s: %s", p, *service, islog ? "/log" : "", m1);
  194. if (errno) {
  195. printf(": "STRERROR_FMT STRERROR_ERRNO);
  196. }
  197. bb_putchar('\n'); /* will also flush the output */
  198. }
  199. #define WARN "warning: "
  200. #define OK "ok: "
  201. static void fail(const char *m1)
  202. {
  203. ++rc;
  204. out("fail: ", m1);
  205. }
  206. static void failx(const char *m1)
  207. {
  208. errno = 0;
  209. fail(m1);
  210. }
  211. static void warn(const char *m1)
  212. {
  213. ++rc;
  214. /* "warning: <service>: <m1>\n" */
  215. out("warning: ", m1);
  216. }
  217. static void ok(const char *m1)
  218. {
  219. errno = 0;
  220. out(OK, m1);
  221. }
  222. static int svstatus_get(void)
  223. {
  224. int fd, r;
  225. fd = open("supervise/ok", O_WRONLY|O_NDELAY);
  226. if (fd == -1) {
  227. if (errno == ENODEV) {
  228. *acts == 'x' ? ok("runsv not running")
  229. : failx("runsv not running");
  230. return 0;
  231. }
  232. warn("can't open supervise/ok");
  233. return -1;
  234. }
  235. close(fd);
  236. fd = open("supervise/status", O_RDONLY|O_NDELAY);
  237. if (fd == -1) {
  238. warn("can't open supervise/status");
  239. return -1;
  240. }
  241. r = read(fd, &svstatus, 20);
  242. close(fd);
  243. switch (r) {
  244. case 20:
  245. break;
  246. case -1:
  247. warn("can't read supervise/status");
  248. return -1;
  249. default:
  250. errno = 0;
  251. warn("can't read supervise/status: bad format");
  252. return -1;
  253. }
  254. return 1;
  255. }
  256. static unsigned svstatus_print(const char *m)
  257. {
  258. int diff;
  259. int pid;
  260. int normallyup = 0;
  261. struct stat s;
  262. uint64_t timestamp;
  263. if (stat("down", &s) == -1) {
  264. if (errno != ENOENT) {
  265. bb_perror_msg(WARN"can't stat %s/down", *service);
  266. return 0;
  267. }
  268. normallyup = 1;
  269. }
  270. pid = SWAP_LE32(svstatus.pid_le32);
  271. timestamp = SWAP_BE64(svstatus.time_be64);
  272. switch (svstatus.run_or_finish) {
  273. case 0: printf("down: "); break;
  274. case 1: printf("run: "); break;
  275. case 2: printf("finish: "); break;
  276. }
  277. printf("%s: ", m);
  278. if (svstatus.run_or_finish)
  279. printf("(pid %d) ", pid);
  280. diff = tnow - timestamp;
  281. printf("%us", (diff < 0 ? 0 : diff));
  282. if (pid) {
  283. if (!normallyup) printf(", normally down");
  284. if (svstatus.paused) printf(", paused");
  285. if (svstatus.want == 'd') printf(", want down");
  286. if (svstatus.got_term) printf(", got TERM");
  287. } else {
  288. if (normallyup) printf(", normally up");
  289. if (svstatus.want == 'u') printf(", want up");
  290. }
  291. return pid ? 1 : 2;
  292. }
  293. static int status(const char *unused UNUSED_PARAM)
  294. {
  295. int r;
  296. if (svstatus_get() <= 0)
  297. return 0;
  298. r = svstatus_print(*service);
  299. islog = 1;
  300. if (chdir("log") == -1) {
  301. if (errno != ENOENT) {
  302. printf("; ");
  303. warn("can't change directory");
  304. } else
  305. bb_putchar('\n');
  306. } else {
  307. printf("; ");
  308. if (svstatus_get()) {
  309. r = svstatus_print("log");
  310. bb_putchar('\n');
  311. }
  312. }
  313. islog = 0;
  314. return r;
  315. }
  316. static int checkscript(void)
  317. {
  318. char *prog[2];
  319. struct stat s;
  320. int pid, w;
  321. if (stat("check", &s) == -1) {
  322. if (errno == ENOENT) return 1;
  323. bb_perror_msg(WARN"can't stat %s/check", *service);
  324. return 0;
  325. }
  326. /* if (!(s.st_mode & S_IXUSR)) return 1; */
  327. prog[0] = (char*)"./check";
  328. prog[1] = NULL;
  329. pid = spawn(prog);
  330. if (pid <= 0) {
  331. bb_perror_msg(WARN"can't %s child %s/check", "run", *service);
  332. return 0;
  333. }
  334. while (safe_waitpid(pid, &w, 0) == -1) {
  335. bb_perror_msg(WARN"can't %s child %s/check", "wait for", *service);
  336. return 0;
  337. }
  338. return WEXITSTATUS(w) == 0;
  339. }
  340. static int check(const char *a)
  341. {
  342. int r;
  343. unsigned pid_le32;
  344. uint64_t timestamp;
  345. r = svstatus_get();
  346. if (r == -1)
  347. return -1;
  348. while (*a) {
  349. if (r == 0) {
  350. if (*a == 'x')
  351. return 1;
  352. return -1;
  353. }
  354. pid_le32 = svstatus.pid_le32;
  355. switch (*a) {
  356. case 'x':
  357. return 0;
  358. case 'u':
  359. if (!pid_le32 || svstatus.run_or_finish != 1)
  360. return 0;
  361. if (!checkscript())
  362. return 0;
  363. break;
  364. case 'd':
  365. if (pid_le32 || svstatus.run_or_finish != 0)
  366. return 0;
  367. break;
  368. case 'C':
  369. if (pid_le32 && !checkscript())
  370. return 0;
  371. break;
  372. case 't':
  373. case 'k':
  374. if (!pid_le32 && svstatus.want == 'd')
  375. break;
  376. timestamp = SWAP_BE64(svstatus.time_be64);
  377. if ((tstart > timestamp) || !pid_le32 || svstatus.got_term || !checkscript())
  378. return 0;
  379. break;
  380. case 'o':
  381. timestamp = SWAP_BE64(svstatus.time_be64);
  382. if ((!pid_le32 && tstart > timestamp) || (pid_le32 && svstatus.want != 'd'))
  383. return 0;
  384. break;
  385. case 'p':
  386. if (pid_le32 && !svstatus.paused)
  387. return 0;
  388. break;
  389. case 'c':
  390. if (pid_le32 && svstatus.paused)
  391. return 0;
  392. break;
  393. }
  394. ++a;
  395. }
  396. printf(OK);
  397. svstatus_print(*service);
  398. bb_putchar('\n'); /* will also flush the output */
  399. return 1;
  400. }
  401. static int control(const char *a)
  402. {
  403. int fd, r, l;
  404. if (svstatus_get() <= 0)
  405. return -1;
  406. if (svstatus.want == *a && (*a != 'd' || svstatus.got_term == 1))
  407. return 0;
  408. fd = open("supervise/control", O_WRONLY|O_NDELAY);
  409. if (fd == -1) {
  410. if (errno != ENODEV)
  411. warn("can't open supervise/control");
  412. else
  413. *a == 'x' ? ok("runsv not running") : failx("runsv not running");
  414. return -1;
  415. }
  416. l = strlen(a);
  417. r = write(fd, a, l);
  418. close(fd);
  419. if (r != l) {
  420. warn("can't write to supervise/control");
  421. return -1;
  422. }
  423. return 1;
  424. }
  425. //usage:#define sv_trivial_usage
  426. //usage: "[-v] [-w SEC] CMD SERVICE_DIR..."
  427. //usage:#define sv_full_usage "\n\n"
  428. //usage: "Control services monitored by runsv supervisor.\n"
  429. //usage: "Commands (only first character is enough):\n"
  430. //usage: "\n"
  431. //usage: "status: query service status\n"
  432. //usage: "up: if service isn't running, start it. If service stops, restart it\n"
  433. //usage: "once: like 'up', but if service stops, don't restart it\n"
  434. //usage: "down: send TERM and CONT signals. If ./run exits, start ./finish\n"
  435. //usage: " if it exists. After it stops, don't restart service\n"
  436. //usage: "exit: send TERM and CONT signals to service and log service. If they exit,\n"
  437. //usage: " runsv exits too\n"
  438. //usage: "pause, cont, hup, alarm, interrupt, quit, 1, 2, term, kill: send\n"
  439. //usage: "STOP, CONT, HUP, ALRM, INT, QUIT, USR1, USR2, TERM, KILL signal to service"
  440. static int sv(char **argv)
  441. {
  442. char *x;
  443. char *action;
  444. const char *varservice = CONFIG_SV_DEFAULT_SERVICE_DIR;
  445. unsigned waitsec = 7;
  446. smallint kll = 0;
  447. int verbose = 0;
  448. int (*act)(const char*);
  449. int (*cbk)(const char*);
  450. int curdir;
  451. INIT_G();
  452. xfunc_error_retval = 100;
  453. x = getenv("SVDIR");
  454. if (x) varservice = x;
  455. x = getenv("SVWAIT");
  456. if (x) waitsec = xatou(x);
  457. getopt32(argv, "^" "w:+v" "\0" "vv" /* -w N, -v is a counter */,
  458. &waitsec, &verbose
  459. );
  460. argv += optind;
  461. action = *argv++;
  462. if (!action || !*argv) bb_show_usage();
  463. tnow = time(NULL) + 0x400000000000000aULL;
  464. tstart = tnow;
  465. curdir = open(".", O_RDONLY|O_NDELAY);
  466. if (curdir == -1)
  467. fatal_cannot("open current directory");
  468. act = &control;
  469. acts = "s";
  470. cbk = &check;
  471. switch (*action) {
  472. case 'x':
  473. case 'e':
  474. acts = "x";
  475. if (!verbose) cbk = NULL;
  476. break;
  477. case 'X':
  478. case 'E':
  479. acts = "x";
  480. kll = 1;
  481. break;
  482. case 'D':
  483. acts = "d";
  484. kll = 1;
  485. break;
  486. case 'T':
  487. acts = "tc";
  488. kll = 1;
  489. break;
  490. case 't':
  491. if (str_equal(action, "try-restart")) {
  492. acts = "tc";
  493. break;
  494. }
  495. case 'c':
  496. if (str_equal(action, "check")) {
  497. act = NULL;
  498. acts = "C";
  499. break;
  500. }
  501. case 'u': case 'd': case 'o': case 'p': case 'h':
  502. case 'a': case 'i': case 'k': case 'q': case '1': case '2':
  503. action[1] = '\0';
  504. acts = action;
  505. if (!verbose)
  506. cbk = NULL;
  507. break;
  508. case 's':
  509. if (str_equal(action, "shutdown")) {
  510. acts = "x";
  511. break;
  512. }
  513. if (str_equal(action, "start")) {
  514. acts = "u";
  515. break;
  516. }
  517. if (str_equal(action, "stop")) {
  518. acts = "d";
  519. break;
  520. }
  521. /* "status" */
  522. act = &status;
  523. cbk = NULL;
  524. break;
  525. case 'r':
  526. if (str_equal(action, "restart")) {
  527. acts = "tcu";
  528. break;
  529. }
  530. if (str_equal(action, "reload")) {
  531. acts = "h";
  532. break;
  533. }
  534. bb_show_usage();
  535. case 'f':
  536. if (str_equal(action, "force-reload")) {
  537. acts = "tc";
  538. kll = 1;
  539. break;
  540. }
  541. if (str_equal(action, "force-restart")) {
  542. acts = "tcu";
  543. kll = 1;
  544. break;
  545. }
  546. if (str_equal(action, "force-shutdown")) {
  547. acts = "x";
  548. kll = 1;
  549. break;
  550. }
  551. if (str_equal(action, "force-stop")) {
  552. acts = "d";
  553. kll = 1;
  554. break;
  555. }
  556. default:
  557. bb_show_usage();
  558. }
  559. service = argv;
  560. while ((x = *service) != NULL) {
  561. if (x[0] != '/' && x[0] != '.'
  562. && x[0] != '\0' && x[strlen(x) - 1] != '/'
  563. ) {
  564. if (chdir(varservice) == -1)
  565. goto chdir_failed_0;
  566. }
  567. if (chdir(x) == -1) {
  568. chdir_failed_0:
  569. fail("can't change to service directory");
  570. goto nullify_service_0;
  571. }
  572. if (act && (act(acts) == -1)) {
  573. nullify_service_0:
  574. *service = (char*) -1L; /* "dead" */
  575. }
  576. if (fchdir(curdir) == -1)
  577. fatal_cannot("change to original directory");
  578. service++;
  579. }
  580. if (cbk) while (1) {
  581. int want_exit;
  582. int diff;
  583. diff = tnow - tstart;
  584. service = argv;
  585. want_exit = 1;
  586. while ((x = *service) != NULL) {
  587. if (x == (char*) -1L) /* "dead" */
  588. goto next;
  589. if (x[0] != '/' && x[0] != '.') {
  590. if (chdir(varservice) == -1)
  591. goto chdir_failed;
  592. }
  593. if (chdir(x) == -1) {
  594. chdir_failed:
  595. fail("can't change to service directory");
  596. goto nullify_service;
  597. }
  598. if (cbk(acts) != 0)
  599. goto nullify_service;
  600. want_exit = 0;
  601. if (diff >= waitsec) {
  602. printf(kll ? "kill: " : "timeout: ");
  603. if (svstatus_get() > 0) {
  604. svstatus_print(x);
  605. ++rc;
  606. }
  607. bb_putchar('\n'); /* will also flush the output */
  608. if (kll)
  609. control("k");
  610. nullify_service:
  611. *service = (char*) -1L; /* "dead" */
  612. }
  613. if (fchdir(curdir) == -1)
  614. fatal_cannot("change to original directory");
  615. next:
  616. service++;
  617. }
  618. if (want_exit) break;
  619. usleep(420000);
  620. tnow = time(NULL) + 0x400000000000000aULL;
  621. }
  622. return rc > 99 ? 99 : rc;
  623. }
  624. #if ENABLE_SV
  625. int sv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  626. int sv_main(int argc UNUSED_PARAM, char **argv)
  627. {
  628. return sv(argv);
  629. }
  630. #endif
  631. //usage:#define svc_trivial_usage
  632. //usage: "[-udopchaitkx] SERVICE_DIR..."
  633. //usage:#define svc_full_usage "\n\n"
  634. //usage: "Control services monitored by runsv supervisor"
  635. //usage: "\n"
  636. //usage: "\n"" -u If service is not running, start it; restart if it stops"
  637. //usage: "\n"" -d If service is running, send TERM+CONT signals; do not restart it"
  638. //usage: "\n"" -o Once: if service is not running, start it; do not restart it"
  639. //usage: "\n"" -pchaitk Send STOP, CONT, HUP, ALRM, INT, TERM, KILL signal to service"
  640. //usage: "\n"" -x Exit: runsv will exit as soon as the service is down"
  641. #if ENABLE_SVC
  642. int svc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  643. int svc_main(int argc UNUSED_PARAM, char **argv)
  644. {
  645. char command[2];
  646. const char *optstring;
  647. unsigned opts;
  648. optstring = "udopchaitkx";
  649. opts = getopt32(argv, optstring);
  650. argv += optind;
  651. if (!argv[0] || !opts)
  652. bb_show_usage();
  653. argv -= 2;
  654. if (optind > 2) {
  655. argv--;
  656. argv[2] = (char*)"--";
  657. }
  658. argv[0] = (char*)"sv";
  659. argv[1] = command;
  660. command[1] = '\0';
  661. do {
  662. if (opts & 1) {
  663. int r;
  664. command[0] = *optstring;
  665. /* getopt() was already called by getopt32():
  666. * reset the libc getopt() function's internal state.
  667. */
  668. GETOPT_RESET();
  669. r = sv(argv);
  670. if (r)
  671. return 1;
  672. }
  673. optstring++;
  674. opts >>= 1;
  675. } while (opts);
  676. return 0;
  677. }
  678. #endif