1 This file is kill.def, from which is created kill.c.
2 It implements the builtin "kill" in Bash.
4 Copyright (C) 1987, 1989, 1991 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
25 $FUNCTION kill_builtin
26 $DEPENDS_ON JOB_CONTROL
27 $SHORT_DOC kill [-s sigspec | -n signum | -sigspec] [pid | job]... or kill -l [sigspec]
28 Send the processes named by PID (or JOB) the signal SIGSPEC. If
29 SIGSPEC is not present, then SIGTERM is assumed. An argument of `-l'
30 lists the signal names; if arguments follow `-l' they are assumed to
31 be signal numbers for which names should be listed. Kill is a shell
32 builtin for two reasons: it allows job IDs to be used instead of
33 process IDs, and, if you have reached the limit on processes that
34 you can create, you don't have to start a process to kill another one.
41 #if defined (HAVE_UNISTD_H)
45 #include "../bashansi.h"
52 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
57 #if defined (JOB_CONTROL)
58 extern int interactive;
59 extern int posixly_correct;
61 #if !defined (CONTINUE_AFTER_KILL_ERROR)
62 # define CONTINUE_OR_FAIL return (EXECUTION_FAILURE)
64 # define CONTINUE_OR_FAIL goto continue_killing
65 #endif /* CONTINUE_AFTER_KILL_ERROR */
67 /* Here is the kill builtin. We only have it so that people can type
68 kill -KILL %1? No, if you fill up the process table this way you
69 can still kill some. */
74 int signal, any_succeeded, listing, saw_signal;
79 return (EXECUTION_SUCCESS);
81 any_succeeded = listing = saw_signal = 0;
85 /* Process options. */
88 word = list->word->word;
90 if (ISOPTION (word, 'l'))
95 else if (ISOPTION (word, 's') || ISOPTION (word, 'n'))
100 sigspec = list->word->word;
101 if (sigspec[0] == '0' && sigspec[1] == '\0')
104 signal = decode_signal (sigspec);
109 builtin_error ("%s requires an argument", word);
110 return (EXECUTION_FAILURE);
113 else if (ISOPTION (word, '-'))
118 else if (ISOPTION (word, '?'))
121 return (EXECUTION_SUCCESS);
123 /* If this is a signal specification then process it. We only process
124 the first one seen; other arguments may signify process groups (e.g,
125 -num == process group num). */
126 else if ((*word == '-') && !saw_signal)
129 signal = decode_signal (sigspec);
138 return (display_signal_list (list, 0));
140 /* OK, we are killing processes. */
141 if (signal == NO_SIG)
143 builtin_error ("bad signal spec `%s'", sigspec);
144 return (EXECUTION_FAILURE);
149 word = list->word->word;
154 if (all_digits (word))
156 /* Use the entire argument in case of minus sign presence. */
157 pid = (pid_t) atoi (list->word->word);
159 if (kill_pid (pid, signal, 0) < 0)
164 else if (*list->word->word != '%')
166 builtin_error ("%s: no such pid", list->word->word);
169 else if (interactive)
170 /* Posix.2 says you can kill without job control active (4.32.4) */
171 { /* Must be a job spec. Check it out. */
175 BLOCK_CHILD (set, oset);
176 job = get_job_spec (list);
178 if (job < 0 || job >= job_slots || !jobs[job])
181 builtin_error ("%s: no such job", list->word->word);
182 UNBLOCK_CHILD (oset);
186 /* Job spec used. Kill the process group. If the job was started
187 without job control, then its pgrp == shell_pgrp, so we have
188 to be careful. We take the pid of the first job in the pipeline
190 pid = IS_JOBCONTROL (job) ? jobs[job]->pgrp : jobs[job]->pipe->pid;
192 UNBLOCK_CHILD (oset);
194 if (kill_pid (pid, signal, 1) < 0)
198 builtin_error ("(%d) - Not owner", (int)pid);
199 else if (errno == ESRCH)
200 builtin_error ("(%d) - No such pid", (int)pid);
202 builtin_error ("Invalid signal %d", signal);
210 builtin_error ("`%s' is not a pid or valid job spec", list->word->word);
217 return (any_succeeded ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
219 #endif /* JOB_CONTROL */