#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
-#include <sys/uio.h>
#include <unistd.h>
#include <sys/socket.h>
# define MSG_CMSG_CLOEXEC 0
#endif
+#if HAVE_SENDMSG
/* sendfd sends the file descriptor fd along the socket
to a process calling recvfd on the other end.
char send = 0;
struct iovec iov;
struct msghdr msg;
-#if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
+# if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
struct cmsghdr *cmsg;
char buf[CMSG_SPACE (sizeof fd)];
-#endif
+# endif
/* send at least one char */
memset (&msg, 0, sizeof msg);
msg.msg_name = NULL;
msg.msg_namelen = 0;
-#if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
+# if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
msg.msg_control = buf;
msg.msg_controllen = sizeof buf;
cmsg = CMSG_FIRSTHDR (&msg);
cmsg->cmsg_len = CMSG_LEN (sizeof fd);
/* Initialize the payload: */
memcpy (CMSG_DATA (cmsg), &fd, sizeof fd);
-#elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY
+# elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY
msg.msg_accrights = &fd;
msg.msg_accrightslen = sizeof fd;
-#else
+# else
errno = ENOSYS;
return -1;
-#endif
+# endif
if (sendmsg (sock, &msg, 0) != iov.iov_len)
return -1;
return 0;
}
+#else
+int
+sendfd (int sock _GL_UNUSED, int fd _GL_UNUSED)
+{
+ errno = ENOSYS;
+ return -1;
+}
+#endif
+
+#if HAVE_RECVMSG
/* recvfd receives a file descriptor through the socket.
The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>).
struct iovec iov;
struct msghdr msg;
int fd = -1;
-#if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
+# if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
struct cmsghdr *cmsg;
char buf[CMSG_SPACE (sizeof fd)];
int flags_recvmsg = flags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0;
-#endif
+# endif
if ((flags & ~O_CLOEXEC) != 0)
{
msg.msg_name = NULL;
msg.msg_namelen = 0;
-#if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
+# if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
msg.msg_control = buf;
msg.msg_controllen = sizeof buf;
cmsg = CMSG_FIRSTHDR (&msg);
}
}
-#elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY
+# elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY
msg.msg_accrights = &fd;
msg.msg_accrightslen = sizeof fd;
if (recvmsg (sock, &msg, 0) < 0)
return -1;
}
}
-#else
+# else
errno = ENOSYS;
-#endif
+# endif
return fd;
}
+#else
+int
+recvfd (int sock _GL_UNUSED, int flags _GL_UNUSED)
+{
+ errno = ENOSYS;
+ return -1;
+}
+#endif