sv.c 19 KB

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