From 54c730bb6649f70866b5ac6dbac395c3ff1b0ff4 Mon Sep 17 00:00:00 2001 From: Michalis Pappas Date: Wed, 26 Mar 2025 05:47:09 +0100 Subject: [PATCH] lib/posix-process: Create PID1 from the thread passed in init Update the instantiation of INIT_PID to use the thread passed in the init context. This allows excluding unikraft's init thread from the process when libukboot creates a separate thread for main(). Signed-off-by: Michalis Pappas Approved-by: Andrei Tatar Approved-by: Sergiu Moga Reviewed-by: Andrei Tatar Reviewed-by: Sergiu Moga GitHub-Closes: #1620 --- lib/posix-process/process.c | 23 ++++++++++++++++++++--- lib/posix-process/process.h | 2 ++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/posix-process/process.c b/lib/posix-process/process.c index fec17e2c5..1f303a076 100644 --- a/lib/posix-process/process.c +++ b/lib/posix-process/process.c @@ -41,6 +41,8 @@ #include #include +struct uk_thread *pprocess_thread_main; + #if CONFIG_LIBPOSIX_PROCESS_PIDS #include #include @@ -484,11 +486,26 @@ void uk_posix_process_kill(struct uk_thread *thread) } #if CONFIG_LIBPOSIX_PROCESS_INIT_PIDS -static int posix_process_init(struct uk_init_ctx *ictx __unused) +static int posix_process_init(struct uk_init_ctx *ictx) { + struct uk_thread *t; + + UK_ASSERT(ictx); + + /* If ictx->tmain in set, main() executes on a + * separate uk_thread. Instantiate PID_INIT from + * that thread, and set pprocess_thread_main, as + * we will need that information later. + */ + if (ictx->tmain) { + t = ictx->tmain; + pprocess_thread_main = ictx->tmain; + } else { + t = uk_thread_current(); + } + /* Create a POSIX process without parent ("init" process) */ - return uk_posix_process_create(uk_alloc_get_default(), - uk_thread_current(), NULL); + return uk_posix_process_create(uk_alloc_get_default(), t, NULL); } uk_late_initcall(posix_process_init, 0x0); diff --git a/lib/posix-process/process.h b/lib/posix-process/process.h index d214a8e3f..10ae276aa 100644 --- a/lib/posix-process/process.h +++ b/lib/posix-process/process.h @@ -46,6 +46,8 @@ #define TIDMAP_SIZE (CONFIG_LIBPOSIX_PROCESS_MAX_PID + 1) +extern struct uk_thread *pprocess_thread_main; + /* Notice: The RUNNING state is not necessarily in sync with the state * of the underlying uk_thread (may be blocked by the scheduler). * On the other hand, the BLOCKED state implies that the underlying -- 2.39.5