]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/kill.c
hwclock: free temporary variable before return
[thirdparty/util-linux.git] / misc-utils / kill.c
index 824735b4aac2f1ed3b3591b889b50abfa954b8ca..2a42267aacad9efcc858228222fb5810cb447cd0 100644 (file)
  *  modifications (c) salvatore valente <svalente@mit.edu>
  *  may be used / modified / distributed under the same terms as the original.
  *
- *  1999-02-22 Arkadiusz Mikiewicz <misiek@pld.ORG.PL>
+ *  1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL>
  *  - added Native Language Support
  *
- *  1999-11-13 aeb Accept signal numers 128+s.
+ *  1999-11-13 aeb Accept signal numbers 128+s.
  *
+ * Copyright (C) 2014 Sami Kerola <kerolasa@iki.fi>
+ * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
  */
 
+#include <ctype.h>             /* for isdigit() */
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <strings.h>
-#include <ctype.h>             /* for isdigit() */
 #include <unistd.h>
-#include <signal.h>
-#include "kill.h"
+
+#include "c.h"
+#include "closestream.h"
 #include "nls.h"
+#include "pidfd-utils.h"
+#include "procfs.h"
+#include "pathnames.h"
+#include "signames.h"
+#include "strutils.h"
+#include "ttyutils.h"
+#include "xalloc.h"
+#include "fileutils.h"
 
-#define SIZE(a)        (sizeof(a)/sizeof(a[0]))
-
-struct signv {
-       char *name;
-       int val;
-} sys_signame[] = {
-       /* POSIX signals */
-       { "HUP",        SIGHUP },       /* 1 */
-       { "INT",        SIGINT },       /* 2 */
-       { "QUIT",       SIGQUIT },      /* 3 */
-       { "ILL",        SIGILL },       /* 4 */
-       { "ABRT",       SIGABRT },      /* 6 */
-       { "FPE",        SIGFPE },       /* 8 */
-       { "KILL",       SIGKILL },      /* 9 */
-       { "SEGV",       SIGSEGV },      /* 11 */
-       { "PIPE",       SIGPIPE },      /* 13 */
-       { "ALRM",       SIGALRM },      /* 14 */
-       { "TERM",       SIGTERM },      /* 15 */
-       { "USR1",       SIGUSR1 },      /* 10 (arm,i386,m68k,ppc), 30 (alpha,sparc*), 16 (mips) */
-       { "USR2",       SIGUSR2 },      /* 12 (arm,i386,m68k,ppc), 31 (alpha,sparc*), 17 (mips) */
-       { "CHLD",       SIGCHLD },      /* 17 (arm,i386,m68k,ppc), 20 (alpha,sparc*), 18 (mips) */
-       { "CONT",       SIGCONT },      /* 18 (arm,i386,m68k,ppc), 19 (alpha,sparc*), 25 (mips) */
-       { "STOP",       SIGSTOP },      /* 19 (arm,i386,m68k,ppc), 17 (alpha,sparc*), 23 (mips) */
-       { "TSTP",       SIGTSTP },      /* 20 (arm,i386,m68k,ppc), 18 (alpha,sparc*), 24 (mips) */
-       { "TTIN",       SIGTTIN },      /* 21 (arm,i386,m68k,ppc,alpha,sparc*), 26 (mips) */
-       { "TTOU",       SIGTTOU },      /* 22 (arm,i386,m68k,ppc,alpha,sparc*), 27 (mips) */
-       /* Miscellaneous other signals */
-#ifdef SIGTRAP
-       { "TRAP",       SIGTRAP },      /* 5 */
-#endif
-#ifdef SIGIOT
-       { "IOT",        SIGIOT },       /* 6, same as SIGABRT */
-#endif
-#ifdef SIGEMT
-       { "EMT",        SIGEMT },       /* 7 (mips,alpha,sparc*) */
-#endif
-#ifdef SIGBUS
-       { "BUS",        SIGBUS },       /* 7 (arm,i386,m68k,ppc), 10 (mips,alpha,sparc*) */
-#endif
-#ifdef SIGSYS
-       { "SYS",        SIGSYS },       /* 12 (mips,alpha,sparc*) */
-#endif
-#ifdef SIGSTKFLT
-       { "STKFLT",     SIGSTKFLT },    /* 16 (arm,i386,m68k,ppc) */
-#endif
-#ifdef SIGURG
-       { "URG",        SIGURG },       /* 23 (arm,i386,m68k,ppc), 16 (alpha,sparc*), 21 (mips) */
-#endif
-#ifdef SIGIO
-       { "IO",         SIGIO },        /* 29 (arm,i386,m68k,ppc), 23 (alpha,sparc*), 22 (mips) */
-#endif
-#ifdef SIGPOLL
-       { "POLL",       SIGPOLL },      /* same as SIGIO */
-#endif
-#ifdef SIGCLD
-       { "CLD",        SIGCLD },       /* same as SIGCHLD (mips) */
-#endif
-#ifdef SIGXCPU
-       { "XCPU",       SIGXCPU },      /* 24 (arm,i386,m68k,ppc,alpha,sparc*), 30 (mips) */
-#endif
-#ifdef SIGXFSZ
-       { "XFSZ",       SIGXFSZ },      /* 25 (arm,i386,m68k,ppc,alpha,sparc*), 31 (mips) */
-#endif
-#ifdef SIGVTALRM
-       { "VTALRM",     SIGVTALRM },    /* 26 (arm,i386,m68k,ppc,alpha,sparc*), 28 (mips) */
-#endif
-#ifdef SIGPROF
-       { "PROF",       SIGPROF },      /* 27 (arm,i386,m68k,ppc,alpha,sparc*), 29 (mips) */
-#endif
-#ifdef SIGPWR
-       { "PWR",        SIGPWR },       /* 30 (arm,i386,m68k,ppc), 29 (alpha,sparc*), 19 (mips) */
-#endif
-#ifdef SIGINFO
-       { "INFO",       SIGINFO },      /* 29 (alpha) */
+/* partial success, otherwise we return regular EXIT_{SUCCESS,FAILURE} */
+#define KILL_EXIT_SOMEOK       64
+
+enum {
+       KILL_FIELD_WIDTH = 11,
+       KILL_OUTPUT_WIDTH = 72
+};
+
+#ifdef UL_HAVE_PIDFD
+# include <poll.h>
+# include "list.h"
+struct timeouts {
+       int period;
+       int sig;
+       struct list_head follow_ups;
+};
 #endif
-#ifdef SIGLOST
-       { "LOST",       SIGLOST },      /* 29 (arm,i386,m68k,ppc,sparc*) */
+
+struct kill_control {
+       char *arg;
+       pid_t pid;
+       int numsig;
+#ifdef HAVE_SIGQUEUE
+       union sigval sigdata;
 #endif
-#ifdef SIGWINCH
-       { "WINCH",      SIGWINCH },     /* 28 (arm,i386,m68k,ppc,alpha,sparc*), 20 (mips) */
+#ifdef UL_HAVE_PIDFD
+       struct list_head follow_ups;
 #endif
-#ifdef SIGUNUSED
-       { "UNUSED",     SIGUNUSED },    /* 31 (arm,i386,m68k,ppc) */
+       unsigned int
+               check_all:1,
+               do_kill:1,
+               do_pid:1,
+               require_handler:1,
+               use_sigval:1,
+#ifdef UL_HAVE_PIDFD
+               timeout:1,
 #endif
+               verbose:1;
 };
 
-int main (int argc, char *argv[]);
-extern char *mybasename(char *);
-int signame_to_signum (char *sig);
-int arg_to_signum (char *arg, int mask);
-void nosig (char *name);
-void printsig (int sig);
-void printsignals (FILE *fp);
-int usage (int status);
-int kill_verbose (char *procname, int pid, int sig);
-
-extern int *get_pids (char *, int);
-
-static char *progname;
-
-int main (int argc, char *argv[])
+static void print_signal_name(int signum)
 {
-    int errors, numsig, pid;
-    char *ep, *arg, *p;
-    int do_pid, do_kill, check_all;
-    int *pids, *ip;
-
-    progname = argv[0];
-    if ((p = strrchr(progname, '/')) != NULL)
-           progname = p+1;
-
-    setlocale(LC_ALL, "");
-    bindtextdomain(PACKAGE, LOCALEDIR);
-    textdomain(PACKAGE);
-
-    numsig = SIGTERM;
-    do_pid = (! strcmp (progname, "pid"));     /* Yecch */
-    do_kill = 0;
-    check_all = 0;
-
-    /*  loop through the arguments.
-       actually, -a is the only option can be used with other options.
-       `kill' is basically a one-option-at-most program.  */
-    for (argc--, argv++; argc > 0; argc--, argv++) {
-       arg = *argv;
-       if (*arg != '-') {
-           break;
-       }
-       if (! strcmp (arg, "--")) {
-           argc--, argv++;
-           break;
-       }
-       if (! strcmp (arg, "-v") || ! strcmp (arg, "-V") ||
-           ! strcmp (arg, "--version")) {
-           printf(_("%s from %s%s\n"), progname, "util-linux-", VERSION);
-           return 0;
-       }
-       if (! strcmp (arg, "-a")) {
-           check_all++;
-           continue;
-       }
-       if (! strcmp (arg, "-l")) {
-           if (argc < 2) {
-               printsignals (stdout);
-               return 0;
-           }
-           if (argc > 2) {
-               return usage (1);
-           }
-           /* argc == 2, accept "kill -l $?" */
-           arg = argv[1];
-           if ((numsig = arg_to_signum (arg, 1)) < 0) {
-               fprintf (stderr, _("%s: unknown signal %s\n"), progname, arg);
-               return 1;
-           }
-           printsig (numsig);
-           return 0;
-       }
-       if (! strcmp (arg, "-p")) {
-           do_pid++;
-           if (do_kill)
-               return usage (1);
-           continue;
+       const char *name = signum_to_signame(signum);
+
+       if (name) {
+               printf("%s\n", name);
+               return;
        }
-       if (! strcmp (arg, "-s")) {
-           if (argc < 2) {
-               return usage (1);
-           }
-           do_kill++;
-           if (do_pid)
-               return usage (1);
-           argc--, argv++;
-           arg = *argv;
-           if ((numsig = arg_to_signum (arg, 0)) < 0) {
-               nosig (arg);
-               return 1;
-           }
-           continue;
+#ifdef SIGRTMIN
+       if (SIGRTMIN <= signum && signum <= SIGRTMAX) {
+               printf("RT%d\n", signum - SIGRTMIN);
+               return;
        }
-       /*  `arg' begins with a dash but is not a known option.
-           so it's probably something like -HUP, or -1/-n
-           try to deal with it.
-           -n could be signal n, or pid -n (i.e. process group n).
-           In case of doubt POSIX tells us to assume a signal.
-           If a signal has been parsed, assume it's a pid, break */
-       if (do_kill)
-         break;
-       arg++;
-       if ((numsig = arg_to_signum (arg, 0)) < 0) {
-           return usage (1);
+#endif
+       printf("%d\n", signum);
+}
+
+static void pretty_print_signal(FILE *fp, size_t term_width, size_t *lpos,
+                               int signum, const char *name)
+{
+       if (term_width < (*lpos + KILL_FIELD_WIDTH)) {
+               fputc('\n', fp);
+               *lpos = 0;
        }
-       do_kill++;
-       if (do_pid)
-           return usage (1);
-       continue;
-    }
-
-    if (! *argv) {
-       return usage (1);
-    }
-    if (do_pid) {
-       numsig = -1;
-    }
-
-    /*  we're done with the options.
-       the rest of the arguments should be process ids and names.
-       kill them.  */
-    for (errors = 0; (arg = *argv) != NULL; argv++) {
-       pid = strtol (arg, &ep, 10);
-       if (! *ep)
-           errors += kill_verbose (arg, pid, numsig);
-       else {
-           pids = get_pids (arg, check_all);
-           if (! pids) {
-               errors++;
-               fprintf (stderr, _("%s: can't find process \"%s\"\n"),
-                        progname, arg);
-               continue;
-           }
-           for (ip = pids; *ip >= 0; ip++)
-               errors += kill_verbose (arg, *ip, numsig);
-           free (pids);
+       *lpos += KILL_FIELD_WIDTH;
+       fprintf(fp, "%2d %-8s", signum, name);
+}
+
+static void print_all_signals(FILE *fp, int pretty)
+{
+       size_t n, lth, lpos = 0, width;
+       const char *signame = NULL;
+       int signum = 0;
+
+       if (!pretty) {
+               for (n = 0; get_signame_by_idx(n, &signame, NULL) == 0; n++) {
+                       lth = 1 + strlen(signame);
+                       if (KILL_OUTPUT_WIDTH < lpos + lth) {
+                               fputc('\n', fp);
+                               lpos = 0;
+                       } else if (lpos)
+                               fputc(' ', fp);
+                       lpos += lth;
+                       fputs(signame, fp);
+               }
+#ifdef SIGRTMIN
+               fputs(" RT<N> RTMIN+<N> RTMAX-<N>", fp);
+#endif
+               fputc('\n', fp);
+               return;
        }
-    }
-    return (errors);
+
+       /* pretty print */
+       width = get_terminal_width(KILL_OUTPUT_WIDTH + 1) - 1;
+       for (n = 0; get_signame_by_idx(n, &signame, &signum) == 0; n++)
+               pretty_print_signal(fp, width, &lpos, signum, signame);
+#ifdef SIGRTMIN
+       pretty_print_signal(fp, width, &lpos, SIGRTMIN, "RTMIN");
+       pretty_print_signal(fp, width, &lpos, SIGRTMAX, "RTMAX");
+#endif
+       fputc('\n', fp);
+}
+
+static void err_nosig(char *name)
+{
+       warnx(_("unknown signal %s; valid signals:"), name);
+       print_all_signals(stderr, 1);
+       exit(EXIT_FAILURE);
 }
 
+static int arg_to_signum(char *arg, int maskbit)
+{
+       int numsig;
+       char *ep;
+
+       if (isdigit(*arg)) {
+               errno = 0;
+               numsig = strtol(arg, &ep, 10);
+               if (NSIG <= numsig && maskbit && (numsig & 128) != 0)
+                       numsig -= 128;
+               if (errno || *ep != 0 || numsig < 0 || NSIG <= numsig)
+                       return -1;
+               return numsig;
+       }
+       return signame_to_signum(arg);
+}
 
-int signame_to_signum (char *sig)
+static void __attribute__((__noreturn__)) usage(void)
 {
-    int n;
-
-    if (! strncasecmp (sig, "sig", 3))
-       sig += 3;
-    for (n = 0; n < SIZE(sys_signame); n++) {
-       if (! strcasecmp (sys_signame[n].name, sig))
-           return sys_signame[n].val;
-    }
-    return (-1);
+       FILE *out = stdout;
+       fputs(USAGE_HEADER, out);
+       fprintf(out, _(" %s [options] <pid>|<name>...\n"), program_invocation_short_name);
+
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Forcibly terminate a process.\n"), out);
+
+       fputs(USAGE_OPTIONS, out);
+       fputs(_(" -a, --all              do not restrict the name-to-pid conversion to processes\n"
+               "                          with the same uid as the present process\n"), out);
+       fputs(_(" -s, --signal <signal>  send this <signal> instead of SIGTERM\n"), out);
+#ifdef HAVE_SIGQUEUE
+       fputs(_(" -q, --queue <value>    use sigqueue(2), not kill(2), and pass <value> as data\n"), out);
+#endif
+#ifdef UL_HAVE_PIDFD
+       fputs(_("     --timeout <milliseconds> <follow-up signal>\n"
+               "                        wait up to timeout and send follow-up signal\n"), out);
+#endif
+       fputs(_(" -p, --pid              print pids without signaling them\n"), out);
+       fputs(_(" -l, --list[=<signal>]  list signal names, or convert a signal number to a name\n"), out);
+       fputs(_(" -L, --table            list signal names and numbers\n"), out);
+       fputs(_(" -r, --require-handler  do not send signal if signal handler is not present\n"), out);
+       fputs(_("     --verbose          print pids that will be signaled\n"), out);
+
+       fputs(USAGE_SEPARATOR, out);
+       fprintf(out, USAGE_HELP_OPTIONS(24));
+       fprintf(out, USAGE_MAN_TAIL("kill(1)"));
+
+       exit(EXIT_SUCCESS);
 }
 
-int arg_to_signum (char *arg, int maskbit)
+static void __attribute__((__noreturn__)) print_kill_version(void)
 {
-    int numsig;
-    char *ep;
-
-    if (isdigit (*arg)) {
-       numsig = strtol (arg, &ep, 10);
-       if (numsig >= NSIG && maskbit && (numsig & 128) != 0)
-           numsig -= 128;
-       if (*ep != 0 || numsig < 0 || numsig >= NSIG)
-           return (-1);
-       return (numsig);
-    }
-    return (signame_to_signum (arg));
+       static const char *features[] = {
+#ifdef HAVE_SIGQUEUE
+               "sigqueue",
+#endif
+#ifdef UL_HAVE_PIDFD
+               "pidfd",
+#endif
+       };
+
+       printf(_("%s from %s"), program_invocation_short_name, PACKAGE_STRING);
+
+       if (ARRAY_SIZE(features)) {
+               size_t i;
+               fputs(_(" (with: "), stdout);
+               for (i = 0; i < ARRAY_SIZE(features); i++) {
+                       fputs(features[i], stdout);
+                       if (i + 1 < ARRAY_SIZE(features))
+                               fputs(", ", stdout);
+               }
+               fputs(")\n", stdout);
+       }
+       exit(EXIT_SUCCESS);
 }
 
-void nosig (char *name)
+static char **parse_arguments(int argc, char **argv, struct kill_control *ctl)
 {
-    fprintf (stderr, _("%s: unknown signal %s; valid signals:\n"), progname, name);
-    printsignals (stderr);
+       char *arg;
+
+       /* Loop through the arguments.  Actually, -a is the only option
+        * can be used with other options.  The 'kill' is basically a
+        * one-option-at-most program. */
+       for (argc--, argv++; 0 < argc; argc--, argv++) {
+               arg = *argv;
+               if (*arg != '-')
+                       break;
+               if (!strcmp(arg, "--")) {
+                       argc--, argv++;
+                       break;
+               }
+               if (!strcmp(arg, "-v") || !strcmp(arg, "-V") ||
+                   !strcmp(arg, "--version"))
+                       print_kill_version();
+               if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
+                       usage();
+               if (!strcmp(arg, "--verbose")) {
+                       ctl->verbose = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
+                       ctl->check_all = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-l") || !strcmp(arg, "--list")) {
+                       if (argc < 2) {
+                               print_all_signals(stdout, 0);
+                               exit(EXIT_SUCCESS);
+                       }
+                       if (2 < argc)
+                               errx(EXIT_FAILURE, _("too many arguments"));
+                       /* argc == 2, accept "kill -l $?" */
+                       arg = argv[1];
+                       if ((ctl->numsig = arg_to_signum(arg, 1)) < 0)
+                               errx(EXIT_FAILURE, _("unknown signal: %s"),
+                                    arg);
+                       print_signal_name(ctl->numsig);
+                       exit(EXIT_SUCCESS);
+               }
+               /* for compatibility with procps kill(1) */
+               if (!strncmp(arg, "--list=", 7) || !strncmp(arg, "-l=", 3)) {
+                       char *p = strchr(arg, '=') + 1;
+                       if ((ctl->numsig = arg_to_signum(p, 1)) < 0)
+                               errx(EXIT_FAILURE, _("unknown signal: %s"), p);
+                       print_signal_name(ctl->numsig);
+                       exit(EXIT_SUCCESS);
+               }
+               if (!strcmp(arg, "-L") || !strcmp(arg, "--table")) {
+                       print_all_signals(stdout, 1);
+                       exit(EXIT_SUCCESS);
+               }
+               if (!strcmp(arg, "-r") || !strcmp(arg, "--require-handler")) {
+                       ctl->require_handler = 1;
+                       continue;
+               }
+               if (!strcmp(arg, "-p") || !strcmp(arg, "--pid")) {
+                       ctl->do_pid = 1;
+                       if (ctl->do_kill)
+                               errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal");
+#ifdef HAVE_SIGQUEUE
+                       if (ctl->use_sigval)
+                               errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--queue");
+#endif
+                       continue;
+               }
+               if (!strcmp(arg, "-s") || !strcmp(arg, "--signal")) {
+                       if (argc < 2)
+                               errx(EXIT_FAILURE, _("not enough arguments"));
+                       ctl->do_kill = 1;
+                       if (ctl->do_pid)
+                               errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal");
+                       argc--, argv++;
+                       arg = *argv;
+                       if ((ctl->numsig = arg_to_signum(arg, 0)) < 0)
+                               err_nosig(arg);
+                       continue;
+               }
+#ifdef HAVE_SIGQUEUE
+               if (!strcmp(arg, "-q") || !strcmp(arg, "--queue")) {
+                       if (argc < 2)
+                               errx(EXIT_FAILURE, _("option '%s' requires an argument"), arg);
+                       if (ctl->do_pid)
+                               errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--queue");
+                       argc--, argv++;
+                       arg = *argv;
+                       ctl->sigdata.sival_int = strtos32_or_err(arg, _("argument error"));
+                       ctl->use_sigval = 1;
+                       continue;
+               }
+#endif
+#ifdef UL_HAVE_PIDFD
+               if (!strcmp(arg, "--timeout")) {
+                       struct timeouts *next;
+
+                       ctl->timeout = 1;
+                       if (argc < 2)
+                               errx(EXIT_FAILURE, _("option '%s' requires an argument"), arg);
+                       argc--, argv++;
+                       arg = *argv;
+                       next = xcalloc(1, sizeof(*next));
+                       next->period = strtos32_or_err(arg, _("argument error"));
+                       INIT_LIST_HEAD(&next->follow_ups);
+                       argc--, argv++;
+                       arg = *argv;
+                       if ((next->sig = arg_to_signum(arg, 0)) < 0)
+                               err_nosig(arg);
+                       list_add_tail(&next->follow_ups, &ctl->follow_ups);
+                       continue;
+               }
+#endif
+               /* 'arg' begins with a dash but is not a known option.
+                * So it's probably something like -HUP, or -1/-n try to
+                * deal with it.
+                *
+                * -n could be either signal n or pid -n (a process group
+                * number).  In case of doubt, POSIX tells us to assume a
+                * signal.  But if a signal has already been parsed, then
+                * assume it is a process group, so stop parsing options. */
+               if (ctl->do_kill)
+                       break;
+               arg++;
+               if ((ctl->numsig = arg_to_signum(arg, 0)) < 0)
+                       errx(EXIT_FAILURE, _("invalid signal name or number: %s"), arg);
+               ctl->do_kill = 1;
+               if (ctl->do_pid)
+                       errx(EXIT_FAILURE, _("%s and %s are mutually exclusive"), "--pid", "--signal");
+       }
+       if (!*argv)
+               errx(EXIT_FAILURE, _("not enough arguments"));
+       return argv;
 }
 
-void printsig (int sig)
+#ifdef UL_HAVE_PIDFD
+static int kill_with_timeout(const struct kill_control *ctl)
 {
-    int n;
+       int pfd, n;
+       struct pollfd p = { 0 };
+       siginfo_t info = { 0 };
+       struct list_head *entry;
 
-    for (n = 0; n < SIZE(sys_signame); n++) {
-       if (sys_signame[n].val == sig) {
-           printf ("%s\n", sys_signame[n].name);
-           return;
+       info.si_code = SI_QUEUE;
+       info.si_signo = ctl->numsig;
+       info.si_uid = getuid();
+       info.si_pid = getpid();
+       info.si_value.sival_int =
+           ctl->use_sigval != 0 ? ctl->use_sigval : ctl->numsig;
+
+       if ((pfd = pidfd_open(ctl->pid, 0)) < 0)
+               err(EXIT_FAILURE, _("pidfd_open() failed: %d"), ctl->pid);
+       p.fd = pfd;
+       p.events = POLLIN;
+
+       if (pidfd_send_signal(pfd, ctl->numsig, &info, 0) < 0)
+               err(EXIT_FAILURE, _("pidfd_send_signal() failed"));
+       list_for_each(entry, &ctl->follow_ups) {
+               struct timeouts *timeout;
+
+               timeout = list_entry(entry, struct timeouts, follow_ups);
+               n = poll(&p, 1, timeout->period);
+               if (n < 0)
+                       err(EXIT_FAILURE, _("poll() failed"));
+               if (n == 0) {
+                       info.si_signo = timeout->sig;
+                       if (ctl->verbose)
+                               printf(_("timeout, sending signal %d to pid %d\n"),
+                                        timeout->sig, ctl->pid);
+                       if (pidfd_send_signal(pfd, timeout->sig, &info, 0) < 0)
+                               err(EXIT_FAILURE, _("pidfd_send_signal() failed"));
+               }
        }
-    }
-    printf("%d\n", sig);
+       return 0;
 }
+#endif
 
-void printsignals (FILE *fp)
+static int kill_verbose(const struct kill_control *ctl)
 {
-    int n, lth;
-    int lpos = 0;
-
-    for (n = 0; n < SIZE(sys_signame); n++) {
-       lth = 1+strlen(sys_signame[n].name);
-       if (lpos+lth > 72) {
-           fputc ('\n', fp);
-           lpos = 0;
-       } else if (lpos)
-           fputc (' ', fp);
-       lpos += lth;
-       fputs (sys_signame[n].name, fp);
-    }
-    fputc ('\n', fp);
+       int rc = 0;
+
+       if (ctl->verbose)
+               printf(_("sending signal %d to pid %d\n"), ctl->numsig, ctl->pid);
+       if (ctl->do_pid) {
+               printf("%ld\n", (long) ctl->pid);
+               return 0;
+       }
+#ifdef UL_HAVE_PIDFD
+       if (ctl->timeout) {
+               rc = kill_with_timeout(ctl);
+       } else
+#endif
+#ifdef HAVE_SIGQUEUE
+       if (ctl->use_sigval)
+               rc = sigqueue(ctl->pid, ctl->numsig, ctl->sigdata);
+       else
+#endif
+               rc = kill(ctl->pid, ctl->numsig);
+
+       if (rc < 0)
+               warn(_("sending signal to %s failed"), ctl->arg);
+       return rc;
 }
 
-int usage (int status)
+static int check_signal_handler(const struct kill_control *ctl)
 {
-    FILE *fp;
+       uintmax_t sigcgt = 0;
+       int rc = 0, has_hnd = 0;
+       struct path_cxt *pc;
+
+       if (!ctl->require_handler)
+               return 1;
 
-    fp = (status == 0 ? stdout : stderr);
-    fprintf (fp, _("usage: %s [ -s signal | -p ] [ -a ] pid ...\n"), progname);
-    fprintf (fp, _("       %s -l [ signal ]\n"), progname);
-    return status;
+       pc = ul_new_procfs_path(ctl->pid, NULL);
+       if (!pc)
+               return -ENOMEM;
+
+       rc = procfs_process_get_stat_nth(pc, 34, &sigcgt);
+       if (rc)
+               return -EINVAL;
+
+       ul_unref_path(pc);
+
+       has_hnd = ((1UL << (ctl->numsig - 1)) & sigcgt) != 0;
+       if (ctl->verbose && !has_hnd)
+               printf(_("not signalling pid %d, it has no userspace handler for signal %d\n"), ctl->pid, ctl->numsig);
+
+       return has_hnd;
 }
 
-int kill_verbose (char *procname, int pid, int sig)
+int main(int argc, char **argv)
 {
-    if (sig < 0) {
-       printf ("%d\n", pid);
-       return 0;
-    }
-    if (kill (pid, sig) < 0) {
-       fprintf (stderr, "%s ", progname);
-       perror (procname);
-       return 1;
-    }
-    return 0;
+       struct kill_control ctl = { .numsig = SIGTERM };
+       int nerrs = 0, ct = 0;
+
+       setlocale(LC_ALL, "");
+       bindtextdomain(PACKAGE, LOCALEDIR);
+       textdomain(PACKAGE);
+       close_stdout_atexit();
+
+#ifdef UL_HAVE_PIDFD
+       INIT_LIST_HEAD(&ctl.follow_ups);
+#endif
+       argv = parse_arguments(argc, argv, &ctl);
+
+       /* The rest of the arguments should be process ids and names. */
+       for ( ; (ctl.arg = *argv) != NULL; argv++) {
+               char *ep = NULL;
+
+               errno = 0;
+               ctl.pid = strtol(ctl.arg, &ep, 10);
+               if (errno == 0 && ep && *ep == '\0' && ctl.arg < ep) {
+                       if (check_signal_handler(&ctl) <= 0)
+                               continue;
+                       if (kill_verbose(&ctl) != 0)
+                               nerrs++;
+                       ct++;
+               } else {
+                       int found = 0;
+                       struct dirent *d;
+                       DIR *dir = opendir(_PATH_PROC);
+                       uid_t uid = !ctl.check_all ? getuid() : 0;
+
+                       if (!dir)
+                               continue;
+
+                       while ((d = xreaddir(dir))) {
+                               if (!ctl.check_all &&
+                                   !procfs_dirent_match_uid(dir, d, uid))
+                                       continue;
+                               if (ctl.arg &&
+                                   !procfs_dirent_match_name(dir, d, ctl.arg))
+                                       continue;
+                               if (procfs_dirent_get_pid(d, &ctl.pid) != 0)
+                                       continue;
+                               if (check_signal_handler(&ctl) <= 0)
+                                       continue;
+
+                               if (kill_verbose(&ctl) != 0)
+                                       nerrs++;
+                               ct++;
+                               found = 1;
+                       }
+
+                       closedir(dir);
+                       if (!found) {
+                               nerrs++, ct++;
+                               warnx(_("cannot find process \"%s\""), ctl.arg);
+                       }
+               }
+       }
+
+#ifdef UL_HAVE_PIDFD
+       while (!list_empty(&ctl.follow_ups)) {
+               struct timeouts *x = list_entry(ctl.follow_ups.next,
+                                                 struct timeouts, follow_ups);
+               list_del(&x->follow_ups);
+               free(x);
+       }
+#endif
+       if (ct && nerrs == 0)
+               return EXIT_SUCCESS;    /* full success */
+       if (ct == nerrs)
+               return EXIT_FAILURE;    /* all failed */
+
+       return KILL_EXIT_SOMEOK;        /* partial success */
 }