Apparently, we used to created several kthreads before the kernel
random number generator was up and running. A toggle, "randompid",
was needed to tell allocpid() whether it made sense to attempt to
allocate random PIDs.
However, these days we get e.g. arc4random(9) into a working state
before any kthreads are spawned, so the toggle is no longer needed.
Thread: https://marc.info/?l=openbsd-tech&m=
165541052614453&w=2
Very nice historical context provided by miod@.
probably ok miod@ deraadt@
-/* $OpenBSD: init_main.c,v 1.315 2022/02/22 01:15:01 guenther Exp $ */
+/* $OpenBSD: init_main.c,v 1.316 2022/07/23 22:10:58 cheloha Exp $ */
/* $NetBSD: init_main.c,v 1.84.4.1 1996/06/02 09:08:06 mrg Exp $ */
/*
initprocess = initproc->p_p;
}
- randompid = 1;
-
/*
* Create any kernel threads whose creation was deferred because
* initprocess had not yet been created.
-/* $OpenBSD: kern_fork.c,v 1.240 2022/05/13 15:32:00 claudio Exp $ */
+/* $OpenBSD: kern_fork.c,v 1.241 2022/07/23 22:10:58 cheloha Exp $ */
/* $NetBSD: kern_fork.c,v 1.29 1996/02/09 18:59:34 christos Exp $ */
/*
int nprocesses = 1; /* process 0 */
int nthreads = 1; /* proc 0 */
-int randompid; /* when set to 1, pid's go random */
struct forkstat forkstat;
void fork_return(void *);
pid_t
allocpid(void)
{
- static pid_t lastpid;
+ static int first = 1;
pid_t pid;
- if (!randompid) {
- /* only used early on for system processes */
- pid = ++lastpid;
- } else {
- /* Find an unused pid satisfying lastpid < pid <= PID_MAX */
- do {
- pid = arc4random_uniform(PID_MAX - lastpid) + 1 +
- lastpid;
- } while (ispidtaken(pid));
+ /* The first PID allocated is always 1. */
+ if (first) {
+ first = 0;
+ return 1;
}
+ /*
+ * All subsequent PIDs are chosen randomly. We need to
+ * find an unused PID in the range [2, PID_MAX].
+ */
+ do {
+ pid = 2 + arc4random_uniform(PID_MAX - 1);
+ } while (ispidtaken(pid));
return pid;
}
-/* $OpenBSD: proc.h,v 1.333 2022/07/05 15:06:16 visa Exp $ */
+/* $OpenBSD: proc.h,v 1.334 2022/07/23 22:10:59 cheloha Exp $ */
/* $NetBSD: proc.h,v 1.44 1996/04/22 01:23:21 christos Exp $ */
/*-
extern struct process process0; /* Process slot for kernel threads. */
extern int nprocesses, maxprocess; /* Cur and max number of processes. */
extern int nthreads, maxthread; /* Cur and max number of threads. */
-extern int randompid; /* fork() should create random pid's */
LIST_HEAD(proclist, proc);
LIST_HEAD(processlist, process);