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