]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
kill: add pretty printed list output (for compatibility with procps)
authorSami Kerola <kerolasa@iki.fi>
Tue, 12 Mar 2013 21:18:13 +0000 (21:18 +0000)
committerKarel Zak <kzak@redhat.com>
Wed, 13 Mar 2013 14:47:12 +0000 (15:47 +0100)
 * add -L, --table

References: http://www.freelists.org/post/procps/kill1-consolidation
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/kill.1
misc-utils/kill.c

index 7daa6acf5a30136c20941586b6e84ecac356d0df..34d3dfa68e4462f1e53acd17e01cae7f7ead456a 100644 (file)
@@ -74,6 +74,10 @@ number.
 Print a list of signal names.  These are found in
 .I /usr/\:include/\:linux/\:signal.h
 .TP
+\fB\-L\fR, \fB\-\-table\fR
+Similar to \-l, but will print signal names and their corresponding
+numbers.
+.TP
 \fB\-a\fR, \fB\-\-all\fR
 Do not restrict the commandname-to-pid conversion to processes with the same
 uid as the present process.
index f6b3f84daae99a98ece97d7f032d12a06cf6c405..9b6640be5a51033779603b3ddf459d61ef1878a7 100644 (file)
 #include "closestream.h"
 #include "procutils.h"
 #include "strutils.h"
+#include "ttyutils.h"
+#include "xalloc.h"
 
 struct signv {
-       char *name;
+       const char *name;
        int val;
 } sys_signame[] = {
        /* POSIX signals */
@@ -64,25 +66,10 @@ struct signv {
        { "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
+       { "ABRT",       SIGABRT },      /* 6 */
 #ifdef SIGIOT
        { "IOT",        SIGIOT },       /* 6, same as SIGABRT */
 #endif
@@ -92,23 +79,28 @@ struct signv {
 #ifdef SIGBUS
        { "BUS",        SIGBUS },       /* 7 (arm,i386,m68k,ppc), 10 (mips,alpha,sparc*) */
 #endif
-#ifdef SIGSYS
-       { "SYS",        SIGSYS },       /* 12 (mips,alpha,sparc*) */
-#endif
+       { "FPE",        SIGFPE },       /* 8 */
+       { "KILL",       SIGKILL },      /* 9 */
+       { "USR1",       SIGUSR1 },      /* 10 (arm,i386,m68k,ppc), 30 (alpha,sparc*), 16 (mips) */
+       { "SEGV",       SIGSEGV },      /* 11 */
+       { "USR2",       SIGUSR2 },      /* 12 (arm,i386,m68k,ppc), 31 (alpha,sparc*), 17 (mips) */
+       { "PIPE",       SIGPIPE },      /* 13 */
+       { "ALRM",       SIGALRM },      /* 14 */
+       { "TERM",       SIGTERM },      /* 15 */
 #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
+       { "CHLD",       SIGCHLD },      /* 17 (arm,i386,m68k,ppc), 20 (alpha,sparc*), 18 (mips) */
 #ifdef SIGCLD
        { "CLD",        SIGCLD },       /* same as SIGCHLD (mips) */
+#endif
+       { "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) */
+#ifdef SIGURG
+       { "URG",        SIGURG },       /* 23 (arm,i386,m68k,ppc), 16 (alpha,sparc*), 21 (mips) */
 #endif
 #ifdef SIGXCPU
        { "XCPU",       SIGXCPU },      /* 24 (arm,i386,m68k,ppc,alpha,sparc*), 30 (mips) */
@@ -122,8 +114,14 @@ struct signv {
 #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) */
+#ifdef SIGWINCH
+       { "WINCH",      SIGWINCH },     /* 28 (arm,i386,m68k,ppc,alpha,sparc*), 20 (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 SIGINFO
        { "INFO",       SIGINFO },      /* 29 (alpha) */
@@ -131,18 +129,21 @@ struct signv {
 #ifdef SIGLOST
        { "LOST",       SIGLOST },      /* 29 (arm,i386,m68k,ppc,sparc*) */
 #endif
-#ifdef SIGWINCH
-       { "WINCH",      SIGWINCH },     /* 28 (arm,i386,m68k,ppc,alpha,sparc*), 20 (mips) */
+#ifdef SIGPWR
+       { "PWR",        SIGPWR },       /* 30 (arm,i386,m68k,ppc), 29 (alpha,sparc*), 19 (mips) */
 #endif
 #ifdef SIGUNUSED
        { "UNUSED",     SIGUNUSED },    /* 31 (arm,i386,m68k,ppc) */
 #endif
+#ifdef SIGSYS
+       { "SYS",        SIGSYS },       /* 31 (mips,alpha,sparc*) */
+#endif
 };
 
 static int arg_to_signum (char *arg, int mask);
 static void nosig (char *name);
 static void printsig (int sig);
-static void printsignals (FILE *fp);
+static void printsignals (FILE *fp, int pretty);
 static int usage (int status);
 static int kill_verbose (char *procname, int pid, int sig);
 
@@ -193,7 +194,7 @@ int main (int argc, char *argv[])
        }
        if (! strcmp (arg, "-l") || ! strcmp (arg, "--list")) {
            if (argc < 2) {
-               printsignals (stdout);
+               printsignals (stdout, 0);
                return EXIT_SUCCESS;
            }
            if (argc > 2) {
@@ -206,6 +207,10 @@ int main (int argc, char *argv[])
            printsig (numsig);
            return EXIT_SUCCESS;
        }
+       if (! strcmp (arg, "-L") || ! strcmp (arg, "--table")) {
+           printsignals (stdout, 1);
+           return EXIT_SUCCESS;
+       }
        if (! strcmp (arg, "-p") || ! strcmp (arg, "--pid")) {
            do_pid++;
            if (do_kill)
@@ -368,7 +373,7 @@ static int arg_to_signum (char *arg, int maskbit)
 static void nosig (char *name)
 {
     warnx (_("unknown signal %s; valid signals:"), name);
-    printsignals (stderr);
+    printsignals (stderr, 1);
 }
 
 static void printsig (int sig)
@@ -390,22 +395,53 @@ static void printsig (int sig)
     printf("%d\n", sig);
 }
 
-static void printsignals (FILE *fp)
+#define FIELD_WIDTH 11
+static void pretty_print_signal(FILE *fp, size_t term_width, size_t *lpos,
+                               int signum, const char *name)
 {
-    size_t n, lth, lpos = 0;
-
-    for (n = 0; n < ARRAY_SIZE(sys_signame); n++) {
-       lth = 1+strlen(sys_signame[n].name);
-       if (lpos+lth > 72) {
+       if (term_width < (*lpos + FIELD_WIDTH)) {
            fputc ('\n', fp);
-           lpos = 0;
-       } else if (lpos)
-           fputc (' ', fp);
-       lpos += lth;
-       fputs (sys_signame[n].name, fp);
+           *lpos = 0;
+       }
+       *lpos += FIELD_WIDTH;
+       fprintf (fp, "%2d %-8s", signum, name);
+}
+
+static void printsignals (FILE *fp, int pretty)
+{
+    size_t n, lth, lpos = 0, width;
+
+    if (!pretty) {
+       for (n = 0; n < ARRAY_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);
+       }
+#ifdef SIGRTMIN
+       fputs (" RT<N> RTMIN+<N> RTMAX-<N>", fp);
+#endif
+       fputc ('\n', fp);
+       return;
     }
+    /* pretty print */
+    width = get_terminal_width();
+    if (width == 0)
+       width = 72;
+    else
+       width -= 1;
+
+    for (n = 0; n < ARRAY_SIZE(sys_signame); n++)
+           pretty_print_signal(fp, width, &lpos,
+                           sys_signame[n].val, sys_signame[n].name);
+
 #ifdef SIGRTMIN
-    fputs (" RT<N> RTMIN+<N> RTMAX-<N>", fp);
+    pretty_print_signal(fp, width, &lpos, SIGRTMIN, "RTMIN");
+    pretty_print_signal(fp, width, &lpos, SIGRTMAX, "RTMAX");
 #endif
     fputc ('\n', fp);
 }
@@ -423,6 +459,7 @@ static int usage(int status)
        fputs(_(" -q, --queue <sig>   use sigqueue(2) rather than kill(2)\n"), out);
        fputs(_(" -p, --pid           print pids without signaling them\n"), out);
        fputs(_(" -l, --list <name>   list signal names\n"), out);
+       fputs(_(" -L, --table         list signal names and numbers\n"), out);
        fputs(USAGE_SEPARATOR, out);
        fputs(USAGE_HELP, out);
        fputs(USAGE_VERSION, out);