From 0217120d464c11113623c73ef63c30dfa5957b8d Mon Sep 17 00:00:00 2001 From: bluhm Date: Thu, 8 Aug 2024 15:02:36 +0000 Subject: [PATCH] In sysctl KERN_FILE_BYPID stop traversal after pid has been found. 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 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c index 3ac262a25a2..74b697ec706 100644 --- a/sys/kern/kern_sysctl.c +++ b/sys/kern/kern_sysctl.c @@ -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; -- 2.20.1