source: trunk/src/kash/bltin/kill.c@ 3409

Last change on this file since 3409 was 3409, checked in by bird, 5 years ago

kash: Generate the signal names at compile time rather than lazily at runtime. This should be more efficient, though may cause trouble if cross building.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 5.9 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
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, pid;
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 if (*argv[0] == '%') {
134 pid = getjobpgrp(psh, *argv);
135 if (pid == 0) {
136 sh_warnx(psh, "illegal job id: %s", *argv);
137 errors = 1;
138 continue;
139 }
140 } else {
141 pid = strtol(*argv, &ep, 10);
142 if (!**argv || *ep) {
143 sh_warnx(psh, "illegal process id: %s", *argv);
144 errors = 1;
145 continue;
146 }
147 }
148 if (sh_kill(psh, pid, numsig) == -1) {
149 sh_warn(psh, "%s", *argv);
150 errors = 1;
151 }
152 /* Wakeup the process if it was suspended, so it can
153 exit without an explicit 'fg'. */
154 if (numsig == SIGTERM || numsig == SIGHUP)
155 sh_kill(psh, pid, SIGCONT);
156 }
157
158 //sh_exit(psh, errors);
159 ///* NOTREACHED */
160 return errors;
161}
162
163static int
164signame_to_signum(char *sig)
165{
166 int n;
167 if (strncasecmp(sig, "sig", 3) == 0)
168 sig += 3;
169 for (n = 1; n < NSIG; n++) {
170 if (!strcasecmp(sys_signame[n], sig))
171 return (n);
172 }
173 return (-1);
174}
175
176static int
177nosig(shinstance *psh, char *name)
178{
179 sh_warnx(psh, "unknown signal %s; valid signals:", name);
180 printsignals(psh, psh->out2);
181 //sh_exit(psh, 1);
182 ///* NOTREACHED */
183 return 1;
184}
185
186static void
187printsignals(shinstance *psh, struct output *out)
188{
189 int sig;
190 size_t len, nl;
191 const char *name;
192 unsigned termwidth = 80;
193
194 if (shfile_isatty(&psh->fdtab, out->fd)) {
195 sh_winsize win;
196 if (shfile_ioctl(&psh->fdtab, out->fd, TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
197 termwidth = win.ws_col;
198 }
199
200 for (len = 0, sig = 1; sig < NSIG; sig++) {
201 name = sys_signame[sig];
202 nl = 1 + strlen(name);
203
204 if (len + nl >= termwidth) {
205 outfmt(out, "\n");
206 len = 0;
207 } else if (len != 0)
208 outfmt(out, " ");
209 len += nl;
210 outfmt(out, "%s", name);
211 }
212 if (len != 0)
213 outfmt(out, "\n");
214}
215
216static int
217usage(shinstance *psh)
218{
219 outfmt(psh->out2,
220 "usage: %s [-s signal_name] pid ...\n"
221 " %s -l [exit_status]\n"
222 " %s -signal_name pid ...\n"
223 " %s -signal_number pid ...\n",
224 psh->commandname, psh->commandname, psh->commandname, psh->commandname);
225 //sh_exit(psh, 1);
226 ///* NOTREACHED */
227 return 1;
228}
Note: See TracBrowser for help on using the repository browser.