VirtualBox

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

Last change on this file since 2392 was 2312, checked in by bird, 16 years ago

kash: eliminating warnings (gcc/darwin).

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