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