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
380
381
382
383
384
|
// 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) (err error)
//pipe(p *[2]int) int
func Pipe(p []int) (err error) {
if len(p) != 2 {
return EINVAL
}
var pp [2]int
err = pipe(&pp)
p[0] = pp[0]
p[1] = pp[1]
return
}
//sys utimes(path string, times *[2]Timeval) (err error)
//utimes(path *byte, times *[2]Timeval) int
func Utimes(path string, tv []Timeval) (err error) {
if len(tv) != 2 {
return EINVAL
}
return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
}
//sys getcwd(buf *byte, size Size_t) (err error)
//getcwd(buf *byte, size Size_t) *byte
const ImplementsGetwd = true
func Getwd() (ret string, err error) {
for len := Size_t(4096); ; len *= 2 {
b := make([]byte, len)
err := getcwd(&b[0], len)
if err == nil {
i := 0
for b[i] != 0 {
i++
}
return string(b[0:i]), nil
}
if err != ERANGE {
return "", err
}
}
}
//sysnb getgroups(size int, list *Gid_t) (nn int, err error)
//getgroups(size int, list *Gid_t) int
func Getgroups() (gids []int, err error) {
n, err := getgroups(0, nil)
if err != nil {
return nil, err
}
if n == 0 {
return nil, nil
}
// 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 != nil {
return nil, err
}
gids = make([]int, n)
for i, v := range a[0:n] {
gids[i] = int(v)
}
return
}
//sysnb setgroups(n int, list *Gid_t) (err error)
//setgroups(n Size_t, list *Gid_t) int
func Setgroups(gids []int) (err error) {
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) (err error)
//mkfifo(path *byte, mode Mode_t) int
//sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
//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) (err error)
//access(path *byte, mode int) int
//sys Chdir(path string) (err error)
//chdir(path *byte) int
//sys Chmod(path string, mode uint32) (err error)
//chmod(path *byte, mode Mode_t) int
//sys Chown(path string, uid int, gid int) (err error)
//chown(path *byte, uid Uid_t, gid Gid_t) int
//sys Chroot(path string) (err error)
//chroot(path *byte) int
//sys Close(fd int) (err error)
//close(fd int) int
//sys Creat(path string, mode uint32) (fd int, err error)
//creat(path *byte, mode Mode_t) int
//sysnb Dup(oldfd int) (fd int, err error)
//dup(oldfd int) int
//sysnb Dup2(oldfd int, newfd int) (err error)
//dup2(oldfd int, newfd int) int
//sys Exit(code int)
//exit(code int)
//sys Fchdir(fd int) (err error)
//fchdir(fd int) int
//sys Fchmod(fd int, mode uint32) (err error)
//fchmod(fd int, mode Mode_t) int
//sys Fchown(fd int, uid int, gid int) (err error)
//fchown(fd int, uid Uid_t, gid Gid_t) int
//sys fcntl(fd int, cmd int, arg int) (val int, err error)
//fcntl(fd int, cmd int, arg int) int
//sys Fdatasync(fd int) (err error)
//fdatasync(fd int) int
//sys Fsync(fd int) (err error)
//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, err error)
//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) (err error)
// //getrlimit(resource int, rlim *Rlimit) int
//sysnb Getrusage(who int, rusage *Rusage) (err error)
//getrusage(who int, rusage *Rusage) int
//sysnb gettimeofday(tv *Timeval, tz *byte) (err error)
//gettimeofday(tv *Timeval, tz *byte) int
func Gettimeofday(tv *Timeval) (err error) {
return gettimeofday(tv, nil)
}
//sysnb Getuid() (uid int)
//getuid() Uid_t
//sysnb Kill(pid int, sig int) (err error)
//kill(pid Pid_t, sig int) int
//sys Lchown(path string, uid int, gid int) (err error)
//lchown(path *byte, uid Uid_t, gid Gid_t) int
//sys Link(oldpath string, newpath string) (err error)
//link(oldpath *byte, newpath *byte) int
//sys Mkdir(path string, mode uint32) (err error)
//mkdir(path *byte, mode Mode_t) int
//sys Mknod(path string, mode uint32, dev int) (err error)
//mknod(path *byte, mode Mode_t, dev _dev_t) int
//sys Mount(source string, target string, fstype string, flags int, data string) (err error)
//mount(source *byte, target *byte, fstype *byte, flags _C_long, data *byte) int
//sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
//nanosleep(time *Timespec, leftover *Timespec) int
//sys Pause() (err error)
//pause() int
//sys Read(fd int, p []byte) (n int, err error)
//read(fd int, buf *byte, count Size_t) Ssize_t
//sys Readlink(path string, buf []byte) (n int, err error)
//readlink(path *byte, buf *byte, bufsiz Size_t) Ssize_t
//sys Rename(oldpath string, newpath string) (err error)
//rename(oldpath *byte, newpath *byte) int
//sys Rmdir(path string) (err error)
//rmdir(path *byte) int
//sys Setdomainname(p []byte) (err error)
//setdomainname(name *byte, len Size_t) int
//sys Sethostname(p []byte) (err error)
//sethostname(name *byte, len Size_t) int
//sysnb Setgid(gid int) (err error)
//setgid(gid Gid_t) int
//sysnb Setregid(rgid int, egid int) (err error)
//setregid(rgid Gid_t, egid Gid_t) int
//sysnb Setpgid(pid int, pgid int) (err error)
//setpgid(pid Pid_t, pgid Pid_t) int
//sysnb Setreuid(ruid int, euid int) (err error)
//setreuid(ruid Uid_t, euid Uid_t) int
// FIXME: mksysinfo Rlimit
// //sysnb Setrlimit(resource int, rlim *Rlimit) (err error)
// //setrlimit(resource int, rlim *Rlimit) int
//sysnb Setsid() (pid int, err error)
//setsid() Pid_t
//sysnb settimeofday(tv *Timeval, tz *byte) (err error)
//settimeofday(tv *Timeval, tz *byte) int
func Settimeofday(tv *Timeval) (err error) {
return settimeofday(tv, nil)
}
//sysnb Setuid(uid int) (err error)
//setuid(uid Uid_t) int
//sys Symlink(oldpath string, newpath string) (err error)
//symlink(oldpath *byte, newpath *byte) int
//sys Sync()
//sync()
// FIXME: mksysinfo Time_t
// //sysnb Time(t *Time_t) (tt Time_t, err error)
// //time(t *Time_t) Time_t
//sysnb Times(tms *Tms) (ticks uintptr, err error)
//times(tms *Tms) _clock_t
//sysnb Umask(mask int) (oldmask int)
//umask(mask Mode_t) Mode_t
//sys Unlink(path string) (err error)
//unlink(path *byte) int
// FIXME: mksysinfo Utimbuf
// //sys Utime(path string, buf *Utimbuf) (err error)
// //utime(path *byte, buf *Utimbuf) int
//sys Write(fd int, p []byte) (n int, err error)
//write(fd int, buf *byte, count Size_t) Ssize_t
//sys munmap(addr uintptr, length uintptr) (err error)
//munmap(addr *byte, length Size_t) int
//sys Madvise(b []byte, advice int) (err error)
//madvise(addr *byte, len Size_t, advice int) int
//sys Mprotect(b []byte, prot int) (err error)
//mprotect(addr *byte, len Size_t, prot int) int
//sys Mlock(b []byte) (err error)
//mlock(addr *byte, len Size_t) int
//sys Munlock(b []byte) (err error)
//munlock(addr *byte, len Size_t) int
//sys Mlockall(flags int) (err error)
//mlockall(flags int) int
//sys Munlockall() (err error)
//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
}
//sysnb Tcgetattr(fd int, p *Termios) (err error)
//tcgetattr(fd int, p *Termios) int
//sys Tcsetattr(fd int, actions int, p *Termios) (err error)
//tcsetattr(fd int, actions int, p *Termios) int
|