aboutsummaryrefslogtreecommitdiff
path: root/runit/sv.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-08-20 17:27:40 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-08-20 17:27:40 +0000
commit45946f8b513d9c292613ac08c3ddf4a89b915752 (patch)
treed6ea51887431e4261a114b95989950d1973d3227 /runit/sv.c
parent63db27f9f4c0ec8b9abe3c32c8d699be0781ffd6 (diff)
downloadbusybox-w32-45946f8b513d9c292613ac08c3ddf4a89b915752.tar.gz
busybox-w32-45946f8b513d9c292613ac08c3ddf4a89b915752.tar.bz2
busybox-w32-45946f8b513d9c292613ac08c3ddf4a89b915752.zip
runit/*: get rid of tai[a] time abstraction, it's too bloaty.
text data bss dec hex filename 772537 1058 11092 784687 bf92f busybox.t0/busybox 772459 1058 11060 784577 bf8c1 busybox.t1/busybox 772326 1058 11028 784412 bf81c busybox.t2/busybox 772158 1058 10980 784196 bf744 busybox.t3/busybox 771490 1055 10988 783533 bf4ad busybox.t4/busybox
Diffstat (limited to 'runit/sv.c')
-rw-r--r--runit/sv.c128
1 files changed, 62 insertions, 66 deletions
diff --git a/runit/sv.c b/runit/sv.c
index e9bfc75c5..c87f565cc 100644
--- a/runit/sv.c
+++ b/runit/sv.c
@@ -161,10 +161,10 @@ Exit Codes
161static const char *acts; 161static const char *acts;
162static char **service; 162static char **service;
163static unsigned rc; 163static unsigned rc;
164static struct taia tstart, tnow; 164/* "Bernstein" time format: unix + 0x400000000000000aULL */
165static char svstatus[20]; 165static uint64_t tstart, tnow;
166svstatus_t svstatus;
166 167
167#define usage() bb_show_usage()
168 168
169static void fatal_cannot(const char *m1) ATTRIBUTE_NORETURN; 169static void fatal_cannot(const char *m1) ATTRIBUTE_NORETURN;
170static void fatal_cannot(const char *m1) 170static void fatal_cannot(const char *m1)
@@ -195,15 +195,11 @@ static void failx(const char *m1)
195 errno = 0; 195 errno = 0;
196 fail(m1); 196 fail(m1);
197} 197}
198static void warn_cannot(const char *m1) 198static void warn(const char *m1)
199{ 199{
200 ++rc; 200 ++rc;
201 out("warning: cannot ", m1); 201 /* "warning: <service>: <m1>\n" */
202} 202 out("warning: ", m1);
203static void warnx_cannot(const char *m1)
204{
205 errno = 0;
206 warn_cannot(m1);
207} 203}
208static void ok(const char *m1) 204static void ok(const char *m1)
209{ 205{
@@ -222,32 +218,38 @@ static int svstatus_get(void)
222 : failx("runsv not running"); 218 : failx("runsv not running");
223 return 0; 219 return 0;
224 } 220 }
225 warn_cannot("open supervise/ok"); 221 warn("cannot open supervise/ok");
226 return -1; 222 return -1;
227 } 223 }
228 close(fd); 224 close(fd);
229 fd = open_read("supervise/status"); 225 fd = open_read("supervise/status");
230 if (fd == -1) { 226 if (fd == -1) {
231 warn_cannot("open supervise/status"); 227 warn("cannot open supervise/status");
232 return -1; 228 return -1;
233 } 229 }
234 r = read(fd, svstatus, 20); 230 r = read(fd, &svstatus, 20);
235 close(fd); 231 close(fd);
236 switch (r) { 232 switch (r) {
237 case 20: break; 233 case 20:
238 case -1: warn_cannot("read supervise/status"); return -1; 234 break;
239 default: warnx_cannot("read supervise/status: bad format"); return -1; 235 case -1:
236 warn("cannot read supervise/status");
237 return -1;
238 default:
239 errno = 0;
240 warn("cannot read supervise/status: bad format");
241 return -1;
240 } 242 }
241 return 1; 243 return 1;
242} 244}
243 245
244static unsigned svstatus_print(const char *m) 246static unsigned svstatus_print(const char *m)
245{ 247{
246 long diff; 248 int diff;
247 int pid; 249 int pid;
248 int normallyup = 0; 250 int normallyup = 0;
249 struct stat s; 251 struct stat s;
250 struct tai tstatus; 252 uint64_t timestamp;
251 253
252 if (stat("down", &s) == -1) { 254 if (stat("down", &s) == -1) {
253 if (errno != ENOENT) { 255 if (errno != ENOENT) {
@@ -256,13 +258,10 @@ static unsigned svstatus_print(const char *m)
256 } 258 }
257 normallyup = 1; 259 normallyup = 1;
258 } 260 }
259 pid = (unsigned char) svstatus[15]; 261 pid = SWAP_LE32(svstatus.pid_le32);
260 pid <<= 8; pid += (unsigned char)svstatus[14]; 262 timestamp = SWAP_BE64(svstatus.time_be64);
261 pid <<= 8; pid += (unsigned char)svstatus[13];
262 pid <<= 8; pid += (unsigned char)svstatus[12];
263 tai_unpack(svstatus, &tstatus);
264 if (pid) { 263 if (pid) {
265 switch (svstatus[19]) { 264 switch (svstatus.run_or_finish) {
266 case 1: printf("run: "); break; 265 case 1: printf("run: "); break;
267 case 2: printf("finish: "); break; 266 case 2: printf("finish: "); break;
268 } 267 }
@@ -270,16 +269,16 @@ static unsigned svstatus_print(const char *m)
270 } else { 269 } else {
271 printf("down: %s: ", m); 270 printf("down: %s: ", m);
272 } 271 }
273 diff = tnow.sec.x - tstatus.x; 272 diff = tnow - timestamp;
274 printf("%lds", (diff < 0 ? 0L : diff)); 273 printf("%us", (diff < 0 ? 0 : diff));
275 if (pid) { 274 if (pid) {
276 if (!normallyup) printf(", normally down"); 275 if (!normallyup) printf(", normally down");
277 if (svstatus[16]) printf(", paused"); 276 if (svstatus.paused) printf(", paused");
278 if (svstatus[17] == 'd') printf(", want down"); 277 if (svstatus.want == 'd') printf(", want down");
279 if (svstatus[18]) printf(", got TERM"); 278 if (svstatus.got_term) printf(", got TERM");
280 } else { 279 } else {
281 if (normallyup) printf(", normally up"); 280 if (normallyup) printf(", normally up");
282 if (svstatus[17] == 'u') printf(", want up"); 281 if (svstatus.want == 'u') printf(", want up");
283 } 282 }
284 return pid ? 1 : 2; 283 return pid ? 1 : 2;
285} 284}
@@ -336,7 +335,7 @@ static int check(const char *a)
336{ 335{
337 int r; 336 int r;
338 unsigned pid; 337 unsigned pid;
339 struct tai tstatus; 338 uint64_t timestamp;
340 339
341 r = svstatus_get(); 340 r = svstatus_get();
342 if (r == -1) 341 if (r == -1)
@@ -346,15 +345,12 @@ static int check(const char *a)
346 return 1; 345 return 1;
347 return -1; 346 return -1;
348 } 347 }
349 pid = (unsigned char)svstatus[15]; 348 pid = SWAP_LE32(svstatus.pid_le32);
350 pid <<= 8; pid += (unsigned char)svstatus[14];
351 pid <<= 8; pid += (unsigned char)svstatus[13];
352 pid <<= 8; pid += (unsigned char)svstatus[12];
353 switch (*a) { 349 switch (*a) {
354 case 'x': 350 case 'x':
355 return 0; 351 return 0;
356 case 'u': 352 case 'u':
357 if (!pid || svstatus[19] != 1) return 0; 353 if (!pid || svstatus.run_or_finish != 1) return 0;
358 if (!checkscript()) return 0; 354 if (!checkscript()) return 0;
359 break; 355 break;
360 case 'd': 356 case 'd':
@@ -364,14 +360,14 @@ static int check(const char *a)
364 if (pid && !checkscript()) return 0; 360 if (pid && !checkscript()) return 0;
365 break; 361 break;
366 case 't': 362 case 't':
367 if (!pid && svstatus[17] == 'd') break; 363 if (!pid && svstatus.want == 'd') break;
368 tai_unpack(svstatus, &tstatus); 364 timestamp = SWAP_BE64(svstatus.time_be64);
369 if ((tstart.sec.x > tstatus.x) || !pid || svstatus[18] || !checkscript()) 365 if ((tstart > timestamp) || !pid || svstatus.got_term || !checkscript())
370 return 0; 366 return 0;
371 break; 367 break;
372 case 'o': 368 case 'o':
373 tai_unpack(svstatus, &tstatus); 369 timestamp = SWAP_BE64(svstatus.time_be64);
374 if ((!pid && tstart.sec.x > tstatus.x) || (pid && svstatus[17] != 'd')) 370 if ((!pid && tstart > timestamp) || (pid && svstatus.want != 'd'))
375 return 0; 371 return 0;
376 } 372 }
377 printf(OK); 373 printf(OK);
@@ -384,12 +380,14 @@ static int control(const char *a)
384{ 380{
385 int fd, r; 381 int fd, r;
386 382
387 if (svstatus_get() <= 0) return -1; 383 if (svstatus_get() <= 0)
388 if (svstatus[17] == *a) return 0; 384 return -1;
385 if (svstatus.want == *a)
386 return 0;
389 fd = open_write("supervise/control"); 387 fd = open_write("supervise/control");
390 if (fd == -1) { 388 if (fd == -1) {
391 if (errno != ENODEV) 389 if (errno != ENODEV)
392 warn_cannot("open supervise/control"); 390 warn("cannot open supervise/control");
393 else 391 else
394 *a == 'x' ? ok("runsv not running") : failx("runsv not running"); 392 *a == 'x' ? ok("runsv not running") : failx("runsv not running");
395 return -1; 393 return -1;
@@ -397,7 +395,7 @@ static int control(const char *a)
397 r = write(fd, a, strlen(a)); 395 r = write(fd, a, strlen(a));
398 close(fd); 396 close(fd);
399 if (r != strlen(a)) { 397 if (r != strlen(a)) {
400 warn_cannot("write to supervise/control"); 398 warn("cannot write to supervise/control");
401 return -1; 399 return -1;
402 } 400 }
403 return 1; 401 return 1;
@@ -413,7 +411,7 @@ int sv_main(int argc, char **argv)
413 const char *varservice = "/var/service/"; 411 const char *varservice = "/var/service/";
414 unsigned services; 412 unsigned services;
415 char **servicex; 413 char **servicex;
416 unsigned long waitsec = 7; 414 unsigned waitsec = 7;
417 smallint kll = 0; 415 smallint kll = 0;
418 smallint verbose = 0; 416 smallint verbose = 0;
419 int (*act)(const char*); 417 int (*act)(const char*);
@@ -425,19 +423,19 @@ int sv_main(int argc, char **argv)
425 x = getenv("SVDIR"); 423 x = getenv("SVDIR");
426 if (x) varservice = x; 424 if (x) varservice = x;
427 x = getenv("SVWAIT"); 425 x = getenv("SVWAIT");
428 if (x) waitsec = xatoul(x); 426 if (x) waitsec = xatou(x);
429 427
430 opt = getopt32(argv, "w:v", &x); 428 opt = getopt32(argv, "w:v", &x);
431 if (opt & 1) waitsec = xatoul(x); // -w 429 if (opt & 1) waitsec = xatou(x); // -w
432 if (opt & 2) verbose = 1; // -v 430 if (opt & 2) verbose = 1; // -v
433 argc -= optind; 431 argc -= optind;
434 argv += optind; 432 argv += optind;
435 action = *argv++; 433 action = *argv++;
436 if (!action || !*argv) usage(); 434 if (!action || !*argv) bb_show_usage();
437 service = argv; 435 service = argv;
438 services = argc - 1; 436 services = argc - 1;
439 437
440 taia_now(&tnow); 438 tnow = time(0) + 0x400000000000000aULL;
441 tstart = tnow; 439 tstart = tnow;
442 curdir = open_read("."); 440 curdir = open_read(".");
443 if (curdir == -1) 441 if (curdir == -1)
@@ -467,7 +465,7 @@ int sv_main(int argc, char **argv)
467 kll = 1; 465 kll = 1;
468 break; 466 break;
469 case 'c': 467 case 'c':
470 if (!str_diff(action, "check")) { 468 if (str_equal(action, "check")) {
471 act = NULL; 469 act = NULL;
472 acts = "c"; 470 acts = "c";
473 break; 471 break;
@@ -479,15 +477,15 @@ int sv_main(int argc, char **argv)
479 if (!verbose) cbk = NULL; 477 if (!verbose) cbk = NULL;
480 break; 478 break;
481 case 's': 479 case 's':
482 if (!str_diff(action, "shutdown")) { 480 if (str_equal(action, "shutdown")) {
483 acts = "x"; 481 acts = "x";
484 break; 482 break;
485 } 483 }
486 if (!str_diff(action, "start")) { 484 if (str_equal(action, "start")) {
487 acts = "u"; 485 acts = "u";
488 break; 486 break;
489 } 487 }
490 if (!str_diff(action, "stop")) { 488 if (str_equal(action, "stop")) {
491 acts = "d"; 489 acts = "d";
492 break; 490 break;
493 } 491 }
@@ -496,34 +494,34 @@ int sv_main(int argc, char **argv)
496 cbk = NULL; 494 cbk = NULL;
497 break; 495 break;
498 case 'r': 496 case 'r':
499 if (!str_diff(action, "restart")) { 497 if (str_equal(action, "restart")) {
500 acts = "tcu"; 498 acts = "tcu";
501 break; 499 break;
502 } 500 }
503 usage(); 501 bb_show_usage();
504 case 'f': 502 case 'f':
505 if (!str_diff(action, "force-reload")) { 503 if (str_equal(action, "force-reload")) {
506 acts = "tc"; 504 acts = "tc";
507 kll = 1; 505 kll = 1;
508 break; 506 break;
509 } 507 }
510 if (!str_diff(action, "force-restart")) { 508 if (str_equal(action, "force-restart")) {
511 acts = "tcu"; 509 acts = "tcu";
512 kll = 1; 510 kll = 1;
513 break; 511 break;
514 } 512 }
515 if (!str_diff(action, "force-shutdown")) { 513 if (str_equal(action, "force-shutdown")) {
516 acts = "x"; 514 acts = "x";
517 kll = 1; 515 kll = 1;
518 break; 516 break;
519 } 517 }
520 if (!str_diff(action, "force-stop")) { 518 if (str_equal(action, "force-stop")) {
521 acts = "d"; 519 acts = "d";
522 kll = 1; 520 kll = 1;
523 break; 521 break;
524 } 522 }
525 default: 523 default:
526 usage(); 524 bb_show_usage();
527 } 525 }
528 526
529 servicex = service; 527 servicex = service;
@@ -547,11 +545,9 @@ int sv_main(int argc, char **argv)
547 } 545 }
548 546
549 if (cbk) while (1) { 547 if (cbk) while (1) {
550 //struct taia tdiff; 548 int diff;
551 long diff;
552 549
553 //taia_sub(&tdiff, &tnow, &tstart); 550 diff = tnow - tstart;
554 diff = tnow.sec.x - tstart.sec.x;
555 service = servicex; 551 service = servicex;
556 want_exit = 1; 552 want_exit = 1;
557 for (i = 0; i < services; ++i, ++service) { 553 for (i = 0; i < services; ++i, ++service) {
@@ -586,7 +582,7 @@ int sv_main(int argc, char **argv)
586 } 582 }
587 if (want_exit) break; 583 if (want_exit) break;
588 usleep(420000); 584 usleep(420000);
589 taia_now(&tnow); 585 tnow = time(0) + 0x400000000000000aULL;
590 } 586 }
591 return rc > 99 ? 99 : rc; 587 return rc > 99 ? 99 : rc;
592} 588}