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)
  • Loading branch information
dtrudg committed Mar 21, 2024
1 parent 88146eb commit 80ca286
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions internal/pkg/runtime/engine/singularity/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
Expand Down Expand Up @@ -220,17 +219,19 @@ func (e *EngineOperations) StartProcess(masterConn net.Conn) error {
// Modify argv argument and program name shown in /proc/self/comm
name := "sinit"

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 80ca286

Please sign in to comment.