From c34a253d1194bdea8b30b86b4de0f64cd609543f Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Tue, 14 Oct 2025 16:37:36 +0200 Subject: [PATCH] Split processctl() syscall into multiple smaller ones --- kernel/proc/proc.c | 2 +- kernel/proc/proc.h | 2 +- kernel/syscall/proc.c | 237 ++++++++++++++++++++++++++++++++++++ kernel/syscall/proc.h | 18 +++ kernel/syscall/processctl.c | 160 ------------------------ kernel/syscall/processctl.h | 9 -- kernel/syscall/syscall.c | 12 +- share/sysdefs/proc.h | 15 +++ share/sysdefs/processctl.h | 29 ----- share/sysdefs/syscall.h | 10 +- ulib/_premain.c | 5 +- ulib/_start.S | 6 - ulib/system/system.c | 43 ++++++- ulib/system/system.h | 12 +- ulib/ulib.h | 2 +- ulib/util/util.c | 3 +- user/init/main.c | 8 +- user/pctl/kill.c | 2 +- user/pctl/ls.c | 4 +- user/tb/interp.c | 8 +- user/tb/main.c | 2 +- user/tb/runtime.c | 6 +- 22 files changed, 359 insertions(+), 236 deletions(-) create mode 100644 kernel/syscall/proc.c create mode 100644 kernel/syscall/proc.h delete mode 100644 kernel/syscall/processctl.c delete mode 100644 kernel/syscall/processctl.h create mode 100644 share/sysdefs/proc.h delete mode 100644 share/sysdefs/processctl.h diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index 1259191..94def34 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -13,7 +13,7 @@ #include "vfs/vfs.h" #include "bootinfo/bootinfo.h" #include "ipc/pipe/pipe.h" -#include "sysdefs/processctl.h" +#include "sysdefs/proc.h" #include "sysdefs/ioctl.h" #define PROC_REAPER_FREQ 30 diff --git a/kernel/proc/proc.h b/kernel/proc/proc.h index 6a9cb23..b3ded6d 100644 --- a/kernel/proc/proc.h +++ b/kernel/proc/proc.h @@ -7,7 +7,7 @@ #include "bitmap/bitmap.h" #include "vfs/vfs.h" #include "ipc/pipe/pipe.h" -#include "sysdefs/processctl.h" +#include "sysdefs/proc.h" #include "dev/dev.h" #define PROC_NAME_MAX 0x100 diff --git a/kernel/syscall/proc.c b/kernel/syscall/proc.c new file mode 100644 index 0000000..e29cfdc --- /dev/null +++ b/kernel/syscall/proc.c @@ -0,0 +1,237 @@ +#include +#include +#include "syscall.h" +#include "proc/proc.h" +#include "spinlock/spinlock.h" +#include "errors.h" +#include "util/util.h" +#include "sysdefs/proc.h" +#include "vfs/vfs.h" +#include "path/path.h" +#include "kprintf.h" +#include "dlmalloc/malloc.h" +#include "ipc/pipe/pipe.h" + +#define _MP_MAX 0xff +#define _PATH_MAX VFS_PATH_MAX + +int32_t SYSCALL1(sys_proc_kill, pid1) { + uint64_t pid = pid1; + int32_t ret = E_OK; + + if (pid == (uint64_t)-1) { + pid = PROCS.current->pid; + } + + spinlock_acquire(&PROCS.spinlock); + Proc *proc = NULL; + LL_FINDPROP(PROCS.procs, proc, pid, pid); + spinlock_release(&PROCS.spinlock); + + proc_kill(proc); + ret = E_DOSCHEDULING; + + return ret; +} + +int32_t SYSCALL3(sys_proc_spawn, opath1, args1, argslen1) { + int32_t ret = E_OK; + + spinlock_acquire(&PROCS.spinlock); + Proc *proc = PROCS.current; + spinlock_release(&PROCS.spinlock); + + const char *opath = (const char *)opath1; + + if (opath == NULL) { + ret = E_INVALIDARGUMENT; + goto done; + } + + char mp[_MP_MAX]; + char path[_PATH_MAX]; + + path_parse(opath, mp, path); + + Proc *newproc = proc_spawnuser(mp, path); + if (newproc == NULL) { + ret = E_SPAWNERROR; + goto done; + } + + size_t argslen = argslen1; + char **args = (char **)args1; + if (args != NULL && argslen > 0) { + for (size_t i = 0; i < argslen; i++) { + PROC_ARG(newproc, args[i]); + } + } + + for (size_t i = 0; i < PROC_PIPEHANDLES_MAX; i++) { + if (newproc->pipes[i] != NULL) { + ipc_pipefree(newproc->pipes[i]); + dlfree(newproc->pipes[i]); + } + newproc->pipes[i] = proc->pipes[i]; + } + + proc_register(newproc); + ret = newproc->pid; + +done: + return ret; +} + +int32_t SYSCALL1(sys_proc_pollstate, pid1) { + uint64_t pid = pid1; + int32_t ret = E_OK; + + if (pid == (uint64_t)-1) { + pid = PROCS.current->pid; + } + + spinlock_acquire(&PROCS.spinlock); + Proc *proc = NULL; + LL_FINDPROP(PROCS.procs, proc, pid, pid); + spinlock_release(&PROCS.spinlock); + + if (proc == NULL) { + ret = PROC_DIED; + goto done; + } + + ret = proc->state; + +done: + return ret; +} + +int32_t SYSCALL0(sys_proc_getpid) { + int32_t ret = E_OK; + + spinlock_acquire(&PROCS.spinlock); + Proc *proc = PROCS.current; + spinlock_release(&PROCS.spinlock); + + ret = proc->pid; + + return ret; +} + +int32_t SYSCALL1(sys_proc_run, pid1) { + uint64_t pid = pid1; + int32_t ret = E_OK; + + spinlock_acquire(&PROCS.spinlock); + Proc *proc = NULL; + LL_FINDPROP(PROCS.procs, proc, pid, pid); + spinlock_release(&PROCS.spinlock); + + if (proc == NULL) { + ret = E_INVALIDARGUMENT; + goto done; + } + + proc->state = PROC_READY; + +done: + return ret; +} + +int32_t SYSCALL1(sys_proc_arglen, pid1) { + uint64_t pid = pid1; + int32_t ret = E_OK; + + if (pid == (uint64_t)-1) { + pid = PROCS.current->pid; + } + + spinlock_acquire(&PROCS.spinlock); + Proc *proc = NULL; + LL_FINDPROP(PROCS.procs, proc, pid, pid); + spinlock_release(&PROCS.spinlock); + + ret = proc->procargs.len; + return ret; +} + +int32_t SYSCALL4(sys_proc_argv, pid1, argslen1, argbuf1, maxargs1) { + uint64_t pid = pid1; + int32_t ret = E_OK; + + if (pid == (uint64_t)-1) { + pid = PROCS.current->pid; + } + + spinlock_acquire(&PROCS.spinlock); + Proc *proc = NULL; + LL_FINDPROP(PROCS.procs, proc, pid, pid); + spinlock_release(&PROCS.spinlock); + + size_t *argslen = (size_t *)argslen1; + char **argbuf = (char **)argbuf1; + if (argbuf == NULL) { + ret = E_INVALIDARGUMENT; + goto done; + } + size_t maxargs = (size_t)maxargs1; + + ProcArg *arg, *argtmp; + size_t i; + LL_FOREACH_SAFE_IDX_LIMIT(proc->procargs.list, arg, argtmp, i, maxargs) { + if (argbuf[i] == NULL) { + ret = E_INVALIDARGUMENT; + goto done; + } + hal_strcpy(argbuf[i], arg->string); + } + *argslen = i; + + ret = E_OK; + +done: + return ret; +} + +int32_t SYSCALL0(sys_proc_listsize) { + int32_t ret; + Proc *p, *ptmp; + size_t i; + spinlock_acquire(&PROCS.spinlock); + LL_FOREACH_SAFE_IDX(PROCS.procs, p, ptmp, i); + spinlock_release(&PROCS.spinlock); + ret = i; + return ret; +} + +int32_t SYSCALL2(sys_proc_stat, pidx, pstat1) { + int32_t ret; + ProcStat *stat = (ProcStat *)pstat1; + + if (stat == NULL) { + ret = E_INVALIDARGUMENT; + goto done; + } + + Proc *p, *ptmp; + size_t i; + spinlock_acquire(&PROCS.spinlock); + LL_FOREACH_SAFE_IDX(PROCS.procs, p, ptmp, i) { + if (i == pidx) { + stat->pid = p->pid; + hal_strcpy(stat->name, p->name); + stat->state = p->state; + + VasRange *vas, *vastmp; + LL_FOREACH_SAFE(p->vas, vas, vastmp) { + stat->usemem += vas->size; + } + break; + } + } + spinlock_release(&PROCS.spinlock); + ret = E_OK; + +done: + return ret; +} diff --git a/kernel/syscall/proc.h b/kernel/syscall/proc.h new file mode 100644 index 0000000..3b3513d --- /dev/null +++ b/kernel/syscall/proc.h @@ -0,0 +1,18 @@ +#ifndef SYSCALL_PROC_H_ +#define SYSCALL_PROC_H_ + +#include +#include +#include "syscall.h" + +int32_t SYSCALL1(sys_proc_kill, pid1); +int32_t SYSCALL3(sys_proc_spawn, opath1, args1, argslen1); +int32_t SYSCALL1(sys_proc_pollstate, pid1); +int32_t SYSCALL0(sys_proc_getpid); +int32_t SYSCALL1(sys_proc_run, pid1); +int32_t SYSCALL1(sys_proc_arglen, pid1); +int32_t SYSCALL4(sys_proc_argv, pid1, argslen1, argbuf1, maxargs1); +int32_t SYSCALL0(sys_proc_listsize); +int32_t SYSCALL2(sys_proc_stat, pidx, pstat1); + +#endif // SYSCALL_PROC_H_ diff --git a/kernel/syscall/processctl.c b/kernel/syscall/processctl.c deleted file mode 100644 index 81b5db3..0000000 --- a/kernel/syscall/processctl.c +++ /dev/null @@ -1,160 +0,0 @@ -#include -#include "syscall.h" -#include "proc/proc.h" -#include "spinlock/spinlock.h" -#include "errors.h" -#include "util/util.h" -#include "sysdefs/processctl.h" -#include "vfs/vfs.h" -#include "path/path.h" -#include "kprintf.h" -#include "dlmalloc/malloc.h" -#include "ipc/pipe/pipe.h" - -#define PCTL_MP_MAX 0xff -#define PCTL_PATH_MAX VFS_PATH_MAX - -int32_t SYSCALL5(sys_processctl, pid1, cmd1, arg1, arg2, arg3) { - uint64_t pid = pid1; - uint64_t cmd = cmd1; - int32_t ret = E_OK; - - if (pid == (uint64_t)-1) { - pid = PROCS.current->pid; - } - - spinlock_acquire(&PROCS.spinlock); - Proc *proc = NULL; - LL_FINDPROP(PROCS.procs, proc, pid, pid); - spinlock_release(&PROCS.spinlock); - - if (proc == NULL) { - if (cmd == PCTL_POLLSTATE) { - ret = PROC_DIED; - goto done; - } - ret = E_INVALIDARGUMENT; - goto done; - } - - switch (cmd) { - case PCTL_KILL: { - proc_kill(proc); - ret = E_DOSCHEDULING; - } break; - case PCTL_SPAWN: { - const char *opath = (const char *)arg1; - - if (opath == NULL) { - ret = E_INVALIDARGUMENT; - goto done; - } - - char mp[PCTL_MP_MAX]; - char path[PCTL_PATH_MAX]; - - path_parse(opath, mp, path); - - Proc *newproc = proc_spawnuser(mp, path); - if (newproc == NULL) { - ret = E_SPAWNERROR; - goto done; - } - - size_t argslen = arg3; - char **args = (char **)arg2; - if (args != NULL && argslen > 0) { - for (size_t i = 0; i < argslen; i++) { - PROC_ARG(newproc, args[i]); - } - } - - for (size_t i = 0; i < PROC_PIPEHANDLES_MAX; i++) { - if (newproc->pipes[i] != NULL) { - ipc_pipefree(newproc->pipes[i]); - dlfree(newproc->pipes[i]); - } - newproc->pipes[i] = proc->pipes[i]; - } - - proc_register(newproc); - ret = newproc->pid; - } break; - case PCTL_POLLSTATE: { - ret = proc->state; - } break; - case PCTL_RUN: { - proc->state = PROC_READY; - } break; - case PCTL_GETPID: { - ret = proc->pid; - } break; - case PCTL_ARGLEN: { - ret = proc->procargs.len; - } break; - case PCTL_ARGV: { - size_t *argslen = (size_t *)arg1; - char **argbuf = (char **)arg2; - if (argbuf == NULL) { - ret = E_INVALIDARGUMENT; - goto done; - } - size_t maxargs = (size_t)arg3; - - ProcArg *arg, *argtmp; - size_t i; - LL_FOREACH_SAFE_IDX_LIMIT(proc->procargs.list, arg, argtmp, i, maxargs) { - if (argbuf[i] == NULL) { - ret = E_INVALIDARGUMENT; - goto done; - } - hal_strcpy(argbuf[i], arg->string); - } - *argslen = i; - - ret = E_OK; - } break; - case PCTL_PLS_SZ: { - Proc *p, *ptmp; - size_t i; - spinlock_acquire(&PROCS.spinlock); - LL_FOREACH_SAFE_IDX(PROCS.procs, p, ptmp, i); - spinlock_release(&PROCS.spinlock); - ret = i; - } break; - case PCTL_PLS_STAT: { - uint64_t pidx = arg1; - ProcStat *stat = (ProcStat *)arg2; - - if (stat == NULL) { - ret = E_INVALIDARGUMENT; - goto done; - } - - Proc *p, *ptmp; - size_t i; - spinlock_acquire(&PROCS.spinlock); - LL_FOREACH_SAFE_IDX(PROCS.procs, p, ptmp, i) { - if (i == pidx) { - stat->pid = p->pid; - hal_strcpy(stat->name, p->name); - stat->state = p->state; - - VasRange *vas, *vastmp; - LL_FOREACH_SAFE(p->vas, vas, vastmp) { - stat->usemem += vas->size; - } - break; - } - } - spinlock_release(&PROCS.spinlock); - ret = E_OK; - } break; - default: { - ret = E_INVALIDARGUMENT; - } break; - } - -done: - return ret; -} diff --git a/kernel/syscall/processctl.h b/kernel/syscall/processctl.h deleted file mode 100644 index 2023d2f..0000000 --- a/kernel/syscall/processctl.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef SYSCALL_PROCESSCTL_H_ -#define SYSCALL_PROCESSCTL_H_ - -#include -#include "syscall.h" - -int32_t SYSCALL5(sys_processctl, pid1, cmd1, arg1, arg2, arg3); - -#endif // SYSCALL_PROCESSCTL_H_ diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index 8494092..47c58e8 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -2,7 +2,6 @@ #include "syscall.h" #include "errors.h" #include "kprintf.h" -#include "processctl.h" #include "sysdefs/syscall.h" #include "ioctl.h" #include "ipcpipe.h" @@ -11,6 +10,7 @@ #include "devctl.h" #include "randcrypto.h" #include "vfs.h" +#include "proc.h" int32_t SYSCALL1(sys_debugprint, string) { char *p = (char *)string; @@ -20,7 +20,6 @@ int32_t SYSCALL1(sys_debugprint, string) { SyscallFn SYSCALL_TABLE[SYSCALLS_MAX] = { [SYS_DEBUGPRINT] = &sys_debugprint, - [SYS_PROCESSCTL] = &sys_processctl, [SYS_IOCTL] = &sys_ioctl, [SYS_MMAN_MAP] = &sys_mman_map, [SYS_MMAN_UNMAP] = &sys_mman_unmap, @@ -34,4 +33,13 @@ SyscallFn SYSCALL_TABLE[SYSCALLS_MAX] = { [SYS_IPC_PIPEMAKE] = &sys_ipc_pipemake, [SYS_IPC_PIPEDELETE] = &sys_ipc_pipedelete, [SYS_IPC_PIPECONNECT] = &sys_ipc_pipeconnect, + [SYS_PROC_KILL] = &sys_proc_kill, + [SYS_PROC_SPAWN] = &sys_proc_spawn, + [SYS_PROC_POLLSTATE] = &sys_proc_pollstate, + [SYS_PROC_GETPID] = &sys_proc_getpid, + [SYS_PROC_RUN] = &sys_proc_run, + [SYS_PROC_ARGLEN] = &sys_proc_arglen, + [SYS_PROC_ARGV] = &sys_proc_argv, + [SYS_PROC_LISTSIZE] = &sys_proc_listsize, + [SYS_PROC_STAT] = &sys_proc_stat, }; diff --git a/share/sysdefs/proc.h b/share/sysdefs/proc.h new file mode 100644 index 0000000..85782a9 --- /dev/null +++ b/share/sysdefs/proc.h @@ -0,0 +1,15 @@ +#ifndef SHARE_SYSDEFS_PROC_H_ +#define SHARE_SYSDEFS_PROC_H_ + +#define PROC_ARG_MAX 128 + +typedef struct { + uint64_t pid; + char name[0x100]; + uint8_t state; + size_t usemem; +} ProcStat; + +typedef uint64_t PID_t; + +#endif // SHARE_SYSDEFS_PROC_H_ diff --git a/share/sysdefs/processctl.h b/share/sysdefs/processctl.h deleted file mode 100644 index 8abe4c1..0000000 --- a/share/sysdefs/processctl.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef SHARE_HDRS_PROCESSCTL_H_ -#define SHARE_HDRS_PROCESSCTL_H_ - -#define PROC_ARG_MAX 128 - -#define PCTL_KILL 0 -#define PCTL_SPAWN 1 -#define PCTL_POLLSTATE 2 -#define PCTL_RUN 3 -#define PCTL_GETPID 4 -#define PCTL_ARGLEN 5 -#define PCTL_ARGV 6 -#define PCTL_PLS_SZ 7 -#define PCTL_PLS_STAT 8 - -#if !defined(__ASSEMBLER__) - -typedef struct { - uint64_t pid; - char name[0x100]; - uint8_t state; - size_t usemem; -} ProcStat; - -typedef uint64_t PID_t; - -#endif - -#endif // SHARE_HDRS_PROCESSCTL_H_ diff --git a/share/sysdefs/syscall.h b/share/sysdefs/syscall.h index 08d23fe..e2a5bbc 100644 --- a/share/sysdefs/syscall.h +++ b/share/sysdefs/syscall.h @@ -2,7 +2,6 @@ #define SHARE_HDRS_SYSCALL_H_ #define SYS_DEBUGPRINT 1 -#define SYS_PROCESSCTL 2 #define SYS_IOCTL 3 #define SYS_MMAN_MAP 5 #define SYS_MMAN_UNMAP 6 @@ -16,6 +15,15 @@ #define SYS_IPC_PIPEMAKE 14 #define SYS_IPC_PIPEDELETE 15 #define SYS_IPC_PIPECONNECT 16 +#define SYS_PROC_KILL 17 +#define SYS_PROC_SPAWN 18 +#define SYS_PROC_POLLSTATE 19 +#define SYS_PROC_RUN 20 +#define SYS_PROC_GETPID 21 +#define SYS_PROC_ARGLEN 22 +#define SYS_PROC_ARGV 23 +#define SYS_PROC_LISTSIZE 24 +#define SYS_PROC_STAT 25 #endif // SHARE_HDRS_SYSCALL_H_ diff --git a/ulib/_premain.c b/ulib/_premain.c index 2bd81e4..9e16503 100644 --- a/ulib/_premain.c +++ b/ulib/_premain.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include @@ -40,8 +40,9 @@ void _premain(void) { _args[i] = umalloc(PROC_ARG_MAX); } - processctl(-1, PCTL_ARGV, (uint64_t)&_argslen, (uint64_t)_args, MAX_ARGS); + proc_argv(-1, &_argslen, _args, MAX_ARGS); main(); + proc_kill(proc_getpid()); } diff --git a/ulib/_start.S b/ulib/_start.S index ade99f6..2a214d8 100644 --- a/ulib/_start.S +++ b/ulib/_start.S @@ -5,9 +5,3 @@ .global _start _start: call _premain - - movq $2, %rax // sys processctl - movq $-1, %rdi // self magic num - movq $0, %rsi // kill cmd - - int $0x80 diff --git a/ulib/system/system.c b/ulib/system/system.c index a534ed4..5df371c 100644 --- a/ulib/system/system.c +++ b/ulib/system/system.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include @@ -15,10 +15,6 @@ int32_t ioctl(uint64_t ioh, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t return syscall(SYS_IOCTL, ioh, cmd, arg1, arg2, arg3, 0); } -int32_t processctl(uint64_t pid, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3) { - return syscall(SYS_PROCESSCTL, pid, cmd, arg1, arg2, arg3, 0); -} - int32_t mman_map(uint8_t *addr, size_t size, uint64_t prot, uint64_t flags, uint8_t **out) { return syscall(SYS_MMAN_MAP, (uint64_t)addr, (uint64_t)size, prot, flags, (uint64_t)out, 0); } @@ -66,3 +62,40 @@ int32_t ipc_pipedelete(uint64_t pipenum) { int32_t ipc_pipeconnect(PID_t pid1, uint64_t pipenum1, PID_t pid2, uint64_t pipenum2) { return syscall(SYS_IPC_PIPECONNECT, pid1, pipenum1, pid2, pipenum2, 0, 0); } + +int32_t proc_kill(PID_t pid) { + return syscall(SYS_PROC_KILL, (uint64_t)pid, 0, 0, 0, 0, 0); +} + +int32_t proc_spawn(char *path, char **args1, size_t argslen1) { + return syscall(SYS_PROC_SPAWN, (uint64_t)path, (uint64_t)args1, (uint64_t)argslen1, 0, 0, 0); +} + +int32_t proc_pollstate(PID_t pid) { + return syscall(SYS_PROC_POLLSTATE, (uint64_t)pid, 0, 0, 0, 0, 0); +} + +int32_t proc_getpid(void) { + return syscall(SYS_PROC_GETPID, 0, 0, 0, 0, 0, 0); +} + +int32_t proc_run(PID_t pid) { + return syscall(SYS_PROC_RUN, (uint64_t)pid, 0, 0, 0, 0, 0); +} + +int32_t proc_arglen(PID_t pid) { + return syscall(SYS_PROC_ARGLEN, (uint64_t)pid, 0, 0, 0, 0, 0); +} + +int32_t proc_argv(PID_t pid, size_t *argslen1, char **argbuf1, size_t maxargs) { + return syscall(SYS_PROC_ARGV, (uint64_t)pid, (uint64_t)argslen1, (uint64_t)argbuf1, (uint64_t)maxargs, 0, 0); +} + +int32_t proc_listsize(void) { + return syscall(SYS_PROC_LISTSIZE, 0, 0, 0, 0, 0, 0); +} + +int32_t proc_stat(size_t idx, ProcStat *pstat) { + return syscall(SYS_PROC_STAT, (uint64_t)idx, (uint64_t)pstat, 0, 0, 0, 0); +} + diff --git a/ulib/system/system.h b/ulib/system/system.h index df84091..bebdde5 100644 --- a/ulib/system/system.h +++ b/ulib/system/system.h @@ -5,11 +5,10 @@ #include #include #include -#include +#include void debugprint(const char *string); int32_t ioctl(uint64_t ioh, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3); -int32_t processctl(uint64_t pid, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3); int32_t mman_map(uint8_t *addr, size_t size, uint64_t prot, uint64_t flags, uint8_t **out); int32_t mman_unmap(uint8_t *addr); int32_t schedrelease(void); @@ -22,5 +21,14 @@ int32_t ipc_pipewrite(PID_t pid, uint64_t pipenum, const uint8_t *buffer, size_t int32_t ipc_pipemake(uint64_t pipenum); int32_t ipc_pipedelete(uint64_t pipenum); int32_t ipc_pipeconnect(PID_t pid1, uint64_t pipenum1, PID_t pid2, uint64_t pipenum2); +int32_t proc_kill(PID_t pid); +int32_t proc_spawn(char *path, char **args1, size_t argslen1); +int32_t proc_pollstate(PID_t pid); +int32_t proc_getpid(void); +int32_t proc_run(PID_t pid); +int32_t proc_arglen(PID_t pid); +int32_t proc_argv(PID_t pid, size_t *argslen1, char **argbuf1, size_t maxargs); +int32_t proc_listsize(void); +int32_t proc_stat(size_t idx, ProcStat *pstat); #endif // ULIB_SYSTEM_SYSTEM_H_ diff --git a/ulib/ulib.h b/ulib/ulib.h index 4436a79..4e1b8db 100644 --- a/ulib/ulib.h +++ b/ulib/ulib.h @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include diff --git a/ulib/util/util.c b/ulib/util/util.c index 1a21dda..5e47ad7 100644 --- a/ulib/util/util.c +++ b/ulib/util/util.c @@ -1,6 +1,5 @@ #include -#include void quit(void) { - processctl(-1, PCTL_KILL, 0, 0, 0); + proc_kill(proc_getpid()); } diff --git a/user/init/main.c b/user/init/main.c index 0f20d07..021ed2b 100644 --- a/user/init/main.c +++ b/user/init/main.c @@ -9,11 +9,11 @@ void tb_runinitscript(void) { devctl(&termdev, DEVCTL_GET_HANDLE, (uint8_t *)"termdev", 0, 0); char *tbargs[] = { "-m", "runfile", "-f", "base:/scripts/init.tb" }; - int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb", (uint64_t)(char **)tbargs, ARRLEN(tbargs)); + int32_t tb = proc_spawn("base:/bin/tb", tbargs, ARRLEN(tbargs)); - processctl(tb, PCTL_RUN, 0, 0, 0); + proc_run(tb); - while(processctl(tb, PCTL_POLLSTATE, 0, 0, 0) != 4) { + while(proc_pollstate(tb) != 4) { int32_t r; char buf[100]; @@ -28,7 +28,7 @@ void tb_runinitscript(void) { } void main(void) { - PID = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0); + PID = proc_getpid(); devctl(&ps2kbdev, DEVCTL_GET_HANDLE, (uint8_t *)"ps2kbdev", 0, 0); devctl(&ps2kbdev, DEV_PS2KBDEV_ATTCHCONS, (uint8_t *)PID, 0, 0); diff --git a/user/pctl/kill.c b/user/pctl/kill.c index 954e6a6..13d4938 100644 --- a/user/pctl/kill.c +++ b/user/pctl/kill.c @@ -22,6 +22,6 @@ void pctl_kill(void) { } if (PCTL_KILL_CONFIG.pid != -1) { - processctl(PCTL_KILL_CONFIG.pid, PCTL_KILL, 0, 0, 0); + proc_kill(PCTL_KILL_CONFIG.pid); } } diff --git a/user/pctl/ls.c b/user/pctl/ls.c index 6de3281..9ea50ad 100644 --- a/user/pctl/ls.c +++ b/user/pctl/ls.c @@ -26,7 +26,7 @@ void pctl_ls(void) { static const char *states[] = {"embryo", "ready", "zombie", "waiting", "died"}; - uint64_t procslen = processctl(-1, PCTL_PLS_SZ, 0, 0, 0); + int32_t procslen = proc_listsize(); char *namebuf = umalloc(34); char *membuf = umalloc(20); @@ -38,7 +38,7 @@ void pctl_ls(void) { string_memset(namebuf, 0, 34); string_memset(membuf, 0, 20); - int32_t r = processctl(-1, PCTL_PLS_STAT, i, (uint64_t)&stat, 0); + int32_t r = proc_stat(i, &stat); if (r == E_OK) { if (PCTL_LS_CONFIG.specificproc != NULL && string_strcmp(stat.name, PCTL_LS_CONFIG.specificproc) != 0) { diff --git a/user/tb/interp.c b/user/tb/interp.c index 7e36de7..5cedaf6 100644 --- a/user/tb/interp.c +++ b/user/tb/interp.c @@ -206,20 +206,20 @@ bool interp_runstring(char *string, InterpResult **res, bool logcmds, bool inter string_memcpy(args1[i], argtk->str, MIN(string_len(argtk->str), PROC_ARG_MAX)); } - int32_t app = processctl(-1, PCTL_SPAWN, (uint64_t)cmdtk->str, (uint64_t)args1, argslen1); + int32_t app = proc_spawn(cmdtk->str, args1, argslen1); if (app < 0) { usprintf(RES.errmsg, "Could not run %s: %s\n", cmdtk->str, ERRSTRING(app)); ok = false; goto cleanup; } - processctl(app, PCTL_RUN, 0, 0, 0); + proc_run(app); - while(processctl(app, PCTL_POLLSTATE, 0, 0, 0) != 4) { + while(proc_pollstate(app) != 4) { if (interactive) { int32_t key = devctl(&ps2kbdev, DEV_PS2KBDEV_READCH, (uint8_t *)PID, 0, 0); if (key > 0 && (uint8_t)key == C('S')) { - processctl(app, PCTL_KILL, 0, 0, 0); + proc_kill(app); goto cleanup; } } diff --git a/user/tb/main.c b/user/tb/main.c index c4c29f5..626467d 100644 --- a/user/tb/main.c +++ b/user/tb/main.c @@ -146,7 +146,7 @@ void do_mode_interactive(void) { } void main(void) { - PID = processctl(-1, PCTL_GETPID, 0, 0, 0); + PID = proc_getpid(); set_config(); diff --git a/user/tb/runtime.c b/user/tb/runtime.c index 44c0272..37cea1c 100644 --- a/user/tb/runtime.c +++ b/user/tb/runtime.c @@ -111,7 +111,7 @@ bool rt_do(Token *tks) { args1[ARRLEN(prepended_args)] = umalloc(PROC_ARG_MAX); string_strcpy(args1[ARRLEN(prepended_args)], s); - int32_t app = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb", (uint64_t)args1, ARRLEN(prepended_args)+1); + int32_t app = proc_spawn("base:/bin/tb", args1, ARRLEN(prepended_args)+1); if (app < 0) { ok = false; goto done; @@ -123,9 +123,9 @@ bool rt_do(Token *tks) { ipc_pipemake(10); ipc_pipeconnect(app, 0, PID, 10); - processctl(app, PCTL_RUN, 0, 0, 0); + proc_run(app); - while (processctl(app, PCTL_POLLSTATE, 0, 0, 0) != 4) { + while (proc_pollstate(app) != 4) { int32_t r; char buf[100]; string_memset(buf, 0, sizeof(buf));