28 lines
413 B
Go
28 lines
413 B
Go
//go:build linux
|
|
|
|
package main
|
|
|
|
import (
|
|
"net"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func askPeerPID(c net.Conn) int {
|
|
uc, ok := c.(*net.UnixConn)
|
|
if !ok {
|
|
return 0
|
|
}
|
|
raw, err := uc.SyscallConn()
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
pid := 0
|
|
_ = raw.Control(func(fd uintptr) {
|
|
if cred, err := unix.GetsockoptUcred(int(fd), unix.SOL_SOCKET, unix.SO_PEERCRED); err == nil {
|
|
pid = int(cred.Pid)
|
|
}
|
|
})
|
|
return pid
|
|
}
|