[626] | 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 |
|
---|
[1214] | 32 | #if 0
|
---|
[626] | 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
|
---|
| 38 | static 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 $");
|
---|
[1214] | 41 | #endif /* not lint */
|
---|
[626] | 42 | #endif
|
---|
| 43 |
|
---|
| 44 | #include <ctype.h>
|
---|
| 45 | #include <errno.h>
|
---|
| 46 | #include <stdio.h>
|
---|
| 47 | #include <stdlib.h>
|
---|
| 48 | #include <string.h>
|
---|
[1214] | 49 | #include "shtypes.h"
|
---|
[1213] | 50 | #include "jobs.h"
|
---|
| 51 | #include "error.h"
|
---|
| 52 | #include "shinstance.h"
|
---|
[626] | 53 |
|
---|
[630] | 54 |
|
---|
[1213] | 55 | static int nosig(shinstance *, char *);
|
---|
| 56 | static void printsignals(shinstance *, struct output *);
|
---|
[626] | 57 | static int signame_to_signum(char *);
|
---|
[1213] | 58 | static int usage(shinstance *psh);
|
---|
[626] | 59 |
|
---|
| 60 | int
|
---|
[1213] | 61 | killcmd(shinstance *psh, int argc, char *argv[])
|
---|
[626] | 62 | {
|
---|
[3438] | 63 | int errors, numsig;
|
---|
[626] | 64 | char *ep;
|
---|
| 65 |
|
---|
| 66 | if (argc < 2)
|
---|
[1213] | 67 | return usage(psh);
|
---|
[626] | 68 |
|
---|
| 69 | numsig = SIGTERM;
|
---|
| 70 |
|
---|
| 71 | argc--, argv++;
|
---|
| 72 | if (strcmp(*argv, "-l") == 0) {
|
---|
| 73 | argc--, argv++;
|
---|
| 74 | if (argc > 1)
|
---|
[1213] | 75 | return usage(psh);
|
---|
[626] | 76 | if (argc == 1) {
|
---|
| 77 | if (isdigit((unsigned char)**argv) == 0)
|
---|
[1213] | 78 | return usage(psh);
|
---|
[626] | 79 | numsig = strtol(*argv, &ep, 10);
|
---|
| 80 | if (*ep != '\0') {
|
---|
[1213] | 81 | sh_errx(psh, EXIT_FAILURE, "illegal signal number: %s",
|
---|
[626] | 82 | *argv);
|
---|
| 83 | /* NOTREACHED */
|
---|
| 84 | }
|
---|
| 85 | if (numsig >= 128)
|
---|
| 86 | numsig -= 128;
|
---|
| 87 | if (numsig <= 0 || numsig >= NSIG)
|
---|
[1213] | 88 | return nosig(psh, *argv);
|
---|
| 89 | outfmt(psh->out1, "%s\n", sys_signame[numsig]);
|
---|
| 90 | //sh_exit(psh, 0);
|
---|
| 91 | return 0;
|
---|
[626] | 92 | }
|
---|
[1213] | 93 | printsignals(psh, psh->out1);
|
---|
| 94 | //sh_exit(psh, 0);
|
---|
| 95 | return 0;
|
---|
[626] | 96 | }
|
---|
| 97 |
|
---|
| 98 | if (!strcmp(*argv, "-s")) {
|
---|
| 99 | argc--, argv++;
|
---|
| 100 | if (argc < 1) {
|
---|
[1213] | 101 | sh_warnx(psh, "option requires an argument -- s");
|
---|
| 102 | return usage(psh);
|
---|
[626] | 103 | }
|
---|
| 104 | if (strcmp(*argv, "0")) {
|
---|
| 105 | if ((numsig = signame_to_signum(*argv)) < 0)
|
---|
[1213] | 106 | return nosig(psh, *argv);
|
---|
[626] | 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)
|
---|
[1213] | 114 | return nosig(psh, *argv);
|
---|
[626] | 115 | } else if (isdigit((unsigned char)**argv)) {
|
---|
| 116 | numsig = strtol(*argv, &ep, 10);
|
---|
| 117 | if (!*argv || *ep) {
|
---|
[1213] | 118 | sh_errx(psh, EXIT_FAILURE, "illegal signal number: %s",
|
---|
[626] | 119 | *argv);
|
---|
| 120 | /* NOTREACHED */
|
---|
| 121 | }
|
---|
| 122 | if (numsig < 0 || numsig >= NSIG)
|
---|
[1213] | 123 | return nosig(psh, *argv);
|
---|
[626] | 124 | } else
|
---|
[1213] | 125 | return nosig(psh, *argv);
|
---|
[626] | 126 | argc--, argv++;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | if (argc == 0)
|
---|
[1213] | 130 | return usage(psh);
|
---|
[626] | 131 |
|
---|
| 132 | for (errors = 0; argc; argc--, argv++) {
|
---|
[3438] | 133 | const char * const strpid = argv[0];
|
---|
| 134 | shpid pid;
|
---|
| 135 | if (*strpid == '%') {
|
---|
| 136 | pid = getjobpgrp(psh, strpid);
|
---|
[626] | 137 | if (pid == 0) {
|
---|
[3438] | 138 | sh_warnx(psh, "illegal job id: %s", strpid);
|
---|
[626] | 139 | errors = 1;
|
---|
| 140 | continue;
|
---|
| 141 | }
|
---|
[1213] | 142 | } else {
|
---|
[3438] | 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);
|
---|
[626] | 152 | errors = 1;
|
---|
| 153 | continue;
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
[1213] | 156 | if (sh_kill(psh, pid, numsig) == -1) {
|
---|
[3438] | 157 | sh_warn(psh, "%s", strpid);
|
---|
[626] | 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)
|
---|
[1213] | 163 | sh_kill(psh, pid, SIGCONT);
|
---|
[626] | 164 | }
|
---|
| 165 |
|
---|
[1213] | 166 | //sh_exit(psh, errors);
|
---|
| 167 | ///* NOTREACHED */
|
---|
| 168 | return errors;
|
---|
[626] | 169 | }
|
---|
| 170 |
|
---|
| 171 | static int
|
---|
| 172 | signame_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 |
|
---|
[1213] | 184 | static int
|
---|
| 185 | nosig(shinstance *psh, char *name)
|
---|
[626] | 186 | {
|
---|
[1213] | 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;
|
---|
[626] | 192 | }
|
---|
| 193 |
|
---|
| 194 | static void
|
---|
[1213] | 195 | printsignals(shinstance *psh, struct output *out)
|
---|
[626] | 196 | {
|
---|
| 197 | int sig;
|
---|
[1213] | 198 | size_t len, nl;
|
---|
[626] | 199 | const char *name;
|
---|
[2312] | 200 | unsigned termwidth = 80;
|
---|
[626] | 201 |
|
---|
[1213] | 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)
|
---|
[626] | 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) {
|
---|
[1213] | 213 | outfmt(out, "\n");
|
---|
[626] | 214 | len = 0;
|
---|
[1213] | 215 | } else if (len != 0)
|
---|
| 216 | outfmt(out, " ");
|
---|
[626] | 217 | len += nl;
|
---|
[1213] | 218 | outfmt(out, "%s", name);
|
---|
[626] | 219 | }
|
---|
| 220 | if (len != 0)
|
---|
[1213] | 221 | outfmt(out, "\n");
|
---|
[626] | 222 | }
|
---|
| 223 |
|
---|
[1213] | 224 | static int
|
---|
| 225 | usage(shinstance *psh)
|
---|
[626] | 226 | {
|
---|
[1213] | 227 | outfmt(psh->out2,
|
---|
| 228 | "usage: %s [-s signal_name] pid ...\n"
|
---|
[626] | 229 | " %s -l [exit_status]\n"
|
---|
| 230 | " %s -signal_name pid ...\n"
|
---|
| 231 | " %s -signal_number pid ...\n",
|
---|
[1213] | 232 | psh->commandname, psh->commandname, psh->commandname, psh->commandname);
|
---|
| 233 | //sh_exit(psh, 1);
|
---|
| 234 | ///* NOTREACHED */
|
---|
| 235 | return 1;
|
---|
[626] | 236 | }
|
---|