summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordzwdz2022-09-02 14:44:52 +0200
committerdzwdz2022-09-02 17:58:47 +0200
commitc16d956c0dd3d69748f7208e09bb69a6df8c6483 (patch)
tree38089f4b9e7fb153aa817e25340b7107f7cdf795 /src
parent41a15bd16806261d66d55bbb85034553cd58361e (diff)
kernel/proc: introduce child ids for telling children apart
Diffstat (limited to 'src')
-rw-r--r--src/kernel/proc.c10
-rw-r--r--src/kernel/proc.h4
-rw-r--r--src/kernel/syscalls.c15
-rw-r--r--src/shared/include/camellia/syscalls.h2
-rw-r--r--src/user/app/init/init.c4
5 files changed, 23 insertions, 12 deletions
diff --git a/src/kernel/proc.c b/src/kernel/proc.c
index 71d1f44..85992d5 100644
--- a/src/kernel/proc.c
+++ b/src/kernel/proc.c
@@ -26,7 +26,9 @@ struct process *process_seed(void *data, size_t datalen) {
process_first->state = PS_RUNNING;
process_first->pages = pagedir_new();
process_first->mount = vfs_mount_seed();
- process_first->id = next_pid++;
+ process_first->globalid = next_pid++;
+ process_first->cid = 1;
+ process_first->nextcid = 1;
process_first->_handles = kzalloc(sizeof(struct handle) * HANDLE_MAX);
// map .shared
@@ -68,7 +70,11 @@ struct process *process_fork(struct process *parent, int flags) {
child->parent = parent;
parent->child = child;
- child->id = next_pid++;
+ if (parent->nextcid == 0)
+ panic_unimplemented();
+ child->cid = parent->nextcid++;
+ child->nextcid = 1;
+ child->globalid = next_pid++;
if ((flags & FORK_NEWFS) == 0 && parent->controlled) {
child->controlled = parent->controlled;
diff --git a/src/kernel/proc.h b/src/kernel/proc.h
index 780c8ee..0ae811e 100644
--- a/src/kernel/proc.h
+++ b/src/kernel/proc.h
@@ -51,7 +51,9 @@ struct process {
struct handle **_handles; /* points to struct handle *[HANDLE_MAX] */
uint64_t *handles_refcount; /* works just like pages_refcount */
- uint32_t id; /* only for debugging, don't expose to userland */
+ uint32_t cid; /* child id. unique amongst all of this process' siblings */
+ uint32_t nextcid; /* the child id to assign to the next spawned child */
+ uint32_t globalid; /* only for debugging, don't expose to userland */
bool noreap;
/* allocated once, the requests from WAITS4FS get stored here */
diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c
index 37a0ce3..62acf5f 100644
--- a/src/kernel/syscalls.c
+++ b/src/kernel/syscalls.c
@@ -76,7 +76,7 @@ long _syscall_fork(int flags, handle_t __user *fs_front) {
virt_cpy_to(process_current->pages, fs_front, &hid, sizeof hid);
}
}
- SYSCALL_RETURN(1);
+ SYSCALL_RETURN(child->cid);
}
handle_t _syscall_open(const char __user *path, long len, int flags) {
@@ -379,12 +379,13 @@ long _syscall_execbuf(void __user *ubuf, size_t len) {
}
void _syscall_debug_klog(const void __user *buf, size_t len) {
- (void)buf; (void)len;
- // static char kbuf[256];
- // if (len >= sizeof(kbuf)) len = sizeof(kbuf) - 1;
- // virt_cpy_from(process_current->pages, kbuf, buf, len);
- // kbuf[len] = '\0';
- // kprintf("[klog] %x\t%s\n", process_current->id, kbuf);
+ if (false) {
+ static char kbuf[256];
+ if (len >= sizeof(kbuf)) len = sizeof(kbuf) - 1;
+ virt_cpy_from(process_current->pages, kbuf, buf, len);
+ kbuf[len] = '\0';
+ kprintf("[klog] %x\t%s\n", process_current->globalid, kbuf);
+ }
}
long _syscall(long num, long a, long b, long c, long d, long e) {
diff --git a/src/shared/include/camellia/syscalls.h b/src/shared/include/camellia/syscalls.h
index 634583b..2450fff 100644
--- a/src/shared/include/camellia/syscalls.h
+++ b/src/shared/include/camellia/syscalls.h
@@ -47,7 +47,7 @@ long _syscall_await(void);
* @param flags FORK_NOREAP, FORK_NEWFS
* @param fs_front requires FORK_NEWFS. the front handle to the new fs is put there
*
- * @return 0 in the child, a meaningless positive value in the parent.
+ * @return 0 in the child, the CID in the parent.
*/
long _syscall_fork(int flags, handle_t __user *fs_front);
diff --git a/src/user/app/init/init.c b/src/user/app/init/init.c
index b97b483..04380a6 100644
--- a/src/user/app/init/init.c
+++ b/src/user/app/init/init.c
@@ -83,9 +83,11 @@ int main(void) {
}
close(killswitch_pipe[1]);
- if (fork()) {
+ long cid = fork();
+ if (cid > 0) {
/* used to trigger a kernel bug
* 7c96f9c03502e0c60f23f4c550d12a629f3b3daf */
+ printf("main at cid %u\n", cid);
_syscall_await();
exit(1);
}