1
0

peercred_linux.go 541 B

12345678910111213141516171819202122232425
  1. //go:build linux
  2. package localapi
  3. import (
  4. "net"
  5. "golang.org/x/sys/unix"
  6. )
  7. // peerCheckSupported reports that this platform can identify a connecting process.
  8. const peerCheckSupported = true
  9. // peerUID returns the uid of the process on the other end of conn.
  10. func peerUID(conn net.Conn) (uid uint32, err error) {
  11. err = withPeerFD(conn, func(fd int) error {
  12. cred, credErr := unix.GetsockoptUcred(fd, unix.SOL_SOCKET, unix.SO_PEERCRED)
  13. if credErr != nil {
  14. return credErr
  15. }
  16. uid = cred.Uid
  17. return nil
  18. })
  19. return uid, err
  20. }