1 // Copyright 2011 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.
5 // POSIX library calls.
6 // This file is compiled as ordinary Go code,
7 // but it is also input to mksyscall,
8 // which parses the //sys lines and generates library call stubs.
9 // Note that sometimes we use a lowercase //sys name and
10 // wrap it in our own nicer implementation.
20 //sysnb pipe(p *[2]_C_int) (err error)
21 //pipe(p *[2]_C_int) _C_int
22 func Pipe(p
[]int) (err error
) {
33 //sys utimes(path string, times *[2]Timeval) (err error)
34 //utimes(path *byte, times *[2]Timeval) _C_int
35 func Utimes(path
string, tv
[]Timeval
) (err error
) {
39 return utimes(path
, (*[2]Timeval
)(unsafe
.Pointer(&tv
[0])))
42 //sys getcwd(buf *byte, size Size_t) (err error)
43 //getcwd(buf *byte, size Size_t) *byte
45 const ImplementsGetwd
= true
47 func Getwd() (ret
string, err error
) {
48 for len := Size_t(4096); ; len *= 2 {
49 b
:= make([]byte, len)
50 err
:= getcwd(&b
[0], len)
56 return string(b
[0:i
]), nil
64 func Getcwd(buf
[]byte) (n
int, err error
) {
65 err
= getcwd(&buf
[0], Size_t(len(buf
)))
76 //sysnb getgroups(size int, list *Gid_t) (nn int, err error)
77 //getgroups(size _C_int, list *Gid_t) _C_int
79 func Getgroups() (gids
[]int, err error
) {
80 n
, err
:= getgroups(0, nil)
88 // Sanity check group count. Max is 1<<16 on GNU/Linux.
89 if n
< 0 || n
> 1<<20 {
94 n
, err
= getgroups(n
, &a
[0])
99 for i
, v
:= range a
[0:n
] {
105 //sysnb setgroups(n int, list *Gid_t) (err error)
106 //setgroups(n Size_t, list *Gid_t) _C_int
108 func Setgroups(gids
[]int) (err error
) {
110 return setgroups(0, nil)
113 a
:= make([]Gid_t
, len(gids
))
114 for i
, v
:= range gids
{
117 return setgroups(len(a
), &a
[0])
120 type WaitStatus
uint32
122 // The WaitStatus methods are implemented in C, to pick up the macros
123 // #defines in <sys/wait.h>.
125 func (w WaitStatus
) Exited() bool
126 func (w WaitStatus
) Signaled() bool
127 func (w WaitStatus
) Stopped() bool
128 func (w WaitStatus
) Continued() bool
129 func (w WaitStatus
) CoreDump() bool
130 func (w WaitStatus
) ExitStatus() int
131 func (w WaitStatus
) Signal() Signal
132 func (w WaitStatus
) StopSignal() Signal
133 func (w WaitStatus
) TrapCause() int
135 //sys Mkfifo(path string, mode uint32) (err error)
136 //mkfifo(path *byte, mode Mode_t) _C_int
138 //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
139 //select(nfd _C_int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) _C_int
141 const nfdbits
= int(unsafe
.Sizeof(fds_bits_type(0)) * 8)
144 Bits
[(FD_SETSIZE
+ nfdbits
- 1) / nfdbits
]fds_bits_type
147 func FDSet(fd
int, set
*FdSet
) {
148 set
.Bits
[fd
/nfdbits
] |
= (1 << (uint)(fd%nfdbits
))
151 func FDClr(fd
int, set
*FdSet
) {
152 set
.Bits
[fd
/nfdbits
] &^= (1 << (uint)(fd%nfdbits
))
155 func FDIsSet(fd
int, set
*FdSet
) bool {
156 if set
.Bits
[fd
/nfdbits
]&(1<<(uint)(fd%nfdbits
)) != 0 {
163 func FDZero(set
*FdSet
) {
164 for i
:= range set
.Bits
{
169 //sys Access(path string, mode uint32) (err error)
170 //access(path *byte, mode _C_int) _C_int
172 //sys Chdir(path string) (err error)
173 //chdir(path *byte) _C_int
175 //sys Chmod(path string, mode uint32) (err error)
176 //chmod(path *byte, mode Mode_t) _C_int
178 //sys Chown(path string, uid int, gid int) (err error)
179 //chown(path *byte, uid Uid_t, gid Gid_t) _C_int
181 //sys Chroot(path string) (err error)
182 //chroot(path *byte) _C_int
184 //sys Close(fd int) (err error)
185 //close(fd _C_int) _C_int
187 //sys Creat(path string, mode uint32) (fd int, err error)
188 //creat(path *byte, mode Mode_t) _C_int
190 //sysnb Dup(oldfd int) (fd int, err error)
191 //dup(oldfd _C_int) _C_int
193 //sysnb Dup2(oldfd int, newfd int) (err error)
194 //dup2(oldfd _C_int, newfd _C_int) _C_int
199 //sys Fchdir(fd int) (err error)
200 //fchdir(fd _C_int) _C_int
202 //sys Fchmod(fd int, mode uint32) (err error)
203 //fchmod(fd _C_int, mode Mode_t) _C_int
205 //sys Fchown(fd int, uid int, gid int) (err error)
206 //fchown(fd _C_int, uid Uid_t, gid Gid_t) _C_int
208 //sys fcntl(fd int, cmd int, arg int) (val int, err error)
209 //__go_fcntl(fd _C_int, cmd _C_int, arg _C_int) _C_int
211 //sys FcntlFlock(fd uintptr, cmd int, lk *Flock_t) (err error)
212 //__go_fcntl_flock(fd _C_int, cmd _C_int, arg *Flock_t) _C_int
214 //sys Fdatasync(fd int) (err error)
215 //fdatasync(fd _C_int) _C_int
217 //sys Fsync(fd int) (err error)
218 //fsync(fd _C_int) _C_int
220 //sysnb Getegid() (egid int)
223 //sysnb Geteuid() (euid int)
226 //sysnb Getgid() (gid int)
229 //sysnb Getpagesize() (pagesize int)
230 //getpagesize() _C_int
232 //sysnb Getpgid(pid int) (pgid int, err error)
233 //getpgid(pid Pid_t) Pid_t
235 //sysnb Getpgrp() (pid int)
238 //sysnb Getpid() (pid int)
241 //sysnb Getppid() (ppid int)
244 //sys Getpriority(which int, who int) (prio int, err error)
245 //getpriority(which _C_int, who _C_int) _C_int
247 //sysnb Getrlimit(resource int, rlim *Rlimit) (err error)
248 //getrlimit(resource _C_int, rlim *Rlimit) _C_int
250 //sysnb Getrusage(who int, rusage *Rusage) (err error)
251 //getrusage(who _C_int, rusage *Rusage) _C_int
253 //sysnb gettimeofday(tv *Timeval, tz *byte) (err error)
254 //gettimeofday(tv *Timeval, tz *byte) _C_int
255 func Gettimeofday(tv
*Timeval
) (err error
) {
256 return gettimeofday(tv
, nil)
259 //sysnb Getuid() (uid int)
262 //sysnb Kill(pid int, sig Signal) (err error)
263 //kill(pid Pid_t, sig _C_int) _C_int
265 //sys Lchown(path string, uid int, gid int) (err error)
266 //lchown(path *byte, uid Uid_t, gid Gid_t) _C_int
268 //sys Link(oldpath string, newpath string) (err error)
269 //link(oldpath *byte, newpath *byte) _C_int
271 //sys Mkdir(path string, mode uint32) (err error)
272 //mkdir(path *byte, mode Mode_t) _C_int
274 //sys Mknod(path string, mode uint32, dev int) (err error)
275 //mknod(path *byte, mode Mode_t, dev _dev_t) _C_int
277 //sys Mount(source string, target string, fstype string, flags uintptr, data string) (err error)
278 //mount(source *byte, target *byte, fstype *byte, flags _C_long, data *byte) _C_int
280 //sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
281 //nanosleep(time *Timespec, leftover *Timespec) _C_int
283 //sys Pause() (err error)
286 //sys read(fd int, p []byte) (n int, err error)
287 //read(fd _C_int, buf *byte, count Size_t) Ssize_t
289 //sys readlen(fd int, p *byte, np int) (n int, err error)
290 //read(fd _C_int, buf *byte, count Size_t) Ssize_t
292 //sys Readlink(path string, buf []byte) (n int, err error)
293 //readlink(path *byte, buf *byte, bufsiz Size_t) Ssize_t
295 //sys Rename(oldpath string, newpath string) (err error)
296 //rename(oldpath *byte, newpath *byte) _C_int
298 //sys Rmdir(path string) (err error)
299 //rmdir(path *byte) _C_int
301 //sys Setdomainname(p []byte) (err error)
302 //setdomainname(name *byte, len Size_t) _C_int
304 //sys Sethostname(p []byte) (err error)
305 //sethostname(name *byte, len Size_t) _C_int
307 //sysnb Setgid(gid int) (err error)
308 //setgid(gid Gid_t) _C_int
310 //sysnb Setregid(rgid int, egid int) (err error)
311 //setregid(rgid Gid_t, egid Gid_t) _C_int
313 //sysnb Setpgid(pid int, pgid int) (err error)
314 //setpgid(pid Pid_t, pgid Pid_t) _C_int
316 //sys Setpriority(which int, who int, prio int) (err error)
317 //setpriority(which _C_int, who _C_int, prio _C_int) _C_int
319 //sysnb Setreuid(ruid int, euid int) (err error)
320 //setreuid(ruid Uid_t, euid Uid_t) _C_int
322 //sysnb Setrlimit(resource int, rlim *Rlimit) (err error)
323 //setrlimit(resource int, rlim *Rlimit) _C_int
325 //sysnb Setsid() (pid int, err error)
328 //sysnb settimeofday(tv *Timeval, tz *byte) (err error)
329 //settimeofday(tv *Timeval, tz *byte) _C_int
331 func Settimeofday(tv
*Timeval
) (err error
) {
332 return settimeofday(tv
, nil)
335 //sysnb Setuid(uid int) (err error)
336 //setuid(uid Uid_t) _C_int
338 //sys Symlink(oldpath string, newpath string) (err error)
339 //symlink(oldpath *byte, newpath *byte) _C_int
344 //sysnb Time(t *Time_t) (tt Time_t, err error)
345 //time(t *Time_t) Time_t
347 //sysnb Times(tms *Tms) (ticks uintptr, err error)
348 //times(tms *Tms) _clock_t
350 //sysnb Umask(mask int) (oldmask int)
351 //umask(mask Mode_t) Mode_t
353 //sys Unlink(path string) (err error)
354 //unlink(path *byte) _C_int
356 //sys Utime(path string, buf *Utimbuf) (err error)
357 //utime(path *byte, buf *Utimbuf) _C_int
359 //sys write(fd int, p []byte) (n int, err error)
360 //write(fd _C_int, buf *byte, count Size_t) Ssize_t
362 //sys writelen(fd int, p *byte, np int) (n int, err error)
363 //write(fd _C_int, buf *byte, count Size_t) Ssize_t
365 //sys munmap(addr uintptr, length uintptr) (err error)
366 //munmap(addr *byte, length Size_t) _C_int
368 //sys Madvise(b []byte, advice int) (err error)
369 //madvise(addr *byte, len Size_t, advice _C_int) _C_int
371 //sys Mprotect(b []byte, prot int) (err error)
372 //mprotect(addr *byte, len Size_t, prot _C_int) _C_int
374 //sys Mlock(b []byte) (err error)
375 //mlock(addr *byte, len Size_t) _C_int
377 //sys Munlock(b []byte) (err error)
378 //munlock(addr *byte, len Size_t) _C_int
380 //sys Mlockall(flags int) (err error)
381 //mlockall(flags _C_int) _C_int
383 //sys Munlockall() (err error)
384 //munlockall() _C_int
386 func TimespecToNsec(ts Timespec
) int64 { return int64(ts
.Sec
)*1e9
+ int64(ts
.Nsec
) }
388 func NsecToTimespec(nsec
int64) (ts Timespec
) {
389 ts
.Sec
= Timespec_sec_t(nsec
/ 1e9
)
390 ts
.Nsec
= Timespec_nsec_t(nsec
% 1e9
)
394 func TimevalToNsec(tv Timeval
) int64 { return int64(tv
.Sec
)*1e9
+ int64(tv
.Usec
)*1e3
}
396 func NsecToTimeval(nsec
int64) (tv Timeval
) {
397 nsec
+= 999 // round up to microsecond
398 tv
.Sec
= Timeval_sec_t(nsec
/ 1e9
)
399 tv
.Usec
= Timeval_usec_t(nsec
% 1e9
/ 1e3
)
403 //sysnb Tcgetattr(fd int, p *Termios) (err error)
404 //tcgetattr(fd _C_int, p *Termios) _C_int
406 //sys Tcsetattr(fd int, actions int, p *Termios) (err error)
407 //tcsetattr(fd _C_int, actions _C_int, p *Termios) _C_int