github.com/gogf/gkafka@v1.0.1-0.20190702070843-033a14468069/third/golang.org/x/sys/unix/syscall_unix.go (about) 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 // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris 6 7 package unix 8 9 import ( 10 "bytes" 11 "sort" 12 "sync" 13 "syscall" 14 "unsafe" 15 ) 16 17 var ( 18 Stdin = 0 19 Stdout = 1 20 Stderr = 2 21 ) 22 23 // Do the interface allocations only once for common 24 // Errno values. 25 var ( 26 errEAGAIN error = syscall.EAGAIN 27 errEINVAL error = syscall.EINVAL 28 errENOENT error = syscall.ENOENT 29 ) 30 31 // errnoErr returns common boxed Errno values, to prevent 32 // allocations at runtime. 33 func errnoErr(e syscall.Errno) error { 34 switch e { 35 case 0: 36 return nil 37 case EAGAIN: 38 return errEAGAIN 39 case EINVAL: 40 return errEINVAL 41 case ENOENT: 42 return errENOENT 43 } 44 return e 45 } 46 47 // ErrnoName returns the error name for error number e. 48 func ErrnoName(e syscall.Errno) string { 49 i := sort.Search(len(errorList), func(i int) bool { 50 return errorList[i].num >= e 51 }) 52 if i < len(errorList) && errorList[i].num == e { 53 return errorList[i].name 54 } 55 return "" 56 } 57 58 // SignalName returns the signal name for signal number s. 59 func SignalName(s syscall.Signal) string { 60 i := sort.Search(len(signalList), func(i int) bool { 61 return signalList[i].num >= s 62 }) 63 if i < len(signalList) && signalList[i].num == s { 64 return signalList[i].name 65 } 66 return "" 67 } 68 69 // clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte. 70 func clen(n []byte) int { 71 i := bytes.IndexByte(n, 0) 72 if i == -1 { 73 i = len(n) 74 } 75 return i 76 } 77 78 // Mmap manager, for use by operating system-specific implementations. 79 80 type mmapper struct { 81 sync.Mutex 82 active map[*byte][]byte // active mappings; key is last byte in mapping 83 mmap func(addr, length uintptr, prot, flags, fd int, offset int64) (uintptr, error) 84 munmap func(addr uintptr, length uintptr) error 85 } 86 87 func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) { 88 if length <= 0 { 89 return nil, EINVAL 90 } 91 92 // Map the requested memory. 93 addr, errno := m.mmap(0, uintptr(length), prot, flags, fd, offset) 94 if errno != nil { 95 return nil, errno 96 } 97 98 // Slice memory layout 99 var sl = struct { 100 addr uintptr 101 len int 102 cap int 103 }{addr, length, length} 104 105 // Use unsafe to turn sl into a []byte. 106 b := *(*[]byte)(unsafe.Pointer(&sl)) 107 108 // Register mapping in m and return it. 109 p := &b[cap(b)-1] 110 m.Lock() 111 defer m.Unlock() 112 m.active[p] = b 113 return b, nil 114 } 115 116 func (m *mmapper) Munmap(data []byte) (err error) { 117 if len(data) == 0 || len(data) != cap(data) { 118 return EINVAL 119 } 120 121 // Find the base of the mapping. 122 p := &data[cap(data)-1] 123 m.Lock() 124 defer m.Unlock() 125 b := m.active[p] 126 if b == nil || &b[0] != &data[0] { 127 return EINVAL 128 } 129 130 // Unmap the memory and update m. 131 if errno := m.munmap(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))); errno != nil { 132 return errno 133 } 134 delete(m.active, p) 135 return nil 136 } 137 138 func Read(fd int, p []byte) (n int, err error) { 139 n, err = read(fd, p) 140 if raceenabled { 141 if n > 0 { 142 raceWriteRange(unsafe.Pointer(&p[0]), n) 143 } 144 if err == nil { 145 raceAcquire(unsafe.Pointer(&ioSync)) 146 } 147 } 148 return 149 } 150 151 func Write(fd int, p []byte) (n int, err error) { 152 if raceenabled { 153 raceReleaseMerge(unsafe.Pointer(&ioSync)) 154 } 155 n, err = write(fd, p) 156 if raceenabled && n > 0 { 157 raceReadRange(unsafe.Pointer(&p[0]), n) 158 } 159 return 160 } 161 162 // For testing: clients can set this flag to force 163 // creation of IPv6 sockets to return EAFNOSUPPORT. 164 var SocketDisableIPv6 bool 165 166 // Sockaddr represents a socket address. 167 type Sockaddr interface { 168 sockaddr() (ptr unsafe.Pointer, len _Socklen, err error) // lowercase; only we can define Sockaddrs 169 } 170 171 // SockaddrInet4 implements the Sockaddr interface for AF_INET type sockets. 172 type SockaddrInet4 struct { 173 Port int 174 Addr [4]byte 175 raw RawSockaddrInet4 176 } 177 178 // SockaddrInet6 implements the Sockaddr interface for AF_INET6 type sockets. 179 type SockaddrInet6 struct { 180 Port int 181 ZoneId uint32 182 Addr [16]byte 183 raw RawSockaddrInet6 184 } 185 186 // SockaddrUnix implements the Sockaddr interface for AF_UNIX type sockets. 187 type SockaddrUnix struct { 188 Name string 189 raw RawSockaddrUnix 190 } 191 192 func Bind(fd int, sa Sockaddr) (err error) { 193 ptr, n, err := sa.sockaddr() 194 if err != nil { 195 return err 196 } 197 return bind(fd, ptr, n) 198 } 199 200 func Connect(fd int, sa Sockaddr) (err error) { 201 ptr, n, err := sa.sockaddr() 202 if err != nil { 203 return err 204 } 205 return connect(fd, ptr, n) 206 } 207 208 func Getpeername(fd int) (sa Sockaddr, err error) { 209 var rsa RawSockaddrAny 210 var len _Socklen = SizeofSockaddrAny 211 if err = getpeername(fd, &rsa, &len); err != nil { 212 return 213 } 214 return anyToSockaddr(fd, &rsa) 215 } 216 217 func GetsockoptByte(fd, level, opt int) (value byte, err error) { 218 var n byte 219 vallen := _Socklen(1) 220 err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen) 221 return n, err 222 } 223 224 func GetsockoptInt(fd, level, opt int) (value int, err error) { 225 var n int32 226 vallen := _Socklen(4) 227 err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen) 228 return int(n), err 229 } 230 231 func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) { 232 vallen := _Socklen(4) 233 err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen) 234 return value, err 235 } 236 237 func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) { 238 var value IPMreq 239 vallen := _Socklen(SizeofIPMreq) 240 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) 241 return &value, err 242 } 243 244 func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) { 245 var value IPv6Mreq 246 vallen := _Socklen(SizeofIPv6Mreq) 247 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) 248 return &value, err 249 } 250 251 func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) { 252 var value IPv6MTUInfo 253 vallen := _Socklen(SizeofIPv6MTUInfo) 254 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) 255 return &value, err 256 } 257 258 func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) { 259 var value ICMPv6Filter 260 vallen := _Socklen(SizeofICMPv6Filter) 261 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) 262 return &value, err 263 } 264 265 func GetsockoptLinger(fd, level, opt int) (*Linger, error) { 266 var linger Linger 267 vallen := _Socklen(SizeofLinger) 268 err := getsockopt(fd, level, opt, unsafe.Pointer(&linger), &vallen) 269 return &linger, err 270 } 271 272 func GetsockoptTimeval(fd, level, opt int) (*Timeval, error) { 273 var tv Timeval 274 vallen := _Socklen(unsafe.Sizeof(tv)) 275 err := getsockopt(fd, level, opt, unsafe.Pointer(&tv), &vallen) 276 return &tv, err 277 } 278 279 func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) { 280 var rsa RawSockaddrAny 281 var len _Socklen = SizeofSockaddrAny 282 if n, err = recvfrom(fd, p, flags, &rsa, &len); err != nil { 283 return 284 } 285 if rsa.Addr.Family != AF_UNSPEC { 286 from, err = anyToSockaddr(fd, &rsa) 287 } 288 return 289 } 290 291 func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) { 292 ptr, n, err := to.sockaddr() 293 if err != nil { 294 return err 295 } 296 return sendto(fd, p, flags, ptr, n) 297 } 298 299 func SetsockoptByte(fd, level, opt int, value byte) (err error) { 300 return setsockopt(fd, level, opt, unsafe.Pointer(&value), 1) 301 } 302 303 func SetsockoptInt(fd, level, opt int, value int) (err error) { 304 var n = int32(value) 305 return setsockopt(fd, level, opt, unsafe.Pointer(&n), 4) 306 } 307 308 func SetsockoptInet4Addr(fd, level, opt int, value [4]byte) (err error) { 309 return setsockopt(fd, level, opt, unsafe.Pointer(&value[0]), 4) 310 } 311 312 func SetsockoptIPMreq(fd, level, opt int, mreq *IPMreq) (err error) { 313 return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPMreq) 314 } 315 316 func SetsockoptIPv6Mreq(fd, level, opt int, mreq *IPv6Mreq) (err error) { 317 return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPv6Mreq) 318 } 319 320 func SetsockoptICMPv6Filter(fd, level, opt int, filter *ICMPv6Filter) error { 321 return setsockopt(fd, level, opt, unsafe.Pointer(filter), SizeofICMPv6Filter) 322 } 323 324 func SetsockoptLinger(fd, level, opt int, l *Linger) (err error) { 325 return setsockopt(fd, level, opt, unsafe.Pointer(l), SizeofLinger) 326 } 327 328 func SetsockoptString(fd, level, opt int, s string) (err error) { 329 return setsockopt(fd, level, opt, unsafe.Pointer(&[]byte(s)[0]), uintptr(len(s))) 330 } 331 332 func SetsockoptTimeval(fd, level, opt int, tv *Timeval) (err error) { 333 return setsockopt(fd, level, opt, unsafe.Pointer(tv), unsafe.Sizeof(*tv)) 334 } 335 336 func Socket(domain, typ, proto int) (fd int, err error) { 337 if domain == AF_INET6 && SocketDisableIPv6 { 338 return -1, EAFNOSUPPORT 339 } 340 fd, err = socket(domain, typ, proto) 341 return 342 } 343 344 func Socketpair(domain, typ, proto int) (fd [2]int, err error) { 345 var fdx [2]int32 346 err = socketpair(domain, typ, proto, &fdx) 347 if err == nil { 348 fd[0] = int(fdx[0]) 349 fd[1] = int(fdx[1]) 350 } 351 return 352 } 353 354 var ioSync int64 355 356 func CloseOnExec(fd int) { fcntl(fd, F_SETFD, FD_CLOEXEC) } 357 358 func SetNonblock(fd int, nonblocking bool) (err error) { 359 flag, err := fcntl(fd, F_GETFL, 0) 360 if err != nil { 361 return err 362 } 363 if nonblocking { 364 flag |= O_NONBLOCK 365 } else { 366 flag &= ^O_NONBLOCK 367 } 368 _, err = fcntl(fd, F_SETFL, flag) 369 return err 370 } 371 372 // Exec calls execve(2), which replaces the calling executable in the process 373 // tree. argv0 should be the full path to an executable ("/bin/ls") and the 374 // executable name should also be the first argument in argv (["ls", "-l"]). 375 // envv are the environment variables that should be passed to the new 376 // process (["USER=go", "PWD=/tmp"]). 377 func Exec(argv0 string, argv []string, envv []string) error { 378 return syscall.Exec(argv0, argv, envv) 379 }