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.
This commit is contained in:
FRIGN 2015-03-16 12:37:46 +01:00
parent ac01240800
commit 64da0d1ebc
3 changed files with 80 additions and 97 deletions

2
README
View File

@ -37,7 +37,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
=* grep yes none =* grep yes none
=*| head yes none =*| head yes none
=*| hostname non-posix none =*| hostname non-posix none
=* kill yes none =*| kill yes none
=*| link yes none =*| link yes none
=*| ln yes none =*| ln yes none
=*| logger yes none =*| logger yes none

41
kill.1
View File

@ -1,4 +1,4 @@
.Dd November 23, 2014 .Dd March 16, 2015
.Dt KILL 1 .Dt KILL 1
.Os sbase .Os sbase
.Sh NAME .Sh NAME
@ -6,34 +6,25 @@
.Nd signal processes .Nd signal processes
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl s Ar signal_name .Op Fl s Ar signame | Fl num | Fl signame
.Ar pid ... .Ar pid ...
.Nm .Nm
.Fl l Op Ar exit_status .Fl l Op Ar num
.Nm
.Fl Ar signal_name
.Ar pid ...
.Nm
.Fl Ar signal_number
.Ar pid ...
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .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 .Sh OPTIONS
.Bl -tag -width Ds .Bl -tag -width Ds
.It Fl l Op Ar exit_status .It Fl l Op Ar num
Lists available signals. If an List all available signals or the signal name of
.Ar exit_status .Ar num .
is given, only the corresponding signal name will be printed. .It Fl s Ar signame | Fl num | Fl signame
.It Fl s Ar signal_name Send signal corresponding to
A symbolic signal name specifying the signal to be sent instead of the .Ar signame
default SIGTERM. Sends the named signal. |
.It Fl signal_name .Ar num .
A symbolic signal name specifying the signal to be sent instead The default is TERM.
of the default SIGTERM.
.It Fl signal_number
A non-negative decimal integer specifying the signal to be sent instead of
the default SIGTERM.
.El .El
.Sh SEE ALSO .Sh SEE ALSO
.Xr kill 2 , .Xr kill 2 ,
@ -46,9 +37,9 @@ utility is compliant with the
specification. specification.
.Pp .Pp
The The
.Fl Ar signal_name .Fl Ar signame
and and
.Fl Ar signal_number .Fl Ar num
syntax is marked by syntax is marked by
.St -p1003.1-2008 .St -p1003.1-2008
as being an as being an

114
kill.c
View File

@ -2,18 +2,16 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <limits.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <strings.h>
#include "util.h" #include "util.h"
struct { struct {
const char *name; const char *name;
int sig; const int sig;
} sigs[] = { } sigs[] = {
{ "0", 0 }, { "0", 0 },
#define SIG(n) { #n, SIG##n } #define SIG(n) { #n, SIG##n }
@ -23,11 +21,8 @@ struct {
#undef SIG #undef SIG
}; };
const char *sig2name(int);
int name2sig(const char *);
const char * const char *
sig2name(int sig) sig2name(const int sig)
{ {
size_t i; size_t i;
@ -35,104 +30,101 @@ sig2name(int sig)
if (sigs[i].sig == sig) if (sigs[i].sig == sig)
return sigs[i].name; return sigs[i].name;
eprintf("%d: bad signal number\n", sig); eprintf("%d: bad signal number\n", sig);
/* unreachable */
return NULL; return NULL; /* not reached */
} }
int const int
name2sig(const char *name) name2sig(const char *name)
{ {
size_t i; size_t i;
for (i = 0; i < LEN(sigs); 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; return sigs[i].sig;
eprintf("%s: bad signal name\n", name); eprintf("%s: bad signal name\n", name);
/* unreachable */
return -1; return -1; /* not reached */
} }
static void static void
usage(void) usage(void)
{ {
weprintf("usage: %s [-s signame | -signum | -signame] pid ...\n", argv0); eprintf("usage: %s [-s signame | -num | -signame] pid ...\n"
weprintf(" %s -l [exit_status]\n", argv0); " %s -l [num]\n", argv0, argv0);
exit(1);
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *end;
int ret = 0;
int sig = SIGTERM;
pid_t pid; pid_t pid;
size_t i; size_t i;
int ret = 0, sig = SIGTERM;
argv0 = argv[0]; argv0 = argv[0], argc--, argv++;
if (argc < 2) if (!argc)
usage(); usage();
if ((*argv)[0] == '-') {
switch ((*argv)[1]) {
case 'l':
if ((*argv)[2])
goto longopt;
argc--, argv++; argc--, argv++;
if (strcmp(argv[0], "-l") == 0) { if (!argc) {
argc--, argv++;
if (argc == 0) {
for (i = 0; i < LEN(sigs); i++) for (i = 0; i < LEN(sigs); i++)
puts(sigs[i].name); puts(sigs[i].name);
exit(0); return 0;
} else if (argc > 1) } else if (argc == 1) {
usage(); sig = estrtonum(*argv, 0, INT_MAX);
errno = 0;
sig = strtol(argv[0], &end, 10);
if (*end != '\0' || errno != 0)
eprintf("%s: bad signal number\n", argv[0]);
if (sig > 128) if (sig > 128)
sig = WTERMSIG(sig); sig = WTERMSIG(sig);
puts(sig2name(sig)); puts(sig2name(sig));
exit(0); return 0;
} } else {
if (strcmp(argv[0], "-s") == 0) {
argc--, argv++;
if (argc == 0)
usage(); usage();
sig = name2sig(argv[0]); }
break;
case 's':
if ((*argv)[2])
goto longopt;
argc--, argv++; argc--, argv++;
} else if (argv[0][0] == '-') { if (!argc)
if (isdigit(argv[0][1])) { usage();
/* handle XSI extension -signum */ sig = name2sig(*argv);
errno = 0; argc--, argv++;
sig = strtol(&argv[0][1], &end, 10); break;
if (*end != '\0' || errno != 0) case '-':
eprintf("%s: bad signal number\n", &argv[0][1]); 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); sig2name(sig);
argc--, argv++; } else {
} else if (argv[0][1] != '-') { sig = name2sig((*argv) + 1);
/* handle XSI extension -signame */ }
sig = name2sig(&argv[0][1]);
argc--, argv++; argc--, argv++;
} }
} }
if (argc > 0 && strcmp(argv[0], "--") == 0) if (argc && !strcmp(*argv, "--"))
argc--, argv++; argc--, argv++;
if (argc == 0) if (!argc)
usage(); usage();
for (; argc; argc--, argv++) { for (; *argv; argc--, argv++) {
errno = 0; pid = estrtonum(*argv, INT_MIN, INT_MAX);
pid = strtol(argv[0], &end, 10);
if (*end == '\0' && errno == 0) {
if (kill(pid, sig) < 0) { if (kill(pid, sig) < 0) {
weprintf("kill %d:", pid); weprintf("kill %d:", pid);
ret = 1; ret = 1;
} }
} else {
weprintf("%s: bad pid\n", argv[0]);
ret = 1;
}
} }
exit(ret); return ret;
} }