UNIX sockets

From vegard.wiki
Jump to navigation Jump to search

Passing file descriptors

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>

#include <errno.h>
#include <error.h>
#include <stdlib.h>

static void sendfd(int socket, int fd)
{
        char buf[CMSG_SPACE(sizeof(fd))];
        memset(buf, 0, sizeof(buf));

        struct msghdr msg = {};
        msg.msg_iov = 0;
        msg.msg_iovlen = 0;
        msg.msg_control = buf;
        msg.msg_controllen = sizeof(buf);

        struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
        cmsg->cmsg_level = SOL_SOCKET;
        cmsg->cmsg_type = SCM_RIGHTS;
        cmsg->cmsg_len = CMSG_LEN(sizeof(fd));

        *((int *) CMSG_DATA(cmsg)) = fd;

        msg.msg_controllen = cmsg->cmsg_len;

        if (sendmsg(socket, &msg, 0) == -1)
                error(EXIT_FAILURE, errno, "sendmsg()");
}

static int recvfd(int socket)
{
        struct msghdr msg = {};
        msg.msg_iov = 0;
        msg.msg_iovlen = 0;

        char buf[256];
        msg.msg_control = buf;
        msg.msg_controllen = sizeof(buf);

        if (recvmsg(socket, &msg, 0) == -1)
                error(EXIT_FAILURE, errno, "recvmsg()");

        struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
        unsigned char *data = CMSG_DATA(cmsg);
        int fd = *((int *) data);
        return fd;
}

int main(int argc, char *argv[])
{
        int unix_fd[2] = {};
        if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, unix_fd) == -1)
                error(EXIT_FAILURE, errno, "socketpair(AF_UNIX)");

        sendfd(unix_fd[0], 0);
        int fd = recvfd(unix_fd[1]);

        return 0;
}