#include <sys_defs.h>
#ifdef STREAM_CONNECTIONS
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stropts.h>
#endif
#include <msg.h>
#include <iostuff.h>
int stream_send_fd(int fd, int sendfd)
{
#ifdef STREAM_CONNECTIONS
const char *myname = "stream_send_fd";
if (ioctl(fd, I_SENDFD, sendfd) < 0)
msg_fatal("%s: send file descriptor %d: %m", myname, sendfd);
return (0);
#else
msg_fatal("stream connections are not implemented");
#endif
}
#ifdef TEST
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <split_at.h>
#include <connect.h>
int main(int argc, char **argv)
{
char *transport;
char *endpoint;
char *path;
int server_sock;
int client_fd;
if (argc < 3
|| (endpoint = split_at(transport = argv[1], ':')) == 0
|| *endpoint == 0 || *transport == 0)
msg_fatal("usage: %s transport:endpoint file...", argv[0]);
if (strcmp(transport, "stream") == 0) {
server_sock = stream_connect(endpoint, BLOCKING, 0);
} else {
msg_fatal("invalid transport name: %s", transport);
}
if (server_sock < 0)
msg_fatal("connect %s:%s: %m", transport, endpoint);
argv += 2;
while ((path = *argv++) != 0) {
if ((client_fd = open(path, O_RDONLY, 0)) < 0)
msg_fatal("open %s: %m", path);
msg_info("path=%s client_fd=%d", path, client_fd);
if (stream_send_fd(server_sock, client_fd) < 0)
msg_fatal("send file descriptor: %m");
if (close(client_fd) != 0)
msg_fatal("close(%d): %m", client_fd);
}
exit(0);
}
#endif