Created
July 6, 2013 08:52
-
-
Save kavu/5939291 to your computer and use it in GitHub Desktop.
Setting Process name in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func SetProcessName(name string) error { | |
argv0str := (*reflect.StringHeader)(unsafe.Pointer(&os.Args[0])) | |
argv0 := (*[1 << 30]byte)(unsafe.Pointer(argv0str.Data))[:argv0str.Len] | |
n := copy(argv0, name) | |
if n < len(argv0) { | |
argv0[n] = 0 | |
} | |
// Syscall PRCTL, not working on Darwin for me | |
// bytes := append([]byte(name), 0) | |
// ptr := unsafe.Pointer(&bytes[0]) | |
// if _, _, errno := syscall.RawSyscall6(syscall.SYS_PRCTL, syscall.PR_SET_NAME, uintptr(ptr), 0, 0, 0, 0); errno != 0 { | |
// return syscall.Errno(errno) | |
// } | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment