mach_sys/
message.rs

1//! This module corresponds to `mach/message.h`.
2
3use crate::ffi::{c_uint, c_void};
4
5use crate::kern_return::kern_return_t;
6use crate::port::{mach_port_name_t, mach_port_seqno_t, mach_port_t};
7use crate::vm_types::{integer_t, natural_t};
8
9pub type mach_msg_timeout_t = natural_t;
10
11pub type mach_msg_bits_t = c_uint;
12pub type mach_msg_id_t   = integer_t;
13pub type mach_msg_size_t = natural_t;
14
15pub type mach_msg_copy_options_t    = c_uint;
16pub type mach_msg_descriptor_type_t = c_uint;
17pub type mach_msg_type_name_t       = c_uint;
18
19pub type mach_msg_guard_flags_t  = c_uint;
20
21pub type mach_msg_trailer_type_t = c_uint;
22pub type mach_msg_trailer_size_t = c_uint;
23
24pub type mach_msg_option_t       = integer_t;
25
26pub type mach_msg_type_number_t  = natural_t;
27pub type mach_msg_type_size_t    = natural_t;
28
29pub type mach_msg_return_t       = kern_return_t;
30
31pub const MACH_MSG_TIMEOUT_NONE: mach_msg_timeout_t = 0;
32
33pub const MACH_MSGH_BITS_ZERO:   mach_msg_bits_t    = 0x0000_0000;
34
35pub const MACH_MSGH_BITS_REMOTE_MASK:  mach_msg_bits_t = 0x0000_001f;
36pub const MACH_MSGH_BITS_LOCAL_MASK:   mach_msg_bits_t = 0x0000_1f00;
37pub const MACH_MSGH_BITS_VOUCHER_MASK: mach_msg_bits_t = 0x001f_0000;
38
39pub const MACH_MSGH_BITS_PORTS_MASK: mach_msg_bits_t =
40    MACH_MSGH_BITS_REMOTE_MASK | MACH_MSGH_BITS_LOCAL_MASK | MACH_MSGH_BITS_VOUCHER_MASK;
41
42pub const MACH_MSGH_BITS_COMPLEX: mach_msg_bits_t = 0x8000_0000;
43pub const MACH_MSGH_BITS_USER:    mach_msg_bits_t = 0x801f_1f1f;
44
45#[allow(non_snake_case)]
46pub const fn MACH_MSGH_BITS(remote: mach_msg_bits_t, local: mach_msg_bits_t) -> mach_msg_bits_t {
47    remote | (local << 8)
48}
49
50pub const MACH_MSG_TYPE_MOVE_RECEIVE:      mach_msg_type_name_t = 16;
51pub const MACH_MSG_TYPE_MOVE_SEND:         mach_msg_type_name_t = 17;
52pub const MACH_MSG_TYPE_MOVE_SEND_ONCE:    mach_msg_type_name_t = 18;
53pub const MACH_MSG_TYPE_COPY_SEND:         mach_msg_type_name_t = 19;
54pub const MACH_MSG_TYPE_MAKE_SEND:         mach_msg_type_name_t = 20;
55pub const MACH_MSG_TYPE_MAKE_SEND_ONCE:    mach_msg_type_name_t = 21;
56pub const MACH_MSG_TYPE_COPY_RECEIVE:      mach_msg_type_name_t = 22;
57pub const MACH_MSG_TYPE_DISPOSE_RECEIVE:   mach_msg_type_name_t = 24;
58pub const MACH_MSG_TYPE_DISPOSE_SEND:      mach_msg_type_name_t = 25;
59pub const MACH_MSG_TYPE_DISPOSE_SEND_ONCE: mach_msg_type_name_t = 26;
60
61pub const MACH_MSG_PHYSICAL_COPY: mach_msg_copy_options_t = 0;
62pub const MACH_MSG_VIRTUAL_COPY:  mach_msg_copy_options_t = 1;
63pub const MACH_MSG_ALLOCATE:      mach_msg_copy_options_t = 2;
64
65pub const MACH_MSG_GUARD_FLAGS_NONE:              mach_msg_guard_flags_t = 0;
66pub const MACH_MSG_GUARD_FLAGS_IMMOVABLE_RECEIVE: mach_msg_guard_flags_t = 1;
67pub const MACH_MSG_GUARD_FLAGS_UNGUARDED_ON_SEND: mach_msg_guard_flags_t = 2;
68pub const MACH_MSG_GUARD_FLAGS_MASK:              mach_msg_guard_flags_t = 3;
69
70pub const MACH_MSG_PORT_DESCRIPTOR:         mach_msg_descriptor_type_t = 0;
71pub const MACH_MSG_OOL_DESCRIPTOR:          mach_msg_descriptor_type_t = 1;
72pub const MACH_MSG_OOL_PORTS_DESCRIPTOR:    mach_msg_descriptor_type_t = 2;
73pub const MACH_MSG_OOL_VOLATILE_DESCRIPTOR: mach_msg_descriptor_type_t = 3;
74pub const MACH_MSG_GUARDED_PORT_DESCRIPTOR: mach_msg_descriptor_type_t = 4;
75
76pub const MACH_MSG_OPTION_NONE: mach_msg_option_t = 0x0000_0000;
77
78pub const MACH_SEND_MSG: mach_msg_option_t = 0x0000_0001;
79pub const MACH_RCV_MSG:  mach_msg_option_t = 0x0000_0002;
80
81pub const MACH_RCV_LARGE:          mach_msg_option_t = 0x0000_0004;
82pub const MACH_RCV_LARGE_IDENTITY: mach_msg_option_t = 0x0000_0008;
83
84pub const MACH_SEND_TIMEOUT:         mach_msg_option_t = 0x0000_0010;
85pub const MACH_SEND_OVERRIDE:        mach_msg_option_t = 0x0000_0020;
86pub const MACH_SEND_INTERRUPT:       mach_msg_option_t = 0x0000_0040;
87pub const MACH_SEND_NOTIFY:          mach_msg_option_t = 0x0000_0080;
88pub const MACH_SEND_ALWAYS:          mach_msg_option_t = 0x0001_0000;
89pub const MACH_SEND_FILTER_NONFATAL: mach_msg_option_t = 0x0001_0000;
90pub const MACH_SEND_TRAILER:         mach_msg_option_t = 0x0002_0000;
91pub const MACH_SEND_NOIMPORTANCE:    mach_msg_option_t = 0x0004_0000;
92pub const MACH_SEND_NODENAP:         mach_msg_option_t = MACH_SEND_NOIMPORTANCE;
93pub const MACH_SEND_IMPORTANCE:      mach_msg_option_t = 0x0008_0000;
94pub const MACH_SEND_SYNC_OVERRIDE:   mach_msg_option_t = 0x0010_0000;
95pub const MACH_SEND_PROPAGATE_QOS:   mach_msg_option_t = 0x0020_0000;
96pub const MACH_SEND_SYNC_USE_THRPRI: mach_msg_option_t = MACH_SEND_PROPAGATE_QOS;
97
98pub const MACH_RCV_TIMEOUT:      mach_msg_option_t = 0x0000_0100;
99pub const MACH_RCV_NOTIFY:       mach_msg_option_t = 0x0000_0000;
100pub const MACH_RCV_INTERRUPT:    mach_msg_option_t = 0x0000_0400;
101pub const MACH_RCV_VOUCHER:      mach_msg_option_t = 0x0000_0800;
102pub const MACH_RCV_OVERWRITE:    mach_msg_option_t = 0x0000_0000;
103pub const MACH_RCV_GUARDED_DESC: mach_msg_option_t = 0x0000_1000;
104pub const MACH_RCV_SYNC_WAIT:    mach_msg_option_t = 0x0000_4000;
105pub const MACH_RCV_SYNC_PEEK:    mach_msg_option_t = 0x0000_8000;
106
107pub const MACH_MSG_STRICT_REPLY: mach_msg_option_t = 0x0000_0200;
108
109pub const MACH_RCV_TRAILER_NULL:   mach_msg_trailer_type_t = 0;
110pub const MACH_RCV_TRAILER_SEQNO:  mach_msg_trailer_type_t = 1;
111pub const MACH_RCV_TRAILER_SENDER: mach_msg_trailer_type_t = 2;
112pub const MACH_RCV_TRAILER_AUDIT:  mach_msg_trailer_type_t = 3;
113pub const MACH_RCV_TRAILER_CTX:    mach_msg_trailer_type_t = 4;
114pub const MACH_RCV_TRAILER_AV:     mach_msg_trailer_type_t = 7;
115pub const MACH_RCV_TRAILER_LABELS: mach_msg_trailer_type_t = 8;
116
117pub const MACH_MSG_SUCCESS:    mach_msg_return_t = 0x0000_0000;
118pub const MACH_MSG_MASK:       mach_msg_return_t = 0x0000_3e00;
119pub const MACH_MSG_IPC_SPACE:  mach_msg_return_t = 0x0000_2000;
120pub const MACH_MSG_VM_SPACE:   mach_msg_return_t = 0x0000_1000;
121pub const MACH_MSG_IPC_KERNEL: mach_msg_return_t = 0x0000_0800;
122pub const MACH_MSG_VM_KERNEL:  mach_msg_return_t = 0x0000_0400;
123
124pub const MACH_SEND_IN_PROGRESS:         mach_msg_return_t = 0x1000_0001;
125pub const MACH_SEND_INVALID_DATA:        mach_msg_return_t = 0x1000_0002;
126pub const MACH_SEND_INVALID_DEST:        mach_msg_return_t = 0x1000_0003;
127pub const MACH_SEND_TIMED_OUT:           mach_msg_return_t = 0x1000_0004;
128pub const MACH_SEND_INVALID_VOUCHER:     mach_msg_return_t = 0x1000_0005;
129pub const MACH_SEND_INTERRUPTED:         mach_msg_return_t = 0x1000_0007;
130pub const MACH_SEND_MSG_TOO_SMALL:       mach_msg_return_t = 0x1000_0008;
131pub const MACH_SEND_INVALID_REPLY:       mach_msg_return_t = 0x1000_0009;
132pub const MACH_SEND_INVALID_RIGHT:       mach_msg_return_t = 0x1000_000a;
133pub const MACH_SEND_INVALID_NOTIFY:      mach_msg_return_t = 0x1000_000b;
134pub const MACH_SEND_INVALID_MEMORY:      mach_msg_return_t = 0x1000_000c;
135pub const MACH_SEND_NO_BUFFER:           mach_msg_return_t = 0x1000_000d;
136pub const MACH_SEND_TOO_LARGE:           mach_msg_return_t = 0x1000_000e;
137pub const MACH_SEND_INVALID_TYPE:        mach_msg_return_t = 0x1000_000f;
138pub const MACH_SEND_INVALID_HEADER:      mach_msg_return_t = 0x1000_0010;
139pub const MACH_SEND_INVALID_TRAILER:     mach_msg_return_t = 0x1000_0011;
140pub const MACH_SEND_INVALID_CONTEXT:     mach_msg_return_t = 0x1000_0012;
141pub const MACH_SEND_INVALID_RT_OOL_SIZE: mach_msg_return_t = 0x1000_0015;
142pub const MACH_SEND_NO_GRANT_DEST:       mach_msg_return_t = 0x1000_0016;
143pub const MACH_SEND_MSG_FILTERED:        mach_msg_return_t = 0x1000_0017;
144
145pub const MACH_RCV_IN_PROGRESS:       mach_msg_return_t = 0x1000_4001;
146pub const MACH_RCV_INVALID_NAME:      mach_msg_return_t = 0x1000_4002;
147pub const MACH_RCV_TIMED_OUT:         mach_msg_return_t = 0x1000_4003;
148pub const MACH_RCV_TOO_LARGE:         mach_msg_return_t = 0x1000_4004;
149pub const MACH_RCV_INTERRUPTED:       mach_msg_return_t = 0x1000_4005;
150pub const MACH_RCV_PORT_CHANGED:      mach_msg_return_t = 0x1000_4006;
151pub const MACH_RCV_INVALID_NOTIFY:    mach_msg_return_t = 0x1000_4007;
152pub const MACH_RCV_INVALID_DATA:      mach_msg_return_t = 0x1000_4008;
153pub const MACH_RCV_PORT_DIED:         mach_msg_return_t = 0x1000_4009;
154pub const MACH_RCV_IN_SET:            mach_msg_return_t = 0x1000_400a;
155pub const MACH_RCV_HEADER_ERROR:      mach_msg_return_t = 0x1000_400b;
156pub const MACH_RCV_BODY_ERROR:        mach_msg_return_t = 0x1000_400c;
157pub const MACH_RCV_INVALID_TYPE:      mach_msg_return_t = 0x1000_400d;
158pub const MACH_RCV_SCATTER_SMALL:     mach_msg_return_t = 0x1000_400e;
159pub const MACH_RCV_INVALID_TRAILER:   mach_msg_return_t = 0x1000_400f;
160pub const MACH_RCV_IN_PROGRESS_TIMED: mach_msg_return_t = 0x1000_4011;
161pub const MACH_RCV_INVALID_REPLY:     mach_msg_return_t = 0x1000_4012;
162
163#[repr(C)]
164#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
165pub struct mach_msg_header_t {
166    pub msgh_bits:         mach_msg_bits_t,
167    pub msgh_size:         mach_msg_size_t,
168    pub msgh_remote_port:  mach_port_t,
169    pub msgh_local_port:   mach_port_t,
170    pub msgh_voucher_port: mach_port_name_t,
171    pub msgh_id:           mach_msg_id_t,
172}
173
174#[repr(C)]
175#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
176pub struct mach_msg_body_t {
177    pub msgh_descriptor_count: mach_msg_size_t,
178}
179
180#[repr(C)]
181#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
182pub struct mach_msg_base_t {
183    pub header: mach_msg_header_t,
184    pub body:   mach_msg_body_t,
185}
186
187pub const MACH_MSG_TRAILER_FORMAT_0: mach_msg_trailer_type_t = 0;
188
189#[repr(C)]
190#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
191pub struct mach_msg_trailer_t {
192    pub msgh_trailer_type: mach_msg_trailer_type_t,
193    pub msgh_trailer_size: mach_msg_trailer_size_t,
194}
195
196#[repr(C)]
197#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
198pub struct mach_msg_seqno_trailer_t {
199    pub msgh_trailer_type: mach_msg_trailer_type_t,
200    pub msgh_trailer_size: mach_msg_trailer_size_t,
201    pub msgh_seqno:        mach_port_seqno_t,
202}
203
204#[repr(C)]
205#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
206pub struct security_token_t {
207    pub val: [c_uint; 2],
208}
209
210#[repr(C)]
211#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
212pub struct mach_msg_security_trailer_t {
213    pub msgh_trailer_type: mach_msg_trailer_type_t,
214    pub msgh_trailer_size: mach_msg_trailer_size_t,
215    pub msgh_seqno:        mach_port_seqno_t,
216    pub msgh_sender:       security_token_t,
217}
218
219#[repr(C)]
220#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
221pub struct audit_token_t {
222    pub val: [c_uint; 8],
223}
224
225#[repr(C)]
226#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
227pub struct mach_msg_audit_trailer_t {
228    pub msgh_trailer_type: mach_msg_trailer_type_t,
229    pub msgh_trailer_size: mach_msg_trailer_size_t,
230    pub msgh_seqno:        mach_port_seqno_t,
231    pub msgh_sender:       security_token_t,
232    pub msgh_audit:        audit_token_t,
233}
234
235#[repr(C)]
236#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
237pub struct mach_msg_type_descriptor_t {
238    pub pad1:  natural_t,
239    pub pad2:  mach_msg_size_t,
240    pub pad3:  [u8; 3],
241    pub type_: u8, // mach_msg_descriptor_type_t bitfield
242}
243
244#[repr(C)]
245#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
246pub struct mach_msg_port_descriptor_t {
247    pub name:        mach_port_t,
248    pub pad1:        mach_msg_size_t,
249    pub pad2:        u16,
250    pub disposition: u8, // mach_msg_type_name_t bitfield
251    pub type_:       u8, // mach_msg_descriptor_type_t bitfield
252}
253
254impl mach_msg_port_descriptor_t {
255    pub const fn new(name: mach_port_t, disposition: mach_msg_type_name_t) -> Self {
256        Self {
257            name,
258            pad1: 0,
259            pad2: 0,
260            disposition: disposition as u8,
261            type_: MACH_MSG_PORT_DESCRIPTOR as u8,
262        }
263    }
264}
265
266#[repr(C)]
267#[derive(Copy, Clone, Debug, Hash, PartialOrd, PartialEq, Eq, Ord)]
268pub struct mach_msg_ool_descriptor_t {
269    pub address:    *mut c_void,
270
271    #[cfg(not(target_pointer_width = "64"))]
272    pub size:       mach_msg_size_t,
273
274    pub deallocate: u8, // boolean_t bitfield
275    pub copy:       u8, // mach_msg_copy_options_t bitfield
276    pub pad1:       u8,
277    pub type_:      u8, // mach_msg_descriptor_type_t bitfield
278
279    #[cfg(target_pointer_width = "64")]
280    pub size:       mach_msg_size_t,
281}
282
283impl mach_msg_ool_descriptor_t {
284    pub const fn new(
285        address:    *mut c_void,
286        deallocate: bool,
287        copy:       mach_msg_copy_options_t,
288        size:       mach_msg_size_t,
289    ) -> Self {
290        Self {
291            address,
292            deallocate: if deallocate { 1 } else { 0 },
293            copy: copy as u8,
294            pad1: 0,
295            type_: MACH_MSG_OOL_DESCRIPTOR as u8,
296            size,
297        }
298    }
299}
300
301#[repr(C)]
302#[derive(Copy, Clone, Debug, Hash, PartialOrd, PartialEq, Eq, Ord)]
303pub struct mach_msg_ool_ports_descriptor_t {
304    pub address:     *mut c_void,
305
306    #[cfg(not(target_pointer_width = "64"))]
307    pub count:       mach_msg_size_t,
308
309    pub deallocate:  u8, // boolean_t bitfield
310    pub copy:        u8, // mach_msg_copy_options_t bitfield
311    pub disposition: u8, // mach_msg_type_name_t bitfield
312    pub type_:       u8, // mach_msg_descriptor_type_t bitfield
313
314    #[cfg(target_pointer_width = "64")]
315    pub count:       mach_msg_size_t,
316}
317
318impl mach_msg_ool_ports_descriptor_t {
319    pub const fn new(
320        address:     *mut c_void,
321        deallocate:  bool,
322        copy:        mach_msg_copy_options_t,
323        disposition: mach_msg_type_name_t,
324        count:       mach_msg_size_t,
325    ) -> Self {
326        Self {
327            address,
328            deallocate: if deallocate { 1 } else { 0 },
329            copy: copy as u8,
330            disposition: disposition as u8,
331            type_: MACH_MSG_OOL_PORTS_DESCRIPTOR as u8,
332            count,
333        }
334    }
335}
336
337extern "C" {
338    pub fn mach_msg(
339        msg:       *mut mach_msg_header_t,
340        option:    mach_msg_option_t,
341        send_size: mach_msg_size_t,
342        recv_size: mach_msg_size_t,
343        recv_name: mach_port_name_t,
344        timeout:   mach_msg_timeout_t,
345        notify:    mach_port_name_t,
346    ) -> mach_msg_return_t;
347
348    // from mach/mach.h
349    pub fn mach_msg_send   (msg: *mut mach_msg_header_t) -> mach_msg_return_t;
350    pub fn mach_msg_destroy(msg: *mut mach_msg_header_t);
351}
352