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 | -sigspec] [pid | job]... | -l [signum]
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.
37 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
42 #include "../bashtypes.h"
49 #if defined (JOB_CONTROL)
50 extern int interactive;
51 extern int posixly_correct;
53 #if !defined (CONTINUE_AFTER_KILL_ERROR)
54 # define CONTINUE_OR_FAIL return (EXECUTION_FAILURE)
56 # define CONTINUE_OR_FAIL goto continue_killing
57 #endif /* CONTINUE_AFTER_KILL_ERROR */
59 /* Here is the kill builtin. We only have it so that people can type
60 kill -KILL %1? No, if you fill up the process table this way you
61 can still kill some. */
67 int any_succeeded = 0, listing = 0, saw_signal = 0;
68 char *sigspec = "TERM", *word;
72 return (EXECUTION_SUCCESS);
74 /* Process options. */
77 word = list->word->word;
79 if (ISOPTION (word, 'l'))
84 else if (ISOPTION (word, 's'))
89 sigspec = list->word->word;
90 if (sigspec[0] == '0' && !sigspec[1])
93 signal = decode_signal (sigspec);
98 builtin_error ("-s requires an argument");
99 return (EXECUTION_FAILURE);
102 else if (ISOPTION (word, '-'))
107 /* If this is a signal specification then process it. We only process
108 the first one seen; other arguments may signify process groups (e.g,
109 -num == process group num). */
110 else if ((*word == '-') && !saw_signal)
113 signal = decode_signal (sigspec);
126 register int column = 0;
129 for (i = 1; i < NSIG; i++)
131 name = signal_name (i);
132 if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
136 printf ("%s%s", name, (i == NSIG - 1) ? "" : " ");
139 printf ("%2d) %s", i, name);
151 if (posixly_correct || column != 0)
156 /* List individual signal names. */
162 if ((sscanf (list->word->word, "%d", &signum) != 1) ||
166 builtin_error ("bad signal number: %s", list->word->word);
171 /* This is specified by Posix.2 so that exit statuses can be
172 mapped into signal numbers. */
179 name = signal_name (signum);
180 if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
185 printf ("%s\n", name);
189 return (EXECUTION_SUCCESS);
192 /* OK, we are killing processes. */
193 if (signal == NO_SIG)
195 builtin_error ("bad signal spec `%s'", sigspec);
196 return (EXECUTION_FAILURE);
201 word = list->word->word;
206 if (all_digits (word))
208 /* Use the entire argument in case of minus sign presence. */
209 pid = (pid_t) atoi (list->word->word);
211 if (kill_pid (pid, signal, 0) < 0)
216 else if (*list->word->word != '%')
218 builtin_error ("No such pid %s", list->word->word);
222 else if (interactive)
223 /* Posix.2 says you can kill without job control active (4.32.4) */
225 else if (job_control) /* can't kill jobs if not using job control */
227 { /* Must be a job spec. Check it out. */
231 BLOCK_CHILD (set, oset);
232 job = get_job_spec (list);
234 if (job < 0 || job >= job_slots || !jobs[job])
237 builtin_error ("No such job %s", list->word->word);
238 UNBLOCK_CHILD (oset);
242 /* Job spec used. Kill the process group. If the job was started
243 without job control, then its pgrp == shell_pgrp, so we have
244 to be careful. We take the pid of the first job in the pipeline
246 if (jobs[job]->flags & J_JOBCONTROL)
247 pid = jobs[job]->pgrp;
249 pid = jobs[job]->pipe->pid;
251 UNBLOCK_CHILD (oset);
253 if (kill_pid (pid, signal, 1) < 0)
257 builtin_error ("(%d) - Not owner", (int)pid);
258 else if (errno == ESRCH)
259 builtin_error ("(%d) - No such pid", (int)pid);
261 builtin_error ("Invalid signal %d", signal);
269 builtin_error ("bad process specification `%s'", list->word->word);
277 return (EXECUTION_SUCCESS);
279 return (EXECUTION_FAILURE);
281 #endif /* JOB_CONTROL */