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