diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-07-08 16:12:10 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-07-08 16:12:10 +0200 |
commit | 1f0ab1dc6427e9340f50551d9e4f2212d03ec845 (patch) | |
tree | 72faccecb02a44c15a67ecdcca33ef5fdb8597bc | |
parent | 45963c873c4eb8d95ba327c9e21d5188d14f1fa4 (diff) | |
download | busybox-w32-1f0ab1dc6427e9340f50551d9e4f2212d03ec845.tar.gz busybox-w32-1f0ab1dc6427e9340f50551d9e4f2212d03ec845.tar.bz2 busybox-w32-1f0ab1dc6427e9340f50551d9e4f2212d03ec845.zip |
crond: code shrink
function old new delta
crond_main 1431 1472 +41
fork_job 457 389 -68
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/crond.c | 67 |
1 files changed, 31 insertions, 36 deletions
diff --git a/miscutils/crond.c b/miscutils/crond.c index 28722563c..a459c6a8c 100644 --- a/miscutils/crond.c +++ b/miscutils/crond.c | |||
@@ -29,7 +29,7 @@ | |||
29 | # define SENDMAIL "sendmail" | 29 | # define SENDMAIL "sendmail" |
30 | #endif | 30 | #endif |
31 | #ifndef SENDMAIL_ARGS | 31 | #ifndef SENDMAIL_ARGS |
32 | # define SENDMAIL_ARGS "-ti", NULL | 32 | # define SENDMAIL_ARGS "-ti" |
33 | #endif | 33 | #endif |
34 | #ifndef CRONUPDATE | 34 | #ifndef CRONUPDATE |
35 | # define CRONUPDATE "cron.update" | 35 | # define CRONUPDATE "cron.update" |
@@ -53,9 +53,8 @@ typedef struct CronLine { | |||
53 | char *cl_cmd; /* shell command */ | 53 | char *cl_cmd; /* shell command */ |
54 | pid_t cl_pid; /* >0:running, <0:needs to be started in this minute, 0:dormant */ | 54 | pid_t cl_pid; /* >0:running, <0:needs to be started in this minute, 0:dormant */ |
55 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL | 55 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
56 | int cl_empty_mail_size; /* size of mail header only */ | 56 | int cl_empty_mail_size; /* size of mail header only, 0 if no mailfile */ |
57 | char *cl_mailto; /* whom to mail results, may be NULL */ | 57 | char *cl_mailto; /* whom to mail results, may be NULL */ |
58 | smallint cl_mail_result; /* mail file is created, need to send it on completion */ | ||
59 | #endif | 58 | #endif |
60 | /* ordered by size, not in natural order. makes code smaller: */ | 59 | /* ordered by size, not in natural order. makes code smaller: */ |
61 | char cl_Dow[7]; /* 0-6, beginning sunday */ | 60 | char cl_Dow[7]; /* 0-6, beginning sunday */ |
@@ -536,13 +535,11 @@ static void change_user(struct passwd *pas) | |||
536 | // TODO: sendmail should be _run-time_ option, not compile-time! | 535 | // TODO: sendmail should be _run-time_ option, not compile-time! |
537 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL | 536 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
538 | 537 | ||
539 | //TODO: return pid (and stop passing line); | 538 | static pid_t |
540 | // stop passing mail_filename here but process it in caller | 539 | fork_job(const char *user, int mailFd, |
541 | static void | 540 | const char *prog, |
542 | fork_job(const char *user, CronLine *line, int mailFd, | 541 | const char *shell_cmd /* if NULL, we run sendmail */ |
543 | const char *prog, const char *cmd, const char *arg, | 542 | ) { |
544 | const char *mail_filename) | ||
545 | { | ||
546 | struct passwd *pas; | 543 | struct passwd *pas; |
547 | pid_t pid; | 544 | pid_t pid; |
548 | 545 | ||
@@ -563,37 +560,25 @@ fork_job(const char *user, CronLine *line, int mailFd, | |||
563 | crondlog(LVL5 "child running %s", prog); | 560 | crondlog(LVL5 "child running %s", prog); |
564 | } | 561 | } |
565 | if (mailFd >= 0) { | 562 | if (mailFd >= 0) { |
566 | xmove_fd(mailFd, mail_filename ? 1 : 0); | 563 | xmove_fd(mailFd, shell_cmd ? 1 : 0); |
567 | dup2(1, 2); | 564 | dup2(1, 2); |
568 | } | 565 | } |
569 | /* crond 3.0pl1-100 puts tasks in separate process groups */ | 566 | /* crond 3.0pl1-100 puts tasks in separate process groups */ |
570 | bb_setpgrp(); | 567 | bb_setpgrp(); |
571 | execlp(prog, prog, cmd, arg, (char *) NULL); | 568 | execlp(prog, prog, (shell_cmd ? "-c" : SENDMAIL_ARGS), shell_cmd, (char *) NULL); |
572 | crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user, prog, cmd, arg); | 569 | crondlog(ERR20 "can't execute '%s' for user %s", prog, user); |
573 | if (mail_filename) { | 570 | if (shell_cmd) { |
574 | fdprintf(1, "Exec failed: %s -c %s\n", prog, arg); | 571 | fdprintf(1, "Exec failed: %s -c %s\n", prog, shell_cmd); |
575 | } | 572 | } |
576 | _exit(EXIT_SUCCESS); | 573 | _exit(EXIT_SUCCESS); |
577 | } | 574 | } |
578 | 575 | ||
579 | line->cl_pid = pid; | ||
580 | if (pid < 0) { | 576 | if (pid < 0) { |
581 | /* FORK FAILED */ | 577 | /* FORK FAILED */ |
582 | crondlog(ERR20 "can't vfork"); | 578 | crondlog(ERR20 "can't vfork"); |
583 | err: | 579 | err: |
584 | line->cl_pid = 0; | 580 | pid = 0; |
585 | if (mail_filename) { | 581 | } /* else: PARENT, FORK SUCCESS */ |
586 | unlink(mail_filename); | ||
587 | } | ||
588 | } else { | ||
589 | /* PARENT, FORK SUCCESS */ | ||
590 | if (mail_filename) { | ||
591 | /* rename mail-file based on pid of process */ | ||
592 | char *mailFile2 = xasprintf("%s/cron.%s.%d", TMPDIR, user, (int)pid); | ||
593 | rename(mail_filename, mailFile2); // TODO: xrename? | ||
594 | free(mailFile2); | ||
595 | } | ||
596 | } | ||
597 | 582 | ||
598 | /* | 583 | /* |
599 | * Close the mail file descriptor.. we can't just leave it open in | 584 | * Close the mail file descriptor.. we can't just leave it open in |
@@ -602,6 +587,7 @@ fork_job(const char *user, CronLine *line, int mailFd, | |||
602 | if (mailFd >= 0) { | 587 | if (mailFd >= 0) { |
603 | close(mailFd); | 588 | close(mailFd); |
604 | } | 589 | } |
590 | return pid; | ||
605 | } | 591 | } |
606 | 592 | ||
607 | static void start_one_job(const char *user, CronLine *line) | 593 | static void start_one_job(const char *user, CronLine *line) |
@@ -610,7 +596,7 @@ static void start_one_job(const char *user, CronLine *line) | |||
610 | int mailFd = -1; | 596 | int mailFd = -1; |
611 | 597 | ||
612 | line->cl_pid = 0; | 598 | line->cl_pid = 0; |
613 | line->cl_mail_result = 0; | 599 | line->cl_empty_mail_size = 0; |
614 | 600 | ||
615 | if (line->cl_mailto) { | 601 | if (line->cl_mailto) { |
616 | /* Open mail file (owner is root so nobody can screw with it) */ | 602 | /* Open mail file (owner is root so nobody can screw with it) */ |
@@ -618,7 +604,6 @@ static void start_one_job(const char *user, CronLine *line) | |||
618 | mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600); | 604 | mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600); |
619 | 605 | ||
620 | if (mailFd >= 0) { | 606 | if (mailFd >= 0) { |
621 | line->cl_mail_result = 1; | ||
622 | fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_mailto, | 607 | fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_mailto, |
623 | line->cl_cmd); | 608 | line->cl_cmd); |
624 | line->cl_empty_mail_size = lseek(mailFd, 0, SEEK_CUR); | 609 | line->cl_empty_mail_size = lseek(mailFd, 0, SEEK_CUR); |
@@ -628,7 +613,17 @@ static void start_one_job(const char *user, CronLine *line) | |||
628 | } | 613 | } |
629 | } | 614 | } |
630 | 615 | ||
631 | fork_job(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_cmd, mailFile); | 616 | line->cl_pid = fork_job(user, mailFd, DEFAULT_SHELL, line->cl_cmd); |
617 | if (mailFd >= 0) { | ||
618 | if (line->cl_pid <= 0) { | ||
619 | unlink(mailFile); | ||
620 | } else { | ||
621 | /* rename mail-file based on pid of process */ | ||
622 | char *mailFile2 = xasprintf("%s/cron.%s.%d", TMPDIR, user, (int)line->cl_pid); | ||
623 | rename(mailFile, mailFile2); // TODO: xrename? | ||
624 | free(mailFile2); | ||
625 | } | ||
626 | } | ||
632 | } | 627 | } |
633 | 628 | ||
634 | /* | 629 | /* |
@@ -647,11 +642,10 @@ static void process_finished_job(const char *user, CronLine *line) | |||
647 | /* No job */ | 642 | /* No job */ |
648 | return; | 643 | return; |
649 | } | 644 | } |
650 | if (line->cl_mail_result == 0) { | 645 | if (line->cl_empty_mail_size <= 0) { |
651 | /* End of job and no mail file, or end of sendmail job */ | 646 | /* End of job and no mail file, or end of sendmail job */ |
652 | return; | 647 | return; |
653 | } | 648 | } |
654 | line->cl_mail_result = 0; | ||
655 | 649 | ||
656 | /* | 650 | /* |
657 | * End of primary job - check for mail file. | 651 | * End of primary job - check for mail file. |
@@ -673,8 +667,9 @@ static void process_finished_job(const char *user, CronLine *line) | |||
673 | close(mailFd); | 667 | close(mailFd); |
674 | return; | 668 | return; |
675 | } | 669 | } |
676 | /* if (line->cl_mailto) - always true if cl_mail_result was true */ | 670 | line->cl_empty_mail_size = 0; |
677 | fork_job(user, line, mailFd, SENDMAIL, SENDMAIL_ARGS, NULL); | 671 | /* if (line->cl_mailto) - always true if cl_empty_mail_size was nonzero */ |
672 | line->cl_pid = fork_job(user, mailFd, SENDMAIL, NULL); | ||
678 | } | 673 | } |
679 | 674 | ||
680 | #else /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */ | 675 | #else /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */ |