summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordzwdz2024-02-21 21:08:12 +0100
committerdzwdz2024-02-21 21:08:12 +0100
commit8adaf0da869a17ba37ab7d5cf4fddbf5d878978f (patch)
treef18336efbb97c7016a170722b84c6838f7cab8ba
parent4b8f771d6ec926f95ec017c427348013d4c1872c (diff)
kernel: integrate the proc_ns_next fixes into proc_next
-rw-r--r--src/kernel/proc.c49
-rw-r--r--src/kernel/proc.h3
-rw-r--r--src/kernel/syscalls.c2
-rw-r--r--src/kernel/vfs/procfs.c4
4 files changed, 26 insertions, 32 deletions
diff --git a/src/kernel/proc.c b/src/kernel/proc.c
index dc17af2..26b9e3a 100644
--- a/src/kernel/proc.c
+++ b/src/kernel/proc.c
@@ -145,34 +145,26 @@ Proc *proc_ns_byid(Proc *ns, uint32_t id) {
return NULL;
}
+/** Used for iterating over all processes in ns. */
Proc *proc_ns_next(Proc *ns, Proc *p) {
- Proc *ret = NULL;
- /* see comments in proc_next */
+ /* analogous to proc_next */
+ assert(ns);
+ assert(ns->pns == ns); /* is ns the namespace root? */
+ assert(p);
+ assert(proc_ns_contains(ns, p));
- if (!p) goto end;
- /* descend into children who own their own namespace, but no further */
if (p->child && proc_ns_contains(ns, p->child)) {
- ret = p->child;
- goto end;
- }
- // TODO diverged from proc_next, integrate this fix into it
- // also once you do that do regression tests - this behaviour is buggy
- if (p == ns) {
- /* don't escape the root */
- goto end;
+ return p->child;
}
+
while (!p->sibling) {
+ if (p == ns) return NULL;
p = p->parent;
assert(p);
- if (p == ns) goto end;
- }
- ret = p->sibling;
-
-end:
- if (ret != NULL) {
- assert(proc_ns_contains(ns, ret));
+ assert(proc_ns_contains(ns, p));
}
- return ret;
+ if (p == ns) return NULL;
+ return p->sibling;
}
void proc_ns_create(Proc *proc) {
@@ -447,7 +439,7 @@ _Noreturn void proc_switch_any(void) {
if (proc_cur && proc_cur->state == PS_RUNNING) {
p = proc_cur;
} else {
- for (p = proc_first; p; p = proc_next(p, NULL)) {
+ for (p = proc_first; p; p = proc_next(proc_first, p)) {
if (p->state == PS_RUNNING) {
break;
}
@@ -468,24 +460,27 @@ _Noreturn void proc_switch_any(void) {
}
}
-Proc *proc_next(Proc *p, Proc *root) {
- /* depth-first search, the order is:
+/** Used for iterating over all descendants of root. */
+Proc *proc_next(Proc *root, Proc *p) {
+ /* depth-first traversal, in this order:
* 1
* / \
* 2 5
* /| |\
* 3 4 6 7
*/
- if (!p) return NULL;
+ assert(root);
+ assert(p);
+
if (p->child) return p->child;
- if (p->sibling) return p->sibling;
/* looking at the diagram above - we're at 4, want to find 5 */
while (!p->sibling) {
+ if (p == root) return NULL;
p = p->parent;
- if (root) assert(p);
- if (!p || p == root) return NULL;
+ assert(p);
}
+ if (p == root) return NULL;
return p->sibling;
}
diff --git a/src/kernel/proc.h b/src/kernel/proc.h
index 2261f85..24206fb 100644
--- a/src/kernel/proc.h
+++ b/src/kernel/proc.h
@@ -128,8 +128,7 @@ void proc_intr(Proc *p, const char *buf, size_t len);
/** Switches execution to any running process. */
_Noreturn void proc_switch_any(void);
-/** Used for iterating over all processes */
-Proc *proc_next(Proc *p, Proc *root);
+Proc *proc_next(Proc *root, Proc *p);
hid_t proc_find_free_handle(Proc *proc, hid_t start_at);
Handle *proc_handle_get(Proc *, hid_t);
diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c
index 5f53ccb..343f45c 100644
--- a/src/kernel/syscalls.c
+++ b/src/kernel/syscalls.c
@@ -367,7 +367,7 @@ int _sys_intr(const char __user *msg, size_t len) {
if (pcpy_from(proc_cur, buf, msg, len) < len) {
SYSCALL_RETURN(-EFAULT);
}
- for (Proc *p = proc_cur->child; p; p = proc_next(p, proc_cur)) {
+ for (Proc *p = proc_cur->child; p; p = proc_next(proc_cur, p)) {
proc_intr(p, buf, len);
count++;
}
diff --git a/src/kernel/vfs/procfs.c b/src/kernel/vfs/procfs.c
index 2e3130d..4bacaf7 100644
--- a/src/kernel/vfs/procfs.c
+++ b/src/kernel/vfs/procfs.c
@@ -82,7 +82,7 @@ openpath(const char *path, size_t len, Proc *root)
static Proc *
findgid(uint32_t gid, Proc *root)
{
- for (Proc *p = root; p; p = proc_next(p, root)) {
+ for (Proc *p = root; p; p = proc_next(root, p)) {
if (p->globalid == gid) return p;
}
return NULL;
@@ -156,7 +156,7 @@ procfs_accept(VfsReq *req)
if (h->type == PhIntr) {
proc_intr(p, buf, len);
} else {
- for (Proc *it = p->child; it; it = proc_next(it, p)) {
+ for (Proc *it = p->child; it; it = proc_next(p, it)) {
proc_intr(it, buf, len);
}
}