In sysctl KERN_FILE_BYPID stop traversal after pid has been found.
authorbluhm <bluhm@openbsd.org>
Thu, 8 Aug 2024 15:02:36 +0000 (15:02 +0000)
committerbluhm <bluhm@openbsd.org>
Thu, 8 Aug 2024 15:02:36 +0000 (15:02 +0000)
When searching for a specific process, there is no need to traverse
the list of all processes to the end.  Break after pid has been
found and the file structure has been filled.  Also check for arg
>= 0 as this is consistent with the arg < -1 check before.  This
makes no functional difference as process 0 has PS_SYSTEM set and
is skipped anyway.

OK millert@ mvs@

sys/kern/kern_sysctl.c

index 3ac262a..74b697e 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: kern_sysctl.c,v 1.435 2024/08/08 10:25:00 mvs Exp $   */
+/*     $OpenBSD: kern_sysctl.c,v 1.436 2024/08/08 15:02:36 bluhm Exp $ */
 /*     $NetBSD: kern_sysctl.c,v 1.17 1996/05/20 17:49:05 mrg Exp $     */
 
 /*-
@@ -1682,7 +1682,7 @@ sysctl_file(int *name, u_int namelen, char *where, size_t *sizep,
                         */
                        if (pr->ps_flags & (PS_SYSTEM | PS_EMBRYO | PS_EXITING))
                                continue;
-                       if (arg > 0 && pr->ps_pid != (pid_t)arg) {
+                       if (arg >= 0 && pr->ps_pid != (pid_t)arg) {
                                /* not the pid we are looking for */
                                continue;
                        }
@@ -1702,6 +1702,9 @@ sysctl_file(int *name, u_int namelen, char *where, size_t *sizep,
                                FILLIT(fp, fdp, i, NULL, pr);
                                FRELE(fp, p);
                        }
+                       /* pid is unique, stop searching */
+                       if (arg >= 0)
+                               break;
                }
                if (!matched)
                        error = ESRCH;