mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-12-06 19:41:15 +01:00
152 lines
3.5 KiB
C
152 lines
3.5 KiB
C
/* Copyright (c) 2003, Roger Dingledine
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
|
* Copyright (c) 2007-2018, The Tor Project, Inc. */
|
|
/* See LICENSE for licensing information */
|
|
|
|
/**
|
|
* \file util.c
|
|
* \brief Common functions for strings, IO, network, data structures,
|
|
* process control.
|
|
**/
|
|
|
|
#include "orconfig.h"
|
|
#ifdef HAVE_FCNTL_H
|
|
#include <fcntl.h>
|
|
#endif
|
|
#define UTIL_PRIVATE
|
|
#include "common/util.h"
|
|
#include "lib/log/torlog.h"
|
|
#include "lib/crypt_ops/crypto_digest.h"
|
|
#include "lib/cc/torint.h"
|
|
#include "lib/container/smartlist.h"
|
|
#include "lib/fdio/fdio.h"
|
|
#include "lib/net/address.h"
|
|
#include "lib/sandbox/sandbox.h"
|
|
#include "lib/err/backtrace.h"
|
|
#include "lib/process/waitpid.h"
|
|
#include "lib/encoding/binascii.h"
|
|
|
|
#ifdef _WIN32
|
|
#include <io.h>
|
|
#include <direct.h>
|
|
#include <process.h>
|
|
#include <tchar.h>
|
|
#include <winbase.h>
|
|
#else /* !(defined(_WIN32)) */
|
|
#include <dirent.h>
|
|
#include <pwd.h>
|
|
#include <grp.h>
|
|
#endif /* defined(_WIN32) */
|
|
|
|
/* math.h needs this on Linux */
|
|
#ifndef _USE_ISOC99_
|
|
#define _USE_ISOC99_ 1
|
|
#endif
|
|
#include <math.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <signal.h>
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
#include <netinet/in.h>
|
|
#endif
|
|
#ifdef HAVE_ARPA_INET_H
|
|
#include <arpa/inet.h>
|
|
#endif
|
|
#ifdef HAVE_ERRNO_H
|
|
#include <errno.h>
|
|
#endif
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
#include <sys/socket.h>
|
|
#endif
|
|
#ifdef HAVE_SYS_TIME_H
|
|
#include <sys/time.h>
|
|
#endif
|
|
#ifdef HAVE_UNISTD_H
|
|
#include <unistd.h>
|
|
#endif
|
|
#ifdef HAVE_SYS_STAT_H
|
|
#include <sys/stat.h>
|
|
#endif
|
|
#ifdef HAVE_SYS_FCNTL_H
|
|
#include <sys/fcntl.h>
|
|
#endif
|
|
#ifdef HAVE_TIME_H
|
|
#include <time.h>
|
|
#endif
|
|
#ifdef HAVE_MALLOC_MALLOC_H
|
|
#include <malloc/malloc.h>
|
|
#endif
|
|
#ifdef HAVE_MALLOC_H
|
|
#if !defined(OpenBSD) && !defined(__FreeBSD__)
|
|
/* OpenBSD has a malloc.h, but for our purposes, it only exists in order to
|
|
* scold us for being so stupid as to autodetect its presence. To be fair,
|
|
* they've done this since 1996, when autoconf was only 5 years old. */
|
|
#include <malloc.h>
|
|
#endif /* !defined(OpenBSD) && !defined(__FreeBSD__) */
|
|
#endif /* defined(HAVE_MALLOC_H) */
|
|
#ifdef HAVE_MALLOC_NP_H
|
|
#include <malloc_np.h>
|
|
#endif
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
#include <sys/wait.h>
|
|
#endif
|
|
#if defined(HAVE_SYS_PRCTL_H) && defined(__linux__)
|
|
#include <sys/prctl.h>
|
|
#endif
|
|
|
|
/* =====
|
|
* Memory management
|
|
* ===== */
|
|
|
|
DISABLE_GCC_WARNING(aggregate-return)
|
|
/** Call the platform malloc info function, and dump the results to the log at
|
|
* level <b>severity</b>. If no such function exists, do nothing. */
|
|
void
|
|
tor_log_mallinfo(int severity)
|
|
{
|
|
#ifdef HAVE_MALLINFO
|
|
struct mallinfo mi;
|
|
memset(&mi, 0, sizeof(mi));
|
|
mi = mallinfo();
|
|
tor_log(severity, LD_MM,
|
|
"mallinfo() said: arena=%d, ordblks=%d, smblks=%d, hblks=%d, "
|
|
"hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, "
|
|
"keepcost=%d",
|
|
mi.arena, mi.ordblks, mi.smblks, mi.hblks,
|
|
mi.hblkhd, mi.usmblks, mi.fsmblks, mi.uordblks, mi.fordblks,
|
|
mi.keepcost);
|
|
#else /* !(defined(HAVE_MALLINFO)) */
|
|
(void)severity;
|
|
#endif /* defined(HAVE_MALLINFO) */
|
|
}
|
|
ENABLE_GCC_WARNING(aggregate-return)
|
|
|
|
/* =====
|
|
* Math
|
|
* ===== */
|
|
|
|
/* =====
|
|
* String manipulation
|
|
* ===== */
|
|
|
|
/* =====
|
|
* Time
|
|
* ===== */
|
|
|
|
#ifdef _WIN32
|
|
HANDLE
|
|
load_windows_system_library(const TCHAR *library_name)
|
|
{
|
|
TCHAR path[MAX_PATH];
|
|
unsigned n;
|
|
n = GetSystemDirectory(path, MAX_PATH);
|
|
if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH)
|
|
return 0;
|
|
_tcscat(path, TEXT("\\"));
|
|
_tcscat(path, library_name);
|
|
return LoadLibrary(path);
|
|
}
|
|
#endif /* defined(_WIN32) */
|