Imported from ../bash-2.01.tar.gz.
[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 job_control;
60 extern int posixly_correct;
61
62 #if !defined (CONTINUE_AFTER_KILL_ERROR)
63 #  define CONTINUE_OR_FAIL return (EXECUTION_FAILURE)
64 #else
65 #  define CONTINUE_OR_FAIL goto continue_killing
66 #endif /* CONTINUE_AFTER_KILL_ERROR */
67
68 /* Here is the kill builtin.  We only have it so that people can type
69    kill -KILL %1?  No, if you fill up the process table this way you
70    can still kill some. */
71 int
72 kill_builtin (list)
73      WORD_LIST *list;
74 {
75   int signal, any_succeeded, listing, saw_signal;
76   char *sigspec, *word;
77   pid_t pid;
78
79   if (list == 0)
80     return (EXECUTION_SUCCESS);
81
82   any_succeeded = listing = saw_signal = 0;
83   signal = SIGTERM;
84   sigspec = "TERM";
85
86   /* Process options. */
87   while (list)
88     {
89       word = list->word->word;
90
91       if (ISOPTION (word, 'l'))
92         {
93           listing++;
94           list = list->next;
95         }
96       else if (ISOPTION (word, 's') || ISOPTION (word, 'n'))
97         {
98           list = list->next;
99           if (list)
100             {
101               sigspec = list->word->word;
102               if (sigspec[0] == '0' && sigspec[1] == '\0')
103                 signal = 0;
104               else
105                 signal = decode_signal (sigspec);
106               list = list->next;
107             }
108           else
109             {
110               builtin_error ("%s requires an argument", word);
111               return (EXECUTION_FAILURE);
112             }
113         }
114       else if (ISOPTION (word, '-'))
115         {
116           list = list->next;
117           break;
118         }
119       else if (ISOPTION (word, '?'))
120         {
121           builtin_usage ();
122           return (EXECUTION_SUCCESS);
123         }
124       /* If this is a signal specification then process it.  We only process
125          the first one seen; other arguments may signify process groups (e.g,
126          -num == process group num). */
127       else if ((*word == '-') && !saw_signal)
128         {
129           sigspec = word + 1;
130           signal = decode_signal (sigspec);
131           saw_signal++;
132           list = list->next;
133         }
134       else
135         break;
136     }
137
138   if (listing)
139     return (display_signal_list (list, 0));
140
141   /* OK, we are killing processes. */
142   if (signal == NO_SIG)
143     {
144       builtin_error ("bad signal spec `%s'", sigspec);
145       return (EXECUTION_FAILURE);
146     }
147
148   while (list)
149     {
150       word = list->word->word;
151
152       if (*word == '-')
153         word++;
154
155       if (all_digits (word))
156         {
157           /* Use the entire argument in case of minus sign presence. */
158           pid = (pid_t) atoi (list->word->word);
159
160           if (kill_pid (pid, signal, 0) < 0)
161             goto signal_error;
162           else
163             any_succeeded++;
164         }
165       else if (*list->word->word != '%')
166         {
167           builtin_error ("%s: no such pid", list->word->word);
168           CONTINUE_OR_FAIL;
169         }
170       else if (interactive || job_control)
171         /* Posix.2 says you can kill without job control active (4.32.4) */
172         {                       /* Must be a job spec.  Check it out. */
173           int job;
174           sigset_t set, oset;
175
176           BLOCK_CHILD (set, oset);
177           job = get_job_spec (list);
178
179           if (job < 0 || job >= job_slots || !jobs[job])
180             {
181               if (job != DUP_JOB)
182                 builtin_error ("%s: no such job", list->word->word);
183               UNBLOCK_CHILD (oset);
184               CONTINUE_OR_FAIL;
185             }
186
187           /* Job spec used.  Kill the process group. If the job was started
188              without job control, then its pgrp == shell_pgrp, so we have
189              to be careful.  We take the pid of the first job in the pipeline
190              in that case. */
191           pid = IS_JOBCONTROL (job) ? jobs[job]->pgrp : jobs[job]->pipe->pid;
192
193           UNBLOCK_CHILD (oset);
194
195           if (kill_pid (pid, signal, 1) < 0)
196             {
197             signal_error:
198               if (errno == EPERM)
199                 builtin_error ("(%d) - Not owner", (int)pid);
200               else if (errno == ESRCH)
201                 builtin_error ("(%d) - No such pid", (int)pid);
202               else
203                 builtin_error ("Invalid signal %d", signal);
204               CONTINUE_OR_FAIL;
205             }
206           else
207             any_succeeded++;
208         }
209       else
210         {
211           builtin_error ("`%s' is not a pid or valid job spec", list->word->word);
212           CONTINUE_OR_FAIL;
213         }
214     continue_killing:
215       list = list->next;
216     }
217
218   return (any_succeeded ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
219 }
220 #endif /* JOB_CONTROL */