Skip to content

Commit

Permalink
chore: fix unsafe.StringHeader lint
Browse files Browse the repository at this point in the history
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:

golang/go#53003 (comment)
Signed-off-by: Dave Dykstra <2129743+DrDaveD@users.noreply.github.com>
  • Loading branch information
dtrudg authored and DrDaveD committed Jun 18, 2024
1 parent 5ef3563 commit 236a586
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions internal/pkg/runtime/engine/apptainer/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 236a586

Please sign in to comment.