Source file
src/syscall/syscall_bsd.go
1
2
3
4
5
6
7
8
9
10
11
12
13 package syscall
14
15 import (
16 "runtime"
17 "unsafe"
18 )
19
20 const ImplementsGetwd = true
21
22 func Getwd() (string, error) {
23 var buf [pathMax]byte
24 _, err := getcwd(buf[:])
25 if err != nil {
26 return "", err
27 }
28 n := clen(buf[:])
29 if n < 1 {
30 return "", EINVAL
31 }
32 return string(buf[:n]), nil
33 }
34
35
38
39
40
41
42 func Getgroups() (gids []int, err error) {
43 n, err := getgroups(0, nil)
44 if err != nil {
45 return nil, err
46 }
47 if n == 0 {
48 return nil, nil
49 }
50
51
52 if n < 0 || n > 1000 {
53 return nil, EINVAL
54 }
55
56 a := make([]_Gid_t, n)
57 n, err = getgroups(n, &a[0])
58 if err != nil {
59 return nil, err
60 }
61 gids = make([]int, n)
62 for i, v := range a[0:n] {
63 gids[i] = int(v)
64 }
65 return
66 }
67
68 func Setgroups(gids []int) (err error) {
69 if len(gids) == 0 {
70 return setgroups(0, nil)
71 }
72
73 a := make([]_Gid_t, len(gids))
74 for i, v := range gids {
75 a[i] = _Gid_t(v)
76 }
77 return setgroups(len(a), &a[0])
78 }
79
80 func ReadDirent(fd int, buf []byte) (n int, err error) {
81
82
83
84
85 var base = (*uintptr)(unsafe.Pointer(new(uint64)))
86 return Getdirentries(fd, buf, base)
87 }
88
89
90
91
92
93
94
95 type WaitStatus uint32
96
97 const (
98 mask = 0x7F
99 core = 0x80
100 shift = 8
101
102 exited = 0
103 stopped = 0x7F
104 )
105
106 func (w WaitStatus) Exited() bool { return w&mask == exited }
107
108 func (w WaitStatus) ExitStatus() int {
109 if w&mask != exited {
110 return -1
111 }
112 return int(w >> shift)
113 }
114
115 func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != 0 }
116
117 func (w WaitStatus) Signal() Signal {
118 sig := Signal(w & mask)
119 if sig == stopped || sig == 0 {
120 return -1
121 }
122 return sig
123 }
124
125 func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
126
127 func (w WaitStatus) Stopped() bool { return w&mask == stopped && Signal(w>>shift) != SIGSTOP }
128
129 func (w WaitStatus) Continued() bool { return w&mask == stopped && Signal(w>>shift) == SIGSTOP }
130
131 func (w WaitStatus) StopSignal() Signal {
132 if !w.Stopped() {
133 return -1
134 }
135 return Signal(w>>shift) & 0xFF
136 }
137
138 func (w WaitStatus) TrapCause() int { return -1 }
139
140
141
142 func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
143 var status _C_int
144 wpid, err = wait4(pid, &status, options, rusage)
145 if wstatus != nil {
146 *wstatus = WaitStatus(status)
147 }
148 return
149 }
150
151
152
153
154
155
156
157
158
159
160
161 func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
162 if sa.Port < 0 || sa.Port > 0xFFFF {
163 return nil, 0, EINVAL
164 }
165 sa.raw.Len = SizeofSockaddrInet4
166 sa.raw.Family = AF_INET
167 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
168 p[0] = byte(sa.Port >> 8)
169 p[1] = byte(sa.Port)
170 sa.raw.Addr = sa.Addr
171 return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
172 }
173
174 func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
175 if sa.Port < 0 || sa.Port > 0xFFFF {
176 return nil, 0, EINVAL
177 }
178 sa.raw.Len = SizeofSockaddrInet6
179 sa.raw.Family = AF_INET6
180 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
181 p[0] = byte(sa.Port >> 8)
182 p[1] = byte(sa.Port)
183 sa.raw.Scope_id = sa.ZoneId
184 sa.raw.Addr = sa.Addr
185 return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
186 }
187
188 func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
189 name := sa.Name
190 n := len(name)
191 if n >= len(sa.raw.Path) || n == 0 {
192 return nil, 0, EINVAL
193 }
194 sa.raw.Len = byte(3 + n)
195 sa.raw.Family = AF_UNIX
196 for i := 0; i < n; i++ {
197 sa.raw.Path[i] = int8(name[i])
198 }
199 return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
200 }
201
202 func (sa *SockaddrDatalink) sockaddr() (unsafe.Pointer, _Socklen, error) {
203 if sa.Index == 0 {
204 return nil, 0, EINVAL
205 }
206 sa.raw.Len = sa.Len
207 sa.raw.Family = AF_LINK
208 sa.raw.Index = sa.Index
209 sa.raw.Type = sa.Type
210 sa.raw.Nlen = sa.Nlen
211 sa.raw.Alen = sa.Alen
212 sa.raw.Slen = sa.Slen
213 sa.raw.Data = sa.Data
214 return unsafe.Pointer(&sa.raw), SizeofSockaddrDatalink, nil
215 }
216
217 func anyToSockaddr(rsa *RawSockaddrAny) (Sockaddr, error) {
218 switch rsa.Addr.Family {
219 case AF_LINK:
220 pp := (*RawSockaddrDatalink)(unsafe.Pointer(rsa))
221 sa := new(SockaddrDatalink)
222 sa.Len = pp.Len
223 sa.Family = pp.Family
224 sa.Index = pp.Index
225 sa.Type = pp.Type
226 sa.Nlen = pp.Nlen
227 sa.Alen = pp.Alen
228 sa.Slen = pp.Slen
229 sa.Data = pp.Data
230 return sa, nil
231
232 case AF_UNIX:
233 pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
234 if pp.Len < 2 || pp.Len > SizeofSockaddrUnix {
235 return nil, EINVAL
236 }
237 sa := new(SockaddrUnix)
238
239
240
241
242
243 n := int(pp.Len) - 2
244 for i := 0; i < n; i++ {
245 if pp.Path[i] == 0 {
246
247
248 n = i
249 break
250 }
251 }
252 bytes := (*[len(pp.Path)]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
253 sa.Name = string(bytes)
254 return sa, nil
255
256 case AF_INET:
257 pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
258 sa := new(SockaddrInet4)
259 p := (*[2]byte)(unsafe.Pointer(&pp.Port))
260 sa.Port = int(p[0])<<8 + int(p[1])
261 sa.Addr = pp.Addr
262 return sa, nil
263
264 case AF_INET6:
265 pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
266 sa := new(SockaddrInet6)
267 p := (*[2]byte)(unsafe.Pointer(&pp.Port))
268 sa.Port = int(p[0])<<8 + int(p[1])
269 sa.ZoneId = pp.Scope_id
270 sa.Addr = pp.Addr
271 return sa, nil
272 }
273 return nil, EAFNOSUPPORT
274 }
275
276 func Accept(fd int) (nfd int, sa Sockaddr, err error) {
277 var rsa RawSockaddrAny
278 var len _Socklen = SizeofSockaddrAny
279 nfd, err = accept(fd, &rsa, &len)
280 if err != nil {
281 return
282 }
283 if (runtime.GOOS == "darwin" || runtime.GOOS == "ios") && len == 0 {
284
285
286
287
288 Close(nfd)
289 return 0, nil, ECONNABORTED
290 }
291 sa, err = anyToSockaddr(&rsa)
292 if err != nil {
293 Close(nfd)
294 nfd = 0
295 }
296 return
297 }
298
299 func Getsockname(fd int) (sa Sockaddr, err error) {
300 var rsa RawSockaddrAny
301 var len _Socklen = SizeofSockaddrAny
302 if err = getsockname(fd, &rsa, &len); err != nil {
303 return
304 }
305
306
307 if runtime.GOOS == "dragonfly" && rsa.Addr.Family == AF_UNSPEC && rsa.Addr.Len == 0 {
308 rsa.Addr.Family = AF_UNIX
309 rsa.Addr.Len = SizeofSockaddrUnix
310 }
311 return anyToSockaddr(&rsa)
312 }
313
314
315
316 func GetsockoptByte(fd, level, opt int) (value byte, err error) {
317 var n byte
318 vallen := _Socklen(1)
319 err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
320 return n, err
321 }
322
323 func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
324 vallen := _Socklen(4)
325 err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
326 return value, err
327 }
328
329 func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
330 var value IPMreq
331 vallen := _Socklen(SizeofIPMreq)
332 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
333 return &value, err
334 }
335
336 func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
337 var value IPv6Mreq
338 vallen := _Socklen(SizeofIPv6Mreq)
339 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
340 return &value, err
341 }
342
343 func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) {
344 var value IPv6MTUInfo
345 vallen := _Socklen(SizeofIPv6MTUInfo)
346 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
347 return &value, err
348 }
349
350 func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) {
351 var value ICMPv6Filter
352 vallen := _Socklen(SizeofICMPv6Filter)
353 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
354 return &value, err
355 }
356
357
358
359
360
361 func recvmsgRaw(fd int, p, oob []byte, flags int, rsa *RawSockaddrAny) (n, oobn int, recvflags int, err error) {
362 var msg Msghdr
363 msg.Name = (*byte)(unsafe.Pointer(rsa))
364 msg.Namelen = uint32(SizeofSockaddrAny)
365 var iov Iovec
366 if len(p) > 0 {
367 iov.Base = (*byte)(unsafe.Pointer(&p[0]))
368 iov.SetLen(len(p))
369 }
370 var dummy byte
371 if len(oob) > 0 {
372
373 if len(p) == 0 {
374 iov.Base = &dummy
375 iov.SetLen(1)
376 }
377 msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
378 msg.SetControllen(len(oob))
379 }
380 msg.Iov = &iov
381 msg.Iovlen = 1
382 if n, err = recvmsg(fd, &msg, flags); err != nil {
383 return
384 }
385 oobn = int(msg.Controllen)
386 recvflags = int(msg.Flags)
387 return
388 }
389
390
391
392 func sendmsgN(fd int, p, oob []byte, ptr unsafe.Pointer, salen _Socklen, flags int) (n int, err error) {
393 var msg Msghdr
394 msg.Name = (*byte)(unsafe.Pointer(ptr))
395 msg.Namelen = uint32(salen)
396 var iov Iovec
397 if len(p) > 0 {
398 iov.Base = (*byte)(unsafe.Pointer(&p[0]))
399 iov.SetLen(len(p))
400 }
401 var dummy byte
402 if len(oob) > 0 {
403
404 if len(p) == 0 {
405 iov.Base = &dummy
406 iov.SetLen(1)
407 }
408 msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
409 msg.SetControllen(len(oob))
410 }
411 msg.Iov = &iov
412 msg.Iovlen = 1
413 if n, err = sendmsg(fd, &msg, flags); err != nil {
414 return 0, err
415 }
416 if len(oob) > 0 && len(p) == 0 {
417 n = 0
418 }
419 return n, nil
420 }
421
422
423
424 func Kevent(kq int, changes, events []Kevent_t, timeout *Timespec) (n int, err error) {
425 var change, event unsafe.Pointer
426 if len(changes) > 0 {
427 change = unsafe.Pointer(&changes[0])
428 }
429 if len(events) > 0 {
430 event = unsafe.Pointer(&events[0])
431 }
432 return kevent(kq, change, len(changes), event, len(events), timeout)
433 }
434
435 func Sysctl(name string) (value string, err error) {
436
437 mib, err := nametomib(name)
438 if err != nil {
439 return "", err
440 }
441
442
443 n := uintptr(0)
444 if err = sysctl(mib, nil, &n, nil, 0); err != nil {
445 return "", err
446 }
447 if n == 0 {
448 return "", nil
449 }
450
451
452 buf := make([]byte, n)
453 if err = sysctl(mib, &buf[0], &n, nil, 0); err != nil {
454 return "", err
455 }
456
457
458 if n > 0 && buf[n-1] == '\x00' {
459 n--
460 }
461 return string(buf[0:n]), nil
462 }
463
464 func SysctlUint32(name string) (value uint32, err error) {
465
466 mib, err := nametomib(name)
467 if err != nil {
468 return 0, err
469 }
470
471
472 n := uintptr(4)
473 buf := make([]byte, 4)
474 if err = sysctl(mib, &buf[0], &n, nil, 0); err != nil {
475 return 0, err
476 }
477 if n != 4 {
478 return 0, EIO
479 }
480 return *(*uint32)(unsafe.Pointer(&buf[0])), nil
481 }
482
483
484
485 func Utimes(path string, tv []Timeval) (err error) {
486 if len(tv) != 2 {
487 return EINVAL
488 }
489 return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
490 }
491
492 func UtimesNano(path string, ts []Timespec) error {
493 if len(ts) != 2 {
494 return EINVAL
495 }
496 err := utimensat(_AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
497 if err != ENOSYS {
498 return err
499 }
500
501
502 tv := [2]Timeval{
503 NsecToTimeval(TimespecToNsec(ts[0])),
504 NsecToTimeval(TimespecToNsec(ts[1])),
505 }
506 return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
507 }
508
509
510
511 func Futimes(fd int, tv []Timeval) (err error) {
512 if len(tv) != 2 {
513 return EINVAL
514 }
515 return futimes(fd, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
516 }
517
518
519
520 var mapper = &mmapper{
521 active: make(map[*byte][]byte),
522 mmap: mmap,
523 munmap: munmap,
524 }
525
526 func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
527 return mapper.Mmap(fd, offset, length, prot, flags)
528 }
529
530 func Munmap(b []byte) (err error) {
531 return mapper.Munmap(b)
532 }
533
View as plain text