#spawn #task #port #process #child #send #child-process

bin+lib spawn-task-port

Spawn a child process on OS X and get the child's Mach task port

2 releases

Uses old Rust 2015

0.1.1 May 25, 2018
0.1.0 Mar 4, 2017

21 downloads per month
Used in akhramov-read-process-memory

MIT license

11KB
128 lines

Build Status crates.io

A crate to spawn a child process on OS X and get the child's Mach task port. Many useful OS X kernel APIs require access to the task port, and in recent releases of OS X the security around task_for_pid has been tightened such that it no longer works reliably even as root. However, for processes that you are spawning it is possible to have the child cooperate and send its task port to the parent. This crate uses CommandExt::before_exec and a handful of Mach APIs to have the child process do just that.

Much of this code is written using information from Michael Weber's Some Fun with Mach Ports blog post, and other bits were gleaned from Chromium's mach_port_broker.mm.

This crate was written so I could use it to write tests for the read-process-memory crate. You may find this crate useful in conjunction with that one!

Example

extern crate spawn_task_port;

use std::ffi::OsString;
use std::io;
use std::process::Command;
use spawn_task_port::CommandSpawnWithTask;

// Spawn `exe` with `args` as a child process and do interesting
// things to it.
fn do_some_work(exe: OsString, args: Vec<OsString>) -> io::Result<()> {
 let (mut child, task_port) = Command::new(&exe)
        .args(&args)
        .spawn_get_task_port()?;
 // Now you can call mach APIs that require a `mach_port_t` using `task_port`,
 // like `vm_read`.
 child.wait()?;
 Ok(())
}

fn main() {
   use std::env;
   let mut args = env::args_os().skip(1);
   let exe = args.next().unwrap();
   let rest: Vec<_> = args.collect();
   do_some_work(exe, rest).unwrap();
}

Documentation

https://docs.rs/spawn-task-port

Dependencies

~640KB
~11K SLoC