22#ifdef HAVE_SYS_EVENTFD_H
23#include <sys/eventfd.h>
31#ifdef HAVE_SYS_SOCKET_H
32#include <sys/socket.h>
38#if defined(HAVE_EVENTFD) || defined(HAVE_PIPE)
42write_ni(
int fd,
const void *buf,
size_t n)
46 r = (int) write(fd, buf, n);
58read_ni(
int fd,
void *buf,
size_t n)
62 r = (int) read(fd, buf, n);
76send_ni(
int fd,
const void *buf,
size_t n,
int flags)
80 r = (int) send(fd, buf, n, flags);
82 int error = tor_socket_errno(fd);
83 if (ERRNO_IS_EINTR(error))
94recv_ni(
int fd,
void *buf,
size_t n,
int flags)
98 r = (int) recv(fd, buf, n, flags);
100 int error = tor_socket_errno(fd);
101 if (ERRNO_IS_EINTR(error))
115 int r = write_ni(fd, (
void*)&u,
sizeof(u));
116 if (r < 0 && -r != EAGAIN)
126 int r = read_ni(fd, (
void*)&u,
sizeof(u));
127 if (r < 0 && -r != EAGAIN)
138 ssize_t r = write_ni(fd,
"x", 1);
139 if (r < 0 && -r != EAGAIN)
152 r = read_ni(fd, buf,
sizeof(buf));
154 if (r < 0 && errno != EAGAIN)
166 ssize_t r =
send_ni(fd,
"x", 1, 0);
167 if (r < 0 && !ERRNO_IS_EAGAIN(-r))
180 r =
recv_ni(fd, buf,
sizeof(buf), 0);
182 if (r < 0 && !ERRNO_IS_EAGAIN(-r))
198#if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
199 if (!(flags & ASOCKS_NOEVENTFD2))
200 socks[0] = eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK);
202 if (socks[0] < 0 && !(flags & ASOCKS_NOEVENTFD)) {
203 socks[0] = eventfd(0,0);
205 if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
217 socks_out->
alert_fn = eventfd_alert;
218 socks_out->
drain_fn = eventfd_drain;
226 if (!(flags & ASOCKS_NOPIPE2) &&
227 pipe2(socks, O_NONBLOCK|O_CLOEXEC) == 0) {
239 if (!(flags & ASOCKS_NOPIPE) &&
241 if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
242 fcntl(socks[1], F_SETFD, FD_CLOEXEC) < 0 ||
261 if (!(flags & ASOCKS_NOSOCKETPAIR) &&
int alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags)
static int recv_ni(int fd, void *buf, size_t n, int flags)
static int sock_alert(tor_socket_t fd)
static int sock_drain(tor_socket_t fd)
void alert_sockets_close(alert_sockets_t *socks)
static int send_ni(int fd, const void *buf, size_t n, int flags)
#define TOR_INVALID_SOCKET
int tor_close_socket(tor_socket_t s)
int set_socket_nonblocking(tor_socket_t sock)
int tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
int(* alert_fn)(tor_socket_t write_fd)
int(* drain_fn)(tor_socket_t read_fd)
Macros to manage assertions, fatal and non-fatal.
#define tor_assert_nonfatal_unreached()