Imported from ../bash-1.14.7.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 | -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.
35 $END
36
37 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
38 #if !defined (errno)
39 extern int errno;
40 #endif /* !errno */
41
42 #include "../bashtypes.h"
43 #include "../shell.h"
44 #include "../trap.h"
45 #include "../jobs.h"
46 #include "common.h"
47 #include <errno.h>
48
49 #if defined (JOB_CONTROL)
50 extern int interactive;
51 extern int posixly_correct;
52
53 #if !defined (CONTINUE_AFTER_KILL_ERROR)
54 #  define CONTINUE_OR_FAIL return (EXECUTION_FAILURE)
55 #else
56 #  define CONTINUE_OR_FAIL goto continue_killing
57 #endif /* CONTINUE_AFTER_KILL_ERROR */
58
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. */
62 int
63 kill_builtin (list)
64      WORD_LIST *list;
65 {
66   int signal = SIGTERM;
67   int any_succeeded = 0, listing = 0, saw_signal = 0;
68   char *sigspec = "TERM", *word;
69   pid_t pid;
70
71   if (!list)
72     return (EXECUTION_SUCCESS);
73
74   /* Process options. */
75   while (list)
76     {
77       word = list->word->word;
78
79       if (ISOPTION (word, 'l'))
80         {
81           listing++;
82           list = list->next;
83         }
84       else if (ISOPTION (word, 's'))
85         {
86           list = list->next;
87           if (list)
88             {
89               sigspec = list->word->word;
90               if (sigspec[0] == '0' && !sigspec[1])
91                 signal = 0;
92               else
93                 signal = decode_signal (sigspec);
94               list = list->next;
95             }
96           else
97             {
98               builtin_error ("-s requires an argument");
99               return (EXECUTION_FAILURE);
100             }
101         }
102       else if (ISOPTION (word, '-'))
103         {
104           list = list->next;
105           break;
106         }
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)
111         {
112           sigspec = word + 1;
113           signal = decode_signal (sigspec);
114           saw_signal++;
115           list = list->next;
116         }
117       else
118         break;
119     }
120
121   if (listing)
122     {
123       if (!list)
124         {
125           register int i;
126           register int column = 0;
127           char *name;
128
129           for (i = 1; i < NSIG; i++)
130             {
131               name = signal_name (i);
132               if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
133                 continue;
134
135               if (posixly_correct)
136                 printf ("%s%s", name, (i == NSIG - 1) ? "" : " ");
137               else
138                 {
139                   printf ("%2d) %s", i, name);
140
141                   if (++column < 4)
142                     printf ("\t");
143                   else
144                     {
145                       printf ("\n");
146                       column = 0;
147                     }
148                 }
149             }
150
151           if (posixly_correct || column != 0)
152             printf ("\n");
153         }
154       else
155         {
156           /* List individual signal names. */
157           while (list)
158             {
159               int signum;
160               char *name;
161
162               if ((sscanf (list->word->word, "%d", &signum) != 1) ||
163                   (signum <= 0))
164                 {
165             list_error:
166                   builtin_error ("bad signal number: %s", list->word->word);
167                   list = list->next;
168                   continue;
169                 }
170
171               /* This is specified by Posix.2 so that exit statuses can be
172                  mapped into signal numbers. */
173               if (signum > 128)
174                 signum -= 128;
175
176               if (signum >= NSIG)
177                 goto list_error;
178
179               name = signal_name (signum);
180               if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
181                 {
182                   list = list->next;
183                   continue;
184                 }
185               printf ("%s\n", name);
186               list = list->next;
187             }
188         }
189       return (EXECUTION_SUCCESS);
190     }
191
192   /* OK, we are killing processes. */
193   if (signal == NO_SIG)
194     {
195       builtin_error ("bad signal spec `%s'", sigspec);
196       return (EXECUTION_FAILURE);
197     }
198
199   while (list)
200     {
201       word = list->word->word;
202
203       if (*word == '-')
204         word++;
205
206       if (all_digits (word))
207         {
208           /* Use the entire argument in case of minus sign presence. */
209           pid = (pid_t) atoi (list->word->word);
210
211           if (kill_pid (pid, signal, 0) < 0)
212             goto signal_error;
213           else
214             any_succeeded++;
215         }
216       else if (*list->word->word != '%')
217         {
218           builtin_error ("No such pid %s", list->word->word);
219           CONTINUE_OR_FAIL;
220         }
221 #if 1
222       else if (interactive)
223         /* Posix.2 says you can kill without job control active (4.32.4) */
224 #else
225       else if (job_control)     /* can't kill jobs if not using job control */
226 #endif
227         {                       /* Must be a job spec.  Check it out. */
228           int job;
229           sigset_t set, oset;
230
231           BLOCK_CHILD (set, oset);
232           job = get_job_spec (list);
233
234           if (job < 0 || job >= job_slots || !jobs[job])
235             {
236               if (job != DUP_JOB)
237                 builtin_error ("No such job %s", list->word->word);
238               UNBLOCK_CHILD (oset);
239               CONTINUE_OR_FAIL;
240             }
241
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
245              in that case. */
246           if (jobs[job]->flags & J_JOBCONTROL)
247             pid = jobs[job]->pgrp;
248           else
249             pid = jobs[job]->pipe->pid;
250
251           UNBLOCK_CHILD (oset);
252
253           if (kill_pid (pid, signal, 1) < 0)
254             {
255             signal_error:
256               if (errno == EPERM)
257                 builtin_error ("(%d) - Not owner", (int)pid);
258               else if (errno == ESRCH)
259                 builtin_error ("(%d) - No such pid", (int)pid);
260               else
261                 builtin_error ("Invalid signal %d", signal);
262               CONTINUE_OR_FAIL;
263             }
264           else
265             any_succeeded++;
266         }
267       else
268         {
269           builtin_error ("bad process specification `%s'", list->word->word);
270           CONTINUE_OR_FAIL;
271         }
272     continue_killing:
273       list = list->next;
274     }
275
276   if (any_succeeded)
277     return (EXECUTION_SUCCESS);
278   else
279     return (EXECUTION_FAILURE);
280 }
281 #endif /* JOB_CONTROL */