Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/runit/runsv.c


Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/runit/runsv.c

    r1765 r2725  
    2626*/
    2727
    28 /* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
     28/* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
    2929/* TODO: depends on runit_lib.c - review and reduce/eliminate */
    3030
     
    6161/* Compare possibly overflowing unsigned counters */
    6262#define LESS(a,b) ((int)((unsigned)(b) - (unsigned)(a)) > 0)
    63 
    64 static int selfpipe[2];
    6563
    6664/* state */
     
    8179    smallint state;
    8280    smallint ctrl;
    83     smallint want;
     81    smallint sd_want;
    8482    smallint islog;
    8583    struct timespec start;
     
    8785    int fdcontrol;
    8886    int fdcontrolwrite;
     87    int wstat;
    8988};
    9089
    91 static struct svdir svd[2];
    92 static smallint sigterm;
    93 static smallint haslog;
    94 static smallint pidchanged = 1;
    95 static int logpipe[2];
    96 static char *dir;
     90struct globals {
     91    smallint haslog;
     92    smallint sigterm;
     93    smallint pidchanged;
     94    struct fd_pair selfpipe;
     95    struct fd_pair logpipe;
     96    char *dir;
     97    struct svdir svd[2];
     98} FIX_ALIASING;
     99#define G (*(struct globals*)&bb_common_bufsiz1)
     100#define haslog       (G.haslog      )
     101#define sigterm      (G.sigterm     )
     102#define pidchanged   (G.pidchanged  )
     103#define selfpipe     (G.selfpipe    )
     104#define logpipe      (G.logpipe     )
     105#define dir          (G.dir         )
     106#define svd          (G.svd         )
     107#define INIT_G() do { \
     108    pidchanged = 1; \
     109} while (0)
    97110
    98111static void fatal2_cannot(const char *m1, const char *m2)
     
    116129}
    117130
    118 static void s_child(int sig_no)
    119 {
    120     write(selfpipe[1], "", 1);
    121 }
    122 
    123 static void s_term(int sig_no)
     131static void s_child(int sig_no UNUSED_PARAM)
     132{
     133    write(selfpipe.wr, "", 1);
     134}
     135
     136static void s_term(int sig_no UNUSED_PARAM)
    124137{
    125138    sigterm = 1;
    126     write(selfpipe[1], "", 1); /* XXX */
    127 }
    128 
    129 static char *add_str(char *p, const char *to_add)
     139    write(selfpipe.wr, "", 1); /* XXX */
     140}
     141
     142/* libbb candidate */
     143static char *bb_stpcpy(char *p, const char *to_add)
    130144{
    131145    while ((*p = *to_add) != '\0') {
     
    138152static int open_trunc_or_warn(const char *name)
    139153{
    140     int fd = open_trunc(name);
     154    /* Why O_NDELAY? */
     155    int fd = open(name, O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT, 0644);
    141156    if (fd < 0)
    142157        bb_perror_msg("%s: warning: cannot open %s",
    143158                dir, name);
    144159    return fd;
    145 }
    146 
    147 static int rename_or_warn(const char *old, const char *new)
    148 {
    149     if (rename(old, new) == -1) {
    150         bb_perror_msg("%s: warning: cannot rename %s to %s",
    151                 dir, old, new);
    152         return -1;
    153     }
    154     return 0;
    155160}
    156161
     
    188193        switch (s->state) {
    189194        case S_DOWN:
    190             p = add_str(p, "down");
     195            p = bb_stpcpy(p, "down");
    191196            break;
    192197        case S_RUN:
    193             p = add_str(p, "run");
     198            p = bb_stpcpy(p, "run");
    194199            break;
    195200        case S_FINISH:
    196             p = add_str(p, "finish");
     201            p = bb_stpcpy(p, "finish");
    197202            break;
    198203        }
    199         if (s->ctrl & C_PAUSE) p = add_str(p, ", paused");
    200         if (s->ctrl & C_TERM) p = add_str(p, ", got TERM");
     204        if (s->ctrl & C_PAUSE)
     205            p = bb_stpcpy(p, ", paused");
     206        if (s->ctrl & C_TERM)
     207            p = bb_stpcpy(p, ", got TERM");
    201208        if (s->state != S_DOWN)
    202             switch (s->want) {
     209            switch (s->sd_want) {
    203210            case W_DOWN:
    204                 p = add_str(p, ", want down");
     211                p = bb_stpcpy(p, ", want down");
    205212                break;
    206213            case W_EXIT:
    207                 p = add_str(p, ", want exit");
     214                p = bb_stpcpy(p, ", want exit");
    208215                break;
    209216            }
     
    223230    if (s->ctrl & C_PAUSE)
    224231        status.paused = 1;
    225     if (s->want == W_UP)
     232    if (s->sd_want == W_UP)
    226233        status.want = 'u';
    227234    else
     
    246253static unsigned custom(struct svdir *s, char c)
    247254{
    248     int pid;
     255    pid_t pid;
    249256    int w;
    250257    char a[10];
    251258    struct stat st;
    252     char *prog[2];
    253 
    254     if (s->islog) return 0;
     259
     260    if (s->islog)
     261        return 0;
    255262    strcpy(a, "control/?");
    256     a[8] = c;
     263    a[8] = c; /* replace '?' */
    257264    if (stat(a, &st) == 0) {
    258265        if (st.st_mode & S_IXUSR) {
    259             pid = fork();
     266            pid = vfork();
    260267            if (pid == -1) {
    261                 warn_cannot("fork for control/?");
     268                warn_cannot("vfork for control/?");
    262269                return 0;
    263270            }
    264             if (!pid) {
    265                 if (haslog && dup2(logpipe[1], 1) == -1)
     271            if (pid == 0) {
     272                /* child */
     273                if (haslog && dup2(logpipe.wr, 1) == -1)
    266274                    warn_cannot("setup stdout for control/?");
    267                 prog[0] = a;
    268                 prog[1] = NULL;
    269                 execve(a, prog, environ);
     275                execl(a, a, (char *) NULL);
    270276                fatal_cannot("run control/?");
    271277            }
    272             while (wait_pid(&w, pid) == -1) {
    273                 if (errno == EINTR) continue;
     278            /* parent */
     279            if (safe_waitpid(pid, &w, 0) == -1) {
    274280                warn_cannot("wait for child control/?");
    275281                return 0;
    276282            }
    277             return !wait_exitcode(w);
     283            return WEXITSTATUS(w) == 0;
    278284        }
    279285    } else {
     
    291297        update_status(s);
    292298    }
    293     if (s->want == W_DOWN) {
     299    if (s->sd_want == W_DOWN) {
    294300        kill(s->pid, SIGCONT);
    295301        custom(s, 'd');
    296302        return;
    297303    }
    298     if (s->want == W_EXIT) {
     304    if (s->sd_want == W_EXIT) {
    299305        kill(s->pid, SIGCONT);
    300306        custom(s, 'x');
     
    305311{
    306312    int p;
    307     char *run[2];
    308 
    309     if (s->state == S_FINISH)
    310         run[0] = (char*)"./finish";
    311     else {
    312         run[0] = (char*)"./run";
     313    const char *arg[4];
     314    char exitcode[sizeof(int)*3 + 2];
     315
     316    if (s->state == S_FINISH) {
     317/* Two arguments are given to ./finish. The first one is ./run exit code,
     318 * or -1 if ./run didnt exit normally. The second one is
     319 * the least significant byte of the exit status as determined by waitpid;
     320 * for instance it is 0 if ./run exited normally, and the signal number
     321 * if ./run was terminated by a signal. If runsv cannot start ./run
     322 * for some reason, the exit code is 111 and the status is 0.
     323 */
     324        arg[0] = "./finish";
     325        arg[1] = "-1";
     326        if (WIFEXITED(s->wstat)) {
     327            *utoa_to_buf(WEXITSTATUS(s->wstat), exitcode, sizeof(exitcode)) = '\0';
     328            arg[1] = exitcode;
     329        }
     330        //arg[2] = "0";
     331        //if (WIFSIGNALED(s->wstat)) {
     332            arg[2] = utoa(WTERMSIG(s->wstat));
     333        //}
     334        arg[3] = NULL;
     335    } else {
     336        arg[0] = "./run";
     337        arg[1] = NULL;
    313338        custom(s, 'u');
    314339    }
    315     run[1] = NULL;
    316340
    317341    if (s->pid != 0)
    318342        stopservice(s); /* should never happen */
    319     while ((p = fork()) == -1) {
    320         warn_cannot("fork, sleeping");
     343    while ((p = vfork()) == -1) {
     344        warn_cannot("vfork, sleeping");
    321345        sleep(5);
    322346    }
     
    324348        /* child */
    325349        if (haslog) {
     350            /* NB: bug alert! right order is close, then dup2 */
    326351            if (s->islog) {
    327                 xdup2(logpipe[0], 0);
    328                 close(logpipe[1]);
    329352                xchdir("./log");
     353                close(logpipe.wr);
     354                xdup2(logpipe.rd, 0);
    330355            } else {
    331                 xdup2(logpipe[1], 1);
    332                 close(logpipe[0]);
    333             }
    334         }
    335         signal(SIGCHLD, SIG_DFL);
    336         signal(SIGTERM, SIG_DFL);
     356                close(logpipe.rd);
     357                xdup2(logpipe.wr, 1);
     358            }
     359        }
     360        /* Non-ignored signals revert to SIG_DFL on exec anyway */
     361        /*bb_signals(0
     362            + (1 << SIGCHLD)
     363            + (1 << SIGTERM)
     364            , SIG_DFL);*/
    337365        sig_unblock(SIGCHLD);
    338366        sig_unblock(SIGTERM);
    339         execvp(*run, run);
    340         fatal2_cannot(s->islog ? "start log/" : "start ", *run);
    341     }
     367        execv(arg[0], (char**) arg);
     368        fatal2_cannot(s->islog ? "start log/" : "start ", arg[0]);
     369    }
     370    /* parent */
    342371    if (s->state != S_FINISH) {
    343372        gettimeofday_ns(&s->start);
     
    356385    switch (c) {
    357386    case 'd': /* down */
    358         s->want = W_DOWN;
     387        s->sd_want = W_DOWN;
    359388        update_status(s);
    360389        if (s->pid && s->state != S_FINISH)
     
    362391        break;
    363392    case 'u': /* up */
    364         s->want = W_UP;
     393        s->sd_want = W_UP;
    365394        update_status(s);
    366395        if (s->pid == 0)
     
    370399        if (s->islog)
    371400            break;
    372         s->want = W_EXIT;
     401        s->sd_want = W_EXIT;
    373402        update_status(s);
    374403        /* FALLTHROUGH */
     
    391420        if (s->pid && !custom(s, c))
    392421            kill(s->pid, SIGCONT);
    393         if (s->ctrl & C_PAUSE)
    394             s->ctrl &= ~C_PAUSE;
     422        s->ctrl &= ~C_PAUSE;
    395423        update_status(s);
    396424        break;
    397425    case 'o': /* once */
    398         s->want = W_DOWN;
     426        s->sd_want = W_DOWN;
    399427        update_status(s);
    400428        if (!s->pid)
     
    427455}
    428456
    429 int runsv_main(int argc, char **argv);
    430 int runsv_main(int argc, char **argv)
     457int runsv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     458int runsv_main(int argc UNUSED_PARAM, char **argv)
    431459{
    432460    struct stat s;
     
    435463    char buf[256];
    436464
    437     if (!argv[1] || argv[2])
    438         bb_show_usage();
    439     dir = argv[1];
    440 
    441     xpipe(selfpipe);
    442     coe(selfpipe[0]);
    443     coe(selfpipe[1]);
    444     ndelay_on(selfpipe[0]);
    445     ndelay_on(selfpipe[1]);
     465    INIT_G();
     466
     467    dir = single_argv(argv);
     468
     469    xpiped_pair(selfpipe);
     470    close_on_exec_on(selfpipe.rd);
     471    close_on_exec_on(selfpipe.wr);
     472    ndelay_on(selfpipe.rd);
     473    ndelay_on(selfpipe.wr);
    446474
    447475    sig_block(SIGCHLD);
    448     sig_catch(SIGCHLD, s_child);
     476    bb_signals_recursive_norestart(1 << SIGCHLD, s_child);
    449477    sig_block(SIGTERM);
    450     sig_catch(SIGTERM, s_term);
     478    bb_signals_recursive_norestart(1 << SIGTERM, s_term);
    451479
    452480    xchdir(dir);
     
    454482    if (S_DOWN) svd[0].state = S_DOWN; /* otherwise already 0 (bss) */
    455483    if (C_NOOP) svd[0].ctrl = C_NOOP;
    456     if (W_UP) svd[0].want = W_UP;
     484    if (W_UP) svd[0].sd_want = W_UP;
    457485    /* bss: svd[0].islog = 0; */
    458486    /* bss: svd[1].pid = 0; */
    459487    gettimeofday_ns(&svd[0].start);
    460     if (stat("down", &s) != -1) svd[0].want = W_DOWN;
     488    if (stat("down", &s) != -1)
     489        svd[0].sd_want = W_DOWN;
    461490
    462491    if (stat("log", &s) == -1) {
     
    471500            svd[1].state = S_DOWN;
    472501            svd[1].ctrl = C_NOOP;
    473             svd[1].want = W_UP;
     502            svd[1].sd_want = W_UP;
    474503            svd[1].islog = 1;
    475504            gettimeofday_ns(&svd[1].start);
    476505            if (stat("log/down", &s) != -1)
    477                 svd[1].want = W_DOWN;
    478             xpipe(logpipe);
    479             coe(logpipe[0]);
    480             coe(logpipe[1]);
     506                svd[1].sd_want = W_DOWN;
     507            xpiped_pair(logpipe);
     508            close_on_exec_on(logpipe.rd);
     509            close_on_exec_on(logpipe.wr);
    481510        }
    482511    }
     
    496525    svd[0].fdlock = xopen3("log/supervise/lock"+4,
    497526            O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
    498     if (lock_exnb(svd[0].fdlock) == -1)
     527    if (flock(svd[0].fdlock, LOCK_EX | LOCK_NB) == -1)
    499528        fatal_cannot("lock supervise/lock");
    500     coe(svd[0].fdlock);
     529    close_on_exec_on(svd[0].fdlock);
    501530    if (haslog) {
    502531        if (mkdir("log/supervise", 0700) == -1) {
     
    520549        svd[1].fdlock = xopen3("log/supervise/lock",
    521550                O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
    522         if (lock_ex(svd[1].fdlock) == -1)
     551        if (flock(svd[1].fdlock, LOCK_EX) == -1)
    523552            fatal_cannot("lock log/supervise/lock");
    524         coe(svd[1].fdlock);
     553        close_on_exec_on(svd[1].fdlock);
    525554    }
    526555
    527556    mkfifo("log/supervise/control"+4, 0600);
    528557    svd[0].fdcontrol = xopen("log/supervise/control"+4, O_RDONLY|O_NDELAY);
    529     coe(svd[0].fdcontrol);
     558    close_on_exec_on(svd[0].fdcontrol);
    530559    svd[0].fdcontrolwrite = xopen("log/supervise/control"+4, O_WRONLY|O_NDELAY);
    531     coe(svd[0].fdcontrolwrite);
     560    close_on_exec_on(svd[0].fdcontrolwrite);
    532561    update_status(&svd[0]);
    533562    if (haslog) {
    534563        mkfifo("log/supervise/control", 0600);
    535564        svd[1].fdcontrol = xopen("log/supervise/control", O_RDONLY|O_NDELAY);
    536         coe(svd[1].fdcontrol);
     565        close_on_exec_on(svd[1].fdcontrol);
    537566        svd[1].fdcontrolwrite = xopen("log/supervise/control", O_WRONLY|O_NDELAY);
    538         coe(svd[1].fdcontrolwrite);
     567        close_on_exec_on(svd[1].fdcontrolwrite);
    539568        update_status(&svd[1]);
    540569    }
    541570    mkfifo("log/supervise/ok"+4, 0600);
    542571    fd = xopen("log/supervise/ok"+4, O_RDONLY|O_NDELAY);
    543     coe(fd);
     572    close_on_exec_on(fd);
    544573    if (haslog) {
    545574        mkfifo("log/supervise/ok", 0600);
    546575        fd = xopen("log/supervise/ok", O_RDONLY|O_NDELAY);
    547         coe(fd);
     576        close_on_exec_on(fd);
    548577    }
    549578    for (;;) {
     
    553582
    554583        if (haslog)
    555             if (!svd[1].pid && svd[1].want == W_UP)
     584            if (!svd[1].pid && svd[1].sd_want == W_UP)
    556585                startservice(&svd[1]);
    557586        if (!svd[0].pid)
    558             if (svd[0].want == W_UP || svd[0].state == S_FINISH)
     587            if (svd[0].sd_want == W_UP || svd[0].state == S_FINISH)
    559588                startservice(&svd[0]);
    560589
    561         x[0].fd = selfpipe[0];
     590        x[0].fd = selfpipe.rd;
    562591        x[0].events = POLLIN;
    563592        x[1].fd = svd[0].fdcontrol;
     
    572601        sig_block(SIGCHLD);
    573602
    574         while (read(selfpipe[0], &ch, 1) == 1)
     603        while (read(selfpipe.rd, &ch, 1) == 1)
    575604            continue;
    576605
    577606        for (;;) {
    578             int child;
     607            pid_t child;
    579608            int wstat;
    580609
    581             child = wait_nohang(&wstat);
     610            child = wait_any_nohang(&wstat);
    582611            if (!child)
    583612                break;
     
    585614                break;
    586615            if (child == svd[0].pid) {
     616                svd[0].wstat = wstat;
    587617                svd[0].pid = 0;
    588618                pidchanged = 1;
    589                 svd[0].ctrl &=~ C_TERM;
     619                svd[0].ctrl &= ~C_TERM;
    590620                if (svd[0].state != S_FINISH) {
    591                     fd = open_read("finish");
     621                    fd = open("finish", O_RDONLY|O_NDELAY);
    592622                    if (fd != -1) {
    593623                        close(fd);
     
    606636            if (haslog) {
    607637                if (child == svd[1].pid) {
     638                    svd[0].wstat = wstat;
    608639                    svd[1].pid = 0;
    609640                    pidchanged = 1;
     
    617648                }
    618649            }
    619         }
     650        } /* for (;;) */
    620651        if (read(svd[0].fdcontrol, &ch, 1) == 1)
    621652            ctrl(&svd[0], ch);
     
    629660        }
    630661
    631         if (svd[0].want == W_EXIT && svd[0].state == S_DOWN) {
     662        if (svd[0].sd_want == W_EXIT && svd[0].state == S_DOWN) {
    632663            if (svd[1].pid == 0)
    633                 _exit(0);
    634             if (svd[1].want != W_EXIT) {
    635                 svd[1].want = W_EXIT;
     664                _exit(EXIT_SUCCESS);
     665            if (svd[1].sd_want != W_EXIT) {
     666                svd[1].sd_want = W_EXIT;
    636667                /* stopservice(&svd[1]); */
    637668                update_status(&svd[1]);
    638                 close(logpipe[1]);
    639                 close(logpipe[0]);
    640             }
    641         }
    642     }
     669                close(logpipe.wr);
     670                close(logpipe.rd);
     671            }
     672        }
     673    } /* for (;;) */
    643674    /* not reached */
    644675    return 0;
Note: See TracChangeset for help on using the changeset viewer.