VirtualBox

source: kBuild/trunk/src/kash/bltin/kill.c@ 1208

Last change on this file since 1208 was 1207, checked in by bird, 17 years ago

jobs and other stuff.

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1/* $NetBSD: kill.c,v 1.23 2003/08/07 09:05:13 agc Exp $ */
2
3/*
4 * Copyright (c) 1988, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#ifdef HAVE_SYS_CDEFS_H
33#include <sys/cdefs.h>
34#endif
35#if !defined(lint) && !defined(SHELL)
36__COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n");
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)kill.c 8.4 (Berkeley) 4/28/95";
43#else
44__RCSID("$NetBSD: kill.c,v 1.23 2003/08/07 09:05:13 agc Exp $");
45#endif
46#endif /* not lint */
47
48#include <ctype.h>
49#ifndef __sun__
50#include <err.h>
51#endif
52#include <errno.h>
53#include <signal.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <termios.h>
58#include <unistd.h>
59#include <locale.h>
60#include <sys/ioctl.h>
61
62#ifndef HAVE_SYS_SIGNAME
63extern void init_sys_signame(void);
64extern char sys_signame[NSIG][16];
65#endif
66
67#ifdef SHELL /* sh (aka ash) builtin */
68#define main killcmd
69#include "bltin/bltin.h"
70#include "jobs.h"
71#endif /* SHELL */
72
73static void nosig(char *);
74static void printsignals(FILE *);
75static int signame_to_signum(char *);
76static void usage(void);
77int main(int, char *[]);
78
79int
80main(int argc, char *argv[])
81{
82 int errors, numsig, pid;
83 char *ep;
84
85 setprogname(argv[0]);
86 setlocale(LC_ALL, "");
87 if (argc < 2)
88 usage();
89
90 numsig = SIGTERM;
91
92 argc--, argv++;
93 if (strcmp(*argv, "-l") == 0) {
94 argc--, argv++;
95 if (argc > 1)
96 usage();
97 if (argc == 1) {
98 if (isdigit((unsigned char)**argv) == 0)
99 usage();
100 numsig = strtol(*argv, &ep, 10);
101 if (*ep != '\0') {
102 errx(EXIT_FAILURE, "illegal signal number: %s",
103 *argv);
104 /* NOTREACHED */
105 }
106 if (numsig >= 128)
107 numsig -= 128;
108 if (numsig <= 0 || numsig >= NSIG)
109 nosig(*argv);
110#ifndef HAVE_SYS_SIGNAME
111 init_sys_signame();
112#endif
113 printf("%s\n", sys_signame[numsig]);
114 exit(0);
115 }
116 printsignals(stdout);
117 exit(0);
118 }
119
120 if (!strcmp(*argv, "-s")) {
121 argc--, argv++;
122 if (argc < 1) {
123 warnx("option requires an argument -- s");
124 usage();
125 }
126 if (strcmp(*argv, "0")) {
127 if ((numsig = signame_to_signum(*argv)) < 0)
128 nosig(*argv);
129 } else
130 numsig = 0;
131 argc--, argv++;
132 } else if (**argv == '-') {
133 ++*argv;
134 if (isalpha((unsigned char)**argv)) {
135 if ((numsig = signame_to_signum(*argv)) < 0)
136 nosig(*argv);
137 } else if (isdigit((unsigned char)**argv)) {
138 numsig = strtol(*argv, &ep, 10);
139 if (!*argv || *ep) {
140 errx(EXIT_FAILURE, "illegal signal number: %s",
141 *argv);
142 /* NOTREACHED */
143 }
144 if (numsig < 0 || numsig >= NSIG)
145 nosig(*argv);
146 } else
147 nosig(*argv);
148 argc--, argv++;
149 }
150
151 if (argc == 0)
152 usage();
153
154 for (errors = 0; argc; argc--, argv++) {
155#ifdef SHELL
156 if (*argv[0] == '%') {
157 pid = getjobpgrp(psh, *argv);
158 if (pid == 0) {
159 warnx("illegal job id: %s", *argv);
160 errors = 1;
161 continue;
162 }
163 } else
164#endif
165 {
166 pid = strtol(*argv, &ep, 10);
167 if (!**argv || *ep) {
168 warnx("illegal process id: %s", *argv);
169 errors = 1;
170 continue;
171 }
172 }
173 if (kill(pid, numsig) == -1) {
174 warn("%s", *argv);
175 errors = 1;
176 }
177#ifdef SHELL
178 /* Wakeup the process if it was suspended, so it can
179 exit without an explicit 'fg'. */
180 if (numsig == SIGTERM || numsig == SIGHUP)
181 kill(pid, SIGCONT);
182#endif
183 }
184
185 exit(errors);
186 /* NOTREACHED */
187}
188
189static int
190signame_to_signum(char *sig)
191{
192 int n;
193#ifndef HAVE_SYS_SIGNAME
194 init_sys_signame();
195#endif
196 if (strncasecmp(sig, "sig", 3) == 0)
197 sig += 3;
198 for (n = 1; n < NSIG; n++) {
199 if (!strcasecmp(sys_signame[n], sig))
200 return (n);
201 }
202 return (-1);
203}
204
205static void
206nosig(char *name)
207{
208
209 warnx("unknown signal %s; valid signals:", name);
210 printsignals(stderr);
211 exit(1);
212 /* NOTREACHED */
213}
214
215static void
216printsignals(FILE *fp)
217{
218 int sig;
219 int len, nl;
220 const char *name;
221 int termwidth = 80;
222
223#ifdef TIOCGWINSZ
224 if (isatty(fileno(fp))) {
225 struct winsize win;
226 if (ioctl(fileno(fp), TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
227 termwidth = win.ws_col;
228 }
229#else
230#ifndef _MSC_VER
231#warning TIOCGWINSZ is not present.
232#endif
233#endif
234#ifndef HAVE_SYS_SIGNAME
235 init_sys_signame();
236#endif
237
238 for (len = 0, sig = 1; sig < NSIG; sig++) {
239 name = sys_signame[sig];
240 nl = 1 + strlen(name);
241
242 if (len + nl >= termwidth) {
243 fprintf(fp, "\n");
244 len = 0;
245 } else
246 if (len != 0)
247 fprintf(fp, " ");
248 len += nl;
249 fprintf(fp, "%s", name);
250 }
251 if (len != 0)
252 fprintf(fp, "\n");
253}
254
255static void
256usage(void)
257{
258
259 fprintf(stderr, "usage: %s [-s signal_name] pid ...\n"
260 " %s -l [exit_status]\n"
261 " %s -signal_name pid ...\n"
262 " %s -signal_number pid ...\n",
263 getprogname(), getprogname(), getprogname(), getprogname());
264 exit(1);
265 /* NOTREACHED */
266}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette