From 73cd54a1f5fbdefbafff9227f5d00feb86b4199e Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sat, 11 Oct 2025 13:40:09 -0400 Subject: kern: proc: Add process lookups by PID Signed-off-by: Ian Moffett --- src/sys/include/sys/proc.h | 9 +++++++++ src/sys/os/os_proc.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/sys/include/sys/proc.h b/src/sys/include/sys/proc.h index 48c784a..0cf105e 100644 --- a/src/sys/include/sys/proc.h +++ b/src/sys/include/sys/proc.h @@ -202,6 +202,15 @@ int md_set_ip(struct proc *procp, uintptr_t ip); */ int proc_check_addr(struct proc *proc, uintptr_t addr, size_t len); +/* + * Lookup a process using its PID + * + * @pid: PID of process to lookup + * + * Returns NULL on failure + */ +struct proc *proc_lookup(pid_t pid); + /* * Put the current process into a halt loop * until the next one runs. diff --git a/src/sys/os/os_proc.c b/src/sys/os/os_proc.c index 4af45a0..decb41e 100644 --- a/src/sys/os/os_proc.c +++ b/src/sys/os/os_proc.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -44,7 +45,17 @@ #include #include #include +#include +/* + * System-wide process state + * + * XXX: The process queue here is different from the runqueues, + * these keep track of the processes present whether they + * are running or not (unless terminated) + */ +static bool is_procq_init = false; +static TAILQ_HEAD(, proc) procq; static pid_t next_pid = 0; /* @@ -171,6 +182,12 @@ proc_init(struct proc *procp, int flags) return -EINVAL; } + /* Initialize the process queue once */ + if (!is_procq_init) { + TAILQ_INIT(&procq); + is_procq_init = true; + } + /* Put the process in a known state */ scdp = &procp->scdom; memset(procp, 0, sizeof(*procp)); @@ -197,7 +214,33 @@ proc_init(struct proc *procp, int flags) } error = fdtab_init(procp); - return error; + if (error != 0) { + return error; + } + + TAILQ_INSERT_TAIL(&procq, procp, link); + return 0; +} + +/* + * Lookup a process by PID + */ +struct proc * +proc_lookup(pid_t pid) +{ + struct proc *curproc; + + TAILQ_FOREACH(curproc, &procq, link) { + if (curproc == NULL) { + continue; + } + + if (curproc->pid == pid) { + return curproc; + } + } + + return NULL; } /* @@ -237,6 +280,7 @@ proc_kill(struct proc *procp, int status) procp->flags |= PROC_EXITING; proc_clear_ranges(procp); + TAILQ_REMOVE(&procq, procp, link); return md_proc_kill(procp, 0); } -- cgit v1.2.3