#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
int main(void)
{
int sv[2];
assert(0 == socketpair(AF_UNIX, SOCK_STREAM, 0, sv));
int sock1 = sv[0];
int sock2 = sv[1];
struct msghdr msg = {0};
char buf[] = "Hello world!";
struct iovec iov = {
.iov_base = buf,
.iov_len = sizeof(buf) - 1,
};
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
int fds[] = {1};
//int fds[] = {1, 2};
msg.msg_controllen = CMSG_LEN(sizeof(fds));
msg.msg_control = calloc(1, msg.msg_controllen);
assert(msg.msg_control != NULL);
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(fds));
memcpy(CMSG_DATA(cmsg), &fds, sizeof(fds));
assert(sendmsg(sock1, &msg, 0) >= 0);
assert(recvmsg(sock2, &msg, 0) == sizeof(buf) - 1);
}