From 61b01cc1eeb3900df25f15dd39d28b846e79b0d9 Mon Sep 17 00:00:00 2001 From: Danny Canter Date: Tue, 1 Jul 2025 02:16:17 -0700 Subject: [PATCH] LCShim: Add CZ prefixes/pidfd wrappers (#189) We should namespace these a bit, and we need pidfd for some upcoming terminal work. This also just gets rid of syscall2 and replaces with a pivot_root wrapper. --- .../LCShim/include/{syscall2.h => syscall.h} | 22 ++++++++++++++----- .../Sources/LCShim/{syscall2.c => syscall.c} | 18 +++++++++++---- vminitd/Sources/vmexec/RunCommand.swift | 2 +- vminitd/Sources/vminitd/Application.swift | 2 +- 4 files changed, 33 insertions(+), 11 deletions(-) rename vminitd/Sources/LCShim/include/{syscall2.h => syscall.h} (63%) rename vminitd/Sources/LCShim/{syscall2.c => syscall.c} (59%) diff --git a/vminitd/Sources/LCShim/include/syscall2.h b/vminitd/Sources/LCShim/include/syscall.h similarity index 63% rename from vminitd/Sources/LCShim/include/syscall2.h rename to vminitd/Sources/LCShim/include/syscall.h index 34e319e4..062ca260 100644 --- a/vminitd/Sources/LCShim/include/syscall2.h +++ b/vminitd/Sources/LCShim/include/syscall.h @@ -14,13 +14,25 @@ * limitations under the License. */ -// +#ifndef __SYSCALL_H +#define __SYSCALL_H -#ifndef __SYSCALL2_H -#define __SYSCALL2_H +#include -int syscall2(long number, void *arg1, void *arg2); +int CZ_pivot_root(const char *new_root, const char *put_old); -int set_sub_reaper(); +int CZ_set_sub_reaper(); + +#ifndef SYS_pidfd_open +#define SYS_pidfd_open 434 +#endif + +int CZ_pidfd_open(pid_t pid, unsigned int flags); + +#ifndef SYS_pidfd_getfd +#define SYS_pidfd_getfd 438 +#endif + +int CZ_pidfd_getfd(int pidfd, int targetfd, unsigned int flags); #endif diff --git a/vminitd/Sources/LCShim/syscall2.c b/vminitd/Sources/LCShim/syscall.c similarity index 59% rename from vminitd/Sources/LCShim/syscall2.c rename to vminitd/Sources/LCShim/syscall.c index 05844d5e..3eb825bc 100644 --- a/vminitd/Sources/LCShim/syscall2.c +++ b/vminitd/Sources/LCShim/syscall.c @@ -18,10 +18,20 @@ #include #include -#include "syscall2.h" +#include "syscall.h" -int syscall2(long number, void *arg1, void *arg2) { - return syscall(number, arg1, arg2); +int CZ_pivot_root(const char *new_root, const char *put_old) { + return syscall(SYS_pivot_root, new_root, put_old); } -int set_sub_reaper() { return prctl(PR_SET_CHILD_SUBREAPER, 1); } +int CZ_set_sub_reaper() { return prctl(PR_SET_CHILD_SUBREAPER, 1); } + +int CZ_pidfd_open(pid_t pid, unsigned int flags) { + // Musl doesn't have pidfd_open. + return syscall(SYS_pidfd_open, pid, flags); +} + +int CZ_pidfd_getfd(int pidfd, int targetfd, unsigned int flags) { + // Musl doesn't have pidfd_getfd. + return syscall(SYS_pidfd_getfd, pidfd, targetfd, flags); +} diff --git a/vminitd/Sources/vmexec/RunCommand.swift b/vminitd/Sources/vmexec/RunCommand.swift index 02246515..42ba3114 100644 --- a/vminitd/Sources/vmexec/RunCommand.swift +++ b/vminitd/Sources/vmexec/RunCommand.swift @@ -228,7 +228,7 @@ struct RunCommand: ParsableCommand { guard fchdir(newRoot) == 0 else { throw App.Errno(stage: "fchdir(newroot)") } - guard syscall2(Int(SYS_pivot_root), toCString("."), toCString(".")) == 0 else { + guard CZ_pivot_root(toCString("."), toCString(".")) == 0 else { throw App.Errno(stage: "pivot_root()") } // change cwd to the old root diff --git a/vminitd/Sources/vminitd/Application.swift b/vminitd/Sources/vminitd/Application.swift index 5629d1a6..24539a7c 100644 --- a/vminitd/Sources/vminitd/Application.swift +++ b/vminitd/Sources/vminitd/Application.swift @@ -88,7 +88,7 @@ struct Application { // since we are not running as pid1 in this mode we must set ourselves // as a subpreaper so that all child processes are reaped by us and not // passed onto our parent. - set_sub_reaper() + CZ_set_sub_reaper() #endif signal(SIGPIPE, SIG_IGN)