开发者

Is it a bug to use msg_accrights to send &fd between processes?

Quoted from here:

    msg.msg_accrights = (caddr_t) &ch->fd;
    msg.msg_accrightslen = sizeof(in开发者_高级运维t);
    ...
    n = sendmsg(s, &msg, 0);

IMHO &ch->fd can't be shared between processes(the address of fd won't be available in another process),should transfer ch->fd directly,

am I right?


You are not sending the address of the fd. You are sending an array (with 1 element in this case). Since only one fd is sent, the address of the filedescriptor itself is used, as there is really no difference between doing that and doing:

int fds[1];
fds[1] = ch->fd;
msg.msg_accrights = (caddr_t) fds;
msg.msg_accrightslen = sizeof fds;

sendmsg will send the value of that array, so the other end will receive the file descriptor value, not the address of the file descriptor.


msg_accrights points to an array of file descriptors, so the code is correct when passing a single file descriptor.

The general form of this call is something like:

int fds[2];
fds[0] = an_fd;
fds[1] = another_fd;
msg.msg_accrights = (caddr_t) fds;
msg.msg_accrightslen = sizeof fds;
...
n = sendmsg(s, &msg, 0);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜