forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_linux_cgo.go
198 lines (163 loc) · 3.72 KB
/
util_linux_cgo.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// +build linux
// +build cgo
package shared
import (
"fmt"
"os"
"unsafe"
// Used by cgo
_ "github.com/lxc/lxd/lxd/include"
"golang.org/x/sys/unix"
)
/*
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <limits.h>
#include <poll.h>
#include <pty.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include "../lxd/include/process_utils.h"
#define ABSTRACT_UNIX_SOCK_LEN sizeof(((struct sockaddr_un *)0)->sun_path)
static int read_pid(int fd)
{
ssize_t ret;
pid_t n = -1;
again:
ret = read(fd, &n, sizeof(n));
if (ret < 0 && errno == EINTR)
goto again;
if (ret < 0)
return -1;
return n;
}
*/
import "C"
const ABSTRACT_UNIX_SOCK_LEN int = C.ABSTRACT_UNIX_SOCK_LEN
// UserId is an adaption from https://codereview.appspot.com/4589049.
func UserId(name string) (int, error) {
var pw C.struct_passwd
var result *C.struct_passwd
bufSize := C.sysconf(C._SC_GETPW_R_SIZE_MAX)
if bufSize < 0 {
bufSize = 4096
}
buf := C.malloc(C.size_t(bufSize))
if buf == nil {
return -1, fmt.Errorf("allocation failed")
}
defer C.free(buf)
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
again:
rv, errno := C.getpwnam_r(cname,
&pw,
(*C.char)(buf),
C.size_t(bufSize),
&result)
if rv < 0 {
// OOM killer will take care of us if we end up doing this too
// often.
if errno == unix.ERANGE {
bufSize *= 2
tmp := C.realloc(buf, C.size_t(bufSize))
if tmp == nil {
return -1, fmt.Errorf("allocation failed")
}
buf = tmp
goto again
}
return -1, fmt.Errorf("failed user lookup: %s", unix.Errno(rv))
}
if result == nil {
return -1, fmt.Errorf("unknown user %s", name)
}
return int(C.int(result.pw_uid)), nil
}
// GroupId is an adaption from https://codereview.appspot.com/4589049.
func GroupId(name string) (int, error) {
var grp C.struct_group
var result *C.struct_group
bufSize := C.sysconf(C._SC_GETGR_R_SIZE_MAX)
if bufSize < 0 {
bufSize = 4096
}
buf := C.malloc(C.size_t(bufSize))
if buf == nil {
return -1, fmt.Errorf("allocation failed")
}
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
again:
rv, errno := C.getgrnam_r(cname,
&grp,
(*C.char)(buf),
C.size_t(bufSize),
&result)
if rv != 0 {
// OOM killer will take care of us if we end up doing this too
// often.
if errno == unix.ERANGE {
bufSize *= 2
tmp := C.realloc(buf, C.size_t(bufSize))
if tmp == nil {
return -1, fmt.Errorf("allocation failed")
}
buf = tmp
goto again
}
C.free(buf)
return -1, fmt.Errorf("failed group lookup: %s", unix.Errno(rv))
}
C.free(buf)
if result == nil {
return -1, fmt.Errorf("unknown group %s", name)
}
return int(C.int(result.gr_gid)), nil
}
func ReadPid(r *os.File) int {
return int(C.read_pid(C.int(r.Fd())))
}
func unCloexec(fd int) error {
var err error = nil
flags, _, errno := unix.Syscall(unix.SYS_FCNTL, uintptr(fd), unix.F_GETFD, 0)
if errno != 0 {
err = errno
return err
}
flags &^= unix.FD_CLOEXEC
_, _, errno = unix.Syscall(unix.SYS_FCNTL, uintptr(fd), unix.F_SETFD, flags)
if errno != 0 {
err = errno
}
return err
}
func PidFdOpen(Pid int, Flags uint32) (*os.File, error) {
pidFd, errno := C.pidfd_open(C.int(Pid), C.uint32_t(Flags))
if errno != nil {
return nil, errno
}
errno = unCloexec(int(pidFd))
if errno != nil {
return nil, errno
}
return os.NewFile(uintptr(pidFd), fmt.Sprintf("%d", Pid)), nil
}
func PidfdSendSignal(Pidfd int, Signal int, Flags uint32) error {
ret, errno := C.pidfd_send_signal(C.int(Pidfd), C.int(Signal), nil, C.uint32_t(Flags))
if ret != 0 {
return errno
}
return nil
}