From 402dfb5fd3272fafd5d13a3065e44ad6b0b28a8b Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Sun, 11 Dec 2022 18:27:10 +0100 Subject: [PATCH] darwin: wrap mach_port_insert_right kernel call --- lib/std/c/darwin.zig | 39 +++++++++++++++++++++++++++++++++++++++ lib/std/os/darwin.zig | 16 ++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/lib/std/c/darwin.zig b/lib/std/c/darwin.zig index 7dd4d8394d..51c5068614 100644 --- a/lib/std/c/darwin.zig +++ b/lib/std/c/darwin.zig @@ -164,6 +164,7 @@ pub const mach_vm_address_t = usize; pub const vm_offset_t = usize; pub const mach_vm_size_t = u64; pub const mach_msg_type_number_t = natural_t; +pub const mach_msg_type_name_t = u32; pub const mach_port_right_t = natural_t; pub const ipc_space_t = mach_port_t; pub const ipc_space_port_t = ipc_space_t; @@ -180,6 +181,38 @@ pub const MACH_PORT_RIGHT = enum(mach_port_right_t) { NUMBER = 6, }; +pub const MACH_MSG_TYPE = enum(mach_msg_type_name_t) { + /// Must hold receive right + MOVE_RECEIVE = 16, + + /// Must hold send right(s) + MOVE_SEND = 17, + + /// Must hold sendonce right + MOVE_SEND_ONCE = 18, + + /// Must hold send right(s) + COPY_SEND = 19, + + /// Must hold receive right + MAKE_SEND = 20, + + /// Must hold receive right + MAKE_SEND_ONCE = 21, + + /// NOT VALID + COPY_RECEIVE = 22, + + /// Must hold receive right + DISPOSE_RECEIVE = 24, + + /// Must hold send right(s) + DISPOSE_SEND = 25, + + /// Must hold sendonce right + DISPOSE_SEND_ONCE = 26, +}; + extern "c" var mach_task_self_: mach_port_t; pub fn mach_task_self() callconv(.C) mach_port_t { return mach_task_self_; @@ -527,6 +560,12 @@ pub extern "c" fn mach_port_allocate( name: *mach_port_name_t, ) kern_return_t; pub extern "c" fn mach_port_deallocate(task: ipc_space_t, name: mach_port_name_t) kern_return_t; +pub extern "c" fn mach_port_insert_right( + task: ipc_space_t, + name: mach_port_name_t, + poly: mach_port_t, + poly_poly: mach_msg_type_name_t, +) kern_return_t; pub extern "c" fn task_info( target_task: task_name_t, diff --git a/lib/std/os/darwin.zig b/lib/std/os/darwin.zig index 1df5eaea5f..717eef0736 100644 --- a/lib/std/os/darwin.zig +++ b/lib/std/os/darwin.zig @@ -44,6 +44,22 @@ const mach_task = if (builtin.target.isDarwin()) struct { _ = std.c.getKernError(std.c.mach_port_deallocate(self.port, port.port)); } + pub fn insertRight(self: MachTask, port: MachTask, msg: std.c.MACH_MSG_TYPE) !void { + switch (std.c.getKernError(std.c.mach_port_insert_right( + self.port, + port.port, + port.port, + @enumToInt(msg), + ))) { + .SUCCESS => return, + .FAILURE => return error.PermissionDenied, + else => |err| { + log.err("mach_port_insert_right kernel call failed with error code: {s}", .{@tagName(err)}); + return error.Unexpected; + }, + } + } + pub const RegionInfo = struct { pub const Tag = enum { basic,