mach_rs

Summary

Mach ports are a powerful IPC mechanism used for most of the low-level communications between processes and kernel on Darwin. I/O Kit, largely the only way to communicate with kernel drivers on these platforms, and XPC, the most widespread userspace IPC mechanism, are both built using the Mach ports. Exposing low-level APIs on these systems in a safe way requires abstractions mach_rs library aims to provide.

Mach port name wrappers

Mach ports are represented by handles called "names" in userspace. Unlike file descriptors, these refer to one or multiple "rights" on a port:

While send and receive rights, for example, are referenced by the same name inside the same process (IPC space) the reference counts for the rights are managed separately. mach_rs makes this distinction on the type level by providing 3 wrappers for Mach port names (there is no need to represent dead names separately):

Sending/Receiving Mach messages

Mach message format is quite non-trivial. All Mach messages start with a fixed-size header followed by optional descriptors, inline data and a trailer that is only present in received messages.

Complex Mach message format diagram

mach_rs provides data structures that can be used to build and parse such messages. See docs for the msg module for more info.

License

mach_rs is distributed under the terms of both the MIT license and the Apache License (Version 2.0).