2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2017-03-15 21:13:17 +01:00
|
|
|
* Copyright (c) 2007-2017, The Tor Project, Inc. */
|
2004-11-01 21:41:47 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2005-06-11 07:31:17 +02:00
|
|
|
/**
|
|
|
|
* \file compat.c
|
|
|
|
* \brief Wrappers to make calls more portable. This code defines
|
2016-02-28 17:57:47 +01:00
|
|
|
* functions such as tor_snprintf, get/set various data types,
|
2005-06-11 07:31:17 +02:00
|
|
|
* renaming, setting socket options, switching user IDs. It is basically
|
|
|
|
* where the non-portable items are conditionally included depending on
|
|
|
|
* the platform.
|
|
|
|
**/
|
|
|
|
|
2013-08-02 16:36:01 +02:00
|
|
|
#define COMPAT_PRIVATE
|
2004-11-04 05:01:19 +01:00
|
|
|
#include "compat.h"
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2015-06-29 22:09:37 +02:00
|
|
|
#include <winsock2.h>
|
2006-08-04 20:32:43 +02:00
|
|
|
#include <windows.h>
|
2008-09-01 22:06:26 +02:00
|
|
|
#include <sys/locking.h>
|
2004-11-01 21:41:47 +01:00
|
|
|
#endif
|
2006-08-04 20:32:43 +02:00
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
#ifdef HAVE_UNAME
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#endif
|
2014-03-19 19:17:55 +01:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
2014-04-03 17:46:01 +02:00
|
|
|
#ifdef HAVE_SYS_SYSCTL_H
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
2014-03-19 19:17:55 +01:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2016-07-08 16:38:59 +02:00
|
|
|
#ifdef HAVE_UTIME_H
|
|
|
|
#include <utime.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_UTIME_H
|
|
|
|
#include <sys/utime.h>
|
|
|
|
#endif
|
2004-11-01 21:41:47 +01:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_FCNTL_H
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_GRP_H
|
|
|
|
#include <grp.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
2012-02-14 12:21:03 +01:00
|
|
|
#ifdef HAVE_CRT_EXTERNS_H
|
|
|
|
#include <crt_externs.h>
|
|
|
|
#endif
|
2015-08-05 20:01:49 +02:00
|
|
|
#ifdef HAVE_SYS_STATVFS_H
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#endif
|
2015-11-06 19:12:44 +01:00
|
|
|
#ifdef HAVE_SYS_CAPABILITY_H
|
|
|
|
#include <sys/capability.h>
|
|
|
|
#endif
|
2015-07-10 15:16:32 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <conio.h>
|
|
|
|
#include <wchar.h>
|
2015-07-10 15:57:22 +02:00
|
|
|
/* Some mingw headers lack these. :p */
|
2015-07-12 21:02:43 +02:00
|
|
|
#if defined(HAVE_DECL__GETWCH) && !HAVE_DECL__GETWCH
|
2015-07-10 15:57:22 +02:00
|
|
|
wint_t _getwch(void);
|
|
|
|
#endif
|
|
|
|
#ifndef WEOF
|
|
|
|
#define WEOF (wchar_t)(0xFFFF)
|
|
|
|
#endif
|
2015-07-12 21:02:43 +02:00
|
|
|
#if defined(HAVE_DECL_SECUREZEROMEMORY) && !HAVE_DECL_SECUREZEROMEMORY
|
2015-07-16 17:10:14 +02:00
|
|
|
static inline void
|
|
|
|
SecureZeroMemory(PVOID ptr, SIZE_T cnt)
|
2015-07-12 21:30:35 +02:00
|
|
|
{
|
|
|
|
volatile char *vcptr = (volatile char*)ptr;
|
|
|
|
while (cnt--)
|
|
|
|
*vcptr++ = 0;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_DECL_SECUREZEROMEMORY) && !HAVE_DECL_SECUREZEROMEMORY */
|
2015-07-10 15:16:32 +02:00
|
|
|
#elif defined(HAVE_READPASSPHRASE_H)
|
2015-03-01 15:35:36 +01:00
|
|
|
#include <readpassphrase.h>
|
2015-06-17 17:22:31 +02:00
|
|
|
#else
|
2015-07-10 15:16:32 +02:00
|
|
|
#include "tor_readpassphrase.h"
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) || ... */
|
2008-09-01 23:16:07 +02:00
|
|
|
|
2011-11-25 05:45:47 +01:00
|
|
|
/* Includes for the process attaching prevention */
|
|
|
|
#if defined(HAVE_SYS_PRCTL_H) && defined(__linux__)
|
2014-10-22 17:35:46 +02:00
|
|
|
/* Only use the linux prctl; the IRIX prctl is totally different */
|
2011-11-25 05:45:47 +01:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
#include <sys/ptrace.h>
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_SYS_PRCTL_H) && defined(__linux__) || ... */
|
2011-11-25 05:45:47 +01:00
|
|
|
|
2004-12-22 06:29:06 +01:00
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
2004-11-01 21:41:47 +01:00
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
#include <sys/param.h> /* FreeBSD needs this to know what version it is */
|
|
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2004-11-03 08:29:03 +01:00
|
|
|
#include <assert.h>
|
2007-02-20 19:34:18 +01:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2006-05-28 18:54:39 +02:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
2007-11-02 17:02:26 +01:00
|
|
|
#ifdef HAVE_SYS_SYSLIMITS_H
|
|
|
|
#include <sys/syslimits.h>
|
|
|
|
#endif
|
2008-09-01 22:06:26 +02:00
|
|
|
#ifdef HAVE_SYS_FILE_H
|
|
|
|
#include <sys/file.h>
|
|
|
|
#endif
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2010-07-10 03:52:20 +02:00
|
|
|
#include "torlog.h"
|
2004-11-01 21:41:47 +01:00
|
|
|
#include "util.h"
|
2007-07-06 05:49:57 +02:00
|
|
|
#include "container.h"
|
2008-07-25 16:43:24 +02:00
|
|
|
#include "address.h"
|
2013-08-14 23:09:07 +02:00
|
|
|
#include "sandbox.h"
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2004-11-09 02:24:10 +01:00
|
|
|
/* Inline the strl functions if the platform doesn't have them. */
|
2004-11-02 20:25:52 +01:00
|
|
|
#ifndef HAVE_STRLCPY
|
|
|
|
#include "strlcpy.c"
|
|
|
|
#endif
|
|
|
|
#ifndef HAVE_STRLCAT
|
|
|
|
#include "strlcat.c"
|
|
|
|
#endif
|
|
|
|
|
2015-06-03 19:56:01 +02:00
|
|
|
/* When set_max_file_descriptors() is called, update this with the max file
|
|
|
|
* descriptor value so we can use it to check the limit when opening a new
|
|
|
|
* socket. Default value is what Debian sets as the default hard limit. */
|
|
|
|
static int max_sockets = 1024;
|
|
|
|
|
2010-11-20 06:58:33 +01:00
|
|
|
/** As open(path, flags, mode), but return an fd with the close-on-exec mode
|
|
|
|
* set. */
|
|
|
|
int
|
|
|
|
tor_open_cloexec(const char *path, int flags, unsigned mode)
|
|
|
|
{
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
int fd;
|
2015-07-07 18:17:31 +02:00
|
|
|
const char *p = sandbox_intern_string(path);
|
2010-11-20 06:58:33 +01:00
|
|
|
#ifdef O_CLOEXEC
|
2014-10-26 04:43:55 +01:00
|
|
|
fd = open(p, flags|O_CLOEXEC, mode);
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
if (fd >= 0)
|
|
|
|
return fd;
|
|
|
|
/* If we got an error, see if it is EINVAL. EINVAL might indicate that,
|
2012-05-07 07:54:53 +02:00
|
|
|
* even though we were built on a system with O_CLOEXEC support, we
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
* are running on one without. */
|
|
|
|
if (errno != EINVAL)
|
|
|
|
return -1;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(O_CLOEXEC) */
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
|
2014-10-26 04:43:55 +01:00
|
|
|
log_debug(LD_FS, "Opening %s with flags %x", p, flags);
|
|
|
|
fd = open(p, flags, mode);
|
2010-11-20 06:58:33 +01:00
|
|
|
#ifdef FD_CLOEXEC
|
2013-03-12 01:58:28 +01:00
|
|
|
if (fd >= 0) {
|
|
|
|
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
|
|
|
|
log_warn(LD_FS,"Couldn't set FD_CLOEXEC: %s", strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(FD_CLOEXEC) */
|
2010-11-20 06:58:33 +01:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2012-06-07 21:35:19 +02:00
|
|
|
/** As fopen(path,mode), but ensures that the O_CLOEXEC bit is set on the
|
2012-06-05 00:50:13 +02:00
|
|
|
* underlying file handle. */
|
2010-11-20 07:16:29 +01:00
|
|
|
FILE *
|
|
|
|
tor_fopen_cloexec(const char *path, const char *mode)
|
|
|
|
{
|
|
|
|
FILE *result = fopen(path, mode);
|
|
|
|
#ifdef FD_CLOEXEC
|
2013-03-12 01:58:28 +01:00
|
|
|
if (result != NULL) {
|
|
|
|
if (fcntl(fileno(result), F_SETFD, FD_CLOEXEC) == -1) {
|
|
|
|
log_warn(LD_FS,"Couldn't set FD_CLOEXEC: %s", strerror(errno));
|
|
|
|
fclose(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(FD_CLOEXEC) */
|
2010-11-20 07:16:29 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-03-28 08:51:50 +01:00
|
|
|
/** As rename(), but work correctly with the sandbox. */
|
|
|
|
int
|
|
|
|
tor_rename(const char *path_old, const char *path_new)
|
|
|
|
{
|
2014-04-16 19:17:09 +02:00
|
|
|
log_debug(LD_FS, "Renaming %s to %s", path_old, path_new);
|
2014-03-28 08:51:50 +01:00
|
|
|
return rename(sandbox_intern_string(path_old),
|
|
|
|
sandbox_intern_string(path_new));
|
|
|
|
}
|
|
|
|
|
2016-11-02 22:44:57 +01:00
|
|
|
/* Some MinGW builds have sys/mman.h, but not the corresponding symbols.
|
|
|
|
* Other configs rename the symbols using macros (including getpagesize).
|
|
|
|
* So check for sys/mman.h and unistd.h, and a getpagesize declaration. */
|
|
|
|
#if (defined(HAVE_SYS_MMAN_H) && defined(HAVE_UNISTD_H) && \
|
|
|
|
defined(HAVE_DECL_GETPAGESIZE))
|
|
|
|
#define COMPAT_HAS_MMAN_AND_PAGESIZE
|
|
|
|
#endif
|
|
|
|
|
2017-09-15 22:15:27 +02:00
|
|
|
#if defined(COMPAT_HAS_MMAN_AND_PAGESIZE) || \
|
|
|
|
defined(RUNNING_DOXYGEN)
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Try to create a memory mapping for <b>filename</b> and return it. On
|
2007-10-24 17:45:42 +02:00
|
|
|
* failure, return NULL. Sets errno properly, using ERANGE to mean
|
|
|
|
* "empty file". */
|
2006-08-04 20:32:43 +02:00
|
|
|
tor_mmap_t *
|
2006-08-05 19:53:21 +02:00
|
|
|
tor_mmap_file(const char *filename)
|
2006-05-28 18:54:39 +02:00
|
|
|
{
|
|
|
|
int fd; /* router file */
|
|
|
|
char *string;
|
2014-03-19 19:17:55 +01:00
|
|
|
int page_size, result;
|
2008-12-05 03:17:41 +01:00
|
|
|
tor_mmap_t *res;
|
2006-10-20 01:05:02 +02:00
|
|
|
size_t size, filesize;
|
2014-03-19 19:17:55 +01:00
|
|
|
struct stat st;
|
2006-05-28 18:54:39 +02:00
|
|
|
|
|
|
|
tor_assert(filename);
|
|
|
|
|
2010-11-20 06:58:33 +01:00
|
|
|
fd = tor_open_cloexec(filename, O_RDONLY, 0);
|
2006-05-28 18:54:39 +02:00
|
|
|
if (fd<0) {
|
2007-10-24 17:45:42 +02:00
|
|
|
int save_errno = errno;
|
2007-02-12 22:39:44 +01:00
|
|
|
int severity = (errno == ENOENT) ? LOG_INFO : LOG_WARN;
|
|
|
|
log_fn(severity, LD_FS,"Could not open \"%s\" for mmap(): %s",filename,
|
|
|
|
strerror(errno));
|
2007-10-24 17:45:42 +02:00
|
|
|
errno = save_errno;
|
2006-05-28 18:54:39 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-19 19:17:55 +01:00
|
|
|
/* Get the size of the file */
|
|
|
|
result = fstat(fd, &st);
|
|
|
|
if (result != 0) {
|
|
|
|
int save_errno = errno;
|
|
|
|
log_warn(LD_FS,
|
|
|
|
"Couldn't fstat opened descriptor for \"%s\" during mmap: %s",
|
|
|
|
filename, strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
errno = save_errno;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
size = filesize = (size_t)(st.st_size);
|
|
|
|
/*
|
|
|
|
* Should we check for weird crap like mmapping a named pipe here,
|
|
|
|
* or just wait for if (!size) below to fail?
|
|
|
|
*/
|
2006-05-28 18:54:39 +02:00
|
|
|
/* ensure page alignment */
|
|
|
|
page_size = getpagesize();
|
2006-09-14 06:53:42 +02:00
|
|
|
size += (size%page_size) ? page_size-(size%page_size) : 0;
|
2006-05-28 18:54:39 +02:00
|
|
|
|
2017-01-25 19:23:08 +01:00
|
|
|
if (st.st_size > SSIZE_T_CEILING || (off_t)size < st.st_size) {
|
2017-01-23 20:08:54 +01:00
|
|
|
log_warn(LD_FS, "File \"%s\" is too large. Ignoring.",filename);
|
|
|
|
errno = EFBIG;
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-11-12 08:09:22 +01:00
|
|
|
if (!size) {
|
2006-12-02 23:47:46 +01:00
|
|
|
/* Zero-length file. If we call mmap on it, it will succeed but
|
|
|
|
* return NULL, and bad things will happen. So just fail. */
|
2006-12-11 05:21:10 +01:00
|
|
|
log_info(LD_FS,"File \"%s\" is empty. Ignoring.",filename);
|
2007-10-24 17:45:42 +02:00
|
|
|
errno = ERANGE;
|
2008-02-13 08:23:37 +01:00
|
|
|
close(fd);
|
2006-11-12 08:09:22 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-08-04 20:32:43 +02:00
|
|
|
string = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
2008-02-13 08:23:37 +01:00
|
|
|
close(fd);
|
2006-06-03 23:41:14 +02:00
|
|
|
if (string == MAP_FAILED) {
|
2007-10-24 17:45:42 +02:00
|
|
|
int save_errno = errno;
|
2006-05-28 18:54:39 +02:00
|
|
|
log_warn(LD_FS,"Could not mmap file \"%s\": %s", filename,
|
|
|
|
strerror(errno));
|
2007-10-24 17:45:42 +02:00
|
|
|
errno = save_errno;
|
2006-05-28 18:54:39 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-05 03:17:41 +01:00
|
|
|
res = tor_malloc_zero(sizeof(tor_mmap_t));
|
|
|
|
res->data = string;
|
|
|
|
res->size = filesize;
|
2006-10-20 01:05:02 +02:00
|
|
|
res->mapping_size = size;
|
2006-08-04 20:32:43 +02:00
|
|
|
|
2008-12-05 03:17:41 +01:00
|
|
|
return res;
|
2006-08-04 20:32:43 +02:00
|
|
|
}
|
2014-03-18 18:47:17 +01:00
|
|
|
/** Release storage held for a memory mapping; returns 0 on success,
|
|
|
|
* or -1 on failure (and logs a warning). */
|
|
|
|
int
|
2006-08-04 20:32:43 +02:00
|
|
|
tor_munmap_file(tor_mmap_t *handle)
|
|
|
|
{
|
2014-03-18 18:47:17 +01:00
|
|
|
int res;
|
|
|
|
|
|
|
|
if (handle == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
res = munmap((char*)handle->data, handle->mapping_size);
|
|
|
|
if (res == 0) {
|
|
|
|
/* munmap() succeeded */
|
|
|
|
tor_free(handle);
|
|
|
|
} else {
|
|
|
|
log_warn(LD_FS, "Failed to munmap() in tor_munmap_file(): %s",
|
|
|
|
strerror(errno));
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2006-05-28 18:54:39 +02:00
|
|
|
}
|
2012-01-31 16:59:42 +01:00
|
|
|
#elif defined(_WIN32)
|
2006-08-04 20:32:43 +02:00
|
|
|
tor_mmap_t *
|
2006-08-05 19:53:21 +02:00
|
|
|
tor_mmap_file(const char *filename)
|
2006-08-04 20:32:43 +02:00
|
|
|
{
|
2010-08-20 18:30:25 +02:00
|
|
|
TCHAR tfilename[MAX_PATH]= {0};
|
2008-12-05 20:36:35 +01:00
|
|
|
tor_mmap_t *res = tor_malloc_zero(sizeof(tor_mmap_t));
|
2007-10-24 17:45:42 +02:00
|
|
|
int empty = 0;
|
2012-05-23 18:39:05 +02:00
|
|
|
HANDLE file_handle = INVALID_HANDLE_VALUE;
|
2012-05-24 16:19:41 +02:00
|
|
|
DWORD size_low, size_high;
|
|
|
|
uint64_t real_size;
|
2006-12-07 15:39:42 +01:00
|
|
|
res->mmap_handle = NULL;
|
2010-08-20 18:30:25 +02:00
|
|
|
#ifdef UNICODE
|
|
|
|
mbstowcs(tfilename,filename,MAX_PATH);
|
|
|
|
#else
|
|
|
|
strlcpy(tfilename,filename,MAX_PATH);
|
|
|
|
#endif
|
2012-05-23 18:39:05 +02:00
|
|
|
file_handle = CreateFile(tfilename,
|
2013-06-12 16:45:48 +02:00
|
|
|
GENERIC_READ, FILE_SHARE_READ,
|
2012-05-31 18:36:54 +02:00
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
0);
|
2006-09-06 03:49:55 +02:00
|
|
|
|
2012-05-23 18:39:05 +02:00
|
|
|
if (file_handle == INVALID_HANDLE_VALUE)
|
2007-02-12 22:39:44 +01:00
|
|
|
goto win_err;
|
2006-09-06 03:49:55 +02:00
|
|
|
|
2012-06-05 17:10:42 +02:00
|
|
|
size_low = GetFileSize(file_handle, &size_high);
|
2006-08-04 20:32:43 +02:00
|
|
|
|
2012-05-24 16:19:41 +02:00
|
|
|
if (size_low == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) {
|
|
|
|
log_warn(LD_FS,"Error getting size of \"%s\".",filename);
|
|
|
|
goto win_err;
|
|
|
|
}
|
|
|
|
if (size_low == 0 && size_high == 0) {
|
2007-02-12 22:39:44 +01:00
|
|
|
log_info(LD_FS,"File \"%s\" is empty. Ignoring.",filename);
|
2007-10-24 17:45:42 +02:00
|
|
|
empty = 1;
|
2007-02-12 22:39:44 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2012-05-24 16:19:41 +02:00
|
|
|
real_size = (((uint64_t)size_high)<<32) | size_low;
|
|
|
|
if (real_size > SIZE_MAX) {
|
|
|
|
log_warn(LD_FS,"File \"%s\" is too big to map; not trying.",filename);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
res->size = real_size;
|
2007-02-12 22:39:44 +01:00
|
|
|
|
2012-05-23 18:39:05 +02:00
|
|
|
res->mmap_handle = CreateFileMapping(file_handle,
|
2006-08-04 20:32:43 +02:00
|
|
|
NULL,
|
|
|
|
PAGE_READONLY,
|
2012-05-24 16:19:41 +02:00
|
|
|
size_high,
|
|
|
|
size_low,
|
2006-08-04 21:03:40 +02:00
|
|
|
NULL);
|
2006-12-07 15:39:42 +01:00
|
|
|
if (res->mmap_handle == NULL)
|
2007-02-12 22:39:44 +01:00
|
|
|
goto win_err;
|
2008-12-05 03:17:41 +01:00
|
|
|
res->data = (char*) MapViewOfFile(res->mmap_handle,
|
|
|
|
FILE_MAP_READ,
|
|
|
|
0, 0, 0);
|
|
|
|
if (!res->data)
|
2007-02-12 22:39:44 +01:00
|
|
|
goto win_err;
|
2006-08-04 20:32:43 +02:00
|
|
|
|
2012-05-23 18:39:05 +02:00
|
|
|
CloseHandle(file_handle);
|
2008-12-05 03:17:41 +01:00
|
|
|
return res;
|
2007-02-12 22:39:44 +01:00
|
|
|
win_err: {
|
|
|
|
DWORD e = GetLastError();
|
2007-02-13 17:36:14 +01:00
|
|
|
int severity = (e == ERROR_FILE_NOT_FOUND || e == ERROR_PATH_NOT_FOUND) ?
|
2007-02-12 22:39:44 +01:00
|
|
|
LOG_INFO : LOG_WARN;
|
|
|
|
char *msg = format_win32_error(e);
|
2007-02-13 17:36:14 +01:00
|
|
|
log_fn(severity, LD_FS, "Couldn't mmap file \"%s\": %s", filename, msg);
|
2007-02-12 22:39:44 +01:00
|
|
|
tor_free(msg);
|
2007-10-25 04:53:24 +02:00
|
|
|
if (e == ERROR_FILE_NOT_FOUND || e == ERROR_PATH_NOT_FOUND)
|
|
|
|
errno = ENOENT;
|
|
|
|
else
|
|
|
|
errno = EINVAL;
|
2007-02-12 22:39:44 +01:00
|
|
|
}
|
2006-08-04 20:32:43 +02:00
|
|
|
err:
|
2007-10-24 17:45:42 +02:00
|
|
|
if (empty)
|
|
|
|
errno = ERANGE;
|
2012-05-23 18:39:05 +02:00
|
|
|
if (file_handle != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(file_handle);
|
2008-12-05 03:17:41 +01:00
|
|
|
tor_munmap_file(res);
|
2006-08-04 20:32:43 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-03-18 18:47:17 +01:00
|
|
|
|
|
|
|
/* Unmap the file, and return 0 for success or -1 for failure */
|
|
|
|
int
|
2006-08-04 20:32:43 +02:00
|
|
|
tor_munmap_file(tor_mmap_t *handle)
|
2006-05-28 18:54:39 +02:00
|
|
|
{
|
2014-03-31 17:35:39 +02:00
|
|
|
if (handle == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-18 18:47:17 +01:00
|
|
|
if (handle->data) {
|
2006-12-07 15:39:42 +01:00
|
|
|
/* This is an ugly cast, but without it, "data" in struct tor_mmap_t would
|
|
|
|
have to be redefined as non-const. */
|
2014-03-18 18:47:17 +01:00
|
|
|
BOOL ok = UnmapViewOfFile( (LPVOID) handle->data);
|
|
|
|
if (!ok) {
|
|
|
|
log_warn(LD_FS, "Failed to UnmapViewOfFile() in tor_munmap_file(): %d",
|
|
|
|
(int)GetLastError());
|
|
|
|
}
|
|
|
|
}
|
2006-09-06 03:49:55 +02:00
|
|
|
|
2008-12-05 03:17:41 +01:00
|
|
|
if (handle->mmap_handle != NULL)
|
2008-12-05 20:36:35 +01:00
|
|
|
CloseHandle(handle->mmap_handle);
|
2008-12-05 03:17:41 +01:00
|
|
|
tor_free(handle);
|
2014-03-18 18:47:17 +01:00
|
|
|
|
|
|
|
return 0;
|
2006-05-28 18:54:39 +02:00
|
|
|
}
|
|
|
|
#else
|
2006-08-04 20:32:43 +02:00
|
|
|
tor_mmap_t *
|
2006-08-05 19:53:21 +02:00
|
|
|
tor_mmap_file(const char *filename)
|
2006-05-28 18:54:39 +02:00
|
|
|
{
|
2007-02-01 19:09:27 +01:00
|
|
|
struct stat st;
|
2007-02-12 22:39:44 +01:00
|
|
|
char *res = read_file_to_str(filename, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
|
2006-08-04 20:32:43 +02:00
|
|
|
tor_mmap_t *handle;
|
2006-08-05 19:53:08 +02:00
|
|
|
if (! res)
|
|
|
|
return NULL;
|
2006-08-04 20:32:43 +02:00
|
|
|
handle = tor_malloc_zero(sizeof(tor_mmap_t));
|
2006-08-05 19:53:21 +02:00
|
|
|
handle->data = res;
|
2007-02-01 19:09:27 +01:00
|
|
|
handle->size = st.st_size;
|
2006-08-04 20:32:43 +02:00
|
|
|
return handle;
|
2006-05-28 18:54:39 +02:00
|
|
|
}
|
2014-03-18 18:47:17 +01:00
|
|
|
|
|
|
|
/** Unmap the file mapped with tor_mmap_file(), and return 0 for success
|
|
|
|
* or -1 for failure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2006-08-04 20:32:43 +02:00
|
|
|
tor_munmap_file(tor_mmap_t *handle)
|
2006-05-28 18:54:39 +02:00
|
|
|
{
|
2014-03-18 18:47:17 +01:00
|
|
|
char *d = NULL;
|
2014-03-31 17:35:39 +02:00
|
|
|
if (handle == NULL)
|
|
|
|
return 0;
|
2014-03-18 18:47:17 +01:00
|
|
|
|
|
|
|
d = (char*)handle->data;
|
2006-08-05 19:53:08 +02:00
|
|
|
tor_free(d);
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(handle, 0, sizeof(tor_mmap_t));
|
2006-08-05 19:53:08 +02:00
|
|
|
tor_free(handle);
|
2014-03-18 18:47:17 +01:00
|
|
|
|
|
|
|
/* Can't fail in this mmap()/munmap()-free case */
|
|
|
|
return 0;
|
2006-05-28 18:54:39 +02:00
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(COMPAT_HAS_MMAN_AND_PAGESIZE) || ... || ... */
|
2006-05-28 18:54:39 +02:00
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/** Replacement for snprintf. Differs from platform snprintf in two
|
|
|
|
* ways: First, always NUL-terminates its output. Second, always
|
|
|
|
* returns -1 if the result is truncated. (Note that this return
|
2006-10-20 21:11:12 +02:00
|
|
|
* behavior does <i>not</i> conform to C99; it just happens to be
|
|
|
|
* easier to emulate "return -1" with conformant implementations than
|
2004-11-01 21:41:47 +01:00
|
|
|
* it is to emulate "return number that would be written" with
|
|
|
|
* non-conformant implementations.) */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
tor_snprintf(char *str, size_t size, const char *format, ...)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int r;
|
|
|
|
va_start(ap,format);
|
|
|
|
r = tor_vsnprintf(str,size,format,ap);
|
|
|
|
va_end(ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2004-11-29 07:49:04 +01:00
|
|
|
/** Replacement for vsnprintf; behavior differs as tor_snprintf differs from
|
2004-11-01 21:41:47 +01:00
|
|
|
* snprintf.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
tor_vsnprintf(char *str, size_t size, const char *format, va_list args)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
int r;
|
2004-11-29 07:49:04 +01:00
|
|
|
if (size == 0)
|
|
|
|
return -1; /* no place for the NUL */
|
2010-12-14 00:40:21 +01:00
|
|
|
if (size > SIZE_T_CEILING)
|
2004-12-02 05:33:01 +01:00
|
|
|
return -1;
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2004-11-01 21:41:47 +01:00
|
|
|
r = _vsnprintf(str, size, format, args);
|
|
|
|
#else
|
|
|
|
r = vsnprintf(str, size, format, args);
|
|
|
|
#endif
|
|
|
|
str[size-1] = '\0';
|
2008-12-11 22:11:22 +01:00
|
|
|
if (r < 0 || r >= (ssize_t)size)
|
2004-11-01 21:41:47 +01:00
|
|
|
return -1;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2010-02-25 21:58:55 +01:00
|
|
|
/**
|
|
|
|
* Portable asprintf implementation. Does a printf() into a newly malloc'd
|
|
|
|
* string. Sets *<b>strp</b> to this string, and returns its length (not
|
|
|
|
* including the terminating NUL character).
|
|
|
|
*
|
|
|
|
* You can treat this function as if its implementation were something like
|
|
|
|
<pre>
|
|
|
|
char buf[_INFINITY_];
|
|
|
|
tor_snprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
*strp = tor_strdup(buf);
|
|
|
|
return strlen(*strp):
|
|
|
|
</pre>
|
|
|
|
* Where _INFINITY_ is an imaginary constant so big that any string can fit
|
|
|
|
* into it.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
tor_asprintf(char **strp, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
r = tor_vasprintf(strp, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
if (!*strp || r < 0) {
|
2016-06-15 23:25:53 +02:00
|
|
|
/* LCOV_EXCL_START */
|
2010-02-25 21:58:55 +01:00
|
|
|
log_err(LD_BUG, "Internal error in asprintf");
|
|
|
|
tor_assert(0);
|
2016-06-15 23:25:53 +02:00
|
|
|
/* LCOV_EXCL_STOP */
|
2010-02-25 21:58:55 +01:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Portable vasprintf implementation. Does a printf() into a newly malloc'd
|
|
|
|
* string. Differs from regular vasprintf in the same ways that
|
|
|
|
* tor_asprintf() differs from regular asprintf.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
tor_vasprintf(char **strp, const char *fmt, va_list args)
|
|
|
|
{
|
2010-03-01 03:46:46 +01:00
|
|
|
/* use a temporary variable in case *strp is in args. */
|
|
|
|
char *strp_tmp=NULL;
|
2010-02-25 21:58:55 +01:00
|
|
|
#ifdef HAVE_VASPRINTF
|
|
|
|
/* If the platform gives us one, use it. */
|
2010-03-01 03:46:46 +01:00
|
|
|
int r = vasprintf(&strp_tmp, fmt, args);
|
2010-02-25 21:58:55 +01:00
|
|
|
if (r < 0)
|
|
|
|
*strp = NULL;
|
2010-03-01 03:46:46 +01:00
|
|
|
else
|
|
|
|
*strp = strp_tmp;
|
2010-02-25 21:58:55 +01:00
|
|
|
return r;
|
2012-11-02 18:48:29 +01:00
|
|
|
#elif defined(HAVE__VSCPRINTF)
|
2010-02-25 21:58:55 +01:00
|
|
|
/* On Windows, _vsnprintf won't tell us the length of the string if it
|
|
|
|
* overflows, so we need to use _vcsprintf to tell how much to allocate */
|
|
|
|
int len, r;
|
2016-11-09 00:44:06 +01:00
|
|
|
va_list tmp_args;
|
|
|
|
va_copy(tmp_args, args);
|
|
|
|
len = _vscprintf(fmt, tmp_args);
|
|
|
|
va_end(tmp_args);
|
2010-02-25 21:58:55 +01:00
|
|
|
if (len < 0) {
|
2010-03-01 03:46:46 +01:00
|
|
|
*strp = NULL;
|
2010-02-25 21:58:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-03-01 03:46:46 +01:00
|
|
|
strp_tmp = tor_malloc(len + 1);
|
|
|
|
r = _vsnprintf(strp_tmp, len+1, fmt, args);
|
2010-02-25 21:58:55 +01:00
|
|
|
if (r != len) {
|
2010-03-01 03:46:46 +01:00
|
|
|
tor_free(strp_tmp);
|
|
|
|
*strp = NULL;
|
2010-02-25 21:58:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-03-01 03:46:46 +01:00
|
|
|
*strp = strp_tmp;
|
2010-02-25 21:58:55 +01:00
|
|
|
return len;
|
|
|
|
#else
|
|
|
|
/* Everywhere else, we have a decent vsnprintf that tells us how many
|
|
|
|
* characters we need. We give it a try on a short buffer first, since
|
|
|
|
* it might be nice to avoid the second vsnprintf call.
|
|
|
|
*/
|
|
|
|
char buf[128];
|
|
|
|
int len, r;
|
|
|
|
va_list tmp_args;
|
|
|
|
va_copy(tmp_args, args);
|
2015-12-01 19:00:58 +01:00
|
|
|
/* vsnprintf() was properly checked but tor_vsnprintf() available so
|
|
|
|
* why not use it? */
|
|
|
|
len = tor_vsnprintf(buf, sizeof(buf), fmt, tmp_args);
|
2010-02-25 21:58:55 +01:00
|
|
|
va_end(tmp_args);
|
|
|
|
if (len < (int)sizeof(buf)) {
|
|
|
|
*strp = tor_strdup(buf);
|
|
|
|
return len;
|
|
|
|
}
|
2010-03-01 03:46:46 +01:00
|
|
|
strp_tmp = tor_malloc(len+1);
|
2015-12-01 18:27:29 +01:00
|
|
|
/* use of tor_vsnprintf() will ensure string is null terminated */
|
|
|
|
r = tor_vsnprintf(strp_tmp, len+1, fmt, args);
|
2010-02-25 21:58:55 +01:00
|
|
|
if (r != len) {
|
2010-03-01 03:46:46 +01:00
|
|
|
tor_free(strp_tmp);
|
|
|
|
*strp = NULL;
|
2010-02-25 21:58:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-03-01 03:46:46 +01:00
|
|
|
*strp = strp_tmp;
|
2010-02-25 21:58:55 +01:00
|
|
|
return len;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_VASPRINTF) || ... */
|
2010-02-25 21:58:55 +01:00
|
|
|
}
|
|
|
|
|
2005-06-18 04:17:11 +02:00
|
|
|
/** Given <b>hlen</b> bytes at <b>haystack</b> and <b>nlen</b> bytes at
|
2005-06-18 04:23:11 +02:00
|
|
|
* <b>needle</b>, return a pointer to the first occurrence of the needle
|
2005-06-18 04:17:11 +02:00
|
|
|
* within the haystack, or NULL if there is no such occurrence.
|
|
|
|
*
|
2011-05-10 22:58:38 +02:00
|
|
|
* This function is <em>not</em> timing-safe.
|
|
|
|
*
|
2011-06-19 10:28:40 +02:00
|
|
|
* Requires that <b>nlen</b> be greater than zero.
|
2005-06-18 04:17:11 +02:00
|
|
|
*/
|
|
|
|
const void *
|
2005-12-14 21:40:40 +01:00
|
|
|
tor_memmem(const void *_haystack, size_t hlen,
|
|
|
|
const void *_needle, size_t nlen)
|
2005-06-18 04:17:11 +02:00
|
|
|
{
|
|
|
|
#if defined(HAVE_MEMMEM) && (!defined(__GNUC__) || __GNUC__ >= 2)
|
|
|
|
tor_assert(nlen);
|
2005-06-28 01:40:17 +02:00
|
|
|
return memmem(_haystack, hlen, _needle, nlen);
|
2005-06-18 04:17:11 +02:00
|
|
|
#else
|
2005-12-14 21:40:40 +01:00
|
|
|
/* This isn't as fast as the GLIBC implementation, but it doesn't need to
|
|
|
|
* be. */
|
2013-12-11 21:14:43 +01:00
|
|
|
const char *p, *last_possible_start;
|
2005-06-28 01:35:04 +02:00
|
|
|
const char *haystack = (const char*)_haystack;
|
|
|
|
const char *needle = (const char*)_needle;
|
2005-06-18 04:17:11 +02:00
|
|
|
char first;
|
|
|
|
tor_assert(nlen);
|
|
|
|
|
2013-12-11 21:14:43 +01:00
|
|
|
if (nlen > hlen)
|
|
|
|
return NULL;
|
|
|
|
|
2005-06-18 04:17:11 +02:00
|
|
|
p = haystack;
|
2013-12-11 21:14:43 +01:00
|
|
|
/* Last position at which the needle could start. */
|
|
|
|
last_possible_start = haystack + hlen - nlen;
|
2005-06-18 04:17:11 +02:00
|
|
|
first = *(const char*)needle;
|
2013-12-11 21:14:43 +01:00
|
|
|
while ((p = memchr(p, first, last_possible_start + 1 - p))) {
|
2011-05-10 22:58:38 +02:00
|
|
|
if (fast_memeq(p, needle, nlen))
|
2005-06-18 04:17:11 +02:00
|
|
|
return p;
|
2013-12-11 21:14:43 +01:00
|
|
|
if (++p > last_possible_start) {
|
|
|
|
/* This comparison shouldn't be necessary, since if p was previously
|
|
|
|
* equal to last_possible_start, the next memchr call would be
|
|
|
|
* "memchr(p, first, 0)", which will return NULL. But it clarifies the
|
|
|
|
* logic. */
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-06-18 04:17:11 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_MEMMEM) && (!defined(__GNUC__) || __GNUC__ >= 2) */
|
2005-06-18 04:17:11 +02:00
|
|
|
}
|
|
|
|
|
2012-06-05 01:51:00 +02:00
|
|
|
/**
|
|
|
|
* Tables to implement ctypes-replacement TOR_IS*() functions. Each table
|
2009-01-20 22:33:56 +01:00
|
|
|
* has 256 bits to look up whether a character is in some set or not. This
|
|
|
|
* fails on non-ASCII platforms, but it is hard to find a platform whose
|
|
|
|
* character set is not a superset of ASCII nowadays. */
|
2012-06-05 01:51:00 +02:00
|
|
|
|
|
|
|
/**@{*/
|
2009-01-21 04:51:14 +01:00
|
|
|
const uint32_t TOR_ISALPHA_TABLE[8] =
|
2009-01-20 22:33:56 +01:00
|
|
|
{ 0, 0, 0x7fffffe, 0x7fffffe, 0, 0, 0, 0 };
|
2009-01-21 04:51:14 +01:00
|
|
|
const uint32_t TOR_ISALNUM_TABLE[8] =
|
2009-01-20 22:33:56 +01:00
|
|
|
{ 0, 0x3ff0000, 0x7fffffe, 0x7fffffe, 0, 0, 0, 0 };
|
2009-01-21 04:51:14 +01:00
|
|
|
const uint32_t TOR_ISSPACE_TABLE[8] = { 0x3e00, 0x1, 0, 0, 0, 0, 0, 0 };
|
|
|
|
const uint32_t TOR_ISXDIGIT_TABLE[8] =
|
2009-01-20 22:33:56 +01:00
|
|
|
{ 0, 0x3ff0000, 0x7e, 0x7e, 0, 0, 0, 0 };
|
2009-01-21 04:51:14 +01:00
|
|
|
const uint32_t TOR_ISDIGIT_TABLE[8] = { 0, 0x3ff0000, 0, 0, 0, 0, 0, 0 };
|
|
|
|
const uint32_t TOR_ISPRINT_TABLE[8] =
|
2009-01-20 22:33:56 +01:00
|
|
|
{ 0, 0xffffffff, 0xffffffff, 0x7fffffff, 0, 0, 0, 0x0 };
|
2009-01-21 04:51:14 +01:00
|
|
|
const uint32_t TOR_ISUPPER_TABLE[8] = { 0, 0, 0x7fffffe, 0, 0, 0, 0, 0 };
|
|
|
|
const uint32_t TOR_ISLOWER_TABLE[8] = { 0, 0, 0, 0x7fffffe, 0, 0, 0, 0 };
|
2012-06-05 01:51:00 +02:00
|
|
|
|
|
|
|
/** Upper-casing and lowercasing tables to map characters to upper/lowercase
|
|
|
|
* equivalents. Used by tor_toupper() and tor_tolower(). */
|
|
|
|
/**@{*/
|
2016-05-25 13:17:29 +02:00
|
|
|
const uint8_t TOR_TOUPPER_TABLE[256] = {
|
2009-01-20 22:33:56 +01:00
|
|
|
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
|
|
|
|
16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
|
|
|
|
32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
|
|
|
|
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
|
|
|
|
64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
|
|
|
|
80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
|
|
|
|
96,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
|
|
|
|
80,81,82,83,84,85,86,87,88,89,90,123,124,125,126,127,
|
|
|
|
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
|
|
|
|
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
|
|
|
|
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
|
|
|
|
176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
|
|
|
|
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
|
|
|
|
208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
|
|
|
|
224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
|
|
|
|
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,
|
|
|
|
};
|
2016-05-25 13:17:29 +02:00
|
|
|
const uint8_t TOR_TOLOWER_TABLE[256] = {
|
2009-01-20 22:33:56 +01:00
|
|
|
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
|
|
|
|
16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
|
|
|
|
32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
|
|
|
|
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
|
|
|
|
64,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
|
|
|
|
112,113,114,115,116,117,118,119,120,121,122,91,92,93,94,95,
|
|
|
|
96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
|
|
|
|
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
|
|
|
|
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
|
|
|
|
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
|
|
|
|
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
|
|
|
|
176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
|
|
|
|
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
|
|
|
|
208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
|
|
|
|
224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
|
|
|
|
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,
|
|
|
|
};
|
2012-06-05 01:51:00 +02:00
|
|
|
/**@}*/
|
2009-01-20 22:33:56 +01:00
|
|
|
|
2012-03-30 17:01:21 +02:00
|
|
|
/** Helper for tor_strtok_r_impl: Advances cp past all characters in
|
|
|
|
* <b>sep</b>, and returns its new value. */
|
|
|
|
static char *
|
|
|
|
strtok_helper(char *cp, const char *sep)
|
|
|
|
{
|
|
|
|
if (sep[1]) {
|
|
|
|
while (*cp && strchr(sep, *cp))
|
|
|
|
++cp;
|
|
|
|
} else {
|
|
|
|
while (*cp && *cp == *sep)
|
|
|
|
++cp;
|
|
|
|
}
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
2009-08-10 02:27:35 +02:00
|
|
|
/** Implementation of strtok_r for platforms whose coders haven't figured out
|
2016-01-27 14:51:28 +01:00
|
|
|
* how to write one. Hey, retrograde libc developers! You can use this code
|
|
|
|
* here for free! */
|
2009-08-10 02:27:35 +02:00
|
|
|
char *
|
|
|
|
tor_strtok_r_impl(char *str, const char *sep, char **lasts)
|
|
|
|
{
|
|
|
|
char *cp, *start;
|
2012-03-30 17:01:21 +02:00
|
|
|
tor_assert(*sep);
|
|
|
|
if (str) {
|
|
|
|
str = strtok_helper(str, sep);
|
|
|
|
if (!*str)
|
|
|
|
return NULL;
|
2009-08-10 02:27:35 +02:00
|
|
|
start = cp = *lasts = str;
|
2012-03-30 17:01:21 +02:00
|
|
|
} else if (!*lasts || !**lasts) {
|
2009-08-10 02:27:35 +02:00
|
|
|
return NULL;
|
2012-03-30 17:01:21 +02:00
|
|
|
} else {
|
2009-08-10 02:27:35 +02:00
|
|
|
start = cp = *lasts;
|
2012-03-30 17:01:21 +02:00
|
|
|
}
|
2009-08-10 02:27:35 +02:00
|
|
|
|
|
|
|
if (sep[1]) {
|
|
|
|
while (*cp && !strchr(sep, *cp))
|
|
|
|
++cp;
|
|
|
|
} else {
|
|
|
|
cp = strchr(cp, *sep);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cp || !*cp) {
|
|
|
|
*lasts = NULL;
|
|
|
|
} else {
|
|
|
|
*cp++ = '\0';
|
2012-03-30 17:01:21 +02:00
|
|
|
*lasts = strtok_helper(cp, sep);
|
2009-08-10 02:27:35 +02:00
|
|
|
}
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2004-12-22 03:32:26 +01:00
|
|
|
/** Take a filename and return a pointer to its final element. This
|
|
|
|
* function is called on __FILE__ to fix a MSVC nit where __FILE__
|
|
|
|
* contains the full path to the file. This is bad, because it
|
|
|
|
* confuses users to find the home directory of the person who
|
2009-05-27 23:55:51 +02:00
|
|
|
* compiled the binary in their warning messages.
|
2004-12-22 03:32:26 +01:00
|
|
|
*/
|
|
|
|
const char *
|
2005-10-25 09:05:03 +02:00
|
|
|
tor_fix_source_file(const char *fname)
|
2004-12-22 03:32:26 +01:00
|
|
|
{
|
2004-12-22 03:46:28 +01:00
|
|
|
const char *cp1, *cp2, *r;
|
2004-12-22 03:32:26 +01:00
|
|
|
cp1 = strrchr(fname, '/');
|
|
|
|
cp2 = strrchr(fname, '\\');
|
|
|
|
if (cp1 && cp2) {
|
2004-12-22 03:46:28 +01:00
|
|
|
r = (cp1<cp2)?(cp2+1):(cp1+1);
|
2004-12-22 03:32:26 +01:00
|
|
|
} else if (cp1) {
|
2004-12-22 03:46:28 +01:00
|
|
|
r = cp1+1;
|
2004-12-22 03:32:26 +01:00
|
|
|
} else if (cp2) {
|
2004-12-22 03:46:28 +01:00
|
|
|
r = cp2+1;
|
2004-12-22 03:32:26 +01:00
|
|
|
} else {
|
2004-12-22 03:46:28 +01:00
|
|
|
r = fname;
|
2004-12-22 03:32:26 +01:00
|
|
|
}
|
2004-12-22 03:46:28 +01:00
|
|
|
return r;
|
2004-12-22 03:32:26 +01:00
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) */
|
2004-12-22 03:32:26 +01:00
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/**
|
2004-12-01 04:48:14 +01:00
|
|
|
* Read a 16-bit value beginning at <b>cp</b>. Equivalent to
|
2004-11-01 21:41:47 +01:00
|
|
|
* *(uint16_t*)(cp), but will not cause segfaults on platforms that forbid
|
|
|
|
* unaligned memory access.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
uint16_t
|
2010-12-14 01:34:01 +01:00
|
|
|
get_uint16(const void *cp)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
uint16_t v;
|
|
|
|
memcpy(&v,cp,2);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
/**
|
2004-12-01 04:48:14 +01:00
|
|
|
* Read a 32-bit value beginning at <b>cp</b>. Equivalent to
|
2004-11-01 21:41:47 +01:00
|
|
|
* *(uint32_t*)(cp), but will not cause segfaults on platforms that forbid
|
|
|
|
* unaligned memory access.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
uint32_t
|
2010-12-14 01:34:01 +01:00
|
|
|
get_uint32(const void *cp)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
uint32_t v;
|
|
|
|
memcpy(&v,cp,4);
|
|
|
|
return v;
|
|
|
|
}
|
2009-03-02 20:15:05 +01:00
|
|
|
/**
|
2009-09-03 03:01:23 +02:00
|
|
|
* Read a 64-bit value beginning at <b>cp</b>. Equivalent to
|
|
|
|
* *(uint64_t*)(cp), but will not cause segfaults on platforms that forbid
|
2009-03-02 20:15:05 +01:00
|
|
|
* unaligned memory access.
|
|
|
|
*/
|
|
|
|
uint64_t
|
2010-12-14 01:34:01 +01:00
|
|
|
get_uint64(const void *cp)
|
2009-03-02 20:15:05 +01:00
|
|
|
{
|
|
|
|
uint64_t v;
|
|
|
|
memcpy(&v,cp,8);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/**
|
|
|
|
* Set a 16-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
|
2009-03-02 20:15:05 +01:00
|
|
|
* *(uint16_t*)(cp) = v, but will not cause segfaults on platforms that forbid
|
2004-11-01 21:41:47 +01:00
|
|
|
* unaligned memory access. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2010-12-14 01:34:01 +01:00
|
|
|
set_uint16(void *cp, uint16_t v)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
memcpy(cp,&v,2);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Set a 32-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
|
2009-03-02 20:15:05 +01:00
|
|
|
* *(uint32_t*)(cp) = v, but will not cause segfaults on platforms that forbid
|
2004-11-01 21:41:47 +01:00
|
|
|
* unaligned memory access. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2010-12-14 01:34:01 +01:00
|
|
|
set_uint32(void *cp, uint32_t v)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
memcpy(cp,&v,4);
|
|
|
|
}
|
2009-03-02 20:15:05 +01:00
|
|
|
/**
|
|
|
|
* Set a 64-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
|
|
|
|
* *(uint64_t*)(cp) = v, but will not cause segfaults on platforms that forbid
|
|
|
|
* unaligned memory access. */
|
|
|
|
void
|
2010-12-14 01:34:01 +01:00
|
|
|
set_uint64(void *cp, uint64_t v)
|
2009-03-02 20:15:05 +01:00
|
|
|
{
|
|
|
|
memcpy(cp,&v,8);
|
|
|
|
}
|
2004-11-01 21:41:47 +01:00
|
|
|
|
|
|
|
/**
|
2009-05-27 23:55:51 +02:00
|
|
|
* Rename the file <b>from</b> to the file <b>to</b>. On Unix, this is
|
2005-06-12 06:33:26 +02:00
|
|
|
* the same as rename(2). On windows, this removes <b>to</b> first if
|
|
|
|
* it already exists.
|
2004-11-01 21:41:47 +01:00
|
|
|
* Returns 0 on success. Returns -1 and sets errno on failure.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
replace_file(const char *from, const char *to)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifndef _WIN32
|
2014-03-28 08:51:50 +01:00
|
|
|
return tor_rename(from, to);
|
2004-11-01 21:41:47 +01:00
|
|
|
#else
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (file_status(to))
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
case FN_NOENT:
|
|
|
|
break;
|
|
|
|
case FN_FILE:
|
2014-10-19 08:48:07 +02:00
|
|
|
case FN_EMPTY:
|
2004-11-01 21:41:47 +01:00
|
|
|
if (unlink(to)) return -1;
|
|
|
|
break;
|
|
|
|
case FN_ERROR:
|
|
|
|
return -1;
|
|
|
|
case FN_DIR:
|
|
|
|
errno = EISDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-28 08:51:50 +01:00
|
|
|
return tor_rename(from,to);
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(_WIN32) */
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
|
|
|
|
2005-09-15 01:27:52 +02:00
|
|
|
/** Change <b>fname</b>'s modification time to now. */
|
|
|
|
int
|
|
|
|
touch_file(const char *fname)
|
|
|
|
{
|
|
|
|
if (utime(fname, NULL)!=0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-10 21:40:01 +01:00
|
|
|
/** Represents a lockfile on which we hold the lock. */
|
2008-09-01 22:06:26 +02:00
|
|
|
struct tor_lockfile_t {
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Name of the file */
|
2008-09-01 22:06:26 +02:00
|
|
|
char *filename;
|
2011-03-16 22:05:37 +01:00
|
|
|
/** File descriptor used to hold the file open */
|
2008-09-01 22:06:26 +02:00
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
2008-11-10 21:40:01 +01:00
|
|
|
/** Try to get a lock on the lockfile <b>filename</b>, creating it as
|
|
|
|
* necessary. If someone else has the lock and <b>blocking</b> is true,
|
|
|
|
* wait until the lock is available. Otherwise return immediately whether
|
|
|
|
* we succeeded or not.
|
|
|
|
*
|
|
|
|
* Set *<b>locked_out</b> to true if somebody else had the lock, and to false
|
|
|
|
* otherwise.
|
|
|
|
*
|
|
|
|
* Return a <b>tor_lockfile_t</b> on success, NULL on failure.
|
2008-11-26 17:10:56 +01:00
|
|
|
*
|
|
|
|
* (Implementation note: because we need to fall back to fcntl on some
|
|
|
|
* platforms, these locks are per-process, not per-thread. If you want
|
2011-02-07 15:40:14 +01:00
|
|
|
* to do in-process locking, use tor_mutex_t like a normal person.
|
|
|
|
* On Windows, when <b>blocking</b> is true, the maximum time that
|
|
|
|
* is actually waited is 10 seconds, after which NULL is returned
|
|
|
|
* and <b>locked_out</b> is set to 1.)
|
2008-11-10 21:40:01 +01:00
|
|
|
*/
|
2008-09-01 22:06:26 +02:00
|
|
|
tor_lockfile_t *
|
|
|
|
tor_lockfile_lock(const char *filename, int blocking, int *locked_out)
|
|
|
|
{
|
|
|
|
tor_lockfile_t *result;
|
|
|
|
int fd;
|
|
|
|
*locked_out = 0;
|
|
|
|
|
|
|
|
log_info(LD_FS, "Locking \"%s\"", filename);
|
2010-11-20 06:58:33 +01:00
|
|
|
fd = tor_open_cloexec(filename, O_RDWR|O_CREAT|O_TRUNC, 0600);
|
2008-09-01 22:06:26 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
log_warn(LD_FS,"Couldn't open \"%s\" for locking: %s", filename,
|
|
|
|
strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-11-26 17:10:56 +01:00
|
|
|
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2008-09-01 22:06:26 +02:00
|
|
|
_lseek(fd, 0, SEEK_SET);
|
2008-11-11 16:29:40 +01:00
|
|
|
if (_locking(fd, blocking ? _LK_LOCK : _LK_NBLCK, 1) < 0) {
|
2011-02-11 16:57:53 +01:00
|
|
|
if (errno != EACCES && errno != EDEADLOCK)
|
2008-09-01 22:06:26 +02:00
|
|
|
log_warn(LD_FS,"Couldn't lock \"%s\": %s", filename, strerror(errno));
|
|
|
|
else
|
|
|
|
*locked_out = 1;
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-11-26 17:10:56 +01:00
|
|
|
#elif defined(HAVE_FLOCK)
|
2008-09-01 22:06:26 +02:00
|
|
|
if (flock(fd, LOCK_EX|(blocking ? 0 : LOCK_NB)) < 0) {
|
|
|
|
if (errno != EWOULDBLOCK)
|
|
|
|
log_warn(LD_FS,"Couldn't lock \"%s\": %s", filename, strerror(errno));
|
|
|
|
else
|
|
|
|
*locked_out = 1;
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-11-26 17:10:56 +01:00
|
|
|
#else
|
|
|
|
{
|
|
|
|
struct flock lock;
|
|
|
|
memset(&lock, 0, sizeof(lock));
|
|
|
|
lock.l_type = F_WRLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
if (fcntl(fd, blocking ? F_SETLKW : F_SETLK, &lock) < 0) {
|
|
|
|
if (errno != EACCES && errno != EAGAIN)
|
|
|
|
log_warn(LD_FS, "Couldn't lock \"%s\": %s", filename, strerror(errno));
|
|
|
|
else
|
|
|
|
*locked_out = 1;
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) || ... */
|
2008-09-01 22:06:26 +02:00
|
|
|
|
|
|
|
result = tor_malloc(sizeof(tor_lockfile_t));
|
|
|
|
result->filename = tor_strdup(filename);
|
|
|
|
result->fd = fd;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-11-10 21:40:01 +01:00
|
|
|
/** Release the lock held as <b>lockfile</b>. */
|
2008-09-01 22:06:26 +02:00
|
|
|
void
|
|
|
|
tor_lockfile_unlock(tor_lockfile_t *lockfile)
|
|
|
|
{
|
|
|
|
tor_assert(lockfile);
|
|
|
|
|
|
|
|
log_info(LD_FS, "Unlocking \"%s\"", lockfile->filename);
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2008-09-04 23:58:09 +02:00
|
|
|
_lseek(lockfile->fd, 0, SEEK_SET);
|
2008-11-11 16:29:40 +01:00
|
|
|
if (_locking(lockfile->fd, _LK_UNLCK, 1) < 0) {
|
2008-09-01 22:06:26 +02:00
|
|
|
log_warn(LD_FS,"Error unlocking \"%s\": %s", lockfile->filename,
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2008-11-26 17:10:56 +01:00
|
|
|
#elif defined(HAVE_FLOCK)
|
2008-09-01 22:06:26 +02:00
|
|
|
if (flock(lockfile->fd, LOCK_UN) < 0) {
|
|
|
|
log_warn(LD_FS, "Error unlocking \"%s\": %s", lockfile->filename,
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2008-11-26 17:10:56 +01:00
|
|
|
#else
|
|
|
|
/* Closing the lockfile is sufficient. */
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) || ... */
|
2008-09-01 22:06:26 +02:00
|
|
|
|
|
|
|
close(lockfile->fd);
|
|
|
|
lockfile->fd = -1;
|
|
|
|
tor_free(lockfile->filename);
|
|
|
|
tor_free(lockfile);
|
|
|
|
}
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @{ */
|
|
|
|
/** Some old versions of Unix didn't define constants for these values,
|
2008-12-03 00:26:04 +01:00
|
|
|
* and instead expect you to say 0, 1, or 2. */
|
2013-06-14 03:56:35 +02:00
|
|
|
#ifndef SEEK_SET
|
|
|
|
#define SEEK_SET 0
|
|
|
|
#endif
|
2008-12-03 00:26:04 +01:00
|
|
|
#ifndef SEEK_CUR
|
|
|
|
#define SEEK_CUR 1
|
|
|
|
#endif
|
|
|
|
#ifndef SEEK_END
|
|
|
|
#define SEEK_END 2
|
|
|
|
#endif
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @} */
|
2008-12-03 00:26:04 +01:00
|
|
|
|
2008-12-03 00:42:21 +01:00
|
|
|
/** Return the position of <b>fd</b> with respect to the start of the file. */
|
2008-12-03 00:26:04 +01:00
|
|
|
off_t
|
|
|
|
tor_fd_getpos(int fd)
|
|
|
|
{
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2008-12-03 00:26:04 +01:00
|
|
|
return (off_t) _lseek(fd, 0, SEEK_CUR);
|
|
|
|
#else
|
|
|
|
return (off_t) lseek(fd, 0, SEEK_CUR);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:45:16 +02:00
|
|
|
/** Move <b>fd</b> to the end of the file. Return -1 on error, 0 on success.
|
|
|
|
* If the file is a pipe, do nothing and succeed.
|
|
|
|
**/
|
2008-12-03 00:26:04 +01:00
|
|
|
int
|
|
|
|
tor_fd_seekend(int fd)
|
|
|
|
{
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2008-12-03 00:26:04 +01:00
|
|
|
return _lseek(fd, 0, SEEK_END) < 0 ? -1 : 0;
|
|
|
|
#else
|
2014-08-20 19:48:17 +02:00
|
|
|
off_t rc = lseek(fd, 0, SEEK_END) < 0 ? -1 : 0;
|
2014-08-20 19:45:16 +02:00
|
|
|
#ifdef ESPIPE
|
|
|
|
/* If we get an error and ESPIPE, then it's a pipe or a socket of a fifo:
|
|
|
|
* no need to worry. */
|
|
|
|
if (rc < 0 && errno == ESPIPE)
|
|
|
|
rc = 0;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(ESPIPE) */
|
2014-08-20 19:48:17 +02:00
|
|
|
return (rc < 0) ? -1 : 0;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) */
|
2008-12-03 00:26:04 +01:00
|
|
|
}
|
|
|
|
|
2013-06-13 18:29:01 +02:00
|
|
|
/** Move <b>fd</b> to position <b>pos</b> in the file. Return -1 on error, 0
|
|
|
|
* on success. */
|
|
|
|
int
|
|
|
|
tor_fd_setpos(int fd, off_t pos)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return _lseek(fd, pos, SEEK_SET) < 0 ? -1 : 0;
|
|
|
|
#else
|
|
|
|
return lseek(fd, pos, SEEK_SET) < 0 ? -1 : 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-07-16 13:58:55 +02:00
|
|
|
/** Replacement for ftruncate(fd, 0): move to the front of the file and remove
|
|
|
|
* all the rest of the file. Return -1 on error, 0 on success. */
|
|
|
|
int
|
|
|
|
tor_ftruncate(int fd)
|
|
|
|
{
|
|
|
|
/* Rumor has it that some versions of ftruncate do not move the file pointer.
|
|
|
|
*/
|
|
|
|
if (tor_fd_setpos(fd, 0) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
return _chsize(fd, 0);
|
|
|
|
#else
|
|
|
|
return ftruncate(fd, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-02-19 23:46:19 +01:00
|
|
|
#undef DEBUG_SOCKET_COUNTING
|
|
|
|
#ifdef DEBUG_SOCKET_COUNTING
|
2008-02-21 04:38:46 +01:00
|
|
|
/** A bitarray of all fds that should be passed to tor_socket_close(). Only
|
|
|
|
* used if DEBUG_SOCKET_COUNTING is defined. */
|
2008-02-19 23:46:19 +01:00
|
|
|
static bitarray_t *open_sockets = NULL;
|
2008-02-21 04:38:46 +01:00
|
|
|
/** The size of <b>open_sockets</b>, in bits. */
|
2008-02-19 23:46:19 +01:00
|
|
|
static int max_socket = -1;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(DEBUG_SOCKET_COUNTING) */
|
2008-02-19 23:46:19 +01:00
|
|
|
|
2007-04-21 19:24:18 +02:00
|
|
|
/** Count of number of sockets currently open. (Undercounts sockets opened by
|
|
|
|
* eventdns and libevent.) */
|
|
|
|
static int n_sockets_open = 0;
|
|
|
|
|
2009-05-12 22:17:32 +02:00
|
|
|
/** Mutex to protect open_sockets, max_socket, and n_sockets_open. */
|
|
|
|
static tor_mutex_t *socket_accounting_mutex = NULL;
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Helper: acquire the socket accounting lock. */
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline void
|
2009-05-12 22:17:32 +02:00
|
|
|
socket_accounting_lock(void)
|
|
|
|
{
|
|
|
|
if (PREDICT_UNLIKELY(!socket_accounting_mutex))
|
|
|
|
socket_accounting_mutex = tor_mutex_new();
|
|
|
|
tor_mutex_acquire(socket_accounting_mutex);
|
|
|
|
}
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Helper: release the socket accounting lock. */
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline void
|
2009-05-12 22:17:32 +02:00
|
|
|
socket_accounting_unlock(void)
|
|
|
|
{
|
|
|
|
tor_mutex_release(socket_accounting_mutex);
|
|
|
|
}
|
|
|
|
|
2007-04-21 19:24:18 +02:00
|
|
|
/** As close(), but guaranteed to work for sockets across platforms (including
|
2013-05-27 21:16:43 +02:00
|
|
|
* Windows, where close()ing a socket doesn't work. Returns 0 on success and
|
|
|
|
* the socket error code on failure. */
|
2008-02-19 20:30:41 +01:00
|
|
|
int
|
2013-05-27 21:16:43 +02:00
|
|
|
tor_close_socket_simple(tor_socket_t s)
|
2007-04-21 19:24:18 +02:00
|
|
|
{
|
2008-02-19 20:30:41 +01:00
|
|
|
int r = 0;
|
2009-05-12 22:17:32 +02:00
|
|
|
|
2007-04-21 19:24:18 +02:00
|
|
|
/* On Windows, you have to call close() on fds returned by open(),
|
2013-05-27 21:16:43 +02:00
|
|
|
* and closesocket() on fds returned by socket(). On Unix, everything
|
|
|
|
* gets close()'d. We abstract this difference by always using
|
|
|
|
* tor_close_socket to close sockets, and always using close() on
|
|
|
|
* files.
|
|
|
|
*/
|
|
|
|
#if defined(_WIN32)
|
|
|
|
r = closesocket(s);
|
|
|
|
#else
|
|
|
|
r = close(s);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (r != 0) {
|
|
|
|
int err = tor_socket_errno(-1);
|
|
|
|
log_info(LD_NET, "Close returned an error: %s", tor_socket_strerror(err));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** As tor_close_socket_simple(), but keeps track of the number
|
|
|
|
* of open sockets. Returns 0 on success, -1 on failure. */
|
2016-09-09 16:28:12 +02:00
|
|
|
MOCK_IMPL(int,
|
|
|
|
tor_close_socket,(tor_socket_t s))
|
2013-05-27 21:16:43 +02:00
|
|
|
{
|
|
|
|
int r = tor_close_socket_simple(s);
|
2009-05-12 22:17:32 +02:00
|
|
|
|
|
|
|
socket_accounting_lock();
|
|
|
|
#ifdef DEBUG_SOCKET_COUNTING
|
|
|
|
if (s > max_socket || ! bitarray_is_set(open_sockets, s)) {
|
|
|
|
log_warn(LD_BUG, "Closing a socket (%d) that wasn't returned by tor_open_"
|
|
|
|
"socket(), or that was already closed or something.", s);
|
|
|
|
} else {
|
|
|
|
tor_assert(open_sockets && s <= max_socket);
|
|
|
|
bitarray_clear(open_sockets, s);
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(DEBUG_SOCKET_COUNTING) */
|
2008-02-19 20:30:41 +01:00
|
|
|
if (r == 0) {
|
|
|
|
--n_sockets_open;
|
|
|
|
} else {
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2013-05-27 21:16:43 +02:00
|
|
|
if (r != WSAENOTSOCK)
|
2008-02-19 20:30:41 +01:00
|
|
|
--n_sockets_open;
|
|
|
|
#else
|
2013-05-27 21:16:43 +02:00
|
|
|
if (r != EBADF)
|
2016-06-20 17:03:13 +02:00
|
|
|
--n_sockets_open; // LCOV_EXCL_LINE -- EIO and EINTR too hard to force.
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) */
|
2008-02-19 20:30:41 +01:00
|
|
|
r = -1;
|
|
|
|
}
|
2009-05-12 22:17:32 +02:00
|
|
|
|
2016-06-20 17:03:13 +02:00
|
|
|
tor_assert_nonfatal(n_sockets_open >= 0);
|
2009-05-12 22:17:32 +02:00
|
|
|
socket_accounting_unlock();
|
2008-02-19 20:30:41 +01:00
|
|
|
return r;
|
2007-04-21 19:24:18 +02:00
|
|
|
}
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @{ */
|
2008-02-20 00:01:07 +01:00
|
|
|
#ifdef DEBUG_SOCKET_COUNTING
|
2008-02-21 04:38:46 +01:00
|
|
|
/** Helper: if DEBUG_SOCKET_COUNTING is enabled, remember that <b>s</b> is
|
|
|
|
* now an open socket. */
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline void
|
2011-05-23 06:17:48 +02:00
|
|
|
mark_socket_open(tor_socket_t s)
|
2008-02-20 00:01:07 +01:00
|
|
|
{
|
2011-05-23 06:17:48 +02:00
|
|
|
/* XXXX This bitarray business will NOT work on windows: sockets aren't
|
|
|
|
small ints there. */
|
2008-02-20 00:01:07 +01:00
|
|
|
if (s > max_socket) {
|
|
|
|
if (max_socket == -1) {
|
|
|
|
open_sockets = bitarray_init_zero(s+128);
|
|
|
|
max_socket = s+128;
|
|
|
|
} else {
|
|
|
|
open_sockets = bitarray_expand(open_sockets, max_socket, s+128);
|
|
|
|
max_socket = s+128;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bitarray_is_set(open_sockets, s)) {
|
|
|
|
log_warn(LD_BUG, "I thought that %d was already open, but socket() just "
|
|
|
|
"gave it to me!", s);
|
|
|
|
}
|
|
|
|
bitarray_set(open_sockets, s);
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(DEBUG_SOCKET_COUNTING)) */
|
2017-10-18 18:13:26 +02:00
|
|
|
#define mark_socket_open(s) ((void) (s))
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(DEBUG_SOCKET_COUNTING) */
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @} */
|
2008-02-20 00:01:07 +01:00
|
|
|
|
2007-04-21 19:24:18 +02:00
|
|
|
/** As socket(), but counts the number of open sockets. */
|
2015-03-14 19:20:50 +01:00
|
|
|
MOCK_IMPL(tor_socket_t,
|
|
|
|
tor_open_socket,(int domain, int type, int protocol))
|
2013-05-27 21:16:43 +02:00
|
|
|
{
|
|
|
|
return tor_open_socket_with_extensions(domain, type, protocol, 1, 0);
|
|
|
|
}
|
|
|
|
|
2015-03-14 19:20:50 +01:00
|
|
|
/** Mockable wrapper for connect(). */
|
|
|
|
MOCK_IMPL(tor_socket_t,
|
2016-07-28 16:22:10 +02:00
|
|
|
tor_connect_socket,(tor_socket_t sock, const struct sockaddr *address,
|
2015-03-14 19:20:50 +01:00
|
|
|
socklen_t address_len))
|
|
|
|
{
|
2016-07-28 16:22:10 +02:00
|
|
|
return connect(sock,address,address_len);
|
2015-03-14 19:20:50 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 21:16:43 +02:00
|
|
|
/** As socket(), but creates a nonblocking socket and
|
|
|
|
* counts the number of open sockets. */
|
|
|
|
tor_socket_t
|
|
|
|
tor_open_socket_nonblocking(int domain, int type, int protocol)
|
|
|
|
{
|
|
|
|
return tor_open_socket_with_extensions(domain, type, protocol, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** As socket(), but counts the number of open sockets and handles
|
|
|
|
* socket creation with either of SOCK_CLOEXEC and SOCK_NONBLOCK specified.
|
|
|
|
* <b>cloexec</b> and <b>nonblock</b> should be either 0 or 1 to indicate
|
|
|
|
* if the corresponding extension should be used.*/
|
|
|
|
tor_socket_t
|
|
|
|
tor_open_socket_with_extensions(int domain, int type, int protocol,
|
|
|
|
int cloexec, int nonblock)
|
2007-04-21 19:24:18 +02:00
|
|
|
{
|
2011-05-30 20:58:26 +02:00
|
|
|
tor_socket_t s;
|
2015-06-03 19:56:01 +02:00
|
|
|
|
|
|
|
/* We are about to create a new file descriptor so make sure we have
|
|
|
|
* enough of them. */
|
|
|
|
if (get_n_open_sockets() >= max_sockets - 1) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSASetLastError(WSAEMFILE);
|
|
|
|
#else
|
|
|
|
errno = EMFILE;
|
|
|
|
#endif
|
|
|
|
return TOR_INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
2013-05-27 21:16:43 +02:00
|
|
|
#if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
|
|
|
|
int ext_flags = (cloexec ? SOCK_CLOEXEC : 0) |
|
|
|
|
(nonblock ? SOCK_NONBLOCK : 0);
|
|
|
|
s = socket(domain, type|ext_flags, protocol);
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
if (SOCKET_OK(s))
|
|
|
|
goto socket_ok;
|
|
|
|
/* If we got an error, see if it is EINVAL. EINVAL might indicate that,
|
2013-05-27 21:16:43 +02:00
|
|
|
* even though we were built on a system with SOCK_CLOEXEC and SOCK_NONBLOCK
|
|
|
|
* support, we are running on one without. */
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
if (errno != EINVAL)
|
|
|
|
return s;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK) */
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
|
2010-11-20 06:58:33 +01:00
|
|
|
s = socket(domain, type, protocol);
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
if (! SOCKET_OK(s))
|
|
|
|
return s;
|
|
|
|
|
|
|
|
#if defined(FD_CLOEXEC)
|
2013-08-02 16:04:21 +02:00
|
|
|
if (cloexec) {
|
|
|
|
if (fcntl(s, F_SETFD, FD_CLOEXEC) == -1) {
|
|
|
|
log_warn(LD_FS,"Couldn't set FD_CLOEXEC: %s", strerror(errno));
|
|
|
|
tor_close_socket_simple(s);
|
|
|
|
return TOR_INVALID_SOCKET;
|
|
|
|
}
|
2013-03-12 01:58:28 +01:00
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(FD_CLOEXEC)) */
|
2013-08-02 16:52:57 +02:00
|
|
|
(void)cloexec;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(FD_CLOEXEC) */
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
|
2013-05-27 21:16:43 +02:00
|
|
|
if (nonblock) {
|
|
|
|
if (set_socket_nonblocking(s) == -1) {
|
|
|
|
tor_close_socket_simple(s);
|
|
|
|
return TOR_INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
goto socket_ok; /* So that socket_ok will not be unused. */
|
|
|
|
|
|
|
|
socket_ok:
|
2017-10-18 18:14:30 +02:00
|
|
|
tor_take_socket_ownership(s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For socket accounting: remember that we are the owner of the socket
|
|
|
|
* <b>s</b>. This will prevent us from overallocating sockets, and prevent us
|
|
|
|
* from asserting later when we close the socket <b>s</b>.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
tor_take_socket_ownership(tor_socket_t s)
|
|
|
|
{
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
socket_accounting_lock();
|
|
|
|
++n_sockets_open;
|
|
|
|
mark_socket_open(s);
|
|
|
|
socket_accounting_unlock();
|
2008-02-20 00:01:07 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 21:16:43 +02:00
|
|
|
/** As accept(), but counts the number of open sockets. */
|
2011-05-23 06:17:48 +02:00
|
|
|
tor_socket_t
|
2011-12-02 22:09:16 +01:00
|
|
|
tor_accept_socket(tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len)
|
2013-05-27 21:16:43 +02:00
|
|
|
{
|
|
|
|
return tor_accept_socket_with_extensions(sockfd, addr, len, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** As accept(), but returns a nonblocking socket and
|
|
|
|
* counts the number of open sockets. */
|
|
|
|
tor_socket_t
|
|
|
|
tor_accept_socket_nonblocking(tor_socket_t sockfd, struct sockaddr *addr,
|
|
|
|
socklen_t *len)
|
|
|
|
{
|
|
|
|
return tor_accept_socket_with_extensions(sockfd, addr, len, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** As accept(), but counts the number of open sockets and handles
|
|
|
|
* socket creation with either of SOCK_CLOEXEC and SOCK_NONBLOCK specified.
|
|
|
|
* <b>cloexec</b> and <b>nonblock</b> should be either 0 or 1 to indicate
|
|
|
|
* if the corresponding extension should be used.*/
|
|
|
|
tor_socket_t
|
|
|
|
tor_accept_socket_with_extensions(tor_socket_t sockfd, struct sockaddr *addr,
|
|
|
|
socklen_t *len, int cloexec, int nonblock)
|
2008-02-20 00:01:07 +01:00
|
|
|
{
|
2011-05-30 20:58:26 +02:00
|
|
|
tor_socket_t s;
|
2015-06-03 19:56:01 +02:00
|
|
|
|
|
|
|
/* We are about to create a new file descriptor so make sure we have
|
|
|
|
* enough of them. */
|
|
|
|
if (get_n_open_sockets() >= max_sockets - 1) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSASetLastError(WSAEMFILE);
|
|
|
|
#else
|
|
|
|
errno = EMFILE;
|
|
|
|
#endif
|
|
|
|
return TOR_INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:15:27 +02:00
|
|
|
#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) \
|
|
|
|
&& defined(SOCK_NONBLOCK)
|
2013-05-27 21:16:43 +02:00
|
|
|
int ext_flags = (cloexec ? SOCK_CLOEXEC : 0) |
|
|
|
|
(nonblock ? SOCK_NONBLOCK : 0);
|
|
|
|
s = accept4(sockfd, addr, len, ext_flags);
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
if (SOCKET_OK(s))
|
|
|
|
goto socket_ok;
|
2012-05-04 19:17:20 +02:00
|
|
|
/* If we got an error, see if it is ENOSYS. ENOSYS indicates that,
|
2012-05-07 07:54:53 +02:00
|
|
|
* even though we were built on a system with accept4 support, we
|
2012-05-04 19:17:20 +02:00
|
|
|
* are running on one without. Also, check for EINVAL, which indicates that
|
2013-05-27 21:16:43 +02:00
|
|
|
* we are missing SOCK_CLOEXEC/SOCK_NONBLOCK support. */
|
2012-05-04 19:17:20 +02:00
|
|
|
if (errno != EINVAL && errno != ENOSYS)
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
return s;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) ... */
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
|
|
|
|
s = accept(sockfd, addr, len);
|
|
|
|
if (!SOCKET_OK(s))
|
|
|
|
return s;
|
|
|
|
|
|
|
|
#if defined(FD_CLOEXEC)
|
2013-05-27 21:16:43 +02:00
|
|
|
if (cloexec) {
|
|
|
|
if (fcntl(s, F_SETFD, FD_CLOEXEC) == -1) {
|
|
|
|
log_warn(LD_NET, "Couldn't set FD_CLOEXEC: %s", strerror(errno));
|
|
|
|
tor_close_socket_simple(s);
|
|
|
|
return TOR_INVALID_SOCKET;
|
|
|
|
}
|
2013-03-12 01:58:28 +01:00
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(FD_CLOEXEC)) */
|
2013-08-02 16:52:57 +02:00
|
|
|
(void)cloexec;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(FD_CLOEXEC) */
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
|
2013-05-27 21:16:43 +02:00
|
|
|
if (nonblock) {
|
|
|
|
if (set_socket_nonblocking(s) == -1) {
|
|
|
|
tor_close_socket_simple(s);
|
|
|
|
return TOR_INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
goto socket_ok; /* So that socket_ok will not be unused. */
|
|
|
|
|
|
|
|
socket_ok:
|
2017-10-18 18:14:30 +02:00
|
|
|
tor_take_socket_ownership(s);
|
2007-04-21 19:24:18 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the number of sockets we currently have opened. */
|
|
|
|
int
|
|
|
|
get_n_open_sockets(void)
|
|
|
|
{
|
2009-05-12 22:17:32 +02:00
|
|
|
int n;
|
|
|
|
socket_accounting_lock();
|
|
|
|
n = n_sockets_open;
|
|
|
|
socket_accounting_unlock();
|
|
|
|
return n;
|
2007-04-21 19:24:18 +02:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:20:50 +01:00
|
|
|
/** Mockable wrapper for getsockname(). */
|
|
|
|
MOCK_IMPL(int,
|
2016-07-28 16:22:10 +02:00
|
|
|
tor_getsockname,(tor_socket_t sock, struct sockaddr *address,
|
2015-03-14 19:20:50 +01:00
|
|
|
socklen_t *address_len))
|
|
|
|
{
|
2016-07-28 16:22:10 +02:00
|
|
|
return getsockname(sock, address, address_len);
|
2015-03-14 19:20:50 +01:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:48:45 +01:00
|
|
|
/**
|
|
|
|
* Find the local address associated with the socket <b>sock</b>, and
|
|
|
|
* place it in *<b>addr_out</b>. Return 0 on success, -1 on failure.
|
|
|
|
*
|
|
|
|
* (As tor_getsockname, but instead places the result in a tor_addr_t.) */
|
|
|
|
int
|
|
|
|
tor_addr_from_getsockname(tor_addr_t *addr_out, tor_socket_t sock)
|
|
|
|
{
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
socklen_t ss_len = sizeof(ss);
|
2018-01-26 17:59:01 +01:00
|
|
|
memset(&ss, 0, sizeof(ss));
|
2018-01-26 17:48:45 +01:00
|
|
|
|
|
|
|
if (tor_getsockname(sock, (struct sockaddr *) &ss, &ss_len) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return tor_addr_from_sockaddr(addr_out, (struct sockaddr *)&ss, NULL);
|
|
|
|
}
|
|
|
|
|
2013-03-12 01:58:28 +01:00
|
|
|
/** Turn <b>socket</b> into a nonblocking socket. Return 0 on success, -1
|
|
|
|
* on failure.
|
2004-11-01 21:41:47 +01:00
|
|
|
*/
|
2013-03-12 01:58:28 +01:00
|
|
|
int
|
2016-07-28 16:22:10 +02:00
|
|
|
set_socket_nonblocking(tor_socket_t sock)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
2012-01-31 16:59:42 +01:00
|
|
|
#if defined(_WIN32)
|
2006-09-06 10:42:16 +02:00
|
|
|
unsigned long nonblocking = 1;
|
2016-07-28 16:22:10 +02:00
|
|
|
ioctlsocket(sock, FIONBIO, (unsigned long*) &nonblocking);
|
2004-11-01 21:41:47 +01:00
|
|
|
#else
|
2013-03-12 01:58:28 +01:00
|
|
|
int flags;
|
|
|
|
|
2016-07-28 16:22:10 +02:00
|
|
|
flags = fcntl(sock, F_GETFL, 0);
|
2013-03-12 01:58:28 +01:00
|
|
|
if (flags == -1) {
|
|
|
|
log_warn(LD_NET, "Couldn't get file status flags: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
flags |= O_NONBLOCK;
|
2016-07-28 16:22:10 +02:00
|
|
|
if (fcntl(sock, F_SETFL, flags) == -1) {
|
2013-03-12 01:58:28 +01:00
|
|
|
log_warn(LD_NET, "Couldn't set file status flags: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) */
|
2013-03-12 01:58:28 +01:00
|
|
|
|
|
|
|
return 0;
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate a pair of connected sockets. (Like socketpair(family,
|
|
|
|
* type,protocol,fd), but works on systems that don't have
|
|
|
|
* socketpair.)
|
|
|
|
*
|
2007-03-15 23:47:21 +01:00
|
|
|
* Currently, only (AF_UNIX, SOCK_STREAM, 0) sockets are supported.
|
2004-11-01 21:41:47 +01:00
|
|
|
*
|
|
|
|
* Note that on systems without socketpair, this call will fail if
|
|
|
|
* localhost is inaccessible (for example, if the networking
|
|
|
|
* stack is down). And even if it succeeds, the socket pair will not
|
|
|
|
* be able to read while localhost is down later (the socket pair may
|
|
|
|
* even close, depending on OS-specific timeouts).
|
2005-06-26 07:03:21 +02:00
|
|
|
*
|
2005-06-30 09:17:38 +02:00
|
|
|
* Returns 0 on success and -errno on failure; do not rely on the value
|
2006-11-22 00:50:21 +01:00
|
|
|
* of errno or WSAGetLastError().
|
2004-11-01 21:41:47 +01:00
|
|
|
**/
|
2005-06-30 09:17:38 +02:00
|
|
|
/* It would be nicer just to set errno, but that won't work for windows. */
|
2004-11-01 21:41:47 +01:00
|
|
|
int
|
2011-05-23 06:17:48 +02:00
|
|
|
tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
2006-09-06 03:49:55 +02:00
|
|
|
//don't use win32 socketpairs (they are always bad)
|
2012-01-31 16:59:42 +01:00
|
|
|
#if defined(HAVE_SOCKETPAIR) && !defined(_WIN32)
|
2005-06-30 09:17:38 +02:00
|
|
|
int r;
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
|
2010-11-20 07:16:29 +01:00
|
|
|
#ifdef SOCK_CLOEXEC
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
r = socketpair(family, type|SOCK_CLOEXEC, protocol, fd);
|
|
|
|
if (r == 0)
|
|
|
|
goto sockets_ok;
|
|
|
|
/* If we got an error, see if it is EINVAL. EINVAL might indicate that,
|
2012-05-07 07:54:53 +02:00
|
|
|
* even though we were built on a system with SOCK_CLOEXEC support, we
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
* are running on one without. */
|
|
|
|
if (errno != EINVAL)
|
|
|
|
return -errno;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(SOCK_CLOEXEC) */
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
|
2005-06-30 09:17:38 +02:00
|
|
|
r = socketpair(family, type, protocol, fd);
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
#if defined(FD_CLOEXEC)
|
2013-03-18 20:44:23 +01:00
|
|
|
if (SOCKET_OK(fd[0])) {
|
|
|
|
r = fcntl(fd[0], F_SETFD, FD_CLOEXEC);
|
|
|
|
if (r == -1) {
|
|
|
|
close(fd[0]);
|
|
|
|
close(fd[1]);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (SOCKET_OK(fd[1])) {
|
|
|
|
r = fcntl(fd[1], F_SETFD, FD_CLOEXEC);
|
|
|
|
if (r == -1) {
|
|
|
|
close(fd[0]);
|
|
|
|
close(fd[1]);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(FD_CLOEXEC) */
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
goto sockets_ok; /* So that sockets_ok will not be unused. */
|
|
|
|
|
|
|
|
sockets_ok:
|
|
|
|
socket_accounting_lock();
|
|
|
|
if (SOCKET_OK(fd[0])) {
|
|
|
|
++n_sockets_open;
|
|
|
|
mark_socket_open(fd[0]);
|
2008-02-20 00:46:08 +01:00
|
|
|
}
|
If SOCK_CLOEXEC and friends fail, fall back to regular socket() calls
Since 0.2.3.1-alpha, we've supported the Linux extensions to socket(),
open(), socketpair(), and accept() that enable us to create an fd and
make it close-on-exec with a single syscall. This not only saves us a
syscall (big deal), but makes us less vulnerable to race conditions
where we open a socket and then exec before we can make it
close-on-exec.
But these extensions are not supported on all Linuxes: They were added
between 2.6.23 or so and 2.6.28 or so. If you were to build your Tor
against a recent Linux's kernel headers, and then run it with a older
kernel, you would find yourselve unable to open sockets. Ouch!
The solution here is that, when one of these syscalls fails with
EINVAL, we should try again in the portable way. This adds an extra
syscall in the case where we built with new headers and are running
with old ones, but it will at least allow Tor to work.
Fixes bug 5112; bugfix on 0.2.3.1-alpha.
2012-02-14 16:34:06 +01:00
|
|
|
if (SOCKET_OK(fd[1])) {
|
|
|
|
++n_sockets_open;
|
|
|
|
mark_socket_open(fd[1]);
|
|
|
|
}
|
|
|
|
socket_accounting_unlock();
|
|
|
|
|
|
|
|
return 0;
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(HAVE_SOCKETPAIR) && !defined(_WIN32)) */
|
2013-08-02 16:36:01 +02:00
|
|
|
return tor_ersatz_socketpair(family, type, protocol, fd);
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_SOCKETPAIR) && !defined(_WIN32) */
|
2013-08-02 16:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NEED_ERSATZ_SOCKETPAIR
|
2015-11-19 09:08:22 +01:00
|
|
|
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline socklen_t
|
2015-11-27 17:48:54 +01:00
|
|
|
SIZEOF_SOCKADDR(int domain)
|
|
|
|
{
|
|
|
|
switch (domain) {
|
|
|
|
case AF_INET:
|
|
|
|
return sizeof(struct sockaddr_in);
|
|
|
|
case AF_INET6:
|
|
|
|
return sizeof(struct sockaddr_in6);
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 09:08:22 +01:00
|
|
|
|
2013-08-02 16:36:01 +02:00
|
|
|
/**
|
|
|
|
* Helper used to implement socketpair on systems that lack it, by
|
|
|
|
* making a direct connection to localhost.
|
|
|
|
*/
|
|
|
|
STATIC int
|
|
|
|
tor_ersatz_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
|
|
|
|
{
|
2004-11-01 21:41:47 +01:00
|
|
|
/* This socketpair does not work when localhost is down. So
|
|
|
|
* it's really not the same thing at all. But it's close enough
|
|
|
|
* for now, and really, when localhost is down sometimes, we
|
|
|
|
* have other problems too.
|
|
|
|
*/
|
2013-08-06 22:41:53 +02:00
|
|
|
tor_socket_t listener = TOR_INVALID_SOCKET;
|
|
|
|
tor_socket_t connector = TOR_INVALID_SOCKET;
|
|
|
|
tor_socket_t acceptor = TOR_INVALID_SOCKET;
|
2015-11-19 09:08:22 +01:00
|
|
|
tor_addr_t listen_tor_addr;
|
2015-11-27 17:52:59 +01:00
|
|
|
struct sockaddr_storage connect_addr_ss, listen_addr_ss;
|
|
|
|
struct sockaddr *listen_addr = (struct sockaddr *) &listen_addr_ss;
|
2015-11-27 17:39:03 +01:00
|
|
|
uint16_t listen_port = 0;
|
2015-11-19 09:08:22 +01:00
|
|
|
tor_addr_t connect_tor_addr;
|
2015-11-27 17:39:03 +01:00
|
|
|
uint16_t connect_port = 0;
|
2015-11-27 17:52:59 +01:00
|
|
|
struct sockaddr *connect_addr = (struct sockaddr *) &connect_addr_ss;
|
2013-08-02 16:36:01 +02:00
|
|
|
socklen_t size;
|
2005-06-30 09:17:38 +02:00
|
|
|
int saved_errno = -1;
|
2015-11-19 09:08:22 +01:00
|
|
|
int ersatz_domain = AF_INET;
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2015-11-19 09:08:22 +01:00
|
|
|
memset(&connect_tor_addr, 0, sizeof(connect_tor_addr));
|
2015-11-27 17:52:59 +01:00
|
|
|
memset(&connect_addr_ss, 0, sizeof(connect_addr_ss));
|
2015-11-19 09:08:22 +01:00
|
|
|
memset(&listen_tor_addr, 0, sizeof(listen_tor_addr));
|
2015-11-27 17:52:59 +01:00
|
|
|
memset(&listen_addr_ss, 0, sizeof(listen_addr_ss));
|
2014-09-02 18:58:32 +02:00
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
if (protocol
|
|
|
|
#ifdef AF_UNIX
|
|
|
|
|| family != AF_UNIX
|
|
|
|
#endif
|
|
|
|
) {
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2005-06-30 09:17:38 +02:00
|
|
|
return -WSAEAFNOSUPPORT;
|
2004-11-01 21:41:47 +01:00
|
|
|
#else
|
2005-06-30 09:17:38 +02:00
|
|
|
return -EAFNOSUPPORT;
|
2004-11-01 21:41:47 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (!fd) {
|
2005-06-30 09:17:38 +02:00
|
|
|
return -EINVAL;
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
|
|
|
|
2015-11-19 09:08:22 +01:00
|
|
|
listener = tor_open_socket(ersatz_domain, type, 0);
|
|
|
|
if (!SOCKET_OK(listener)) {
|
|
|
|
int first_errno = tor_socket_errno(-1);
|
|
|
|
if (first_errno == SOCK_ERRNO(EPROTONOSUPPORT)
|
|
|
|
&& ersatz_domain == AF_INET) {
|
|
|
|
/* Assume we're on an IPv6-only system */
|
|
|
|
ersatz_domain = AF_INET6;
|
|
|
|
listener = tor_open_socket(ersatz_domain, type, 0);
|
|
|
|
if (!SOCKET_OK(listener)) {
|
|
|
|
/* Keep the previous behaviour, which was to return the IPv4 error.
|
|
|
|
* (This may be less informative on IPv6-only systems.)
|
|
|
|
* XX/teor - is there a better way to decide which errno to return?
|
|
|
|
* (I doubt we care much either way, once there is an error.)
|
|
|
|
*/
|
|
|
|
return -first_errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* If there is no 127.0.0.1 or ::1, this will and must fail. Otherwise, we
|
|
|
|
* risk exposing a socketpair on a routable IP address. (Some BSD jails
|
|
|
|
* use a routable address for localhost. Fortunately, they have the real
|
|
|
|
* AF_UNIX socketpair.) */
|
|
|
|
if (ersatz_domain == AF_INET) {
|
|
|
|
tor_addr_from_ipv4h(&listen_tor_addr, INADDR_LOOPBACK);
|
|
|
|
} else {
|
|
|
|
tor_addr_parse(&listen_tor_addr, "[::1]");
|
|
|
|
}
|
|
|
|
tor_assert(tor_addr_is_loopback(&listen_tor_addr));
|
2015-11-27 17:52:59 +01:00
|
|
|
size = tor_addr_to_sockaddr(&listen_tor_addr,
|
2015-11-19 09:08:22 +01:00
|
|
|
0 /* kernel chooses port. */,
|
2015-11-27 17:52:59 +01:00
|
|
|
listen_addr,
|
|
|
|
sizeof(listen_addr_ss));
|
|
|
|
if (bind(listener, listen_addr, size) == -1)
|
2005-06-30 09:17:38 +02:00
|
|
|
goto tidy_up_and_fail;
|
2004-11-01 21:41:47 +01:00
|
|
|
if (listen(listener, 1) == -1)
|
2005-06-30 09:17:38 +02:00
|
|
|
goto tidy_up_and_fail;
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2015-11-19 09:08:22 +01:00
|
|
|
connector = tor_open_socket(ersatz_domain, type, 0);
|
2012-01-17 22:35:07 +01:00
|
|
|
if (!SOCKET_OK(connector))
|
2005-06-30 09:17:38 +02:00
|
|
|
goto tidy_up_and_fail;
|
2004-11-01 21:41:47 +01:00
|
|
|
/* We want to find out the port number to connect to. */
|
2015-11-27 17:52:59 +01:00
|
|
|
size = sizeof(connect_addr_ss);
|
|
|
|
if (getsockname(listener, connect_addr, &size) == -1)
|
2005-06-30 09:17:38 +02:00
|
|
|
goto tidy_up_and_fail;
|
2015-11-27 17:52:59 +01:00
|
|
|
if (size != SIZEOF_SOCKADDR (connect_addr->sa_family))
|
2005-06-30 09:17:38 +02:00
|
|
|
goto abort_tidy_up_and_fail;
|
2015-11-27 17:52:59 +01:00
|
|
|
if (connect(connector, connect_addr, size) == -1)
|
2005-06-30 09:17:38 +02:00
|
|
|
goto tidy_up_and_fail;
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2015-11-27 17:52:59 +01:00
|
|
|
size = sizeof(listen_addr_ss);
|
|
|
|
acceptor = tor_accept_socket(listener, listen_addr, &size);
|
2012-01-17 22:35:07 +01:00
|
|
|
if (!SOCKET_OK(acceptor))
|
2005-06-30 09:17:38 +02:00
|
|
|
goto tidy_up_and_fail;
|
2015-11-27 17:52:59 +01:00
|
|
|
if (size != SIZEOF_SOCKADDR(listen_addr->sa_family))
|
2005-06-30 09:17:38 +02:00
|
|
|
goto abort_tidy_up_and_fail;
|
2004-11-01 21:41:47 +01:00
|
|
|
/* Now check we are talking to ourself by matching port and host on the
|
|
|
|
two sockets. */
|
2015-11-27 17:52:59 +01:00
|
|
|
if (getsockname(connector, connect_addr, &size) == -1)
|
2005-06-30 09:17:38 +02:00
|
|
|
goto tidy_up_and_fail;
|
2015-11-19 09:08:22 +01:00
|
|
|
/* Set *_tor_addr and *_port to the address and port that was used */
|
2015-11-27 17:52:59 +01:00
|
|
|
tor_addr_from_sockaddr(&listen_tor_addr, listen_addr, &listen_port);
|
|
|
|
tor_addr_from_sockaddr(&connect_tor_addr, connect_addr, &connect_port);
|
|
|
|
if (size != SIZEOF_SOCKADDR (connect_addr->sa_family)
|
2015-11-19 09:08:22 +01:00
|
|
|
|| tor_addr_compare(&listen_tor_addr, &connect_tor_addr, CMP_SEMANTIC)
|
|
|
|
|| listen_port != connect_port) {
|
2005-06-30 09:17:38 +02:00
|
|
|
goto abort_tidy_up_and_fail;
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
2013-08-06 22:41:57 +02:00
|
|
|
tor_close_socket(listener);
|
2004-11-01 21:41:47 +01:00
|
|
|
fd[0] = connector;
|
|
|
|
fd[1] = acceptor;
|
2005-01-03 20:07:25 +01:00
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
abort_tidy_up_and_fail:
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2005-06-30 09:17:38 +02:00
|
|
|
saved_errno = WSAECONNABORTED;
|
2004-11-01 21:41:47 +01:00
|
|
|
#else
|
2005-06-30 09:17:38 +02:00
|
|
|
saved_errno = ECONNABORTED; /* I hope this is portable and appropriate. */
|
2004-11-01 21:41:47 +01:00
|
|
|
#endif
|
|
|
|
tidy_up_and_fail:
|
2005-06-30 09:17:38 +02:00
|
|
|
if (saved_errno < 0)
|
|
|
|
saved_errno = errno;
|
2013-08-06 22:41:53 +02:00
|
|
|
if (SOCKET_OK(listener))
|
2005-06-30 09:17:38 +02:00
|
|
|
tor_close_socket(listener);
|
2013-08-06 22:41:53 +02:00
|
|
|
if (SOCKET_OK(connector))
|
2005-06-30 09:17:38 +02:00
|
|
|
tor_close_socket(connector);
|
2013-08-06 22:41:53 +02:00
|
|
|
if (SOCKET_OK(acceptor))
|
2005-06-30 09:17:38 +02:00
|
|
|
tor_close_socket(acceptor);
|
|
|
|
return -saved_errno;
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
2015-11-19 09:08:22 +01:00
|
|
|
|
|
|
|
#undef SIZEOF_SOCKADDR
|
|
|
|
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(NEED_ERSATZ_SOCKETPAIR) */
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2015-07-30 20:43:15 +02:00
|
|
|
/* Return the maximum number of allowed sockets. */
|
|
|
|
int
|
|
|
|
get_max_sockets(void)
|
|
|
|
{
|
|
|
|
return max_sockets;
|
|
|
|
}
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Number of extra file descriptors to keep in reserve beyond those that we
|
|
|
|
* tell Tor it's allowed to use. */
|
2012-10-12 18:22:13 +02:00
|
|
|
#define ULIMIT_BUFFER 32 /* keep 32 extra fd's beyond ConnLimit_ */
|
2005-02-23 00:52:44 +01:00
|
|
|
|
2015-06-02 22:37:11 +02:00
|
|
|
/** Learn the maximum allowed number of file descriptors, and tell the
|
|
|
|
* system we want to use up to that number. (Some systems have a low soft
|
|
|
|
* limit, and let us set it higher.) We compute this by finding the largest
|
|
|
|
* number that we can use.
|
2006-02-09 03:59:37 +01:00
|
|
|
*
|
2015-06-02 22:37:11 +02:00
|
|
|
* If the limit is below the reserved file descriptor value (ULIMIT_BUFFER),
|
|
|
|
* return -1 and <b>max_out</b> is untouched.
|
2006-02-09 03:59:37 +01:00
|
|
|
*
|
2015-06-02 22:37:11 +02:00
|
|
|
* If we can't find a number greater than or equal to <b>limit</b>, then we
|
|
|
|
* fail by returning -1 and <b>max_out</b> is untouched.
|
|
|
|
*
|
|
|
|
* If we are unable to set the limit value because of setrlimit() failing,
|
2018-01-04 19:20:37 +01:00
|
|
|
* return 0 and <b>max_out</b> is set to the current maximum value returned
|
2015-06-02 22:37:11 +02:00
|
|
|
* by getrlimit().
|
|
|
|
*
|
|
|
|
* Otherwise, return 0 and store the maximum we found inside <b>max_out</b>
|
|
|
|
* and set <b>max_sockets</b> with that value as well.*/
|
2005-03-14 04:28:46 +01:00
|
|
|
int
|
2008-02-21 22:15:31 +01:00
|
|
|
set_max_file_descriptors(rlim_t limit, int *max_out)
|
2005-09-30 03:09:52 +02:00
|
|
|
{
|
2015-06-03 19:56:01 +02:00
|
|
|
if (limit < ULIMIT_BUFFER) {
|
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"ConnLimit must be at least %d. Failing.", ULIMIT_BUFFER);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-20 23:28:26 +01:00
|
|
|
/* Define some maximum connections values for systems where we cannot
|
|
|
|
* automatically determine a limit. Re Cygwin, see
|
|
|
|
* http://archives.seul.org/or/talk/Aug-2006/msg00210.html
|
|
|
|
* For an iPhone, 9999 should work. For Windows and all other unknown
|
|
|
|
* systems we use 15000 as the default. */
|
2004-11-01 21:41:47 +01:00
|
|
|
#ifndef HAVE_GETRLIMIT
|
2008-03-18 16:11:52 +01:00
|
|
|
#if defined(CYGWIN) || defined(__CYGWIN__)
|
|
|
|
const char *platform = "Cygwin";
|
|
|
|
const unsigned long MAX_CONNECTIONS = 3200;
|
2012-01-31 16:59:42 +01:00
|
|
|
#elif defined(_WIN32)
|
2008-03-18 16:11:52 +01:00
|
|
|
const char *platform = "Windows";
|
|
|
|
const unsigned long MAX_CONNECTIONS = 15000;
|
|
|
|
#else
|
|
|
|
const char *platform = "unknown platforms with no getrlimit()";
|
|
|
|
const unsigned long MAX_CONNECTIONS = 15000;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(CYGWIN) || defined(__CYGWIN__) || ... */
|
2005-10-18 23:58:19 +02:00
|
|
|
log_fn(LOG_INFO, LD_NET,
|
|
|
|
"This platform is missing getrlimit(). Proceeding.");
|
2008-03-18 16:11:52 +01:00
|
|
|
if (limit > MAX_CONNECTIONS) {
|
2008-02-20 23:28:26 +01:00
|
|
|
log_warn(LD_CONFIG,
|
2008-02-22 04:26:35 +01:00
|
|
|
"We do not support more than %lu file descriptors "
|
2008-03-18 16:11:52 +01:00
|
|
|
"on %s. Tried to raise to %lu.",
|
|
|
|
(unsigned long)MAX_CONNECTIONS, platform, (unsigned long)limit);
|
2008-02-20 23:28:26 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2008-03-18 16:11:52 +01:00
|
|
|
limit = MAX_CONNECTIONS;
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(!defined(HAVE_GETRLIMIT)) */
|
2004-11-01 21:41:47 +01:00
|
|
|
struct rlimit rlim;
|
|
|
|
|
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_NET, "Could not get maximum number of file descriptors: %s",
|
|
|
|
strerror(errno));
|
2004-11-01 21:41:47 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2008-02-21 22:15:31 +01:00
|
|
|
if (rlim.rlim_max < limit) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_CONFIG,"We need %lu file descriptors available, and we're "
|
|
|
|
"limited to %lu. Please change your ulimit -n.",
|
2008-02-21 22:15:31 +01:00
|
|
|
(unsigned long)limit, (unsigned long)rlim.rlim_max);
|
2004-11-01 21:41:47 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2008-02-20 23:28:26 +01:00
|
|
|
|
|
|
|
if (rlim.rlim_max > rlim.rlim_cur) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_info(LD_NET,"Raising max file descriptors from %lu to %lu.",
|
2008-02-20 23:28:26 +01:00
|
|
|
(unsigned long)rlim.rlim_cur, (unsigned long)rlim.rlim_max);
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
2015-06-03 19:56:01 +02:00
|
|
|
/* Set the current limit value so if the attempt to set the limit to the
|
|
|
|
* max fails at least we'll have a valid value of maximum sockets. */
|
2015-06-02 22:37:11 +02:00
|
|
|
*max_out = max_sockets = (int)rlim.rlim_cur - ULIMIT_BUFFER;
|
2008-02-20 23:28:26 +01:00
|
|
|
rlim.rlim_cur = rlim.rlim_max;
|
2007-11-02 17:02:26 +01:00
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
|
2018-01-04 19:20:37 +01:00
|
|
|
int couldnt_set = 1;
|
|
|
|
const int setrlimit_errno = errno;
|
2007-11-02 17:02:26 +01:00
|
|
|
#ifdef OPEN_MAX
|
2017-07-03 07:30:52 +02:00
|
|
|
uint64_t try_limit = OPEN_MAX - ULIMIT_BUFFER;
|
2017-07-05 21:56:57 +02:00
|
|
|
if (errno == EINVAL && try_limit < (uint64_t) rlim.rlim_cur) {
|
2007-11-02 17:02:26 +01:00
|
|
|
/* On some platforms, OPEN_MAX is the real limit, and getrlimit() is
|
|
|
|
* full of nasty lies. I'm looking at you, OSX 10.5.... */
|
2018-01-17 15:06:32 +01:00
|
|
|
rlim.rlim_cur = MIN((rlim_t) try_limit, rlim.rlim_cur);
|
2007-11-02 17:02:26 +01:00
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rlim) == 0) {
|
2007-11-06 15:19:14 +01:00
|
|
|
if (rlim.rlim_cur < (rlim_t)limit) {
|
2007-11-02 17:02:26 +01:00
|
|
|
log_warn(LD_CONFIG, "We are limited to %lu file descriptors by "
|
2017-07-03 07:30:52 +02:00
|
|
|
"OPEN_MAX (%lu), and ConnLimit is %lu. Changing "
|
|
|
|
"ConnLimit; sorry.",
|
|
|
|
(unsigned long)try_limit, (unsigned long)OPEN_MAX,
|
|
|
|
(unsigned long)limit);
|
2007-11-02 17:02:26 +01:00
|
|
|
} else {
|
2017-07-03 07:30:52 +02:00
|
|
|
log_info(LD_CONFIG, "Dropped connection limit to %lu based on "
|
|
|
|
"OPEN_MAX (%lu); Apparently, %lu was too high and rlimit "
|
|
|
|
"lied to us.",
|
|
|
|
(unsigned long)try_limit, (unsigned long)OPEN_MAX,
|
|
|
|
(unsigned long)rlim.rlim_max);
|
2007-11-02 17:02:26 +01:00
|
|
|
}
|
2018-01-04 19:20:37 +01:00
|
|
|
couldnt_set = 0;
|
2007-11-02 17:02:26 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(OPEN_MAX) */
|
2018-01-04 19:20:37 +01:00
|
|
|
if (couldnt_set) {
|
2007-11-02 18:16:34 +01:00
|
|
|
log_warn(LD_CONFIG,"Couldn't set maximum number of file descriptors: %s",
|
2018-01-04 19:20:37 +01:00
|
|
|
strerror(setrlimit_errno));
|
2007-11-02 17:02:26 +01:00
|
|
|
}
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
2005-02-23 00:52:44 +01:00
|
|
|
/* leave some overhead for logs, etc, */
|
2008-02-20 23:28:26 +01:00
|
|
|
limit = rlim.rlim_cur;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(HAVE_GETRLIMIT) */
|
2005-02-23 00:52:44 +01:00
|
|
|
|
2008-02-20 23:28:26 +01:00
|
|
|
if (limit > INT_MAX)
|
2008-02-21 22:15:31 +01:00
|
|
|
limit = INT_MAX;
|
2008-02-20 23:28:26 +01:00
|
|
|
tor_assert(max_out);
|
2015-06-03 19:56:01 +02:00
|
|
|
*max_out = max_sockets = (int)limit - ULIMIT_BUFFER;
|
2008-02-20 23:28:26 +01:00
|
|
|
return 0;
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
|
|
|
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifndef _WIN32
|
2008-11-07 03:06:12 +01:00
|
|
|
/** Log details of current user and group credentials. Return 0 on
|
|
|
|
* success. Logs and return -1 on failure.
|
2004-11-01 21:41:47 +01:00
|
|
|
*/
|
2008-11-07 03:53:46 +01:00
|
|
|
static int
|
|
|
|
log_credential_status(void)
|
2008-11-07 03:06:12 +01:00
|
|
|
{
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Log level to use when describing non-error UID/GID status. */
|
2008-11-07 03:06:12 +01:00
|
|
|
#define CREDENTIAL_LOG_LEVEL LOG_INFO
|
|
|
|
/* Real, effective and saved UIDs */
|
|
|
|
uid_t ruid, euid, suid;
|
|
|
|
/* Read, effective and saved GIDs */
|
|
|
|
gid_t rgid, egid, sgid;
|
|
|
|
/* Supplementary groups */
|
2011-07-01 18:36:33 +02:00
|
|
|
gid_t *sup_gids = NULL;
|
|
|
|
int sup_gids_size;
|
2008-11-07 03:06:12 +01:00
|
|
|
/* Number of supplementary groups */
|
|
|
|
int ngids;
|
|
|
|
|
|
|
|
/* log UIDs */
|
|
|
|
#ifdef HAVE_GETRESUID
|
|
|
|
if (getresuid(&ruid, &euid, &suid) != 0 ) {
|
|
|
|
log_warn(LD_GENERAL, "Error getting changed UIDs: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
} else {
|
2008-11-07 03:53:46 +01:00
|
|
|
log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL,
|
2008-11-26 17:13:12 +01:00
|
|
|
"UID is %u (real), %u (effective), %u (saved)",
|
|
|
|
(unsigned)ruid, (unsigned)euid, (unsigned)suid);
|
2008-11-07 03:06:12 +01:00
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(HAVE_GETRESUID)) */
|
2008-11-07 03:06:12 +01:00
|
|
|
/* getresuid is not present on MacOS X, so we can't get the saved (E)UID */
|
|
|
|
ruid = getuid();
|
|
|
|
euid = geteuid();
|
|
|
|
(void)suid;
|
|
|
|
|
2008-11-07 03:53:46 +01:00
|
|
|
log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL,
|
2008-11-26 17:13:12 +01:00
|
|
|
"UID is %u (real), %u (effective), unknown (saved)",
|
|
|
|
(unsigned)ruid, (unsigned)euid);
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_GETRESUID) */
|
2008-11-07 03:06:12 +01:00
|
|
|
|
|
|
|
/* log GIDs */
|
|
|
|
#ifdef HAVE_GETRESGID
|
|
|
|
if (getresgid(&rgid, &egid, &sgid) != 0 ) {
|
|
|
|
log_warn(LD_GENERAL, "Error getting changed GIDs: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
} else {
|
2008-11-07 03:53:46 +01:00
|
|
|
log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL,
|
2008-11-26 17:13:12 +01:00
|
|
|
"GID is %u (real), %u (effective), %u (saved)",
|
|
|
|
(unsigned)rgid, (unsigned)egid, (unsigned)sgid);
|
2008-11-07 03:06:12 +01:00
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(HAVE_GETRESGID)) */
|
2008-11-07 03:06:12 +01:00
|
|
|
/* getresgid is not present on MacOS X, so we can't get the saved (E)GID */
|
|
|
|
rgid = getgid();
|
|
|
|
egid = getegid();
|
|
|
|
(void)sgid;
|
2008-11-07 03:53:46 +01:00
|
|
|
log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL,
|
2008-11-26 17:13:12 +01:00
|
|
|
"GID is %u (real), %u (effective), unknown (saved)",
|
|
|
|
(unsigned)rgid, (unsigned)egid);
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_GETRESGID) */
|
2008-11-07 03:06:12 +01:00
|
|
|
|
|
|
|
/* log supplementary groups */
|
2011-07-01 18:36:33 +02:00
|
|
|
sup_gids_size = 64;
|
2014-11-02 17:56:02 +01:00
|
|
|
sup_gids = tor_calloc(64, sizeof(gid_t));
|
2011-07-01 18:36:33 +02:00
|
|
|
while ((ngids = getgroups(sup_gids_size, sup_gids)) < 0 &&
|
|
|
|
errno == EINVAL &&
|
|
|
|
sup_gids_size < NGROUPS_MAX) {
|
|
|
|
sup_gids_size *= 2;
|
2014-08-13 16:36:06 +02:00
|
|
|
sup_gids = tor_reallocarray(sup_gids, sizeof(gid_t), sup_gids_size);
|
2011-07-01 18:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ngids < 0) {
|
2008-11-07 03:53:46 +01:00
|
|
|
log_warn(LD_GENERAL, "Error getting supplementary GIDs: %s",
|
|
|
|
strerror(errno));
|
2011-07-01 18:36:33 +02:00
|
|
|
tor_free(sup_gids);
|
2008-11-07 03:06:12 +01:00
|
|
|
return -1;
|
|
|
|
} else {
|
2008-12-05 02:29:59 +01:00
|
|
|
int i, retval = 0;
|
2008-11-07 03:06:12 +01:00
|
|
|
char *s = NULL;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *elts = smartlist_new();
|
2008-11-07 03:06:12 +01:00
|
|
|
|
|
|
|
for (i = 0; i<ngids; i++) {
|
2012-01-11 20:02:59 +01:00
|
|
|
smartlist_add_asprintf(elts, "%u", (unsigned)sup_gids[i]);
|
2008-11-07 03:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s = smartlist_join_strings(elts, " ", 0, NULL);
|
|
|
|
|
2008-11-07 03:53:46 +01:00
|
|
|
log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL, "Supplementary groups are: %s",s);
|
2008-11-07 03:06:12 +01:00
|
|
|
|
|
|
|
tor_free(s);
|
2012-01-11 20:02:59 +01:00
|
|
|
SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
|
2008-11-07 03:06:12 +01:00
|
|
|
smartlist_free(elts);
|
2011-07-01 18:36:33 +02:00
|
|
|
tor_free(sup_gids);
|
2008-11-07 03:06:12 +01:00
|
|
|
|
2008-12-05 02:29:59 +01:00
|
|
|
return retval;
|
2008-11-07 03:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(_WIN32) */
|
2008-11-07 03:06:12 +01:00
|
|
|
|
2014-05-14 19:50:43 +02:00
|
|
|
#ifndef _WIN32
|
2014-05-15 04:49:38 +02:00
|
|
|
/** Cached struct from the last getpwname() call we did successfully. */
|
2014-05-14 19:50:43 +02:00
|
|
|
static struct passwd *passwd_cached = NULL;
|
|
|
|
|
2014-05-15 04:49:38 +02:00
|
|
|
/** Helper: copy a struct passwd object.
|
|
|
|
*
|
|
|
|
* We only copy the fields pw_uid, pw_gid, pw_name, pw_dir. Tor doesn't use
|
|
|
|
* any others, and I don't want to run into incompatibilities.
|
|
|
|
*/
|
2014-05-14 19:50:43 +02:00
|
|
|
static struct passwd *
|
|
|
|
tor_passwd_dup(const struct passwd *pw)
|
|
|
|
{
|
|
|
|
struct passwd *new_pw = tor_malloc_zero(sizeof(struct passwd));
|
|
|
|
if (pw->pw_name)
|
|
|
|
new_pw->pw_name = tor_strdup(pw->pw_name);
|
|
|
|
if (pw->pw_dir)
|
|
|
|
new_pw->pw_dir = tor_strdup(pw->pw_dir);
|
|
|
|
new_pw->pw_uid = pw->pw_uid;
|
|
|
|
new_pw->pw_gid = pw->pw_gid;
|
|
|
|
|
|
|
|
return new_pw;
|
|
|
|
}
|
|
|
|
|
2017-12-08 16:21:12 +01:00
|
|
|
#define tor_passwd_free(pw) \
|
|
|
|
FREE_AND_NULL(struct passwd, tor_passwd_free_, (pw))
|
|
|
|
|
2014-05-15 04:49:38 +02:00
|
|
|
/** Helper: free one of our cached 'struct passwd' values. */
|
2014-05-14 19:50:43 +02:00
|
|
|
static void
|
2017-12-08 16:21:12 +01:00
|
|
|
tor_passwd_free_(struct passwd *pw)
|
2014-05-14 19:50:43 +02:00
|
|
|
{
|
|
|
|
if (!pw)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tor_free(pw->pw_name);
|
|
|
|
tor_free(pw->pw_dir);
|
|
|
|
tor_free(pw);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Wrapper around getpwnam() that caches result. Used so that we don't need
|
2014-05-15 04:49:38 +02:00
|
|
|
* to give the sandbox access to /etc/passwd.
|
|
|
|
*
|
|
|
|
* The following fields alone will definitely be copied in the output: pw_uid,
|
|
|
|
* pw_gid, pw_name, pw_dir. Other fields are not present in cached values.
|
|
|
|
*
|
|
|
|
* When called with a NULL argument, this function clears storage associated
|
|
|
|
* with static variables it uses.
|
|
|
|
**/
|
2014-05-14 19:50:43 +02:00
|
|
|
const struct passwd *
|
|
|
|
tor_getpwnam(const char *username)
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
if (username == NULL) {
|
|
|
|
tor_passwd_free(passwd_cached);
|
|
|
|
passwd_cached = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pw = getpwnam(username))) {
|
|
|
|
tor_passwd_free(passwd_cached);
|
|
|
|
passwd_cached = tor_passwd_dup(pw);
|
2015-02-19 18:30:34 +01:00
|
|
|
log_info(LD_GENERAL, "Caching new entry %s for %s",
|
|
|
|
passwd_cached->pw_name, username);
|
2014-05-14 19:50:43 +02:00
|
|
|
return pw;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup failed */
|
|
|
|
if (! passwd_cached || ! passwd_cached->pw_name)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (! strcmp(username, passwd_cached->pw_name))
|
2016-06-20 17:03:13 +02:00
|
|
|
return passwd_cached; // LCOV_EXCL_LINE - would need to make getpwnam flaky
|
2014-05-14 19:50:43 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Wrapper around getpwnam() that can use cached result from
|
|
|
|
* tor_getpwnam(). Used so that we don't need to give the sandbox access to
|
2014-05-15 04:49:38 +02:00
|
|
|
* /etc/passwd.
|
|
|
|
*
|
|
|
|
* The following fields alone will definitely be copied in the output: pw_uid,
|
|
|
|
* pw_gid, pw_name, pw_dir. Other fields are not present in cached values.
|
|
|
|
*/
|
2014-05-14 19:50:43 +02:00
|
|
|
const struct passwd *
|
|
|
|
tor_getpwuid(uid_t uid)
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
if ((pw = getpwuid(uid))) {
|
|
|
|
return pw;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup failed */
|
|
|
|
if (! passwd_cached)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (uid == passwd_cached->pw_uid)
|
2016-06-20 17:03:13 +02:00
|
|
|
return passwd_cached; // LCOV_EXCL_LINE - would need to make getpwnam flaky
|
2014-05-14 19:50:43 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(_WIN32) */
|
2014-05-14 19:50:43 +02:00
|
|
|
|
2015-11-06 19:12:44 +01:00
|
|
|
/** Return true iff we were compiled with capability support, and capabilities
|
|
|
|
* seem to work. **/
|
|
|
|
int
|
|
|
|
have_capability_support(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LINUX_CAPABILITIES
|
|
|
|
cap_t caps = cap_get_proc();
|
|
|
|
if (caps == NULL)
|
|
|
|
return 0;
|
|
|
|
cap_free(caps);
|
|
|
|
return 1;
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(HAVE_LINUX_CAPABILITIES)) */
|
2015-11-06 19:12:44 +01:00
|
|
|
return 0;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_LINUX_CAPABILITIES) */
|
2015-11-06 19:12:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_LINUX_CAPABILITIES
|
|
|
|
/** Helper. Drop all capabilities but a small set, and set PR_KEEPCAPS as
|
|
|
|
* appropriate.
|
|
|
|
*
|
|
|
|
* If pre_setuid, retain only CAP_NET_BIND_SERVICE, CAP_SETUID, and
|
|
|
|
* CAP_SETGID, and use PR_KEEPCAPS to ensure that capabilities persist across
|
|
|
|
* setuid().
|
|
|
|
*
|
|
|
|
* If not pre_setuid, retain only CAP_NET_BIND_SERVICE, and disable
|
|
|
|
* PR_KEEPCAPS.
|
|
|
|
*
|
|
|
|
* Return 0 on success, and -1 on failure.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
drop_capabilities(int pre_setuid)
|
|
|
|
{
|
|
|
|
/* We keep these three capabilities, and these only, as we setuid.
|
|
|
|
* After we setuid, we drop all but the first. */
|
|
|
|
const cap_value_t caplist[] = {
|
|
|
|
CAP_NET_BIND_SERVICE, CAP_SETUID, CAP_SETGID
|
|
|
|
};
|
|
|
|
const char *where = pre_setuid ? "pre-setuid" : "post-setuid";
|
|
|
|
const int n_effective = pre_setuid ? 3 : 1;
|
|
|
|
const int n_permitted = pre_setuid ? 3 : 1;
|
|
|
|
const int n_inheritable = 1;
|
|
|
|
const int keepcaps = pre_setuid ? 1 : 0;
|
|
|
|
|
|
|
|
/* Sets whether we keep capabilities across a setuid. */
|
|
|
|
if (prctl(PR_SET_KEEPCAPS, keepcaps) < 0) {
|
|
|
|
log_warn(LD_CONFIG, "Unable to call prctl() %s: %s",
|
|
|
|
where, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cap_t caps = cap_get_proc();
|
|
|
|
if (!caps) {
|
|
|
|
log_warn(LD_CONFIG, "Unable to call cap_get_proc() %s: %s",
|
|
|
|
where, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cap_clear(caps);
|
|
|
|
|
|
|
|
cap_set_flag(caps, CAP_EFFECTIVE, n_effective, caplist, CAP_SET);
|
|
|
|
cap_set_flag(caps, CAP_PERMITTED, n_permitted, caplist, CAP_SET);
|
|
|
|
cap_set_flag(caps, CAP_INHERITABLE, n_inheritable, caplist, CAP_SET);
|
|
|
|
|
|
|
|
int r = cap_set_proc(caps);
|
|
|
|
cap_free(caps);
|
|
|
|
if (r < 0) {
|
|
|
|
log_warn(LD_CONFIG, "No permission to set capabilities %s: %s",
|
|
|
|
where, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_LINUX_CAPABILITIES) */
|
2015-11-06 19:12:44 +01:00
|
|
|
|
2008-11-07 05:11:03 +01:00
|
|
|
/** Call setuid and setgid to run as <b>user</b> and switch to their
|
2008-11-07 03:06:12 +01:00
|
|
|
* primary group. Return 0 on success. On failure, log and return -1.
|
2015-11-06 19:12:44 +01:00
|
|
|
*
|
2015-11-26 17:03:35 +01:00
|
|
|
* If SWITCH_ID_KEEP_BINDLOW is set in 'flags', try to use the capability
|
2015-11-06 19:12:44 +01:00
|
|
|
* system to retain the abilitity to bind low ports.
|
2015-11-26 17:03:35 +01:00
|
|
|
*
|
|
|
|
* If SWITCH_ID_WARN_IF_NO_CAPS is set in flags, also warn if we have
|
|
|
|
* don't have capability support.
|
2008-11-07 03:06:12 +01:00
|
|
|
*/
|
|
|
|
int
|
2015-11-06 19:12:44 +01:00
|
|
|
switch_id(const char *user, const unsigned flags)
|
2005-09-30 03:09:52 +02:00
|
|
|
{
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifndef _WIN32
|
2014-05-14 19:53:14 +02:00
|
|
|
const struct passwd *pw = NULL;
|
2008-11-07 03:06:12 +01:00
|
|
|
uid_t old_uid;
|
|
|
|
gid_t old_gid;
|
2008-11-07 05:34:47 +01:00
|
|
|
static int have_already_switched_id = 0;
|
2015-11-06 19:12:44 +01:00
|
|
|
const int keep_bindlow = !!(flags & SWITCH_ID_KEEP_BINDLOW);
|
2015-11-26 17:03:35 +01:00
|
|
|
const int warn_if_no_caps = !!(flags & SWITCH_ID_WARN_IF_NO_CAPS);
|
2008-11-07 03:53:46 +01:00
|
|
|
|
2008-11-07 03:06:12 +01:00
|
|
|
tor_assert(user);
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2008-11-07 05:34:47 +01:00
|
|
|
if (have_already_switched_id)
|
|
|
|
return 0;
|
|
|
|
|
2008-11-07 03:53:46 +01:00
|
|
|
/* Log the initial credential state */
|
2008-11-07 05:11:03 +01:00
|
|
|
if (log_credential_status())
|
|
|
|
return -1;
|
|
|
|
|
2008-11-07 03:06:12 +01:00
|
|
|
log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL, "Changing user and groups");
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2008-11-07 03:06:12 +01:00
|
|
|
/* Get old UID/GID to check if we changed correctly */
|
|
|
|
old_uid = getuid();
|
|
|
|
old_gid = getgid();
|
|
|
|
|
|
|
|
/* Lookup the user and group information, if we have a problem, bail out. */
|
2014-05-14 19:53:14 +02:00
|
|
|
pw = tor_getpwnam(user);
|
2008-11-07 05:11:03 +01:00
|
|
|
if (pw == NULL) {
|
|
|
|
log_warn(LD_CONFIG, "Error setting configured user: %s not found", user);
|
2008-11-07 03:06:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2015-11-06 19:12:44 +01:00
|
|
|
#ifdef HAVE_LINUX_CAPABILITIES
|
2015-11-26 17:03:35 +01:00
|
|
|
(void) warn_if_no_caps;
|
2015-11-06 19:12:44 +01:00
|
|
|
if (keep_bindlow) {
|
|
|
|
if (drop_capabilities(1))
|
|
|
|
return -1;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(HAVE_LINUX_CAPABILITIES)) */
|
2015-11-26 17:03:35 +01:00
|
|
|
(void) keep_bindlow;
|
|
|
|
if (warn_if_no_caps) {
|
|
|
|
log_warn(LD_CONFIG, "KeepBindCapabilities set, but no capability support "
|
|
|
|
"on this system.");
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_LINUX_CAPABILITIES) */
|
2015-11-06 19:12:44 +01:00
|
|
|
|
2008-11-07 03:06:12 +01:00
|
|
|
/* Properly switch egid,gid,euid,uid here or bail out */
|
|
|
|
if (setgroups(1, &pw->pw_gid)) {
|
2008-12-10 00:26:12 +01:00
|
|
|
log_warn(LD_GENERAL, "Error setting groups to gid %d: \"%s\".",
|
2008-11-09 17:54:54 +01:00
|
|
|
(int)pw->pw_gid, strerror(errno));
|
2008-12-10 00:26:12 +01:00
|
|
|
if (old_uid == pw->pw_uid) {
|
|
|
|
log_warn(LD_GENERAL, "Tor is already running as %s. You do not need "
|
|
|
|
"the \"User\" option if you are already running as the user "
|
|
|
|
"you want to be. (If you did not set the User option in your "
|
|
|
|
"torrc, check whether it was specified on the command line "
|
|
|
|
"by a startup script.)", user);
|
|
|
|
} else {
|
|
|
|
log_warn(LD_GENERAL, "If you set the \"User\" option, you must start Tor"
|
|
|
|
" as root.");
|
|
|
|
}
|
2008-11-07 03:06:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setegid(pw->pw_gid)) {
|
2008-11-09 17:54:54 +01:00
|
|
|
log_warn(LD_GENERAL, "Error setting egid to %d: %s",
|
|
|
|
(int)pw->pw_gid, strerror(errno));
|
2008-11-07 03:06:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setgid(pw->pw_gid)) {
|
2008-11-09 17:54:54 +01:00
|
|
|
log_warn(LD_GENERAL, "Error setting gid to %d: %s",
|
|
|
|
(int)pw->pw_gid, strerror(errno));
|
2008-11-07 03:06:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setuid(pw->pw_uid)) {
|
2008-11-09 17:54:54 +01:00
|
|
|
log_warn(LD_GENERAL, "Error setting configured uid to %s (%d): %s",
|
|
|
|
user, (int)pw->pw_uid, strerror(errno));
|
2008-11-07 03:06:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-11-07 03:53:46 +01:00
|
|
|
if (seteuid(pw->pw_uid)) {
|
2008-11-09 17:54:54 +01:00
|
|
|
log_warn(LD_GENERAL, "Error setting configured euid to %s (%d): %s",
|
|
|
|
user, (int)pw->pw_uid, strerror(errno));
|
2008-11-07 03:06:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is how OpenBSD rolls:
|
|
|
|
if (setgroups(1, &pw->pw_gid) || setegid(pw->pw_gid) ||
|
|
|
|
setgid(pw->pw_gid) || setuid(pw->pw_uid) || seteuid(pw->pw_uid)) {
|
|
|
|
setgid(pw->pw_gid) || seteuid(pw->pw_uid) || setuid(pw->pw_uid)) {
|
|
|
|
log_warn(LD_GENERAL, "Error setting configured UID/GID: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* We've properly switched egid, gid, euid, uid, and supplementary groups if
|
|
|
|
* we're here. */
|
2015-11-06 19:12:44 +01:00
|
|
|
#ifdef HAVE_LINUX_CAPABILITIES
|
|
|
|
if (keep_bindlow) {
|
|
|
|
if (drop_capabilities(0))
|
|
|
|
return -1;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_LINUX_CAPABILITIES) */
|
2008-11-07 03:06:12 +01:00
|
|
|
|
|
|
|
#if !defined(CYGWIN) && !defined(__CYGWIN__)
|
|
|
|
/* If we tried to drop privilege to a group/user other than root, attempt to
|
|
|
|
* restore root (E)(U|G)ID, and abort if the operation succeeds */
|
|
|
|
|
|
|
|
/* Only check for privilege dropping if we were asked to be non-root */
|
|
|
|
if (pw->pw_uid) {
|
|
|
|
/* Try changing GID/EGID */
|
2008-11-07 03:53:46 +01:00
|
|
|
if (pw->pw_gid != old_gid &&
|
|
|
|
(setgid(old_gid) != -1 || setegid(old_gid) != -1)) {
|
2008-11-09 17:54:54 +01:00
|
|
|
log_warn(LD_GENERAL, "Was able to restore group credentials even after "
|
|
|
|
"switching GID: this means that the setgid code didn't work.");
|
2004-11-01 21:41:47 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2008-11-07 03:53:46 +01:00
|
|
|
|
2008-11-07 03:06:12 +01:00
|
|
|
/* Try changing UID/EUID */
|
2008-11-07 03:53:46 +01:00
|
|
|
if (pw->pw_uid != old_uid &&
|
|
|
|
(setuid(old_uid) != -1 || seteuid(old_uid) != -1)) {
|
2008-11-09 17:54:54 +01:00
|
|
|
log_warn(LD_GENERAL, "Was able to restore user credentials even after "
|
|
|
|
"switching UID: this means that the setuid code didn't work.");
|
2004-11-01 21:41:47 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(CYGWIN) && !defined(__CYGWIN__) */
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2008-11-07 03:06:12 +01:00
|
|
|
/* Check what really happened */
|
2008-11-07 05:11:03 +01:00
|
|
|
if (log_credential_status()) {
|
|
|
|
return -1;
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
|
|
|
|
2008-11-07 05:34:47 +01:00
|
|
|
have_already_switched_id = 1; /* mark success so we never try again */
|
2009-02-09 16:20:17 +01:00
|
|
|
|
2017-09-15 22:15:27 +02:00
|
|
|
#if defined(__linux__) && defined(HAVE_SYS_PRCTL_H) && \
|
|
|
|
defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
|
2009-02-09 16:20:17 +01:00
|
|
|
if (pw->pw_uid) {
|
|
|
|
/* Re-enable core dumps if we're not running as root. */
|
|
|
|
log_info(LD_CONFIG, "Re-enabling coredumps");
|
|
|
|
if (prctl(PR_SET_DUMPABLE, 1)) {
|
|
|
|
log_warn(LD_CONFIG, "Unable to re-enable coredumps: %s",strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(__linux__) && defined(HAVE_SYS_PRCTL_H) && ... */
|
2004-11-01 21:41:47 +01:00
|
|
|
return 0;
|
2008-11-07 03:06:12 +01:00
|
|
|
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(!defined(_WIN32)) */
|
2007-02-28 21:24:27 +01:00
|
|
|
(void)user;
|
2015-11-06 19:12:44 +01:00
|
|
|
(void)flags;
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2015-12-16 23:11:38 +01:00
|
|
|
log_warn(LD_CONFIG, "Switching users is unsupported on your OS.");
|
2004-11-01 21:41:47 +01:00
|
|
|
return -1;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(_WIN32) */
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
|
|
|
|
2011-11-25 05:45:47 +01:00
|
|
|
/* We only use the linux prctl for now. There is no Win32 support; this may
|
|
|
|
* also work on various BSD systems and Mac OS X - send testing feedback!
|
|
|
|
*
|
|
|
|
* On recent Gnu/Linux kernels it is possible to create a system-wide policy
|
|
|
|
* that will prevent non-root processes from attaching to other processes
|
|
|
|
* unless they are the parent process; thus gdb can attach to programs that
|
|
|
|
* they execute but they cannot attach to other processes running as the same
|
|
|
|
* user. The system wide policy may be set with the sysctl
|
|
|
|
* kernel.yama.ptrace_scope or by inspecting
|
|
|
|
* /proc/sys/kernel/yama/ptrace_scope and it is 1 by default on Ubuntu 11.04.
|
|
|
|
*
|
|
|
|
* This ptrace scope will be ignored on Gnu/Linux for users with
|
|
|
|
* CAP_SYS_PTRACE and so it is very likely that root will still be able to
|
|
|
|
* attach to the Tor process.
|
|
|
|
*/
|
2011-12-08 09:19:09 +01:00
|
|
|
/** Attempt to disable debugger attachment: return 1 on success, -1 on
|
|
|
|
* failure, and 0 if we don't know how to try on this platform. */
|
2011-11-25 05:45:47 +01:00
|
|
|
int
|
|
|
|
tor_disable_debugger_attach(void)
|
|
|
|
{
|
2017-09-12 23:46:09 +02:00
|
|
|
int r = -1;
|
2011-11-25 05:45:47 +01:00
|
|
|
log_debug(LD_CONFIG,
|
|
|
|
"Attemping to disable debugger attachment to Tor for "
|
|
|
|
"unprivileged users.");
|
2017-09-15 22:15:27 +02:00
|
|
|
#if defined(__linux__) && defined(HAVE_SYS_PRCTL_H) \
|
|
|
|
&& defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
|
2017-09-12 23:46:09 +02:00
|
|
|
#define TRIED_TO_DISABLE
|
2011-11-25 05:45:47 +01:00
|
|
|
r = prctl(PR_SET_DUMPABLE, 0);
|
2017-09-12 23:46:09 +02:00
|
|
|
#elif defined(__APPLE__) && defined(PT_DENY_ATTACH)
|
2017-09-15 22:40:11 +02:00
|
|
|
#define TRIED_TO_ATTACH
|
2017-09-12 23:46:09 +02:00
|
|
|
r = ptrace(PT_DENY_ATTACH, 0, 0, 0);
|
2017-09-15 22:40:11 +02:00
|
|
|
#endif /* defined(__linux__) && defined(HAVE_SYS_PRCTL_H) ... || ... */
|
2011-11-25 05:45:47 +01:00
|
|
|
|
|
|
|
// XXX: TODO - Mac OS X has dtrace and this may be disabled.
|
|
|
|
// XXX: TODO - Windows probably has something similar
|
2017-09-12 23:46:09 +02:00
|
|
|
#ifdef TRIED_TO_DISABLE
|
|
|
|
if (r == 0) {
|
2011-11-25 05:45:47 +01:00
|
|
|
log_debug(LD_CONFIG,"Debugger attachment disabled for "
|
|
|
|
"unprivileged users.");
|
2011-12-08 09:19:09 +01:00
|
|
|
return 1;
|
2017-09-12 23:46:09 +02:00
|
|
|
} else {
|
2011-12-08 09:19:09 +01:00
|
|
|
log_warn(LD_CONFIG, "Unable to disable debugger attaching: %s",
|
2011-11-25 05:45:47 +01:00
|
|
|
strerror(errno));
|
|
|
|
}
|
2017-09-15 22:40:11 +02:00
|
|
|
#endif /* defined(TRIED_TO_DISABLE) */
|
2017-09-12 23:46:09 +02:00
|
|
|
#undef TRIED_TO_DISABLE
|
2011-11-25 05:45:47 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2004-11-10 15:23:31 +01:00
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
/** Allocate and return a string containing the home directory for the
|
2007-06-01 01:57:46 +02:00
|
|
|
* user <b>username</b>. Only works on posix-like systems. */
|
2004-11-10 15:23:31 +01:00
|
|
|
char *
|
|
|
|
get_user_homedir(const char *username)
|
|
|
|
{
|
2014-05-14 19:53:14 +02:00
|
|
|
const struct passwd *pw;
|
2004-11-10 15:23:31 +01:00
|
|
|
tor_assert(username);
|
|
|
|
|
2014-05-14 19:53:14 +02:00
|
|
|
if (!(pw = tor_getpwnam(username))) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_err(LD_CONFIG,"User \"%s\" not found.", username);
|
2004-11-10 15:23:31 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return tor_strdup(pw->pw_dir);
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_PWD_H) */
|
2004-11-10 15:23:31 +01:00
|
|
|
|
2012-05-24 18:56:31 +02:00
|
|
|
/** Modify <b>fname</b> to contain the name of its parent directory. Doesn't
|
|
|
|
* actually examine the filesystem; does a purely syntactic modification.
|
|
|
|
*
|
|
|
|
* The parent of the root director is considered to be iteself.
|
2013-07-15 10:46:11 +02:00
|
|
|
*
|
|
|
|
* Path separators are the forward slash (/) everywhere and additionally
|
|
|
|
* the backslash (\) on Win32.
|
|
|
|
*
|
|
|
|
* Cuts off any number of trailing path separators but otherwise ignores
|
|
|
|
* them for purposes of finding the parent directory.
|
|
|
|
*
|
|
|
|
* Returns 0 if a parent directory was successfully found, -1 otherwise (fname
|
2013-07-15 15:08:35 +02:00
|
|
|
* did not have any path separators or only had them at the end).
|
2012-05-24 18:56:31 +02:00
|
|
|
* */
|
2011-05-13 21:15:41 +02:00
|
|
|
int
|
|
|
|
get_parent_directory(char *fname)
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
int at_end = 1;
|
|
|
|
tor_assert(fname);
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2011-05-13 21:15:41 +02:00
|
|
|
/* If we start with, say, c:, then don't consider that the start of the path
|
|
|
|
*/
|
|
|
|
if (fname[0] && fname[1] == ':') {
|
|
|
|
fname += 2;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) */
|
2011-05-15 19:03:15 +02:00
|
|
|
/* Now we want to remove all path-separators at the end of the string,
|
|
|
|
* and to remove the end of the string starting with the path separator
|
|
|
|
* before the last non-path-separator. In perl, this would be
|
|
|
|
* s#[/]*$##; s#/[^/]*$##;
|
|
|
|
* on a unixy platform.
|
|
|
|
*/
|
2011-05-13 21:15:41 +02:00
|
|
|
cp = fname + strlen(fname);
|
|
|
|
at_end = 1;
|
2012-05-24 18:56:31 +02:00
|
|
|
while (--cp >= fname) {
|
2011-05-13 21:15:41 +02:00
|
|
|
int is_sep = (*cp == '/'
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2011-05-13 21:15:41 +02:00
|
|
|
|| *cp == '\\'
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
if (is_sep) {
|
2012-05-24 18:56:31 +02:00
|
|
|
if (cp == fname) {
|
|
|
|
/* This is the first separator in the file name; don't remove it! */
|
|
|
|
cp[1] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
2011-05-13 21:15:41 +02:00
|
|
|
*cp = '\0';
|
|
|
|
if (! at_end)
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
at_end = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-05-10 20:20:15 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
/** Return a newly allocated string containing the output of getcwd(). Return
|
|
|
|
* NULL on failure. (We can't just use getcwd() into a PATH_MAX buffer, since
|
|
|
|
* Hurd hasn't got a PATH_MAX.)
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
alloc_getcwd(void)
|
|
|
|
{
|
2017-08-03 21:45:46 +02:00
|
|
|
#ifdef HAVE_GET_CURRENT_DIR_NAME
|
2017-08-04 18:26:35 +02:00
|
|
|
/* Glibc makes this nice and simple for us. */
|
|
|
|
char *cwd = get_current_dir_name();
|
|
|
|
char *result = NULL;
|
|
|
|
if (cwd) {
|
|
|
|
/* We make a copy here, in case tor_malloc() is not malloc(). */
|
|
|
|
result = tor_strdup(cwd);
|
|
|
|
raw_free(cwd); // alias for free to avoid tripping check-spaces.
|
|
|
|
}
|
|
|
|
return result;
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(HAVE_GET_CURRENT_DIR_NAME)) */
|
2017-08-03 21:45:46 +02:00
|
|
|
size_t size = 1024;
|
|
|
|
char *buf = NULL;
|
|
|
|
char *ptr = NULL;
|
|
|
|
|
|
|
|
while (ptr == NULL) {
|
|
|
|
buf = tor_realloc(buf, size);
|
|
|
|
ptr = getcwd(buf, size);
|
2016-06-20 16:46:53 +02:00
|
|
|
|
2017-08-03 21:45:46 +02:00
|
|
|
if (ptr == NULL && errno != ERANGE) {
|
|
|
|
tor_free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size *= 2;
|
|
|
|
}
|
|
|
|
return buf;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_GET_CURRENT_DIR_NAME) */
|
2012-05-10 20:20:15 +02:00
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(_WIN32) */
|
2012-05-10 20:20:15 +02:00
|
|
|
|
2011-12-04 17:33:20 +01:00
|
|
|
/** Expand possibly relative path <b>fname</b> to an absolute path.
|
|
|
|
* Return a newly allocated string, possibly equal to <b>fname</b>. */
|
|
|
|
char *
|
|
|
|
make_path_absolute(char *fname)
|
|
|
|
{
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2011-12-04 17:33:20 +01:00
|
|
|
char *absfname_malloced = _fullpath(NULL, fname, 1);
|
|
|
|
|
|
|
|
/* We don't want to assume that tor_free can free a string allocated
|
|
|
|
* with malloc. On failure, return fname (it's better than nothing). */
|
|
|
|
char *absfname = tor_strdup(absfname_malloced ? absfname_malloced : fname);
|
checkSpace.pl now forbids more identifiers.
The functions it warns about are:
assert, memcmp, strcat, strcpy, sprintf, malloc, free, realloc,
strdup, strndup, calloc.
Also, fix a few lingering instances of these in the code. Use other
conventions to indicate _intended_ use of assert and
malloc/realloc/etc.
2016-09-06 18:35:37 +02:00
|
|
|
if (absfname_malloced) raw_free(absfname_malloced);
|
2011-12-04 17:33:20 +01:00
|
|
|
|
|
|
|
return absfname;
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(_WIN32)) */
|
2012-05-10 20:20:15 +02:00
|
|
|
char *absfname = NULL, *path = NULL;
|
2011-12-04 17:33:20 +01:00
|
|
|
|
|
|
|
tor_assert(fname);
|
|
|
|
|
2011-12-30 18:06:41 +01:00
|
|
|
if (fname[0] == '/') {
|
2011-12-04 17:33:20 +01:00
|
|
|
absfname = tor_strdup(fname);
|
|
|
|
} else {
|
2012-05-10 20:20:15 +02:00
|
|
|
path = alloc_getcwd();
|
|
|
|
if (path) {
|
2011-12-04 17:33:20 +01:00
|
|
|
tor_asprintf(&absfname, "%s/%s", path, fname);
|
2012-05-10 20:20:15 +02:00
|
|
|
tor_free(path);
|
2011-12-04 17:33:20 +01:00
|
|
|
} else {
|
2016-06-20 16:46:53 +02:00
|
|
|
/* LCOV_EXCL_START Can't make getcwd fail. */
|
2011-12-04 17:33:20 +01:00
|
|
|
/* If getcwd failed, the best we can do here is keep using the
|
|
|
|
* relative path. (Perhaps / isn't readable by this UID/GID.) */
|
2012-05-10 20:20:15 +02:00
|
|
|
log_warn(LD_GENERAL, "Unable to find current working directory: %s",
|
|
|
|
strerror(errno));
|
2011-12-04 17:33:20 +01:00
|
|
|
absfname = tor_strdup(fname);
|
2016-06-20 16:46:53 +02:00
|
|
|
/* LCOV_EXCL_STOP */
|
2011-12-04 17:33:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return absfname;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) */
|
2011-12-04 17:33:20 +01:00
|
|
|
}
|
|
|
|
|
2016-12-16 18:16:52 +01:00
|
|
|
#ifndef HAVE__NSGETENVIRON
|
|
|
|
#ifndef HAVE_EXTERN_ENVIRON_DECLARED
|
|
|
|
/* Some platforms declare environ under some circumstances, others don't. */
|
|
|
|
#ifndef RUNNING_DOXYGEN
|
|
|
|
extern char **environ;
|
|
|
|
#endif
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(HAVE_EXTERN_ENVIRON_DECLARED) */
|
|
|
|
#endif /* !defined(HAVE__NSGETENVIRON) */
|
2016-12-16 18:16:52 +01:00
|
|
|
|
2012-02-14 12:21:03 +01:00
|
|
|
/** Return the current environment. This is a portable replacement for
|
|
|
|
* 'environ'. */
|
|
|
|
char **
|
|
|
|
get_environment(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE__NSGETENVIRON
|
|
|
|
/* This is for compatibility between OSX versions. Otherwise (for example)
|
|
|
|
* when we do a mostly-static build on OSX 10.7, the resulting binary won't
|
|
|
|
* work on OSX 10.6. */
|
|
|
|
return *_NSGetEnviron();
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(HAVE__NSGETENVIRON)) */
|
2012-02-14 12:21:03 +01:00
|
|
|
return environ;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE__NSGETENVIRON) */
|
2012-02-14 12:21:03 +01:00
|
|
|
}
|
|
|
|
|
2014-04-28 22:20:58 +02:00
|
|
|
/** Get name of current host and write it to <b>name</b> array, whose
|
|
|
|
* length is specified by <b>namelen</b> argument. Return 0 upon
|
2018-01-24 09:55:15 +01:00
|
|
|
* successful completion; otherwise return return -1. (Currently,
|
2014-04-28 22:20:58 +02:00
|
|
|
* this function is merely a mockable wrapper for POSIX gethostname().)
|
|
|
|
*/
|
|
|
|
MOCK_IMPL(int,
|
|
|
|
tor_gethostname,(char *name, size_t namelen))
|
|
|
|
{
|
|
|
|
return gethostname(name,namelen);
|
|
|
|
}
|
|
|
|
|
2014-04-26 15:53:28 +02:00
|
|
|
/** Set *addr to the IP address (in dotted-quad notation) stored in *str.
|
2014-04-28 22:20:58 +02:00
|
|
|
* Return 1 on success, 0 if *str is badly formatted.
|
|
|
|
* (Like inet_aton(str,addr), but works on Windows and Solaris.)
|
2004-11-01 21:41:47 +01:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2009-01-04 20:47:17 +01:00
|
|
|
tor_inet_aton(const char *str, struct in_addr* addr)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
2009-03-03 19:02:36 +01:00
|
|
|
unsigned a,b,c,d;
|
2009-01-04 20:47:17 +01:00
|
|
|
char more;
|
2009-03-03 19:02:36 +01:00
|
|
|
if (tor_sscanf(str, "%3u.%3u.%3u.%3u%c", &a,&b,&c,&d,&more) != 4)
|
2009-01-04 20:47:17 +01:00
|
|
|
return 0;
|
2009-03-03 19:02:36 +01:00
|
|
|
if (a > 255) return 0;
|
|
|
|
if (b > 255) return 0;
|
|
|
|
if (c > 255) return 0;
|
|
|
|
if (d > 255) return 0;
|
2009-01-04 20:47:17 +01:00
|
|
|
addr->s_addr = htonl((a<<24) | (b<<16) | (c<<8) | d);
|
|
|
|
return 1;
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Given <b>af</b>==AF_INET and <b>src</b> a struct in_addr, or
|
|
|
|
* <b>af</b>==AF_INET6 and <b>src</b> a struct in6_addr, try to format the
|
|
|
|
* address and store it in the <b>len</b>-byte buffer <b>dst</b>. Returns
|
|
|
|
* <b>dst</b> on success, NULL on failure.
|
|
|
|
*
|
|
|
|
* (Like inet_ntop(af,src,dst,len), but works on platforms that don't have it:
|
|
|
|
* Tor sometimes needs to format ipv6 addresses even on platforms without ipv6
|
|
|
|
* support.) */
|
2007-05-25 20:22:37 +02:00
|
|
|
const char *
|
|
|
|
tor_inet_ntop(int af, const void *src, char *dst, size_t len)
|
|
|
|
{
|
|
|
|
if (af == AF_INET) {
|
|
|
|
if (tor_inet_ntoa(src, dst, len) < 0)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return dst;
|
|
|
|
} else if (af == AF_INET6) {
|
|
|
|
const struct in6_addr *addr = src;
|
|
|
|
char buf[64], *cp;
|
|
|
|
int longestGapLen = 0, longestGapPos = -1, i,
|
|
|
|
curGapPos = -1, curGapLen = 0;
|
|
|
|
uint16_t words[8];
|
|
|
|
for (i = 0; i < 8; ++i) {
|
|
|
|
words[i] = (((uint16_t)addr->s6_addr[2*i])<<8) + addr->s6_addr[2*i+1];
|
|
|
|
}
|
|
|
|
if (words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 &&
|
2007-07-20 20:30:47 +02:00
|
|
|
words[4] == 0 && ((words[5] == 0 && words[6] && words[7]) ||
|
2007-07-20 18:25:27 +02:00
|
|
|
(words[5] == 0xffff))) {
|
2007-05-25 20:22:37 +02:00
|
|
|
/* This is an IPv4 address. */
|
2007-05-31 20:48:25 +02:00
|
|
|
if (words[5] == 0) {
|
|
|
|
tor_snprintf(buf, sizeof(buf), "::%d.%d.%d.%d",
|
|
|
|
addr->s6_addr[12], addr->s6_addr[13],
|
|
|
|
addr->s6_addr[14], addr->s6_addr[15]);
|
|
|
|
} else {
|
|
|
|
tor_snprintf(buf, sizeof(buf), "::%x:%d.%d.%d.%d", words[5],
|
|
|
|
addr->s6_addr[12], addr->s6_addr[13],
|
|
|
|
addr->s6_addr[14], addr->s6_addr[15]);
|
|
|
|
}
|
2011-11-11 07:47:00 +01:00
|
|
|
if ((strlen(buf) + 1) > len) /* +1 for \0 */
|
2007-05-25 20:22:37 +02:00
|
|
|
return NULL;
|
|
|
|
strlcpy(dst, buf, len);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
while (i < 8) {
|
|
|
|
if (words[i] == 0) {
|
|
|
|
curGapPos = i++;
|
|
|
|
curGapLen = 1;
|
|
|
|
while (i<8 && words[i] == 0) {
|
|
|
|
++i; ++curGapLen;
|
|
|
|
}
|
|
|
|
if (curGapLen > longestGapLen) {
|
|
|
|
longestGapPos = curGapPos;
|
|
|
|
longestGapLen = curGapLen;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2007-05-31 20:48:25 +02:00
|
|
|
if (longestGapLen<=1)
|
|
|
|
longestGapPos = -1;
|
|
|
|
|
2007-05-25 20:22:37 +02:00
|
|
|
cp = buf;
|
|
|
|
for (i = 0; i < 8; ++i) {
|
|
|
|
if (words[i] == 0 && longestGapPos == i) {
|
2007-05-31 20:48:25 +02:00
|
|
|
if (i == 0)
|
|
|
|
*cp++ = ':';
|
2007-05-25 20:22:37 +02:00
|
|
|
*cp++ = ':';
|
|
|
|
while (i < 8 && words[i] == 0)
|
|
|
|
++i;
|
|
|
|
--i; /* to compensate for loop increment. */
|
|
|
|
} else {
|
|
|
|
tor_snprintf(cp, sizeof(buf)-(cp-buf), "%x", (unsigned)words[i]);
|
|
|
|
cp += strlen(cp);
|
|
|
|
if (i != 7)
|
|
|
|
*cp++ = ':';
|
|
|
|
}
|
|
|
|
}
|
2007-05-31 20:48:25 +02:00
|
|
|
*cp = '\0';
|
2011-11-11 07:47:00 +01:00
|
|
|
if ((strlen(buf) + 1) > len) /* +1 for \0 */
|
2007-05-25 20:22:37 +02:00
|
|
|
return NULL;
|
|
|
|
strlcpy(dst, buf, len);
|
|
|
|
return dst;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Given <b>af</b>==AF_INET or <b>af</b>==AF_INET6, and a string <b>src</b>
|
|
|
|
* encoding an IPv4 address or IPv6 address correspondingly, try to parse the
|
|
|
|
* address and store the result in <b>dst</b> (which must have space for a
|
|
|
|
* struct in_addr or a struct in6_addr, as appropriate). Return 1 on success,
|
|
|
|
* 0 on a bad parse, and -1 on a bad <b>af</b>.
|
|
|
|
*
|
|
|
|
* (Like inet_pton(af,src,dst) but works on platforms that don't have it: Tor
|
|
|
|
* sometimes needs to format ipv6 addresses even on platforms without ipv6
|
|
|
|
* support.) */
|
2007-05-25 20:22:37 +02:00
|
|
|
int
|
|
|
|
tor_inet_pton(int af, const char *src, void *dst)
|
|
|
|
{
|
|
|
|
if (af == AF_INET) {
|
|
|
|
return tor_inet_aton(src, dst);
|
|
|
|
} else if (af == AF_INET6) {
|
|
|
|
struct in6_addr *out = dst;
|
|
|
|
uint16_t words[8];
|
|
|
|
int gapPos = -1, i, setWords=0;
|
|
|
|
const char *dot = strchr(src, '.');
|
|
|
|
const char *eow; /* end of words. */
|
2017-09-13 03:09:18 +02:00
|
|
|
memset(words, 0xf8, sizeof(words));
|
2007-05-25 20:22:37 +02:00
|
|
|
if (dot == src)
|
|
|
|
return 0;
|
|
|
|
else if (!dot)
|
|
|
|
eow = src+strlen(src);
|
|
|
|
else {
|
2009-03-03 19:02:36 +01:00
|
|
|
unsigned byte1,byte2,byte3,byte4;
|
2007-05-31 20:48:25 +02:00
|
|
|
char more;
|
2014-04-19 19:07:30 +02:00
|
|
|
for (eow = dot-1; eow > src && TOR_ISDIGIT(*eow); --eow)
|
2007-05-25 20:22:37 +02:00
|
|
|
;
|
2014-04-19 19:07:30 +02:00
|
|
|
if (*eow != ':')
|
|
|
|
return 0;
|
2007-05-25 20:22:37 +02:00
|
|
|
++eow;
|
|
|
|
|
2007-05-31 20:48:25 +02:00
|
|
|
/* We use "scanf" because some platform inet_aton()s are too lax
|
|
|
|
* about IPv4 addresses of the form "1.2.3" */
|
2009-03-03 19:02:36 +01:00
|
|
|
if (tor_sscanf(eow, "%3u.%3u.%3u.%3u%c",
|
|
|
|
&byte1,&byte2,&byte3,&byte4,&more) != 4)
|
2007-05-31 20:48:25 +02:00
|
|
|
return 0;
|
|
|
|
|
2009-03-03 19:02:36 +01:00
|
|
|
if (byte1 > 255 || byte2 > 255 || byte3 > 255 || byte4 > 255)
|
2007-05-25 20:22:37 +02:00
|
|
|
return 0;
|
2007-05-31 20:48:25 +02:00
|
|
|
|
|
|
|
words[6] = (byte1<<8) | byte2;
|
|
|
|
words[7] = (byte3<<8) | byte4;
|
2007-05-25 20:22:37 +02:00
|
|
|
setWords += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (src < eow) {
|
|
|
|
if (i > 7)
|
|
|
|
return 0;
|
|
|
|
if (TOR_ISXDIGIT(*src)) {
|
|
|
|
char *next;
|
2011-11-24 09:16:43 +01:00
|
|
|
ssize_t len;
|
2008-02-21 22:57:47 +01:00
|
|
|
long r = strtol(src, &next, 16);
|
2017-07-03 17:20:09 +02:00
|
|
|
if (next == NULL || next == src) {
|
|
|
|
/* The 'next == src' error case can happen on versions of openbsd
|
2017-08-07 06:22:27 +02:00
|
|
|
* which treat "0xfoo" as an error, rather than as "0" followed by
|
2017-07-03 17:20:09 +02:00
|
|
|
* "xfoo". */
|
|
|
|
return 0;
|
|
|
|
}
|
2011-11-18 23:37:54 +01:00
|
|
|
|
|
|
|
len = *next == '\0' ? eow - src : next - src;
|
|
|
|
if (len > 4)
|
2007-05-25 20:22:37 +02:00
|
|
|
return 0;
|
2011-11-18 23:37:54 +01:00
|
|
|
if (len > 1 && !TOR_ISXDIGIT(src[1]))
|
|
|
|
return 0; /* 0x is not valid */
|
2007-05-25 20:22:37 +02:00
|
|
|
|
2011-11-18 23:37:54 +01:00
|
|
|
tor_assert(r >= 0);
|
|
|
|
tor_assert(r < 65536);
|
2007-05-25 20:22:37 +02:00
|
|
|
words[i++] = (uint16_t)r;
|
|
|
|
setWords++;
|
|
|
|
src = next;
|
2007-05-31 20:48:25 +02:00
|
|
|
if (*src != ':' && src != eow)
|
2007-05-25 20:22:37 +02:00
|
|
|
return 0;
|
|
|
|
++src;
|
2011-11-18 23:37:54 +01:00
|
|
|
} else if (*src == ':' && i > 0 && gapPos == -1) {
|
2007-05-25 20:22:37 +02:00
|
|
|
gapPos = i;
|
|
|
|
++src;
|
2011-11-18 23:37:54 +01:00
|
|
|
} else if (*src == ':' && i == 0 && src+1 < eow && src[1] == ':' &&
|
|
|
|
gapPos == -1) {
|
2007-05-25 20:22:37 +02:00
|
|
|
gapPos = i;
|
|
|
|
src += 2;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-20 18:25:27 +02:00
|
|
|
if (setWords > 8 ||
|
|
|
|
(setWords == 8 && gapPos != -1) ||
|
|
|
|
(setWords < 8 && gapPos == -1))
|
2007-05-25 20:22:37 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (gapPos >= 0) {
|
2007-05-31 20:48:25 +02:00
|
|
|
int nToMove = setWords - (dot ? 2 : 0) - gapPos;
|
2007-05-25 20:22:37 +02:00
|
|
|
int gapLen = 8 - setWords;
|
2007-05-31 20:48:25 +02:00
|
|
|
tor_assert(nToMove >= 0);
|
2007-05-25 20:22:37 +02:00
|
|
|
memmove(&words[gapPos+gapLen], &words[gapPos],
|
2007-05-31 20:48:25 +02:00
|
|
|
sizeof(uint16_t)*nToMove);
|
|
|
|
memset(&words[gapPos], 0, sizeof(uint16_t)*gapLen);
|
2007-05-25 20:22:37 +02:00
|
|
|
}
|
|
|
|
for (i = 0; i < 8; ++i) {
|
|
|
|
out->s6_addr[2*i ] = words[i] >> 8;
|
|
|
|
out->s6_addr[2*i+1] = words[i] & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-22 06:29:06 +01:00
|
|
|
/** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set
|
2007-07-19 20:46:09 +02:00
|
|
|
* *<b>addr</b> to the proper IP address, in host byte order. Returns 0
|
2004-12-22 06:29:06 +01:00
|
|
|
* on success, -1 on failure; 1 on transient failure.
|
|
|
|
*
|
|
|
|
* (This function exists because standard windows gethostbyname
|
|
|
|
* doesn't treat raw IP addresses properly.)
|
|
|
|
*/
|
2014-04-28 22:20:58 +02:00
|
|
|
|
|
|
|
MOCK_IMPL(int,
|
|
|
|
tor_lookup_hostname,(const char *name, uint32_t *addr))
|
2007-07-19 20:46:09 +02:00
|
|
|
{
|
|
|
|
tor_addr_t myaddr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = tor_addr_lookup(name, AF_INET, &myaddr)))
|
|
|
|
return ret;
|
|
|
|
|
2008-05-16 21:19:49 +02:00
|
|
|
if (tor_addr_family(&myaddr) == AF_INET) {
|
|
|
|
*addr = tor_addr_to_ipv4h(&myaddr);
|
2007-07-19 20:46:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-06-12 06:33:26 +02:00
|
|
|
/** Hold the result of our call to <b>uname</b>. */
|
2004-11-01 21:41:47 +01:00
|
|
|
static char uname_result[256];
|
2005-06-12 06:33:26 +02:00
|
|
|
/** True iff uname_result is set. */
|
2004-11-01 21:41:47 +01:00
|
|
|
static int uname_result_is_set = 0;
|
|
|
|
|
2005-06-12 06:33:26 +02:00
|
|
|
/** Return a pointer to a description of our platform.
|
2004-11-01 21:41:47 +01:00
|
|
|
*/
|
2017-05-26 20:05:50 +02:00
|
|
|
MOCK_IMPL(const char *,
|
|
|
|
get_uname,(void))
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
#ifdef HAVE_UNAME
|
|
|
|
struct utsname u;
|
|
|
|
#endif
|
|
|
|
if (!uname_result_is_set) {
|
|
|
|
#ifdef HAVE_UNAME
|
|
|
|
if (uname(&u) != -1) {
|
2009-05-27 23:55:51 +02:00
|
|
|
/* (Linux says 0 is success, Solaris says 1 is success) */
|
2012-05-11 23:50:30 +02:00
|
|
|
strlcpy(uname_result, u.sysname, sizeof(uname_result));
|
2004-11-01 21:41:47 +01:00
|
|
|
} else
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_UNAME) */
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2010-08-20 18:30:25 +02:00
|
|
|
OSVERSIONINFOEX info;
|
2005-01-20 00:10:16 +01:00
|
|
|
int i;
|
|
|
|
const char *plat = NULL;
|
|
|
|
static struct {
|
2007-02-28 21:24:27 +01:00
|
|
|
unsigned major; unsigned minor; const char *version;
|
2005-01-20 00:10:16 +01:00
|
|
|
} win_version_table[] = {
|
2011-10-01 14:44:39 +02:00
|
|
|
{ 6, 2, "Windows 8" },
|
2010-02-07 05:18:50 +01:00
|
|
|
{ 6, 1, "Windows 7" },
|
|
|
|
{ 6, 0, "Windows Vista" },
|
2005-01-20 00:10:16 +01:00
|
|
|
{ 5, 2, "Windows Server 2003" },
|
|
|
|
{ 5, 1, "Windows XP" },
|
|
|
|
{ 5, 0, "Windows 2000" },
|
|
|
|
/* { 4, 0, "Windows NT 4.0" }, */
|
|
|
|
{ 4, 90, "Windows Me" },
|
|
|
|
{ 4, 10, "Windows 98" },
|
|
|
|
/* { 4, 0, "Windows 95" } */
|
|
|
|
{ 3, 51, "Windows NT 3.51" },
|
2007-02-28 21:24:27 +01:00
|
|
|
{ 0, 0, NULL }
|
2005-01-20 00:10:16 +01:00
|
|
|
};
|
2006-03-24 06:26:33 +01:00
|
|
|
memset(&info, 0, sizeof(info));
|
2005-01-20 00:10:16 +01:00
|
|
|
info.dwOSVersionInfoSize = sizeof(info);
|
2010-08-20 18:30:25 +02:00
|
|
|
if (! GetVersionEx((LPOSVERSIONINFO)&info)) {
|
2006-03-24 06:26:33 +01:00
|
|
|
strlcpy(uname_result, "Bizarre version of Windows where GetVersionEx"
|
|
|
|
" doesn't work.", sizeof(uname_result));
|
|
|
|
uname_result_is_set = 1;
|
|
|
|
return uname_result;
|
|
|
|
}
|
2005-01-20 00:10:16 +01:00
|
|
|
if (info.dwMajorVersion == 4 && info.dwMinorVersion == 0) {
|
|
|
|
if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
|
|
|
|
plat = "Windows NT 4.0";
|
|
|
|
else
|
|
|
|
plat = "Windows 95";
|
|
|
|
} else {
|
2007-02-28 21:24:27 +01:00
|
|
|
for (i=0; win_version_table[i].major>0; ++i) {
|
2005-01-20 00:10:16 +01:00
|
|
|
if (win_version_table[i].major == info.dwMajorVersion &&
|
2005-01-20 20:03:09 +01:00
|
|
|
win_version_table[i].minor == info.dwMinorVersion) {
|
2005-01-20 00:10:16 +01:00
|
|
|
plat = win_version_table[i].version;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (plat) {
|
2012-05-11 23:50:30 +02:00
|
|
|
strlcpy(uname_result, plat, sizeof(uname_result));
|
2005-01-20 00:10:16 +01:00
|
|
|
} else {
|
2005-10-06 00:04:45 +02:00
|
|
|
if (info.dwMajorVersion > 6 ||
|
2011-10-01 14:44:39 +02:00
|
|
|
(info.dwMajorVersion==6 && info.dwMinorVersion>2))
|
2005-01-20 20:03:09 +01:00
|
|
|
tor_snprintf(uname_result, sizeof(uname_result),
|
2012-05-11 23:50:30 +02:00
|
|
|
"Very recent version of Windows [major=%d,minor=%d]",
|
2012-05-14 18:08:05 +02:00
|
|
|
(int)info.dwMajorVersion,(int)info.dwMinorVersion);
|
2005-01-20 00:10:16 +01:00
|
|
|
else
|
2005-01-20 20:03:09 +01:00
|
|
|
tor_snprintf(uname_result, sizeof(uname_result),
|
2012-05-11 23:50:30 +02:00
|
|
|
"Unrecognized version of Windows [major=%d,minor=%d]",
|
|
|
|
(int)info.dwMajorVersion,(int)info.dwMinorVersion);
|
2005-01-20 00:10:16 +01:00
|
|
|
}
|
2012-05-11 23:50:30 +02:00
|
|
|
#ifdef VER_NT_SERVER
|
|
|
|
if (info.wProductType == VER_NT_SERVER ||
|
|
|
|
info.wProductType == VER_NT_DOMAIN_CONTROLLER) {
|
|
|
|
strlcat(uname_result, " [server]", sizeof(uname_result));
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(VER_NT_SERVER) */
|
|
|
|
#else /* !(defined(_WIN32)) */
|
2016-06-20 17:03:13 +02:00
|
|
|
/* LCOV_EXCL_START -- can't provoke uname failure */
|
2004-11-01 21:41:47 +01:00
|
|
|
strlcpy(uname_result, "Unknown platform", sizeof(uname_result));
|
2016-06-20 17:03:13 +02:00
|
|
|
/* LCOV_EXCL_STOP */
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) */
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
|
|
|
uname_result_is_set = 1;
|
|
|
|
}
|
|
|
|
return uname_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process control
|
|
|
|
*/
|
|
|
|
|
2010-09-28 20:36:28 +02:00
|
|
|
/** Implementation logic for compute_num_cpus(). */
|
|
|
|
static int
|
|
|
|
compute_num_cpus_impl(void)
|
|
|
|
{
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2010-09-28 20:36:28 +02:00
|
|
|
SYSTEM_INFO info;
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
GetSystemInfo(&info);
|
|
|
|
if (info.dwNumberOfProcessors >= 1 && info.dwNumberOfProcessors < INT_MAX)
|
|
|
|
return (int)info.dwNumberOfProcessors;
|
|
|
|
else
|
|
|
|
return -1;
|
2013-02-19 08:31:53 +01:00
|
|
|
#elif defined(HAVE_SYSCONF)
|
|
|
|
#ifdef _SC_NPROCESSORS_CONF
|
|
|
|
long cpus_conf = sysconf(_SC_NPROCESSORS_CONF);
|
|
|
|
#else
|
|
|
|
long cpus_conf = -1;
|
|
|
|
#endif
|
|
|
|
#ifdef _SC_NPROCESSORS_ONLN
|
|
|
|
long cpus_onln = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
#else
|
|
|
|
long cpus_onln = -1;
|
|
|
|
#endif
|
|
|
|
long cpus = -1;
|
|
|
|
|
|
|
|
if (cpus_conf > 0 && cpus_onln < 0) {
|
|
|
|
cpus = cpus_conf;
|
|
|
|
} else if (cpus_onln > 0 && cpus_conf < 0) {
|
|
|
|
cpus = cpus_onln;
|
|
|
|
} else if (cpus_onln > 0 && cpus_conf > 0) {
|
|
|
|
if (cpus_onln < cpus_conf) {
|
|
|
|
log_notice(LD_GENERAL, "I think we have %ld CPUS, but only %ld of them "
|
|
|
|
"are available. Telling Tor to only use %ld. You can over"
|
|
|
|
"ride this with the NumCPUs option",
|
|
|
|
cpus_conf, cpus_onln, cpus_onln);
|
|
|
|
}
|
|
|
|
cpus = cpus_onln;
|
|
|
|
}
|
|
|
|
|
2010-09-28 20:36:28 +02:00
|
|
|
if (cpus >= 1 && cpus < INT_MAX)
|
|
|
|
return (int)cpus;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
return -1;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) || ... */
|
2010-09-28 20:36:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_DETECTABLE_CPUS 16
|
|
|
|
|
|
|
|
/** Return how many CPUs we are running with. We assume that nobody is
|
|
|
|
* using hot-swappable CPUs, so we don't recompute this after the first
|
|
|
|
* time. Return -1 if we don't know how to tell the number of CPUs on this
|
|
|
|
* system.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
compute_num_cpus(void)
|
|
|
|
{
|
|
|
|
static int num_cpus = -2;
|
|
|
|
if (num_cpus == -2) {
|
|
|
|
num_cpus = compute_num_cpus_impl();
|
|
|
|
tor_assert(num_cpus != -2);
|
2016-06-20 17:03:13 +02:00
|
|
|
if (num_cpus > MAX_DETECTABLE_CPUS) {
|
|
|
|
/* LCOV_EXCL_START */
|
2010-09-28 20:36:28 +02:00
|
|
|
log_notice(LD_GENERAL, "Wow! I detected that you have %d CPUs. I "
|
|
|
|
"will not autodetect any more than %d, though. If you "
|
|
|
|
"want to configure more, set NumCPUs in your torrc",
|
|
|
|
num_cpus, MAX_DETECTABLE_CPUS);
|
2016-10-31 19:19:39 +01:00
|
|
|
num_cpus = MAX_DETECTABLE_CPUS;
|
2016-06-20 17:03:13 +02:00
|
|
|
/* LCOV_EXCL_STOP */
|
|
|
|
}
|
2010-09-28 20:36:28 +02:00
|
|
|
}
|
|
|
|
return num_cpus;
|
|
|
|
}
|
|
|
|
|
2014-06-20 16:20:10 +02:00
|
|
|
#if !defined(_WIN32)
|
2007-02-02 21:06:43 +01:00
|
|
|
/** Defined iff we need to add locks when defining fake versions of reentrant
|
|
|
|
* versions of time-related functions. */
|
2005-02-22 08:09:17 +01:00
|
|
|
#define TIME_FNS_NEED_LOCKS
|
|
|
|
#endif
|
|
|
|
|
2012-06-05 06:17:54 +02:00
|
|
|
/** Helper: Deal with confused or out-of-bounds values from localtime_r and
|
|
|
|
* friends. (On some platforms, they can give out-of-bounds values or can
|
|
|
|
* return NULL.) If <b>islocal</b>, this is a localtime result; otherwise
|
2018-02-07 18:22:29 +01:00
|
|
|
* it's from gmtime. The function returns <b>r</b>, when given <b>timep</b>
|
2012-06-05 06:17:54 +02:00
|
|
|
* as its input. If we need to store new results, store them in
|
|
|
|
* <b>resultbuf</b>. */
|
2010-12-07 20:37:50 +01:00
|
|
|
static struct tm *
|
|
|
|
correct_tm(int islocal, const time_t *timep, struct tm *resultbuf,
|
|
|
|
struct tm *r)
|
|
|
|
{
|
|
|
|
const char *outcome;
|
|
|
|
|
|
|
|
if (PREDICT_LIKELY(r)) {
|
2014-10-19 17:47:31 +02:00
|
|
|
/* We can't strftime dates after 9999 CE, and we want to avoid dates
|
|
|
|
* before 1 CE (avoiding the year 0 issue and negative years). */
|
|
|
|
if (r->tm_year > 8099) {
|
2010-12-07 20:37:50 +01:00
|
|
|
r->tm_year = 8099;
|
|
|
|
r->tm_mon = 11;
|
|
|
|
r->tm_mday = 31;
|
2014-10-19 17:32:05 +02:00
|
|
|
r->tm_yday = 364;
|
2016-05-05 16:49:55 +02:00
|
|
|
r->tm_wday = 6;
|
2010-12-07 20:37:50 +01:00
|
|
|
r->tm_hour = 23;
|
|
|
|
r->tm_min = 59;
|
|
|
|
r->tm_sec = 59;
|
2014-10-19 17:47:31 +02:00
|
|
|
} else if (r->tm_year < (1-1900)) {
|
|
|
|
r->tm_year = (1-1900);
|
|
|
|
r->tm_mon = 0;
|
|
|
|
r->tm_mday = 1;
|
|
|
|
r->tm_yday = 0;
|
2016-05-12 20:39:06 +02:00
|
|
|
r->tm_wday = 0;
|
2014-10-19 17:47:31 +02:00
|
|
|
r->tm_hour = 0;
|
|
|
|
r->tm_min = 0;
|
|
|
|
r->tm_sec = 0;
|
2010-12-07 20:37:50 +01:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we get here, gmtime or localtime returned NULL. It might have done
|
|
|
|
* this because of overrun or underrun, or it might have done it because of
|
|
|
|
* some other weird issue. */
|
|
|
|
if (timep) {
|
|
|
|
if (*timep < 0) {
|
|
|
|
r = resultbuf;
|
|
|
|
r->tm_year = 70; /* 1970 CE */
|
|
|
|
r->tm_mon = 0;
|
|
|
|
r->tm_mday = 1;
|
2014-10-19 17:32:05 +02:00
|
|
|
r->tm_yday = 0;
|
2016-05-05 16:49:55 +02:00
|
|
|
r->tm_wday = 0;
|
2010-12-07 20:37:50 +01:00
|
|
|
r->tm_hour = 0;
|
|
|
|
r->tm_min = 0 ;
|
|
|
|
r->tm_sec = 0;
|
|
|
|
outcome = "Rounding up to 1970";
|
|
|
|
goto done;
|
|
|
|
} else if (*timep >= INT32_MAX) {
|
|
|
|
/* Rounding down to INT32_MAX isn't so great, but keep in mind that we
|
|
|
|
* only do it if gmtime/localtime tells us NULL. */
|
|
|
|
r = resultbuf;
|
|
|
|
r->tm_year = 137; /* 2037 CE */
|
|
|
|
r->tm_mon = 11;
|
|
|
|
r->tm_mday = 31;
|
2014-10-19 17:32:05 +02:00
|
|
|
r->tm_yday = 364;
|
2016-05-05 16:49:55 +02:00
|
|
|
r->tm_wday = 6;
|
2010-12-07 20:37:50 +01:00
|
|
|
r->tm_hour = 23;
|
|
|
|
r->tm_min = 59;
|
|
|
|
r->tm_sec = 59;
|
|
|
|
outcome = "Rounding down to 2037";
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we get here, then gmtime/localtime failed without getting an extreme
|
|
|
|
* value for *timep */
|
2016-06-15 23:25:53 +02:00
|
|
|
/* LCOV_EXCL_START */
|
2010-12-07 20:37:50 +01:00
|
|
|
tor_fragile_assert();
|
|
|
|
r = resultbuf;
|
|
|
|
memset(resultbuf, 0, sizeof(struct tm));
|
|
|
|
outcome="can't recover";
|
2016-06-15 23:25:53 +02:00
|
|
|
/* LCOV_EXCL_STOP */
|
2010-12-07 20:37:50 +01:00
|
|
|
done:
|
|
|
|
log_warn(LD_BUG, "%s("I64_FORMAT") failed with error %s: %s",
|
|
|
|
islocal?"localtime":"gmtime",
|
|
|
|
timep?I64_PRINTF_ARG(*timep):0,
|
|
|
|
strerror(errno),
|
|
|
|
outcome);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @{ */
|
|
|
|
/** As localtime_r, but defined for platforms that don't have it:
|
|
|
|
*
|
|
|
|
* Convert *<b>timep</b> to a struct tm in local time, and store the value in
|
|
|
|
* *<b>result</b>. Return the result on success, or NULL on failure.
|
|
|
|
*/
|
2010-12-07 20:37:50 +01:00
|
|
|
#ifdef HAVE_LOCALTIME_R
|
|
|
|
struct tm *
|
|
|
|
tor_localtime_r(const time_t *timep, struct tm *result)
|
|
|
|
{
|
|
|
|
struct tm *r;
|
|
|
|
r = localtime_r(timep, result);
|
|
|
|
return correct_tm(1, timep, result, r);
|
|
|
|
}
|
|
|
|
#elif defined(TIME_FNS_NEED_LOCKS)
|
2005-06-11 20:52:12 +02:00
|
|
|
struct tm *
|
|
|
|
tor_localtime_r(const time_t *timep, struct tm *result)
|
2005-02-22 08:03:03 +01:00
|
|
|
{
|
|
|
|
struct tm *r;
|
|
|
|
static tor_mutex_t *m=NULL;
|
|
|
|
if (!m) { m=tor_mutex_new(); }
|
|
|
|
tor_assert(result);
|
|
|
|
tor_mutex_acquire(m);
|
|
|
|
r = localtime(timep);
|
2010-12-07 20:37:50 +01:00
|
|
|
if (r)
|
|
|
|
memcpy(result, r, sizeof(struct tm));
|
2005-02-22 08:03:03 +01:00
|
|
|
tor_mutex_release(m);
|
2010-12-07 20:37:50 +01:00
|
|
|
return correct_tm(1, timep, result, r);
|
2005-02-22 08:03:03 +01:00
|
|
|
}
|
2005-02-22 08:09:17 +01:00
|
|
|
#else
|
2005-06-11 20:52:12 +02:00
|
|
|
struct tm *
|
|
|
|
tor_localtime_r(const time_t *timep, struct tm *result)
|
2005-02-22 08:09:17 +01:00
|
|
|
{
|
|
|
|
struct tm *r;
|
|
|
|
tor_assert(result);
|
|
|
|
r = localtime(timep);
|
2010-12-07 20:37:50 +01:00
|
|
|
if (r)
|
|
|
|
memcpy(result, r, sizeof(struct tm));
|
|
|
|
return correct_tm(1, timep, result, r);
|
2005-02-22 08:09:17 +01:00
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_LOCALTIME_R) || ... */
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @} */
|
2005-02-22 08:03:03 +01:00
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @{ */
|
2014-10-22 17:35:46 +02:00
|
|
|
/** As gmtime_r, but defined for platforms that don't have it:
|
2011-03-16 22:05:37 +01:00
|
|
|
*
|
|
|
|
* Convert *<b>timep</b> to a struct tm in UTC, and store the value in
|
|
|
|
* *<b>result</b>. Return the result on success, or NULL on failure.
|
|
|
|
*/
|
2011-01-04 05:36:09 +01:00
|
|
|
#ifdef HAVE_GMTIME_R
|
2010-12-07 20:37:50 +01:00
|
|
|
struct tm *
|
|
|
|
tor_gmtime_r(const time_t *timep, struct tm *result)
|
|
|
|
{
|
|
|
|
struct tm *r;
|
|
|
|
r = gmtime_r(timep, result);
|
|
|
|
return correct_tm(0, timep, result, r);
|
|
|
|
}
|
|
|
|
#elif defined(TIME_FNS_NEED_LOCKS)
|
2005-06-11 20:52:12 +02:00
|
|
|
struct tm *
|
|
|
|
tor_gmtime_r(const time_t *timep, struct tm *result)
|
2005-02-22 08:03:03 +01:00
|
|
|
{
|
|
|
|
struct tm *r;
|
|
|
|
static tor_mutex_t *m=NULL;
|
|
|
|
if (!m) { m=tor_mutex_new(); }
|
|
|
|
tor_assert(result);
|
|
|
|
tor_mutex_acquire(m);
|
|
|
|
r = gmtime(timep);
|
2011-01-04 05:36:09 +01:00
|
|
|
if (r)
|
|
|
|
memcpy(result, r, sizeof(struct tm));
|
2005-02-22 08:03:03 +01:00
|
|
|
tor_mutex_release(m);
|
2010-12-07 20:37:50 +01:00
|
|
|
return correct_tm(0, timep, result, r);
|
2005-02-22 08:03:03 +01:00
|
|
|
}
|
2005-02-22 08:09:17 +01:00
|
|
|
#else
|
2005-06-11 20:52:12 +02:00
|
|
|
struct tm *
|
|
|
|
tor_gmtime_r(const time_t *timep, struct tm *result)
|
2005-02-22 08:09:17 +01:00
|
|
|
{
|
|
|
|
struct tm *r;
|
|
|
|
tor_assert(result);
|
|
|
|
r = gmtime(timep);
|
2011-01-04 05:36:09 +01:00
|
|
|
if (r)
|
|
|
|
memcpy(result, r, sizeof(struct tm));
|
2010-12-07 20:37:50 +01:00
|
|
|
return correct_tm(0, timep, result, r);
|
2005-02-22 08:09:17 +01:00
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_GMTIME_R) || ... */
|
2005-02-22 08:03:03 +01:00
|
|
|
|
2010-01-19 04:59:24 +01:00
|
|
|
#if defined(HAVE_MLOCKALL) && HAVE_DECL_MLOCKALL && defined(RLIMIT_MEMLOCK)
|
2017-09-15 22:15:27 +02:00
|
|
|
#define HAVE_UNIX_MLOCKALL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_UNIX_MLOCKALL
|
2009-10-22 06:21:57 +02:00
|
|
|
/** Attempt to raise the current and max rlimit to infinity for our process.
|
|
|
|
* This only needs to be done once and can probably only be done when we have
|
|
|
|
* not already dropped privileges.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
tor_set_max_memlock(void)
|
|
|
|
{
|
|
|
|
/* Future consideration for Windows is probably SetProcessWorkingSetSize
|
|
|
|
* This is similar to setting the memory rlimit of RLIMIT_MEMLOCK
|
|
|
|
* http://msdn.microsoft.com/en-us/library/ms686234(VS.85).aspx
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct rlimit limit;
|
|
|
|
|
|
|
|
/* RLIM_INFINITY is -1 on some platforms. */
|
|
|
|
limit.rlim_cur = RLIM_INFINITY;
|
|
|
|
limit.rlim_max = RLIM_INFINITY;
|
|
|
|
|
2010-01-03 19:58:28 +01:00
|
|
|
if (setrlimit(RLIMIT_MEMLOCK, &limit) == -1) {
|
2009-10-22 06:21:57 +02:00
|
|
|
if (errno == EPERM) {
|
|
|
|
log_warn(LD_GENERAL, "You appear to lack permissions to change memory "
|
|
|
|
"limits. Are you root?");
|
|
|
|
}
|
2010-01-03 19:58:28 +01:00
|
|
|
log_warn(LD_GENERAL, "Unable to raise RLIMIT_MEMLOCK: %s",
|
|
|
|
strerror(errno));
|
2009-10-22 06:21:57 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_UNIX_MLOCKALL) */
|
2009-10-22 06:21:57 +02:00
|
|
|
|
|
|
|
/** Attempt to lock all current and all future memory pages.
|
|
|
|
* This should only be called once and while we're privileged.
|
|
|
|
* Like mlockall() we return 0 when we're successful and -1 when we're not.
|
|
|
|
* Unlike mlockall() we return 1 if we've already attempted to lock memory.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
tor_mlockall(void)
|
|
|
|
{
|
|
|
|
static int memory_lock_attempted = 0;
|
|
|
|
|
|
|
|
if (memory_lock_attempted) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memory_lock_attempted = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Future consideration for Windows may be VirtualLock
|
|
|
|
* VirtualLock appears to implement mlock() but not mlockall()
|
|
|
|
*
|
|
|
|
* http://msdn.microsoft.com/en-us/library/aa366895(VS.85).aspx
|
|
|
|
*/
|
|
|
|
|
2017-09-15 22:15:27 +02:00
|
|
|
#ifdef HAVE_UNIX_MLOCKALL
|
2009-11-20 19:28:16 +01:00
|
|
|
if (tor_set_max_memlock() == 0) {
|
2010-01-03 19:58:28 +01:00
|
|
|
log_debug(LD_GENERAL, "RLIMIT_MEMLOCK is now set to RLIM_INFINITY.");
|
2009-10-22 06:21:57 +02:00
|
|
|
}
|
|
|
|
|
2009-11-20 19:28:16 +01:00
|
|
|
if (mlockall(MCL_CURRENT|MCL_FUTURE) == 0) {
|
2010-01-03 19:58:28 +01:00
|
|
|
log_info(LD_GENERAL, "Insecure OS paging is effectively disabled.");
|
2009-10-22 06:21:57 +02:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (errno == ENOSYS) {
|
|
|
|
/* Apple - it's 2009! I'm looking at you. Grrr. */
|
|
|
|
log_notice(LD_GENERAL, "It appears that mlockall() is not available on "
|
|
|
|
"your platform.");
|
|
|
|
} else if (errno == EPERM) {
|
|
|
|
log_notice(LD_GENERAL, "It appears that you lack the permissions to "
|
|
|
|
"lock memory. Are you root?");
|
|
|
|
}
|
|
|
|
log_notice(LD_GENERAL, "Unable to lock all current and future memory "
|
|
|
|
"pages: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(HAVE_UNIX_MLOCKALL)) */
|
2009-10-22 06:21:57 +02:00
|
|
|
log_warn(LD_GENERAL, "Unable to lock memory pages. mlockall() unsupported?");
|
|
|
|
return -1;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_UNIX_MLOCKALL) */
|
2009-10-22 06:21:57 +02:00
|
|
|
}
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/**
|
|
|
|
* On Windows, WSAEWOULDBLOCK is not always correct: when you see it,
|
|
|
|
* you need to ask the socket for its actual errno. Also, you need to
|
|
|
|
* get your errors from WSAGetLastError, not errno. (If you supply a
|
|
|
|
* socket of -1, we check WSAGetLastError, but don't correct
|
|
|
|
* WSAEWOULDBLOCKs.)
|
2004-11-28 06:48:02 +01:00
|
|
|
*
|
|
|
|
* The upshot of all of this is that when a socket call fails, you
|
|
|
|
* should call tor_socket_errno <em>at most once</em> on the failing
|
|
|
|
* socket to get the error.
|
2004-11-01 21:41:47 +01:00
|
|
|
*/
|
2012-01-31 16:59:42 +01:00
|
|
|
#if defined(_WIN32)
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2011-05-23 06:17:48 +02:00
|
|
|
tor_socket_errno(tor_socket_t sock)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
int optval, optvallen=sizeof(optval);
|
|
|
|
int err = WSAGetLastError();
|
2011-05-23 06:17:48 +02:00
|
|
|
if (err == WSAEWOULDBLOCK && SOCKET_OK(sock)) {
|
2004-11-01 21:41:47 +01:00
|
|
|
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval, &optvallen))
|
|
|
|
return err;
|
|
|
|
if (optval)
|
|
|
|
return optval;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) */
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2012-01-31 16:59:42 +01:00
|
|
|
#if defined(_WIN32)
|
2004-11-01 21:41:47 +01:00
|
|
|
#define E(code, s) { code, (s " [" #code " ]") }
|
|
|
|
struct { int code; const char *msg; } windows_socket_errors[] = {
|
|
|
|
E(WSAEINTR, "Interrupted function call"),
|
|
|
|
E(WSAEACCES, "Permission denied"),
|
|
|
|
E(WSAEFAULT, "Bad address"),
|
|
|
|
E(WSAEINVAL, "Invalid argument"),
|
|
|
|
E(WSAEMFILE, "Too many open files"),
|
|
|
|
E(WSAEWOULDBLOCK, "Resource temporarily unavailable"),
|
|
|
|
E(WSAEINPROGRESS, "Operation now in progress"),
|
|
|
|
E(WSAEALREADY, "Operation already in progress"),
|
|
|
|
E(WSAENOTSOCK, "Socket operation on nonsocket"),
|
|
|
|
E(WSAEDESTADDRREQ, "Destination address required"),
|
|
|
|
E(WSAEMSGSIZE, "Message too long"),
|
|
|
|
E(WSAEPROTOTYPE, "Protocol wrong for socket"),
|
|
|
|
E(WSAENOPROTOOPT, "Bad protocol option"),
|
|
|
|
E(WSAEPROTONOSUPPORT, "Protocol not supported"),
|
|
|
|
E(WSAESOCKTNOSUPPORT, "Socket type not supported"),
|
|
|
|
/* What's the difference between NOTSUPP and NOSUPPORT? :) */
|
|
|
|
E(WSAEOPNOTSUPP, "Operation not supported"),
|
|
|
|
E(WSAEPFNOSUPPORT, "Protocol family not supported"),
|
|
|
|
E(WSAEAFNOSUPPORT, "Address family not supported by protocol family"),
|
|
|
|
E(WSAEADDRINUSE, "Address already in use"),
|
|
|
|
E(WSAEADDRNOTAVAIL, "Cannot assign requested address"),
|
|
|
|
E(WSAENETDOWN, "Network is down"),
|
|
|
|
E(WSAENETUNREACH, "Network is unreachable"),
|
|
|
|
E(WSAENETRESET, "Network dropped connection on reset"),
|
|
|
|
E(WSAECONNABORTED, "Software caused connection abort"),
|
|
|
|
E(WSAECONNRESET, "Connection reset by peer"),
|
2004-12-01 04:48:14 +01:00
|
|
|
E(WSAENOBUFS, "No buffer space available"),
|
2004-11-01 21:41:47 +01:00
|
|
|
E(WSAEISCONN, "Socket is already connected"),
|
|
|
|
E(WSAENOTCONN, "Socket is not connected"),
|
|
|
|
E(WSAESHUTDOWN, "Cannot send after socket shutdown"),
|
|
|
|
E(WSAETIMEDOUT, "Connection timed out"),
|
|
|
|
E(WSAECONNREFUSED, "Connection refused"),
|
|
|
|
E(WSAEHOSTDOWN, "Host is down"),
|
|
|
|
E(WSAEHOSTUNREACH, "No route to host"),
|
|
|
|
E(WSAEPROCLIM, "Too many processes"),
|
|
|
|
/* Yes, some of these start with WSA, not WSAE. No, I don't know why. */
|
|
|
|
E(WSASYSNOTREADY, "Network subsystem is unavailable"),
|
|
|
|
E(WSAVERNOTSUPPORTED, "Winsock.dll out of range"),
|
|
|
|
E(WSANOTINITIALISED, "Successful WSAStartup not yet performed"),
|
|
|
|
E(WSAEDISCON, "Graceful shutdown now in progress"),
|
|
|
|
#ifdef WSATYPE_NOT_FOUND
|
|
|
|
E(WSATYPE_NOT_FOUND, "Class type not found"),
|
|
|
|
#endif
|
|
|
|
E(WSAHOST_NOT_FOUND, "Host not found"),
|
|
|
|
E(WSATRY_AGAIN, "Nonauthoritative host not found"),
|
|
|
|
E(WSANO_RECOVERY, "This is a nonrecoverable error"),
|
|
|
|
E(WSANO_DATA, "Valid name, no data record of requested type)"),
|
|
|
|
|
|
|
|
/* There are some more error codes whose numeric values are marked
|
|
|
|
* <b>OS dependent</b>. They start with WSA_, apparently for the same
|
|
|
|
* reason that practitioners of some craft traditions deliberately
|
|
|
|
* introduce imperfections into their baskets and rugs "to allow the
|
|
|
|
* evil spirits to escape." If we catch them, then our binaries
|
|
|
|
* might not report consistent results across versions of Windows.
|
|
|
|
* Thus, I'm going to let them all fall through.
|
|
|
|
*/
|
|
|
|
{ -1, NULL },
|
|
|
|
};
|
2009-05-27 23:55:51 +02:00
|
|
|
/** There does not seem to be a strerror equivalent for Winsock errors.
|
2004-11-01 21:41:47 +01:00
|
|
|
* Naturally, we have to roll our own.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
const char *
|
|
|
|
tor_socket_strerror(int e)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; windows_socket_errors[i].code >= 0; ++i) {
|
|
|
|
if (e == windows_socket_errors[i].code)
|
|
|
|
return windows_socket_errors[i].msg;
|
|
|
|
}
|
|
|
|
return strerror(e);
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) */
|
2004-12-22 06:29:06 +01:00
|
|
|
|
|
|
|
/** Called before we make any calls to network-related functions.
|
|
|
|
* (Some operating systems require their network libraries to be
|
|
|
|
* initialized.) */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
network_init(void)
|
2004-12-22 06:29:06 +01:00
|
|
|
{
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2005-06-12 06:33:26 +02:00
|
|
|
/* This silly exercise is necessary before windows will allow
|
|
|
|
* gethostbyname to work. */
|
2004-12-22 06:29:06 +01:00
|
|
|
WSADATA WSAData;
|
|
|
|
int r;
|
|
|
|
r = WSAStartup(0x101,&WSAData);
|
|
|
|
if (r) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_NET,"Error initializing windows network layer: code was %d",r);
|
2004-12-22 06:29:06 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2012-01-17 22:38:47 +01:00
|
|
|
if (sizeof(SOCKET) != sizeof(tor_socket_t)) {
|
|
|
|
log_warn(LD_BUG,"The tor_socket_t type does not match SOCKET in size; Tor "
|
|
|
|
"might not work. (Sizes are %d and %d respectively.)",
|
|
|
|
(int)sizeof(tor_socket_t), (int)sizeof(SOCKET));
|
|
|
|
}
|
2005-03-28 03:11:02 +02:00
|
|
|
/* WSAData.iMaxSockets might show the max sockets we're allowed to use.
|
|
|
|
* We might use it to complain if we're trying to be a server but have
|
|
|
|
* too few sockets available. */
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) */
|
2004-12-22 06:29:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2007-02-12 22:39:44 +01:00
|
|
|
/** Return a newly allocated string describing the windows system error code
|
|
|
|
* <b>err</b>. Note that error codes are different from errno. Error codes
|
|
|
|
* come from GetLastError() when a winapi call fails. errno is set only when
|
2009-05-27 23:55:51 +02:00
|
|
|
* ANSI functions fail. Whee. */
|
2007-02-12 22:39:44 +01:00
|
|
|
char *
|
|
|
|
format_win32_error(DWORD err)
|
|
|
|
{
|
2010-08-20 18:30:25 +02:00
|
|
|
TCHAR *str = NULL;
|
2007-02-12 22:39:44 +01:00
|
|
|
char *result;
|
2012-05-17 16:08:48 +02:00
|
|
|
DWORD n;
|
2007-02-12 22:39:44 +01:00
|
|
|
|
|
|
|
/* Somebody once decided that this interface was better than strerror(). */
|
2012-05-17 16:08:48 +02:00
|
|
|
n = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
2007-02-12 22:39:44 +01:00
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, err,
|
2017-07-07 19:11:18 +02:00
|
|
|
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
|
2012-05-17 16:08:48 +02:00
|
|
|
(LPVOID)&str,
|
2007-02-12 22:39:44 +01:00
|
|
|
0, NULL);
|
|
|
|
|
2012-05-17 16:08:48 +02:00
|
|
|
if (str && n) {
|
2010-08-20 18:30:25 +02:00
|
|
|
#ifdef UNICODE
|
2012-05-17 16:08:48 +02:00
|
|
|
size_t len;
|
|
|
|
if (n > 128*1024)
|
|
|
|
len = (128 * 1024) * 2 + 1; /* This shouldn't be possible, but let's
|
|
|
|
* make sure. */
|
|
|
|
else
|
|
|
|
len = n * 2 + 1;
|
|
|
|
result = tor_malloc(len);
|
|
|
|
wcstombs(result,str,len);
|
|
|
|
result[len-1] = '\0';
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(UNICODE)) */
|
2010-08-20 18:30:25 +02:00
|
|
|
result = tor_strdup(str);
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(UNICODE) */
|
2007-02-12 22:39:44 +01:00
|
|
|
} else {
|
|
|
|
result = tor_strdup("<unformattable error>");
|
|
|
|
}
|
2012-05-17 16:08:48 +02:00
|
|
|
if (str) {
|
|
|
|
LocalFree(str); /* LocalFree != free() */
|
|
|
|
}
|
2007-02-12 22:39:44 +01:00
|
|
|
return result;
|
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_WIN32) */
|
2007-02-12 22:39:53 +01:00
|
|
|
|
2014-04-03 17:46:01 +02:00
|
|
|
#if defined(HW_PHYSMEM64)
|
|
|
|
/* This appears to be an OpenBSD thing */
|
|
|
|
#define INT64_HW_MEM HW_PHYSMEM64
|
|
|
|
#elif defined(HW_MEMSIZE)
|
|
|
|
/* OSX defines this one */
|
|
|
|
#define INT64_HW_MEM HW_MEMSIZE
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HW_PHYSMEM64) || ... */
|
2014-04-03 17:46:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper: try to detect the total system memory, and return it. On failure,
|
|
|
|
* return 0.
|
|
|
|
*/
|
|
|
|
static uint64_t
|
|
|
|
get_total_system_memory_impl(void)
|
|
|
|
{
|
|
|
|
#if defined(__linux__)
|
|
|
|
/* On linux, sysctl is deprecated. Because proc is so awesome that you
|
|
|
|
* shouldn't _want_ to write portable code, I guess? */
|
|
|
|
unsigned long long result=0;
|
|
|
|
int fd = -1;
|
|
|
|
char *s = NULL;
|
|
|
|
const char *cp;
|
|
|
|
size_t file_size=0;
|
|
|
|
if (-1 == (fd = tor_open_cloexec("/proc/meminfo",O_RDONLY,0)))
|
|
|
|
return 0;
|
|
|
|
s = read_file_to_str_until_eof(fd, 65536, &file_size);
|
|
|
|
if (!s)
|
|
|
|
goto err;
|
|
|
|
cp = strstr(s, "MemTotal:");
|
|
|
|
if (!cp)
|
|
|
|
goto err;
|
|
|
|
/* Use the system sscanf so that space will match a wider number of space */
|
|
|
|
if (sscanf(cp, "MemTotal: %llu kB\n", &result) != 1)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
tor_free(s);
|
|
|
|
return result * 1024;
|
|
|
|
|
2016-06-20 17:03:13 +02:00
|
|
|
/* LCOV_EXCL_START Can't reach this unless proc is broken. */
|
2017-09-28 15:25:17 +02:00
|
|
|
err:
|
2014-04-03 17:46:01 +02:00
|
|
|
tor_free(s);
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
2016-06-20 17:03:13 +02:00
|
|
|
/* LCOV_EXCL_STOP */
|
2014-04-03 17:46:01 +02:00
|
|
|
#elif defined (_WIN32)
|
|
|
|
/* Windows has MEMORYSTATUSEX; pretty straightforward. */
|
|
|
|
MEMORYSTATUSEX ms;
|
|
|
|
memset(&ms, 0, sizeof(ms));
|
|
|
|
ms.dwLength = sizeof(ms);
|
|
|
|
if (! GlobalMemoryStatusEx(&ms))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return ms.ullTotalPhys;
|
|
|
|
|
|
|
|
#elif defined(HAVE_SYSCTL) && defined(INT64_HW_MEM)
|
|
|
|
/* On many systems, HW_PYHSMEM is clipped to 32 bits; let's use a better
|
|
|
|
* variant if we know about it. */
|
|
|
|
uint64_t memsize = 0;
|
|
|
|
size_t len = sizeof(memsize);
|
|
|
|
int mib[2] = {CTL_HW, INT64_HW_MEM};
|
|
|
|
if (sysctl(mib,2,&memsize,&len,NULL,0))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return memsize;
|
|
|
|
|
|
|
|
#elif defined(HAVE_SYSCTL) && defined(HW_PHYSMEM)
|
|
|
|
/* On some systems (like FreeBSD I hope) you can use a size_t with
|
|
|
|
* HW_PHYSMEM. */
|
|
|
|
size_t memsize=0;
|
|
|
|
size_t len = sizeof(memsize);
|
|
|
|
int mib[2] = {CTL_HW, HW_USERMEM};
|
|
|
|
if (sysctl(mib,2,&memsize,&len,NULL,0))
|
2015-02-09 02:58:22 +01:00
|
|
|
return 0;
|
2014-04-03 17:46:01 +02:00
|
|
|
|
|
|
|
return memsize;
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* I have no clue. */
|
|
|
|
return 0;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(__linux__) || ... */
|
2014-04-03 17:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to find out how much physical memory the system has. On success,
|
|
|
|
* return 0 and set *<b>mem_out</b> to that value. On failure, return -1.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
get_total_system_memory(size_t *mem_out)
|
|
|
|
{
|
|
|
|
static size_t mem_cached=0;
|
|
|
|
uint64_t m = get_total_system_memory_impl();
|
|
|
|
if (0 == m) {
|
2016-06-20 17:03:13 +02:00
|
|
|
/* LCOV_EXCL_START -- can't make this happen without mocking. */
|
2014-04-03 17:46:01 +02:00
|
|
|
/* We couldn't find our memory total */
|
|
|
|
if (0 == mem_cached) {
|
|
|
|
/* We have no cached value either */
|
|
|
|
*mem_out = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*mem_out = mem_cached;
|
|
|
|
return 0;
|
2016-06-20 17:03:13 +02:00
|
|
|
/* LCOV_EXCL_STOP */
|
2014-04-03 17:46:01 +02:00
|
|
|
}
|
|
|
|
|
2014-09-09 18:08:03 +02:00
|
|
|
#if SIZE_MAX != UINT64_MAX
|
|
|
|
if (m > SIZE_MAX) {
|
2014-04-03 17:46:01 +02:00
|
|
|
/* I think this could happen if we're a 32-bit Tor running on a 64-bit
|
|
|
|
* system: we could have more system memory than would fit in a
|
|
|
|
* size_t. */
|
2014-09-09 18:08:03 +02:00
|
|
|
m = SIZE_MAX;
|
2014-04-03 17:46:01 +02:00
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* SIZE_MAX != UINT64_MAX */
|
2014-04-03 17:46:01 +02:00
|
|
|
|
|
|
|
*mem_out = mem_cached = (size_t) m;
|
|
|
|
|
2014-05-08 06:32:22 +02:00
|
|
|
return 0;
|
2014-04-03 17:46:01 +02:00
|
|
|
}
|
|
|
|
|
2015-03-01 15:35:36 +01:00
|
|
|
/** Emit the password prompt <b>prompt</b>, then read up to <b>buflen</b>
|
2015-06-17 17:22:31 +02:00
|
|
|
* bytes of passphrase into <b>output</b>. Return the number of bytes in
|
|
|
|
* the passphrase, excluding terminating NUL.
|
|
|
|
*/
|
2015-03-01 15:35:36 +01:00
|
|
|
ssize_t
|
|
|
|
tor_getpass(const char *prompt, char *output, size_t buflen)
|
|
|
|
{
|
|
|
|
tor_assert(buflen <= SSIZE_MAX);
|
2015-06-17 17:22:31 +02:00
|
|
|
tor_assert(buflen >= 1);
|
2015-03-01 15:35:36 +01:00
|
|
|
#if defined(HAVE_READPASSPHRASE)
|
|
|
|
char *pwd = readpassphrase(prompt, output, buflen, RPP_ECHO_OFF);
|
|
|
|
if (pwd == NULL)
|
|
|
|
return -1;
|
|
|
|
return strlen(pwd);
|
2015-06-17 17:22:31 +02:00
|
|
|
#elif defined(_WIN32)
|
|
|
|
int r = -1;
|
|
|
|
while (*prompt) {
|
|
|
|
_putch(*prompt++);
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_assert(buflen <= INT_MAX);
|
|
|
|
wchar_t *buf = tor_calloc(buflen, sizeof(wchar_t));
|
|
|
|
|
|
|
|
wchar_t *ptr = buf, *lastch = buf + buflen - 1;
|
|
|
|
while (ptr < lastch) {
|
|
|
|
wint_t ch = _getwch();
|
|
|
|
switch (ch) {
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
case WEOF:
|
|
|
|
goto done_reading;
|
|
|
|
case 3:
|
|
|
|
goto done; /* Can't actually read ctrl-c this way. */
|
|
|
|
case '\b':
|
|
|
|
if (ptr > buf)
|
|
|
|
--ptr;
|
|
|
|
continue;
|
|
|
|
case 0:
|
|
|
|
case 0xe0:
|
|
|
|
ch = _getwch(); /* Ignore; this is a function or arrow key */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*ptr++ = ch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done_reading:
|
|
|
|
;
|
|
|
|
|
|
|
|
#ifndef WC_ERR_INVALID_CHARS
|
|
|
|
#define WC_ERR_INVALID_CHARS 0x80
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Now convert it to UTF-8 */
|
|
|
|
r = WideCharToMultiByte(CP_UTF8,
|
|
|
|
WC_NO_BEST_FIT_CHARS|WC_ERR_INVALID_CHARS,
|
|
|
|
buf, (int)(ptr-buf),
|
|
|
|
output, (int)(buflen-1),
|
|
|
|
NULL, NULL);
|
|
|
|
if (r <= 0) {
|
|
|
|
r = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_assert(r < (int)buflen);
|
|
|
|
|
|
|
|
output[r] = 0;
|
|
|
|
|
|
|
|
done:
|
|
|
|
SecureZeroMemory(buf, sizeof(wchar_t)*buflen);
|
|
|
|
tor_free(buf);
|
|
|
|
return r;
|
2015-03-01 15:35:36 +01:00
|
|
|
#else
|
2015-06-17 17:52:54 +02:00
|
|
|
#error "No implementation for tor_getpass found!"
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_READPASSPHRASE) || ... */
|
2015-03-01 15:35:36 +01:00
|
|
|
}
|
2015-07-16 17:10:14 +02:00
|
|
|
|
2015-08-05 20:01:49 +02:00
|
|
|
/** Return the amount of free disk space we have permission to use, in
|
|
|
|
* bytes. Return -1 if the amount of free space can't be determined. */
|
|
|
|
int64_t
|
|
|
|
tor_get_avail_disk_space(const char *path)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_STATVFS
|
|
|
|
struct statvfs st;
|
|
|
|
int r;
|
|
|
|
memset(&st, 0, sizeof(st));
|
|
|
|
|
|
|
|
r = statvfs(path, &st);
|
|
|
|
if (r < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int64_t result = st.f_bavail;
|
|
|
|
if (st.f_frsize) {
|
|
|
|
result *= st.f_frsize;
|
|
|
|
} else if (st.f_bsize) {
|
|
|
|
result *= st.f_bsize;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
ULARGE_INTEGER freeBytesAvail;
|
|
|
|
BOOL ok;
|
|
|
|
|
|
|
|
ok = GetDiskFreeSpaceEx(path, &freeBytesAvail, NULL, NULL);
|
|
|
|
if (!ok) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-08-11 15:09:06 +02:00
|
|
|
return (int64_t)freeBytesAvail.QuadPart;
|
2015-08-05 20:01:49 +02:00
|
|
|
#else
|
|
|
|
(void)path;
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(HAVE_STATVFS) || ... */
|
2015-08-05 20:01:49 +02:00
|
|
|
}
|
2015-08-20 20:28:47 +02:00
|
|
|
|