diff options
author | Ian Moffett <ian@osmora.org> | 2025-10-11 13:40:09 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-10-11 13:40:09 -0400 |
commit | 73cd54a1f5fbdefbafff9227f5d00feb86b4199e (patch) | |
tree | 476024f30be7c401b06a3c825dfb7217f908ea5a /src/sys | |
parent | 15b856bec1cbe233bcdcdf2712c646c73d87f9f5 (diff) |
kern: proc: Add process lookups by PID
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src/sys')
-rw-r--r-- | src/sys/include/sys/proc.h | 9 | ||||
-rw-r--r-- | src/sys/os/os_proc.c | 46 |
2 files changed, 54 insertions, 1 deletions
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 @@ -203,6 +203,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 <sys/syslog.h> #include <sys/panic.h> #include <sys/proc.h> +#include <sys/queue.h> #include <sys/cpuvar.h> #include <os/systm.h> #include <vm/vm.h> @@ -44,7 +45,17 @@ #include <os/kalloc.h> #include <os/filedesc.h> #include <string.h> +#include <stdbool.h> +/* + * 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); } |