| // Copyright 2016 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| // +build fuchsia |
| |
| package zx |
| |
| import ( |
| "sync/atomic" |
| "unsafe" |
| ) |
| |
| // Process-wide FDIO handles. |
| var ( |
| StdioHandles [3]Handle // in/out/err |
| StdioHandleTypes [3]int |
| CwdHandle Handle |
| ProcHandle Handle |
| VMARRoot VMAR |
| RootNSMap map[string]Handle |
| ) |
| |
| const ZX_CPRNG_DRAW_MAX_LEN = 256 |
| |
| type HandleInfo uint32 |
| |
| func set_stdio_handle(n int, h uint32, t uint32) { |
| StdioHandles[n] = Handle(h) |
| StdioHandleTypes[n] = int(t) |
| } |
| |
| func set_proc_handle(h uint32) { |
| ProcHandle = Handle(h) |
| } |
| |
| func set_vmar_root(h uint32) { |
| VMARRoot = VMAR(h) |
| } |
| |
| func set_namespace(ns map[string]uint32) { |
| if RootNSMap == nil { |
| RootNSMap = make(map[string]Handle) |
| } |
| |
| for k, v := range ns { |
| RootNSMap[k] = Handle(v) |
| } |
| } |
| |
| func NewHandleInfo(t, arg uint32) HandleInfo { |
| return HandleInfo((t & 0xFFFF) | ((arg & 0xFFFF) << 16)) |
| } |
| |
| func (hi HandleInfo) Type() uint32 { |
| return uint32(hi) & 0xFFFF |
| } |
| |
| func (hi HandleInfo) Arg() uint32 { |
| return (uint32(hi) >> 16) & 0xFFFF |
| } |
| |
| type Socket Handle |
| |
| func (s *Socket) Handle() *Handle { |
| return (*Handle)(s) |
| } |
| |
| func bytesPtr(data []byte) unsafe.Pointer { |
| if len(data) > 0 { |
| return unsafe.Pointer(&data[0]) |
| } |
| return nil |
| } |
| |
| func (s *Socket) Read(data []byte, flags uint32) (int, error) { |
| var actual uint |
| status := sys_socket_read(Handle(*s), flags, bytesPtr(data), uint(len(data)), &actual) |
| if status != ErrOk { |
| return int(actual), &Error{Status: status, Text: "zx.Socket.Read"} |
| } |
| return int(actual), nil |
| } |
| |
| func (s *Socket) Write(data []byte, flags uint32) (int, error) { |
| var actual uint |
| status := sys_socket_write(Handle(*s), flags, bytesPtr(data), uint(len(data)), &actual) |
| if status != ErrOk { |
| return int(actual), &Error{Status: status, Text: "zx.Socket.Write"} |
| } |
| return int(actual), nil |
| } |
| |
| func (s *Socket) Close() error { |
| return s.Handle().Close() |
| } |
| |
| func (s *Socket) Shutdown(flags uint32) error { |
| status := Sys_socket_shutdown(Handle(*s), flags) |
| if status != ErrOk { |
| return &Error{Status: status, Text: "zx.Socket.Shutdown"} |
| } |
| return nil |
| } |
| |
| func NewSocket(flags uint32) (Socket, Socket, error) { |
| var h0, h1 Handle |
| if status := sys_socket_create(flags, &h0, &h1); status != ErrOk { |
| return Socket(HandleInvalid), Socket(HandleInvalid), &Error{Status: status, Text: "zx.Socket"} |
| } |
| return Socket(h0), Socket(h1), nil |
| } |
| |
| type Event Handle |
| |
| func NewEvent(options uint32) (Event, error) { |
| var h Handle |
| if status := Sys_event_create(options, &h); status != ErrOk { |
| return Event(HandleInvalid), &Error{Status: status, Text: "zx.Event"} |
| } |
| return Event(h), nil |
| } |
| |
| func (e *Event) Handle() *Handle { |
| return (*Handle)(e) |
| } |
| |
| func (e *Event) Close() error { |
| return e.Handle().Close() |
| } |
| |
| func (e *Event) Duplicate(rights Rights) (Event, error) { |
| h := Handle(*e) |
| h, err := h.Duplicate(rights) |
| if err != nil { |
| return Event(HandleInvalid), err |
| } |
| return Event(h), nil |
| } |
| |
| var zxwaitCloseFn = func(Handle) {} |
| |
| // SetZXWaitCloseFn is used by package zxwait to avoid a circular dependency. |
| func SetZXWaitCloseFn(fn func(Handle)) { |
| zxwaitCloseFn = fn |
| } |
| |
| func (h *Handle) Close() error { |
| if handle := Handle(atomic.SwapUint32((*uint32)(h), uint32(HandleInvalid))); handle != HandleInvalid { |
| if status := Sys_handle_close(handle); status != ErrOk { |
| return &Error{Status: status, Text: "zx.Handle.Close"} |
| } |
| zxwaitCloseFn(handle) |
| } |
| return nil |
| } |
| |
| func (h *Handle) Duplicate(rights Rights) (Handle, error) { |
| var dup Handle |
| if status := sys_handle_duplicate(*h, rights, &dup); status != ErrOk { |
| return HandleInvalid, &Error{Status: status, Text: "zx.Handle.Duplicate"} |
| } |
| return dup, nil |
| } |
| |
| func (h *Handle) Signal(clearMask, setMask Signals) error { |
| if status := Sys_object_signal(*h, uint32(clearMask), uint32(setMask)); status != ErrOk { |
| return &Error{Status: status, Text: "zx.Handle.Signal"} |
| } |
| return nil |
| } |
| |
| func (h *Handle) SignalPeer(clearMask, setMask Signals) error { |
| if status := Sys_object_signal_peer(*h, uint32(clearMask), uint32(setMask)); status != ErrOk { |
| return &Error{Status: status, Text: "zx.Handle.SignalPeer"} |
| } |
| return nil |
| } |
| |
| func (h *Handle) GetInfo(topic uint32, buffer unsafe.Pointer, bufferSize uint) error { |
| if status := Sys_object_get_info(*h, topic, buffer, bufferSize, nil, nil); status != ErrOk { |
| return &Error{Status: status, Text: "zx.Handle.GetInfo"} |
| } |
| return nil |
| } |
| |
| func (h *Handle) GetInfoHandleBasic() (InfoHandleBasic, error) { |
| var info InfoHandleBasic |
| err := h.GetInfo(ObjectInfoHandleBasic, unsafe.Pointer(&info), uint(unsafe.Sizeof(info))) |
| return info, err |
| } |
| |
| func (h *Handle) GetProperty(property uint32, data []byte) error { |
| var numBytes = uint(len(data)) |
| if status := Sys_object_get_property(*h, property, bytesPtr(data), numBytes); status != ErrOk { |
| return &Error{Status: status, Text: "zx.Handle.GetProperty"} |
| } |
| return nil |
| } |
| |
| func (h *Handle) SetProperty(property uint32, data []byte) error { |
| var numBytes = uint(len(data)) |
| if status := Sys_object_set_property(*h, property, bytesPtr(data), numBytes); status != ErrOk { |
| return &Error{Status: status, Text: "zx.Handle.SetProperty"} |
| } |
| return nil |
| } |
| |
| func (h *Handle) IsValid() bool { |
| return *h > HandleInvalid |
| } |
| |
| func WaitMany(items []WaitItem, timeout Time) error { |
| var ptr *WaitItem |
| if len(items) > 0 { |
| ptr = &items[0] |
| } |
| if status := sys_object_wait_many(ptr, uint(len(items)), timeout); status != ErrOk { |
| return &Error{Status: status, Text: "zx.Handle.WaitMany"} |
| } |
| return nil |
| } |
| |
| const ( |
| ChannelMaxMessageBytes = 65536 |
| ChannelMaxMessageHandles = 64 |
| ) |
| |
| type Channel Handle |
| |
| func (c *Channel) Handle() *Handle { |
| return (*Handle)(c) |
| } |
| |
| func (c *Channel) Close() error { |
| return c.Handle().Close() |
| } |
| |
| func handlePtr(data []Handle) *Handle { |
| if len(data) > 0 { |
| return &data[0] |
| } |
| return nil |
| } |
| |
| func (c *Channel) Read(data []byte, handles []Handle, flags uint32) (numBytes, numHandles uint32, _ error) { |
| numBytes = uint32(len(data)) |
| numHandles = uint32(len(handles)) |
| if status := sys_channel_read(Handle(*c), flags, bytesPtr(data), handlePtr(handles), numBytes, numHandles, &numBytes, &numHandles); status != ErrOk { |
| return 0, 0, &Error{Status: status, Text: "zx.Channel.Read"} |
| } |
| return numBytes, numHandles, nil |
| } |
| |
| func (c *Channel) Write(data []byte, handles []Handle, flags uint32) error { |
| if status := sys_channel_write(Handle(*c), flags, bytesPtr(data), uint32(len(data)), handlePtr(handles), uint32(len(handles))); status != ErrOk { |
| return &Error{Status: status, Text: "zx.Channel.Write"} |
| } |
| return nil |
| } |
| |
| func (c *Channel) Call(flags uint32, deadline Time, wData []byte, wHandles []Handle, rData []byte, rHandles []Handle) (actualBytes, actualHandles uint32, _ error) { |
| args := &ChannelCallArgs{ |
| WriteBytes: bytesPtr(wData), |
| WriteHandles: handlePtr(wHandles), |
| ReadBytes: bytesPtr(rData), |
| ReadHandles: handlePtr(rHandles), |
| WriteNumBytes: uint32(len(wData)), |
| WriteNumHandles: uint32(len(wHandles)), |
| ReadNumBytes: uint32(len(rData)), |
| ReadNumHandles: uint32(len(rHandles)), |
| } |
| |
| if status := Sys_channel_call(Handle(*c), flags, deadline, args, &actualBytes, &actualHandles); status != ErrOk { |
| return 0, 0, &Error{Status: status, Text: "zx.Channel.Call"} |
| } |
| return actualBytes, actualHandles, nil |
| } |
| |
| func NewChannel(flags uint32) (Channel, Channel, error) { |
| var h0, h1 Handle |
| if status := sys_channel_create(flags, &h0, &h1); status != ErrOk { |
| return Channel(HandleInvalid), Channel(HandleInvalid), &Error{Status: status, Text: "zx.Channel"} |
| } |
| return Channel(h0), Channel(h1), nil |
| } |
| |
| const ( |
| PortWaitAsyncOnce = iota |
| PortWaitAsyncRepeating |
| ) |
| |
| const ( |
| PortPacketTypeUser = iota |
| PortPacketTypeSignalOne |
| PortPacketTypeSignalRepeating |
| ) |
| |
| const ( |
| PacketHeaderSize = uint(unsafe.Sizeof(PacketHeader{})) |
| PacketMaxSize = uint(unsafe.Sizeof(Packet{})) |
| ) |
| |
| type PacketHeader struct { |
| Key uint64 |
| Type uint32 |
| Extra uint32 |
| } |
| |
| // Packet describes the unit of communication via an FDIO protocol |
| type Packet struct { |
| Hdr PacketHeader |
| Bytes [32]uint8 |
| } |
| |
| func (p *Packet) Signal() *PacketSignal { |
| return (*PacketSignal)(unsafe.Pointer(p)) |
| } |
| |
| type PortPacket interface { |
| Header() *PacketHeader |
| Length() int |
| } |
| |
| // PacketSignal describes packets of type PortPacketTypeSignal |
| type PacketSignal struct { |
| Hdr PacketHeader |
| Trigger Signals |
| Observed Signals |
| Count uint64 |
| } |
| |
| func (s *PacketSignal) Header() *PacketHeader { |
| return &s.Hdr |
| } |
| |
| func (s *PacketSignal) Length() int { |
| return int(unsafe.Sizeof(*s)) |
| } |
| |
| type Port Handle |
| |
| func (p *Port) Handle() *Handle { |
| return (*Handle)(p) |
| } |
| |
| func (p *Port) Close() error { |
| return p.Handle().Close() |
| } |
| |
| // Queue a PortPacket, which may have a varying size. |
| func (p *Port) Queue(packet PortPacket) error { |
| if status := Sys_port_queue(Handle(*p), (*int)(unsafe.Pointer(packet.Header()))); status != ErrOk { |
| return &Error{Status: status, Text: "zx.Port.Queue"} |
| } |
| return nil |
| } |
| |
| // Wait for a Packet, which must be as large as the largest |
| // possible packet that may be received. |
| func (p *Port) Wait(packet *Packet, deadline Time) error { |
| if status := Sys_port_wait(Handle(*p), deadline, (*int)(unsafe.Pointer(packet))); status != ErrOk { |
| return &Error{Status: status, Text: "zx.Port.Wait"} |
| } |
| return nil |
| } |
| |
| func (p *Port) Cancel(source Handle, key uint64) error { |
| if status := Sys_port_cancel(Handle(*p), source, key); status != ErrOk { |
| return &Error{Status: status, Text: "zx.Port.Cancel"} |
| } |
| return nil |
| } |
| |
| func (p *Port) WaitAsync(handle Handle, key uint64, signals Signals, options uint32) error { |
| if status := Sys_object_wait_async(handle, Handle(*p), key, signals, options); status != ErrOk { |
| return &Error{Status: status, Text: "fdio.Port.WaitAsync"} |
| } |
| return nil |
| } |
| |
| func NewPort(options uint32) (Port, error) { |
| var h Handle |
| if status := Sys_port_create(options, &h); status != ErrOk { |
| return Port(HandleInvalid), &Error{Status: status, Text: "zx.Port"} |
| } |
| return Port(h), nil |
| } |
| |
| type VMO Handle |
| |
| func (vmo *VMO) Handle() *Handle { |
| return (*Handle)(vmo) |
| } |
| |
| func (vmo *VMO) CreateChild(options VMOChildOption, offset, size uint64) (VMO, error) { |
| var h Handle |
| if status := Sys_vmo_create_child(Handle(*vmo), uint32(options), offset, size, &h); status != ErrOk { |
| return VMO(HandleInvalid), &Error{Status: status, Text: "zx.VMO.CreateChild"} |
| } |
| return VMO(h), nil |
| } |
| |
| func (vmo *VMO) Read(b []byte, offset uint64) error { |
| if status := sys_vmo_read(Handle(*vmo), bytesPtr(b), offset, uint(len(b))); status != ErrOk { |
| return &Error{Status: status, Text: "zx.VMO.Read"} |
| } |
| return nil |
| } |
| |
| func (vmo *VMO) Write(b []byte, offset uint64) error { |
| if status := sys_vmo_write(Handle(*vmo), bytesPtr(b), offset, uint(len(b))); status != ErrOk { |
| return &Error{Status: status, Text: "zx.VMO.Write"} |
| } |
| return nil |
| } |
| |
| func (vmo *VMO) Size() (uint64, error) { |
| var size uint64 |
| if status := sys_vmo_get_size(Handle(*vmo), &size); status != ErrOk { |
| return size, &Error{Status: status, Text: "zx.VMO.Size"} |
| } |
| return size, nil |
| } |
| |
| func (vmo *VMO) SetSize(size uint64) error { |
| if status := sys_vmo_set_size(Handle(*vmo), size); status != ErrOk { |
| return &Error{Status: status, Text: "zx.VMO.SetSize"} |
| } |
| return nil |
| } |
| |
| func (vmo *VMO) OpRange(op uint32, offset, size uint64, b []byte) error { |
| if status := sys_vmo_op_range(Handle(*vmo), op, offset, size, bytesPtr(b), uint(len(b))); status != ErrOk { |
| return &Error{Status: status, Text: "zx.VMO.OpRange"} |
| } |
| return nil |
| } |
| |
| func (vmo *VMO) Close() error { |
| return vmo.Handle().Close() |
| } |
| |
| func NewVMO(size uint64, options VMOOption) (VMO, error) { |
| var h Handle |
| if status := Sys_vmo_create(size, uint32(options), &h); status != ErrOk { |
| return VMO(HandleInvalid), &Error{Status: status, Text: "zx.VMO"} |
| } |
| return VMO(h), nil |
| } |
| |
| type VMAR Handle |
| |
| func (v VMAR) Destroy() error { |
| if status := sys_vmar_destroy(Handle(v)); status != ErrOk { |
| return &Error{Status: status, Text: "zx.VMAR.Destroy"} |
| } |
| return nil |
| } |
| |
| func (v VMAR) Map(vmarOffset uint64, vmo VMO, vmoOffset, len uint64, flags VMFlag) (Vaddr, error) { |
| var addr Vaddr |
| if status := Sys_vmar_map(Handle(v), flags, uint64(vmarOffset), Handle(vmo), vmoOffset, uint64(len), &addr); status != ErrOk { |
| return 0, &Error{Status: status, Text: "zx.VMAR.Map"} |
| } |
| return addr, nil |
| } |
| |
| func (v VMAR) Unmap(addr Vaddr, len uint64) error { |
| if status := Sys_vmar_unmap(Handle(v), addr, uint64(len)); status != ErrOk { |
| return &Error{Status: status, Text: "zx.VMAR.Unmap"} |
| } |
| return nil |
| } |
| |
| func (v VMAR) Protect(addr Vaddr, len uint64, flags VMFlag) error { |
| if status := Sys_vmar_protect(Handle(v), flags, addr, uint64(len)); status != ErrOk { |
| return &Error{Status: status, Text: "zx.VMAR.Protect"} |
| } |
| return nil |
| } |
| |
| func NewVMAR(parent VMAR, offset, size uint64, flags VMFlag) (VMAR, Vaddr, error) { |
| var childHandle Handle |
| var addr Vaddr |
| if status := sys_vmar_allocate(Handle(parent), flags, uint64(offset), uint64(size), &childHandle, &addr); status != ErrOk { |
| return 0, 0, &Error{Status: status, Text: "zx.NewVMAR"} |
| } |
| return VMAR(childHandle), addr, nil |
| } |
| |
| type Log Handle |
| |
| func NewLog(options uint32) Log { |
| var h Handle |
| status := Sys_debuglog_create(HandleInvalid, options, &h) |
| if status != ErrOk { |
| return Log(HandleInvalid) |
| } |
| return Log(h) |
| } |
| |
| func (l *Log) Handle() *Handle { |
| return (*Handle)(l) |
| } |
| |
| func (l Log) Write(b []byte) (int, error) { |
| if status := Sys_debuglog_write(Handle(l), 0, bytesPtr(b), uint(len(b))); status != ErrOk { |
| return 0, &Error{Status: status, Text: "zx.Log.Write"} |
| } |
| return len(b), nil |
| } |
| |
| func (l Log) Read(b []byte) (int, error) { |
| if status := Sys_debuglog_read(Handle(l), 0, bytesPtr(b), uint(len(b))); status != ErrOk { |
| return 0, &Error{Status: status, Text: "zx.Log.Read"} |
| } |
| return len(b), nil |
| } |
| |
| func (l *Log) Close() error { |
| return l.Handle().Close() |
| } |
| |
| func RandRead(b []byte) { |
| Sys_cprng_draw(bytesPtr(b), uint(len(b))) |
| } |
| |
| // Error is a Status with associated error text. |
| // It is used as a Go error. |
| type Error struct { |
| Status Status |
| Text string |
| } |
| |
| var _ error = (*Error)(nil) |
| |
| func (e *Error) Error() string { |
| if e.Text == "" { |
| return "zx.Status: " + e.Status.String() |
| } |
| return e.Status.String() + ": " + e.Text |
| } |