#include #include #include #ifndef NULL #define NULL ((void *) 0) #endif // http://www.thomasstover.com/uds.html int send_fd(int socket, int fd_to_send) { if(fd_to_send==-1) { return send(socket, "X", 1, 0); } struct msghdr socket_message; struct iovec io_vector[1]; struct cmsghdr *control_message = NULL; char message_buffer[1]; /* storage space needed for an ancillary element with a paylod of length is CMSG_SPACE(sizeof(length)) */ char ancillary_element_buffer[CMSG_SPACE(sizeof(int))]; int available_ancillary_element_buffer_space; /* at least one vector of one byte must be sent */ message_buffer[0] = 'F'; io_vector[0].iov_base = message_buffer; io_vector[0].iov_len = 1; /* initialize socket message */ memset(&socket_message, 0, sizeof(struct msghdr)); socket_message.msg_iov = io_vector; socket_message.msg_iovlen = 1; /* provide space for the ancillary data */ available_ancillary_element_buffer_space = CMSG_SPACE(sizeof(int)); memset(ancillary_element_buffer, 0, available_ancillary_element_buffer_space); socket_message.msg_control = ancillary_element_buffer; socket_message.msg_controllen = available_ancillary_element_buffer_space; /* initialize a single ancillary data element for fd passing */ control_message = CMSG_FIRSTHDR(&socket_message); control_message->cmsg_level = SOL_SOCKET; control_message->cmsg_type = SCM_RIGHTS; control_message->cmsg_len = CMSG_LEN(sizeof(int)); *((int *) CMSG_DATA(control_message)) = fd_to_send; return sendmsg(socket, &socket_message, 0); }