Commit 402dfb5fd3

Jakub Konka <kubkon@jakubkonka.com>
2022-12-11 18:27:10
darwin: wrap mach_port_insert_right kernel call
1 parent 2a65971
Changed files (2)
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,
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,