1 /* kill -- send a signal to a process
2 Copyright (C) 2002-2024 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Paul Eggert. */
22 #include <sys/types.h>
27 #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 static char const short_options
[] =
36 "0::1::2::3::4::5::6::7::8::9::"
37 "A::B::C::D::E::F::G::H::I::J::K::M::"
38 "N::O::P::Q::R::S::T::U::V::W::X::Y::Z::"
41 static struct option
const long_options
[] =
43 {"list", no_argument
, nullptr, 'l'},
44 {"signal", required_argument
, nullptr, 's'},
45 {"table", no_argument
, nullptr, 't'},
46 {GETOPT_HELP_OPTION_DECL
},
47 {GETOPT_VERSION_OPTION_DECL
},
48 {nullptr, 0, nullptr, 0}
54 if (status
!= EXIT_SUCCESS
)
59 Usage: %s [-s SIGNAL | -SIGNAL] PID...\n\
60 or: %s -l [SIGNAL]...\n\
61 or: %s -t [SIGNAL]...\n\
63 program_name
, program_name
, program_name
);
65 Send signals to processes, or list signals.\n\
68 emit_mandatory_arg_note ();
71 -s, --signal=SIGNAL, -SIGNAL\n\
72 specify the name or number of the signal to be sent\n\
73 -l, --list list signal names, or convert signal names to/from numbers\n\
74 -t, --table print a table of signal information\n\
76 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
77 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
79 SIGNAL may be a signal name like 'HUP', or a signal number like '1',\n\
80 or the exit status of a process terminated by a signal.\n\
81 PID is an integer; if negative it identifies a process group.\n\
83 printf (USAGE_BUILTIN_WARNING
, PROGRAM_NAME
);
84 emit_ancillary_info (PROGRAM_NAME
);
89 /* Print a row of 'kill -t' output. NUM_WIDTH is the maximum signal
90 number width, and SIGNUM is the signal number to print. The
91 maximum name width is NAME_WIDTH, and SIGNAME is the name to print. */
94 print_table_row (int num_width
, int signum
,
95 int name_width
, char const *signame
)
97 char const *description
= strsignal (signum
);
98 printf ("%*d %-*s %s\n", num_width
, signum
, name_width
, signame
,
99 description
? description
: "?");
102 /* Print a list of signal names. If TABLE, print a table.
103 Print the names specified by ARGV if nonzero; otherwise,
104 print all known names. Return a suitable exit status. */
107 list_signals (bool table
, char *const *argv
)
110 int status
= EXIT_SUCCESS
;
111 char signame
[SIG2STR_MAX
];
117 /* Compute the maximum width of a signal number. */
119 for (signum
= 1; signum
<= SIGNUM_BOUND
/ 10; signum
*= 10)
122 /* Compute the maximum width of a signal name. */
123 for (signum
= 1; signum
<= SIGNUM_BOUND
; signum
++)
124 if (sig2str (signum
, signame
) == 0)
126 idx_t len
= strlen (signame
);
127 if (name_width
< len
)
132 for (; *argv
; argv
++)
134 signum
= operand2sig (*argv
);
136 status
= EXIT_FAILURE
;
139 if (sig2str (signum
, signame
) != 0)
140 snprintf (signame
, sizeof signame
, "SIG%d", signum
);
141 print_table_row (num_width
, signum
, name_width
, signame
);
145 for (signum
= 1; signum
<= SIGNUM_BOUND
; signum
++)
146 if (sig2str (signum
, signame
) == 0)
147 print_table_row (num_width
, signum
, name_width
, signame
);
152 for (; *argv
; argv
++)
154 signum
= operand2sig (*argv
);
156 status
= EXIT_FAILURE
;
157 else if (ISDIGIT (**argv
))
159 if (sig2str (signum
, signame
) == 0)
162 printf ("%d\n", signum
);
165 printf ("%d\n", signum
);
168 for (signum
= 1; signum
<= SIGNUM_BOUND
; signum
++)
169 if (sig2str (signum
, signame
) == 0)
176 /* Send signal SIGNUM to all the processes or process groups specified
177 by ARGV. Return a suitable exit status. */
180 send_signals (int signum
, char *const *argv
)
182 int status
= EXIT_SUCCESS
;
183 char const *arg
= *argv
;
188 intmax_t n
= (errno
= 0, strtoimax (arg
, &endp
, 10));
191 if (errno
== ERANGE
|| ckd_add (&pid
, n
, 0)
192 || arg
== endp
|| *endp
)
194 error (0, 0, _("%s: invalid process id"), quote (arg
));
195 status
= EXIT_FAILURE
;
197 else if (kill (pid
, signum
) != 0)
200 error (0, errno
, "%d", signum
);
202 error (0, errno
, "%s", quote (arg
));
203 status
= EXIT_FAILURE
;
206 while ((arg
= *++argv
));
212 main (int argc
, char **argv
)
219 initialize_main (&argc
, &argv
);
220 set_program_name (argv
[0]);
221 setlocale (LC_ALL
, "");
222 bindtextdomain (PACKAGE
, LOCALEDIR
);
223 textdomain (PACKAGE
);
225 atexit (close_stdout
);
227 while ((optc
= getopt_long (argc
, argv
, short_options
, long_options
, nullptr))
231 case '0': case '1': case '2': case '3': case '4':
232 case '5': case '6': case '7': case '8': case '9':
235 /* This option is actually a process-id. */
237 goto no_more_options
;
240 case 'A': case 'B': case 'C': case 'D': case 'E':
241 case 'F': case 'G': case 'H': case 'I': case 'J':
242 case 'K': /*case 'L':*/ case 'M': case 'N': case 'O':
243 case 'P': case 'Q': case 'R': case 'S': case 'T':
244 case 'U': case 'V': case 'W': case 'X': case 'Y':
247 optarg
= argv
[optind
- 1] + strlen (argv
[optind
- 1]);
248 if (optarg
!= argv
[optind
- 1] + 2)
250 error (0, 0, _("invalid option -- %c"), optc
);
251 usage (EXIT_FAILURE
);
255 case 'n': /* -n is not documented, but is for Bash compatibility. */
259 error (0, 0, _("%s: multiple signals specified"), quote (optarg
));
260 usage (EXIT_FAILURE
);
262 signum
= operand2sig (optarg
);
264 usage (EXIT_FAILURE
);
267 case 'L': /* -L is not documented, but is for procps compatibility. */
274 error (0, 0, _("multiple -l or -t options specified"));
275 usage (EXIT_FAILURE
);
280 case_GETOPT_HELP_CHAR
;
281 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
283 usage (EXIT_FAILURE
);
291 error (0, 0, _("cannot combine signal with -l or -t"));
292 usage (EXIT_FAILURE
);
295 if ( ! list
&& argc
<= optind
)
297 error (0, 0, _("no process ID specified"));
298 usage (EXIT_FAILURE
);
302 ? list_signals (table
, optind
< argc
? argv
+ optind
: nullptr)
303 : send_signals (signum
, argv
+ optind
));