unix: add Xucred, GetsockoptXucred on freebsd

Follow CL 292330 which added these on darwin.

Generated on FreeBSD 13.0BETA3

For golang/go#41659

Change-Id: I30eeef43c4f61a9449f3fe8b5cc0033f46dfe822
Reviewed-on: https://go-review.googlesource.com/c/sys/+/294989
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Tobias Klauser
2021-02-23 09:50:22 +01:00
committed by Tobias Klauser
parent b80eb88b80
commit 7937bea010
13 changed files with 112 additions and 1 deletions

View File

@@ -115,6 +115,7 @@ includes_FreeBSD='
#include <sys/sched.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/sockio.h>
#include <sys/stat.h>
#include <sys/sysctl.h>

View File

@@ -382,7 +382,7 @@ func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
// The usual level and opt are SOL_LOCAL and LOCAL_PEERCRED, respectively.
func GetsockoptXucred(fd, level, opt int) (*Xucred, error) {
x := new(Xucred)
vallen := _Socklen(unsafe.Sizeof(Xucred{}))
vallen := _Socklen(SizeofXucred)
err := getsockopt(fd, level, opt, unsafe.Pointer(x), &vallen)
return x, err
}

View File

@@ -126,6 +126,15 @@ func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) {
return setsockopt(fd, level, opt, unsafe.Pointer(mreq), unsafe.Sizeof(*mreq))
}
// GetsockoptXucred is a getsockopt wrapper that returns an Xucred struct.
// The usual level and opt are SOL_LOCAL and LOCAL_PEERCRED, respectively.
func GetsockoptXucred(fd, level, opt int) (*Xucred, error) {
x := new(Xucred)
vallen := _Socklen(SizeofXucred)
err := getsockopt(fd, level, opt, unsafe.Pointer(x), &vallen)
return x, err
}
func Accept4(fd, flags int) (nfd int, sa Sockaddr, err error) {
var rsa RawSockaddrAny
var len _Socklen = SizeofSockaddrAny

View File

@@ -11,6 +11,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path"
@@ -297,6 +298,42 @@ func TestCapRightsSetAndClear(t *testing.T) {
}
}
func TestGetsockoptXucred(t *testing.T) {
fds, err := unix.Socketpair(unix.AF_LOCAL, unix.SOCK_STREAM, 0)
if err != nil {
t.Fatalf("Socketpair: %v", err)
}
defer unix.Close(fds[0])
defer unix.Close(fds[1])
srvFile := os.NewFile(uintptr(fds[0]), "server")
defer srvFile.Close()
srv, err := net.FileConn(srvFile)
if err != nil {
t.Fatalf("FileConn: %v", err)
}
defer srv.Close()
cliFile := os.NewFile(uintptr(fds[1]), "client")
defer cliFile.Close()
cli, err := net.FileConn(cliFile)
if err != nil {
t.Fatalf("FileConn: %v", err)
}
defer cli.Close()
cred, err := unix.GetsockoptXucred(fds[1], unix.SOL_LOCAL, unix.LOCAL_PEERCRED)
if err == unix.ENOTCONN {
t.Skip("GetsockoptXucred not supported with Socketpair on FreeBSD 11 and earlier")
} else if err != nil {
t.Fatal(err)
}
t.Logf("got: %+v", cred)
if got, want := cred.Uid, os.Getuid(); int(got) != int(want) {
t.Errorf("uid = %v; want %v", got, want)
}
}
// stringsFromByteSlice converts a sequence of attributes to a []string.
// On FreeBSD, each entry consists of a single byte containing the length
// of the attribute name, followed by the attribute name.

View File

@@ -40,6 +40,7 @@ package unix
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ucred.h>
#include <sys/un.h>
#include <sys/utsname.h>
#include <sys/wait.h>
@@ -204,6 +205,8 @@ type RawSockaddrAny C.struct_sockaddr_any
type _Socklen C.socklen_t
type Xucred C.struct_xucred
type Linger C.struct_linger
type Iovec C.struct_iovec
@@ -230,6 +233,7 @@ const (
SizeofSockaddrAny = C.sizeof_struct_sockaddr_any
SizeofSockaddrUnix = C.sizeof_struct_sockaddr_un
SizeofSockaddrDatalink = C.sizeof_struct_sockaddr_dl
SizeofXucred = C.sizeof_struct_xucred
SizeofLinger = C.sizeof_struct_linger
SizeofIovec = C.sizeof_struct_iovec
SizeofIPMreq = C.sizeof_struct_ip_mreq

View File

@@ -998,6 +998,11 @@ const (
KERN_OSRELEASE = 0x2
KERN_OSTYPE = 0x1
KERN_VERSION = 0x4
LOCAL_CONNWAIT = 0x4
LOCAL_CREDS = 0x2
LOCAL_CREDS_PERSISTENT = 0x3
LOCAL_PEERCRED = 0x1
LOCAL_VENDOR = 0x80000000
LOCK_EX = 0x2
LOCK_NB = 0x4
LOCK_SH = 0x1
@@ -1376,6 +1381,7 @@ const (
SOCK_RDM = 0x4
SOCK_SEQPACKET = 0x5
SOCK_STREAM = 0x1
SOL_LOCAL = 0x0
SOL_SOCKET = 0xffff
SOMAXCONN = 0x80
SO_ACCEPTCONN = 0x2

View File

@@ -998,6 +998,11 @@ const (
KERN_OSRELEASE = 0x2
KERN_OSTYPE = 0x1
KERN_VERSION = 0x4
LOCAL_CONNWAIT = 0x4
LOCAL_CREDS = 0x2
LOCAL_CREDS_PERSISTENT = 0x3
LOCAL_PEERCRED = 0x1
LOCAL_VENDOR = 0x80000000
LOCK_EX = 0x2
LOCK_NB = 0x4
LOCK_SH = 0x1
@@ -1377,6 +1382,7 @@ const (
SOCK_RDM = 0x4
SOCK_SEQPACKET = 0x5
SOCK_STREAM = 0x1
SOL_LOCAL = 0x0
SOL_SOCKET = 0xffff
SOMAXCONN = 0x80
SO_ACCEPTCONN = 0x2

View File

@@ -981,6 +981,11 @@ const (
KERN_OSRELEASE = 0x2
KERN_OSTYPE = 0x1
KERN_VERSION = 0x4
LOCAL_CONNWAIT = 0x4
LOCAL_CREDS = 0x2
LOCAL_CREDS_PERSISTENT = 0x3
LOCAL_PEERCRED = 0x1
LOCAL_VENDOR = 0x80000000
LOCK_EX = 0x2
LOCK_NB = 0x4
LOCK_SH = 0x1
@@ -1342,6 +1347,7 @@ const (
SOCK_RDM = 0x4
SOCK_SEQPACKET = 0x5
SOCK_STREAM = 0x1
SOL_LOCAL = 0x0
SOL_SOCKET = 0xffff
SOMAXCONN = 0x80
SO_ACCEPTCONN = 0x2

View File

@@ -998,6 +998,11 @@ const (
KERN_OSRELEASE = 0x2
KERN_OSTYPE = 0x1
KERN_VERSION = 0x4
LOCAL_CONNWAIT = 0x4
LOCAL_CREDS = 0x2
LOCAL_CREDS_PERSISTENT = 0x3
LOCAL_PEERCRED = 0x1
LOCAL_VENDOR = 0x80000000
LOCK_EX = 0x2
LOCK_NB = 0x4
LOCK_SH = 0x1
@@ -1377,6 +1382,7 @@ const (
SOCK_RDM = 0x4
SOCK_SEQPACKET = 0x5
SOCK_STREAM = 0x1
SOL_LOCAL = 0x0
SOL_SOCKET = 0xffff
SOMAXCONN = 0x80
SO_ACCEPTCONN = 0x2

View File

@@ -251,6 +251,14 @@ type RawSockaddrAny struct {
type _Socklen uint32
type Xucred struct {
Version uint32
Uid uint32
Ngroups int16
Groups [16]uint32
_ *byte
}
type Linger struct {
Onoff int32
Linger int32
@@ -313,6 +321,7 @@ const (
SizeofSockaddrAny = 0x6c
SizeofSockaddrUnix = 0x6a
SizeofSockaddrDatalink = 0x36
SizeofXucred = 0x50
SizeofLinger = 0x8
SizeofIovec = 0x8
SizeofIPMreq = 0x8

View File

@@ -247,6 +247,14 @@ type RawSockaddrAny struct {
type _Socklen uint32
type Xucred struct {
Version uint32
Uid uint32
Ngroups int16
Groups [16]uint32
_ *byte
}
type Linger struct {
Onoff int32
Linger int32
@@ -309,6 +317,7 @@ const (
SizeofSockaddrAny = 0x6c
SizeofSockaddrUnix = 0x6a
SizeofSockaddrDatalink = 0x36
SizeofXucred = 0x58
SizeofLinger = 0x8
SizeofIovec = 0x10
SizeofIPMreq = 0x8

View File

@@ -249,6 +249,14 @@ type RawSockaddrAny struct {
type _Socklen uint32
type Xucred struct {
Version uint32
Uid uint32
Ngroups int16
Groups [16]uint32
_ *byte
}
type Linger struct {
Onoff int32
Linger int32
@@ -311,6 +319,7 @@ const (
SizeofSockaddrAny = 0x6c
SizeofSockaddrUnix = 0x6a
SizeofSockaddrDatalink = 0x36
SizeofXucred = 0x50
SizeofLinger = 0x8
SizeofIovec = 0x8
SizeofIPMreq = 0x8

View File

@@ -247,6 +247,14 @@ type RawSockaddrAny struct {
type _Socklen uint32
type Xucred struct {
Version uint32
Uid uint32
Ngroups int16
Groups [16]uint32
_ *byte
}
type Linger struct {
Onoff int32
Linger int32
@@ -309,6 +317,7 @@ const (
SizeofSockaddrAny = 0x6c
SizeofSockaddrUnix = 0x6a
SizeofSockaddrDatalink = 0x36
SizeofXucred = 0x58
SizeofLinger = 0x8
SizeofIovec = 0x10
SizeofIPMreq = 0x8