1// Copyright 2022 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package unix
6
7import (
8	"internal/abi"
9	"syscall"
10	"unsafe"
11)
12
13//go:cgo_import_dynamic libc_getgrouplist getgrouplist "/usr/lib/libSystem.B.dylib"
14func libc_getgrouplist_trampoline()
15
16func Getgrouplist(name *byte, gid uint32, gids *uint32, n *int32) error {
17	_, _, errno := syscall_syscall6(abi.FuncPCABI0(libc_getgrouplist_trampoline),
18		uintptr(unsafe.Pointer(name)), uintptr(gid), uintptr(unsafe.Pointer(gids)),
19		uintptr(unsafe.Pointer(n)), 0, 0)
20	if errno != 0 {
21		return errno
22	}
23	return nil
24}
25
26const (
27	SC_GETGR_R_SIZE_MAX = 0x46
28	SC_GETPW_R_SIZE_MAX = 0x47
29)
30
31type Passwd struct {
32	Name   *byte
33	Passwd *byte
34	Uid    uint32 // uid_t
35	Gid    uint32 // gid_t
36	Change int64  // time_t
37	Class  *byte
38	Gecos  *byte
39	Dir    *byte
40	Shell  *byte
41	Expire int64 // time_t
42}
43
44type Group struct {
45	Name   *byte
46	Passwd *byte
47	Gid    uint32 // gid_t
48	Mem    **byte
49}
50
51//go:cgo_import_dynamic libc_getpwnam_r getpwnam_r  "/usr/lib/libSystem.B.dylib"
52func libc_getpwnam_r_trampoline()
53
54func Getpwnam(name *byte, pwd *Passwd, buf *byte, size uintptr, result **Passwd) syscall.Errno {
55	// Note: Returns an errno as its actual result, not in global errno.
56	errno, _, _ := syscall_syscall6(abi.FuncPCABI0(libc_getpwnam_r_trampoline),
57		uintptr(unsafe.Pointer(name)),
58		uintptr(unsafe.Pointer(pwd)),
59		uintptr(unsafe.Pointer(buf)),
60		size,
61		uintptr(unsafe.Pointer(result)),
62		0)
63	return syscall.Errno(errno)
64}
65
66//go:cgo_import_dynamic libc_getpwuid_r getpwuid_r  "/usr/lib/libSystem.B.dylib"
67func libc_getpwuid_r_trampoline()
68
69func Getpwuid(uid uint32, pwd *Passwd, buf *byte, size uintptr, result **Passwd) syscall.Errno {
70	// Note: Returns an errno as its actual result, not in global errno.
71	errno, _, _ := syscall_syscall6(abi.FuncPCABI0(libc_getpwuid_r_trampoline),
72		uintptr(uid),
73		uintptr(unsafe.Pointer(pwd)),
74		uintptr(unsafe.Pointer(buf)),
75		size,
76		uintptr(unsafe.Pointer(result)),
77		0)
78	return syscall.Errno(errno)
79}
80
81//go:cgo_import_dynamic libc_getgrnam_r getgrnam_r  "/usr/lib/libSystem.B.dylib"
82func libc_getgrnam_r_trampoline()
83
84func Getgrnam(name *byte, grp *Group, buf *byte, size uintptr, result **Group) syscall.Errno {
85	// Note: Returns an errno as its actual result, not in global errno.
86	errno, _, _ := syscall_syscall6(abi.FuncPCABI0(libc_getgrnam_r_trampoline),
87		uintptr(unsafe.Pointer(name)),
88		uintptr(unsafe.Pointer(grp)),
89		uintptr(unsafe.Pointer(buf)),
90		size,
91		uintptr(unsafe.Pointer(result)),
92		0)
93	return syscall.Errno(errno)
94}
95
96//go:cgo_import_dynamic libc_getgrgid_r getgrgid_r  "/usr/lib/libSystem.B.dylib"
97func libc_getgrgid_r_trampoline()
98
99func Getgrgid(gid uint32, grp *Group, buf *byte, size uintptr, result **Group) syscall.Errno {
100	// Note: Returns an errno as its actual result, not in global errno.
101	errno, _, _ := syscall_syscall6(abi.FuncPCABI0(libc_getgrgid_r_trampoline),
102		uintptr(gid),
103		uintptr(unsafe.Pointer(grp)),
104		uintptr(unsafe.Pointer(buf)),
105		size,
106		uintptr(unsafe.Pointer(result)),
107		0)
108	return syscall.Errno(errno)
109}
110
111//go:cgo_import_dynamic libc_sysconf sysconf "/usr/lib/libSystem.B.dylib"
112func libc_sysconf_trampoline()
113
114func Sysconf(key int32) int64 {
115	val, _, errno := syscall_syscall6X(abi.FuncPCABI0(libc_sysconf_trampoline),
116		uintptr(key), 0, 0, 0, 0, 0)
117	if errno != 0 {
118		return -1
119	}
120	return int64(val)
121}
122