forked from git-ecosystem/trace2receiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unixsocket_linux.go
49 lines (38 loc) · 944 Bytes
/
unixsocket_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//go:build linux
// +build linux
package trace2receiver
import (
"net"
"os/user"
"strconv"
"golang.org/x/sys/unix"
)
// Get the username of the process on the other end of
// the unix domain socket connection. (It is not sufficient
// to just call `user.Current()` because the telemetry
// service will probably be running as root or some other
// pseudo-user.)
func getPeerUsername(conn *net.UnixConn) (string, error) {
raw, err := conn.SyscallConn()
if err != nil {
return "", err
}
// On Linux we use "Ucred" on Darwin we use "Xucred".
var cred *unix.Ucred
var crederr error
err = raw.Control(
func(fd uintptr) {
cred, crederr = unix.GetsockoptUcred(int(fd),
unix.SOL_SOCKET, unix.SO_PEERCRED)
err = crederr
})
if err != nil {
return "", err
}
uidString := strconv.FormatUint(uint64(cred.Uid), 10)
u, err := user.LookupId(uidString)
if err != nil {
return "", err
}
return u.Username, nil
}