VirtualBox

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

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

kill.c ++

  • Property svn:eol-style set to native
File size: 6.0 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#include <errno.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include "jobs.h"
54#include "error.h"
55#include "shinstance.h"
56
57#ifndef HAVE_SYS_SIGNAME
58extern void init_sys_signame(void);
59extern char sys_signame[NSIG][16];
60#endif
61
62static int nosig(shinstance *, char *);
63static void printsignals(shinstance *, struct output *);
64static int signame_to_signum(char *);
65static int usage(shinstance *psh);
66
67int
68killcmd(shinstance *psh, int argc, char *argv[])
69{
70 int errors, numsig, pid;
71 char *ep;
72
73 if (argc < 2)
74 return usage(psh);
75
76 numsig = SIGTERM;
77#ifndef HAVE_SYS_SIGNAME
78 init_sys_signame();
79#endif
80
81 argc--, argv++;
82 if (strcmp(*argv, "-l") == 0) {
83 argc--, argv++;
84 if (argc > 1)
85 return usage(psh);
86 if (argc == 1) {
87 if (isdigit((unsigned char)**argv) == 0)
88 return usage(psh);
89 numsig = strtol(*argv, &ep, 10);
90 if (*ep != '\0') {
91 sh_errx(psh, EXIT_FAILURE, "illegal signal number: %s",
92 *argv);
93 /* NOTREACHED */
94 }
95 if (numsig >= 128)
96 numsig -= 128;
97 if (numsig <= 0 || numsig >= NSIG)
98 return nosig(psh, *argv);
99 outfmt(psh->out1, "%s\n", sys_signame[numsig]);
100 //sh_exit(psh, 0);
101 return 0;
102 }
103 printsignals(psh, psh->out1);
104 //sh_exit(psh, 0);
105 return 0;
106 }
107
108 if (!strcmp(*argv, "-s")) {
109 argc--, argv++;
110 if (argc < 1) {
111 sh_warnx(psh, "option requires an argument -- s");
112 return usage(psh);
113 }
114 if (strcmp(*argv, "0")) {
115 if ((numsig = signame_to_signum(*argv)) < 0)
116 return nosig(psh, *argv);
117 } else
118 numsig = 0;
119 argc--, argv++;
120 } else if (**argv == '-') {
121 ++*argv;
122 if (isalpha((unsigned char)**argv)) {
123 if ((numsig = signame_to_signum(*argv)) < 0)
124 return nosig(psh, *argv);
125 } else if (isdigit((unsigned char)**argv)) {
126 numsig = strtol(*argv, &ep, 10);
127 if (!*argv || *ep) {
128 sh_errx(psh, EXIT_FAILURE, "illegal signal number: %s",
129 *argv);
130 /* NOTREACHED */
131 }
132 if (numsig < 0 || numsig >= NSIG)
133 return nosig(psh, *argv);
134 } else
135 return nosig(psh, *argv);
136 argc--, argv++;
137 }
138
139 if (argc == 0)
140 return usage(psh);
141
142 for (errors = 0; argc; argc--, argv++) {
143 if (*argv[0] == '%') {
144 pid = getjobpgrp(psh, *argv);
145 if (pid == 0) {
146 sh_warnx(psh, "illegal job id: %s", *argv);
147 errors = 1;
148 continue;
149 }
150 } else {
151 pid = strtol(*argv, &ep, 10);
152 if (!**argv || *ep) {
153 sh_warnx(psh, "illegal process id: %s", *argv);
154 errors = 1;
155 continue;
156 }
157 }
158 if (sh_kill(psh, pid, numsig) == -1) {
159 sh_warn(psh, "%s", *argv);
160 errors = 1;
161 }
162 /* Wakeup the process if it was suspended, so it can
163 exit without an explicit 'fg'. */
164 if (numsig == SIGTERM || numsig == SIGHUP)
165 sh_kill(psh, pid, SIGCONT);
166 }
167
168 //sh_exit(psh, errors);
169 ///* NOTREACHED */
170 return errors;
171}
172
173static int
174signame_to_signum(char *sig)
175{
176 int n;
177 if (strncasecmp(sig, "sig", 3) == 0)
178 sig += 3;
179 for (n = 1; n < NSIG; n++) {
180 if (!strcasecmp(sys_signame[n], sig))
181 return (n);
182 }
183 return (-1);
184}
185
186static int
187nosig(shinstance *psh, char *name)
188{
189 sh_warnx(psh, "unknown signal %s; valid signals:", name);
190 printsignals(psh, psh->out2);
191 //sh_exit(psh, 1);
192 ///* NOTREACHED */
193 return 1;
194}
195
196static void
197printsignals(shinstance *psh, struct output *out)
198{
199 int sig;
200 size_t len, nl;
201 const char *name;
202 int termwidth = 80;
203
204 if (shfile_isatty(&psh->fdtab, out->fd)) {
205 sh_winsize win;
206 if (shfile_ioctl(&psh->fdtab, out->fd, TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
207 termwidth = win.ws_col;
208 }
209
210 for (len = 0, sig = 1; sig < NSIG; sig++) {
211 name = sys_signame[sig];
212 nl = 1 + strlen(name);
213
214 if (len + nl >= termwidth) {
215 outfmt(out, "\n");
216 len = 0;
217 } else if (len != 0)
218 outfmt(out, " ");
219 len += nl;
220 outfmt(out, "%s", name);
221 }
222 if (len != 0)
223 outfmt(out, "\n");
224}
225
226static int
227usage(shinstance *psh)
228{
229 outfmt(psh->out2,
230 "usage: %s [-s signal_name] pid ...\n"
231 " %s -l [exit_status]\n"
232 " %s -signal_name pid ...\n"
233 " %s -signal_number pid ...\n",
234 psh->commandname, psh->commandname, psh->commandname, psh->commandname);
235 //sh_exit(psh, 1);
236 ///* NOTREACHED */
237 return 1;
238}
Note: See TracBrowser for help on using the repository browser.

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