Skip to content

Instantly share code, notes, and snippets.

@eam
Created May 20, 2012 14:51
Show Gist options
  • Save eam/2758371 to your computer and use it in GitHub Desktop.
Save eam/2758371 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
arch, os = RUBY_PLATFORM.split('-')
if os =~ /^darwin/
SYS_poll = 230 # OSX 7 on linux x86_64, 168 on linux i386
# /usr/include/sys/poll.h
# Requestable events. If poll(2) finds any of these set, they are
# copied to revents on return.
POLLIN = 0x0001 #/* any readable data available */
POLLPRI = 0x0002 #/* OOB/Urgent readable data */
POLLOUT = 0x0004 #/* file descriptor is writeable */
POLLRDNORM = 0x0040 #/* non-OOB/URG data available */
POLLWRNORM = POLLOUT #/* no write type differentiation */
POLLRDBAND = 0x0080 #/* OOB/Urgent readable data */
POLLWRBAND = 0x0100 #/* OOB/Urgent data can be written */
# FreeBSD extensions: polling on a regular file might return one
# of these events (currently only supported on local filesystems).
POLLEXTEND = 0x0200 #/* file may have been extended */
POLLATTRIB = 0x0400 #/* file attributes may have changed */
POLLNLINK = 0x0800 #/* (un)link/rename may have happened */
POLLWRITE = 0x1000 #/* file's contents may have changed */
# These events are set if they occur regardless of whether they were
# requested
POLLERR = 0x0008 #/* some poll error occurred */
POLLHUP = 0x0010 #/* file descriptor was "hung up" */
POLLNVAL = 0x0020 #/* requested events "invalid" */
POLLSTANDARD = (POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLRDBAND|POLLWRBAND|POLLERR|POLLHUP|POLLNVAL)
elsif os == "linux"
# /usr/include/bits/poll.h
#/* Event types that can be polled for. These bits may be set in `events'
# to indicate the interesting event types; they will appear in `revents'
# to indicate the status of the file descriptor. */
POLLIN = 0x001 #/* There is data to read. */
POLLPRI = 0x002 #/* There is urgent data to read. */
POLLOUT = 0x004 #/* Writing now will not block. */
#/* These values are defined in XPG4.2. */
POLLRDNORM = 0x040 #/* Normal data may be read. */
POLLRDBAND = 0x080 #/* Priority data may be read. */
POLLWRNORM = 0x100 #/* Writing now will not block. */
POLLWRBAND = 0x200 #/* Priority data may be written. */
#/* These are extensions for Linux. */
POLLMSG = 0x400
POLLREMOVE = 0x1000
POLLRDHUP = 0x2000
#/* Event types always implicitly polled for. These bits need not be set in
# `events', but they will appear in `revents' to indicate the status of
# the file descriptor. */
POLLERR = 0x008 # /* Error condition. */
POLLHUP = 0x010 # /* Hung up. */
POLLNVAL = 0x020 # /* Invalid polling request. */
# not part of poll.h on linux, but looks handy
POLLSTANDARD = (POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLRDBAND|POLLWRBAND|POLLERR|POLLHUP|POLLNVAL)
if arch == "x86_64"
SYS_poll = 7
else
SYS_poll = 168
end
else
raise "unknown platform: #{RUBY_PLATFORM}"
end
# poll() constants
puts "using SYS_poll = #{SYS_poll}"
# s_ | Integer | signed short, native endian
# i, i_ | Integer | signed int, native endian
# syscall SYS_poll, [STDIN.fileno, ]
require 'rubygems'
require 'ffi'
module PollThing
extend FFI::Library
ffi_lib FFI::Library::LIBC
class PollFd < FFI::Struct
layout :fd, :int,
:events, :short,
:revents, :short
end
class PollFdT < FFI::Struct
layout :len, :uint,
:val, :pointer
end
attach_function 'poll', [:pointer, :int, :int], :int
end
pollfd_len = 3
pollfds = FFI::MemoryPointer.new(PollThing::PollFd, PollThing::PollFd.size * pollfd_len)
# pollfds_rb = []
# x = PollFd.new
require 'pp'
puts "pointer is: #{pollfds.get_pointer(0)}"
pp pollfds
pollfd_len.times do |i|
struct = PollThing::PollFd.new(pollfds[i])
struct[:fd] = i
struct[:events] = 7
struct[:revents] = 0
end
ret = PollThing.poll(pollfds.get_pointer(0), pollfd_len, 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment