3
0

sv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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.sunsite.dk/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,
  32. it is searched in the default services directory /var/service/, otherwise
  33. 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, restart, shutdown, force-stop,
  36. force-reload, force-restart, force-shutdown.
  37. The sv program can be sym-linked to /etc/init.d/ to provide an LSB init
  38. script interface. The service to be controlled then is specified by the
  39. base name of the "init script".
  40. status
  41. Report the current status of the service, and the appendant log service
  42. if available, to standard output.
  43. up
  44. If the service is not running, start it. If the service stops, restart it.
  45. down
  46. If the service is running, send it the TERM signal, and the CONT signal.
  47. If ./run exits, start ./finish if it exists. After it stops, do not
  48. restart service.
  49. once
  50. If the service is not running, start it. Do not restart it if it stops.
  51. pause cont hup alarm interrupt quit 1 2 term kill
  52. If the service is running, send it the STOP, CONT, HUP, ALRM, INT, QUIT,
  53. USR1, USR2, TERM, or KILL signal respectively.
  54. exit
  55. If the service is running, send it the TERM signal, and the CONT signal.
  56. Do not restart the service. If the service is down, and no log service
  57. exists, runsv(8) exits. If the service is down and a log service exists,
  58. send the TERM signal to the log service. If the log service is down,
  59. runsv(8) exits. This command is ignored if it is given to an appendant
  60. log service.
  61. sv actually looks only at the first character of above commands.
  62. status
  63. Same as status.
  64. start
  65. Same as up, but wait up to 7 seconds for the command to take effect.
  66. Then report the status or timeout. If the script ./check exists in
  67. the service directory, sv runs this script to check whether the service
  68. is up and available; it's considered to be available if ./check exits
  69. with 0.
  70. stop
  71. Same as down, but wait up to 7 seconds for the service to become down.
  72. Then report the status or timeout.
  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. Additional Commands
  101. check
  102. Check for the service to be in the state that's been requested. Wait up to
  103. 7 seconds for the service to reach the requested state, then report
  104. the status or timeout. If the requested state of the service is up,
  105. and the script ./check exists in the service directory, sv runs
  106. this script to check whether the service is up and running;
  107. it's considered to be up if ./check exits with 0.
  108. Options
  109. -v
  110. wait up to 7 seconds for the command to take effect.
  111. Then report the status or timeout.
  112. -w sec
  113. Override the default timeout of 7 seconds with sec seconds. Implies -v.
  114. Environment
  115. SVDIR
  116. The environment variable $SVDIR overrides the default services directory
  117. /var/service.
  118. SVWAIT
  119. The environment variable $SVWAIT overrides the default 7 seconds to wait
  120. for a command to take effect. It is overridden by the -w option.
  121. Exit Codes
  122. sv exits 0, if the command was successfully sent to all services, and,
  123. if it was told to wait, the command has taken effect to all services.
  124. For each service that caused an error (e.g. the directory is not
  125. controlled by a runsv(8) process, or sv timed out while waiting),
  126. sv increases the exit code by one and exits non zero. The maximum
  127. is 99. sv exits 100 on error.
  128. */
  129. /* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
  130. /* TODO: depends on runit_lib.c - review and reduce/eliminate */
  131. #include <sys/poll.h>
  132. #include <sys/file.h>
  133. #include "libbb.h"
  134. #include "runit_lib.h"
  135. static const char *acts;
  136. static char **service;
  137. static unsigned rc;
  138. static struct taia tstart, tnow;
  139. static char svstatus[20];
  140. #define usage() bb_show_usage()
  141. static void fatal_cannot(const char *m1) ATTRIBUTE_NORETURN;
  142. static void fatal_cannot(const char *m1)
  143. {
  144. bb_perror_msg("fatal: cannot %s", m1);
  145. _exit(151);
  146. }
  147. static void out(const char *p, const char *m1)
  148. {
  149. printf("%s%s: %s", p, *service, m1);
  150. if (errno) {
  151. printf(": %s", strerror(errno));
  152. }
  153. puts(""); /* will also flush the output */
  154. }
  155. #define WARN "warning: "
  156. #define OK "ok: "
  157. static void fail(const char *m1)
  158. {
  159. ++rc;
  160. out("fail: ", m1);
  161. }
  162. static void failx(const char *m1)
  163. {
  164. errno = 0;
  165. fail(m1);
  166. }
  167. static void warn_cannot(const char *m1)
  168. {
  169. ++rc;
  170. out("warning: cannot ", m1);
  171. }
  172. static void warnx_cannot(const char *m1)
  173. {
  174. errno = 0;
  175. warn_cannot(m1);
  176. }
  177. static void ok(const char *m1)
  178. {
  179. errno = 0;
  180. out(OK, m1);
  181. }
  182. static int svstatus_get(void)
  183. {
  184. int fd, r;
  185. fd = open_write("supervise/ok");
  186. if (fd == -1) {
  187. if (errno == ENODEV) {
  188. *acts == 'x' ? ok("runsv not running")
  189. : failx("runsv not running");
  190. return 0;
  191. }
  192. warn_cannot("open supervise/ok");
  193. return -1;
  194. }
  195. close(fd);
  196. fd = open_read("supervise/status");
  197. if (fd == -1) {
  198. warn_cannot("open supervise/status");
  199. return -1;
  200. }
  201. r = read(fd, svstatus, 20);
  202. close(fd);
  203. switch (r) {
  204. case 20: break;
  205. case -1: warn_cannot("read supervise/status"); return -1;
  206. default: warnx_cannot("read supervise/status: bad format"); return -1;
  207. }
  208. return 1;
  209. }
  210. static unsigned svstatus_print(const char *m)
  211. {
  212. long diff;
  213. int pid;
  214. int normallyup = 0;
  215. struct stat s;
  216. struct tai tstatus;
  217. if (stat("down", &s) == -1) {
  218. if (errno != ENOENT) {
  219. bb_perror_msg(WARN"cannot stat %s/down", *service);
  220. return 0;
  221. }
  222. normallyup = 1;
  223. }
  224. pid = (unsigned char) svstatus[15];
  225. pid <<= 8; pid += (unsigned char)svstatus[14];
  226. pid <<= 8; pid += (unsigned char)svstatus[13];
  227. pid <<= 8; pid += (unsigned char)svstatus[12];
  228. tai_unpack(svstatus, &tstatus);
  229. if (pid) {
  230. switch (svstatus[19]) {
  231. case 1: printf("run: "); break;
  232. case 2: printf("finish: "); break;
  233. }
  234. printf("%s: (pid %d) ", m, pid);
  235. } else {
  236. printf("down: %s: ", m);
  237. }
  238. diff = tnow.sec.x - tstatus.x;
  239. printf("%lds", (diff < 0 ? 0L : diff));
  240. if (pid) {
  241. if (!normallyup) printf(", normally down");
  242. if (svstatus[16]) printf(", paused");
  243. if (svstatus[17] == 'd') printf(", want down");
  244. if (svstatus[18]) printf(", got TERM");
  245. } else {
  246. if (normallyup) printf(", normally up");
  247. if (svstatus[17] == 'u') printf(", want up");
  248. }
  249. return pid ? 1 : 2;
  250. }
  251. static int status(const char *unused)
  252. {
  253. int r;
  254. r = svstatus_get();
  255. switch (r) { case -1: case 0: return 0; }
  256. r = svstatus_print(*service);
  257. if (chdir("log") == -1) {
  258. if (errno != ENOENT) {
  259. printf("; log: "WARN"cannot change to log service directory: %s",
  260. strerror(errno));
  261. }
  262. } else if (svstatus_get()) {
  263. printf("; ");
  264. svstatus_print("log");
  265. }
  266. puts(""); /* will also flush the output */
  267. return r;
  268. }
  269. static int checkscript(void)
  270. {
  271. char *prog[2];
  272. struct stat s;
  273. int pid, w;
  274. if (stat("check", &s) == -1) {
  275. if (errno == ENOENT) return 1;
  276. bb_perror_msg(WARN"cannot stat %s/check", *service);
  277. return 0;
  278. }
  279. /* if (!(s.st_mode & S_IXUSR)) return 1; */
  280. prog[0] = (char*)"./check";
  281. prog[1] = NULL;
  282. pid = spawn(prog);
  283. if (pid <= 0) {
  284. bb_perror_msg(WARN"cannot %s child %s/check", "run", *service);
  285. return 0;
  286. }
  287. while (wait_pid(&w, pid) == -1) {
  288. if (errno == EINTR) continue;
  289. bb_perror_msg(WARN"cannot %s child %s/check", "wait for", *service);
  290. return 0;
  291. }
  292. return !wait_exitcode(w);
  293. }
  294. static int check(const char *a)
  295. {
  296. int r;
  297. unsigned pid;
  298. struct tai tstatus;
  299. r = svstatus_get();
  300. if (r == -1)
  301. return -1;
  302. if (r == 0) {
  303. if (*a == 'x')
  304. return 1;
  305. return -1;
  306. }
  307. pid = (unsigned char)svstatus[15];
  308. pid <<= 8; pid += (unsigned char)svstatus[14];
  309. pid <<= 8; pid += (unsigned char)svstatus[13];
  310. pid <<= 8; pid += (unsigned char)svstatus[12];
  311. switch (*a) {
  312. case 'x':
  313. return 0;
  314. case 'u':
  315. if (!pid || svstatus[19] != 1) return 0;
  316. if (!checkscript()) return 0;
  317. break;
  318. case 'd':
  319. if (pid) return 0;
  320. break;
  321. case 'c':
  322. if (pid && !checkscript()) return 0;
  323. break;
  324. case 't':
  325. if (!pid && svstatus[17] == 'd') break;
  326. tai_unpack(svstatus, &tstatus);
  327. if ((tstart.sec.x > tstatus.x) || !pid || svstatus[18] || !checkscript())
  328. return 0;
  329. break;
  330. case 'o':
  331. tai_unpack(svstatus, &tstatus);
  332. if ((!pid && tstart.sec.x > tstatus.x) || (pid && svstatus[17] != 'd'))
  333. return 0;
  334. }
  335. printf(OK);
  336. svstatus_print(*service);
  337. puts(""); /* will also flush the output */
  338. return 1;
  339. }
  340. static int control(const char *a)
  341. {
  342. int fd, r;
  343. if (svstatus_get() <= 0) return -1;
  344. if (svstatus[17] == *a) return 0;
  345. fd = open_write("supervise/control");
  346. if (fd == -1) {
  347. if (errno != ENODEV)
  348. warn_cannot("open supervise/control");
  349. else
  350. *a == 'x' ? ok("runsv not running") : failx("runsv not running");
  351. return -1;
  352. }
  353. r = write(fd, a, strlen(a));
  354. close(fd);
  355. if (r != strlen(a)) {
  356. warn_cannot("write to supervise/control");
  357. return -1;
  358. }
  359. return 1;
  360. }
  361. int sv_main(int argc, char **argv);
  362. int sv_main(int argc, char **argv)
  363. {
  364. unsigned opt;
  365. unsigned i, want_exit;
  366. char *x;
  367. char *action;
  368. const char *varservice = "/var/service/";
  369. unsigned services;
  370. char **servicex;
  371. unsigned long waitsec = 7;
  372. smallint kll = 0;
  373. smallint verbose = 0;
  374. int (*act)(const char*);
  375. int (*cbk)(const char*);
  376. int curdir;
  377. xfunc_error_retval = 100;
  378. x = getenv("SVDIR");
  379. if (x) varservice = x;
  380. x = getenv("SVWAIT");
  381. if (x) waitsec = xatoul(x);
  382. opt = getopt32(argc, argv, "w:v", &x);
  383. if (opt & 1) waitsec = xatoul(x); // -w
  384. if (opt & 2) verbose = 1; // -v
  385. argc -= optind;
  386. argv += optind;
  387. action = *argv++;
  388. if (!action || !*argv) usage();
  389. service = argv;
  390. services = argc - 1;
  391. taia_now(&tnow);
  392. tstart = tnow;
  393. curdir = open_read(".");
  394. if (curdir == -1)
  395. fatal_cannot("open current directory");
  396. act = &control;
  397. acts = "s";
  398. cbk = &check;
  399. switch (*action) {
  400. case 'x':
  401. case 'e':
  402. acts = "x";
  403. if (!verbose) cbk = NULL;
  404. break;
  405. case 'X':
  406. case 'E':
  407. acts = "x";
  408. kll = 1;
  409. break;
  410. case 'D':
  411. acts = "d";
  412. kll = 1;
  413. break;
  414. case 'T':
  415. acts = "tc";
  416. kll = 1;
  417. break;
  418. case 'c':
  419. if (!str_diff(action, "check")) {
  420. act = NULL;
  421. acts = "c";
  422. break;
  423. }
  424. case 'u': case 'd': case 'o': case 't': case 'p': case 'h':
  425. case 'a': case 'i': case 'k': case 'q': case '1': case '2':
  426. action[1] = '\0';
  427. acts = action;
  428. if (!verbose) cbk = NULL;
  429. break;
  430. case 's':
  431. if (!str_diff(action, "shutdown")) {
  432. acts = "x";
  433. break;
  434. }
  435. if (!str_diff(action, "start")) {
  436. acts = "u";
  437. break;
  438. }
  439. if (!str_diff(action, "stop")) {
  440. acts = "d";
  441. break;
  442. }
  443. /* "status" */
  444. act = &status;
  445. cbk = NULL;
  446. break;
  447. case 'r':
  448. if (!str_diff(action, "restart")) {
  449. acts = "tcu";
  450. break;
  451. }
  452. usage();
  453. case 'f':
  454. if (!str_diff(action, "force-reload")) {
  455. acts = "tc";
  456. kll = 1;
  457. break;
  458. }
  459. if (!str_diff(action, "force-restart")) {
  460. acts = "tcu";
  461. kll = 1;
  462. break;
  463. }
  464. if (!str_diff(action, "force-shutdown")) {
  465. acts = "x";
  466. kll = 1;
  467. break;
  468. }
  469. if (!str_diff(action, "force-stop")) {
  470. acts = "d";
  471. kll = 1;
  472. break;
  473. }
  474. default:
  475. usage();
  476. }
  477. servicex = service;
  478. for (i = 0; i < services; ++i) {
  479. if ((**service != '/') && (**service != '.')) {
  480. if (chdir(varservice) == -1)
  481. goto chdir_failed_0;
  482. }
  483. if (chdir(*service) == -1) {
  484. chdir_failed_0:
  485. fail("cannot change to service directory");
  486. goto nullify_service_0;
  487. }
  488. if (act && (act(acts) == -1)) {
  489. nullify_service_0:
  490. *service = NULL;
  491. }
  492. if (fchdir(curdir) == -1)
  493. fatal_cannot("change to original directory");
  494. service++;
  495. }
  496. if (cbk) while (1) {
  497. //struct taia tdiff;
  498. long diff;
  499. //taia_sub(&tdiff, &tnow, &tstart);
  500. diff = tnow.sec.x - tstart.sec.x;
  501. service = servicex;
  502. want_exit = 1;
  503. for (i = 0; i < services; ++i, ++service) {
  504. if (!*service)
  505. continue;
  506. if ((**service != '/') && (**service != '.')) {
  507. if (chdir(varservice) == -1)
  508. goto chdir_failed;
  509. }
  510. if (chdir(*service) == -1) {
  511. chdir_failed:
  512. fail("cannot change to service directory");
  513. goto nullify_service;
  514. }
  515. if (cbk(acts) != 0)
  516. goto nullify_service;
  517. want_exit = 0;
  518. if (diff >= waitsec) {
  519. printf(kll ? "kill: " : "timeout: ");
  520. if (svstatus_get() > 0) {
  521. svstatus_print(*service);
  522. ++rc;
  523. }
  524. puts(""); /* will also flush the output */
  525. if (kll)
  526. control("k");
  527. nullify_service:
  528. *service = NULL;
  529. }
  530. if (fchdir(curdir) == -1)
  531. fatal_cannot("change to original directory");
  532. }
  533. if (want_exit) break;
  534. usleep(420000);
  535. taia_now(&tnow);
  536. }
  537. return rc > 99 ? 99 : rc;
  538. }