1 #ifndef _DINIT_SOCKET_H_INCLUDED
2 #define _DINIT_SOCKET_H_INCLUDED
4 #include <sys/socket.h>
8 #if !defined(SOCK_NONBLOCK) && !defined(SOCK_CLOEXEC)
9 // make our own accept4 on systems that don't have it:
10 constexpr int SOCK_NONBLOCK = 1;
11 constexpr int SOCK_CLOEXEC = 2;
12 inline int dinit_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)
14 int fd = accept(sockfd, addr, addrlen);
19 if (flags & SOCK_CLOEXEC) fcntl(fd, F_SETFD, FD_CLOEXEC);
20 if (flags & SOCK_NONBLOCK) fcntl(fd, F_SETFL, O_NONBLOCK);
24 inline int dinit_socket(int domain, int type, int protocol, int flags)
26 int fd = socket(domain, type, protocol);
31 if (flags & SOCK_CLOEXEC) fcntl(fd, F_SETFD, FD_CLOEXEC);
32 if (flags & SOCK_NONBLOCK) fcntl(fd, F_SETFL, O_NONBLOCK);
36 inline int dinit_socketpair(int domain, int type, int protocol, int socket_vector[2], int flags)
38 int r = socketpair(domain, type, protocol, socket_vector);
43 if (flags & SOCK_CLOEXEC) {
44 fcntl(socket_vector[0], F_SETFD, FD_CLOEXEC);
45 fcntl(socket_vector[1], F_SETFD, FD_CLOEXEC);
47 if (flags & SOCK_NONBLOCK) {
48 fcntl(socket_vector[0], F_SETFL, O_NONBLOCK);
49 fcntl(socket_vector[1], F_SETFL, O_NONBLOCK);
55 inline int dinit_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)
57 return accept4(sockfd, addr, addrlen, flags);
60 inline int dinit_socket(int domain, int type, int protocol, int flags)
62 return socket(domain, type | flags, protocol);
65 inline int dinit_socketpair(int domain, int type, int protocol, int socket_vector[2], int flags)
67 return socketpair(domain, type | flags, protocol, socket_vector);