VirtualBox

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

最後變更 在這個檔案是 3438,由 bird 提交於 5 年 前

kash: Hammering on threaded mode.

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

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