1 // Copyright 2009 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 5 package syscall 6 7 import "unsafe" 8 9 const ( 10 _SYS_dup = SYS_DUP2 11 _SYS_getdents = SYS_GETDENTS64 12 _SYS_setgroups = SYS_SETGROUPS32 13 ) 14 15 func setTimespec(sec, nsec int64) Timespec { 16 return Timespec{Sec: int32(sec), Nsec: int32(nsec)} 17 } 18 19 func setTimeval(sec, usec int64) Timeval { 20 return Timeval{Sec: int32(sec), Usec: int32(usec)} 21 } 22 23 func Pipe(p []int) (err error) { 24 if len(p) != 2 { 25 return EINVAL 26 } 27 var pp [2]_C_int 28 err = pipe2(&pp, 0) 29 p[0] = int(pp[0]) 30 p[1] = int(pp[1]) 31 return 32 } 33 34 //sysnb pipe2(p *[2]_C_int, flags int) (err error) 35 36 func Pipe2(p []int, flags int) (err error) { 37 if len(p) != 2 { 38 return EINVAL 39 } 40 var pp [2]_C_int 41 err = pipe2(&pp, flags) 42 p[0] = int(pp[0]) 43 p[1] = int(pp[1]) 44 return 45 } 46 47 // Underlying system call writes to newoffset via pointer. 48 // Implemented in assembly to avoid allocation. 49 func seek(fd int, offset int64, whence int) (newoffset int64, err Errno) 50 51 func Seek(fd int, offset int64, whence int) (newoffset int64, err error) { 52 newoffset, errno := seek(fd, offset, whence) 53 if errno != 0 { 54 return 0, errno 55 } 56 return newoffset, nil 57 } 58 59 //sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) 60 //sys accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (fd int, err error) 61 //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) 62 //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) 63 //sysnb getgroups(n int, list *_Gid_t) (nn int, err error) = SYS_GETGROUPS32 64 //sysnb setgroups(n int, list *_Gid_t) (err error) = SYS_SETGROUPS32 65 //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) 66 //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) 67 //sysnb socket(domain int, typ int, proto int) (fd int, err error) 68 //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) 69 //sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) 70 //sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) 71 //sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) 72 //sysnb socketpair(domain int, typ int, flags int, fd *[2]int32) (err error) 73 //sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error) 74 //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error) 75 76 // 64-bit file system and 32-bit uid calls 77 // (16-bit uid calls are not always supported in newer kernels) 78 //sys Dup2(oldfd int, newfd int) (err error) 79 //sys Fchown(fd int, uid int, gid int) (err error) = SYS_FCHOWN32 80 //sys Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64 81 //sysnb Getegid() (egid int) = SYS_GETEGID32 82 //sysnb Geteuid() (euid int) = SYS_GETEUID32 83 //sysnb Getgid() (gid int) = SYS_GETGID32 84 //sysnb Getuid() (uid int) = SYS_GETUID32 85 //sysnb InotifyInit() (fd int, err error) 86 //sys Lchown(path string, uid int, gid int) (err error) = SYS_LCHOWN32 87 //sys Listen(s int, n int) (err error) 88 //sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64 89 //sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) = SYS_SENDFILE64 90 //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) = SYS__NEWSELECT 91 //sys Setfsgid(gid int) (err error) = SYS_SETFSGID32 92 //sys Setfsuid(uid int) (err error) = SYS_SETFSUID32 93 //sysnb Setregid(rgid int, egid int) (err error) = SYS_SETREGID32 94 //sysnb Setresgid(rgid int, egid int, sgid int) (err error) = SYS_SETRESGID32 95 //sysnb Setresuid(ruid int, euid int, suid int) (err error) = SYS_SETRESUID32 96 //sysnb Setreuid(ruid int, euid int) (err error) = SYS_SETREUID32 97 //sys Shutdown(fd int, how int) (err error) 98 //sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error) 99 //sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64 100 101 // Vsyscalls on amd64. 102 //sysnb Gettimeofday(tv *Timeval) (err error) 103 //sysnb Time(t *Time_t) (tt Time_t, err error) 104 105 //sys Pread(fd int, p []byte, offset int64) (n int, err error) = SYS_PREAD64 106 //sys Pwrite(fd int, p []byte, offset int64) (n int, err error) = SYS_PWRITE64 107 //sys Truncate(path string, length int64) (err error) = SYS_TRUNCATE64 108 //sys Ftruncate(fd int, length int64) (err error) = SYS_FTRUNCATE64 109 110 //sys mmap2(addr uintptr, length uintptr, prot int, flags int, fd int, pageOffset uintptr) (xaddr uintptr, err error) 111 112 func Fstatfs(fd int, buf *Statfs_t) (err error) { 113 _, _, e := Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf))) 114 if e != 0 { 115 err = e 116 } 117 return 118 } 119 120 func Statfs(path string, buf *Statfs_t) (err error) { 121 pathp, err := BytePtrFromString(path) 122 if err != nil { 123 return err 124 } 125 _, _, e := Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf))) 126 use(unsafe.Pointer(pathp)) 127 if e != 0 { 128 err = e 129 } 130 return 131 } 132 133 func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) { 134 page := uintptr(offset / 4096) 135 if offset != int64(page)*4096 { 136 return 0, EINVAL 137 } 138 return mmap2(addr, length, prot, flags, fd, page) 139 } 140 141 type rlimit32 struct { 142 Cur uint32 143 Max uint32 144 } 145 146 //sysnb getrlimit(resource int, rlim *rlimit32) (err error) = SYS_GETRLIMIT 147 148 const rlimInf32 = ^uint32(0) 149 const rlimInf64 = ^uint64(0) 150 151 func Getrlimit(resource int, rlim *Rlimit) (err error) { 152 err = prlimit(0, resource, nil, rlim) 153 if err != ENOSYS { 154 return err 155 } 156 157 rl := rlimit32{} 158 err = getrlimit(resource, &rl) 159 if err != nil { 160 return 161 } 162 163 if rl.Cur == rlimInf32 { 164 rlim.Cur = rlimInf64 165 } else { 166 rlim.Cur = uint64(rl.Cur) 167 } 168 169 if rl.Max == rlimInf32 { 170 rlim.Max = rlimInf64 171 } else { 172 rlim.Max = uint64(rl.Max) 173 } 174 return 175 } 176 177 //sysnb setrlimit(resource int, rlim *rlimit32) (err error) = SYS_SETRLIMIT 178 179 func Setrlimit(resource int, rlim *Rlimit) (err error) { 180 err = prlimit(0, resource, rlim, nil) 181 if err != ENOSYS { 182 return err 183 } 184 185 rl := rlimit32{} 186 if rlim.Cur == rlimInf64 { 187 rl.Cur = rlimInf32 188 } else if rlim.Cur < uint64(rlimInf32) { 189 rl.Cur = uint32(rlim.Cur) 190 } else { 191 return EINVAL 192 } 193 if rlim.Max == rlimInf64 { 194 rl.Max = rlimInf32 195 } else if rlim.Max < uint64(rlimInf32) { 196 rl.Max = uint32(rlim.Max) 197 } else { 198 return EINVAL 199 } 200 201 return setrlimit(resource, &rl) 202 } 203 204 func (r *PtraceRegs) PC() uint64 { return uint64(r.Uregs[15]) } 205 206 func (r *PtraceRegs) SetPC(pc uint64) { r.Uregs[15] = uint32(pc) } 207 208 func (iov *Iovec) SetLen(length int) { 209 iov.Len = uint32(length) 210 } 211 212 func (msghdr *Msghdr) SetControllen(length int) { 213 msghdr.Controllen = uint32(length) 214 } 215 216 func (cmsg *Cmsghdr) SetLen(length int) { 217 cmsg.Len = uint32(length) 218 } 219