mirror of
https://git.savannah.gnu.org/git/coreutils.git
synced 2026-11-17 18:02:09 +03:00
* src/chroot.c (parse_additional_groups): Declare variable in the 'for' clause. * src/comm.c (compare_files): Likewise. * src/cp.c (re_protect): Likewise. * src/csplit.c (save_buffer, find_line, write_to_file) (split_file, main): Likewise. * src/dd.c (apply_translations, translate_buffer) (copy_with_block, main): Likewise. * src/df.c (selected_fstype, excluded_fstype, filter_mount_list) (last_device_for_mount, get_device, get_point, get_all_entries) (main): Likewise. * src/fmt.c (copy_rest, get_prefix, fmt_paragraph, put_paragraph) (put_word): Likewise. * src/fold.c (main): Likewise. * src/head.c (elide_tail_bytes_pipe, main): Likewise. * src/install.c (main): Likewise. * src/join.c (prfields, join, main): Likewise. * src/kill.c (list_signals): Likewise. * src/ls.c (main, decode_switches, parse_ls_color, patterns_match): Likewise. * src/operand2sig.c (operand2sig): Likewise. * src/pathchk.c (no_leading_hyphen, validate_file_name): Likewise. * src/pr.c (char_to_clump): Likewise. * src/printenv.c (main): Likewise. * src/ptx.c (initialize_regex, digest_break_file) (find_occurs_in_text, print_field): Likewise. * src/remove.c (mark_ancestor_dirs): Likewise. * src/seq.c (print_numbers): Likewise. * src/shred.c (do_wipefd, main): Likewise. * src/sort.c (cleanup, inittables, key_warnings, mergefps) (check_ordering_compatibility, main): Likewise. * src/split.c (closeout): Likewise. * src/stat.c (find_bind_mount, print_it, format_to_mask): Likewise. * src/stdbuf.c (set_program_path): Likewise. * src/stty.c (apply_settings, display_changed, display_all) (recover_mode, sane_mode): Likewise. * src/system.h (stzncpy): Likewise. * src/tail.c (pipe_lines): Likewise. * src/tee.c (tee_files): Likewise. * src/tr.c (look_up_char_class, get_spec_stats): Likewise. * src/users.c (list_entries_users): Likewise.
305 lines
8.4 KiB
C
305 lines
8.4 KiB
C
/* kill -- send a signal to a process
|
|
Copyright (C) 2002-2025 Free Software Foundation, Inc.
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
|
|
|
/* Written by Paul Eggert. */
|
|
|
|
#include <config.h>
|
|
#include <stdio.h>
|
|
#include <getopt.h>
|
|
#include <sys/types.h>
|
|
#include <signal.h>
|
|
|
|
#include "system.h"
|
|
#include "c-ctype.h"
|
|
#include "sig2str.h"
|
|
#include "operand2sig.h"
|
|
#include "quote.h"
|
|
|
|
/* The official name of this program (e.g., no 'g' prefix). */
|
|
#define PROGRAM_NAME "kill"
|
|
|
|
#define AUTHORS proper_name ("Paul Eggert")
|
|
|
|
static char const short_options[] =
|
|
"0::1::2::3::4::5::6::7::8::9::"
|
|
"A::B::C::D::E::F::G::H::I::J::K::M::"
|
|
"N::O::P::Q::R::S::T::U::V::W::X::Y::Z::"
|
|
"Lln:s:t";
|
|
|
|
static struct option const long_options[] =
|
|
{
|
|
{"list", no_argument, nullptr, 'l'},
|
|
{"signal", required_argument, nullptr, 's'},
|
|
{"table", no_argument, nullptr, 't'},
|
|
{GETOPT_HELP_OPTION_DECL},
|
|
{GETOPT_VERSION_OPTION_DECL},
|
|
{nullptr, 0, nullptr, 0}
|
|
};
|
|
|
|
void
|
|
usage (int status)
|
|
{
|
|
if (status != EXIT_SUCCESS)
|
|
emit_try_help ();
|
|
else
|
|
{
|
|
printf (_("\
|
|
Usage: %s [-s SIGNAL | -SIGNAL] PID...\n\
|
|
or: %s -l [SIGNAL]...\n\
|
|
or: %s -t [SIGNAL]...\n\
|
|
"),
|
|
program_name, program_name, program_name);
|
|
fputs (_("\
|
|
Send signals to processes, or list signals.\n\
|
|
"), stdout);
|
|
|
|
emit_mandatory_arg_note ();
|
|
|
|
fputs (_("\
|
|
-s, --signal=SIGNAL, -SIGNAL\n\
|
|
specify the name or number of the signal to be sent\n\
|
|
-l, --list list signal names, or convert signal names to/from numbers\n\
|
|
-t, --table print a table of signal information\n\
|
|
"), stdout);
|
|
fputs (HELP_OPTION_DESCRIPTION, stdout);
|
|
fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
|
fputs (_("\n\
|
|
SIGNAL may be a signal name like 'HUP', or a signal number like '1',\n\
|
|
or the exit status of a process terminated by a signal.\n\
|
|
PID is an integer; if negative it identifies a process group.\n\
|
|
"), stdout);
|
|
printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
|
|
emit_ancillary_info (PROGRAM_NAME);
|
|
}
|
|
exit (status);
|
|
}
|
|
|
|
/* Print a row of 'kill -t' output. NUM_WIDTH is the maximum signal
|
|
number width, and SIGNUM is the signal number to print. The
|
|
maximum name width is NAME_WIDTH, and SIGNAME is the name to print. */
|
|
|
|
static void
|
|
print_table_row (int num_width, int signum,
|
|
int name_width, char const *signame)
|
|
{
|
|
char const *description = strsignal (signum);
|
|
printf ("%*d %-*s %s\n", num_width, signum, name_width, signame,
|
|
description ? description : "?");
|
|
}
|
|
|
|
/* Print a list of signal names. If TABLE, print a table.
|
|
Print the names specified by ARGV if nonzero; otherwise,
|
|
print all known names. Return a suitable exit status. */
|
|
|
|
static int
|
|
list_signals (bool table, char *const *argv)
|
|
{
|
|
int status = EXIT_SUCCESS;
|
|
char signame[SIG2STR_MAX];
|
|
|
|
if (table)
|
|
{
|
|
int name_width = 0;
|
|
|
|
/* Compute the maximum width of a signal number. */
|
|
int num_width = 1;
|
|
for (int signum = 1; signum <= SIGNUM_BOUND / 10; signum *= 10)
|
|
num_width++;
|
|
|
|
/* Compute the maximum width of a signal name. */
|
|
for (int signum = 0; signum <= SIGNUM_BOUND; signum++)
|
|
if (sig2str (signum, signame) == 0)
|
|
{
|
|
idx_t len = strlen (signame);
|
|
if (name_width < len)
|
|
name_width = len;
|
|
}
|
|
|
|
if (argv)
|
|
for (; *argv; argv++)
|
|
{
|
|
int signum = operand2sig (*argv);
|
|
if (signum < 0)
|
|
status = EXIT_FAILURE;
|
|
else
|
|
{
|
|
if (sig2str (signum, signame) != 0)
|
|
snprintf (signame, sizeof signame, "SIG%d", signum);
|
|
print_table_row (num_width, signum, name_width, signame);
|
|
}
|
|
}
|
|
else
|
|
for (int signum = 0; signum <= SIGNUM_BOUND; signum++)
|
|
if (sig2str (signum, signame) == 0)
|
|
print_table_row (num_width, signum, name_width, signame);
|
|
}
|
|
else
|
|
{
|
|
if (argv)
|
|
for (; *argv; argv++)
|
|
{
|
|
int signum = operand2sig (*argv);
|
|
if (signum < 0)
|
|
status = EXIT_FAILURE;
|
|
else if (c_isdigit (**argv))
|
|
{
|
|
if (sig2str (signum, signame) == 0)
|
|
puts (signame);
|
|
else
|
|
printf ("%d\n", signum);
|
|
}
|
|
else
|
|
printf ("%d\n", signum);
|
|
}
|
|
else
|
|
for (int signum = 0; signum <= SIGNUM_BOUND; signum++)
|
|
if (sig2str (signum, signame) == 0)
|
|
puts (signame);
|
|
}
|
|
|
|
return status;
|
|
}
|
|
|
|
/* Send signal SIGNUM to all the processes or process groups specified
|
|
by ARGV. Return a suitable exit status. */
|
|
|
|
static int
|
|
send_signals (int signum, char *const *argv)
|
|
{
|
|
int status = EXIT_SUCCESS;
|
|
char const *arg = *argv;
|
|
|
|
do
|
|
{
|
|
char *endp;
|
|
intmax_t n = (errno = 0, strtoimax (arg, &endp, 10));
|
|
pid_t pid;
|
|
|
|
if (errno == ERANGE || ckd_add (&pid, n, 0)
|
|
|| arg == endp || *endp)
|
|
{
|
|
error (0, 0, _("%s: invalid process id"), quote (arg));
|
|
status = EXIT_FAILURE;
|
|
}
|
|
else if (kill (pid, signum) != 0)
|
|
{
|
|
if (errno == EINVAL)
|
|
error (0, errno, "%d", signum);
|
|
else
|
|
error (0, errno, "%s", quote (arg));
|
|
status = EXIT_FAILURE;
|
|
}
|
|
}
|
|
while ((arg = *++argv));
|
|
|
|
return status;
|
|
}
|
|
|
|
int
|
|
main (int argc, char **argv)
|
|
{
|
|
int optc;
|
|
bool list = false;
|
|
bool table = false;
|
|
int signum = -1;
|
|
|
|
initialize_main (&argc, &argv);
|
|
set_program_name (argv[0]);
|
|
setlocale (LC_ALL, "");
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
textdomain (PACKAGE);
|
|
|
|
atexit (close_stdout);
|
|
|
|
while ((optc = getopt_long (argc, argv, short_options, long_options, nullptr))
|
|
!= -1)
|
|
switch (optc)
|
|
{
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
if (optind != 2)
|
|
{
|
|
/* This option is actually a process-id. */
|
|
optind--;
|
|
goto no_more_options;
|
|
}
|
|
FALLTHROUGH;
|
|
case 'A': case 'B': case 'C': case 'D': case 'E':
|
|
case 'F': case 'G': case 'H': case 'I': case 'J':
|
|
case 'K': /*case 'L':*/ case 'M': case 'N': case 'O':
|
|
case 'P': case 'Q': case 'R': case 'S': case 'T':
|
|
case 'U': case 'V': case 'W': case 'X': case 'Y':
|
|
case 'Z':
|
|
if (! optarg)
|
|
optarg = argv[optind - 1] + strlen (argv[optind - 1]);
|
|
if (optarg != argv[optind - 1] + 2)
|
|
{
|
|
error (0, 0, _("invalid option -- %c"), optc);
|
|
usage (EXIT_FAILURE);
|
|
}
|
|
optarg--;
|
|
FALLTHROUGH;
|
|
case 'n': /* -n is not documented, but is for Bash compatibility. */
|
|
case 's':
|
|
if (0 <= signum)
|
|
{
|
|
error (0, 0, _("%s: multiple signals specified"), quote (optarg));
|
|
usage (EXIT_FAILURE);
|
|
}
|
|
signum = operand2sig (optarg);
|
|
if (signum < 0)
|
|
usage (EXIT_FAILURE);
|
|
break;
|
|
|
|
case 'L': /* -L is not documented, but is for procps compatibility. */
|
|
case 't':
|
|
table = true;
|
|
FALLTHROUGH;
|
|
case 'l':
|
|
if (list)
|
|
{
|
|
error (0, 0, _("multiple -l or -t options specified"));
|
|
usage (EXIT_FAILURE);
|
|
}
|
|
list = true;
|
|
break;
|
|
|
|
case_GETOPT_HELP_CHAR;
|
|
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
|
|
default:
|
|
usage (EXIT_FAILURE);
|
|
}
|
|
no_more_options:
|
|
|
|
if (signum < 0)
|
|
signum = SIGTERM;
|
|
else if (list)
|
|
{
|
|
error (0, 0, _("cannot combine signal with -l or -t"));
|
|
usage (EXIT_FAILURE);
|
|
}
|
|
|
|
if ( ! list && argc <= optind)
|
|
{
|
|
error (0, 0, _("no process ID specified"));
|
|
usage (EXIT_FAILURE);
|
|
}
|
|
|
|
return (list
|
|
? list_signals (table, optind < argc ? argv + optind : nullptr)
|
|
: send_signals (signum, argv + optind));
|
|
}
|