Skip to content

Commit e466742

Browse files
committed
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: golang/go#53003 (comment)
1 parent 891adcf commit e466742

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

internal/pkg/runtime/engine/singularity/process_linux.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"os/exec"
1919
"os/signal"
2020
"path/filepath"
21-
"reflect"
2221
"regexp"
2322
"runtime"
2423
"strconv"
@@ -220,17 +219,19 @@ func (e *EngineOperations) StartProcess(masterConn net.Conn) error {
220219
// Modify argv argument and program name shown in /proc/self/comm
221220
name := "sinit"
222221

223-
argv0str := (*reflect.StringHeader)(unsafe.Pointer(&os.Args[0]))
224-
argv0 := (*[1 << 30]byte)(unsafe.Pointer(argv0str.Data))[:argv0str.Len]
225-
progname := make([]byte, argv0str.Len)
222+
argv0 := unsafe.Slice(unsafe.StringData(os.Args[0]), len(os.Args[0]))
223+
progname := make([]byte, len(os.Args[0]))
226224

227-
if len(name) > argv0str.Len {
225+
if len(name) > len(progname) {
228226
return fmt.Errorf("program name too short")
229227
}
230228

231229
copy(progname, name)
230+
231+
// Set name by overwriting argv[0]
232232
copy(argv0, progname)
233233

234+
// Set name by PR_SET_NAME (only affects PR_GET_NAME)
234235
ptr := unsafe.Pointer(&progname[0])
235236
if _, _, err := syscall.Syscall(syscall.SYS_PRCTL, syscall.PR_SET_NAME, uintptr(ptr), 0); err != 0 {
236237
return syscall.Errno(err)

0 commit comments

Comments
 (0)