private static void machMoveToUserNamespace(int uid) throws NativeCallException {
    LibSystem libSystem;
    try {
      libSystem = Native.load("System", LibSystem.class);
    }
    catch (UnsatisfiedLinkError e) {
      throw new IllegalStateException("Unable to load libSystem", e);
    }

    NativeLibrary systemLibrary = Objects.requireNonNull(((Library.Handler)Proxy.getInvocationHandler(libSystem)).getNativeLibrary());

    IntByReference bootstrapPort = new IntByReference();
    bootstrapPort.setPointer(systemLibrary.getGlobalVariableAddress("bootstrap_port"));

    IntByReference rootPort = new IntByReference();
    if (libSystem.bootstrap_get_root(bootstrapPort.getValue(), rootPort) != LibSystem.KERN_SUCCESS) {
      throw new NativeCallException("bootstrap_get_root");
    }

    IntByReference userPort = new IntByReference();
    if (libSystem.bootstrap_look_up_per_user(bootstrapPort.getValue(), null, uid, userPort) != LibSystem.KERN_SUCCESS) {
      throw new NativeCallException("bootstrap_look_up_per_user");
    }

    int task = libSystem.mach_task_self();
    if (libSystem.task_set_special_port(task, LibSystem.TASK_BOOTSTRAP_PORT, userPort.getValue()) != LibSystem.KERN_SUCCESS) {
      throw new NativeCallException("task_set_bootstrap_port");
    }

    if (libSystem.mach_port_deallocate(task, bootstrapPort.getValue()) != LibSystem.KERN_SUCCESS) {
      throw new NativeCallException("mach_port_deallocate");
    }

    bootstrapPort.setValue(userPort.getValue());
  }
private static void machMoveToUserNamespace(int uid)