2013-09-23 03:30:46 +02:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
|
|
|
* Copyright (c) 2007-2015, The Tor Project, Inc. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2013-09-26 05:25:02 +02:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2013-09-25 02:43:48 +02:00
|
|
|
#include "orconfig.h"
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <stdlib.h>
|
2013-09-23 03:30:46 +02:00
|
|
|
#include "compat.h"
|
2013-09-25 02:43:48 +02:00
|
|
|
#include "compat_threads.h"
|
|
|
|
|
2013-09-23 03:30:46 +02:00
|
|
|
#include "util.h"
|
2013-09-25 02:43:48 +02:00
|
|
|
#include "torlog.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_EVENTFD_H
|
|
|
|
#include <sys/eventfd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2013-09-26 05:25:02 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2013-09-23 03:30:46 +02:00
|
|
|
|
|
|
|
/** Return a newly allocated, ready-for-use mutex. */
|
|
|
|
tor_mutex_t *
|
|
|
|
tor_mutex_new(void)
|
|
|
|
{
|
|
|
|
tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t));
|
|
|
|
tor_mutex_init(m);
|
|
|
|
return m;
|
|
|
|
}
|
2013-09-25 17:41:40 +02:00
|
|
|
/** Return a newly allocated, ready-for-use mutex. This one might be
|
2013-09-27 18:32:19 +02:00
|
|
|
* non-recursive, if that's faster. */
|
2013-09-25 17:41:40 +02:00
|
|
|
tor_mutex_t *
|
2013-09-27 18:32:19 +02:00
|
|
|
tor_mutex_new_nonrecursive(void)
|
2013-09-25 17:41:40 +02:00
|
|
|
{
|
|
|
|
tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t));
|
2013-09-27 18:32:19 +02:00
|
|
|
tor_mutex_init_nonrecursive(m);
|
2013-09-25 17:41:40 +02:00
|
|
|
return m;
|
|
|
|
}
|
2013-09-23 03:30:46 +02:00
|
|
|
/** Release all storage and system resources held by <b>m</b>. */
|
|
|
|
void
|
|
|
|
tor_mutex_free(tor_mutex_t *m)
|
|
|
|
{
|
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
tor_mutex_uninit(m);
|
|
|
|
tor_free(m);
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:05:27 +02:00
|
|
|
/** Allocate and return a new condition variable. */
|
2013-09-23 07:15:30 +02:00
|
|
|
tor_cond_t *
|
|
|
|
tor_cond_new(void)
|
|
|
|
{
|
|
|
|
tor_cond_t *cond = tor_malloc(sizeof(tor_cond_t));
|
|
|
|
if (tor_cond_init(cond)<0)
|
|
|
|
tor_free(cond);
|
|
|
|
return cond;
|
|
|
|
}
|
2013-09-25 17:05:27 +02:00
|
|
|
|
|
|
|
/** Free all storage held in <b>c</b>. */
|
2013-09-23 07:15:30 +02:00
|
|
|
void
|
|
|
|
tor_cond_free(tor_cond_t *c)
|
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
return;
|
|
|
|
tor_cond_uninit(c);
|
|
|
|
tor_free(c);
|
|
|
|
}
|
|
|
|
|
2013-09-23 03:30:46 +02:00
|
|
|
/** Identity of the "main" thread */
|
|
|
|
static unsigned long main_thread_id = -1;
|
|
|
|
|
|
|
|
/** Start considering the current thread to be the 'main thread'. This has
|
|
|
|
* no effect on anything besides in_main_thread(). */
|
|
|
|
void
|
|
|
|
set_main_thread(void)
|
|
|
|
{
|
|
|
|
main_thread_id = tor_get_thread_id();
|
|
|
|
}
|
|
|
|
/** Return true iff called from the main thread. */
|
|
|
|
int
|
|
|
|
in_main_thread(void)
|
|
|
|
{
|
|
|
|
return main_thread_id == tor_get_thread_id();
|
|
|
|
}
|
2013-09-25 02:43:48 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_EVENTFD
|
|
|
|
static int
|
|
|
|
eventfd_alert(int fd)
|
|
|
|
{
|
|
|
|
uint64_t u = 1;
|
|
|
|
int r = write(fd, (void*)&u, sizeof(u));
|
|
|
|
if (r < 0 && errno != EAGAIN)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
eventfd_drain(int fd)
|
|
|
|
{
|
|
|
|
uint64_t u = 0;
|
|
|
|
int r = read(fd, (void*)&u, sizeof(u));
|
|
|
|
if (r < 0 && errno != EAGAIN)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_PIPE
|
|
|
|
static int
|
|
|
|
pipe_alert(int fd)
|
|
|
|
{
|
|
|
|
ssize_t r = write(fd, "x", 1);
|
|
|
|
if (r < 0 && errno != EAGAIN)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pipe_drain(int fd)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
ssize_t r;
|
|
|
|
while ((r = read(fd, buf, sizeof(buf))) >= 0)
|
|
|
|
;
|
|
|
|
if (r == 0 || errno != EAGAIN)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
|
|
|
sock_alert(tor_socket_t fd)
|
|
|
|
{
|
|
|
|
ssize_t r = send(fd, "x", 1, 0);
|
|
|
|
if (r < 0 && !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sock_drain(tor_socket_t fd)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
ssize_t r;
|
|
|
|
while ((r = recv(fd, buf, sizeof(buf), 0)) >= 0)
|
|
|
|
;
|
|
|
|
if (r == 0 || !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-25 17:05:27 +02:00
|
|
|
/** Allocate a new set of alert sockets, and set the appropriate function
|
|
|
|
* pointers, in <b>socks_out</b>. */
|
2013-09-25 02:43:48 +02:00
|
|
|
int
|
2013-09-25 20:31:59 +02:00
|
|
|
alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags)
|
2013-09-25 02:43:48 +02:00
|
|
|
{
|
2013-09-25 20:31:59 +02:00
|
|
|
tor_socket_t socks[2] = { TOR_INVALID_SOCKET, TOR_INVALID_SOCKET };
|
2013-09-25 02:43:48 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_EVENTFD
|
2013-09-25 17:05:27 +02:00
|
|
|
/* First, we try the Linux eventfd() syscall. This gives a 64-bit counter
|
|
|
|
* associated with a single file descriptor. */
|
2013-09-25 02:43:48 +02:00
|
|
|
#if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
|
2013-09-25 20:31:59 +02:00
|
|
|
if (!(flags & ASOCKS_NOEVENTFD2))
|
|
|
|
socks[0] = eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK);
|
2013-09-25 02:43:48 +02:00
|
|
|
#endif
|
2013-09-25 20:31:59 +02:00
|
|
|
if (socks[0] < 0 && !(flags & ASOCKS_NOEVENTFD)) {
|
2013-09-25 02:43:48 +02:00
|
|
|
socks[0] = eventfd(0,0);
|
|
|
|
if (socks[0] >= 0) {
|
|
|
|
if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
|
|
|
|
set_socket_nonblocking(socks[0]) < 0) {
|
|
|
|
close(socks[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (socks[0] >= 0) {
|
|
|
|
socks_out->read_fd = socks_out->write_fd = socks[0];
|
|
|
|
socks_out->alert_fn = eventfd_alert;
|
|
|
|
socks_out->drain_fn = eventfd_drain;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_PIPE2
|
2013-09-25 17:05:27 +02:00
|
|
|
/* Now we're going to try pipes. First type the pipe2() syscall, if we
|
|
|
|
* have it, so we can save some calls... */
|
2013-09-25 20:31:59 +02:00
|
|
|
if (!(flags & ASOCKS_NOPIPE2) &&
|
|
|
|
pipe2(socks, O_NONBLOCK|O_CLOEXEC) == 0) {
|
2013-09-25 02:43:48 +02:00
|
|
|
socks_out->read_fd = socks[0];
|
|
|
|
socks_out->write_fd = socks[1];
|
|
|
|
socks_out->alert_fn = pipe_alert;
|
|
|
|
socks_out->drain_fn = pipe_drain;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_PIPE
|
2013-09-25 17:05:27 +02:00
|
|
|
/* Now try the regular pipe() syscall. Pipes have a bit lower overhead than
|
|
|
|
* socketpairs, fwict. */
|
2013-09-25 20:31:59 +02:00
|
|
|
if (!(flags & ASOCKS_NOPIPE) &&
|
|
|
|
pipe(socks) == 0) {
|
2013-09-25 02:43:48 +02:00
|
|
|
if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
|
|
|
|
fcntl(socks[1], F_SETFD, FD_CLOEXEC) < 0 ||
|
|
|
|
set_socket_nonblocking(socks[0]) < 0 ||
|
|
|
|
set_socket_nonblocking(socks[1]) < 0) {
|
|
|
|
close(socks[0]);
|
|
|
|
close(socks[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
socks_out->read_fd = socks[0];
|
|
|
|
socks_out->write_fd = socks[1];
|
|
|
|
socks_out->alert_fn = pipe_alert;
|
|
|
|
socks_out->drain_fn = pipe_drain;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-25 17:05:27 +02:00
|
|
|
/* If nothing else worked, fall back on socketpair(). */
|
2013-09-25 20:31:59 +02:00
|
|
|
if (!(flags & ASOCKS_NOSOCKETPAIR) &&
|
|
|
|
tor_socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == 0) {
|
2013-09-25 17:05:27 +02:00
|
|
|
if (set_socket_nonblocking(socks[0]) < 0 ||
|
|
|
|
set_socket_nonblocking(socks[1])) {
|
|
|
|
tor_close_socket(socks[0]);
|
|
|
|
tor_close_socket(socks[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
socks_out->read_fd = socks[0];
|
|
|
|
socks_out->write_fd = socks[1];
|
2013-09-25 02:43:48 +02:00
|
|
|
socks_out->alert_fn = sock_alert;
|
|
|
|
socks_out->drain_fn = sock_drain;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-25 17:05:27 +02:00
|
|
|
|
|
|
|
/** Close the sockets in <b>socks</b>. */
|
|
|
|
void
|
|
|
|
alert_sockets_close(alert_sockets_t *socks)
|
|
|
|
{
|
|
|
|
if (socks->alert_fn == sock_alert) {
|
|
|
|
/* they are sockets. */
|
|
|
|
tor_close_socket(socks->read_fd);
|
|
|
|
tor_close_socket(socks->write_fd);
|
|
|
|
} else {
|
|
|
|
close(socks->read_fd);
|
|
|
|
if (socks->write_fd != socks->read_fd)
|
|
|
|
close(socks->write_fd);
|
|
|
|
}
|
|
|
|
socks->read_fd = socks->write_fd = -1;
|
|
|
|
}
|
2013-09-28 06:52:28 +02:00
|
|
|
|