From 63f2ae716ee3d8d27200bb723be6ec4b9573747b Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Mon, 2 Oct 2023 19:33:47 -0400 Subject: [PATCH] Add spawn and wait/waitpid functions into libc. --- SConstruct | 4 ++++ bin/false/SConscript | 23 +++++++++++++++++++++++ bin/false/false.c | 7 +++++++ bin/shell/shell.c | 14 ++++++++------ bin/true/SConscript | 23 +++++++++++++++++++++++ bin/true/true.c | 7 +++++++ include/unistd.h | 1 + lib/libc/exit.c | 2 +- lib/libc/process.c | 38 ++++++++++++++++++++++++++++++++++++++ lib/libc/syscall.c | 2 +- release/bootdisk.manifest | 2 ++ sys/include/cdefs.h | 2 ++ sys/include/types.h | 2 ++ sys/kern/process.c | 8 +++----- 14 files changed, 122 insertions(+), 13 deletions(-) create mode 100644 bin/false/SConscript create mode 100644 bin/false/false.c create mode 100644 bin/true/SConscript create mode 100644 bin/true/true.c diff --git a/SConstruct b/SConstruct index 0fc6261..c34a888 100644 --- a/SConstruct +++ b/SConstruct @@ -177,8 +177,10 @@ SConscript('bin/date/SConscript', variant_dir='build/bin/date') SConscript('bin/echo/SConscript', variant_dir='build/bin/echo') SConscript('bin/ethdump/SConscript', variant_dir='build/bin/ethdump') SConscript('bin/ethinject/SConscript', variant_dir='build/bin/ethinject') +SConscript('bin/false/SConscript', variant_dir='build/bin/false') SConscript('bin/ls/SConscript', variant_dir='build/bin/ls') SConscript('bin/shell/SConscript', variant_dir='build/bin/shell') +SConscript('bin/true/SConscript', variant_dir='build/bin/true') SConscript('sbin/ifconfig/SConscript', variant_dir='build/sbin/ifconfig') SConscript('sbin/init/SConscript', variant_dir='build/sbin/init') SConscript('sbin/sysctl/SConscript', variant_dir='build/sbin/sysctl') @@ -207,8 +209,10 @@ if env["BOOTDISK"] == "1": Depends(bootdisk, "#build/bin/echo/echo") Depends(bootdisk, "#build/bin/ethdump/ethdump") Depends(bootdisk, "#build/bin/ethinject/ethinject") + Depends(bootdisk, "#build/bin/false/false") Depends(bootdisk, "#build/bin/ls/ls") Depends(bootdisk, "#build/bin/shell/shell") + Depends(bootdisk, "#build/bin/true/true") Depends(bootdisk, "#build/sbin/ifconfig/ifconfig") Depends(bootdisk, "#build/sbin/init/init") Depends(bootdisk, "#build/sbin/sysctl/sysctl") diff --git a/bin/false/SConscript b/bin/false/SConscript new file mode 100644 index 0000000..1230bb0 --- /dev/null +++ b/bin/false/SConscript @@ -0,0 +1,23 @@ +import sys + +Import('env') + +false_env = env.Clone() + +src = [ ] + +src_common = [ + "false.c" +] + +src.append(env["CRTBEGIN"]) +src.append(src_common) +src.append(env["CRTEND"]) + +false_env.Append(LINKFLAGS = ['-nostdlib']) +false_env.Append(CPPFLAGS = ['-fno-builtin', '-nostdinc']) +false_env.Append(CPPPATH = ['#build/include']) +false_env.Append(LIBPATH = ['#build/lib/libc'], LIBS = ['c']) + +false_env.Program("false", src) + diff --git a/bin/false/false.c b/bin/false/false.c new file mode 100644 index 0000000..6a0df37 --- /dev/null +++ b/bin/false/false.c @@ -0,0 +1,7 @@ + +int +main(int argc, const char *argv[]) +{ + return 1; +} + diff --git a/bin/shell/shell.c b/bin/shell/shell.c index 5902607..5da08ed 100644 --- a/bin/shell/shell.c +++ b/bin/shell/shell.c @@ -3,10 +3,11 @@ #include #include -// Castor Only -#include -#include #include +#include +#include +#include +#include #define SHELL_MAX_ARGS 5 #define SHELL_MAX_LINE 256 @@ -66,12 +67,13 @@ Cmd_Run(int argc, const char *argv[]) } argv[argc] = NULL; - status = OSSpawn(path, &argv[0]); + status = spawn(path, &argv[0]); if (status > 100000) { printf("Spawn failed!\n"); } - status = OSWait(status); - printf("Process result: %d\n", status); + + pid_t pid = wait(&status); + printf("Process result: %d (pid %d)\n", WEXITSTATUS(status), pid); return; } diff --git a/bin/true/SConscript b/bin/true/SConscript new file mode 100644 index 0000000..fa12083 --- /dev/null +++ b/bin/true/SConscript @@ -0,0 +1,23 @@ +import sys + +Import('env') + +true_env = env.Clone() + +src = [ ] + +src_common = [ + "true.c" +] + +src.append(env["CRTBEGIN"]) +src.append(src_common) +src.append(env["CRTEND"]) + +true_env.Append(LINKFLAGS = ['-nostdlib']) +true_env.Append(CPPFLAGS = ['-fno-builtin', '-nostdinc']) +true_env.Append(CPPPATH = ['#build/include']) +true_env.Append(LIBPATH = ['#build/lib/libc'], LIBS = ['c']) + +true_env.Program("true", src) + diff --git a/bin/true/true.c b/bin/true/true.c new file mode 100644 index 0000000..f86b315 --- /dev/null +++ b/bin/true/true.c @@ -0,0 +1,7 @@ + +int +main(int argc, const char *argv[]) +{ + return 0; +} + diff --git a/include/unistd.h b/include/unistd.h index e7d895e..faef9f8 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -10,6 +10,7 @@ struct timeval; int syscall(int number, ...); unsigned int sleep(unsigned int seconds); +pid_t spawn(const char *path, const char *argv[]); #endif /* __UNISTD_H__ */ diff --git a/lib/libc/exit.c b/lib/libc/exit.c index 14d210f..ab4b960 100644 --- a/lib/libc/exit.c +++ b/lib/libc/exit.c @@ -40,7 +40,7 @@ exit(int status) _atexit_last = _atexit_last->next; } - OSExit(status); + OSExit(status & 0x00ff); __builtin_unreachable(); } diff --git a/lib/libc/process.c b/lib/libc/process.c index 67b1f14..9dc9d4e 100644 --- a/lib/libc/process.c +++ b/lib/libc/process.c @@ -1,7 +1,11 @@ #include +#include +#include +#include #include +#include #include unsigned int @@ -13,3 +17,37 @@ sleep(unsigned int seconds) return 0; } +pid_t +spawn(const char *path, const char *argv[]) +{ + uint64_t status = OSSpawn(path, argv); + + if (SYSCALL_ERRCODE(status) != 0) { + errno = SYSCALL_ERRCODE(status); + return -1; + } + + return (pid_t)SYSCALL_VALUE(status); +} + +pid_t +waitpid(pid_t pid, int *status, UNUSED int options) +{ + uint64_t wstatus = OSWait(pid); + + if (SYSCALL_ERRCODE(wstatus) != 0) { + errno = SYSCALL_ERRCODE(wstatus); + return -1; + } + + *status = SYSCALL_VALUE(wstatus) & 0x0000ffff; + + return WGETPID(SYSCALL_VALUE(wstatus)); +} + +pid_t +wait(int *status) +{ + return waitpid(WAIT_ANY, status, 0); +} + diff --git a/lib/libc/syscall.c b/lib/libc/syscall.c index 573161b..fe26d75 100644 --- a/lib/libc/syscall.c +++ b/lib/libc/syscall.c @@ -16,7 +16,7 @@ OSTime() void OSExit(int status) { - syscall(SYSCALL_EXIT); + syscall(SYSCALL_EXIT, status); } uint64_t diff --git a/release/bootdisk.manifest b/release/bootdisk.manifest index bf827bb..d88b416 100644 --- a/release/bootdisk.manifest +++ b/release/bootdisk.manifest @@ -10,8 +10,10 @@ DIR / FILE echo build/bin/echo/echo FILE ethdump build/bin/ethdump/ethdump FILE ethinject build/bin/ethinject/ethinject + FILE false build/bin/false/false FILE ls build/bin/ls/ls FILE shell build/bin/shell/shell + FILE true build/bin/true/true END DIR sbin FILE ifconfig build/sbin/ifconfig/ifconfig diff --git a/sys/include/cdefs.h b/sys/include/cdefs.h index 4236ee3..2574fde 100644 --- a/sys/include/cdefs.h +++ b/sys/include/cdefs.h @@ -13,6 +13,8 @@ #define NO_RETURN __attribute__((noreturn)) #define UNREACHABLE __builtin_unreachable +#define UNUSED __attribute__((unused)) + #define ROUNDUP(_x, _n) (((_x) + (_n) - 1) & ~((_n) - 1)) #define __MALLOC __attribute__((__malloc__)) diff --git a/sys/include/types.h b/sys/include/types.h index 7c2fca0..491c71b 100644 --- a/sys/include/types.h +++ b/sys/include/types.h @@ -25,6 +25,8 @@ typedef uint64_t ino_t; typedef uint64_t time_t; typedef uint64_t suseconds_t; +typedef uint16_t pid_t; + #define NULL ((void *)0) #endif /* __SYS_TYPES_H__ */ diff --git a/sys/kern/process.c b/sys/kern/process.c index 30ac57b..e7292d2 100644 --- a/sys/kern/process.c +++ b/sys/kern/process.c @@ -209,8 +209,6 @@ Process_Wait(Process *proc, uint64_t pid) Process *p = NULL; uint64_t status; - // XXX: Need to verify pid exists! - Mutex_Lock(&proc->zombieProcLock); if (pid == 0) { while (1) { @@ -226,7 +224,7 @@ Process_Wait(Process *proc, uint64_t pid) p = Process_Lookup(pid); if (p == NULL) { Mutex_Unlock(&proc->zombieProcLock); - return ENOENT; + return SYSCALL_PACK(ENOENT, 0); } while (p->procState != PROC_STATE_ZOMBIE) { @@ -242,7 +240,7 @@ Process_Wait(Process *proc, uint64_t pid) } Mutex_Unlock(&proc->zombieProcLock); - status = (p->pid << 16) | p->exitCode; + status = (p->pid << 16) | (p->exitCode & 0xff); // Release threads Spinlock_Lock(&proc->lock); @@ -261,7 +259,7 @@ Process_Wait(Process *proc, uint64_t pid) // Release process Process_Release(p); - return status; + return SYSCALL_PACK(0, status); } /*