diff options
author | dzwdz | 2023-06-10 16:26:46 +0200 |
---|---|---|
committer | dzwdz | 2023-06-10 16:26:46 +0200 |
commit | b09263bd64b3408cbd109dacc4b7618d22b02248 (patch) | |
tree | e461c86fdf229670451f657ef83dd1e2ac802bba /src | |
parent | 112eafe13c3628cad6e692179c064dbbc3be2d8b (diff) |
kernel: implement getpid, getppid
Diffstat (limited to 'src')
-rw-r--r-- | src/kernel/proc.h | 1 | ||||
-rw-r--r-- | src/kernel/syscalls.c | 16 | ||||
-rw-r--r-- | src/shared/include/camellia/syscalls.h | 5 | ||||
-rw-r--r-- | src/user/app/shell/shell.c | 3 | ||||
-rw-r--r-- | src/user/lib/syscall.c | 8 | ||||
-rw-r--r-- | src/user/lib/unistd.c | 4 |
6 files changed, 35 insertions, 2 deletions
diff --git a/src/kernel/proc.h b/src/kernel/proc.h index dce99fb..bf5db69 100644 --- a/src/kernel/proc.h +++ b/src/kernel/proc.h @@ -69,6 +69,7 @@ struct Proc { Handle *procfs; } specialh; + // TODO pids should be 64bit. also typedef pid_t uint32_t globalid; /* only for internal use, don't expose to userland */ uint32_t refcount; /* non-owning. should always be 0 on kill */ bool noreap; diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index 589098b..03d9ef1 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -388,6 +388,20 @@ void _sys_intr_set(void __user *ip) { proc_cur->intr_fn = ip; } +uint32_t _sys_getpid(void) { + SYSCALL_RETURN(proc_ns_id(proc_cur->pns, proc_cur)); +} + +uint32_t _sys_getppid(void) { + if (proc_cur->pns == proc_cur) { + SYSCALL_RETURN(0); + } else { + assert(proc_cur->parent); + assert(proc_cur->parent->pns == proc_cur->pns); + SYSCALL_RETURN(proc_ns_id(proc_cur->pns, proc_cur->parent)); + } +} + long _sys_execbuf(void __user *ubuf, size_t len) { if (len == 0) SYSCALL_RETURN(0); if (len > EXECBUF_MAX_LEN) @@ -440,6 +454,8 @@ long _syscall(long num, long a, long b, long c, long d, long e) { break; case _SYS_FILICIDE: _sys_filicide(); break; case _SYS_INTR: _sys_intr(); break; case _SYS_INTR_SET: _sys_intr_set((userptr_t)a); + break; case _SYS_GETPID: _sys_getpid(); + break; case _SYS_GETPPID: _sys_getppid(); break; case _SYS_EXECBUF: _sys_execbuf((userptr_t)a, b); break; case _SYS_DEBUG_KLOG: _sys_debug_klog((userptr_t)a, b); break; diff --git a/src/shared/include/camellia/syscalls.h b/src/shared/include/camellia/syscalls.h index 5d63223..809cad7 100644 --- a/src/shared/include/camellia/syscalls.h +++ b/src/shared/include/camellia/syscalls.h @@ -19,6 +19,8 @@ #define _SYS_FILICIDE 16 #define _SYS_INTR 17 #define _SYS_INTR_SET 18 +#define _SYS_GETPID 19 +#define _SYS_GETPPID 20 #define _SYS_EXECBUF 100 #define _SYS_DEBUG_KLOG 101 @@ -79,6 +81,9 @@ void _sys_filicide(void); void _sys_intr(void); void _sys_intr_set(void __user *ip); +uint32_t _sys_getpid(void); +uint32_t _sys_getppid(void); + /* see shared/execbuf.h */ long _sys_execbuf(void __user *buf, size_t len); diff --git a/src/user/app/shell/shell.c b/src/user/app/shell/shell.c index becc8d8..8e13029 100644 --- a/src/user/app/shell/shell.c +++ b/src/user/app/shell/shell.c @@ -80,6 +80,9 @@ void run_args(int argc, char **argv, struct redir *redir) { return; } else if (!strcmp(argv[0], "exit")) { exit(0); + } else if (!strcmp(argv[0], "getpid")) { + printf("my\t%d\nparent\t%d\n", getpid(), getppid()); + return; } if (fork()) { diff --git a/src/user/lib/syscall.c b/src/user/lib/syscall.c index f7eaddb..3e8473c 100644 --- a/src/user/lib/syscall.c +++ b/src/user/lib/syscall.c @@ -82,6 +82,14 @@ void _sys_intr_set(void __user *ip) { return (void)_syscall(_SYS_INTR_SET, (long)ip, 0, 0, 0, 0); } +uint32_t _sys_getpid(void) { + return (uint32_t)_syscall(_SYS_GETPID, 0, 0, 0, 0, 0); +} + +uint32_t _sys_getppid(void) { + return (uint32_t)_syscall(_SYS_GETPPID, 0, 0, 0, 0, 0); +} + long _sys_execbuf(void __user *buf, size_t len) { return _syscall(_SYS_EXECBUF, (long)buf, (long)len, 0, 0, 0); } diff --git a/src/user/lib/unistd.c b/src/user/lib/unistd.c index 28164e6..d6f24f8 100644 --- a/src/user/lib/unistd.c +++ b/src/user/lib/unistd.c @@ -185,11 +185,11 @@ pid_t getpgrp(void) { } pid_t getpid(void) { - __libc_panic("unimplemented"); + return _sys_getpid(); } pid_t getppid(void) { - __libc_panic("unimplemented"); + return _sys_getppid(); } int getgroups(int size, gid_t list[]) { |