2012-01-21 21:06:46 +00:00
|
|
|
require 'ffi'
|
2012-01-15 14:02:53 +00:00
|
|
|
require 'mach/functions'
|
|
|
|
|
|
|
|
module Mach
|
2012-01-21 21:06:46 +00:00
|
|
|
# Michael Weber's "Some Fun with Mach Ports" was an indispensable
|
|
|
|
# resource in learning the Mach ports API.
|
|
|
|
#
|
|
|
|
# @see http://www.foldr.org/~michaelw/log/computers/macosx/task-info-fun-with-mach
|
2012-01-15 14:02:53 +00:00
|
|
|
class Port
|
|
|
|
include Functions
|
|
|
|
|
|
|
|
attr_reader :ipc_space, :port
|
|
|
|
|
2012-01-16 14:14:58 +00:00
|
|
|
# @param [Hash] opts
|
|
|
|
#
|
|
|
|
# @option opts [Integer] :ipc_space defaults to +mach_task_self+
|
|
|
|
#
|
|
|
|
# @option opts [MachPortRight] :right defaults to +:receive+
|
|
|
|
#
|
|
|
|
# @option opts [Port, Integer] :port if given, the existing port
|
|
|
|
# is wrapped in a new Port object; otherwise a new port is
|
|
|
|
# allocated according to the other options
|
|
|
|
def initialize(opts = {})
|
2012-01-15 14:02:53 +00:00
|
|
|
if opts.kind_of? Hash
|
2012-01-16 14:14:58 +00:00
|
|
|
@ipc_space = opts[:ipc_space] || mach_task_self
|
2012-01-15 14:02:53 +00:00
|
|
|
right = opts[:right] || :receive
|
|
|
|
|
2012-01-16 14:14:58 +00:00
|
|
|
@port = if opts[:port]
|
2012-01-21 21:06:46 +00:00
|
|
|
opts[:port].to_i
|
2012-01-16 14:14:58 +00:00
|
|
|
else
|
|
|
|
mem = new_memory_pointer(:mach_port_right_t)
|
2012-01-21 21:06:46 +00:00
|
|
|
mach_port_allocate(@ipc_space.to_i, right, mem)
|
2012-01-16 14:14:58 +00:00
|
|
|
mem.get_uint(0)
|
|
|
|
end
|
2012-01-15 14:02:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-21 21:06:46 +00:00
|
|
|
# With this alias, we can call #to_i on either bare Integer ports
|
|
|
|
# or wrapped Port objects when passing the arg to a foreign
|
|
|
|
# function.
|
|
|
|
alias_method :to_i, :port
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"#<#{self.class} #{to_i}>"
|
|
|
|
end
|
|
|
|
|
2012-01-15 14:02:53 +00:00
|
|
|
def ==(other)
|
|
|
|
(port == other.port) && (ipc_space == other.ipc_space)
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy(opts = {})
|
|
|
|
ipc_space = opts[:ipc_space] || @ipc_space
|
|
|
|
mach_port_destroy(ipc_space, @port)
|
|
|
|
end
|
|
|
|
|
|
|
|
def deallocate(opts = {})
|
|
|
|
ipc_space = opts[:ipc_space] || @ipc_space
|
2012-01-21 21:06:46 +00:00
|
|
|
mach_port_deallocate(ipc_space.to_i, @port)
|
2012-01-15 14:02:53 +00:00
|
|
|
end
|
|
|
|
|
2012-01-22 00:39:27 +00:00
|
|
|
# Insert +right+ into another ipc space. The current task must
|
|
|
|
# have sufficient rights to insert the requested right.
|
|
|
|
#
|
|
|
|
# @param [MachMsgType] right
|
|
|
|
#
|
|
|
|
# @param [Hash] opts
|
|
|
|
#
|
|
|
|
# @option opts [Port,Integer] :ipc_space the space (task port) into which
|
|
|
|
# +right+ will be inserted; defaults to this port's ipc_space
|
|
|
|
#
|
|
|
|
# @option opts [Port,Integer] :port the name the port right should
|
|
|
|
# have in :ipc_space; defaults to the same name as this port
|
|
|
|
def insert_right(right, opts = {})
|
2012-01-15 14:02:53 +00:00
|
|
|
ipc_space = opts[:ipc_space] || @ipc_space
|
|
|
|
port_name = opts[:port_name] || @port
|
|
|
|
|
2012-01-22 00:39:27 +00:00
|
|
|
mach_port_insert_right(ipc_space.to_i, port_name.to_i, @port, right)
|
2012-01-21 21:06:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Send +right+ on this Port to +remote_port+. The current task
|
|
|
|
# must already have the requisite rights allowing it to send
|
|
|
|
# +right+.
|
|
|
|
def send_right(right, remote_port)
|
|
|
|
msg = FFI::Struct.new(nil,
|
|
|
|
:header, MsgHeader,
|
|
|
|
:body, MsgBody,
|
|
|
|
:port, MsgPortDescriptor)
|
|
|
|
|
|
|
|
msg[:header].tap do |h|
|
|
|
|
h[:remote_port] = remote_port.to_i
|
|
|
|
h[:local_port] = MACH_PORT_NULL
|
|
|
|
h[:bits] =
|
|
|
|
(MachMsgType[right] | (0 << 8)) | 0x80000000 # MACH_MSGH_BITS_COMPLEX
|
|
|
|
h[:size] = msg.size
|
|
|
|
end
|
|
|
|
|
|
|
|
msg[:body][:descriptor_count] = 1
|
|
|
|
|
|
|
|
msg[:port].tap do |p|
|
|
|
|
p[:name] = port
|
|
|
|
p[:disposition] = MachMsgType[right]
|
|
|
|
p[:type] = 0 # MACH_MSG_PORT_DESCRIPTOR;
|
|
|
|
end
|
|
|
|
|
|
|
|
mach_msg_send msg
|
|
|
|
end
|
|
|
|
|
|
|
|
# Copy the send right on this port and send it in a message to
|
|
|
|
# +remote_port+. The current task must have an existing send
|
|
|
|
# right on this Port.
|
|
|
|
def copy_send(remote_port)
|
|
|
|
send_right(:copy_send, remote_port)
|
|
|
|
end
|
|
|
|
|
2012-01-21 21:11:16 +00:00
|
|
|
# Create a new Port by receiving a port right message on this
|
2012-01-21 21:06:46 +00:00
|
|
|
# port.
|
|
|
|
def receive_right
|
|
|
|
msg = FFI::Struct.new(nil,
|
|
|
|
:header, MsgHeader,
|
|
|
|
:body, MsgBody,
|
|
|
|
:port, MsgPortDescriptor,
|
|
|
|
:trailer, MsgTrailer)
|
|
|
|
|
|
|
|
mach_msg(msg,
|
|
|
|
2, # MACH_RCV_MSG,
|
|
|
|
0,
|
|
|
|
msg.size,
|
|
|
|
port,
|
|
|
|
MACH_MSG_TIMEOUT_NONE,
|
|
|
|
MACH_PORT_NULL)
|
|
|
|
|
|
|
|
self.class.new :port => msg[:port][:name]
|
2012-01-15 14:02:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|