build: support fully excluding stdbuf from the build
[coreutils/ericb.git] / src / kill.c
blob4210c37b5db64fcda291cf03481cbfbe8beb01cc
1 /* kill -- send a signal to a process
2 Copyright (C) 2002-2005, 2008-2010 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Paul Eggert. */
19 #include <config.h>
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <sys/types.h>
23 #include <signal.h>
24 #include <sys/wait.h>
26 #include "system.h"
27 #include "error.h"
28 #include "sig2str.h"
29 #include "operand2sig.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "kill"
34 #define AUTHORS proper_name ("Paul Eggert")
36 #if ! (HAVE_DECL_STRSIGNAL || defined strsignal)
37 # if ! (HAVE_DECL_SYS_SIGLIST || defined sys_siglist)
38 # if HAVE_DECL__SYS_SIGLIST || defined _sys_siglist
39 # define sys_siglist _sys_siglist
40 # elif HAVE_DECL___SYS_SIGLIST || defined __sys_siglist
41 # define sys_siglist __sys_siglist
42 # endif
43 # endif
44 # if HAVE_DECL_SYS_SIGLIST || defined sys_siglist
45 # define strsignal(signum) (0 <= (signum) && (signum) <= SIGNUM_BOUND \
46 ? sys_siglist[signum] \
47 : 0)
48 # endif
49 # ifndef strsignal
50 # define strsignal(signum) 0
51 # endif
52 #endif
54 static char const short_options[] =
55 "0::1::2::3::4::5::6::7::8::9::"
56 "A::B::C::D::E::F::G::H::I::J::K::L::M::"
57 "N::O::P::Q::R::S::T::U::V::W::X::Y::Z::"
58 "ln:s:t";
60 static struct option const long_options[] =
62 {"list", no_argument, NULL, 'l'},
63 {"signal", required_argument, NULL, 's'},
64 {"table", no_argument, NULL, 't'},
65 {GETOPT_HELP_OPTION_DECL},
66 {GETOPT_VERSION_OPTION_DECL},
67 {NULL, 0, NULL, 0}
70 void
71 usage (int status)
73 if (status != EXIT_SUCCESS)
74 fprintf (stderr, _("Try `%s --help' for more information.\n"),
75 program_name);
76 else
78 printf (_("\
79 Usage: %s [-s SIGNAL | -SIGNAL] PID...\n\
80 or: %s -l [SIGNAL]...\n\
81 or: %s -t [SIGNAL]...\n\
82 "),
83 program_name, program_name, program_name);
84 fputs (_("\
85 Send signals to processes, or list signals.\n\
86 \n\
87 "), stdout);
88 fputs (_("\
89 Mandatory arguments to long options are mandatory for short options too.\n\
90 "), stdout);
91 fputs (_("\
92 -s, --signal=SIGNAL, -SIGNAL\n\
93 specify the name or number of the signal to be sent\n\
94 -l, --list list signal names, or convert signal names to/from numbers\n\
95 -t, --table print a table of signal information\n\
96 "), stdout);
97 fputs (HELP_OPTION_DESCRIPTION, stdout);
98 fputs (VERSION_OPTION_DESCRIPTION, stdout);
99 fputs (_("\n\
100 SIGNAL may be a signal name like `HUP', or a signal number like `1',\n\
101 or the exit status of a process terminated by a signal.\n\
102 PID is an integer; if negative it identifies a process group.\n\
103 "), stdout);
104 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
105 emit_ancillary_info ();
107 exit (status);
110 /* Print a row of `kill -t' output. NUM_WIDTH is the maximum signal
111 number width, and SIGNUM is the signal number to print. The
112 maximum name width is NAME_WIDTH, and SIGNAME is the name to print. */
114 static void
115 print_table_row (unsigned int num_width, int signum,
116 unsigned int name_width, char const *signame)
118 char const *description = strsignal (signum);
119 printf ("%*d %-*s %s\n", num_width, signum, name_width, signame,
120 description ? description : "?");
123 /* Print a list of signal names. If TABLE, print a table.
124 Print the names specified by ARGV if nonzero; otherwise,
125 print all known names. Return a suitable exit status. */
127 static int
128 list_signals (bool table, char *const *argv)
130 int signum;
131 int status = EXIT_SUCCESS;
132 char signame[SIG2STR_MAX];
134 if (table)
136 unsigned int name_width = 0;
138 /* Compute the maximum width of a signal number. */
139 unsigned int num_width = 1;
140 for (signum = 1; signum <= SIGNUM_BOUND / 10; signum *= 10)
141 num_width++;
143 /* Compute the maximum width of a signal name. */
144 for (signum = 1; signum <= SIGNUM_BOUND; signum++)
145 if (sig2str (signum, signame) == 0)
147 size_t len = strlen (signame);
148 if (name_width < len)
149 name_width = len;
152 if (argv)
153 for (; *argv; argv++)
155 signum = operand2sig (*argv, signame);
156 if (signum < 0)
157 status = EXIT_FAILURE;
158 else
159 print_table_row (num_width, signum, name_width, signame);
161 else
162 for (signum = 1; signum <= SIGNUM_BOUND; signum++)
163 if (sig2str (signum, signame) == 0)
164 print_table_row (num_width, signum, name_width, signame);
166 else
168 if (argv)
169 for (; *argv; argv++)
171 signum = operand2sig (*argv, signame);
172 if (signum < 0)
173 status = EXIT_FAILURE;
174 else
176 if (ISDIGIT (**argv))
177 puts (signame);
178 else
179 printf ("%d\n", signum);
182 else
183 for (signum = 1; signum <= SIGNUM_BOUND; signum++)
184 if (sig2str (signum, signame) == 0)
185 puts (signame);
188 return status;
191 /* Send signal SIGNUM to all the processes or process groups specified
192 by ARGV. Return a suitable exit status. */
194 static int
195 send_signals (int signum, char *const *argv)
197 int status = EXIT_SUCCESS;
198 char const *arg = *argv;
202 char *endp;
203 intmax_t n = (errno = 0, strtoimax (arg, &endp, 10));
204 pid_t pid = n;
206 if (errno == ERANGE || pid != n || arg == endp || *endp)
208 error (0, 0, _("%s: invalid process id"), arg);
209 status = EXIT_FAILURE;
211 else if (kill (pid, signum) != 0)
213 error (0, errno, "%s", arg);
214 status = EXIT_FAILURE;
217 while ((arg = *++argv));
219 return status;
223 main (int argc, char **argv)
225 int optc;
226 bool list = false;
227 bool table = false;
228 int signum = -1;
229 char signame[SIG2STR_MAX];
231 initialize_main (&argc, &argv);
232 set_program_name (argv[0]);
233 setlocale (LC_ALL, "");
234 bindtextdomain (PACKAGE, LOCALEDIR);
235 textdomain (PACKAGE);
237 atexit (close_stdout);
239 while ((optc = getopt_long (argc, argv, short_options, long_options, NULL))
240 != -1)
241 switch (optc)
243 case '0': case '1': case '2': case '3': case '4':
244 case '5': case '6': case '7': case '8': case '9':
245 if (optind != 2)
247 /* This option is actually a process-id. */
248 optind--;
249 goto no_more_options;
251 /* Fall through. */
252 case 'A': case 'B': case 'C': case 'D': case 'E':
253 case 'F': case 'G': case 'H': case 'I': case 'J':
254 case 'K': case 'L': case 'M': case 'N': case 'O':
255 case 'P': case 'Q': case 'R': case 'S': case 'T':
256 case 'U': case 'V': case 'W': case 'X': case 'Y':
257 case 'Z':
258 if (! optarg)
259 optarg = argv[optind - 1] + strlen (argv[optind - 1]);
260 if (optarg != argv[optind - 1] + 2)
262 error (0, 0, _("invalid option -- %c"), optc);
263 usage (EXIT_FAILURE);
265 optarg--;
266 /* Fall through. */
267 case 'n': /* -n is not documented, but is for Bash compatibility. */
268 case 's':
269 if (0 <= signum)
271 error (0, 0, _("%s: multiple signals specified"), optarg);
272 usage (EXIT_FAILURE);
274 signum = operand2sig (optarg, signame);
275 if (signum < 0)
276 usage (EXIT_FAILURE);
277 break;
279 case 't':
280 table = true;
281 /* Fall through. */
282 case 'l':
283 if (list)
285 error (0, 0, _("multiple -l or -t options specified"));
286 usage (EXIT_FAILURE);
288 list = true;
289 break;
291 case_GETOPT_HELP_CHAR;
292 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
293 default:
294 usage (EXIT_FAILURE);
296 no_more_options:;
298 if (signum < 0)
299 signum = SIGTERM;
300 else if (list)
302 error (0, 0, _("cannot combine signal with -l or -t"));
303 usage (EXIT_FAILURE);
306 if ( ! list && argc <= optind)
308 error (0, 0, _("no process ID specified"));
309 usage (EXIT_FAILURE);
312 return (list
313 ? list_signals (table, optind < argc ? argv + optind : NULL)
314 : send_signals (signum, argv + optind));