我正在尝试将 mach 端口传递给fork
在 Mac OSX 上创建的子进程。我看到了这个 SO question Sharing Mach ports with child processes但它没有解决方案,它只是描述了问题。查看此站点https://robert.sesek.com/2014/1/changes_to_xnu_mach_ipc.html它包含有关将马赫端口传递给子进程的说明,但不幸的是它们不是示例代码。
我尝试实现端口交换,但子进程无法接收父进程发送的消息,mach_msg
内部recv_port
失败并带有invalid name
. 以下是我到目前为止所拥有的。抱歉,有这么多代码,mach IPC 有点让人难以简洁。
那么,既然引导端口黑客不再起作用,我如何将 mach 端口传递给 Mac OSX 上的子进程呢?
编辑
我更改了代码示例以反映 Ken Thomases 在他的回答中提出的观点,子进程创建一个具有发送权限的端口并将其发送给父进程。但是父进程无法接收子进程创建和发送的端口,只能挂起recv_port
。
#include <stdio.h>
#include <mach/mach.h>
#include <mach/error.h>
#include <mach/message.h>
#include <unistd.h>
static int32_t
send_port(mach_port_t remote_port, mach_port_t port)
{
kern_return_t err;
struct
{
mach_msg_header_t header;
mach_msg_body_t body;
mach_msg_port_descriptor_t task_port;
} msg;
msg.header.msgh_remote_port = remote_port;
msg.header.msgh_local_port = MACH_PORT_NULL;
msg.header.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND, 0) |
MACH_MSGH_BITS_COMPLEX;
msg.header.msgh_size = sizeof msg;
msg.body.msgh_descriptor_count = 1;
msg.task_port.name = port;
msg.task_port.disposition = MACH_MSG_TYPE_COPY_SEND;
msg.task_port.type = MACH_MSG_PORT_DESCRIPTOR;
err = mach_msg_send(&msg.header);
if(err != KERN_SUCCESS)
{
mach_error("Can't send mach msg\n", err);
return (-1);
}
return (0);
}
static int32_t
recv_port(mach_port_t recv_port, mach_port_t *port)
{
kern_return_t err;
struct
{
mach_msg_header_t header;
mach_msg_body_t body;
mach_msg_port_descriptor_t task_port;
mach_msg_trailer_t trailer;
} msg;
err = mach_msg(&msg.header, MACH_RCV_MSG,
0, sizeof msg, recv_port,
MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
if(err != KERN_SUCCESS)
{
mach_error("Can't recieve mach message\n", err);
return (-1);
}
*port = msg.task_port.name;
return 0;
}
static int32_t
setup_recv_port(mach_port_t *recv_port)
{
kern_return_t err;
mach_port_t port = MACH_PORT_NULL;
err = mach_port_allocate(mach_task_self (),
MACH_PORT_RIGHT_RECEIVE, &port);
if(err != KERN_SUCCESS)
{
mach_error("Can't allocate mach port\n", err);
return (-1);
}
err = mach_port_insert_right(mach_task_self (),
port,
port,
MACH_MSG_TYPE_MAKE_SEND);
if(err != KERN_SUCCESS)
{
mach_error("Can't insert port right\n", err);
return (-1);
}
(*recv_port) = port;
return (0);
}
pid_t
fork_pass_port(mach_port_t pass_port, int32_t (*child_start)(mach_port_t port, void *arg), void *arg)
{
pid_t pid = 0;
int32_t rtrn = 0;
kern_return_t err;
mach_port_t special_port = MACH_PORT_NULL;
/* Setup the mach port. */
if(setup_recv_port(&pass_port) != 0)
{
printf("Can't setup mach port\n");
return (-1);
}
/* Grab our current task's(process's) HOST_NAME special port. */
err = task_get_special_port(mach_task_self(), TASK_HOST_PORT, &special_port);
if(err != KERN_SUCCESS)
{
mach_error("Can't get special port:\n", err);
return (-1);
}
/* Set the HOST_NAME special port as the parent recv port. */
err = task_set_special_port(mach_task_self(), TASK_HOST_PORT, pass_port);
if(err != KERN_SUCCESS)
{
mach_error("Can't set special port:\n", err);
return (-1);
}
pid = fork();
if(pid == 0)
{
mach_port_t host_port = MACH_PORT_NULL;
mach_port_t port = MACH_PORT_NULL;
/* In the child process grab the port passed by the parent. */
err = task_get_special_port(mach_task_self(), TASK_HOST_PORT, &pass_port);
if(err != KERN_SUCCESS)
{
mach_error("Can't get special port:\n", err);
return (-1);
}
/* Create a port with a send right. */
if(setup_recv_port(&port) != 0)
{
printf("Can't setup mach port\n");
return (-1);
}
/* Send port to parent. */
rtrn = send_port(pass_port, port);
if(rtrn < 0)
{
printf("Can't send port\n");
return (-1);
}
/* Now that were done passing the mach port, start the function passed by the caller. */
child_start(pass_port, arg);
/* Exit and clean up the child process. */
_exit(0);
}
else if(pid > 0)
{
mach_port_t child_port = MACH_PORT_NULL;
rtrn = recv_port(pass_port, &child_port);
if(rtrn < 0)
{
printf("Can't recv port\n");
return (-1);
}
/* Reset parents special port. */
err = task_set_special_port(mach_task_self(), TASK_HOST_PORT, special_port);
if(err != KERN_SUCCESS)
{
mach_error("Can't set special port:\n", err);
return (-1);
}
return (0);
}
else
{
/* Error, so cleanup the mach port. */
err = mach_port_deallocate(mach_task_self(), pass_port);
if(err != KERN_SUCCESS)
{
mach_error("Can't deallocate mach port\n", err);
return (-1);
}
perror("fork");
return (-1);
}
}
static int32_t start(mach_port_t port, void *arg)
{
printf("Started\n");
return (0);
}
int main(void)
{
char *arg = "argument";
mach_port_t port = MACH_PORT_NULL;
pid_t pid = fork_pass_port(port, start, arg);
if(pid < 0)
{
printf("Can't fork and pass msg port\n");
return (-1);
}
return (0);
}