git » go-net » unix-separados » tree

[unix-separados] / unix / cred.go

// Copyright 2016 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package unix contains utilities for UNIX sockets.
package unix

import (
	"errors"
	"net"
)

var (
	errOpNoSupport = errors.New("operation not supported")
)

// Credentials of a peer connected to an UNIX socket.
// Not all fields will be set in all OSs, some only provide Uid and Gid.
// Values will be set to -1 if they're not supported.
type UnixPeerCreds struct {
	Uid int
	Gid int
	Pid int
}

// GetPeerCredentials obtains the credentials of the peer c is connected to.
func GetPeerCredentials(c *net.UnixConn) (*UnixPeerCreds, error) {
	f, err := c.File()
	if err != nil {
		return nil, err
	}
	defer f.Close()

	return getPeerCredentials(f.Fd())
}