sendfd.c

来自「unix环境高级编程的源代码」· C语言 代码 · 共 30 行

C
30
字号
#include    <sys/types.h>
#include    <stropts.h>
#include    "ourhdr.h"

/* Pass a file descriptor to another process.
 * If fd<0, then -fd is sent back instead as the error status. */

int
send_fd(int clifd, int fd)
{
    char    buf[2];        /* send_fd()/recv_fd() 2-byte protocol */

    buf[0] = 0;            /* null byte flag to recv_fd() */
    if (fd < 0) {
        buf[1] = -fd;    /* nonzero status means error */
        if (buf[1] == 0)
            buf[1] = 1;    /* -256, etc. would screw up protocol */
    } else {
        buf[1] = 0;        /* zero status means OK */
    }

    if (write(clifd, buf, 2) != 2)
        return(-1);

    if (fd >= 0)
        if (ioctl(clifd, I_SENDFD, fd) < 0)
            return(-1);
    return(0);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?