Statistics
| Branch: | Revision:

root / prex-0.9.0 / usr / bin / kill / kill.c @ 03e9c04a

History | View | Annotate | Download (4.17 KB)

1
/*
2
 * Copyright (c) 1988, 1993, 1994
3
 *        The Regents of the University of California.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. Neither the name of the University nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29

    
30
#include <ctype.h>
31
#include <err.h>
32
#include <errno.h>
33
#include <signal.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37

    
38
#ifdef CMDBOX
39
#define main(argc, argv)        kill_main(argc, argv)
40
#endif
41

    
42
static void nosig(char *);
43
static void printsignals(FILE *);
44
static int signame_to_signum(char *);
45
static void usage(void);
46

    
47
int
48
main(int argc, char *argv[])
49
{
50
        int errors, numsig, pid;
51
        char *ep;
52

    
53
        if (argc < 2)
54
                usage();
55

    
56
        numsig = SIGTERM;
57

    
58
        argc--;
59
        argv++;
60
        if (!strcmp(*argv, "-l")) {
61
                argc--, argv++;
62
                if (argc > 1)
63
                        usage();
64
                if (argc == 1) {
65
                        if (isdigit((unsigned char)**argv) == 0)
66
                                usage();
67
                        numsig = strtol(*argv, &ep, 10);
68
                        if (!*argv || *ep)
69
                                errx(1, "illegal signal number: %s", *argv);
70
                        if (numsig >= 128)
71
                                numsig -= 128;
72
                        if (numsig <= 0 || numsig >= NSIG)
73
                                nosig(*argv);
74
                        printf("%s\n", sys_signame[numsig]);
75
                        exit(0);
76
                }
77
                printsignals(stdout);
78
                exit(0);
79
        }
80

    
81
        if (!strcmp(*argv, "-s")) {
82
                argc--, argv++;
83
                if (argc < 1) {
84
                        warnx("option requires an argument -- s");
85
                        usage();
86
                }
87
                if (strcmp(*argv, "0")) {
88
                        if ((numsig = signame_to_signum(*argv)) < 0)
89
                                nosig(*argv);
90
                } else
91
                        numsig = 0;
92
                argc--, argv++;
93
        } else if (**argv == '-') {
94
                ++*argv;
95
                if (isalpha(**argv)) {
96
                        if ((numsig = signame_to_signum(*argv)) < 0)
97
                                nosig(*argv);
98
                } else if (isdigit(**argv)) {
99
                        numsig = strtol(*argv, &ep, 10);
100
                        if (!*argv || *ep)
101
                                errx(1, "illegal signal number: %s", *argv);
102
                        if (numsig <= 0 || numsig >= NSIG)
103
                                nosig(*argv);
104
                } else
105
                        nosig(*argv);
106
                argc--, argv++;
107
        }
108

    
109
        if (argc == 0)
110
                usage();
111

    
112
        for (errors = 0; argc; argc--, argv++) {
113
                pid = strtol(*argv, &ep, 10);
114
                if (!*argv || *ep) {
115
                        warnx("illegal process id: %s", *argv);
116
                        errors = 1;
117
                } else if (kill(pid, numsig) == -1) {
118
                        warn("%s", *argv);
119
                        errors = 1;
120
                }
121
        }
122

    
123
        exit(errors);
124
}
125

    
126
static int
127
signame_to_signum(sig)
128
        char *sig;
129
{
130
        int n;
131

    
132
        if (!strncasecmp(sig, "sig", 3))
133
                sig += 3;
134
        for (n = 1; n < NSIG; n++) {
135
                if (!strcasecmp(sys_signame[n], sig))
136
                        return n;
137
        }
138
        return -1;
139
}
140

    
141
static void
142
nosig(name)
143
        char *name;
144
{
145

    
146
        warnx("unknown signal %s; valid signals:", name);
147
        printsignals(stderr);
148
        exit(1);
149
        /* NOTREACHED */
150
}
151

    
152
static void
153
printsignals(fp)
154
        FILE *fp;
155
{
156
        int n;
157
        const char *name;
158

    
159
        for (n = 1; n < NSIG; n++) {
160
                name = sys_signame[n];
161
                if (n == (NSIG / 2) || n == (NSIG - 1))
162
                        (void)fprintf(fp, "%s\n", name);
163
                else
164
                        (void)fprintf(fp, "%s ", name);
165
        }
166
}
167

    
168
static void
169
usage(void)
170
{
171

    
172
        fprintf(stderr, "usage: kill [-s signal_name] pid ...\n"
173
                "       kill -l [exit_status]\n"
174
                "       kill -signal_name pid ...\n"
175
                "       kill -signal_number pid ...\n");
176
        exit(1);
177
        /* NOTREACHED */
178
}