df: do not treat rootfs specially
[coreutils.git] / src / kill.c
blobd1b836b6bc3c848c1d07ac16a4f233c3cc9ea0b5
1 /* kill -- send a signal to a process
2 Copyright (C) 2002-2013 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>
25 #include "system.h"
26 #include "error.h"
27 #include "sig2str.h"
28 #include "operand2sig.h"
30 /* The official name of this program (e.g., no 'g' prefix). */
31 #define PROGRAM_NAME "kill"
33 #define AUTHORS proper_name ("Paul Eggert")
35 #if ! (HAVE_DECL_STRSIGNAL || defined strsignal)
36 # if ! (HAVE_DECL_SYS_SIGLIST || defined sys_siglist)
37 # if HAVE_DECL__SYS_SIGLIST || defined _sys_siglist
38 # define sys_siglist _sys_siglist
39 # elif HAVE_DECL___SYS_SIGLIST || defined __sys_siglist
40 # define sys_siglist __sys_siglist
41 # endif
42 # endif
43 # if HAVE_DECL_SYS_SIGLIST || defined sys_siglist
44 # define strsignal(signum) (0 <= (signum) && (signum) <= SIGNUM_BOUND \
45 ? sys_siglist[signum] \
46 : 0)
47 # endif
48 # ifndef strsignal
49 # define strsignal(signum) 0
50 # endif
51 #endif
53 static char const short_options[] =
54 "0::1::2::3::4::5::6::7::8::9::"
55 "A::B::C::D::E::F::G::H::I::J::K::L::M::"
56 "N::O::P::Q::R::S::T::U::V::W::X::Y::Z::"
57 "ln:s:t";
59 static struct option const long_options[] =
61 {"list", no_argument, NULL, 'l'},
62 {"signal", required_argument, NULL, 's'},
63 {"table", no_argument, NULL, 't'},
64 {GETOPT_HELP_OPTION_DECL},
65 {GETOPT_VERSION_OPTION_DECL},
66 {NULL, 0, NULL, 0}
69 void
70 usage (int status)
72 if (status != EXIT_SUCCESS)
73 emit_try_help ();
74 else
76 printf (_("\
77 Usage: %s [-s SIGNAL | -SIGNAL] PID...\n\
78 or: %s -l [SIGNAL]...\n\
79 or: %s -t [SIGNAL]...\n\
80 "),
81 program_name, program_name, program_name);
82 fputs (_("\
83 Send signals to processes, or list signals.\n\
84 "), stdout);
86 emit_mandatory_arg_note ();
88 fputs (_("\
89 -s, --signal=SIGNAL, -SIGNAL\n\
90 specify the name or number of the signal to be sent\n\
91 -l, --list list signal names, or convert signal names to/from numbers\n\
92 -t, --table print a table of signal information\n\
93 "), stdout);
94 fputs (HELP_OPTION_DESCRIPTION, stdout);
95 fputs (VERSION_OPTION_DESCRIPTION, stdout);
96 fputs (_("\n\
97 SIGNAL may be a signal name like 'HUP', or a signal number like '1',\n\
98 or the exit status of a process terminated by a signal.\n\
99 PID is an integer; if negative it identifies a process group.\n\
100 "), stdout);
101 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
102 emit_ancillary_info ();
104 exit (status);
107 /* Print a row of 'kill -t' output. NUM_WIDTH is the maximum signal
108 number width, and SIGNUM is the signal number to print. The
109 maximum name width is NAME_WIDTH, and SIGNAME is the name to print. */
111 static void
112 print_table_row (unsigned int num_width, int signum,
113 unsigned int name_width, char const *signame)
115 char const *description = strsignal (signum);
116 printf ("%*d %-*s %s\n", num_width, signum, name_width, signame,
117 description ? description : "?");
120 /* Print a list of signal names. If TABLE, print a table.
121 Print the names specified by ARGV if nonzero; otherwise,
122 print all known names. Return a suitable exit status. */
124 static int
125 list_signals (bool table, char *const *argv)
127 int signum;
128 int status = EXIT_SUCCESS;
129 char signame[SIG2STR_MAX];
131 if (table)
133 unsigned int name_width = 0;
135 /* Compute the maximum width of a signal number. */
136 unsigned int num_width = 1;
137 for (signum = 1; signum <= SIGNUM_BOUND / 10; signum *= 10)
138 num_width++;
140 /* Compute the maximum width of a signal name. */
141 for (signum = 1; signum <= SIGNUM_BOUND; signum++)
142 if (sig2str (signum, signame) == 0)
144 size_t len = strlen (signame);
145 if (name_width < len)
146 name_width = len;
149 if (argv)
150 for (; *argv; argv++)
152 signum = operand2sig (*argv, signame);
153 if (signum < 0)
154 status = EXIT_FAILURE;
155 else
156 print_table_row (num_width, signum, name_width, signame);
158 else
159 for (signum = 1; signum <= SIGNUM_BOUND; signum++)
160 if (sig2str (signum, signame) == 0)
161 print_table_row (num_width, signum, name_width, signame);
163 else
165 if (argv)
166 for (; *argv; argv++)
168 signum = operand2sig (*argv, signame);
169 if (signum < 0)
170 status = EXIT_FAILURE;
171 else
173 if (ISDIGIT (**argv))
174 puts (signame);
175 else
176 printf ("%d\n", signum);
179 else
180 for (signum = 1; signum <= SIGNUM_BOUND; signum++)
181 if (sig2str (signum, signame) == 0)
182 puts (signame);
185 return status;
188 /* Send signal SIGNUM to all the processes or process groups specified
189 by ARGV. Return a suitable exit status. */
191 static int
192 send_signals (int signum, char *const *argv)
194 int status = EXIT_SUCCESS;
195 char const *arg = *argv;
199 char *endp;
200 intmax_t n = (errno = 0, strtoimax (arg, &endp, 10));
201 pid_t pid = n;
203 if (errno == ERANGE || pid != n || arg == endp || *endp)
205 error (0, 0, _("%s: invalid process id"), arg);
206 status = EXIT_FAILURE;
208 else if (kill (pid, signum) != 0)
210 error (0, errno, "%s", arg);
211 status = EXIT_FAILURE;
214 while ((arg = *++argv));
216 return status;
220 main (int argc, char **argv)
222 int optc;
223 bool list = false;
224 bool table = false;
225 int signum = -1;
226 char signame[SIG2STR_MAX];
228 initialize_main (&argc, &argv);
229 set_program_name (argv[0]);
230 setlocale (LC_ALL, "");
231 bindtextdomain (PACKAGE, LOCALEDIR);
232 textdomain (PACKAGE);
234 atexit (close_stdout);
236 while ((optc = getopt_long (argc, argv, short_options, long_options, NULL))
237 != -1)
238 switch (optc)
240 case '0': case '1': case '2': case '3': case '4':
241 case '5': case '6': case '7': case '8': case '9':
242 if (optind != 2)
244 /* This option is actually a process-id. */
245 optind--;
246 goto no_more_options;
248 /* Fall through. */
249 case 'A': case 'B': case 'C': case 'D': case 'E':
250 case 'F': case 'G': case 'H': case 'I': case 'J':
251 case 'K': case 'L': case 'M': case 'N': case 'O':
252 case 'P': case 'Q': case 'R': case 'S': case 'T':
253 case 'U': case 'V': case 'W': case 'X': case 'Y':
254 case 'Z':
255 if (! optarg)
256 optarg = argv[optind - 1] + strlen (argv[optind - 1]);
257 if (optarg != argv[optind - 1] + 2)
259 error (0, 0, _("invalid option -- %c"), optc);
260 usage (EXIT_FAILURE);
262 optarg--;
263 /* Fall through. */
264 case 'n': /* -n is not documented, but is for Bash compatibility. */
265 case 's':
266 if (0 <= signum)
268 error (0, 0, _("%s: multiple signals specified"), optarg);
269 usage (EXIT_FAILURE);
271 signum = operand2sig (optarg, signame);
272 if (signum < 0)
273 usage (EXIT_FAILURE);
274 break;
276 case 't':
277 table = true;
278 /* Fall through. */
279 case 'l':
280 if (list)
282 error (0, 0, _("multiple -l or -t options specified"));
283 usage (EXIT_FAILURE);
285 list = true;
286 break;
288 case_GETOPT_HELP_CHAR;
289 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
290 default:
291 usage (EXIT_FAILURE);
293 no_more_options:
295 if (signum < 0)
296 signum = SIGTERM;
297 else if (list)
299 error (0, 0, _("cannot combine signal with -l or -t"));
300 usage (EXIT_FAILURE);
303 if ( ! list && argc <= optind)
305 error (0, 0, _("no process ID specified"));
306 usage (EXIT_FAILURE);
309 return (list
310 ? list_signals (table, optind < argc ? argv + optind : NULL)
311 : send_signals (signum, argv + optind));