Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/runit/sv.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/runit/sv.c
r1765 r2725 151 151 */ 152 152 153 /* Busyboxed by Den is Vlasenko <vda.linux@googlemail.com> */153 /* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */ 154 154 /* TODO: depends on runit_lib.c - review and reduce/eliminate */ 155 155 … … 159 159 #include "runit_lib.h" 160 160 161 static const char *acts; 162 static char **service; 163 static unsigned rc; 161 struct globals { 162 const char *acts; 163 char **service; 164 unsigned rc; 164 165 /* "Bernstein" time format: unix + 0x400000000000000aULL */ 165 static uint64_t tstart, tnow; 166 svstatus_t svstatus; 167 168 169 static void fatal_cannot(const char *m1) ATTRIBUTE_NORETURN; 166 uint64_t tstart, tnow; 167 svstatus_t svstatus; 168 } FIX_ALIASING; 169 #define G (*(struct globals*)&bb_common_bufsiz1) 170 #define acts (G.acts ) 171 #define service (G.service ) 172 #define rc (G.rc ) 173 #define tstart (G.tstart ) 174 #define tnow (G.tnow ) 175 #define svstatus (G.svstatus ) 176 #define INIT_G() do { } while (0) 177 178 179 #define str_equal(s,t) (!strcmp((s), (t))) 180 181 182 static void fatal_cannot(const char *m1) NORETURN; 170 183 static void fatal_cannot(const char *m1) 171 184 { 172 bb_perror_msg("fatal: can not %s", m1);185 bb_perror_msg("fatal: can't %s", m1); 173 186 _exit(151); 174 187 } … … 180 193 printf(": %s", strerror(errno)); 181 194 } 182 puts(""); /* will also flush the output */195 bb_putchar('\n'); /* will also flush the output */ 183 196 } 184 197 … … 212 225 int fd, r; 213 226 214 fd = open _write("supervise/ok");227 fd = open("supervise/ok", O_WRONLY|O_NDELAY); 215 228 if (fd == -1) { 216 229 if (errno == ENODEV) { … … 219 232 return 0; 220 233 } 221 warn("can not open supervise/ok");234 warn("can't open supervise/ok"); 222 235 return -1; 223 236 } 224 237 close(fd); 225 fd = open _read("supervise/status");238 fd = open("supervise/status", O_RDONLY|O_NDELAY); 226 239 if (fd == -1) { 227 warn("can not open supervise/status");240 warn("can't open supervise/status"); 228 241 return -1; 229 242 } … … 234 247 break; 235 248 case -1: 236 warn("can not read supervise/status");249 warn("can't read supervise/status"); 237 250 return -1; 238 251 default: 239 252 errno = 0; 240 warn("can not read supervise/status: bad format");253 warn("can't read supervise/status: bad format"); 241 254 return -1; 242 255 } … … 254 267 if (stat("down", &s) == -1) { 255 268 if (errno != ENOENT) { 256 bb_perror_msg(WARN"can not stat %s/down", *service);269 bb_perror_msg(WARN"can't stat %s/down", *service); 257 270 return 0; 258 271 } … … 284 297 } 285 298 286 static int status(const char *unused )299 static int status(const char *unused UNUSED_PARAM) 287 300 { 288 301 int r; 289 302 290 r = svstatus_get();291 switch (r) { case -1: case 0: return 0; }303 if (svstatus_get() <= 0) 304 return 0; 292 305 293 306 r = svstatus_print(*service); 294 307 if (chdir("log") == -1) { 295 308 if (errno != ENOENT) { 296 printf("; log: "WARN"can not change to log service directory: %s",309 printf("; log: "WARN"can't change to log service directory: %s", 297 310 strerror(errno)); 298 311 } … … 301 314 svstatus_print("log"); 302 315 } 303 puts(""); /* will also flush the output */316 bb_putchar('\n'); /* will also flush the output */ 304 317 return r; 305 318 } … … 313 326 if (stat("check", &s) == -1) { 314 327 if (errno == ENOENT) return 1; 315 bb_perror_msg(WARN"can not stat %s/check", *service);328 bb_perror_msg(WARN"can't stat %s/check", *service); 316 329 return 0; 317 330 } … … 321 334 pid = spawn(prog); 322 335 if (pid <= 0) { 323 bb_perror_msg(WARN"can not %s child %s/check", "run", *service);336 bb_perror_msg(WARN"can't %s child %s/check", "run", *service); 324 337 return 0; 325 338 } 326 while (wait_pid(&w, pid) == -1) { 327 if (errno == EINTR) continue; 328 bb_perror_msg(WARN"cannot %s child %s/check", "wait for", *service); 339 while (safe_waitpid(pid, &w, 0) == -1) { 340 bb_perror_msg(WARN"can't %s child %s/check", "wait for", *service); 329 341 return 0; 330 342 } 331 return !wait_exitcode(w);343 return WEXITSTATUS(w) == 0; 332 344 } 333 345 … … 335 347 { 336 348 int r; 337 unsigned pid ;349 unsigned pid_le32; 338 350 uint64_t timestamp; 339 351 … … 346 358 return -1; 347 359 } 348 pid = SWAP_LE32(svstatus.pid_le32);360 pid_le32 = svstatus.pid_le32; 349 361 switch (*a) { 350 362 case 'x': 351 363 return 0; 352 364 case 'u': 353 if (!pid || svstatus.run_or_finish != 1) return 0;365 if (!pid_le32 || svstatus.run_or_finish != 1) return 0; 354 366 if (!checkscript()) return 0; 355 367 break; 356 368 case 'd': 357 if (pid ) return 0;369 if (pid_le32) return 0; 358 370 break; 359 371 case 'c': 360 if (pid && !checkscript()) return 0;372 if (pid_le32 && !checkscript()) return 0; 361 373 break; 362 374 case 't': 363 if (!pid && svstatus.want == 'd') break;375 if (!pid_le32 && svstatus.want == 'd') break; 364 376 timestamp = SWAP_BE64(svstatus.time_be64); 365 if ((tstart > timestamp) || !pid || svstatus.got_term || !checkscript())377 if ((tstart > timestamp) || !pid_le32 || svstatus.got_term || !checkscript()) 366 378 return 0; 367 379 break; 368 380 case 'o': 369 381 timestamp = SWAP_BE64(svstatus.time_be64); 370 if ((!pid && tstart > timestamp) || (pid&& svstatus.want != 'd'))382 if ((!pid_le32 && tstart > timestamp) || (pid_le32 && svstatus.want != 'd')) 371 383 return 0; 372 384 } 373 385 printf(OK); 374 386 svstatus_print(*service); 375 puts(""); /* will also flush the output */387 bb_putchar('\n'); /* will also flush the output */ 376 388 return 1; 377 389 } … … 379 391 static int control(const char *a) 380 392 { 381 int fd, r; 382 393 int fd, r, l; 394 395 /* Is it an optimization? 396 It causes problems with "sv o SRV; ...; sv d SRV" 397 ('d' is not passed to SRV because its .want == 'd'): 383 398 if (svstatus_get() <= 0) 384 399 return -1; 385 400 if (svstatus.want == *a) 386 401 return 0; 387 fd = open_write("supervise/control"); 402 */ 403 fd = open("supervise/control", O_WRONLY|O_NDELAY); 388 404 if (fd == -1) { 389 405 if (errno != ENODEV) 390 warn("can not open supervise/control");406 warn("can't open supervise/control"); 391 407 else 392 408 *a == 'x' ? ok("runsv not running") : failx("runsv not running"); 393 409 return -1; 394 410 } 395 r = write(fd, a, strlen(a)); 411 l = strlen(a); 412 r = write(fd, a, l); 396 413 close(fd); 397 if (r != strlen(a)) {398 warn("can not write to supervise/control");414 if (r != l) { 415 warn("can't write to supervise/control"); 399 416 return -1; 400 417 } … … 402 419 } 403 420 404 int sv_main(int argc, char **argv) ;405 int sv_main(int argc , char **argv)421 int sv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 422 int sv_main(int argc UNUSED_PARAM, char **argv) 406 423 { 407 424 unsigned opt; 408 unsigned i, want_exit;409 425 char *x; 410 426 char *action; 411 const char *varservice = "/var/service/"; 412 unsigned services; 413 char **servicex; 427 const char *varservice = CONFIG_SV_DEFAULT_SERVICE_DIR; 414 428 unsigned waitsec = 7; 415 429 smallint kll = 0; 416 smallint verbose = 0;430 int verbose = 0; 417 431 int (*act)(const char*); 418 432 int (*cbk)(const char*); 419 433 int curdir; 434 435 INIT_G(); 420 436 421 437 xfunc_error_retval = 100; … … 426 442 if (x) waitsec = xatou(x); 427 443 428 opt = getopt32(argv, "w:v", &x); 429 if (opt & 1) waitsec = xatou(x); // -w 430 if (opt & 2) verbose = 1; // -v 431 argc -= optind; 444 opt_complementary = "w+:vv"; /* -w N, -v is a counter */ 445 opt = getopt32(argv, "w:v", &waitsec, &verbose); 432 446 argv += optind; 433 447 action = *argv++; 434 448 if (!action || !*argv) bb_show_usage(); 435 service = argv; 436 services = argc - 1; 437 438 tnow = time(0) + 0x400000000000000aULL; 449 450 tnow = time(NULL) + 0x400000000000000aULL; 439 451 tstart = tnow; 440 curdir = open _read(".");452 curdir = open(".", O_RDONLY|O_NDELAY); 441 453 if (curdir == -1) 442 454 fatal_cannot("open current directory"); … … 525 537 } 526 538 527 service x = service;528 for (i = 0; i < services; ++i) {529 if ( (**service != '/') && (**service != '.')) {539 service = argv; 540 while ((x = *service) != NULL) { 541 if (x[0] != '/' && x[0] != '.') { 530 542 if (chdir(varservice) == -1) 531 543 goto chdir_failed_0; 532 544 } 533 if (chdir( *service) == -1) {545 if (chdir(x) == -1) { 534 546 chdir_failed_0: 535 fail("can not change to service directory");547 fail("can't change to service directory"); 536 548 goto nullify_service_0; 537 549 } 538 550 if (act && (act(acts) == -1)) { 539 551 nullify_service_0: 540 *service = NULL;552 *service = (char*) -1L; /* "dead" */ 541 553 } 542 554 if (fchdir(curdir) == -1) … … 546 558 547 559 if (cbk) while (1) { 560 int want_exit; 548 561 int diff; 549 562 550 563 diff = tnow - tstart; 551 service = servicex;564 service = argv; 552 565 want_exit = 1; 553 for (i = 0; i < services; ++i, ++service) {554 if ( !*service)555 continue;556 if ( (**service != '/') && (**service != '.')) {566 while ((x = *service) != NULL) { 567 if (x == (char*) -1L) /* "dead" */ 568 goto next; 569 if (x[0] != '/' && x[0] != '.') { 557 570 if (chdir(varservice) == -1) 558 571 goto chdir_failed; 559 572 } 560 if (chdir( *service) == -1) {573 if (chdir(x) == -1) { 561 574 chdir_failed: 562 fail("can not change to service directory");575 fail("can't change to service directory"); 563 576 goto nullify_service; 564 577 } … … 569 582 printf(kll ? "kill: " : "timeout: "); 570 583 if (svstatus_get() > 0) { 571 svstatus_print( *service);584 svstatus_print(x); 572 585 ++rc; 573 586 } 574 puts(""); /* will also flush the output */587 bb_putchar('\n'); /* will also flush the output */ 575 588 if (kll) 576 589 control("k"); 577 590 nullify_service: 578 *service = NULL;591 *service = (char*) -1L; /* "dead" */ 579 592 } 580 593 if (fchdir(curdir) == -1) 581 594 fatal_cannot("change to original directory"); 595 next: 596 service++; 582 597 } 583 598 if (want_exit) break; 584 599 usleep(420000); 585 tnow = time( 0) + 0x400000000000000aULL;600 tnow = time(NULL) + 0x400000000000000aULL; 586 601 } 587 602 return rc > 99 ? 99 : rc;
Note:
See TracChangeset
for help on using the changeset viewer.