1 This file is kill.def, from which is created kill.c.
2 It implements the builtin "kill" in Bash.
4 Copyright (C) 1987-2005 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 it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25 $FUNCTION kill_builtin
26 $SHORT_DOC kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
27 Send the processes named by PID (or JOBSPEC) the signal SIGSPEC. If
28 SIGSPEC is not present, then SIGTERM is assumed. An argument of `-l'
29 lists the signal names; if arguments follow `-l' they are assumed to
30 be signal numbers for which names should be listed. Kill is a shell
31 builtin for two reasons: it allows job IDs to be used instead of
32 process IDs, and, if you have reached the limit on processes that
33 you can create, you don't have to start a process to kill another one.
40 #if defined (HAVE_UNISTD_H)
42 # include <sys/types.h>
47 #include "../bashansi.h"
48 #include "../bashintl.h"
55 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
60 extern int posixly_correct;
62 static void kill_error __P((pid_t, int));
64 #if !defined (CONTINUE_AFTER_KILL_ERROR)
65 # define CONTINUE_OR_FAIL return (EXECUTION_FAILURE)
67 # define CONTINUE_OR_FAIL goto continue_killing
68 #endif /* CONTINUE_AFTER_KILL_ERROR */
70 /* Here is the kill builtin. We only have it so that people can type
71 kill -KILL %1? No, if you fill up the process table this way you
72 can still kill some. */
77 int sig, any_succeeded, listing, saw_signal, dflags;
85 return (EXECUTION_FAILURE);
88 any_succeeded = listing = saw_signal = 0;
92 dflags = DSIG_NOCASE | ((posixly_correct == 0) ? DSIG_SIGPREFIX : 0);
93 /* Process options. */
96 word = list->word->word;
98 if (ISOPTION (word, 'l'))
103 else if (ISOPTION (word, 's') || ISOPTION (word, 'n'))
108 sigspec = list->word->word;
109 if (sigspec[0] == '0' && sigspec[1] == '\0')
112 sig = decode_signal (sigspec, dflags);
118 return (EXECUTION_FAILURE);
121 else if (ISOPTION (word, '-'))
126 else if (ISOPTION (word, '?'))
129 return (EXECUTION_SUCCESS);
131 /* If this is a signal specification then process it. We only process
132 the first one seen; other arguments may signify process groups (e.g,
133 -num == process group num). */
134 else if ((*word == '-') && !saw_signal)
137 sig = decode_signal (sigspec, dflags);
146 return (display_signal_list (list, 0));
148 /* OK, we are killing processes. */
151 sh_invalidsig (sigspec);
152 return (EXECUTION_FAILURE);
158 return (EXECUTION_FAILURE);
163 word = list->word->word;
168 /* Use the entire argument in case of minus sign presence. */
169 if (*word && legal_number (list->word->word, &pid_value) && (pid_value == (pid_t)pid_value))
171 pid = (pid_t) pid_value;
173 if (kill_pid (pid, sig, pid < -1) < 0)
176 sh_invalidsig (sigspec);
178 kill_error (pid, errno);
184 #if defined (JOB_CONTROL)
185 else if (*list->word->word && *list->word->word != '%')
187 builtin_error (_("%s: arguments must be process or job IDs"), list->word->word);
191 /* Posix.2 says you can kill without job control active (4.32.4) */
192 { /* Must be a job spec. Check it out. */
197 BLOCK_CHILD (set, oset);
198 job = get_job_spec (list);
200 if (INVALID_JOB (job))
203 sh_badjob (list->word->word);
204 UNBLOCK_CHILD (oset);
208 j = get_job_by_jid (job);
209 /* Job spec used. Kill the process group. If the job was started
210 without job control, then its pgrp == shell_pgrp, so we have
211 to be careful. We take the pid of the first job in the pipeline
213 pid = IS_JOBCONTROL (job) ? j->pgrp : j->pipe->pid;
215 UNBLOCK_CHILD (oset);
217 if (kill_pid (pid, sig, 1) < 0)
220 sh_invalidsig (sigspec);
222 kill_error (pid, errno);
228 #endif /* !JOB_CONTROL */
231 sh_badpid (list->word->word);
238 return (any_succeeded ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
250 x = _("Unknown error");
251 builtin_error ("(%ld) - %s", (long)pid, x);