Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is kill.def, from which is created kill.c. |
| 2 | It implements the builtin "kill" in Bash. |
| 3 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 4 | Copyright (C) 1987-2010 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Bash is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Bash is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES kill.c |
| 22 | |
| 23 | $BUILTIN kill |
| 24 | $FUNCTION kill_builtin |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 25 | $SHORT_DOC kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 26 | Send a signal to a job. |
| 27 | |
| 28 | Send the processes identified by PID or JOBSPEC the signal named by |
| 29 | SIGSPEC or SIGNUM. If neither SIGSPEC nor SIGNUM is present, then |
| 30 | SIGTERM is assumed. |
| 31 | |
| 32 | Options: |
| 33 | -s sig SIG is a signal name |
| 34 | -n sig SIG is a signal number |
| 35 | -l list the signal names; if arguments follow `-l' they are |
| 36 | assumed to be signal numbers for which names should be listed |
| 37 | |
| 38 | Kill is a shell builtin for two reasons: it allows job IDs to be used |
| 39 | instead of process IDs, and allows processes to be killed if the limit |
| 40 | on processes that you can create is reached. |
| 41 | |
| 42 | Exit Status: |
| 43 | Returns success unless an invalid option is given or an error occurs. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 44 | $END |
| 45 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 46 | #include <config.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 47 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 48 | #include <stdio.h> |
| 49 | #include <errno.h> |
| 50 | #if defined (HAVE_UNISTD_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 51 | # ifdef _MINIX |
| 52 | # include <sys/types.h> |
| 53 | # endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 54 | # include <unistd.h> |
| 55 | #endif |
| 56 | |
| 57 | #include "../bashansi.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 58 | #include "../bashintl.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 59 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 60 | #include <signal.h> |
| 61 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 62 | #include "../shell.h" |
| 63 | #include "../trap.h" |
| 64 | #include "../jobs.h" |
| 65 | #include "common.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 66 | |
| 67 | /* Not all systems declare ERRNO in errno.h... and some systems #define it! */ |
| 68 | #if !defined (errno) |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 69 | #include <errno.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 70 | #endif /* !errno */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 71 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 72 | extern int posixly_correct; |
| 73 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 74 | static void kill_error __P((pid_t, int)); |
| 75 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 76 | #if !defined (CONTINUE_AFTER_KILL_ERROR) |
| 77 | # define CONTINUE_OR_FAIL return (EXECUTION_FAILURE) |
| 78 | #else |
| 79 | # define CONTINUE_OR_FAIL goto continue_killing |
| 80 | #endif /* CONTINUE_AFTER_KILL_ERROR */ |
| 81 | |
| 82 | /* Here is the kill builtin. We only have it so that people can type |
| 83 | kill -KILL %1? No, if you fill up the process table this way you |
| 84 | can still kill some. */ |
| 85 | int |
| 86 | kill_builtin (list) |
| 87 | WORD_LIST *list; |
| 88 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 89 | int sig, any_succeeded, listing, saw_signal, dflags; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 90 | char *sigspec, *word; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 91 | pid_t pid; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 92 | intmax_t pid_value; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 93 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 94 | if (list == 0) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 95 | { |
| 96 | builtin_usage (); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 97 | return (EX_USAGE); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 98 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 99 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 100 | any_succeeded = listing = saw_signal = 0; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 101 | sig = SIGTERM; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 102 | sigspec = "TERM"; |
| 103 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 104 | dflags = DSIG_NOCASE | ((posixly_correct == 0) ? DSIG_SIGPREFIX : 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 105 | /* Process options. */ |
| 106 | while (list) |
| 107 | { |
| 108 | word = list->word->word; |
| 109 | |
| 110 | if (ISOPTION (word, 'l')) |
| 111 | { |
| 112 | listing++; |
| 113 | list = list->next; |
| 114 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 115 | else if (ISOPTION (word, 's') || ISOPTION (word, 'n')) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 116 | { |
| 117 | list = list->next; |
| 118 | if (list) |
| 119 | { |
| 120 | sigspec = list->word->word; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 121 | if (sigspec[0] == '0' && sigspec[1] == '\0') |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 122 | sig = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 123 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 124 | sig = decode_signal (sigspec, dflags); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 125 | list = list->next; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 126 | saw_signal++; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 127 | } |
| 128 | else |
| 129 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 130 | sh_needarg (word); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 131 | return (EXECUTION_FAILURE); |
| 132 | } |
| 133 | } |
| 134 | else if (ISOPTION (word, '-')) |
| 135 | { |
| 136 | list = list->next; |
| 137 | break; |
| 138 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 139 | else if (ISOPTION (word, '?')) |
| 140 | { |
| 141 | builtin_usage (); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 142 | return (EX_USAGE); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 143 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 144 | /* If this is a signal specification then process it. We only process |
| 145 | the first one seen; other arguments may signify process groups (e.g, |
| 146 | -num == process group num). */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 147 | else if (*word == '-' && saw_signal == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 148 | { |
| 149 | sigspec = word + 1; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 150 | sig = decode_signal (sigspec, dflags); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 151 | saw_signal++; |
| 152 | list = list->next; |
| 153 | } |
| 154 | else |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | if (listing) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 159 | return (display_signal_list (list, 0)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 160 | |
| 161 | /* OK, we are killing processes. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 162 | if (sig == NO_SIG) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 163 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 164 | sh_invalidsig (sigspec); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 165 | return (EXECUTION_FAILURE); |
| 166 | } |
| 167 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 168 | if (list == 0) |
| 169 | { |
| 170 | builtin_usage (); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 171 | return (EX_USAGE); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 174 | while (list) |
| 175 | { |
| 176 | word = list->word->word; |
| 177 | |
| 178 | if (*word == '-') |
| 179 | word++; |
| 180 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 181 | /* Use the entire argument in case of minus sign presence. */ |
| 182 | if (*word && legal_number (list->word->word, &pid_value) && (pid_value == (pid_t)pid_value)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 183 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 184 | pid = (pid_t) pid_value; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 185 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 186 | if (kill_pid (pid, sig, pid < -1) < 0) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 187 | { |
| 188 | if (errno == EINVAL) |
| 189 | sh_invalidsig (sigspec); |
| 190 | else |
| 191 | kill_error (pid, errno); |
| 192 | CONTINUE_OR_FAIL; |
| 193 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 194 | else |
| 195 | any_succeeded++; |
| 196 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 197 | #if defined (JOB_CONTROL) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 198 | else if (*list->word->word && *list->word->word != '%') |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 199 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 200 | builtin_error (_("%s: arguments must be process or job IDs"), list->word->word); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 201 | CONTINUE_OR_FAIL; |
| 202 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 203 | else if (*word) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 204 | /* Posix.2 says you can kill without job control active (4.32.4) */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 205 | { /* Must be a job spec. Check it out. */ |
| 206 | int job; |
| 207 | sigset_t set, oset; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 208 | JOB *j; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 209 | |
| 210 | BLOCK_CHILD (set, oset); |
| 211 | job = get_job_spec (list); |
| 212 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 213 | if (INVALID_JOB (job)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 214 | { |
| 215 | if (job != DUP_JOB) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 216 | sh_badjob (list->word->word); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 217 | UNBLOCK_CHILD (oset); |
| 218 | CONTINUE_OR_FAIL; |
| 219 | } |
| 220 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 221 | j = get_job_by_jid (job); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 222 | /* Job spec used. Kill the process group. If the job was started |
| 223 | without job control, then its pgrp == shell_pgrp, so we have |
| 224 | to be careful. We take the pid of the first job in the pipeline |
| 225 | in that case. */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 226 | pid = IS_JOBCONTROL (job) ? j->pgrp : j->pipe->pid; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 227 | |
| 228 | UNBLOCK_CHILD (oset); |
| 229 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 230 | if (kill_pid (pid, sig, 1) < 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 231 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 232 | if (errno == EINVAL) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 233 | sh_invalidsig (sigspec); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 234 | else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 235 | kill_error (pid, errno); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 236 | CONTINUE_OR_FAIL; |
| 237 | } |
| 238 | else |
| 239 | any_succeeded++; |
| 240 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 241 | #endif /* !JOB_CONTROL */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 242 | else |
| 243 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 244 | sh_badpid (list->word->word); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 245 | CONTINUE_OR_FAIL; |
| 246 | } |
| 247 | continue_killing: |
| 248 | list = list->next; |
| 249 | } |
| 250 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 251 | return (any_succeeded ? EXECUTION_SUCCESS : EXECUTION_FAILURE); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 252 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 253 | |
| 254 | static void |
| 255 | kill_error (pid, e) |
| 256 | pid_t pid; |
| 257 | int e; |
| 258 | { |
| 259 | char *x; |
| 260 | |
| 261 | x = strerror (e); |
| 262 | if (x == 0) |
| 263 | x = _("Unknown error"); |
| 264 | builtin_error ("(%ld) - %s", (long)pid, x); |
| 265 | } |