ba779bf6e7f1e1bb3604cb2d175dc1ac622f8862
[platform/upstream/bash.git] / builtins / kill.def
1 This file is kill.def, from which is created kill.c.
2 It implements the builtin "kill" in Bash.
3
4 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
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
11 version.
12
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
16 for more details.
17
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.
21
22 $PRODUCES kill.c
23
24 $BUILTIN kill
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.
35 $END
36
37 #include <config.h>
38
39 #include <stdio.h>
40 #include <errno.h>
41 #if defined (HAVE_UNISTD_H)
42 #  include <unistd.h>
43 #endif
44
45 #include "../bashansi.h"
46
47 #include "../shell.h"
48 #include "../trap.h"
49 #include "../jobs.h"
50 #include "common.h"
51
52 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
53 #if !defined (errno)
54 extern int errno;
55 #endif /* !errno */
56
57 #if defined (JOB_CONTROL)
58 extern int interactive;
59 extern int posixly_correct;
60
61 #if !defined (CONTINUE_AFTER_KILL_ERROR)
62 #  define CONTINUE_OR_FAIL return (EXECUTION_FAILURE)
63 #else
64 #  define CONTINUE_OR_FAIL goto continue_killing
65 #endif /* CONTINUE_AFTER_KILL_ERROR */
66
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. */
70 int
71 kill_builtin (list)
72      WORD_LIST *list;
73 {
74   int signal, any_succeeded, listing, saw_signal;
75   char *sigspec, *word;
76   pid_t pid;
77
78   if (list == 0)
79     return (EXECUTION_SUCCESS);
80
81   any_succeeded = listing = saw_signal = 0;
82   signal = SIGTERM;
83   sigspec = "TERM";
84
85   /* Process options. */
86   while (list)
87     {
88       word = list->word->word;
89
90       if (ISOPTION (word, 'l'))
91         {
92           listing++;
93           list = list->next;
94         }
95       else if (ISOPTION (word, 's') || ISOPTION (word, 'n'))
96         {
97           list = list->next;
98           if (list)
99             {
100               sigspec = list->word->word;
101               if (sigspec[0] == '0' && sigspec[1] == '\0')
102                 signal = 0;
103               else
104                 signal = decode_signal (sigspec);
105               list = list->next;
106             }
107           else
108             {
109               builtin_error ("%s requires an argument", word);
110               return (EXECUTION_FAILURE);
111             }
112         }
113       else if (ISOPTION (word, '-'))
114         {
115           list = list->next;
116           break;
117         }
118       else if (ISOPTION (word, '?'))
119         {
120           builtin_usage ();
121           return (EXECUTION_SUCCESS);
122         }
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)
127         {
128           sigspec = word + 1;
129           signal = decode_signal (sigspec);
130           saw_signal++;
131           list = list->next;
132         }
133       else
134         break;
135     }
136
137   if (listing)
138     return (display_signal_list (list, 0));
139
140   /* OK, we are killing processes. */
141   if (signal == NO_SIG)
142     {
143       builtin_error ("bad signal spec `%s'", sigspec);
144       return (EXECUTION_FAILURE);
145     }
146
147   while (list)
148     {
149       word = list->word->word;
150
151       if (*word == '-')
152         word++;
153
154       if (all_digits (word))
155         {
156           /* Use the entire argument in case of minus sign presence. */
157           pid = (pid_t) atoi (list->word->word);
158
159           if (kill_pid (pid, signal, 0) < 0)
160             goto signal_error;
161           else
162             any_succeeded++;
163         }
164       else if (*list->word->word != '%')
165         {
166           builtin_error ("%s: no such pid", list->word->word);
167           CONTINUE_OR_FAIL;
168         }
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. */
172           int job;
173           sigset_t set, oset;
174
175           BLOCK_CHILD (set, oset);
176           job = get_job_spec (list);
177
178           if (job < 0 || job >= job_slots || !jobs[job])
179             {
180               if (job != DUP_JOB)
181                 builtin_error ("%s: no such job", list->word->word);
182               UNBLOCK_CHILD (oset);
183               CONTINUE_OR_FAIL;
184             }
185
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
189              in that case. */
190           pid = IS_JOBCONTROL (job) ? jobs[job]->pgrp : jobs[job]->pipe->pid;
191
192           UNBLOCK_CHILD (oset);
193
194           if (kill_pid (pid, signal, 1) < 0)
195             {
196             signal_error:
197               if (errno == EPERM)
198                 builtin_error ("(%d) - Not owner", (int)pid);
199               else if (errno == ESRCH)
200                 builtin_error ("(%d) - No such pid", (int)pid);
201               else
202                 builtin_error ("Invalid signal %d", signal);
203               CONTINUE_OR_FAIL;
204             }
205           else
206             any_succeeded++;
207         }
208       else
209         {
210           builtin_error ("`%s' is not a pid or valid job spec", list->word->word);
211           CONTINUE_OR_FAIL;
212         }
213     continue_killing:
214       list = list->next;
215     }
216
217   return (any_succeeded ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
218 }
219 #endif /* JOB_CONTROL */