#include "cloexec.h"
+#ifndef MSG_CMSG_CLOEXEC
+# define MSG_CMSG_CLOEXEC 0
+#endif
+
/* sendfd sends the file descriptor fd along the socket
to a process calling recvfd on the other end.
sendfd (int sock, int fd)
{
char send = 0;
- struct iovec iov[1];
+ struct iovec iov;
struct msghdr msg;
/* send at least one char */
memset (&msg, 0, sizeof msg);
- iov[0].iov_base = &send;
- iov[0].iov_len = 1;
- msg.msg_iov = iov;
+ iov.iov_base = &send;
+ iov.iov_len = 1;
+ msg.msg_iov = &iov;
msg.msg_iovlen = 1;
- msg.msg_name = 0;
+ msg.msg_name = NULL;
msg.msg_namelen = 0;
{
#if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
struct cmsghdr *cmsg;
- char buf[CMSG_SPACE (sizeof (fd))];
+ char buf[CMSG_SPACE (sizeof fd)];
msg.msg_control = buf;
- msg.msg_controllen = sizeof (buf);
+ msg.msg_controllen = sizeof buf;
cmsg = CMSG_FIRSTHDR (&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
- cmsg->cmsg_len = CMSG_LEN (sizeof (int));
+ cmsg->cmsg_len = CMSG_LEN (sizeof fd);
/* Initialize the payload: */
- memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
+ memcpy (CMSG_DATA (cmsg), &fd, sizeof fd);
#elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY
msg.msg_accrights = &fd;
- msg.msg_accrightslen = sizeof (fd);
+ msg.msg_accrightslen = sizeof fd;
#else
errno = ENOSYS;
return -1;
#endif
}
- if (sendmsg (sock, &msg, 0) != iov[0].iov_len)
+ if (sendmsg (sock, &msg, 0) != iov.iov_len)
return -1;
return 0;
}
recvfd (int sock, int flags)
{
char recv = 0;
- struct iovec iov[1];
+ struct iovec iov;
struct msghdr msg;
if ((flags & ~O_CLOEXEC) != 0)
}
/* send at least one char */
- iov[0].iov_base = &recv;
- iov[0].iov_len = 1;
- msg.msg_iov = iov;
+ iov.iov_base = &recv;
+ iov.iov_len = 1;
+ msg.msg_iov = &iov;
msg.msg_iovlen = 1;
- msg.msg_name = 0;
+ msg.msg_name = NULL;
msg.msg_namelen = 0;
{
#if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
int fd;
struct cmsghdr *cmsg;
- char buf[CMSG_SPACE (sizeof (fd))];
+ char buf[CMSG_SPACE (sizeof fd)];
const int mone = -1;
-# if HAVE_MSG_CMSG_CLOEXEC
- int flags_recvmsg = (flags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0);
-# else
- int flags_recvmsg = 0;
-# endif
+ int flags_recvmsg = flags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0;
msg.msg_control = buf;
- msg.msg_controllen = sizeof (buf);
+ msg.msg_controllen = sizeof buf;
cmsg = CMSG_FIRSTHDR (&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
- cmsg->cmsg_len = CMSG_LEN (sizeof (int));
+ cmsg->cmsg_len = CMSG_LEN (sizeof mone);
/* Initialize the payload: */
- memcpy (CMSG_DATA (cmsg), &mone, sizeof (mone));
+ memcpy (CMSG_DATA (cmsg), &mone, sizeof mone);
msg.msg_controllen = cmsg->cmsg_len;
if (recvmsg (sock, &msg, flags_recvmsg) < 0)
cmsg = CMSG_FIRSTHDR (&msg);
/* be paranoiac */
- if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof (int))
+ if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof fd)
|| cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
{
/* fake errno: at end the file is not available */
return -1;
}
- memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
+ memcpy (&fd, CMSG_DATA (cmsg), sizeof fd);
-# if !HAVE_MSG_CMSG_CLOEXEC
/* set close-on-exec flag */
- if (flags & O_CLOEXEC)
+ if (!MSG_CMSG_CLOEXEC && (flags & O_CLOEXEC))
{
if (set_cloexec_flag (fd, true) < 0)
{
return -1;
}
}
-# endif
return fd;
int fd;
msg.msg_accrights = &fd;
- msg.msg_accrightslen = sizeof (fd);
+ msg.msg_accrightslen = sizeof fd;
if (recvmsg (sock, &msg, 0) < 0)
return -1;
if (set_cloexec_flag (fd, true) < 0)
{
int saved_errno = errno;
- (void) close (fd);
+ close (fd);
errno = saved_errno;
return -1;
}
-# afunix.m4 serial 5
+# afunix.m4 serial 6
dnl Copyright (C) 2011 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
AC_DEFINE([HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY], [1],
[Define to 1 if fd can be sent/received in the BSD4.3 way.])
fi
-
- AC_MSG_CHECKING([for UNIX domain sockets recvmsg() MSG_CMSG_CLOEXEC flag])
- AC_CACHE_VAL([gl_cv_socket_unix_msg_cmsg_cloexec],
- [AC_COMPILE_IFELSE(
- [AC_LANG_PROGRAM(
- [[#include <sys/types.h>
- #ifdef HAVE_SYS_SOCKET_H
- #include <sys/socket.h>
- #endif
- #ifdef HAVE_SYS_UN_H
- #include <sys/un.h>
- #endif
- #ifdef HAVE_WINSOCK2_H
- #include <winsock2.h>
- #endif
- ]],
- [[int flags = MSG_CMSG_CLOEXEC;
- if (&flags) return 0;
- ]])],
- [gl_cv_socket_unix_msg_cmsg_cloexec=yes],
- [gl_cv_socket_unix_msg_cmsg_cloexec=no])
- ])
- AC_MSG_RESULT([$gl_cv_socket_unix_msg_cmsg_cloexec])
- if test $gl_cv_socket_unix_msg_cmsg_cloexec = yes; then
- AC_DEFINE([HAVE_MSG_CMSG_CLOEXEC], [1],
- [Define to 1 if recvmsg could be specified with MSG_CMSG_CLOEXEC.])
- fi
])