1 /* kill -- send a signal to a process
2 Copyright (C) 2002, 2003, 2004 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Paul Eggert. */
23 #include <sys/types.h>
27 # include <sys/wait.h>
30 # define WIFSIGNALED(s) (((s) & 0xFFFF) - 1 < (unsigned int) 0xFF)
33 # define WTERMSIG(s) ((s) & 0x7F)
40 /* The official name of this program (e.g., no `g' prefix). */
41 #define PROGRAM_NAME "kill"
43 #define AUTHORS "Paul Eggert"
45 #if ! (HAVE_DECL_STRTOIMAX || defined strtoimax)
46 intmax_t strtoimax ();
49 #if ! (HAVE_DECL_STRSIGNAL || defined strsignal)
50 # if ! (HAVE_DECL_SYS_SIGLIST || defined sys_siglist)
51 # if HAVE_DECL__SYS_SIGLIST || defined _sys_siglist
52 # define sys_siglist _sys_siglist
53 # elif HAVE_DECL___SYS_SIGLIST || defined __sys_siglist
54 # define sys_siglist __sys_siglist
57 # if HAVE_DECL_SYS_SIGLIST || defined sys_siglist
58 # define strsignal(signum) (0 <= (signum) && (signum) <= SIGNUM_BOUND \
59 ? sys_siglist[signum] \
63 # define strsignal(signum) 0
67 /* The name this program was run with, for error messages. */
70 static char const short_options
[] =
71 "0::1::2::3::4::5::6::7::8::9::"
72 "A::B::C::D::E::F::G::H::I::J::K::L::M::"
73 "N::O::P::Q::R::S::T::U::V::W::X::Y::Z::"
76 static struct option
const long_options
[] =
78 {"list", no_argument
, NULL
, 'l'},
79 {"signal", required_argument
, NULL
, 's'},
80 {"table", no_argument
, NULL
, 't'},
81 {GETOPT_HELP_OPTION_DECL
},
82 {GETOPT_VERSION_OPTION_DECL
},
89 if (status
!= EXIT_SUCCESS
)
90 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
95 Usage: %s [-s SIGNAL | -SIGNAL] PID...\n\
96 or: %s -l [SIGNAL]...\n\
97 or: %s -t [SIGNAL]...\n\
99 program_name
, program_name
, program_name
);
101 Send signals to processes, or list signals.\n\
105 Mandatory arguments to long options are mandatory for short options too.\n\
108 -s, --signal=SIGNAL, -SIGNAL\n\
109 specify the name or number of the signal to be sent\n\
110 -l, --list list signal names, or convert signal names to/from numbers\n\
111 -t, --table print a table of signal information\n\
113 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
114 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
116 SIGNAL may be a signal name like `HUP', or a signal number like `1',\n\
117 or an exit status of a process terminated by a signal.\n\
118 PID is an integer; if negative it identifies a process group.\n\
120 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
125 /* Convert OPERAND to a signal number with printable representation SIGNAME.
126 Return the signal number, or -1 if unsuccessful. */
129 operand2sig (char const *operand
, char *signame
)
133 if (ISDIGIT (*operand
))
136 long int l
= (errno
= 0, strtol (operand
, &endp
, 10));
138 signum
= (operand
== endp
|| *endp
|| errno
|| i
!= l
? -1
139 : WIFSIGNALED (i
) ? WTERMSIG (i
)
144 /* Convert signal to upper case in the C locale, not in the
145 current locale. Don't assume ASCII; it might be EBCDIC. */
146 char *upcased
= xstrdup (operand
);
148 for (p
= upcased
; *p
; p
++)
149 if (strchr ("abcdefghijklmnopqrstuvwxyz", *p
))
152 /* Look for the signal name, possibly prefixed by "SIG",
153 and possibly lowercased. */
154 if (! (str2sig (upcased
, &signum
) == 0
155 || (upcased
[0] == 'S' && upcased
[1] == 'I' && upcased
[2] == 'G'
156 && str2sig (upcased
+ 3, &signum
) == 0)))
162 if (signum
< 0 || sig2str (signum
, signame
) != 0)
164 error (0, 0, _("%s: invalid signal"), operand
);
171 /* Print a row of `kill -t' output. NUM_WIDTH is the maximum signal
172 number width, and SIGNUM is the signal number to print. The
173 maximum name width is NAME_WIDTH, and SIGNAME is the name to print. */
176 print_table_row (unsigned int num_width
, int signum
,
177 unsigned int name_width
, char const *signame
)
179 char const *description
= strsignal (signum
);
180 printf ("%*d %-*s %s\n", num_width
, signum
, name_width
, signame
,
181 description
? description
: "?");
184 /* Print a list of signal names. If TABLE, print a table.
185 Print the names specified by ARGV if nonzero; otherwise,
186 print all known names. Return a suitable exit status. */
189 list_signals (bool table
, char *const *argv
)
192 int status
= EXIT_SUCCESS
;
193 char signame
[SIG2STR_MAX
];
197 unsigned int name_width
= 0;
199 /* Compute the maximum width of a signal number. */
200 unsigned int num_width
= 1;
201 for (signum
= 1; signum
<= SIGNUM_BOUND
/ 10; signum
*= 10)
204 /* Compute the maximum width of a signal name. */
205 for (signum
= 1; signum
<= SIGNUM_BOUND
; signum
++)
206 if (sig2str (signum
, signame
) == 0)
208 size_t len
= strlen (signame
);
209 if (name_width
< len
)
214 for (; *argv
; argv
++)
216 signum
= operand2sig (*argv
, signame
);
218 status
= EXIT_FAILURE
;
220 print_table_row (num_width
, signum
, name_width
, signame
);
223 for (signum
= 1; signum
<= SIGNUM_BOUND
; signum
++)
224 if (sig2str (signum
, signame
) == 0)
225 print_table_row (num_width
, signum
, name_width
, signame
);
230 for (; *argv
; argv
++)
232 signum
= operand2sig (*argv
, signame
);
234 status
= EXIT_FAILURE
;
237 if (ISDIGIT (**argv
))
240 printf ("%d\n", signum
);
244 for (signum
= 1; signum
<= SIGNUM_BOUND
; signum
++)
245 if (sig2str (signum
, signame
) == 0)
252 /* Send signal SIGNUM to all the processes or process groups specified
253 by ARGV. Return a suitable exit status. */
256 send_signals (int signum
, char *const *argv
)
258 int status
= EXIT_SUCCESS
;
259 char const *arg
= *argv
;
264 intmax_t n
= (errno
= 0, strtoimax (arg
, &endp
, 10));
267 if (errno
== ERANGE
|| pid
!= n
|| arg
== endp
|| *endp
)
269 error (0, 0, _("%s: invalid process id"), arg
);
270 status
= EXIT_FAILURE
;
272 else if (kill (pid
, signum
) != 0)
274 error (0, errno
, "%s", arg
);
275 status
= EXIT_FAILURE
;
278 while ((arg
= *++argv
));
284 main (int argc
, char **argv
)
290 char signame
[SIG2STR_MAX
];
292 initialize_main (&argc
, &argv
);
293 program_name
= argv
[0];
294 setlocale (LC_ALL
, "");
295 bindtextdomain (PACKAGE
, LOCALEDIR
);
296 textdomain (PACKAGE
);
298 atexit (close_stdout
);
300 while ((optc
= getopt_long (argc
, argv
, short_options
, long_options
, NULL
))
304 case '0': case '1': case '2': case '3': case '4':
305 case '5': case '6': case '7': case '8': case '9':
308 /* This option is actually a process-id. */
310 goto no_more_options
;
313 case 'A': case 'B': case 'C': case 'D': case 'E':
314 case 'F': case 'G': case 'H': case 'I': case 'J':
315 case 'K': case 'L': case 'M': case 'N': case 'O':
316 case 'P': case 'Q': case 'R': case 'S': case 'T':
317 case 'U': case 'V': case 'W': case 'X': case 'Y':
320 optarg
= argv
[optind
- 1] + strlen (argv
[optind
- 1]);
321 if (optarg
!= argv
[optind
- 1] + 2)
323 error (0, 0, _("invalid option -- %c"), optc
);
324 usage (EXIT_FAILURE
);
328 case 'n': /* -n is not documented, but is for Bash compatibility. */
332 error (0, 0, _("%s: multiple signals specified"), optarg
);
333 usage (EXIT_FAILURE
);
335 signum
= operand2sig (optarg
, signame
);
337 usage (EXIT_FAILURE
);
346 error (0, 0, _("multiple -l or -t options specified"));
347 usage (EXIT_FAILURE
);
352 case_GETOPT_HELP_CHAR
;
353 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
355 usage (EXIT_FAILURE
);
363 error (0, 0, _("cannot combine signal with -l or -t"));
364 usage (EXIT_FAILURE
);
367 if ( ! list
&& argc
<= optind
)
369 error (0, 0, _("no process ID specified"));
370 usage (EXIT_FAILURE
);
374 ? list_signals (table
, optind
< argc
? argv
+ optind
: NULL
)
375 : send_signals (signum
, argv
+ optind
));