Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/kill Every integer that fits within a pid_t is a potenti...



details:   https://anonhg.NetBSD.org/src/rev/db2bc1f7ee9a
branches:  trunk
changeset: 938012:db2bc1f7ee9a
user:      kre <kre%NetBSD.org@localhost>
date:      Sun Aug 30 19:35:09 2020 +0000

description:
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.

diffstat:

 bin/kill/kill.c |  27 +++++++++++++++------------
 1 files changed, 15 insertions(+), 12 deletions(-)

diffs (83 lines):

diff -r 2ca7c5a432fc -r db2bc1f7ee9a bin/kill/kill.c
--- a/bin/kill/kill.c   Sun Aug 30 18:26:41 2020 +0000
+++ b/bin/kill/kill.c   Sun Aug 30 19:35:09 2020 +0000
@@ -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 @@
 #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 @@
 __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 @@
        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 @@
                        }
                } 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 @@
        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



Home | Main Index | Thread Index | Old Index