mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-10 21:23:58 +01:00
Refactor a bit so that it is safe to include math.h, and mostly not needed.
This commit is contained in:
parent
d086c9a7f7
commit
e56747f9cf
@ -328,7 +328,7 @@ logv(int severity, log_domain_mask_t domain, const char *funcname,
|
||||
|
||||
/** Output a message to the log. */
|
||||
void
|
||||
_log(int severity, log_domain_mask_t domain, const char *format, ...)
|
||||
tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
if (severity > _log_global_min_severity)
|
||||
|
@ -140,9 +140,9 @@ void change_callback_log_severity(int loglevelMin, int loglevelMax,
|
||||
void log_set_application_name(const char *name);
|
||||
|
||||
/* Outputs a message to stdout */
|
||||
void _log(int severity, log_domain_mask_t domain, const char *format, ...)
|
||||
void tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
|
||||
CHECK_PRINTF(3,4);
|
||||
#define log _log /* hack it so we don't conflict with log() as much */
|
||||
#define log tor_log /* hack it so we don't conflict with log() as much */
|
||||
|
||||
#ifdef __GNUC__
|
||||
extern int _log_global_min_severity;
|
||||
|
@ -16,6 +16,7 @@
|
||||
#include "orconfig.h"
|
||||
#include "util.h"
|
||||
#include "log.h"
|
||||
#undef log
|
||||
#include "crypto.h"
|
||||
#include "torint.h"
|
||||
#include "container.h"
|
||||
@ -30,6 +31,11 @@
|
||||
#include <pwd.h>
|
||||
#endif
|
||||
|
||||
/* 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>
|
||||
@ -278,7 +284,7 @@ tor_log_mallinfo(int severity)
|
||||
struct mallinfo mi;
|
||||
memset(&mi, 0, sizeof(mi));
|
||||
mi = mallinfo();
|
||||
log(severity, LD_MM,
|
||||
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",
|
||||
@ -301,6 +307,25 @@ tor_log_mallinfo(int severity)
|
||||
* Math
|
||||
* ===== */
|
||||
|
||||
/**
|
||||
* Returns the natural logarithm of d base 2. We define this wrapper here so
|
||||
* as to make it easier not to conflict with Tor's log() macro.
|
||||
*/
|
||||
double
|
||||
tor_mathlog(double d)
|
||||
{
|
||||
return log(d);
|
||||
}
|
||||
|
||||
/** Return the long integer closest to d. We define this wrapper here so
|
||||
* that not all users of math.h need to use the right incancations to get
|
||||
* the c99 functions. */
|
||||
long
|
||||
tor_lround(double d)
|
||||
{
|
||||
return lround(d);
|
||||
}
|
||||
|
||||
/** Returns floor(log2(u64)). If u64 is 0, (incorrectly) returns 0. */
|
||||
int
|
||||
tor_log2(uint64_t u64)
|
||||
@ -1650,12 +1675,12 @@ check_private_dir(const char *dirname, cpd_check_t check)
|
||||
tor_free(f);
|
||||
if (r) {
|
||||
if (errno != ENOENT) {
|
||||
log(LOG_WARN, LD_FS, "Directory %s cannot be read: %s", dirname,
|
||||
strerror(errno));
|
||||
log_warn(LD_FS, "Directory %s cannot be read: %s", dirname,
|
||||
strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (check == CPD_NONE) {
|
||||
log(LOG_WARN, LD_FS, "Directory %s does not exist.", dirname);
|
||||
log_warn(LD_FS, "Directory %s does not exist.", dirname);
|
||||
return -1;
|
||||
} else if (check == CPD_CREATE) {
|
||||
log_info(LD_GENERAL, "Creating directory %s", dirname);
|
||||
@ -1665,7 +1690,7 @@ check_private_dir(const char *dirname, cpd_check_t check)
|
||||
r = mkdir(dirname, 0700);
|
||||
#endif
|
||||
if (r) {
|
||||
log(LOG_WARN, LD_FS, "Error creating directory %s: %s", dirname,
|
||||
log_warn(LD_FS, "Error creating directory %s: %s", dirname,
|
||||
strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
@ -1675,7 +1700,7 @@ check_private_dir(const char *dirname, cpd_check_t check)
|
||||
return 0;
|
||||
}
|
||||
if (!(st.st_mode & S_IFDIR)) {
|
||||
log(LOG_WARN, LD_FS, "%s is not a directory", dirname);
|
||||
log_warn(LD_FS, "%s is not a directory", dirname);
|
||||
return -1;
|
||||
}
|
||||
#ifndef MS_WINDOWS
|
||||
@ -1688,7 +1713,7 @@ check_private_dir(const char *dirname, cpd_check_t check)
|
||||
|
||||
pw = getpwuid(st.st_uid);
|
||||
|
||||
log(LOG_WARN, LD_FS, "%s is not owned by this user (%s, %d) but by "
|
||||
log_warn(LD_FS, "%s is not owned by this user (%s, %d) but by "
|
||||
"%s (%d). Perhaps you are running Tor as the wrong user?",
|
||||
dirname, process_ownername, (int)getuid(),
|
||||
pw ? pw->pw_name : "<unknown>", (int)st.st_uid);
|
||||
@ -1697,9 +1722,9 @@ check_private_dir(const char *dirname, cpd_check_t check)
|
||||
return -1;
|
||||
}
|
||||
if (st.st_mode & 0077) {
|
||||
log(LOG_WARN, LD_FS, "Fixing permissions on directory %s", dirname);
|
||||
log_warn(LD_FS, "Fixing permissions on directory %s", dirname);
|
||||
if (chmod(dirname, 0700)) {
|
||||
log(LOG_WARN, LD_FS, "Could not chmod directory %s: %s", dirname,
|
||||
log_warn(LD_FS, "Could not chmod directory %s: %s", dirname,
|
||||
strerror(errno));
|
||||
return -1;
|
||||
} else {
|
||||
@ -1784,7 +1809,7 @@ start_writing_to_file(const char *fname, int open_flags, int mode,
|
||||
} else {
|
||||
open_name = new_file->tempname = tor_malloc(tempname_len);
|
||||
if (tor_snprintf(new_file->tempname, tempname_len, "%s.tmp", fname)<0) {
|
||||
log(LOG_WARN, LD_GENERAL, "Failed to generate filename");
|
||||
log_warn(LD_GENERAL, "Failed to generate filename");
|
||||
goto err;
|
||||
}
|
||||
/* We always replace an existing temporary file if there is one. */
|
||||
@ -1796,7 +1821,7 @@ start_writing_to_file(const char *fname, int open_flags, int mode,
|
||||
new_file->binary = 1;
|
||||
|
||||
if ((new_file->fd = open(open_name, open_flags, mode)) < 0) {
|
||||
log(LOG_WARN, LD_FS, "Couldn't open \"%s\" (%s) for writing: %s",
|
||||
log_warn(LD_FS, "Couldn't open \"%s\" (%s) for writing: %s",
|
||||
open_name, fname, strerror(errno));
|
||||
goto err;
|
||||
}
|
||||
@ -1933,7 +1958,7 @@ write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
|
||||
{
|
||||
result = write_all(fd, chunk->bytes, chunk->len, 0);
|
||||
if (result < 0) {
|
||||
log(LOG_WARN, LD_FS, "Error writing to \"%s\": %s", fname,
|
||||
log_warn(LD_FS, "Error writing to \"%s\": %s", fname,
|
||||
strerror(errno));
|
||||
goto err;
|
||||
}
|
||||
|
@ -43,7 +43,7 @@
|
||||
* stderr. */
|
||||
#define tor_assert(expr) STMT_BEGIN \
|
||||
if (PREDICT_UNLIKELY(!(expr))) { \
|
||||
log(LOG_ERR, LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \
|
||||
log_err(LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \
|
||||
_SHORT_FILE_, __LINE__, __func__, #expr); \
|
||||
fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
|
||||
_SHORT_FILE_, __LINE__, __func__, #expr); \
|
||||
@ -152,6 +152,8 @@ void tor_log_mallinfo(int severity);
|
||||
#define bool_neq(a,b) (!(a)!=!(b))
|
||||
|
||||
/* Math functions */
|
||||
double tor_mathlog(double d) ATTR_CONST;
|
||||
long tor_lround(double d) ATTR_CONST;
|
||||
int tor_log2(uint64_t u64) ATTR_CONST;
|
||||
uint64_t round_to_power_of_2(uint64_t u64);
|
||||
unsigned round_to_next_multiple_of(unsigned number, unsigned divisor);
|
||||
|
@ -13,41 +13,13 @@
|
||||
|
||||
#include "or.h"
|
||||
#include "crypto.h"
|
||||
#undef log
|
||||
#include <math.h>
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a,b) ((a)<(b)?(a):(b))
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This madness is needed because if we simply #undef log
|
||||
* before including or.h or log.h, we get linker collisions
|
||||
* and random segfaults due to memory corruption (and
|
||||
* not even at calls to log() either!)
|
||||
*/
|
||||
/* XXX022 somebody should rename Tor's log() function, so we can
|
||||
* remove this wart. -RD */
|
||||
#undef log
|
||||
|
||||
/*
|
||||
* Linux doesn't provide lround in math.h by default, but mac os does...
|
||||
* It's best just to leave math.h out of the picture entirely.
|
||||
*/
|
||||
//#define log math_h_log
|
||||
//#include <math.h>
|
||||
//#undef log
|
||||
long int lround(double x);
|
||||
double ln(double x);
|
||||
double log(double x);
|
||||
double pow(double x, double y);
|
||||
|
||||
double
|
||||
ln(double x)
|
||||
{
|
||||
return log(x);
|
||||
}
|
||||
|
||||
#define log _log
|
||||
|
||||
/********* START VARIABLES **********/
|
||||
/** Global list of circuit build times */
|
||||
// FIXME: Add this as a member for entry_guard_t instead of global?
|
||||
@ -523,9 +495,9 @@ circuit_build_times_update_alpha(circuit_build_times_t *cbt)
|
||||
}
|
||||
|
||||
if (x[i] < cbt->Xm) {
|
||||
a += ln(cbt->Xm);
|
||||
a += tor_mathlog(cbt->Xm);
|
||||
} else {
|
||||
a += ln(x[i]);
|
||||
a += tor_mathlog(x[i]);
|
||||
}
|
||||
n++;
|
||||
}
|
||||
@ -536,7 +508,7 @@ circuit_build_times_update_alpha(circuit_build_times_t *cbt)
|
||||
}
|
||||
tor_assert(n==cbt->total_build_times);
|
||||
|
||||
a -= n*ln(cbt->Xm);
|
||||
a -= n*tor_mathlog(cbt->Xm);
|
||||
a = n/a;
|
||||
|
||||
cbt->alpha = a;
|
||||
@ -611,7 +583,8 @@ circuit_build_times_generate_sample(circuit_build_times_t *cbt,
|
||||
|
||||
tor_assert(0 <= u && u < 1.0);
|
||||
/* circuit_build_times_calculate_timeout returns <= INT32_MAX */
|
||||
ret = (build_time_t)lround(circuit_build_times_calculate_timeout(cbt, u));
|
||||
ret = (build_time_t)
|
||||
tor_lround(circuit_build_times_calculate_timeout(cbt, u));
|
||||
tor_assert(ret > 0);
|
||||
return ret;
|
||||
}
|
||||
@ -624,7 +597,7 @@ circuit_build_times_add_timeout_worker(circuit_build_times_t *cbt,
|
||||
build_time_t gentime = circuit_build_times_generate_sample(cbt,
|
||||
quantile_cutoff, MAX_SYNTHETIC_QUANTILE);
|
||||
|
||||
if (gentime < (build_time_t)lround(cbt->timeout_ms)) {
|
||||
if (gentime < (build_time_t)tor_lround(cbt->timeout_ms)) {
|
||||
log_warn(LD_CIRC,
|
||||
"Generated a synthetic timeout LESS than the current timeout: "
|
||||
"%ums vs %lfms using Xm: %d a: %lf, q: %lf",
|
||||
@ -658,7 +631,8 @@ circuit_build_times_initial_alpha(circuit_build_times_t *cbt,
|
||||
// -ln(1-0.8)/(ln(CircBuildTimeout)-ln(Xm))=a
|
||||
tor_assert(quantile >= 0);
|
||||
tor_assert(cbt->Xm > 0);
|
||||
cbt->alpha = ln(1.0-quantile)/(ln(cbt->Xm)-ln(timeout_ms));
|
||||
cbt->alpha = tor_mathlog(1.0-quantile)/
|
||||
(tor_mathlog(cbt->Xm)-tor_mathlog(timeout_ms));
|
||||
tor_assert(cbt->alpha > 0);
|
||||
}
|
||||
|
||||
@ -795,7 +769,7 @@ circuit_build_times_network_check_live(circuit_build_times_t *cbt)
|
||||
"Network is flaky. No activity for %ld seconds. "
|
||||
"Temporarily raising timeout to %lds.",
|
||||
(long int)(now - cbt->liveness.network_last_live),
|
||||
lround(circuit_build_times_get_initial_timeout()/1000));
|
||||
tor_lround(circuit_build_times_get_initial_timeout()/1000));
|
||||
cbt->timeout_ms = circuit_build_times_get_initial_timeout();
|
||||
}
|
||||
|
||||
@ -849,7 +823,8 @@ circuit_build_times_network_check_changed(circuit_build_times_t *cbt)
|
||||
log_notice(LD_CIRC,
|
||||
"Network connection speed appears to have changed. Resetting "
|
||||
"timeout to %lds after %d timeouts and %d buildtimes.",
|
||||
lround(cbt->timeout_ms/1000), timeout_count, total_build_times);
|
||||
tor_lround(cbt->timeout_ms/1000), timeout_count,
|
||||
total_build_times);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -921,7 +896,7 @@ circuit_build_times_set_timeout(circuit_build_times_t *cbt)
|
||||
|
||||
log_info(LD_CIRC,
|
||||
"Set circuit build timeout to %lds (%lfms, Xm: %d, a: %lf) "
|
||||
"based on %d circuit times", lround(cbt->timeout_ms/1000),
|
||||
"based on %d circuit times", tor_lround(cbt->timeout_ms/1000),
|
||||
cbt->timeout_ms, cbt->Xm, cbt->alpha, cbt->total_build_times);
|
||||
|
||||
}
|
||||
@ -1083,7 +1058,7 @@ void
|
||||
circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
|
||||
{
|
||||
char *s = circuit_list_path(circ,1);
|
||||
log(severity,domain,"%s",s);
|
||||
tor_log(severity,domain,"%s",s);
|
||||
tor_free(s);
|
||||
}
|
||||
|
||||
@ -1402,7 +1377,7 @@ inform_testing_reachability(void)
|
||||
"CHECKING_REACHABILITY DIRADDRESS=%s:%d",
|
||||
me->address, me->dir_port);
|
||||
}
|
||||
log(LOG_NOTICE, LD_OR, "Now checking whether ORPort %s:%d%s %s reachable... "
|
||||
log_notice(LD_OR, "Now checking whether ORPort %s:%d%s %s reachable... "
|
||||
"(this may take up to %d minutes -- look for log "
|
||||
"messages indicating success)",
|
||||
me->address, me->or_port,
|
||||
@ -1527,7 +1502,7 @@ circuit_send_next_onion_skin(origin_circuit_t *circ)
|
||||
or_options_t *options = get_options();
|
||||
has_completed_circuit=1;
|
||||
/* FFFF Log a count of known routers here */
|
||||
log(LOG_NOTICE, LD_GENERAL,
|
||||
log_notice(LD_GENERAL,
|
||||
"Tor has successfully opened a circuit. "
|
||||
"Looks like client functionality is working.");
|
||||
control_event_bootstrap(BOOTSTRAP_STATUS_DONE, 0);
|
||||
@ -1582,7 +1557,7 @@ void
|
||||
circuit_note_clock_jumped(int seconds_elapsed)
|
||||
{
|
||||
int severity = server_mode(get_options()) ? LOG_WARN : LOG_NOTICE;
|
||||
log(severity, LD_GENERAL, "Your system clock just jumped %d seconds %s; "
|
||||
tor_log(severity, LD_GENERAL, "Your system clock just jumped %d seconds %s; "
|
||||
"assuming established circuits no longer work.",
|
||||
seconds_elapsed >=0 ? seconds_elapsed : -seconds_elapsed,
|
||||
seconds_elapsed >=0 ? "forward" : "backward");
|
||||
|
@ -3002,7 +3002,7 @@ typedef uint32_t build_time_t;
|
||||
* at which point we switch back to computing the timeout from
|
||||
* our saved history.
|
||||
*/
|
||||
#define NETWORK_NONLIVE_TIMEOUT_COUNT (lround(RECENT_CIRCUITS*0.15))
|
||||
#define NETWORK_NONLIVE_TIMEOUT_COUNT (tor_lround(RECENT_CIRCUITS*0.15))
|
||||
|
||||
/**
|
||||
* This tells us when to toss out the last streak of N timeouts.
|
||||
@ -3010,7 +3010,8 @@ typedef uint32_t build_time_t;
|
||||
* If instead we start getting cells, we switch back to computing the timeout
|
||||
* from our saved history.
|
||||
*/
|
||||
#define NETWORK_NONLIVE_DISCARD_COUNT (lround(NETWORK_NONLIVE_TIMEOUT_COUNT*2))
|
||||
#define NETWORK_NONLIVE_DISCARD_COUNT \
|
||||
(tor_lround(NETWORK_NONLIVE_TIMEOUT_COUNT*2))
|
||||
|
||||
/**
|
||||
* Maximum count of timeouts that finish the first hop in the past
|
||||
@ -3019,7 +3020,7 @@ typedef uint32_t build_time_t;
|
||||
* This tells us to abandon timeout history and set
|
||||
* the timeout back to BUILD_TIMEOUT_INITIAL_VALUE.
|
||||
*/
|
||||
#define MAX_RECENT_TIMEOUT_COUNT (lround(RECENT_CIRCUITS*0.8))
|
||||
#define MAX_RECENT_TIMEOUT_COUNT (tor_lround(RECENT_CIRCUITS*0.8))
|
||||
|
||||
/** Information about the state of our local network connection */
|
||||
typedef struct {
|
||||
|
@ -3,16 +3,16 @@ noinst_PROGRAMS = tor-checkkey
|
||||
|
||||
tor_resolve_SOURCES = tor-resolve.c
|
||||
tor_resolve_LDFLAGS = @TOR_LDFLAGS_libevent@
|
||||
tor_resolve_LDADD = ../common/libor.a @TOR_LIB_WS32@
|
||||
tor_resolve_LDADD = -lm ../common/libor.a @TOR_LIB_WS32@
|
||||
|
||||
tor_gencert_SOURCES = tor-gencert.c
|
||||
tor_gencert_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \
|
||||
@TOR_LDFLAGS_libevent@
|
||||
tor_gencert_LDADD = ../common/libor.a ../common/libor-crypto.a \
|
||||
-lz -lcrypto @TOR_LIB_WS32@ @TOR_LIB_GDI@
|
||||
-lm -lz -lcrypto @TOR_LIB_WS32@ @TOR_LIB_GDI@
|
||||
|
||||
tor_checkkey_SOURCES = tor-checkkey.c
|
||||
tor_checkkey_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \
|
||||
@TOR_LDFLAGS_libevent@
|
||||
tor_checkkey_LDADD = ../common/libor.a ../common/libor-crypto.a \
|
||||
-lz -lcrypto @TOR_LIB_WS32@ @TOR_LIB_GDI@
|
||||
-lm -lz -lcrypto @TOR_LIB_WS32@ @TOR_LIB_GDI@
|
||||
|
Loading…
Reference in New Issue
Block a user