2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2008-02-07 06:31:47 +01:00
|
|
|
* Copyright (c) 2007-2008, The Tor Project, Inc. */
|
2004-11-01 21:41:47 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2005-12-14 21:40:40 +01:00
|
|
|
const char compat_c_id[] =
|
|
|
|
"$Id$";
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2005-06-11 07:31:17 +02:00
|
|
|
/**
|
|
|
|
* \file compat.c
|
|
|
|
* \brief Wrappers to make calls more portable. This code defines
|
|
|
|
* functions such as tor_malloc, tor_snprintf, get/set various data types,
|
|
|
|
* renaming, setting socket options, switching user IDs. It is basically
|
|
|
|
* where the non-portable items are conditionally included depending on
|
|
|
|
* the platform.
|
|
|
|
**/
|
|
|
|
|
2005-06-18 04:17:11 +02:00
|
|
|
/* This is required on rh7 to make strptime not complain.
|
|
|
|
* We also need it to make memmem get defined (where available)
|
|
|
|
*/
|
2004-11-01 21:41:47 +01:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2004-11-04 05:01:19 +01:00
|
|
|
#include "compat.h"
|
2004-11-01 21:41:47 +01:00
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
#include <process.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
|
|
|
|
#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
|
2008-09-01 23:16:07 +02:00
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
#ifndef HAVE_GETTIMEOFDAY
|
|
|
|
#ifdef HAVE_FTIME
|
|
|
|
#include <sys/timeb.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
2008-09-01 23:16:07 +02: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
|
2005-09-15 01:27:52 +02:00
|
|
|
#ifdef HAVE_UTIME_H
|
|
|
|
#include <utime.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_UTIME_H
|
|
|
|
#include <sys/utime.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
|
|
|
|
2006-09-19 22:41:31 +02:00
|
|
|
#ifdef USE_BSOCKETS
|
|
|
|
#include <bsocket.h>
|
|
|
|
#endif
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
#include "log.h"
|
|
|
|
#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"
|
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
|
|
|
|
|
2004-12-22 06:29:06 +01:00
|
|
|
#ifndef INADDR_NONE
|
2007-02-02 21:06:43 +01:00
|
|
|
/* This is used by inet_addr, but apparently Solaris doesn't define it
|
|
|
|
* anyplace. */
|
2004-12-22 06:29:06 +01:00
|
|
|
#define INADDR_NONE ((unsigned long) -1)
|
|
|
|
#endif
|
|
|
|
|
2006-08-04 20:32:43 +02:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
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;
|
|
|
|
int page_size;
|
2008-12-05 03:17:41 +01:00
|
|
|
tor_mmap_t *res;
|
2006-10-20 01:05:02 +02:00
|
|
|
size_t size, filesize;
|
2006-05-28 18:54:39 +02:00
|
|
|
|
|
|
|
tor_assert(filename);
|
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY, 0);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-11-14 02:07:52 +01:00
|
|
|
size = filesize = (size_t) lseek(fd, 0, SEEK_END);
|
2006-05-28 18:54:39 +02:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
/* 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
|
|
|
|
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
|
|
|
}
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Release storage held for a memory mapping. */
|
2006-08-04 20:32:43 +02:00
|
|
|
void
|
|
|
|
tor_munmap_file(tor_mmap_t *handle)
|
|
|
|
{
|
2008-12-05 03:17:41 +01:00
|
|
|
munmap((char*)handle->data, handle->mapping_size);
|
|
|
|
tor_free(handle);
|
2006-05-28 18:54:39 +02:00
|
|
|
}
|
2006-08-04 20:32:43 +02:00
|
|
|
#elif defined(MS_WINDOWS)
|
|
|
|
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
|
|
|
{
|
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;
|
2006-12-07 15:39:42 +01:00
|
|
|
res->file_handle = INVALID_HANDLE_VALUE;
|
|
|
|
res->mmap_handle = NULL;
|
2006-08-04 20:32:43 +02:00
|
|
|
|
2006-09-06 03:49:55 +02:00
|
|
|
res->file_handle = CreateFile(filename,
|
2007-05-09 23:22:56 +02:00
|
|
|
GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
NULL,
|
2006-09-06 10:42:16 +02:00
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
0);
|
2006-09-06 03:49:55 +02:00
|
|
|
|
|
|
|
if (res->file_handle == INVALID_HANDLE_VALUE)
|
2007-02-12 22:39:44 +01:00
|
|
|
goto win_err;
|
2006-09-06 03:49:55 +02:00
|
|
|
|
2008-12-05 03:17:41 +01:00
|
|
|
res->size = GetFileSize(res->file_handle, NULL);
|
2006-08-04 20:32:43 +02:00
|
|
|
|
2008-12-05 03:17:41 +01:00
|
|
|
if (res->size == 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;
|
|
|
|
}
|
|
|
|
|
2006-08-04 20:32:43 +02:00
|
|
|
res->mmap_handle = CreateFileMapping(res->file_handle,
|
|
|
|
NULL,
|
|
|
|
PAGE_READONLY,
|
2006-09-28 02:53:02 +02:00
|
|
|
#if SIZEOF_SIZE_T > 4
|
2006-09-06 10:42:16 +02:00
|
|
|
(res->base.size >> 32),
|
2006-09-28 02:53:02 +02:00
|
|
|
#else
|
|
|
|
0,
|
|
|
|
#endif
|
2008-12-05 20:36:35 +01:00
|
|
|
(res->size & 0xfffffffful),
|
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
|
|
|
|
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;
|
2008-12-05 03:17:41 +01:00
|
|
|
tor_munmap_file(res);
|
2006-08-04 20:32:43 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-05-28 18:54:39 +02:00
|
|
|
void
|
2006-08-04 20:32:43 +02:00
|
|
|
tor_munmap_file(tor_mmap_t *handle)
|
2006-05-28 18:54:39 +02:00
|
|
|
{
|
2006-08-04 20:32:43 +02: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. */
|
2006-09-06 03:49:55 +02:00
|
|
|
UnmapViewOfFile( (LPVOID) handle->data);
|
|
|
|
|
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
|
|
|
if (handle->file_handle != INVALID_HANDLE_VALUE)
|
2008-12-05 20:36:35 +01:00
|
|
|
CloseHandle(handle->file_handle);
|
2008-12-05 03:17:41 +01:00
|
|
|
tor_free(handle);
|
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
|
|
|
}
|
|
|
|
void
|
2006-08-04 20:32:43 +02:00
|
|
|
tor_munmap_file(tor_mmap_t *handle)
|
2006-05-28 18:54:39 +02:00
|
|
|
{
|
2006-08-05 19:53:08 +02:00
|
|
|
char *d = (char*)handle->data;
|
|
|
|
tor_free(d);
|
2007-02-20 23:25:20 +01:00
|
|
|
memset(handle, 0, sizeof(tor_mmap_t));
|
2006-08-05 19:53:08 +02:00
|
|
|
tor_free(handle);
|
2006-05-28 18:54:39 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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 */
|
2008-12-11 22:11:22 +01:00
|
|
|
if (size > SSIZE_T_MAX-16)
|
2004-12-02 05:33:01 +01:00
|
|
|
return -1;
|
2004-11-01 21:41:47 +01:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* Requires that nlen be greater than zero.
|
|
|
|
*/
|
|
|
|
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. */
|
2005-06-28 01:35:04 +02:00
|
|
|
const char *p, *end;
|
|
|
|
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);
|
|
|
|
|
|
|
|
p = haystack;
|
|
|
|
end = haystack + hlen;
|
|
|
|
first = *(const char*)needle;
|
|
|
|
while ((p = memchr(p, first, end-p))) {
|
2005-08-22 02:34:23 +02:00
|
|
|
if (p+nlen > end)
|
2005-06-18 04:17:11 +02:00
|
|
|
return NULL;
|
|
|
|
if (!memcmp(p, needle, nlen))
|
|
|
|
return p;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-10-25 09:05:03 +02:00
|
|
|
#ifdef MS_WINDOWS
|
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
|
|
|
|
* compiled the binary in their warrning messages.
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
2005-10-25 09:05:03 +02:00
|
|
|
#endif
|
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
|
|
|
|
get_uint16(const char *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
|
|
|
|
get_uint32(const char *cp)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
uint32_t v;
|
|
|
|
memcpy(&v,cp,4);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Set a 16-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
|
|
|
|
* *(uint16_t)(cp) = v, but will not cause segfaults on platforms that forbid
|
|
|
|
* unaligned memory access. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
set_uint16(char *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
|
|
|
|
* *(uint32_t)(cp) = v, but will not cause segfaults on platforms that forbid
|
|
|
|
* unaligned memory access. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
set_uint32(char *cp, uint32_t v)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
memcpy(cp,&v,4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-12 06:33:26 +02:00
|
|
|
* Rename the file <b>from</b> to the file <b>to</b>. On unix, this is
|
|
|
|
* 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
|
|
|
{
|
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
return rename(from,to);
|
|
|
|
#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:
|
|
|
|
if (unlink(to)) return -1;
|
|
|
|
break;
|
|
|
|
case FN_ERROR:
|
|
|
|
return -1;
|
|
|
|
case FN_DIR:
|
|
|
|
errno = EISDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return rename(from,to);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
char *filename;
|
|
|
|
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
|
|
|
|
* to do in-process locking, use tor_mutex_t like a normal person.)
|
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);
|
|
|
|
fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0600);
|
|
|
|
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
|
|
|
|
2008-09-01 22:06:26 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
_lseek(fd, 0, SEEK_SET);
|
2008-11-11 16:29:40 +01:00
|
|
|
if (_locking(fd, blocking ? _LK_LOCK : _LK_NBLCK, 1) < 0) {
|
2008-09-01 22:06:26 +02:00
|
|
|
if (errno != EDEADLOCK)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2008-09-01 22:06:26 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
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);
|
|
|
|
#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. */
|
2008-09-01 22:06:26 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
close(lockfile->fd);
|
|
|
|
lockfile->fd = -1;
|
|
|
|
tor_free(lockfile->filename);
|
|
|
|
tor_free(lockfile);
|
|
|
|
}
|
|
|
|
|
2008-12-03 00:26:04 +01:00
|
|
|
/* Some old versions of unix didn't define constants for these values,
|
|
|
|
* and instead expect you to say 0, 1, or 2. */
|
|
|
|
#ifndef SEEK_CUR
|
|
|
|
#define SEEK_CUR 1
|
|
|
|
#endif
|
|
|
|
#ifndef SEEK_END
|
|
|
|
#define SEEK_END 2
|
|
|
|
#endif
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
return (off_t) _lseek(fd, 0, SEEK_CUR);
|
|
|
|
#else
|
|
|
|
return (off_t) lseek(fd, 0, SEEK_CUR);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-12-03 00:42:21 +01:00
|
|
|
/** Move <b>fd</b> to the end of the file. Return -1 on error, 0 on success. */
|
2008-12-03 00:26:04 +01:00
|
|
|
int
|
|
|
|
tor_fd_seekend(int fd)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
return _lseek(fd, 0, SEEK_END) < 0 ? -1 : 0;
|
|
|
|
#else
|
|
|
|
return lseek(fd, 0, SEEK_END) < 0 ? -1 : 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;
|
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/** As close(), but guaranteed to work for sockets across platforms (including
|
2008-02-19 20:30:41 +01:00
|
|
|
* Windows, where close()ing a socket doesn't work. Returns 0 on success, -1
|
|
|
|
* on failure. */
|
|
|
|
int
|
2007-04-21 19:24:18 +02:00
|
|
|
tor_close_socket(int s)
|
|
|
|
{
|
2008-02-19 20:30:41 +01:00
|
|
|
int r = 0;
|
2008-02-19 23:46:19 +01:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
#endif
|
2007-04-21 19:24:18 +02:00
|
|
|
/* On Windows, you have to call close() on fds returned by open(),
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#ifdef USE_BSOCKETS
|
2008-02-19 20:30:41 +01:00
|
|
|
r = bclose(s);
|
2007-04-21 19:24:18 +02:00
|
|
|
#elif defined(MS_WINDOWS)
|
2008-02-19 20:30:41 +01:00
|
|
|
r = closesocket(s);
|
2007-04-21 19:24:18 +02:00
|
|
|
#else
|
2008-02-19 20:30:41 +01:00
|
|
|
r = close(s);
|
2007-04-21 19:24:18 +02:00
|
|
|
#endif
|
2008-02-19 20:30:41 +01:00
|
|
|
if (r == 0) {
|
|
|
|
--n_sockets_open;
|
|
|
|
} else {
|
|
|
|
int err = tor_socket_errno(-1);
|
|
|
|
log_info(LD_NET, "Close returned an error: %s", tor_socket_strerror(err));
|
|
|
|
#ifdef WIN32
|
|
|
|
if (err != WSAENOTSOCK)
|
|
|
|
--n_sockets_open;
|
|
|
|
#else
|
|
|
|
if (err != EBADF)
|
|
|
|
--n_sockets_open;
|
|
|
|
#endif
|
|
|
|
r = -1;
|
|
|
|
}
|
2008-02-19 20:48:07 +01:00
|
|
|
if (n_sockets_open < 0)
|
|
|
|
log_warn(LD_BUG, "Our socket count is below zero: %d. Please submit a "
|
|
|
|
"bug report.", n_sockets_open);
|
2008-02-19 20:30:41 +01:00
|
|
|
return r;
|
2007-04-21 19:24:18 +02: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. */
|
2008-02-20 00:01:07 +01:00
|
|
|
static INLINE void
|
|
|
|
mark_socket_open(int s)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define mark_socket_open(s) STMT_NIL
|
|
|
|
#endif
|
|
|
|
|
2007-04-21 19:24:18 +02:00
|
|
|
/** As socket(), but counts the number of open sockets. */
|
|
|
|
int
|
|
|
|
tor_open_socket(int domain, int type, int protocol)
|
|
|
|
{
|
|
|
|
int s = socket(domain, type, protocol);
|
2008-02-19 23:46:19 +01:00
|
|
|
if (s >= 0) {
|
2007-04-21 19:24:18 +02:00
|
|
|
++n_sockets_open;
|
2008-02-20 00:01:07 +01:00
|
|
|
mark_socket_open(s);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** As socket(), but counts the number of open sockets. */
|
|
|
|
int
|
|
|
|
tor_accept_socket(int sockfd, struct sockaddr *addr, socklen_t *len)
|
|
|
|
{
|
|
|
|
int s = accept(sockfd, addr, len);
|
|
|
|
if (s >= 0) {
|
|
|
|
++n_sockets_open;
|
|
|
|
mark_socket_open(s);
|
2008-02-19 23:46:19 +01:00
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
return n_sockets_open;
|
|
|
|
}
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/** Turn <b>socket</b> into a nonblocking socket.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
set_socket_nonblocking(int socket)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
2006-09-19 22:41:31 +02:00
|
|
|
#if defined(MS_WINDOWS) && !defined(USE_BSOCKETS)
|
2006-09-06 10:42:16 +02:00
|
|
|
unsigned long nonblocking = 1;
|
|
|
|
ioctlsocket(socket, FIONBIO, (unsigned long*) &nonblocking);
|
2004-11-01 21:41:47 +01:00
|
|
|
#else
|
|
|
|
fcntl(socket, F_SETFL, O_NONBLOCK);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
tor_socketpair(int family, int type, int protocol, int fd[2])
|
|
|
|
{
|
2006-09-06 03:49:55 +02:00
|
|
|
//don't use win32 socketpairs (they are always bad)
|
|
|
|
#if defined(HAVE_SOCKETPAIR) && !defined(MS_WINDOWS)
|
2005-06-30 09:17:38 +02:00
|
|
|
int r;
|
|
|
|
r = socketpair(family, type, protocol, fd);
|
2008-02-20 00:46:08 +01:00
|
|
|
if (r == 0) {
|
|
|
|
if (fd[0] >= 0) {
|
|
|
|
++n_sockets_open;
|
|
|
|
mark_socket_open(fd[0]);
|
|
|
|
}
|
|
|
|
if (fd[1] >= 0) {
|
|
|
|
++n_sockets_open;
|
|
|
|
mark_socket_open(fd[1]);
|
|
|
|
}
|
|
|
|
}
|
2005-06-30 09:17:38 +02:00
|
|
|
return r < 0 ? -errno : r;
|
2006-09-19 22:41:31 +02:00
|
|
|
#elif defined(USE_BSOCKETS)
|
2007-03-15 23:47:21 +01:00
|
|
|
return bsocketpair(family, type, protocol, fd);
|
2004-11-01 21:41:47 +01:00
|
|
|
#else
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
int listener = -1;
|
|
|
|
int connector = -1;
|
|
|
|
int acceptor = -1;
|
|
|
|
struct sockaddr_in listen_addr;
|
|
|
|
struct sockaddr_in connect_addr;
|
|
|
|
int size;
|
2005-06-30 09:17:38 +02:00
|
|
|
int saved_errno = -1;
|
2004-11-01 21:41:47 +01:00
|
|
|
|
|
|
|
if (protocol
|
|
|
|
#ifdef AF_UNIX
|
|
|
|
|| family != AF_UNIX
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
#ifdef MS_WINDOWS
|
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
|
|
|
}
|
|
|
|
|
2007-04-21 19:24:18 +02:00
|
|
|
listener = tor_open_socket(AF_INET, type, 0);
|
2006-06-05 11:08:10 +02:00
|
|
|
if (listener < 0)
|
2005-06-30 09:17:38 +02:00
|
|
|
return -tor_socket_errno(-1);
|
2004-11-23 00:28:26 +01:00
|
|
|
memset(&listen_addr, 0, sizeof(listen_addr));
|
2004-11-01 21:41:47 +01:00
|
|
|
listen_addr.sin_family = AF_INET;
|
2004-11-23 00:28:26 +01:00
|
|
|
listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
2004-12-01 04:48:14 +01:00
|
|
|
listen_addr.sin_port = 0; /* kernel chooses port. */
|
2004-11-01 21:41:47 +01:00
|
|
|
if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr))
|
|
|
|
== -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
|
|
|
|
2007-04-21 19:24:18 +02:00
|
|
|
connector = tor_open_socket(AF_INET, type, 0);
|
2006-06-05 11:08:10 +02:00
|
|
|
if (connector < 0)
|
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. */
|
2004-11-23 00:28:26 +01:00
|
|
|
size = sizeof(connect_addr);
|
2004-11-01 21:41:47 +01:00
|
|
|
if (getsockname(listener, (struct sockaddr *) &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
|
|
|
if (size != sizeof (connect_addr))
|
2005-06-30 09:17:38 +02:00
|
|
|
goto abort_tidy_up_and_fail;
|
2004-11-01 21:41:47 +01:00
|
|
|
if (connect(connector, (struct sockaddr *) &connect_addr,
|
2004-11-23 00:28:26 +01:00
|
|
|
sizeof(connect_addr)) == -1)
|
2005-06-30 09:17:38 +02:00
|
|
|
goto tidy_up_and_fail;
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2004-11-23 00:28:26 +01:00
|
|
|
size = sizeof(listen_addr);
|
2008-02-20 00:01:07 +01:00
|
|
|
acceptor = tor_accept_socket(listener,
|
|
|
|
(struct sockaddr *) &listen_addr, &size);
|
2006-06-05 11:08:10 +02:00
|
|
|
if (acceptor < 0)
|
2005-06-30 09:17:38 +02:00
|
|
|
goto tidy_up_and_fail;
|
2004-11-01 21:41:47 +01:00
|
|
|
if (size != sizeof(listen_addr))
|
2005-06-30 09:17:38 +02:00
|
|
|
goto abort_tidy_up_and_fail;
|
2004-11-01 21:41:47 +01:00
|
|
|
tor_close_socket(listener);
|
|
|
|
/* Now check we are talking to ourself by matching port and host on the
|
|
|
|
two sockets. */
|
|
|
|
if (getsockname(connector, (struct sockaddr *) &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
|
|
|
if (size != sizeof (connect_addr)
|
|
|
|
|| listen_addr.sin_family != connect_addr.sin_family
|
|
|
|
|| listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
|
|
|
|
|| listen_addr.sin_port != connect_addr.sin_port) {
|
2005-06-30 09:17:38 +02:00
|
|
|
goto abort_tidy_up_and_fail;
|
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:
|
|
|
|
#ifdef MS_WINDOWS
|
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;
|
|
|
|
if (listener != -1)
|
|
|
|
tor_close_socket(listener);
|
|
|
|
if (connector != -1)
|
|
|
|
tor_close_socket(connector);
|
|
|
|
if (acceptor != -1)
|
|
|
|
tor_close_socket(acceptor);
|
|
|
|
return -saved_errno;
|
2004-11-01 21:41:47 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-02-23 00:52:44 +01:00
|
|
|
#define ULIMIT_BUFFER 32 /* keep 32 extra fd's beyond _ConnLimit */
|
|
|
|
|
2006-02-09 03:59:37 +01:00
|
|
|
/** Learn the maximum allowed number of file descriptors. (Some systems
|
|
|
|
* have a low soft limit.
|
|
|
|
*
|
2008-02-20 23:28:26 +01:00
|
|
|
* We compute this by finding the largest number that we can use.
|
|
|
|
* If we can't find a number greater than or equal to <b>limit</b>,
|
|
|
|
* then we fail: return -1.
|
2006-02-09 03:59:37 +01:00
|
|
|
*
|
2008-02-20 23:28:26 +01:00
|
|
|
* Otherwise, return 0 and store the maximum we found inside <b>max_out</b>.*/
|
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
|
|
|
{
|
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;
|
|
|
|
#elif defined(IPHONE)
|
|
|
|
const char *platform = "iPhone";
|
|
|
|
const unsigned long MAX_CONNECTIONS = 9999;
|
|
|
|
#elif defined(MS_WINDOWS)
|
|
|
|
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;
|
|
|
|
#endif
|
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;
|
|
|
|
#else /* HAVE_GETRLIMIT */
|
2004-11-01 21:41:47 +01:00
|
|
|
struct rlimit rlim;
|
2005-03-14 04:28:46 +01:00
|
|
|
tor_assert(limit > 0);
|
2004-11-01 21:41:47 +01:00
|
|
|
|
|
|
|
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-20 23:28:26 +01:00
|
|
|
|
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
|
|
|
}
|
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) {
|
2007-11-02 17:02:26 +01:00
|
|
|
int bad = 1;
|
|
|
|
#ifdef OPEN_MAX
|
|
|
|
if (errno == EINVAL && OPEN_MAX < rlim.rlim_cur) {
|
|
|
|
/* On some platforms, OPEN_MAX is the real limit, and getrlimit() is
|
|
|
|
* full of nasty lies. I'm looking at you, OSX 10.5.... */
|
|
|
|
rlim.rlim_cur = OPEN_MAX;
|
|
|
|
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 "
|
|
|
|
"OPEN_MAX, and ConnLimit is %lu. Changing ConnLimit; sorry.",
|
2008-02-21 22:15:31 +01:00
|
|
|
(unsigned long)OPEN_MAX, (unsigned long)limit);
|
2007-11-02 17:02:26 +01:00
|
|
|
} else {
|
|
|
|
log_info(LD_CONFIG, "Dropped connection limit to OPEN_MAX (%lu); "
|
|
|
|
"Apparently, %lu was too high and rlimit lied to us.",
|
2008-02-20 23:28:26 +01:00
|
|
|
(unsigned long)OPEN_MAX, (unsigned long)rlim.rlim_max);
|
2007-11-02 17:02:26 +01:00
|
|
|
}
|
|
|
|
bad = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-03-18 16:11:52 +01:00
|
|
|
#endif /* OPEN_MAX */
|
2007-11-02 17:02:26 +01:00
|
|
|
if (bad) {
|
2007-11-02 18:16:34 +01:00
|
|
|
log_warn(LD_CONFIG,"Couldn't set maximum number of file descriptors: %s",
|
2007-11-02 17:02:26 +01:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
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;
|
2008-03-18 16:11:52 +01:00
|
|
|
#endif /* HAVE_GETRLIMIT */
|
2005-02-23 00:52:44 +01:00
|
|
|
|
|
|
|
if (limit < ULIMIT_BUFFER) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"ConnLimit must be at least %d. Failing.", ULIMIT_BUFFER);
|
2005-02-23 00:52:44 +01:00
|
|
|
return -1;
|
|
|
|
}
|
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);
|
|
|
|
*max_out = (int)limit - ULIMIT_BUFFER;
|
|
|
|
return 0;
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
|
|
|
|
2008-12-08 20:52:26 +01:00
|
|
|
#ifndef MS_WINDOWS
|
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
|
|
|
{
|
|
|
|
#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 */
|
|
|
|
gid_t sup_gids[NGROUPS_MAX + 1];
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* 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);
|
2008-11-07 03:06:12 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* 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);
|
2008-11-07 03:06:12 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* log supplementary groups */
|
2008-11-07 03:53:46 +01:00
|
|
|
if ((ngids = getgroups(NGROUPS_MAX + 1, sup_gids)) < 0) {
|
|
|
|
log_warn(LD_GENERAL, "Error getting supplementary GIDs: %s",
|
|
|
|
strerror(errno));
|
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 *strgid;
|
|
|
|
char *s = NULL;
|
|
|
|
smartlist_t *elts = smartlist_create();
|
|
|
|
|
|
|
|
for (i = 0; i<ngids; i++) {
|
|
|
|
strgid = tor_malloc(11);
|
2008-12-05 02:29:59 +01:00
|
|
|
if (tor_snprintf(strgid, 11, "%u", (unsigned)sup_gids[i]) < 0) {
|
2008-11-07 03:06:12 +01:00
|
|
|
log_warn(LD_GENERAL, "Error printing supplementary GIDs");
|
2008-12-05 02:29:59 +01:00
|
|
|
tor_free(strgid);
|
|
|
|
retval = -1;
|
2008-11-07 03:06:12 +01:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
smartlist_add(elts, strgid);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
error:
|
|
|
|
tor_free(s);
|
|
|
|
SMARTLIST_FOREACH(elts, char *, cp,
|
|
|
|
{
|
|
|
|
tor_free(cp);
|
|
|
|
});
|
|
|
|
smartlist_free(elts);
|
|
|
|
|
2008-12-05 02:29:59 +01:00
|
|
|
return retval;
|
2008-11-07 03:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-12-08 20:52:26 +01:00
|
|
|
#endif
|
2008-11-07 03:06:12 +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.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
switch_id(const char *user)
|
2005-09-30 03:09:52 +02:00
|
|
|
{
|
2004-11-01 21:41:47 +01:00
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
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;
|
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. */
|
2008-11-07 05:11:03 +01:00
|
|
|
pw = getpwnam(user);
|
|
|
|
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
|
|
|
|
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. */
|
|
|
|
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
}
|
2008-11-07 03:06:12 +01:00
|
|
|
#endif
|
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 */
|
2004-11-01 21:41:47 +01:00
|
|
|
return 0;
|
2008-11-07 03:06:12 +01:00
|
|
|
|
2007-02-28 21:24:27 +01:00
|
|
|
#else
|
|
|
|
(void)user;
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2006-03-26 08:47:08 +02:00
|
|
|
log_warn(LD_CONFIG,
|
2008-11-07 03:06:12 +01:00
|
|
|
"User specified but switching users is unsupported on your OS.");
|
2004-11-01 21:41:47 +01:00
|
|
|
return -1;
|
2008-11-07 05:35:41 +01:00
|
|
|
#endif
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
tor_assert(username);
|
|
|
|
|
|
|
|
if (!(pw = 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);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/** Set *addr to the IP address (in dotted-quad notation) stored in c.
|
|
|
|
* Return 1 on success, 0 if c is badly formatted. (Like inet_aton(c,addr),
|
|
|
|
* but works on Windows and Solaris.)
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
tor_inet_aton(const char *c, struct in_addr* addr)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
#ifdef HAVE_INET_ATON
|
|
|
|
return inet_aton(c, addr);
|
|
|
|
#else
|
|
|
|
uint32_t r;
|
|
|
|
tor_assert(c);
|
|
|
|
tor_assert(addr);
|
|
|
|
if (strcmp(c, "255.255.255.255") == 0) {
|
|
|
|
addr->s_addr = 0xFFFFFFFFu;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
r = inet_addr(c);
|
|
|
|
if (r == INADDR_NONE)
|
|
|
|
return 0;
|
|
|
|
addr->s_addr = r;
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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]);
|
|
|
|
}
|
2007-05-25 20:22:37 +02:00
|
|
|
if (strlen(buf) > len)
|
|
|
|
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';
|
2007-05-25 20:22:37 +02:00
|
|
|
if (strlen(buf) > len)
|
|
|
|
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. */
|
|
|
|
if (dot == src)
|
|
|
|
return 0;
|
|
|
|
else if (!dot)
|
|
|
|
eow = src+strlen(src);
|
|
|
|
else {
|
2007-05-31 20:48:25 +02:00
|
|
|
int byte1,byte2,byte3,byte4;
|
|
|
|
char more;
|
2007-05-25 20:22:37 +02:00
|
|
|
for (eow = dot-1; eow >= src && TOR_ISDIGIT(*eow); --eow)
|
|
|
|
;
|
|
|
|
++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" */
|
|
|
|
if (sscanf(eow, "%d.%d.%d.%d%c", &byte1,&byte2,&byte3,&byte4,&more) != 4)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (byte1 > 255 || byte1 < 0 ||
|
|
|
|
byte2 > 255 || byte2 < 0 ||
|
|
|
|
byte3 > 255 || byte3 < 0 ||
|
|
|
|
byte4 > 255 || byte4 < 0)
|
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;
|
2008-02-21 22:57:47 +01:00
|
|
|
long r = strtol(src, &next, 16);
|
2007-05-25 20:22:37 +02:00
|
|
|
if (next > 4+src)
|
|
|
|
return 0;
|
|
|
|
if (next == src)
|
|
|
|
return 0;
|
|
|
|
if (r<0 || r>65536)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
} else if (*src == ':' && i > 0 && gapPos==-1) {
|
|
|
|
gapPos = i;
|
|
|
|
++src;
|
2007-07-20 18:25:27 +02:00
|
|
|
} else if (*src == ':' && i == 0 && 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.)
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
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
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
get_uname(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_UNAME
|
|
|
|
struct utsname u;
|
|
|
|
#endif
|
|
|
|
if (!uname_result_is_set) {
|
|
|
|
#ifdef HAVE_UNAME
|
|
|
|
if (uname(&u) != -1) {
|
|
|
|
/* (linux says 0 is success, solaris says 1 is success) */
|
2005-04-06 23:11:05 +02:00
|
|
|
tor_snprintf(uname_result, sizeof(uname_result), "%s %s",
|
|
|
|
u.sysname, u.machine);
|
2004-11-01 21:41:47 +01:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2005-01-20 00:10:16 +01:00
|
|
|
#ifdef MS_WINDOWS
|
2005-10-06 00:04:45 +02:00
|
|
|
OSVERSIONINFOEX info;
|
2005-01-20 00:10:16 +01:00
|
|
|
int i;
|
2005-10-24 20:37:09 +02:00
|
|
|
unsigned int leftover_mask;
|
2005-01-20 00:10:16 +01:00
|
|
|
const char *plat = NULL;
|
2007-04-30 03:27:47 +02:00
|
|
|
const char *extra = NULL;
|
2005-01-20 00:10:16 +01:00
|
|
|
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[] = {
|
2005-10-06 00:04:45 +02:00
|
|
|
{ 6, 0, "Windows \"Longhorn\"" },
|
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-07-10 00:29:12 +02:00
|
|
|
#ifdef VER_SUITE_BACKOFFICE
|
2005-10-24 20:37:09 +02:00
|
|
|
static struct {
|
|
|
|
unsigned int mask; const char *str;
|
|
|
|
} win_mask_table[] = {
|
|
|
|
{ VER_SUITE_BACKOFFICE, " {backoffice}" },
|
|
|
|
{ VER_SUITE_BLADE, " {\"blade\" (2003, web edition)}" },
|
|
|
|
{ VER_SUITE_DATACENTER, " {datacenter}" },
|
|
|
|
{ VER_SUITE_ENTERPRISE, " {enterprise}" },
|
|
|
|
{ VER_SUITE_EMBEDDEDNT, " {embedded}" },
|
|
|
|
{ VER_SUITE_PERSONAL, " {personal}" },
|
2005-12-14 21:40:40 +01:00
|
|
|
{ VER_SUITE_SINGLEUSERTS,
|
|
|
|
" {terminal services, single user}" },
|
2005-10-24 20:37:09 +02:00
|
|
|
{ VER_SUITE_SMALLBUSINESS, " {small business}" },
|
2005-12-14 21:40:40 +01:00
|
|
|
{ VER_SUITE_SMALLBUSINESS_RESTRICTED,
|
|
|
|
" {small business, restricted}" },
|
2005-10-24 20:37:09 +02:00
|
|
|
{ VER_SUITE_TERMINAL, " {terminal services}" },
|
|
|
|
{ 0, NULL },
|
|
|
|
};
|
2006-07-10 00:29:12 +02:00
|
|
|
#endif
|
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);
|
2006-03-24 06:26:33 +01:00
|
|
|
if (! GetVersionEx((LPOSVERSIONINFO)&info)) {
|
|
|
|
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";
|
2007-04-30 03:27:47 +02:00
|
|
|
if (info.szCSDVersion[1] == 'B')
|
|
|
|
extra = "OSR2 (B)";
|
|
|
|
else if (info.szCSDVersion[1] == 'C')
|
|
|
|
extra = "OSR2 (C)";
|
2005-01-20 00:10:16 +01:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-30 03:27:47 +02:00
|
|
|
if (plat && !strcmp(plat, "Windows 98")) {
|
|
|
|
if (info.szCSDVersion[1] == 'A')
|
|
|
|
extra = "SE (A)";
|
|
|
|
else if (info.szCSDVersion[1] == 'B')
|
|
|
|
extra = "SE (B)";
|
|
|
|
}
|
2005-01-20 00:10:16 +01:00
|
|
|
if (plat) {
|
2007-04-30 03:27:47 +02:00
|
|
|
if (!extra)
|
|
|
|
extra = info.szCSDVersion;
|
2005-10-07 21:33:47 +02:00
|
|
|
tor_snprintf(uname_result, sizeof(uname_result), "%s %s",
|
2007-04-30 03:27:47 +02:00
|
|
|
plat, extra);
|
2005-01-20 00:10:16 +01:00
|
|
|
} else {
|
2005-10-06 00:04:45 +02:00
|
|
|
if (info.dwMajorVersion > 6 ||
|
|
|
|
(info.dwMajorVersion==6 && info.dwMinorVersion>0))
|
2005-01-20 20:03:09 +01:00
|
|
|
tor_snprintf(uname_result, sizeof(uname_result),
|
2005-12-14 21:40:40 +01:00
|
|
|
"Very recent version of Windows [major=%d,minor=%d] %s",
|
|
|
|
(int)info.dwMajorVersion,(int)info.dwMinorVersion,
|
|
|
|
info.szCSDVersion);
|
2005-01-20 00:10:16 +01:00
|
|
|
else
|
2005-01-20 20:03:09 +01:00
|
|
|
tor_snprintf(uname_result, sizeof(uname_result),
|
2005-12-14 21:40:40 +01:00
|
|
|
"Unrecognized version of Windows [major=%d,minor=%d] %s",
|
|
|
|
(int)info.dwMajorVersion,(int)info.dwMinorVersion,
|
|
|
|
info.szCSDVersion);
|
2005-01-20 00:10:16 +01:00
|
|
|
}
|
2006-07-10 00:29:12 +02:00
|
|
|
#ifdef VER_SUITE_BACKOFFICE
|
2005-10-07 21:33:47 +02:00
|
|
|
if (info.wProductType == VER_NT_DOMAIN_CONTROLLER) {
|
2005-10-06 00:04:45 +02:00
|
|
|
strlcat(uname_result, " [domain controller]", sizeof(uname_result));
|
2005-10-07 21:33:47 +02:00
|
|
|
} else if (info.wProductType == VER_NT_SERVER) {
|
2005-10-06 00:04:45 +02:00
|
|
|
strlcat(uname_result, " [server]", sizeof(uname_result));
|
2005-10-07 21:33:47 +02:00
|
|
|
} else if (info.wProductType == VER_NT_WORKSTATION) {
|
2005-10-06 00:04:45 +02:00
|
|
|
strlcat(uname_result, " [workstation]", sizeof(uname_result));
|
|
|
|
}
|
2005-10-24 20:37:09 +02:00
|
|
|
leftover_mask = info.wSuiteMask;
|
|
|
|
for (i = 0; win_mask_table[i].mask; ++i) {
|
2005-11-15 04:05:23 +01:00
|
|
|
if (info.wSuiteMask & win_mask_table[i].mask) {
|
2005-10-24 20:37:09 +02:00
|
|
|
strlcat(uname_result, win_mask_table[i].str, sizeof(uname_result));
|
|
|
|
leftover_mask &= ~win_mask_table[i].mask;
|
|
|
|
}
|
2005-10-06 00:04:45 +02:00
|
|
|
}
|
2005-10-24 20:37:09 +02:00
|
|
|
if (leftover_mask) {
|
|
|
|
size_t len = strlen(uname_result);
|
|
|
|
tor_snprintf(uname_result+len, sizeof(uname_result)-len,
|
|
|
|
" {0x%x}", info.wSuiteMask);
|
2005-10-06 00:04:45 +02:00
|
|
|
}
|
2006-07-10 00:29:12 +02:00
|
|
|
#endif
|
2005-01-20 00:10:16 +01:00
|
|
|
#else
|
2004-11-01 21:41:47 +01:00
|
|
|
strlcpy(uname_result, "Unknown platform", sizeof(uname_result));
|
2005-01-20 00:10:16 +01:00
|
|
|
#endif
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
|
|
|
uname_result_is_set = 1;
|
|
|
|
}
|
|
|
|
return uname_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process control
|
|
|
|
*/
|
|
|
|
|
2005-01-27 23:34:48 +01:00
|
|
|
#if defined(USE_PTHREADS)
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Wraps a void (*)(void*) function and its argument so we can
|
2005-10-06 06:33:40 +02:00
|
|
|
* invoke them in a way pthreads would expect.
|
|
|
|
*/
|
2005-07-22 23:12:10 +02:00
|
|
|
typedef struct tor_pthread_data_t {
|
2006-09-06 10:42:20 +02:00
|
|
|
void (*func)(void *);
|
2005-01-27 23:34:48 +01:00
|
|
|
void *data;
|
2005-07-22 23:12:10 +02:00
|
|
|
} tor_pthread_data_t;
|
2007-02-26 06:36:02 +01:00
|
|
|
/** Given a tor_pthread_data_t <b>_data</b>, call _data->func(d->data)
|
2007-02-24 08:50:38 +01:00
|
|
|
* and free _data. Used to make sure we can call functions the way pthread
|
2007-02-16 21:01:02 +01:00
|
|
|
* expects. */
|
2005-01-27 23:34:48 +01:00
|
|
|
static void *
|
|
|
|
tor_pthread_helper_fn(void *_data)
|
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
tor_pthread_data_t *data = _data;
|
2006-09-06 10:42:20 +02:00
|
|
|
void (*func)(void*);
|
2005-01-27 23:34:48 +01:00
|
|
|
void *arg;
|
2007-02-20 19:34:18 +01:00
|
|
|
/* mask signals to worker threads to avoid SIGPIPE, etc */
|
|
|
|
sigset_t sigs;
|
|
|
|
/* We're in a subthread; don't handle any signals here. */
|
|
|
|
sigfillset(&sigs);
|
|
|
|
pthread_sigmask(SIG_SETMASK, &sigs, NULL);
|
|
|
|
|
2005-01-27 23:34:48 +01:00
|
|
|
func = data->func;
|
|
|
|
arg = data->data;
|
|
|
|
tor_free(_data);
|
|
|
|
func(arg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/** Minimalist interface to run a void function in the background. On
|
|
|
|
* unix calls fork, on win32 calls beginthread. Returns -1 on failure.
|
|
|
|
* func should not return, but rather should call spawn_exit.
|
2005-01-03 20:07:25 +01:00
|
|
|
*
|
|
|
|
* NOTE: if <b>data</b> is used, it should not be allocated on the stack,
|
|
|
|
* since in a multithreaded environment, there is no way to be sure that
|
|
|
|
* the caller's stack will still be around when the called function is
|
|
|
|
* running.
|
2004-11-01 21:41:47 +01:00
|
|
|
*/
|
2004-11-23 00:28:26 +01:00
|
|
|
int
|
2006-09-06 03:49:55 +02:00
|
|
|
spawn_func(void (*func)(void *), void *data)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
2005-01-27 23:34:48 +01:00
|
|
|
#if defined(USE_WIN32_THREADS)
|
2004-11-01 21:41:47 +01:00
|
|
|
int rv;
|
2007-02-28 21:24:27 +01:00
|
|
|
rv = (int)_beginthread(func, 0, data);
|
|
|
|
if (rv == (int)-1)
|
2004-11-01 21:41:47 +01:00
|
|
|
return -1;
|
|
|
|
return 0;
|
2005-01-27 23:34:48 +01:00
|
|
|
#elif defined(USE_PTHREADS)
|
|
|
|
pthread_t thread;
|
2005-07-22 23:12:10 +02:00
|
|
|
tor_pthread_data_t *d;
|
|
|
|
d = tor_malloc(sizeof(tor_pthread_data_t));
|
2005-01-27 23:34:48 +01:00
|
|
|
d->data = data;
|
|
|
|
d->func = func;
|
|
|
|
if (pthread_create(&thread,NULL,tor_pthread_helper_fn,d))
|
|
|
|
return -1;
|
|
|
|
if (pthread_detach(thread))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2004-11-01 21:41:47 +01:00
|
|
|
#else
|
|
|
|
pid_t pid;
|
|
|
|
pid = fork();
|
|
|
|
if (pid<0)
|
|
|
|
return -1;
|
|
|
|
if (pid==0) {
|
|
|
|
/* Child */
|
|
|
|
func(data);
|
|
|
|
tor_assert(0); /* Should never reach here. */
|
|
|
|
return 0; /* suppress "control-reaches-end-of-non-void" warning. */
|
|
|
|
} else {
|
|
|
|
/* Parent */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/** End the current thread/process.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2005-09-30 00:59:17 +02:00
|
|
|
spawn_exit(void)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
2005-01-27 23:34:48 +01:00
|
|
|
#if defined(USE_WIN32_THREADS)
|
2004-11-01 21:41:47 +01:00
|
|
|
_endthread();
|
2006-09-06 03:49:55 +02:00
|
|
|
//we should never get here. my compiler thinks that _endthread returns, this
|
|
|
|
//is an attempt to fool it.
|
|
|
|
tor_assert(0);
|
|
|
|
_exit(0);
|
2005-01-27 23:34:48 +01:00
|
|
|
#elif defined(USE_PTHREADS)
|
|
|
|
pthread_exit(NULL);
|
2004-11-01 21:41:47 +01:00
|
|
|
#else
|
2005-02-03 07:40:06 +01:00
|
|
|
/* http://www.erlenstar.demon.co.uk/unix/faq_2.html says we should
|
|
|
|
* call _exit, not exit, from child processes. */
|
|
|
|
_exit(0);
|
2004-11-01 21:41:47 +01:00
|
|
|
#endif
|
2006-09-06 03:49:55 +02:00
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set *timeval to the current time of day. On error, log and terminate.
|
|
|
|
* (Same as gettimeofday(timeval,NULL), but never returns -1.)
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
tor_gettimeofday(struct timeval *timeval)
|
|
|
|
{
|
2005-08-03 18:28:39 +02:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
/* Epoch bias copied from perl: number of units between windows epoch and
|
|
|
|
* unix epoch. */
|
|
|
|
#define EPOCH_BIAS U64_LITERAL(116444736000000000)
|
|
|
|
#define UNITS_PER_SEC U64_LITERAL(10000000)
|
|
|
|
#define USEC_PER_SEC U64_LITERAL(1000000)
|
|
|
|
#define UNITS_PER_USEC U64_LITERAL(10)
|
|
|
|
union {
|
|
|
|
uint64_t ft_64;
|
|
|
|
FILETIME ft_ft;
|
|
|
|
} ft;
|
|
|
|
/* number of 100-nsec units since Jan 1, 1601 */
|
|
|
|
GetSystemTimeAsFileTime(&ft.ft_ft);
|
|
|
|
if (ft.ft_64 < EPOCH_BIAS) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_err(LD_GENERAL,"System time is before 1970; failing.");
|
2005-08-03 18:28:39 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ft.ft_64 -= EPOCH_BIAS;
|
2005-08-03 19:16:48 +02:00
|
|
|
timeval->tv_sec = (unsigned) (ft.ft_64 / UNITS_PER_SEC);
|
|
|
|
timeval->tv_usec = (unsigned) ((ft.ft_64 / UNITS_PER_USEC) % USEC_PER_SEC);
|
2005-08-03 18:28:39 +02:00
|
|
|
#elif defined(HAVE_GETTIMEOFDAY)
|
2004-11-01 21:41:47 +01:00
|
|
|
if (gettimeofday(timeval, NULL)) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_err(LD_GENERAL,"gettimeofday failed.");
|
2004-11-01 21:41:47 +01:00
|
|
|
/* If gettimeofday dies, we have either given a bad timezone (we didn't),
|
|
|
|
or segfaulted.*/
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#elif defined(HAVE_FTIME)
|
|
|
|
struct timeb tb;
|
|
|
|
ftime(&tb);
|
|
|
|
timeval->tv_sec = tb.time;
|
|
|
|
timeval->tv_usec = tb.millitm * 1000;
|
|
|
|
#else
|
|
|
|
#error "No way to get time."
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-02-22 08:09:17 +01:00
|
|
|
#if defined(TOR_IS_MULTITHREADED) && !defined(MS_WINDOWS)
|
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
|
|
|
|
|
2005-02-22 08:03:03 +01:00
|
|
|
#ifndef HAVE_LOCALTIME_R
|
2005-02-22 08:09:17 +01:00
|
|
|
#ifdef 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);
|
|
|
|
memcpy(result, r, sizeof(struct tm));
|
|
|
|
tor_mutex_release(m);
|
|
|
|
return result;
|
|
|
|
}
|
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);
|
|
|
|
memcpy(result, r, sizeof(struct tm));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
2005-02-22 08:03:03 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_GMTIME_R
|
2005-02-22 08:09:17 +01:00
|
|
|
#ifdef 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);
|
|
|
|
memcpy(result, r, sizeof(struct tm));
|
|
|
|
tor_mutex_release(m);
|
|
|
|
return result;
|
|
|
|
}
|
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);
|
|
|
|
memcpy(result, r, sizeof(struct tm));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
2005-02-22 08:03:03 +01:00
|
|
|
#endif
|
|
|
|
|
2008-08-22 18:24:52 +02:00
|
|
|
#if defined(USE_WIN32_THREADS)
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2008-08-22 18:24:52 +02:00
|
|
|
tor_mutex_init(tor_mutex_t *m)
|
2007-07-01 18:22:45 +02:00
|
|
|
{
|
2007-07-06 16:17:30 +02:00
|
|
|
InitializeCriticalSection(&m->mutex);
|
2007-07-01 18:22:45 +02:00
|
|
|
}
|
|
|
|
void
|
2008-08-22 18:24:52 +02:00
|
|
|
tor_mutex_uninit(tor_mutex_t *m)
|
2007-07-01 18:22:45 +02:00
|
|
|
{
|
|
|
|
DeleteCriticalSection(&m->mutex);
|
|
|
|
}
|
|
|
|
void
|
|
|
|
tor_mutex_acquire(tor_mutex_t *m)
|
|
|
|
{
|
|
|
|
tor_assert(m);
|
|
|
|
EnterCriticalSection(&m->mutex);
|
|
|
|
}
|
|
|
|
void
|
|
|
|
tor_mutex_release(tor_mutex_t *m)
|
|
|
|
{
|
|
|
|
LeaveCriticalSection(&m->mutex);
|
|
|
|
}
|
|
|
|
unsigned long
|
|
|
|
tor_get_thread_id(void)
|
|
|
|
{
|
|
|
|
return (unsigned long)GetCurrentThreadId();
|
|
|
|
}
|
2005-01-27 23:34:48 +01:00
|
|
|
#elif defined(USE_PTHREADS)
|
2008-04-22 17:59:59 +02:00
|
|
|
static pthread_mutexattr_t attr_reentrant;
|
2008-04-23 22:32:31 +02:00
|
|
|
static int threads_initialized = 0;
|
2008-12-22 15:56:28 +01:00
|
|
|
/* DOCDOC tor_mutex_init */
|
2008-08-22 18:24:52 +02:00
|
|
|
void
|
|
|
|
tor_mutex_init(tor_mutex_t *mutex)
|
2005-01-27 23:34:48 +01:00
|
|
|
{
|
2007-03-17 22:09:49 +01:00
|
|
|
int err;
|
2008-04-23 22:32:31 +02:00
|
|
|
if (PREDICT_UNLIKELY(!threads_initialized))
|
|
|
|
tor_threads_init();
|
2008-04-22 17:59:59 +02:00
|
|
|
err = pthread_mutex_init(&mutex->mutex, &attr_reentrant);
|
2007-03-17 22:09:49 +01:00
|
|
|
if (PREDICT_UNLIKELY(err)) {
|
|
|
|
log_err(LD_GENERAL, "Error %d creating a mutex.", err);
|
|
|
|
tor_fragile_assert();
|
|
|
|
}
|
2005-01-27 23:34:48 +01:00
|
|
|
}
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Wait until <b>m</b> is free, then acquire it. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
tor_mutex_acquire(tor_mutex_t *m)
|
2005-01-27 23:34:48 +01:00
|
|
|
{
|
2007-03-17 22:09:49 +01:00
|
|
|
int err;
|
2005-01-27 23:34:48 +01:00
|
|
|
tor_assert(m);
|
2007-03-17 22:09:49 +01:00
|
|
|
err = pthread_mutex_lock(&m->mutex);
|
|
|
|
if (PREDICT_UNLIKELY(err)) {
|
|
|
|
log_err(LD_GENERAL, "Error %d locking a mutex.", err);
|
|
|
|
tor_fragile_assert();
|
|
|
|
}
|
2005-01-27 23:34:48 +01:00
|
|
|
}
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Release the lock <b>m</b> so another thread can have it. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
tor_mutex_release(tor_mutex_t *m)
|
2005-01-27 23:34:48 +01:00
|
|
|
{
|
2007-03-17 22:09:49 +01:00
|
|
|
int err;
|
2005-01-27 23:34:48 +01:00
|
|
|
tor_assert(m);
|
2007-03-17 22:09:49 +01:00
|
|
|
err = pthread_mutex_unlock(&m->mutex);
|
|
|
|
if (PREDICT_UNLIKELY(err)) {
|
|
|
|
log_err(LD_GENERAL, "Error %d unlocking a mutex.", err);
|
|
|
|
tor_fragile_assert();
|
|
|
|
}
|
2005-01-27 23:34:48 +01:00
|
|
|
}
|
2008-12-22 15:56:28 +01:00
|
|
|
/* DOCDOC tor_mutex_uninit */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2008-08-22 18:24:52 +02:00
|
|
|
tor_mutex_uninit(tor_mutex_t *m)
|
2005-01-27 23:34:48 +01:00
|
|
|
{
|
2007-03-17 22:09:49 +01:00
|
|
|
int err;
|
2005-01-27 23:34:48 +01:00
|
|
|
tor_assert(m);
|
2007-03-17 22:09:49 +01:00
|
|
|
err = pthread_mutex_destroy(&m->mutex);
|
|
|
|
if (PREDICT_UNLIKELY(err)) {
|
|
|
|
log_err(LD_GENERAL, "Error %d destroying a mutex.", err);
|
|
|
|
tor_fragile_assert();
|
|
|
|
}
|
2005-01-27 23:34:48 +01:00
|
|
|
}
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Return an integer representing this thread. */
|
2005-02-13 23:32:25 +01:00
|
|
|
unsigned long
|
|
|
|
tor_get_thread_id(void)
|
|
|
|
{
|
2005-09-14 22:59:25 +02:00
|
|
|
union {
|
|
|
|
pthread_t thr;
|
|
|
|
unsigned long id;
|
|
|
|
} r;
|
|
|
|
r.thr = pthread_self();
|
|
|
|
return r.id;
|
2005-02-13 23:32:25 +01:00
|
|
|
}
|
2008-08-22 18:24:52 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TOR_IS_MULTITHREADED
|
2008-12-22 15:56:28 +01:00
|
|
|
/* DOCDOC tor_mutex_new */
|
2008-08-22 18:24:52 +02:00
|
|
|
tor_mutex_t *
|
|
|
|
tor_mutex_new(void)
|
|
|
|
{
|
|
|
|
tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t));
|
|
|
|
tor_mutex_init(m);
|
|
|
|
return m;
|
|
|
|
}
|
2008-12-22 15:56:28 +01:00
|
|
|
/* DOCDOC tor_mutex_free */
|
2008-08-22 18:24:52 +02:00
|
|
|
void
|
|
|
|
tor_mutex_free(tor_mutex_t *m)
|
|
|
|
{
|
|
|
|
tor_mutex_uninit(m);
|
|
|
|
tor_free(m);
|
|
|
|
}
|
2004-11-01 21:41:47 +01:00
|
|
|
#endif
|
|
|
|
|
2007-08-15 21:56:01 +02:00
|
|
|
/* Conditions. */
|
2007-07-01 18:22:45 +02:00
|
|
|
#ifdef USE_PTHREADS
|
2008-02-05 20:40:19 +01:00
|
|
|
#if 0
|
2008-02-09 11:45:49 +01:00
|
|
|
/** Cross-platform condition implementation. */
|
2007-07-01 18:22:45 +02:00
|
|
|
struct tor_cond_t {
|
|
|
|
pthread_cond_t cond;
|
|
|
|
};
|
2007-08-15 21:56:01 +02:00
|
|
|
/** Return a newly allocated condition, with nobody waiting on it. */
|
2007-07-01 18:22:45 +02:00
|
|
|
tor_cond_t *
|
|
|
|
tor_cond_new(void)
|
|
|
|
{
|
|
|
|
tor_cond_t *cond = tor_malloc_zero(sizeof(tor_cond_t));
|
|
|
|
if (pthread_cond_init(&cond->cond, NULL)) {
|
|
|
|
tor_free(cond);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return cond;
|
|
|
|
}
|
2007-08-15 21:56:01 +02:00
|
|
|
/** Release all resources held by <b>cond</b>. */
|
2007-07-01 18:22:45 +02:00
|
|
|
void
|
2007-08-17 23:46:34 +02:00
|
|
|
tor_cond_free(tor_cond_t *cond)
|
2007-07-01 18:22:45 +02:00
|
|
|
{
|
|
|
|
tor_assert(cond);
|
|
|
|
if (pthread_cond_destroy(&cond->cond)) {
|
|
|
|
log_warn(LD_GENERAL,"Error freeing condition: %s", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tor_free(cond);
|
|
|
|
}
|
2007-08-15 21:56:01 +02:00
|
|
|
/** Wait until one of the tor_cond_signal functions is called on <b>cond</b>.
|
|
|
|
* All waiters on the condition must wait holding the same <b>mutex</b>.
|
|
|
|
* Returns 0 on success, negative on failure. */
|
2007-07-01 18:22:45 +02:00
|
|
|
int
|
|
|
|
tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
return pthread_cond_wait(&cond->cond, &mutex->mutex) ? -1 : 0;
|
|
|
|
}
|
2007-08-15 21:56:01 +02:00
|
|
|
/** Wake up one of the waiters on <b>cond</b>. */
|
2007-07-01 18:22:45 +02:00
|
|
|
void
|
|
|
|
tor_cond_signal_one(tor_cond_t *cond)
|
|
|
|
{
|
|
|
|
pthread_cond_signal(&cond->cond);
|
|
|
|
}
|
2007-08-15 21:56:01 +02:00
|
|
|
/** Wake up all of the waiters on <b>cond</b>. */
|
2007-07-01 18:22:45 +02:00
|
|
|
void
|
|
|
|
tor_cond_signal_all(tor_cond_t *cond)
|
|
|
|
{
|
|
|
|
pthread_cond_broadcast(&cond->cond);
|
|
|
|
}
|
2008-02-05 20:40:19 +01:00
|
|
|
#endif
|
2007-08-15 21:56:01 +02:00
|
|
|
/** Set up common structures for use by threading. */
|
2007-07-01 18:22:45 +02:00
|
|
|
void
|
|
|
|
tor_threads_init(void)
|
|
|
|
{
|
2008-04-23 22:32:31 +02:00
|
|
|
if (!threads_initialized) {
|
|
|
|
pthread_mutexattr_init(&attr_reentrant);
|
|
|
|
pthread_mutexattr_settype(&attr_reentrant, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
threads_initialized = 1;
|
|
|
|
}
|
2007-07-01 18:22:45 +02:00
|
|
|
}
|
|
|
|
#elif defined(USE_WIN32_THREADS)
|
2008-02-05 20:40:19 +01:00
|
|
|
#if 0
|
2007-07-01 18:22:45 +02:00
|
|
|
static DWORD cond_event_tls_index;
|
|
|
|
struct tor_cond_t {
|
|
|
|
CRITICAL_SECTION mutex;
|
|
|
|
smartlist_t *events;
|
|
|
|
};
|
|
|
|
tor_cond_t *
|
|
|
|
tor_cond_new(void)
|
|
|
|
{
|
|
|
|
tor_cond_t *cond = tor_malloc_zero(sizeof(tor_cond_t));
|
2007-07-06 16:17:30 +02:00
|
|
|
InitializeCriticalSection(&cond->mutex);
|
2007-07-01 18:22:45 +02:00
|
|
|
cond->events = smartlist_create();
|
|
|
|
return cond;
|
|
|
|
}
|
|
|
|
void
|
|
|
|
tor_cond_free(tor_cond_t *cond)
|
|
|
|
{
|
|
|
|
tor_assert(cond);
|
|
|
|
DeleteCriticalSection(&cond->mutex);
|
2008-02-05 20:40:19 +01:00
|
|
|
/* XXXX notify? */
|
2007-07-01 18:22:45 +02:00
|
|
|
smartlist_free(cond->events);
|
|
|
|
tor_free(cond);
|
|
|
|
}
|
2007-07-06 16:17:30 +02:00
|
|
|
int
|
2007-07-01 18:22:45 +02:00
|
|
|
tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
HANDLE event;
|
|
|
|
int r;
|
|
|
|
tor_assert(cond);
|
|
|
|
tor_assert(mutex);
|
|
|
|
event = TlsGetValue(cond_event_tls_index);
|
|
|
|
if (!event) {
|
|
|
|
event = CreateEvent(0, FALSE, FALSE, NULL);
|
|
|
|
TlsSetValue(cond_event_tls_index, event);
|
|
|
|
}
|
|
|
|
EnterCriticalSection(&cond->mutex);
|
|
|
|
|
|
|
|
tor_assert(WaitForSingleObject(event, 0) == WAIT_TIMEOUT);
|
|
|
|
tor_assert(!smartlist_isin(cond->events, event));
|
|
|
|
smartlist_add(cond->events, event);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&cond->mutex);
|
|
|
|
|
|
|
|
tor_mutex_release(mutex);
|
|
|
|
r = WaitForSingleObject(event, INFINITE);
|
|
|
|
tor_mutex_acquire(mutex);
|
|
|
|
|
|
|
|
switch (r) {
|
|
|
|
case WAIT_OBJECT_0: /* we got the mutex normally. */
|
|
|
|
break;
|
|
|
|
case WAIT_ABANDONED: /* holding thread exited. */
|
|
|
|
case WAIT_TIMEOUT: /* Should never happen. */
|
|
|
|
tor_assert(0);
|
|
|
|
break;
|
|
|
|
case WAIT_FAILED:
|
|
|
|
log_warn(LD_GENERAL, "Failed to acquire mutex: %d",(int) GetLastError());
|
|
|
|
}
|
2007-07-06 16:17:30 +02:00
|
|
|
return 0;
|
2007-07-01 18:22:45 +02:00
|
|
|
}
|
|
|
|
void
|
|
|
|
tor_cond_signal_one(tor_cond_t *cond)
|
|
|
|
{
|
|
|
|
HANDLE event;
|
|
|
|
tor_assert(cond);
|
|
|
|
|
|
|
|
EnterCriticalSection(&cond->mutex);
|
|
|
|
|
2007-07-06 16:17:30 +02:00
|
|
|
if ((event = smartlist_pop_last(cond->events)))
|
2007-07-01 18:22:45 +02:00
|
|
|
SetEvent(event);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&cond->mutex);
|
|
|
|
}
|
|
|
|
void
|
|
|
|
tor_cond_signal_all(tor_cond_t *cond)
|
|
|
|
{
|
|
|
|
tor_assert(cond);
|
|
|
|
|
|
|
|
EnterCriticalSection(&cond->mutex);
|
|
|
|
SMARTLIST_FOREACH(cond->events, HANDLE, event, SetEvent(event));
|
|
|
|
smartlist_clear(cond->events);
|
|
|
|
LeaveCriticalSection(&cond->mutex);
|
|
|
|
}
|
2008-02-05 20:40:19 +01:00
|
|
|
#endif
|
2007-07-01 18:22:45 +02:00
|
|
|
void
|
|
|
|
tor_threads_init(void)
|
|
|
|
{
|
2008-02-05 20:40:19 +01:00
|
|
|
#if 0
|
2007-07-01 18:22:45 +02:00
|
|
|
cond_event_tls_index = TlsAlloc();
|
2008-02-05 20:40:19 +01:00
|
|
|
#endif
|
2007-07-01 18:22:45 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
*/
|
2006-09-19 22:41:31 +02:00
|
|
|
#if defined(MS_WINDOWS) && !defined(USE_BSOCKETS)
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
|
|
|
tor_socket_errno(int sock)
|
2004-11-01 21:41:47 +01:00
|
|
|
{
|
|
|
|
int optval, optvallen=sizeof(optval);
|
|
|
|
int err = WSAGetLastError();
|
|
|
|
if (err == WSAEWOULDBLOCK && sock >= 0) {
|
|
|
|
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval, &optvallen))
|
|
|
|
return err;
|
|
|
|
if (optval)
|
|
|
|
return optval;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-09-19 22:41:31 +02:00
|
|
|
#if defined(MS_WINDOWS) && !defined(USE_BSOCKETS)
|
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 },
|
|
|
|
};
|
|
|
|
/** There does not seem to be a strerror equivalent for winsock errors.
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
#endif
|
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
|
|
|
{
|
|
|
|
#ifdef MS_WINDOWS
|
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;
|
|
|
|
}
|
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. */
|
2004-12-22 06:29:06 +01:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2007-02-12 22:39:44 +01:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
/** 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
|
|
|
|
* ansi functions fail. Whee. */
|
|
|
|
char *
|
|
|
|
format_win32_error(DWORD err)
|
|
|
|
{
|
|
|
|
LPVOID str = NULL;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
/* Somebody once decided that this interface was better than strerror(). */
|
|
|
|
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, err,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR) &str,
|
|
|
|
0, NULL);
|
|
|
|
|
|
|
|
if (str) {
|
|
|
|
result = tor_strdup((char*)str);
|
|
|
|
LocalFree(str); /* LocalFree != free() */
|
|
|
|
} else {
|
|
|
|
result = tor_strdup("<unformattable error>");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
2007-02-12 22:39:53 +01:00
|
|
|
|