From ad56890ee9dad84b286e3734353183e7adc01977 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Fri, 19 Sep 2025 18:12:36 +0200 Subject: [PATCH] Adopt pipe handle inheritance --- base/scripts/init.tb | 1 + user/init/main.c | 30 +++++++++++------------------- user/tb/interp.c | 22 +++------------------- 3 files changed, 15 insertions(+), 38 deletions(-) diff --git a/base/scripts/init.tb b/base/scripts/init.tb index 0ff3e62..7b873c3 100644 --- a/base/scripts/init.tb +++ b/base/scripts/init.tb @@ -1,2 +1,3 @@ @print "this is an init script!" base:/bin/pctl ls +base:/bin/tb -m interactive diff --git a/user/init/main.c b/user/init/main.c index c80a643..94f9c32 100644 --- a/user/init/main.c +++ b/user/init/main.c @@ -10,34 +10,18 @@ #include #include -#define SUBPROC_PIPE_OUT 0x1f - uint64_t PID; -void main(void) { - PID = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0); - - ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_MAKE, NULL, 0); - +void tb_runinitscript(void) { char *tbargs[] = { "-m", "runfile", "-f", "base:/scripts/init.tb", "-logcmds", "yes" }; int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb", (uint64_t)&tbargs, ARRLEN(tbargs)); - ipcpipe(tb, IPCPIPE_OUT, IPCPIPE_REPLACE, (uint8_t *)PID, SUBPROC_PIPE_OUT); - + ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_MAKE, NULL, 0); ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_ADD_BCAST, NULL, 1); - #define OUTBUF_MAX 1024 - char *outbuf = dlmalloc(OUTBUF_MAX); - processctl(tb, PCTL_RUN, 0, 0, 0); - for(;;) { - string_memset(outbuf, 0, OUTBUF_MAX); - int32_t nrd = ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_READ, (uint8_t *)outbuf, sizeof(outbuf)); - if (nrd > 0) { - uprintf("%s", outbuf); - } - + while(processctl(tb, PCTL_POLLSTATE, 0, 0, 0) != 4) { int32_t kbchr; int32_t read = ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_READ, (uint8_t *)&kbchr, sizeof(kbchr)); if (read > 0) { @@ -49,6 +33,14 @@ void main(void) { } } } +} + +void main(void) { + PID = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0); + + tb_runinitscript(); + + uprintf("Shell exited! Please reboot the system.\n"); for(;;); } diff --git a/user/tb/interp.c b/user/tb/interp.c index 01aa65a..69ba865 100644 --- a/user/tb/interp.c +++ b/user/tb/interp.c @@ -13,7 +13,7 @@ #include "interp.h" #include "runtime.h" -#define SUBPROC_PIPE_OUT 0x1f +/* #define SUBPROC_PIPE_OUT 31 */ extern uint64_t PID; @@ -210,34 +210,18 @@ bool interp_runstring(const char *string, InterpResult **res, bool logcmds) { argtk = argtk->next; } - #define OUTBUF_MAX 1024 - char *outbuf = dlmalloc(OUTBUF_MAX); - string_memset(outbuf, 0, OUTBUF_MAX); - - ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_MAKE, NULL, 0); - - int32_t app = processctl(-1, PCTL_SPAWN, (uint64_t)(char *)appname, (uint64_t)args1, argslen1); + int32_t app = processctl(-1, PCTL_SPAWN, (uint64_t)appname, (uint64_t)args1, argslen1); if (app < 0) { usprintf(RES.errmsg, "Could not run %s: %s\n", appname, ERRSTRING(app)); ok = false; goto cleanup; } - ipcpipe(app, IPCPIPE_OUT, IPCPIPE_REPLACE, (uint8_t *)PID, SUBPROC_PIPE_OUT); - ipcpipe(app, IPCPIPE_IN, IPCPIPE_REPLACE, (uint8_t *)PID, IPCPIPE_IN); processctl(app, PCTL_RUN, 0, 0, 0); - do { - int32_t nrd = ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_READ, (uint8_t *)outbuf, OUTBUF_MAX); - if (nrd > 0) { - uprintf("%s", outbuf); - string_memset(outbuf, 0, OUTBUF_MAX); - } - } while(processctl(app, PCTL_POLLSTATE, 0, 0, 0) != 4); + while(processctl(app, PCTL_POLLSTATE, 0, 0, 0) != 4); cleanup: - ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_DELETE, NULL, 0); - dlfree(outbuf); for (size_t j = 0; j < argslen1; j++) { dlfree(args1[j]); }