1 This file is kill.def, from which is created kill.c.
2 It implements the builtin "kill" in Bash.
4 Copyright (C) 1987-2009 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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/>.
24 $FUNCTION kill_builtin
25 $SHORT_DOC kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
26 Send a signal to a job.
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
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
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.
43 Returns success unless an invalid option is given or an error occurs.
50 #if defined (HAVE_UNISTD_H)
52 # include <sys/types.h>
57 #include "../bashansi.h"
58 #include "../bashintl.h"
65 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
70 extern int posixly_correct;
72 static void kill_error __P((pid_t, int));
74 #if !defined (CONTINUE_AFTER_KILL_ERROR)
75 # define CONTINUE_OR_FAIL return (EXECUTION_FAILURE)
77 # define CONTINUE_OR_FAIL goto continue_killing
78 #endif /* CONTINUE_AFTER_KILL_ERROR */
80 /* Here is the kill builtin. We only have it so that people can type
81 kill -KILL %1? No, if you fill up the process table this way you
82 can still kill some. */
87 int sig, any_succeeded, listing, saw_signal, dflags;
95 return (EXECUTION_FAILURE);
98 any_succeeded = listing = saw_signal = 0;
102 dflags = DSIG_NOCASE | ((posixly_correct == 0) ? DSIG_SIGPREFIX : 0);
103 /* Process options. */
106 word = list->word->word;
108 if (ISOPTION (word, 'l'))
113 else if (ISOPTION (word, 's') || ISOPTION (word, 'n'))
118 sigspec = list->word->word;
119 if (sigspec[0] == '0' && sigspec[1] == '\0')
122 sig = decode_signal (sigspec, dflags);
125 saw_signal++; /* XXX - for bash-4.2 */
131 return (EXECUTION_FAILURE);
134 else if (ISOPTION (word, '-'))
139 else if (ISOPTION (word, '?'))
142 return (EXECUTION_SUCCESS);
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). */
147 else if (*word == '-' && saw_signal == 0)
150 sig = decode_signal (sigspec, dflags);
159 return (display_signal_list (list, 0));
161 /* OK, we are killing processes. */
164 sh_invalidsig (sigspec);
165 return (EXECUTION_FAILURE);
171 return (EXECUTION_FAILURE);
176 word = list->word->word;
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))
184 pid = (pid_t) pid_value;
186 if (kill_pid (pid, sig, pid < -1) < 0)
189 sh_invalidsig (sigspec);
191 kill_error (pid, errno);
197 #if defined (JOB_CONTROL)
198 else if (*list->word->word && *list->word->word != '%')
200 builtin_error (_("%s: arguments must be process or job IDs"), list->word->word);
204 /* Posix.2 says you can kill without job control active (4.32.4) */
205 { /* Must be a job spec. Check it out. */
210 BLOCK_CHILD (set, oset);
211 job = get_job_spec (list);
213 if (INVALID_JOB (job))
216 sh_badjob (list->word->word);
217 UNBLOCK_CHILD (oset);
221 j = get_job_by_jid (job);
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
226 pid = IS_JOBCONTROL (job) ? j->pgrp : j->pipe->pid;
228 UNBLOCK_CHILD (oset);
230 if (kill_pid (pid, sig, 1) < 0)
233 sh_invalidsig (sigspec);
235 kill_error (pid, errno);
241 #endif /* !JOB_CONTROL */
244 sh_badpid (list->word->word);
251 return (any_succeeded ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
263 x = _("Unknown error");
264 builtin_error ("(%ld) - %s", (long)pid, x);