Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/procps/kill.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/procps/kill.c

    r1765 r2725  
    66 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
    77 *
    8  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
     8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    99 */
    1010
     
    2525 */
    2626
    27 int kill_main(int argc, char **argv);
    2827int kill_main(int argc, char **argv)
    2928{
     
    5958        if (argc == 1) {
    6059            /* Print the whole signal list */
    61             for (signo = 1; signo < 32; signo++) {
    62                 const char *name = get_signame(signo);
    63                 if (!isdigit(name[0]))
    64                     puts(name);
    65             }
    66         } else { /* -l <sig list> */
    67             while ((arg = *++argv)) {
    68                 if (isdigit(arg[0])) {
    69                     signo = bb_strtou(arg, NULL, 10);
    70                     if (errno) {
    71                         bb_error_msg("unknown signal '%s'", arg);
    72                         return EXIT_FAILURE;
    73                     }
    74                     /* Exitcodes >= 0x80 are to be treated
    75                      * as "killed by signal (exitcode & 0x7f)" */
    76                     puts(get_signame(signo & 0x7f));
    77                     /* TODO: 'bad' signal# - coreutils says:
    78                      * kill: 127: invalid signal
    79                      * we just print "127" instead */
    80                 } else {
    81                     signo = get_signum(arg);
    82                     if (signo < 0) {
    83                         bb_error_msg("unknown signal '%s'", arg);
    84                         return EXIT_FAILURE;
    85                     }
    86                     printf("%d\n", signo);
    87                 }
     60            print_signames();
     61            return 0;
     62        }
     63        /* -l <sig list> */
     64        while ((arg = *++argv)) {
     65            if (isdigit(arg[0])) {
     66                signo = bb_strtou(arg, NULL, 10);
     67                if (errno) {
     68                    bb_error_msg("unknown signal '%s'", arg);
     69                    return EXIT_FAILURE;
     70                }
     71                /* Exitcodes >= 0x80 are to be treated
     72                 * as "killed by signal (exitcode & 0x7f)" */
     73                puts(get_signame(signo & 0x7f));
     74                /* TODO: 'bad' signal# - coreutils says:
     75                 * kill: 127: invalid signal
     76                 * we just print "127" instead */
     77            } else {
     78                signo = get_signum(arg);
     79                if (signo < 0) {
     80                    bb_error_msg("unknown signal '%s'", arg);
     81                    return EXIT_FAILURE;
     82                }
     83                printf("%d\n", signo);
    8884            }
    8985        }
     
    9793        arg = *++argv;
    9894        argc--;
    99         if (argc < 1) bb_show_usage();
    100         if (arg[0] != '-') goto do_it_now;
    101     }
    102 
    103     /* -SIG */
    104     signo = get_signum(&arg[1]);
     95        if (argc < 1)
     96            bb_show_usage();
     97        if (arg[0] != '-')
     98            goto do_it_now;
     99    }
     100
     101    arg++; /* skip '-' */
     102
     103    /* -o PID? (if present, it always is at the end of command line) */
     104    if (killall5 && arg[0] == 'o')
     105        goto do_it_now;
     106
     107    if (argc > 1 && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */
     108        argc--;
     109        arg = *++argv;
     110    } /* else it must be -SIG */
     111    signo = get_signum(arg);
    105112    if (signo < 0) { /* || signo > MAX_SIGNUM ? */
    106         bb_error_msg("bad signal name '%s'", &arg[1]);
     113        bb_error_msg("bad signal name '%s'", arg);
    107114        return EXIT_FAILURE;
    108115    }
     
    110117    argc--;
    111118
    112 do_it_now:
     119 do_it_now:
     120    pid = getpid();
    113121
    114122    if (killall5) {
    115123        pid_t sid;
    116124        procps_status_t* p = NULL;
    117 
    118         /* Now stop all processes */
     125        int ret = 0;
     126
     127        /* Find out our session id */
     128        sid = getsid(pid);
     129        /* Stop all processes */
    119130        kill(-1, SIGSTOP);
    120         /* Find out our own session id */
    121         pid = getpid();
    122         sid = getsid(pid);
    123         /* Now kill all processes except our session */
     131        /* Signal all processes except those in our session */
    124132        while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) {
    125             if (p->sid != sid && p->pid != pid && p->pid != 1)
    126                 kill(p->pid, signo);
    127         }
     133            int i;
     134
     135            if (p->sid == (unsigned)sid
     136             || p->pid == (unsigned)pid
     137             || p->pid == 1)
     138                continue;
     139
     140            /* All remaining args must be -o PID options.
     141             * Check p->pid against them. */
     142            for (i = 0; i < argc; i++) {
     143                pid_t omit;
     144
     145                arg = argv[i];
     146                if (arg[0] != '-' || arg[1] != 'o') {
     147                    bb_error_msg("bad option '%s'", arg);
     148                    ret = 1;
     149                    goto resume;
     150                }
     151                arg += 2;
     152                if (!arg[0] && argv[++i])
     153                    arg = argv[i];
     154                omit = bb_strtoi(arg, NULL, 10);
     155                if (errno) {
     156                    bb_error_msg("invalid number '%s'", arg);
     157                    ret = 1;
     158                    goto resume;
     159                }
     160                if (p->pid == omit)
     161                    goto dont_kill;
     162            }
     163            kill(p->pid, signo);
     164 dont_kill: ;
     165        }
     166 resume:
    128167        /* And let them continue */
    129168        kill(-1, SIGCONT);
    130         return 0;
     169        return ret;
    131170    }
    132171
    133172    /* Pid or name is required for kill/killall */
    134173    if (argc < 1) {
    135         puts("You need to specify whom to kill");
     174        bb_error_msg("you need to specify whom to kill");
    136175        return EXIT_FAILURE;
    137176    }
     
    139178    if (killall) {
    140179        /* Looks like they want to do a killall.  Do that */
    141         pid = getpid();
    142180        while (arg) {
    143181            pid_t* pidList;
     
    158196                    errors++;
    159197                    if (!quiet)
    160                         bb_perror_msg("cannot kill pid %u", (unsigned)*pl);
     198                        bb_perror_msg("can't kill pid %d", (int)*pl);
    161199                }
    162200            }
     
    174212        pid = bb_strtoi(arg, NULL, 10);
    175213        if (errno) {
    176             bb_error_msg("bad pid '%s'", arg);
     214            bb_error_msg("invalid number '%s'", arg);
    177215            errors++;
    178216        } else if (kill(pid, signo) != 0) {
    179             bb_perror_msg("cannot kill pid %d", (int)pid);
     217            bb_perror_msg("can't kill pid %d", (int)pid);
    180218            errors++;
    181219        }
Note: See TracChangeset for help on using the changeset viewer.