Annotation of coherent/d/bin/kill.c, revision 1.1.1.1

1.1       root        1: static char _version[]="kill version 2.0";
                      2: /*
                      3:  * kill -- send a signal to a process
                      4:  */
                      5: #include       <signal.h>
                      6: #include       <ctype.h>
                      7: #include       <errno.h>
                      8: #include       <stdio.h>
                      9: 
                     10: #define        NOTREACHED return
                     11: 
                     12: int err = 0;
                     13: 
                     14: main( argc, argv)
                     15: register char  **argv;
                     16: {
                     17:        register        pid;
                     18:        char            **getsignal( );
                     19:        extern char     *sys_errlist[];
                     20:        extern          errno;
                     21:        static          int sig = SIGTERM;
                     22: 
                     23:        /*
                     24:         * Fetch the signal we are going to send, from the command line.
                     25:         */
                     26:        argv = getsignal(argv, &sig);
                     27: 
                     28: 
                     29:        if (*argv == NULL) {
                     30:                fprintf(stderr, "Usage: kill [ signal ] pid ...\n");
                     31:                exit(1);        
                     32:        }
                     33: 
                     34:        /*
                     35:         * If we are sending this signal to our own process group
                     36:         * leader, we don't want to receive this signal here.
                     37:         */
                     38:        signal(sig, SIG_IGN);
                     39: 
                     40: 
                     41:        do {
                     42:                if (isdigit(**argv)==0 && **argv!='-') {
                     43:                        error("\"%s\" is not a process id", *argv);
                     44:                        continue;
                     45:                }
                     46:                pid = atoi(*argv);
                     47:                if (kill(pid, sig)) {
                     48:                        if (errno == EINVAL)
                     49:                                fatal("signal %d is invalid", sig);
                     50:                        error("process %d: %s", pid, sys_errlist[errno]);
                     51:                        continue;
                     52:                }
                     53:        } while (*++argv);
                     54:        exit(err);
                     55: }
                     56: 
                     57: 
                     58: /*
                     59:  * Attempt to extract a signal number from argv[].
                     60:  * Puts any signal number it finds in *sigp, and then returns
                     61:  * the next argument in argv[].
                     62:  */
                     63: char **
                     64: getsignal( av, sigp)
                     65: register char **av;
                     66: int *sigp;
                     67: {
                     68:        if ((++av)[0] == NULL)
                     69:                return (av);
                     70:        if (isdigit( av[0][0]))
                     71:                return (av);
                     72:        /*
                     73:         * Do we have an explicit signal number?
                     74:         */
                     75:        if (av[0][0] == '-') {
                     76:                ++av[0];        /* Skip the '-'.  */
                     77:                if (isdigit( av[0][0])) {
                     78:                        /*
                     79:                         * Convert the signal number to an integer and return.
                     80:                         */
                     81:                        *sigp = atoi( &av[0][0]);
                     82:                        return (++av);
                     83:                }
                     84:        }
                     85: 
                     86:        /*
                     87:         * The signal must be symbolic.
                     88:         */
                     89:        uppercase( av[0]);
                     90:        /*
                     91:         * Strip off an optional leading "SIG".
                     92:         */
                     93:        if (strncmp( av[0], "SIG", 3) == 0)
                     94:                *av += 3;
                     95:        /*
                     96:         * Walk through the signal names looking for a match.
                     97:         */
                     98:        for (*sigp = 1; notsame( *sigp, av[0]); ++*sigp) {
                     99:                /* Do nothing.  */
                    100:        }
                    101: 
                    102:        /*
                    103:         * Bump argv to next argument.
                    104:         */
                    105:        return (++av);
                    106: }
                    107: 
                    108: 
                    109: /*
                    110:  * Returns 0 only if 'sig' has symbolic name 'name'.
                    111:  */
                    112: notsame(sig, name)
                    113: char *name;
                    114: {
                    115:        static char     *names[] = {
                    116: #ifdef _I386
                    117:                0,
                    118:                "HUP", "INT", "QUIT", "ILL",
                    119:                "TRAP", "IOT", "EMT", "FPE",
                    120:                "KILL", "BUS", "SEGV", "SYS",
                    121:                "PIPE", "ALRM", "TERM", "USR1",
                    122:                "USR2", "CHLD", "PWR", "WINCH",
                    123:                "", "POLL",
                    124:                NULL
                    125: #else /* _I386 */
                    126:                0,
                    127:                "HUP",  "INT",  "QUIT", "ALRM",
                    128:                "TERM", "REST", "SYS",  "PIPE",
                    129:                "KILL", "TRAP", "SEGV", "ILL",
                    130:                "IOT",  "EMT",  "FPE",  "BUS",
                    131:                NULL
                    132: #endif /* _I386 */
                    133:        };
                    134: 
                    135:        if (sig>NSIG || names[sig]==NULL) {
                    136:                fatal( "no such signal SIG%s", name);
                    137:        }
                    138: 
                    139: #ifdef _I386
                    140:        /*
                    141:         * SIGABRT and SIGCLD share numbers with other signals.
                    142:         */
                    143:        if ( SIGABRT == sig ) {
                    144:                if ( 0 == strcmp( "ABRT", name ) ) {
                    145:                        return (0);     /* Match! */
                    146:                }
                    147:        }
                    148:        if ( SIGCLD == sig ) {
                    149:                if ( 0 == strcmp( "CLD", name ) ) {
                    150:                        return (0);     /* Match! */
                    151:                }
                    152:        }
                    153: #endif /* _I386 */
                    154: 
                    155:        return (strcmp( names[sig], name));
                    156: }
                    157: 
                    158: 
                    159: uppercase(s)
                    160: register char *s;
                    161: {
                    162:        for (; *s; ++s)
                    163:                if (islower( *s))
                    164:                        *s &= ~040;
                    165: }
                    166: 
                    167: 
                    168: error(arg0)
                    169: char *arg0;
                    170: {
                    171:        fprintf( stderr, "kill: %r\n", &arg0);
                    172:        err = 1;
                    173: }
                    174: 
                    175: 
                    176: fatal(arg0)
                    177: char *arg0;
                    178: {
                    179:        fprintf( stderr, "kill: %r\n", &arg0);
                    180:        exit(1);
                    181: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.