summaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/sys/unix/syscall_solaris.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/golang.org/x/sys/unix/syscall_solaris.go')
-rw-r--r--vendor/golang.org/x/sys/unix/syscall_solaris.go240
1 files changed, 240 insertions, 0 deletions
diff --git a/vendor/golang.org/x/sys/unix/syscall_solaris.go b/vendor/golang.org/x/sys/unix/syscall_solaris.go
index 77fcde7c1..d2a6495c7 100644
--- a/vendor/golang.org/x/sys/unix/syscall_solaris.go
+++ b/vendor/golang.org/x/sys/unix/syscall_solaris.go
@@ -13,7 +13,10 @@
package unix
import (
+ "fmt"
+ "os"
"runtime"
+ "sync"
"syscall"
"unsafe"
)
@@ -744,3 +747,240 @@ func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, e
func Munmap(b []byte) (err error) {
return mapper.Munmap(b)
}
+
+// Event Ports
+
+type fileObjCookie struct {
+ fobj *fileObj
+ cookie interface{}
+}
+
+// EventPort provides a safe abstraction on top of Solaris/illumos Event Ports.
+type EventPort struct {
+ port int
+ mu sync.Mutex
+ fds map[uintptr]interface{}
+ paths map[string]*fileObjCookie
+}
+
+// PortEvent is an abstraction of the port_event C struct.
+// Compare Source against PORT_SOURCE_FILE or PORT_SOURCE_FD
+// to see if Path or Fd was the event source. The other will be
+// uninitialized.
+type PortEvent struct {
+ Cookie interface{}
+ Events int32
+ Fd uintptr
+ Path string
+ Source uint16
+ fobj *fileObj
+}
+
+// NewEventPort creates a new EventPort including the
+// underlying call to port_create(3c).
+func NewEventPort() (*EventPort, error) {
+ port, err := port_create()
+ if err != nil {
+ return nil, err
+ }
+ e := &EventPort{
+ port: port,
+ fds: make(map[uintptr]interface{}),
+ paths: make(map[string]*fileObjCookie),
+ }
+ return e, nil
+}
+
+//sys port_create() (n int, err error)
+//sys port_associate(port int, source int, object uintptr, events int, user *byte) (n int, err error)
+//sys port_dissociate(port int, source int, object uintptr) (n int, err error)
+//sys port_get(port int, pe *portEvent, timeout *Timespec) (n int, err error)
+//sys port_getn(port int, pe *portEvent, max uint32, nget *uint32, timeout *Timespec) (n int, err error)
+
+// Close closes the event port.
+func (e *EventPort) Close() error {
+ e.mu.Lock()
+ defer e.mu.Unlock()
+ e.fds = nil
+ e.paths = nil
+ return Close(e.port)
+}
+
+// PathIsWatched checks to see if path is associated with this EventPort.
+func (e *EventPort) PathIsWatched(path string) bool {
+ e.mu.Lock()
+ defer e.mu.Unlock()
+ _, found := e.paths[path]
+ return found
+}
+
+// FdIsWatched checks to see if fd is associated with this EventPort.
+func (e *EventPort) FdIsWatched(fd uintptr) bool {
+ e.mu.Lock()
+ defer e.mu.Unlock()
+ _, found := e.fds[fd]
+ return found
+}
+
+// AssociatePath wraps port_associate(3c) for a filesystem path including
+// creating the necessary file_obj from the provided stat information.
+func (e *EventPort) AssociatePath(path string, stat os.FileInfo, events int, cookie interface{}) error {
+ e.mu.Lock()
+ defer e.mu.Unlock()
+ if _, found := e.paths[path]; found {
+ return fmt.Errorf("%v is already associated with this Event Port", path)
+ }
+ fobj, err := createFileObj(path, stat)
+ if err != nil {
+ return err
+ }
+ fCookie := &fileObjCookie{fobj, cookie}
+ _, err = port_associate(e.port, PORT_SOURCE_FILE, uintptr(unsafe.Pointer(fobj)), events, (*byte)(unsafe.Pointer(&fCookie.cookie)))
+ if err != nil {
+ return err
+ }
+ e.paths[path] = fCookie
+ return nil
+}
+
+// DissociatePath wraps port_dissociate(3c) for a filesystem path.
+func (e *EventPort) DissociatePath(path string) error {
+ e.mu.Lock()
+ defer e.mu.Unlock()
+ f, ok := e.paths[path]
+ if !ok {
+ return fmt.Errorf("%v is not associated with this Event Port", path)
+ }
+ _, err := port_dissociate(e.port, PORT_SOURCE_FILE, uintptr(unsafe.Pointer(f.fobj)))
+ if err != nil {
+ return err
+ }
+ delete(e.paths, path)
+ return nil
+}
+
+// AssociateFd wraps calls to port_associate(3c) on file descriptors.
+func (e *EventPort) AssociateFd(fd uintptr, events int, cookie interface{}) error {
+ e.mu.Lock()
+ defer e.mu.Unlock()
+ if _, found := e.fds[fd]; found {
+ return fmt.Errorf("%v is already associated with this Event Port", fd)
+ }
+ pcookie := &cookie
+ _, err := port_associate(e.port, PORT_SOURCE_FD, fd, events, (*byte)(unsafe.Pointer(pcookie)))
+ if err != nil {
+ return err
+ }
+ e.fds[fd] = pcookie
+ return nil
+}
+
+// DissociateFd wraps calls to port_dissociate(3c) on file descriptors.
+func (e *EventPort) DissociateFd(fd uintptr) error {
+ e.mu.Lock()
+ defer e.mu.Unlock()
+ _, ok := e.fds[fd]
+ if !ok {
+ return fmt.Errorf("%v is not associated with this Event Port", fd)
+ }
+ _, err := port_dissociate(e.port, PORT_SOURCE_FD, fd)
+ if err != nil {
+ return err
+ }
+ delete(e.fds, fd)
+ return nil
+}
+
+func createFileObj(name string, stat os.FileInfo) (*fileObj, error) {
+ fobj := new(fileObj)
+ bs, err := ByteSliceFromString(name)
+ if err != nil {
+ return nil, err
+ }
+ fobj.Name = (*int8)(unsafe.Pointer(&bs[0]))
+ s := stat.Sys().(*syscall.Stat_t)
+ fobj.Atim.Sec = s.Atim.Sec
+ fobj.Atim.Nsec = s.Atim.Nsec
+ fobj.Mtim.Sec = s.Mtim.Sec
+ fobj.Mtim.Nsec = s.Mtim.Nsec
+ fobj.Ctim.Sec = s.Ctim.Sec
+ fobj.Ctim.Nsec = s.Ctim.Nsec
+ return fobj, nil
+}
+
+// GetOne wraps port_get(3c) and returns a single PortEvent.
+func (e *EventPort) GetOne(t *Timespec) (*PortEvent, error) {
+ pe := new(portEvent)
+ _, err := port_get(e.port, pe, t)
+ if err != nil {
+ return nil, err
+ }
+ p := new(PortEvent)
+ p.Events = pe.Events
+ p.Source = pe.Source
+ e.mu.Lock()
+ defer e.mu.Unlock()
+ switch pe.Source {
+ case PORT_SOURCE_FD:
+ p.Fd = uintptr(pe.Object)
+ cookie := (*interface{})(unsafe.Pointer(pe.User))
+ p.Cookie = *cookie
+ delete(e.fds, p.Fd)
+ case PORT_SOURCE_FILE:
+ p.fobj = (*fileObj)(unsafe.Pointer(uintptr(pe.Object)))
+ p.Path = BytePtrToString((*byte)(unsafe.Pointer(p.fobj.Name)))
+ cookie := (*interface{})(unsafe.Pointer(pe.User))
+ p.Cookie = *cookie
+ delete(e.paths, p.Path)
+ }
+ return p, nil
+}
+
+// Pending wraps port_getn(3c) and returns how many events are pending.
+func (e *EventPort) Pending() (int, error) {
+ var n uint32 = 0
+ _, err := port_getn(e.port, nil, 0, &n, nil)
+ return int(n), err
+}
+
+// Get wraps port_getn(3c) and fills a slice of PortEvent.
+// It will block until either min events have been received
+// or the timeout has been exceeded. It will return how many
+// events were actually received along with any error information.
+func (e *EventPort) Get(s []PortEvent, min int, timeout *Timespec) (int, error) {
+ if min == 0 {
+ return 0, fmt.Errorf("need to request at least one event or use Pending() instead")
+ }
+ if len(s) < min {
+ return 0, fmt.Errorf("len(s) (%d) is less than min events requested (%d)", len(s), min)
+ }
+ got := uint32(min)
+ max := uint32(len(s))
+ var err error
+ ps := make([]portEvent, max, max)
+ _, err = port_getn(e.port, &ps[0], max, &got, timeout)
+ // got will be trustworthy with ETIME, but not any other error.
+ if err != nil && err != ETIME {
+ return 0, err
+ }
+ e.mu.Lock()
+ defer e.mu.Unlock()
+ for i := 0; i < int(got); i++ {
+ s[i].Events = ps[i].Events
+ s[i].Source = ps[i].Source
+ switch ps[i].Source {
+ case PORT_SOURCE_FD:
+ s[i].Fd = uintptr(ps[i].Object)
+ cookie := (*interface{})(unsafe.Pointer(ps[i].User))
+ s[i].Cookie = *cookie
+ delete(e.fds, s[i].Fd)
+ case PORT_SOURCE_FILE:
+ s[i].fobj = (*fileObj)(unsafe.Pointer(uintptr(ps[i].Object)))
+ s[i].Path = BytePtrToString((*byte)(unsafe.Pointer(s[i].fobj.Name)))
+ cookie := (*interface{})(unsafe.Pointer(ps[i].User))
+ s[i].Cookie = *cookie
+ delete(e.paths, s[i].Path)
+ }
+ }
+ return int(got), err
+}