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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
// Copyright 2011 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.
// POSIX library calls.
// This file is compiled as ordinary Go code,
// but it is also input to mksyscall,
// which parses the //sys lines and generates library call stubs.
// Note that sometimes we use a lowercase //sys name and
// wrap it in our own nicer implementation.
package syscall
import "unsafe"
/*
* Wrapped
*/
//sysnb pipe(p *[2]int) (errno int)
//pipe(p *[2]int) int
func Pipe(p []int) (errno int) {
if len(p) != 2 {
return EINVAL
}
var pp [2]int
errno = pipe(&pp)
p[0] = pp[0]
p[1] = pp[1]
return
}
//sys utimes(path string, times *[2]Timeval) (errno int)
//utimes(path *byte, times *[2]Timeval) int
func Utimes(path string, tv []Timeval) (errno int) {
if len(tv) != 2 {
return EINVAL
}
return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
}
//sys getcwd(buf *byte, size Size_t) (errno int)
//getcwd(buf *byte, size Size_t) *byte
const ImplementsGetwd = true
func Getwd() (ret string, errno int) {
for len := Size_t(4096); ; len *= 2 {
b := make([]byte, len)
err := getcwd(&b[0], len)
if err == 0 {
i := 0;
for b[i] != 0 {
i++;
}
return string(b[0:i]), 0;
}
if err != ERANGE {
return "", err
}
}
}
//sysnb getgroups(size int, list *Gid_t) (nn int, errno int)
//getgroups(size int, list *Gid_t) int
func Getgroups() (gids []int, errno int) {
n, err := getgroups(0, nil)
if err != 0 {
return nil, errno
}
if n == 0 {
return nil, 0
}
// Sanity check group count. Max is 1<<16 on GNU/Linux.
if n < 0 || n > 1<<20 {
return nil, EINVAL
}
a := make([]Gid_t, n)
n, err = getgroups(n, &a[0])
if err != 0 {
return nil, errno
}
gids = make([]int, n)
for i, v := range a[0:n] {
gids[i] = int(v)
}
return
}
//sysnb setgroups(n int, list *Gid_t) (errno int)
//setgroups(n Size_t, list *Gid_t) int
func Setgroups(gids []int) (errno int) {
if len(gids) == 0 {
return setgroups(0, nil)
}
a := make([]Gid_t, len(gids))
for i, v := range gids {
a[i] = Gid_t(v)
}
return setgroups(len(a), &a[0])
}
type WaitStatus uint32
// The WaitStatus methods are implemented in C, to pick up the macros
// #defines in <sys/wait.h>.
func (w WaitStatus) Exited() bool
func (w WaitStatus) Signaled() bool
func (w WaitStatus) Stopped() bool
func (w WaitStatus) Continued() bool
func (w WaitStatus) CoreDump() bool
func (w WaitStatus) ExitStatus() int
func (w WaitStatus) Signal() int
func (w WaitStatus) StopSignal() int
func (w WaitStatus) TrapCause() int
//sys Mkfifo(path string, mode uint32) (errno int)
//mkfifo(path *byte, mode Mode_t) int
//sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, errno int)
//select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) int
const nfdbits = unsafe.Sizeof(fds_bits_type) * 8
type FdSet struct {
Bits [(FD_SETSIZE + nfdbits - 1) / nfdbits]fds_bits_type
}
func FDSet(fd int, set *FdSet) {
set.Bits[fd / nfdbits] |= (1 << (uint)(fd % nfdbits))
}
func FDClr(fd int, set *FdSet) {
set.Bits[fd / nfdbits] &^= (1 << (uint)(fd % nfdbits))
}
func FDIsSet(fd int, set *FdSet) bool {
if set.Bits[fd / nfdbits] & (1 << (uint)(fd % nfdbits)) != 0 {
return true
} else {
return false
}
}
func FDZero(set *FdSet) {
for i := range set.Bits {
set.Bits[i] = 0
}
}
//sys Access(path string, mode uint32) (errno int)
//access(path *byte, mode int) int
//sys Chdir(path string) (errno int)
//chdir(path *byte) int
//sys Chmod(path string, mode uint32) (errno int)
//chmod(path *byte, mode Mode_t) int
//sys Chown(path string, uid int, gid int) (errno int)
//chown(path *byte, uid Uid_t, gid Gid_t) int
//sys Chroot(path string) (errno int)
//chroot(path *byte) int
//sys Close(fd int) (errno int)
//close(fd int) int
//sys Creat(path string, mode uint32) (fd int, errno int)
//creat(path *byte, mode Mode_t) int
//sysnb Dup(oldfd int) (fd int, errno int)
//dup(oldfd int) int
//sysnb Dup2(oldfd int, newfd int) (fd int, errno int)
//dup2(oldfd int, newfd int) int
//sys Exit(code int)
//exit(code int)
//sys Fchdir(fd int) (errno int)
//fchdir(fd int) int
//sys Fchmod(fd int, mode uint32) (errno int)
//fchmod(fd int, mode Mode_t) int
//sys Fchown(fd int, uid int, gid int) (errno int)
//fchown(fd int, uid Uid_t, gid Gid_t) int
//sys fcntl(fd int, cmd int, arg int) (val int, errno int)
//fcntl(fd int, cmd int, arg int) int
//sys Fdatasync(fd int) (errno int)
//fdatasync(fd int) int
//sys Fsync(fd int) (errno int)
//fsync(fd int) int
//sysnb Getegid() (egid int)
//getegid() Gid_t
//sysnb Geteuid() (euid int)
//geteuid() Uid_t
//sysnb Getgid() (gid int)
//getgid() Gid_t
//sysnb Getpagesize() (pagesize int)
//getpagesize() int
//sysnb Getpgid(pid int) (pgid int, errno int)
//getpgid(pid Pid_t) Pid_t
//sysnb Getpgrp() (pid int)
//getpgrp() Pid_t
//sysnb Getpid() (pid int)
//getpid() Pid_t
//sysnb Getppid() (ppid int)
//getppid() Pid_t
// FIXME: mksysinfo Rlimit
// //sysnb Getrlimit(resource int, rlim *Rlimit) (errno int)
// //getrlimit(resource int, rlim *Rlimit) int
//sysnb Getrusage(who int, rusage *Rusage) (errno int)
//getrusage(who int, rusage *Rusage) int
//sysnb gettimeofday(tv *Timeval, tz *byte) (errno int)
//gettimeofday(tv *Timeval, tz *byte) int
func Gettimeofday(tv *Timeval) (errno int) {
return gettimeofday(tv, nil)
}
//sysnb Getuid() (uid int)
//getuid() Uid_t
//sysnb Kill(pid int, sig int) (errno int)
//kill(pid Pid_t, sig int) int
//sys Lchown(path string, uid int, gid int) (errno int)
//lchown(path *byte, uid Uid_t, gid Gid_t) int
//sys Link(oldpath string, newpath string) (errno int)
//link(oldpath *byte, newpath *byte) int
//sys Mkdir(path string, mode uint32) (errno int)
//mkdir(path *byte, mode Mode_t) int
//sys Mknod(path string, mode uint32, dev int) (errno int)
//mknod(path *byte, mode Mode_t, dev _dev_t) int
//sys Mount(source string, target string, fstype string, flags int, data string) (errno int)
//mount(source *byte, target *byte, fstype *byte, flags _C_long, data *byte) int
//sys Nanosleep(time *Timespec, leftover *Timespec) (errno int)
//nanosleep(time *Timespec, leftover *Timespec) int
//sys Pause() (errno int)
//pause() int
//sys Read(fd int, p []byte) (n int, errno int)
//read(fd int, buf *byte, count Size_t) Ssize_t
//sys Readlink(path string, buf []byte) (n int, errno int)
//readlink(path *byte, buf *byte, bufsiz Size_t) Ssize_t
//sys Rename(oldpath string, newpath string) (errno int)
//rename(oldpath *byte, newpath *byte) int
//sys Rmdir(path string) (errno int)
//rmdir(path *byte) int
//sys Setdomainname(p []byte) (errno int)
//setdomainname(name *byte, len Size_t) int
//sys Sethostname(p []byte) (errno int)
//sethostname(name *byte, len Size_t) int
//sysnb Setgid(gid int) (errno int)
//setgid(gid Gid_t) int
//sysnb Setregid(rgid int, egid int) (errno int)
//setregid(rgid Gid_t, egid Gid_t) int
//sysnb Setpgid(pid int, pgid int) (errno int)
//setpgid(pid Pid_t, pgid Pid_t) int
//sysnb Setreuid(ruid int, euid int) (errno int)
//setreuid(ruid Uid_t, euid Uid_t) int
// FIXME: mksysinfo Rlimit
// //sysnb Setrlimit(resource int, rlim *Rlimit) (errno int)
// //setrlimit(resource int, rlim *Rlimit) int
//sysnb Setsid() (pid int, errno int)
//setsid() Pid_t
//sysnb settimeofday(tv *Timeval, tz *byte) (errno int)
//settimeofday(tv *Timeval, tz *byte) int
func Settimeofday(tv *Timeval) (errno int) {
return settimeofday(tv, nil)
}
//sysnb Setuid(uid int) (errno int)
//setuid(uid Uid_t) int
//sys Symlink(oldpath string, newpath string) (errno int)
//symlink(oldpath *byte, newpath *byte) int
//sys Sync()
//sync()
// FIXME: mksysinfo Time_t
// //sysnb Time(t *Time_t) (tt Time_t, errno int)
// //time(t *Time_t) Time_t
// FIXME: mksysinfo Tms
// //sysnb Times(tms *Tms) (ticks uintptr, errno int)
// //times(tms *Tms) _clock_t
//sysnb Umask(mask int) (oldmask int)
//umask(mask Mode_t) Mode_t
//sys Unlink(path string) (errno int)
//unlink(path *byte) int
// FIXME: mksysinfo Utimbuf
// //sys Utime(path string, buf *Utimbuf) (errno int)
// //utime(path *byte, buf *Utimbuf) int
//sys Write(fd int, p []byte) (n int, errno int)
//write(fd int, buf *byte, count Size_t) Ssize_t
//sys munmap(addr uintptr, length uintptr) (errno int)
//munmap(addr *byte, length Size_t) int
//sys Madvise(b []byte, advice int) (errno int)
//madvise(addr *byte, len Size_t, advice int) int
//sys Mprotect(b []byte, prot int) (errno int)
//mprotect(addr *byte, len Size_t, prot int) int
//sys Mlock(b []byte) (errno int)
//mlock(addr *byte, len Size_t) int
//sys Munlock(b []byte) (errno int)
//munlock(addr *byte, len Size_t) int
//sys Mlockall(flags int) (errno int)
//mlockall(flags int) int
//sys Munlockall() (errno int)
//munlockall() int
func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) }
func NsecToTimespec(nsec int64) (ts Timespec) {
ts.Sec = Timespec_sec_t(nsec / 1e9)
ts.Nsec = Timespec_nsec_t(nsec % 1e9)
return
}
func TimevalToNsec(tv Timeval) int64 { return int64(tv.Sec)*1e9 + int64(tv.Usec)*1e3 }
func NsecToTimeval(nsec int64) (tv Timeval) {
nsec += 999 // round up to microsecond
tv.Sec = Timeval_sec_t(nsec / 1e9)
tv.Usec = Timeval_usec_t(nsec % 1e9 / 1e3)
return
}
|