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