blob: 902453587d6f6099178559a75f746f123b30063c [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001This file is kill.def, from which is created kill.c.
2It implements the builtin "kill" in Bash.
3
Chet Ramey495aee42011-11-22 19:11:26 -05004Copyright (C) 1987-2010 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00005
6This file is part of GNU Bash, the Bourne Again SHell.
7
Jari Aalto31859422009-01-12 13:36:28 +00008Bash is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000012
Jari Aalto31859422009-01-12 13:36:28 +000013Bash is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000017
Jari Aalto31859422009-01-12 13:36:28 +000018You should have received a copy of the GNU General Public License
19along with Bash. If not, see <http://www.gnu.org/licenses/>.
Jari Aalto726f6381996-08-26 18:22:31 +000020
21$PRODUCES kill.c
22
23$BUILTIN kill
24$FUNCTION kill_builtin
Jari Aalto95732b42005-12-07 14:08:12 +000025$SHORT_DOC kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
Jari Aalto31859422009-01-12 13:36:28 +000026Send a signal to a job.
27
28Send the processes identified by PID or JOBSPEC the signal named by
29SIGSPEC or SIGNUM. If neither SIGSPEC nor SIGNUM is present, then
30SIGTERM is assumed.
31
32Options:
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
38Kill is a shell builtin for two reasons: it allows job IDs to be used
39instead of process IDs, and allows processes to be killed if the limit
40on processes that you can create is reached.
41
42Exit Status:
43Returns success unless an invalid option is given or an error occurs.
Jari Aalto726f6381996-08-26 18:22:31 +000044$END
45
Jari Aaltoccc6cda1996-12-23 17:02:34 +000046#include <config.h>
Jari Aalto726f6381996-08-26 18:22:31 +000047
Jari Aaltoccc6cda1996-12-23 17:02:34 +000048#include <stdio.h>
49#include <errno.h>
50#if defined (HAVE_UNISTD_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000051# ifdef _MINIX
52# include <sys/types.h>
53# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +000054# include <unistd.h>
55#endif
56
57#include "../bashansi.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000058#include "../bashintl.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000059
Chet Rameyac50fba2014-02-26 09:36:43 -050060#include <signal.h>
61
Jari Aalto726f6381996-08-26 18:22:31 +000062#include "../shell.h"
63#include "../trap.h"
64#include "../jobs.h"
65#include "common.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000066
67/* Not all systems declare ERRNO in errno.h... and some systems #define it! */
68#if !defined (errno)
Ricardo Cerqueiraa02fbff2013-07-25 22:35:34 +010069#include <errno.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000070#endif /* !errno */
Jari Aalto726f6381996-08-26 18:22:31 +000071
Jari Aalto726f6381996-08-26 18:22:31 +000072extern int posixly_correct;
73
Jari Aaltob80f6442004-07-27 13:29:18 +000074static void kill_error __P((pid_t, int));
75
Jari Aalto726f6381996-08-26 18:22:31 +000076#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. */
85int
86kill_builtin (list)
87 WORD_LIST *list;
88{
Jari Aaltob80f6442004-07-27 13:29:18 +000089 int sig, any_succeeded, listing, saw_signal, dflags;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000090 char *sigspec, *word;
Jari Aalto726f6381996-08-26 18:22:31 +000091 pid_t pid;
Jari Aalto7117c2d2002-07-17 14:10:11 +000092 intmax_t pid_value;
Jari Aalto726f6381996-08-26 18:22:31 +000093
Jari Aaltoccc6cda1996-12-23 17:02:34 +000094 if (list == 0)
Jari Aaltocce855b1998-04-17 19:52:44 +000095 {
96 builtin_usage ();
Chet Rameyac50fba2014-02-26 09:36:43 -050097 return (EX_USAGE);
Jari Aaltocce855b1998-04-17 19:52:44 +000098 }
Jari Aalto726f6381996-08-26 18:22:31 +000099
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000100 any_succeeded = listing = saw_signal = 0;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000101 sig = SIGTERM;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000102 sigspec = "TERM";
103
Jari Aaltob80f6442004-07-27 13:29:18 +0000104 dflags = DSIG_NOCASE | ((posixly_correct == 0) ? DSIG_SIGPREFIX : 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000105 /* 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 Aaltoccc6cda1996-12-23 17:02:34 +0000115 else if (ISOPTION (word, 's') || ISOPTION (word, 'n'))
Jari Aalto726f6381996-08-26 18:22:31 +0000116 {
117 list = list->next;
118 if (list)
119 {
120 sigspec = list->word->word;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000121 if (sigspec[0] == '0' && sigspec[1] == '\0')
Jari Aalto7117c2d2002-07-17 14:10:11 +0000122 sig = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000123 else
Jari Aaltob80f6442004-07-27 13:29:18 +0000124 sig = decode_signal (sigspec, dflags);
Jari Aalto726f6381996-08-26 18:22:31 +0000125 list = list->next;
Chet Ramey495aee42011-11-22 19:11:26 -0500126 saw_signal++;
Jari Aalto726f6381996-08-26 18:22:31 +0000127 }
128 else
129 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000130 sh_needarg (word);
Jari Aalto726f6381996-08-26 18:22:31 +0000131 return (EXECUTION_FAILURE);
132 }
133 }
134 else if (ISOPTION (word, '-'))
135 {
136 list = list->next;
137 break;
138 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000139 else if (ISOPTION (word, '?'))
140 {
141 builtin_usage ();
Chet Rameyac50fba2014-02-26 09:36:43 -0500142 return (EX_USAGE);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000143 }
Jari Aalto726f6381996-08-26 18:22:31 +0000144 /* 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 Ramey00018032011-11-21 20:51:19 -0500147 else if (*word == '-' && saw_signal == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000148 {
149 sigspec = word + 1;
Jari Aaltob80f6442004-07-27 13:29:18 +0000150 sig = decode_signal (sigspec, dflags);
Jari Aalto726f6381996-08-26 18:22:31 +0000151 saw_signal++;
152 list = list->next;
153 }
154 else
155 break;
156 }
157
158 if (listing)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000159 return (display_signal_list (list, 0));
Jari Aalto726f6381996-08-26 18:22:31 +0000160
161 /* OK, we are killing processes. */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000162 if (sig == NO_SIG)
Jari Aalto726f6381996-08-26 18:22:31 +0000163 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000164 sh_invalidsig (sigspec);
Jari Aalto726f6381996-08-26 18:22:31 +0000165 return (EXECUTION_FAILURE);
166 }
167
Jari Aaltocce855b1998-04-17 19:52:44 +0000168 if (list == 0)
169 {
170 builtin_usage ();
Chet Rameyac50fba2014-02-26 09:36:43 -0500171 return (EX_USAGE);
Jari Aaltocce855b1998-04-17 19:52:44 +0000172 }
173
Jari Aalto726f6381996-08-26 18:22:31 +0000174 while (list)
175 {
176 word = list->word->word;
177
178 if (*word == '-')
179 word++;
180
Jari Aalto7117c2d2002-07-17 14:10:11 +0000181 /* 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 Aalto726f6381996-08-26 18:22:31 +0000183 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000184 pid = (pid_t) pid_value;
Jari Aalto726f6381996-08-26 18:22:31 +0000185
Jari Aalto95732b42005-12-07 14:08:12 +0000186 if (kill_pid (pid, sig, pid < -1) < 0)
Jari Aaltob80f6442004-07-27 13:29:18 +0000187 {
188 if (errno == EINVAL)
189 sh_invalidsig (sigspec);
190 else
191 kill_error (pid, errno);
192 CONTINUE_OR_FAIL;
193 }
Jari Aalto726f6381996-08-26 18:22:31 +0000194 else
195 any_succeeded++;
196 }
Jari Aaltob80f6442004-07-27 13:29:18 +0000197#if defined (JOB_CONTROL)
Jari Aaltocce855b1998-04-17 19:52:44 +0000198 else if (*list->word->word && *list->word->word != '%')
Jari Aalto726f6381996-08-26 18:22:31 +0000199 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000200 builtin_error (_("%s: arguments must be process or job IDs"), list->word->word);
Jari Aalto726f6381996-08-26 18:22:31 +0000201 CONTINUE_OR_FAIL;
202 }
Jari Aaltob80f6442004-07-27 13:29:18 +0000203 else if (*word)
Jari Aalto726f6381996-08-26 18:22:31 +0000204 /* Posix.2 says you can kill without job control active (4.32.4) */
Jari Aalto726f6381996-08-26 18:22:31 +0000205 { /* Must be a job spec. Check it out. */
206 int job;
207 sigset_t set, oset;
Jari Aalto95732b42005-12-07 14:08:12 +0000208 JOB *j;
Jari Aalto726f6381996-08-26 18:22:31 +0000209
210 BLOCK_CHILD (set, oset);
211 job = get_job_spec (list);
212
Jari Aalto95732b42005-12-07 14:08:12 +0000213 if (INVALID_JOB (job))
Jari Aalto726f6381996-08-26 18:22:31 +0000214 {
215 if (job != DUP_JOB)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000216 sh_badjob (list->word->word);
Jari Aalto726f6381996-08-26 18:22:31 +0000217 UNBLOCK_CHILD (oset);
218 CONTINUE_OR_FAIL;
219 }
220
Jari Aalto95732b42005-12-07 14:08:12 +0000221 j = get_job_by_jid (job);
Jari Aalto726f6381996-08-26 18:22:31 +0000222 /* 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 Aalto95732b42005-12-07 14:08:12 +0000226 pid = IS_JOBCONTROL (job) ? j->pgrp : j->pipe->pid;
Jari Aalto726f6381996-08-26 18:22:31 +0000227
228 UNBLOCK_CHILD (oset);
229
Jari Aalto7117c2d2002-07-17 14:10:11 +0000230 if (kill_pid (pid, sig, 1) < 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000231 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000232 if (errno == EINVAL)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000233 sh_invalidsig (sigspec);
Jari Aaltof73dda02001-11-13 17:56:06 +0000234 else
Jari Aaltob80f6442004-07-27 13:29:18 +0000235 kill_error (pid, errno);
Jari Aalto726f6381996-08-26 18:22:31 +0000236 CONTINUE_OR_FAIL;
237 }
238 else
239 any_succeeded++;
240 }
Jari Aaltob80f6442004-07-27 13:29:18 +0000241#endif /* !JOB_CONTROL */
Jari Aalto726f6381996-08-26 18:22:31 +0000242 else
243 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000244 sh_badpid (list->word->word);
Jari Aalto726f6381996-08-26 18:22:31 +0000245 CONTINUE_OR_FAIL;
246 }
247 continue_killing:
248 list = list->next;
249 }
250
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000251 return (any_succeeded ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
Jari Aalto726f6381996-08-26 18:22:31 +0000252}
Jari Aaltob80f6442004-07-27 13:29:18 +0000253
254static void
255kill_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}