3

I need to create a unit of execution with a different PID, but does not acquire resources from the initial process (open files, memory, ...).

On Linux, clone is used for this purpose. On other BSDs, rfork. Is there a Mac OS equivalent?

Alternatively, standard execve can be used to reset the process environment, but requires a name on the filesystem, which is suboptimal for my purposes. On Linux a memfd can be created and used with execveat. Is there a Mac OS equivalent?

  • 2
    I don't believe it's possible to execute a file that doesn't exist in the global file system namespace on macOS. I think that's what you're asking? (The only alternatives to `fork()` on macOS are `vfork()` and `posix_spawn()`, neither of which is any help with what you're trying to do, and the other `exec*()` variants on macOS are no more powerful than `execve()` itself.) – pmdj Apr 01 '19 at 19:17
  • Yes, that was what I was asking. Sucks to hear it isn't possible but that is what I expected to hear. I think I can at least limit some of the damage by using `vm_inherit` & `VM_INHERIT_SHARE` on regions I allocate (not sure, I am not very familiar with Mac-specific system programming) – Christopher Monsanto Apr 01 '19 at 23:17
  • Yes, that should work for memory. I don't think there's much you can do about file descriptors, as you can only mark them close-on-*exec*, not close-on-*fork*. – pmdj Apr 02 '19 at 14:34
  • One more option you could explore is using the Mach task APIs, i.e. `task_create()`, then manipulate the memory regions, followed by `thread_create_running()` to actually kick off execution. These are extremely low-level APIs however, and you'll need to do some extra work to get POSIX APIs working inside the new task. (e.g. you'll need to explicitly register the main thread as a POSIX thread) I don't know what happens to file descriptors when using `task_create()`. – pmdj Apr 02 '19 at 14:35
  • It's a good lead, but apparently [`task_create` was disabled in 10.5](https://robert.sesek.com/2014/1/changes_to_xnu_mach_ipc.html). – Christopher Monsanto Apr 02 '19 at 22:54

0 Answers0