设置复杂的马赫消息头

时间:2016-03-13 19:30:42

标签: c macos ipc mach

我试图设置一个马赫消息,用于使用mach IPC发送字符串。但是,在尝试发送离线消息时出现(ipc/send) invalid msg-header错误。

这是消息结构。

struct ool_msg
{
    mach_msg_header_t header;
    mach_msg_body_t body;
    mach_msg_ool_descriptor_t data;
    mach_msg_type_number_t count;
};

我设置这样的标题。下面的变量msgstruct ool_msg

hdr = &(msg.header);
hdr->msgh_bits = MACH_MSGH_BITS_LOCAL(MACH_MSG_TYPE_COPY_SEND);
hdr->msgh_bits |= MACH_MSGH_BITS_COMPLEX;
hdr->msgh_size = sizeof(msg);
hdr->msgh_local_port = MACH_PORT_NULL;
hdr->msgh_remote_port = port;

这就是我如何设置Mach消息的其余部分。

msg.body.msgh_descriptor_count = 1;
msg.data.address = string;
msg.data.size = strlen(string) + 1;
msg.data.deallocate = FALSE;
msg.data.copy = MACH_MSG_VIRTUAL_COPY;
msg.data.type = MACH_MSG_OOL_DESCRIPTOR;
msg.count = msg.data.size;

那么如何正确设置OOL Mach IPC的消息头?

修改

这是一个可以重现我的问题的程序。

#include <stdio.h>
#include <mach/mach.h>

static char *string = "123456789";

struct ool_msg
{
    mach_msg_header_t header;
    mach_msg_body_t body;
    mach_msg_ool_descriptor_t data;
    mach_msg_type_number_t count;
};

int main(void)
{
    kern_return_t err;
    struct ool_msg msg;
    mach_msg_header_t *hdr;
    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);
    }

    hdr = &(msg.header);
    hdr->msgh_bits = MACH_MSGH_BITS_LOCAL(MACH_MSG_TYPE_COPY_SEND);
    hdr->msgh_bits |= MACH_MSGH_BITS_COMPLEX;
    hdr->msgh_size = sizeof(msg);
    hdr->msgh_local_port = MACH_PORT_NULL;
    hdr->msgh_remote_port = port;

    msg.body.msgh_descriptor_count = 1;
    msg.data.address = string;
    msg.data.size = strlen(string) + 1;
    msg.data.deallocate = FALSE;
    msg.data.copy = MACH_MSG_VIRTUAL_COPY;
    msg.data.type = MACH_MSG_OOL_DESCRIPTOR;
    msg.count = msg.data.size;

    err = mach_msg(hdr,
                  MACH_SEND_MSG,
                  hdr->msgh_size,
                  0,
                  MACH_PORT_NULL,
                  MACH_MSG_TIMEOUT_NONE,
                  MACH_PORT_NULL);
    if(err != KERN_SUCCESS)
    {
        mach_error("Can't send mach msg\n", err);
        return (-1);
    }

    return (0);
}

1 个答案:

答案 0 :(得分:3)

作为the docs州:

  

import { stringify, parse } from 'qs' import { Router, useRouterHistory } from 'react-router' import createBrowserHistory from 'history/lib/createBrowserHistory' const stringifyQuery = query => stringify(query, { arrayFormat: 'brackets' }) const history = useRouterHistory(createBrowserHistory)({ parseQueryString: parse, stringifyQuery }) <Router history={history} /> MACH_MSGH_BITS_REMOTE宏返回   给定MACH_MSGH_BITS_LOCAL值的mach_msg_type_name_t值。该   msgh_bits宏构造MACH_MSGH_BITS的值,给定两个   msgh_bits值。

因此,在设置mach_msg_type_name_t时,您需要使用MACH_MSGH_BITS宏而不是MACH_MSGH_BITS_LOCAL宏。否则,您将其设置为mach_msg_header_t.msgh_bits,而不是您想要的mach_msg_type_name_t

为了澄清,您需要类似下面的代码来获取msgh_bits:

msgh_bits