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@
-/* $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 $ */
/*-
*/
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;
}
FILLIT(fp, fdp, i, NULL, pr);
FRELE(fp, p);
}
+ /* pid is unique, stop searching */
+ if (arg >= 0)
+ break;
}
if (!matched)
error = ESRCH;