Не получается передать дескриптор дочернему процессу с помощью sendmsg
Передать дескриптор дочернему процессу, созданному с помощью fork(). Это можно сделать с помощью sendmsg, но у меня не получается. В чем ршибка, код ниже.
static
void wyslij(int socket, int fd) // send fd by socket
{
struct msghdr msg = { 0 };
char buf[CMSG_SPACE(sizeof(fd))];
memset(buf, '\0', sizeof(buf));
struct iovec io = { .iov_base = (void*)"ABC", .iov_len = 3 };
msg.msg_iov = &io;
msg.msg_iovlen = 1;
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_SPACE(sizeof(fd));
sendmsg(socket, &msg, 0);
}
static
int odbierz(int socket) // receive fd from socket
{
struct msghdr msg = {0};
char m_buffer[256];
struct iovec io = { .iov_base = m_buffer, .iov_len = sizeof(m_buffer) };
msg.msg_iov = &io;
msg.msg_iovlen = 1;
char c_buffer[256];
msg.msg_control = c_buffer;
msg.msg_controllen = sizeof(c_buffer);
recvmsg(socket, &msg, 0) < 0;
struct cmsghdr * cmsg = CMSG_FIRSTHDR(&msg);
unsigned char * data = CMSG_DATA(cmsg);
int fd = *((int*) data);
return fd;
}
int WS_server_worker();
void http_heandle(int size, int *fd, int client_d, int epfd);
int main()
{
int pipe_in[2];
int pipe_out[2];
pipe(pipe_in);
pipe(pipe_out);
int fdin;
int fdout;
if (fork() == 0)
{
close(pipe_in[1]);
close(pipe_out[0]);
dup2(pipe_in[0], 0);
dup2(pipe_out[1], 1);
int fd = odbierz(0);
printf("%d \n", fd);
}
close(pipe_in[0]);
close(pipe_out[1]);
fdin = dup(pipe_in[1]);
fdout = dup(pipe_out[0]);
int server_d = create_socket("8000");
wyslij(fdin, server_d);
return 0;
}