From 236a586a0dc544e660dc496e67ed28321fc6a87c Mon Sep 17 00:00:00 2001 From: David Trudgian Date: Thu, 21 Mar 2024 12:12:04 +0000 Subject: [PATCH] chore: fix unsafe.StringHeader lint If we are running with a PID namespace, we have a shim process by default. We have code that sets the name of this process to `sinit` by: * Overwriting the value of argv[0] * Using PR_SET_NAME Both are necessary as PR_SET_NAME only affects the value from PR_GET_NAME. In this code, unsafe.StringHeader is now deprecated. Rewrite using the pattern that takes an unsafe.Slice from an unsafe.StringData to get the raw bytes for the string os.Args[0]. See: https://github.com/golang/go/issues/53003#issuecomment-1964775055 Signed-off-by: Dave Dykstra <2129743+DrDaveD@users.noreply.github.com> --- .../pkg/runtime/engine/apptainer/process_linux.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/pkg/runtime/engine/apptainer/process_linux.go b/internal/pkg/runtime/engine/apptainer/process_linux.go index 3f5f4daf51..d443b2445e 100644 --- a/internal/pkg/runtime/engine/apptainer/process_linux.go +++ b/internal/pkg/runtime/engine/apptainer/process_linux.go @@ -21,7 +21,6 @@ import ( "os/exec" "os/signal" "path/filepath" - "reflect" "regexp" "runtime" "strconv" @@ -238,17 +237,19 @@ func (e *EngineOperations) StartProcess(masterConnFd int) error { // Modify argv argument and program name shown in /proc/self/comm name := "appinit" - argv0str := (*reflect.StringHeader)(unsafe.Pointer(&os.Args[0])) - argv0 := (*[1 << 30]byte)(unsafe.Pointer(argv0str.Data))[:argv0str.Len] - progname := make([]byte, argv0str.Len) + argv0 := unsafe.Slice(unsafe.StringData(os.Args[0]), len(os.Args[0])) + progname := make([]byte, len(os.Args[0])) - if len(name) > argv0str.Len { + if len(name) > len(progname) { return fmt.Errorf("program name too short") } copy(progname, name) + + // Set name by overwriting argv[0] copy(argv0, progname) + // Set name by PR_SET_NAME (only affects PR_GET_NAME) ptr := unsafe.Pointer(&progname[0]) if _, _, err := syscall.Syscall(syscall.SYS_PRCTL, syscall.PR_SET_NAME, uintptr(ptr), 0); err != 0 { return syscall.Errno(err)