Skip to content

Commit

Permalink
unix: add Poll for linux
Browse files Browse the repository at this point in the history
Change-Id: I273bd852f85d204694872a1615be51dc027b97ee
Reviewed-on: https://go-review.googlesource.com/23661
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
  • Loading branch information
kortschak authored and ianlancetaylor committed Jun 11, 2016
1 parent b44883b commit 7f918dd
Show file tree
Hide file tree
Showing 21 changed files with 304 additions and 1 deletion.
10 changes: 9 additions & 1 deletion unix/syscall_linux.go
Expand Up @@ -60,6 +60,15 @@ func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
return openat(dirfd, path, flags|O_LARGEFILE, mode)
}

//sys poll(fds *PollFd, nfd int, timeout int) (n int, err error)

func Poll(fds []PollFd, timeout int) (n int, err error) {
if len(fds) == 0 {
return poll(nil, 0, timeout)
}
return poll(&fds[0], len(fds), timeout)
}

//sys readlinkat(dirfd int, path string, buf []byte) (n int, err error)

func Readlink(path string, buf []byte) (n int, err error) {
Expand Down Expand Up @@ -1043,7 +1052,6 @@ func Munmap(b []byte) (err error) {
// Newfstatat
// Nfsservctl
// Personality
// Poll
// Ppoll
// Pselect6
// Ptrace
Expand Down
39 changes: 39 additions & 0 deletions unix/syscall_linux_test.go
Expand Up @@ -15,6 +15,45 @@ import (
"golang.org/x/sys/unix"
)

func TestPoll(t *testing.T) {
err := unix.Mkfifo("fifo", 0666)
if err != nil {
t.Errorf("Poll: failed to create FIFO: %v", err)
return
}
defer os.Remove("fifo")

f, err := os.OpenFile("fifo", os.O_RDWR, 0666)
if err != nil {
t.Errorf("Poll: failed to open FIFO: %v", err)
return
}
defer f.Close()

const timeout = 100

ok := make(chan bool, 1)
go func() {
select {
case <-time.After(10 * timeout * time.Millisecond):
t.Errorf("Poll: failed to timeout after %d milliseconds", 10*timeout)
case <-ok:
}
}()

fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
n, err := unix.Poll(fds, timeout)
ok <- true
if err != nil {
t.Errorf("Poll: unexpected error: %v", err)
return
}
if n != 0 {
t.Errorf("Poll: wrong number of events: got %v, expected %v", n, 0)
return
}
}

func TestTime(t *testing.T) {
var ut unix.Time_t
ut2, err := unix.Time(&ut)
Expand Down
13 changes: 13 additions & 0 deletions unix/types_linux.go
Expand Up @@ -24,6 +24,7 @@ package unix
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netpacket/packet.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <sys/epoll.h>
Expand Down Expand Up @@ -430,6 +431,18 @@ const (
AT_SYMLINK_NOFOLLOW = C.AT_SYMLINK_NOFOLLOW
)

type PollFd C.struct_pollfd

const (
POLLIN = C.POLLIN
POLLPRI = C.POLLPRI
POLLOUT = C.POLLOUT
POLLRDHUP = C.POLLRDHUP
POLLERR = C.POLLERR
POLLHUP = C.POLLHUP
POLLNVAL = C.POLLNVAL
)

// Terminal handling

type Termios C.termios_t
11 changes: 11 additions & 0 deletions unix/zsyscall_linux_386.go
Expand Up @@ -53,6 +53,17 @@ func openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func poll(fds *PollFd, nfd int, timeout int) (n int, err error) {
r0, _, e1 := Syscall(SYS_POLL, uintptr(unsafe.Pointer(fds)), uintptr(nfd), uintptr(timeout))
n = int(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func readlinkat(dirfd int, path string, buf []byte) (n int, err error) {
var _p0 *byte
_p0, err = BytePtrFromString(path)
Expand Down
11 changes: 11 additions & 0 deletions unix/zsyscall_linux_amd64.go
Expand Up @@ -53,6 +53,17 @@ func openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func poll(fds *PollFd, nfd int, timeout int) (n int, err error) {
r0, _, e1 := Syscall(SYS_POLL, uintptr(unsafe.Pointer(fds)), uintptr(nfd), uintptr(timeout))
n = int(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func readlinkat(dirfd int, path string, buf []byte) (n int, err error) {
var _p0 *byte
_p0, err = BytePtrFromString(path)
Expand Down
11 changes: 11 additions & 0 deletions unix/zsyscall_linux_arm.go
Expand Up @@ -53,6 +53,17 @@ func openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func poll(fds *PollFd, nfd int, timeout int) (n int, err error) {
r0, _, e1 := Syscall(SYS_POLL, uintptr(unsafe.Pointer(fds)), uintptr(nfd), uintptr(timeout))
n = int(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func readlinkat(dirfd int, path string, buf []byte) (n int, err error) {
var _p0 *byte
_p0, err = BytePtrFromString(path)
Expand Down
11 changes: 11 additions & 0 deletions unix/zsyscall_linux_arm64.go
Expand Up @@ -53,6 +53,17 @@ func openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func poll(fds *PollFd, nfd int, timeout int) (n int, err error) {
r0, _, e1 := Syscall(SYS_POLL, uintptr(unsafe.Pointer(fds)), uintptr(nfd), uintptr(timeout))
n = int(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func readlinkat(dirfd int, path string, buf []byte) (n int, err error) {
var _p0 *byte
_p0, err = BytePtrFromString(path)
Expand Down
11 changes: 11 additions & 0 deletions unix/zsyscall_linux_mips64.go
Expand Up @@ -53,6 +53,17 @@ func openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func poll(fds *PollFd, nfd int, timeout int) (n int, err error) {
r0, _, e1 := Syscall(SYS_POLL, uintptr(unsafe.Pointer(fds)), uintptr(nfd), uintptr(timeout))
n = int(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func readlinkat(dirfd int, path string, buf []byte) (n int, err error) {
var _p0 *byte
_p0, err = BytePtrFromString(path)
Expand Down
11 changes: 11 additions & 0 deletions unix/zsyscall_linux_mips64le.go
Expand Up @@ -53,6 +53,17 @@ func openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func poll(fds *PollFd, nfd int, timeout int) (n int, err error) {
r0, _, e1 := Syscall(SYS_POLL, uintptr(unsafe.Pointer(fds)), uintptr(nfd), uintptr(timeout))
n = int(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func readlinkat(dirfd int, path string, buf []byte) (n int, err error) {
var _p0 *byte
_p0, err = BytePtrFromString(path)
Expand Down
11 changes: 11 additions & 0 deletions unix/zsyscall_linux_ppc64.go
Expand Up @@ -53,6 +53,17 @@ func openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func poll(fds *PollFd, nfd int, timeout int) (n int, err error) {
r0, _, e1 := Syscall(SYS_POLL, uintptr(unsafe.Pointer(fds)), uintptr(nfd), uintptr(timeout))
n = int(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func readlinkat(dirfd int, path string, buf []byte) (n int, err error) {
var _p0 *byte
_p0, err = BytePtrFromString(path)
Expand Down
11 changes: 11 additions & 0 deletions unix/zsyscall_linux_ppc64le.go
Expand Up @@ -53,6 +53,17 @@ func openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func poll(fds *PollFd, nfd int, timeout int) (n int, err error) {
r0, _, e1 := Syscall(SYS_POLL, uintptr(unsafe.Pointer(fds)), uintptr(nfd), uintptr(timeout))
n = int(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func readlinkat(dirfd int, path string, buf []byte) (n int, err error) {
var _p0 *byte
_p0, err = BytePtrFromString(path)
Expand Down
11 changes: 11 additions & 0 deletions unix/zsyscall_linux_s390x.go
Expand Up @@ -53,6 +53,17 @@ func openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func poll(fds *PollFd, nfd int, timeout int) (n int, err error) {
r0, _, e1 := Syscall(SYS_POLL, uintptr(unsafe.Pointer(fds)), uintptr(nfd), uintptr(timeout))
n = int(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}

// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT

func readlinkat(dirfd int, path string, buf []byte) (n int, err error) {
var _p0 *byte
_p0, err = BytePtrFromString(path)
Expand Down
16 changes: 16 additions & 0 deletions unix/ztypes_linux_386.go
Expand Up @@ -595,6 +595,22 @@ const (
AT_SYMLINK_NOFOLLOW = 0x100
)

type PollFd struct {
Fd int32
Events int16
Revents int16
}

const (
POLLIN = 0x1
POLLPRI = 0x2
POLLOUT = 0x4
POLLRDHUP = 0x2000
POLLERR = 0x8
POLLHUP = 0x10
POLLNVAL = 0x20
)

type Termios struct {
Iflag uint32
Oflag uint32
Expand Down
16 changes: 16 additions & 0 deletions unix/ztypes_linux_amd64.go
Expand Up @@ -613,6 +613,22 @@ const (
AT_SYMLINK_NOFOLLOW = 0x100
)

type PollFd struct {
Fd int32
Events int16
Revents int16
}

const (
POLLIN = 0x1
POLLPRI = 0x2
POLLOUT = 0x4
POLLRDHUP = 0x2000
POLLERR = 0x8
POLLHUP = 0x10
POLLNVAL = 0x20
)

type Termios struct {
Iflag uint32
Oflag uint32
Expand Down
16 changes: 16 additions & 0 deletions unix/ztypes_linux_arm.go
Expand Up @@ -575,6 +575,22 @@ const (
AT_SYMLINK_NOFOLLOW = 0x100
)

type PollFd struct {
Fd int32
Events int16
Revents int16
}

const (
POLLIN = 0x1
POLLPRI = 0x2
POLLOUT = 0x4
POLLRDHUP = 0x2000
POLLERR = 0x8
POLLHUP = 0x10
POLLNVAL = 0x20
)

type Termios struct {
Iflag uint32
Oflag uint32
Expand Down
16 changes: 16 additions & 0 deletions unix/ztypes_linux_arm64.go
Expand Up @@ -592,6 +592,22 @@ const (
AT_SYMLINK_NOFOLLOW = 0x100
)

type PollFd struct {
Fd int32
Events int16
Revents int16
}

const (
POLLIN = 0x1
POLLPRI = 0x2
POLLOUT = 0x4
POLLRDHUP = 0x2000
POLLERR = 0x8
POLLHUP = 0x10
POLLNVAL = 0x20
)

type Termios struct {
Iflag uint32
Oflag uint32
Expand Down
16 changes: 16 additions & 0 deletions unix/ztypes_linux_mips64.go
Expand Up @@ -596,6 +596,22 @@ const (
AT_SYMLINK_NOFOLLOW = 0x100
)

type PollFd struct {
Fd int32
Events int16
Revents int16
}

const (
POLLIN = 0x1
POLLPRI = 0x2
POLLOUT = 0x4
POLLRDHUP = 0x2000
POLLERR = 0x8
POLLHUP = 0x10
POLLNVAL = 0x20
)

type Termios struct {
Iflag uint32
Oflag uint32
Expand Down
16 changes: 16 additions & 0 deletions unix/ztypes_linux_mips64le.go
Expand Up @@ -596,6 +596,22 @@ const (
AT_SYMLINK_NOFOLLOW = 0x100
)

type PollFd struct {
Fd int32
Events int16
Revents int16
}

const (
POLLIN = 0x1
POLLPRI = 0x2
POLLOUT = 0x4
POLLRDHUP = 0x2000
POLLERR = 0x8
POLLHUP = 0x10
POLLNVAL = 0x20
)

type Termios struct {
Iflag uint32
Oflag uint32
Expand Down

0 comments on commit 7f918dd

Please sign in to comment.