From c0a7f6776f0cf99eb8c7948770403ef5b90fb27e Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 11 Mar 2023 22:19:58 +0100 Subject: [PATCH] kernel+libc: Add getpid() --- apps/app.c | 2 +- initrd/etc/motd | 2 +- kernel/CMakeLists.txt | 1 + kernel/src/sys/id.cpp | 7 +++++++ libc/include/unistd.h | 4 +++- libc/src/unistd.cpp | 5 +++++ libluna/include/luna/Syscall.h | 2 +- 7 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 kernel/src/sys/id.cpp diff --git a/apps/app.c b/apps/app.c index eeaaadfc..ea977091 100644 --- a/apps/app.c +++ b/apps/app.c @@ -12,7 +12,7 @@ void bye() int main() { atexit(bye); - printf("Welcome to %s from userspace!\n", "Luna"); + printf("Welcome to %s from userspace (pid %d)!\n", "Luna", getpid()); int fd = open("/etc/motd", 0); if (fd < 0) diff --git a/initrd/etc/motd b/initrd/etc/motd index f222d343..af5626b4 100644 --- a/initrd/etc/motd +++ b/initrd/etc/motd @@ -1 +1 @@ -G'day mate! +Hello, world! diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 6c53ecb3..ffc1b5fe 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -28,6 +28,7 @@ set(SOURCES src/sys/usleep.cpp src/sys/open.cpp src/sys/file.cpp + src/sys/id.cpp src/fs/VFS.cpp src/fs/tmpfs/FileSystem.cpp src/InitRD.cpp diff --git a/kernel/src/sys/id.cpp b/kernel/src/sys/id.cpp new file mode 100644 index 00000000..43dccdca --- /dev/null +++ b/kernel/src/sys/id.cpp @@ -0,0 +1,7 @@ +#include "sys/Syscall.h" +#include "thread/Scheduler.h" + +Result sys_getpid(Registers*, SyscallArgs) +{ + return Scheduler::current()->id; +} diff --git a/libc/include/unistd.h b/libc/include/unistd.h index 6bef9cf3..c35d7030 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -15,7 +15,9 @@ extern "C" #endif pid_t fork(); - pid_t getpid(); + + /* Returns the current process' process ID. */ + pid_t getpid(void); int execv(const char*, char* const*); int execve(const char*, char* const*, char* const*); diff --git a/libc/src/unistd.cpp b/libc/src/unistd.cpp index 02374238..220d429b 100644 --- a/libc/src/unistd.cpp +++ b/libc/src/unistd.cpp @@ -8,6 +8,11 @@ extern "C" long arch_invoke_syscall(long, uintptr_t, uintptr_t, uintptr_t, uintp extern "C" { + pid_t getpid(void) + { + return (pid_t)syscall(SYS_getpid); + } + long syscall(long num, ...) { va_list ap; diff --git a/libluna/include/luna/Syscall.h b/libluna/include/luna/Syscall.h index 1797b548..f6516ecc 100644 --- a/libluna/include/luna/Syscall.h +++ b/libluna/include/luna/Syscall.h @@ -2,7 +2,7 @@ #define enumerate_syscalls(_e) \ _e(exit) _e(console_write) _e(clock_gettime) _e(allocate_memory) _e(deallocate_memory) _e(usleep) _e(open) \ - _e(close) _e(read) + _e(close) _e(read) _e(getpid) enum Syscalls {