arblant
firefox-vpn-client / core / internal / tunapi / socket_linux.go
socket_linux.go34 lines · 833 B
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
package tunapi

import (
	"net"
	"os"
	"strconv"

	"golang.org/x/sys/unix"
)

// SocketPath is the service's socket, in a folder only root may write to.
const SocketPath = "/run/firefox-vpn-tun.sock"

// ServiceFile is the service's systemd unit.
const ServiceFile = "/etc/systemd/system/" + ServiceName + ".service"

// peer asks the kernel who is at the other end of c.
func peer(c net.Conn) (peerInfo, error) {
	var p peerInfo
	err := fdOf(c, func(fd int) error {
		cred, err := unix.GetsockoptUcred(fd, unix.SOL_SOCKET, unix.SO_PEERCRED)
		if err != nil {
			return err
		}
		p = peerInfo{uid: cred.Uid, pid: cred.Pid}
		return nil
	})
	return p, err
}

// ProcessPath is the executable of process pid.
func ProcessPath(pid uint32) (string, error) {
	return os.Readlink("/proc/" + strconv.FormatUint(uint64(pid), 10) + "/exe")
}