Every integer that fits within a pid_t is a potential "pid" arg to kill.
That means we cannot use (pid_t)-1 as an error indicator, as that's a valid pid to use (described as working in kill(1) - yet it wasn't working in /bin/kill (nor sh's builtin kill, which is essentially the same code). This is even required to work by POSIX. So change processnum() (the parser/validator for the pid args) to take a pointer to a pid_t and return the pid that way, leaving the return value of the (now int) function to indicate just ok/error. While here, fix the validation a little ('' is no longer an accepted alias for 0) and in case of an error from kill(2) have the error message indicate whether the kill was targeted at a pid of a pgrp.
This commit is contained in:
parent
1465c374a8
commit
4d4d16423c
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: kill.c,v 1.31 2020/08/30 16:10:40 kre Exp $ */
|
||||
/* $NetBSD: kill.c,v 1.32 2020/08/30 19:35:09 kre Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1988, 1993, 1994
|
||||
|
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\
|
|||
#if 0
|
||||
static char sccsid[] = "@(#)kill.c 8.4 (Berkeley) 4/28/95";
|
||||
#else
|
||||
__RCSID("$NetBSD: kill.c,v 1.31 2020/08/30 16:10:40 kre Exp $");
|
||||
__RCSID("$NetBSD: kill.c,v 1.32 2020/08/30 19:35:09 kre Exp $");
|
||||
#endif
|
||||
#endif /* not lint */
|
||||
|
||||
|
@ -66,7 +66,7 @@ int killcmd(int, char *argv[]);
|
|||
__dead static void nosig(const char *);
|
||||
void printsignals(FILE *, int);
|
||||
static int signum(const char *);
|
||||
static pid_t processnum(const char *);
|
||||
static int processnum(const char *, pid_t *);
|
||||
__dead static void usage(void);
|
||||
|
||||
int
|
||||
|
@ -177,6 +177,7 @@ main(int argc, char *argv[])
|
|||
for (errors = 0; argc; argc--, argv++) {
|
||||
#ifdef SHELL
|
||||
extern int getjobpgrp(const char *);
|
||||
|
||||
if (*argv[0] == '%') {
|
||||
pid = getjobpgrp(*argv);
|
||||
if (pid == 0) {
|
||||
|
@ -186,13 +187,13 @@ main(int argc, char *argv[])
|
|||
}
|
||||
} else
|
||||
#endif
|
||||
if ((pid = processnum(*argv)) == (pid_t)-1) {
|
||||
if (processnum(*argv, &pid) != 0) {
|
||||
errors = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (kill(pid, numsig) == -1) {
|
||||
warn("%s", *argv);
|
||||
warn("%s %s", pid < -1 ? "pgrp" : "pid", *argv);
|
||||
errors = 1;
|
||||
}
|
||||
#ifdef SHELL
|
||||
|
@ -226,22 +227,24 @@ signum(const char *sn)
|
|||
return (int)n;
|
||||
}
|
||||
|
||||
static pid_t
|
||||
processnum(const char *s)
|
||||
static int
|
||||
processnum(const char *s, pid_t *pid)
|
||||
{
|
||||
intmax_t n;
|
||||
char *ep;
|
||||
|
||||
errno = 0;
|
||||
n = strtoimax(s, &ep, 10);
|
||||
|
||||
/* check for correctly parsed number */
|
||||
if (*ep || n == INTMAX_MIN || n == INTMAX_MAX || (pid_t)n != n ||
|
||||
n == -1) {
|
||||
warnx("illegal process%s id: %s", (n < 0 ? " group" : ""), s);
|
||||
n = -1;
|
||||
if (ep == s || *ep || n == INTMAX_MIN || n == INTMAX_MAX ||
|
||||
(pid_t)n != n || errno != 0) {
|
||||
warnx("illegal process%s id: '%s'", (n < 0 ? " group" : ""), s);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (pid_t)n;
|
||||
*pid = (pid_t)n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
Loading…
Reference in New Issue