process_shared 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,78 +0,0 @@
1
- require 'ffi'
2
-
3
- require 'mach'
4
- require 'mach/functions'
5
-
6
- include Mach
7
- include Mach::Functions
8
-
9
- def setup_recv_port
10
- port = new_memory_pointer :mach_port_t
11
- mach_port_allocate(mach_task_self, :receive, port)
12
- p = port.get_uint(0)
13
- mach_port_insert_right(mach_task_self, p, p, :make_send)
14
- p
15
- end
16
-
17
- def send_port(remote_port, port)
18
- puts "send_port: (in #{mach_task_self}) sending #{port} -> #{remote_port}"
19
- msg = FFI::Struct.new(nil,
20
- :header, MsgHeader,
21
- :body, MsgBody,
22
- :task_port, MsgPortDescriptor)
23
- msg[:header].tap do |h|
24
- h[:remote_port] = remote_port
25
- h[:local_port] = 0
26
- h[:bits] = (MachMsgType[:copy_send] | (0 << 8)) | 0x80000000 # MACH_MSGH_BITS_COMPLEX
27
- h[:size] = msg.size
28
- end
29
-
30
- msg[:body][:descriptor_count] = 1
31
-
32
- msg[:task_port].tap do |p|
33
- p[:name] = port
34
- p[:disposition] = MachMsgType[:copy_send]
35
- p[:type] = 0 # MACH_MSG_PORT_DESCRIPTOR;
36
- end
37
-
38
- mach_msg_send(msg)
39
- end
40
-
41
- def recv_port(recv_port)
42
- msg = FFI::Struct.new(nil,
43
- :header, MsgHeader,
44
- :body, MsgBody,
45
- :task_port, MsgPortDescriptor,
46
- :trailer, MsgTrailer)
47
-
48
- mach_msg(msg, 2, 0, msg.size, recv_port, 0, 0)
49
-
50
- msg.size.times do |i|
51
- print "%02x " % msg.to_ptr.get_uint8(i)
52
- end
53
- puts
54
-
55
- msg[:task_port][:name]
56
- end
57
-
58
- def sampling_fork
59
- parent_recv_port = setup_recv_port
60
- task_set_special_port(mach_task_self, :bootstrap, parent_recv_port)
61
-
62
- fork do
63
- parent_recv_port_p = new_memory_pointer :mach_port_t
64
- task_get_special_port(mach_task_self, :bootstrap, parent_recv_port_p)
65
- parent_recv_port = parent_recv_port_p.get_uint(0)
66
- puts "child self:#{mach_task_self} parent_recv:#{parent_recv_port}"
67
-
68
- child_recv_port = setup_recv_port
69
- puts "child sending #{mach_task_self}"
70
- send_port(parent_recv_port, mach_task_self)
71
- end
72
-
73
- task_set_special_port(mach_task_self, :bootstrap, Mach::Functions::bootstrap_port)
74
- child_task = recv_port(parent_recv_port)
75
- puts "parent received #{child_task}"
76
- end
77
-
78
- sampling_fork