Browse Source

support SOCK_CLOEXEC on macOS

OPENBSD_6_2
Brent Cook 7 years ago
parent
commit
19d2615f52
3 changed files with 46 additions and 0 deletions
  1. +2
    -0
      compat/Makefile.am
  2. +29
    -0
      compat/socket.c
  3. +15
    -0
      include/sys/socket.h

+ 2
- 0
compat/Makefile.am View File

@ -29,6 +29,8 @@ endif
libcompat_la_SOURCES = libcompat_la_SOURCES =
libcompat_la_LIBADD = $(PLATFORM_LDADD) libcompat_la_LIBADD = $(PLATFORM_LDADD)
libcompat_la_SOURCES += socket.c
if !HAVE_ADJFREQ if !HAVE_ADJFREQ
if HOST_FREEBSD if HOST_FREEBSD
libcompat_la_SOURCES += adjfreq_freebsd.c libcompat_la_SOURCES += adjfreq_freebsd.c


+ 29
- 0
compat/socket.c View File

@ -0,0 +1,29 @@
#define SOCKET_FLAGS_PRIV
#include <sys/socket.h>
#ifdef NEED_SOCKET_FLAGS
#include <fcntl.h>
int
_socket(int domain, int type, int protocol)
{
int s = socket(domain, type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK), protocol);
int flags;
if (s == -1)
return s;
if (type & SOCK_CLOEXEC) {
flags = fcntl(s, F_GETFD);
fcntl(s, F_SETFD, flags | FD_CLOEXEC);
}
if (type & SOCK_NONBLOCK) {
flags = fcntl(s, F_GETFL);
fcntl(s, F_SETFL, flags | O_NONBLOCK);
}
return s;
}
#endif

+ 15
- 0
include/sys/socket.h View File

@ -11,6 +11,15 @@
((struct sockaddr*)(X))->sa_family == AF_INET6 ? sizeof(struct sockaddr_in6) : \ ((struct sockaddr*)(X))->sa_family == AF_INET6 ? sizeof(struct sockaddr_in6) : \
((struct sockaddr*)(X))->sa_family == AF_UNSPEC ? sizeof(struct sockaddr) : \ ((struct sockaddr*)(X))->sa_family == AF_UNSPEC ? sizeof(struct sockaddr) : \
sizeof(struct sockaddr)) sizeof(struct sockaddr))
#endif
#if !defined(SOCK_NONBLOCK) || !defined(SOCK_CLOEXEC)
#define NEED_SOCKET_FLAGS
int _socket(int domain, int type, int protocol);
#ifndef SOCKET_FLAGS_PRIV
#define socket(d, t, p) _socket(d, t, p)
#endif
#endif
/* /*
* Prevent Solaris 10 system header leakage * Prevent Solaris 10 system header leakage
@ -19,4 +28,10 @@
#undef MODEMASK #undef MODEMASK
#endif #endif
#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK 0x4000 /* set O_NONBLOCK */
#endif
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC 0x8000 /* set FD_CLOEXEC */
#endif #endif

Loading…
Cancel
Save