[hackers] [sbase] Audit kill(1) || FRIGN

From: <git_AT_suckless.org>
Date: Mon, 16 Mar 2015 18:41:17 +0100 (CET)

commit bc65f343f7d2959410f9786b8c8c2df557ede0be
Author: FRIGN <dev_AT_frign.de>
Date: Mon Mar 16 12:37:46 2015 +0100

    Audit kill(1)
    
    1) Refactor the manpage with num-options, optimize wording to be more
       concise and to the point, pid also specifies process groups.
    2) Make int sig const.
    3) Remove prototypes.
    4) /* not reached */ consistency.
    5) Refactor usage() with eprintf.
    6) Refactor arg-parser with a switch, use estrtonum
    7) Use return instead of exit() in main()
    8) argc-argv-correctness.

diff --git a/README b/README
index 24d71d6..ecace49 100644
--- a/README
+++ b/README
_AT_@ -37,7 +37,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
 =* grep yes none
 =*| head yes none
 =*| hostname non-posix none
-=* kill yes none
+=*| kill yes none
 =*| link yes none
 =*| ln yes none
 =*| logger yes none
diff --git a/kill.1 b/kill.1
index bb11c7a..8ef072b 100644
--- a/kill.1
+++ b/kill.1
_AT_@ -1,4 +1,4 @@
-.Dd November 23, 2014
+.Dd March 16, 2015
 .Dt KILL 1
 .Os sbase
 .Sh NAME
_AT_@ -6,34 +6,25 @@
 .Nd signal processes
 .Sh SYNOPSIS
 .Nm
-.Op Fl s Ar signal_name
+.Op Fl s Ar signame | Fl num | Fl signame
 .Ar pid ...
 .Nm
-.Fl l Op Ar exit_status
-.Nm
-.Fl Ar signal_name
-.Ar pid ...
-.Nm
-.Fl Ar signal_number
-.Ar pid ...
+.Fl l Op Ar num
 .Sh DESCRIPTION
 .Nm
-by default sends a TERM signal to the given processes.
+signals TERM to each process or process group specified by
+.Ar pid .
 .Sh OPTIONS
 .Bl -tag -width Ds
-.It Fl l Op Ar exit_status
-Lists available signals. If an
-.Ar exit_status
-is given, only the corresponding signal name will be printed.
-.It Fl s Ar signal_name
-A symbolic signal name specifying the signal to be sent instead of the
-default SIGTERM. Sends the named signal.
-.It Fl signal_name
-A symbolic signal name specifying the signal to be sent instead
-of the default SIGTERM.
-.It Fl signal_number
-A non-negative decimal integer specifying the signal to be sent instead of
-the default SIGTERM.
+.It Fl l Op Ar num
+List all available signals or the signal name of
+.Ar num .
+.It Fl s Ar signame | Fl num | Fl signame
+Send signal corresponding to
+.Ar signame
+|
+.Ar num .
+The default is TERM.
 .El
 .Sh SEE ALSO
 .Xr kill 2 ,
_AT_@ -46,9 +37,9 @@ utility is compliant with the
 specification.
 .Pp
 The
-.Fl Ar signal_name
+.Fl Ar signame
 and
-.Fl Ar signal_number
+.Fl Ar num
 syntax is marked by
 .St -p1003.1-2008
 as being an
diff --git a/kill.c b/kill.c
index 22bb731..7930085 100644
--- a/kill.c
+++ b/kill.c
_AT_@ -2,18 +2,16 @@
 #include <sys/wait.h>
 
 #include <ctype.h>
-#include <errno.h>
+#include <limits.h>
 #include <signal.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
-#include <strings.h>
 
 #include "util.h"
 
 struct {
         const char *name;
- int sig;
+ const int sig;
 } sigs[] = {
         { "0", 0 },
 #define SIG(n) { #n, SIG##n }
_AT_@ -23,11 +21,8 @@ struct {
 #undef SIG
 };
 
-const char *sig2name(int);
-int name2sig(const char *);
-
 const char *
-sig2name(int sig)
+sig2name(const int sig)
 {
         size_t i;
 
_AT_@ -35,104 +30,101 @@ sig2name(int sig)
                 if (sigs[i].sig == sig)
                         return sigs[i].name;
         eprintf("%d: bad signal number\n", sig);
- /* unreachable */
- return NULL;
+
+ return NULL; /* not reached */
 }
 
-int
+const int
 name2sig(const char *name)
 {
         size_t i;
 
         for (i = 0; i < LEN(sigs); i++)
- if (strcasecmp(sigs[i].name, name) == 0)
+ if (!strcasecmp(sigs[i].name, name))
                         return sigs[i].sig;
         eprintf("%s: bad signal name\n", name);
- /* unreachable */
- return -1;
+
+ return -1; /* not reached */
 }
 
 static void
 usage(void)
 {
- weprintf("usage: %s [-s signame | -signum | -signame] pid ...\n", argv0);
- weprintf(" %s -l [exit_status]\n", argv0);
- exit(1);
+ eprintf("usage: %s [-s signame | -num | -signame] pid ...\n"
+ " %s -l [num]\n", argv0, argv0);
 }
 
 int
 main(int argc, char *argv[])
 {
- char *end;
- int ret = 0;
- int sig = SIGTERM;
         pid_t pid;
         size_t i;
+ int ret = 0, sig = SIGTERM;
 
- argv0 = argv[0];
- if (argc < 2)
+ argv0 = argv[0], argc--, argv++;
+ if (!argc)
                 usage();
 
- argc--, argv++;
- if (strcmp(argv[0], "-l") == 0) {
- argc--, argv++;
- if (argc == 0) {
- for (i = 0; i < LEN(sigs); i++)
- puts(sigs[i].name);
- exit(0);
- } else if (argc > 1)
- usage();
- errno = 0;
- sig = strtol(argv[0], &end, 10);
- if (*end != '\0' || errno != 0)
- eprintf("%s: bad signal number\n", argv[0]);
- if (sig > 128)
- sig = WTERMSIG(sig);
- puts(sig2name(sig));
- exit(0);
- }
-
- if (strcmp(argv[0], "-s") == 0) {
- argc--, argv++;
- if (argc == 0)
- usage();
- sig = name2sig(argv[0]);
- argc--, argv++;
- } else if (argv[0][0] == '-') {
- if (isdigit(argv[0][1])) {
- /* handle XSI extension -signum */
- errno = 0;
- sig = strtol(&argv[0][1], &end, 10);
- if (*end != '\0' || errno != 0)
- eprintf("%s: bad signal number\n", &argv[0][1]);
- sig2name(sig);
+ if ((*argv)[0] == '-') {
+ switch ((*argv)[1]) {
+ case 'l':
+ if ((*argv)[2])
+ goto longopt;
+ argc--, argv++;
+ if (!argc) {
+ for (i = 0; i < LEN(sigs); i++)
+ puts(sigs[i].name);
+ return 0;
+ } else if (argc == 1) {
+ sig = estrtonum(*argv, 0, INT_MAX);
+ if (sig > 128)
+ sig = WTERMSIG(sig);
+ puts(sig2name(sig));
+ return 0;
+ } else {
+ usage();
+ }
+ break;
+ case 's':
+ if ((*argv)[2])
+ goto longopt;
+ argc--, argv++;
+ if (!argc)
+ usage();
+ sig = name2sig(*argv);
                         argc--, argv++;
- } else if (argv[0][1] != '-') {
- /* handle XSI extension -signame */
- sig = name2sig(&argv[0][1]);
+ break;
+ case '-':
+ if ((*argv)[2])
+ goto longopt;
+ argc--, argv++;
+ break;
+ default:
+ longopt:
+ /* XSI-extensions -argnum and -argname*/
+ if (isdigit((*argv)[1])) {
+ sig = estrtonum((*argv) + 1, 0, INT_MAX);
+ sig2name(sig);
+ } else {
+ sig = name2sig((*argv) + 1);
+ }
                         argc--, argv++;
                 }
         }
 
- if (argc > 0 && strcmp(argv[0], "--") == 0)
+ if (argc && !strcmp(*argv, "--"))
                 argc--, argv++;
 
- if (argc == 0)
+ if (!argc)
                 usage();
 
- for (; argc; argc--, argv++) {
- errno = 0;
- pid = strtol(argv[0], &end, 10);
- if (*end == '\0' && errno == 0) {
- if (kill(pid, sig) < 0) {
- weprintf("kill %d:", pid);
- ret = 1;
- }
- } else {
- weprintf("%s: bad pid\n", argv[0]);
+ for (; *argv; argc--, argv++) {
+ pid = estrtonum(*argv, INT_MIN, INT_MAX);
+ if (kill(pid, sig) < 0) {
+ weprintf("kill %d:", pid);
                         ret = 1;
                 }
         }
 
- exit(ret);
+ return ret;
 }
Received on Mon Mar 16 2015 - 18:41:17 CET

This archive was generated by hypermail 2.3.0 : Mon Mar 16 2015 - 18:48:10 CET