tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

commit c08b7b10c520db08e20e6ac5630f0a668d5ecf3a
parent 9ec80909ec7be3d9eea863dfc3f320bfe9072ae8
Author: Nick Mathewson <nickm@torproject.org>
Date:   Tue, 10 Jul 2018 15:35:49 -0400

Merge branch 'post-refactor-renaming'

Diffstat:
Msrc/core/crypto/onion_ntor.c | 2+-
Msrc/core/or/or.h | 2+-
Msrc/feature/dirauth/keypin.c | 2+-
Msrc/feature/nodelist/parsecommon.c | 2+-
Msrc/feature/nodelist/torcert.c | 2+-
Msrc/lib/compress/compress.c | 4++--
Msrc/lib/compress/compress_lzma.c | 4++--
Msrc/lib/compress/compress_none.c | 2+-
Msrc/lib/compress/compress_zlib.c | 2+-
Msrc/lib/compress/compress_zstd.c | 2+-
Msrc/lib/container/bitarray.h | 2+-
Msrc/lib/container/bloomfilt.c | 2+-
Msrc/lib/container/buffers.c | 4++--
Msrc/lib/container/handles.h | 2+-
Msrc/lib/container/map.c | 2+-
Msrc/lib/container/smartlist.c | 2+-
Msrc/lib/crypt_ops/aes.c | 2+-
Msrc/lib/crypt_ops/aes.h | 2+-
Msrc/lib/crypt_ops/crypto.c | 2+-
Msrc/lib/crypt_ops/crypto_curve25519.c | 2+-
Msrc/lib/crypt_ops/crypto_dh.c | 2+-
Msrc/lib/crypt_ops/crypto_digest.c | 2+-
Msrc/lib/crypt_ops/crypto_digest.h | 2+-
Msrc/lib/crypt_ops/crypto_ed25519.c | 2+-
Msrc/lib/crypt_ops/crypto_format.c | 2+-
Msrc/lib/crypt_ops/crypto_rand.c | 4++--
Msrc/lib/crypt_ops/crypto_rsa.c | 2+-
Msrc/lib/crypt_ops/crypto_rsa.h | 2+-
Msrc/lib/crypt_ops/crypto_util.c | 2+-
Msrc/lib/ctime/di_ops.c | 2+-
Msrc/lib/encoding/binascii.c | 4++--
Msrc/lib/encoding/confline.c | 4++--
Msrc/lib/encoding/cstring.c | 4++--
Msrc/lib/encoding/keyval.c | 2+-
Msrc/lib/encoding/time_fmt.c | 6+++---
Msrc/lib/evloop/compat_libevent.c | 2+-
Msrc/lib/evloop/compat_libevent.h | 2+-
Msrc/lib/evloop/procmon.c | 4++--
Msrc/lib/evloop/procmon.h | 2+-
Msrc/lib/evloop/timers.c | 4++--
Msrc/lib/evloop/workqueue.c | 2+-
Msrc/lib/fs/conffile.c | 4++--
Msrc/lib/fs/dir.c | 4++--
Msrc/lib/fs/files.c | 4++--
Msrc/lib/fs/lockfile.c | 4++--
Msrc/lib/fs/mmap.c | 4++--
Msrc/lib/fs/path.c | 4++--
Msrc/lib/fs/storagedir.c | 4++--
Msrc/lib/fs/userdb.c | 4++--
Msrc/lib/lock/compat_mutex.c | 2+-
Msrc/lib/lock/compat_mutex.h | 2+-
Msrc/lib/log/escape.c | 2+-
Msrc/lib/log/include.am | 4++--
Asrc/lib/log/log.c | 1488+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/lib/log/log.h | 276+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/lib/log/ratelim.c | 2+-
Dsrc/lib/log/torlog.c | 1488-------------------------------------------------------------------------------
Dsrc/lib/log/torlog.h | 276-------------------------------------------------------------------------------
Msrc/lib/log/util_bug.c | 4++--
Msrc/lib/log/util_bug.h | 2+-
Msrc/lib/log/win32err.c | 2+-
Msrc/lib/malloc/include.am | 4++--
Asrc/lib/malloc/malloc.c | 230+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/lib/malloc/malloc.h | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dsrc/lib/malloc/util_malloc.c | 230-------------------------------------------------------------------------------
Dsrc/lib/malloc/util_malloc.h | 92-------------------------------------------------------------------------------
Msrc/lib/memarea/memarea.c | 4++--
Msrc/lib/meminfo/meminfo.c | 4++--
Msrc/lib/net/address.c | 4++--
Msrc/lib/net/buffers_net.c | 2+-
Msrc/lib/net/resolve.c | 2+-
Msrc/lib/net/socket.c | 2+-
Msrc/lib/process/daemon.c | 2+-
Msrc/lib/process/env.c | 6+++---
Msrc/lib/process/pidfile.c | 2+-
Msrc/lib/process/restrict.c | 2+-
Msrc/lib/process/setuid.c | 4++--
Msrc/lib/process/subprocess.c | 4++--
Msrc/lib/process/waitpid.c | 4++--
Msrc/lib/sandbox/sandbox.c | 4++--
Msrc/lib/smartlist_core/smartlist_core.c | 2+-
Msrc/lib/smartlist_core/smartlist_split.c | 2+-
Msrc/lib/string/printf.c | 2+-
Msrc/lib/term/getpass.c | 2+-
Msrc/lib/thread/compat_pthreads.c | 2+-
Msrc/lib/thread/compat_threads.c | 2+-
Msrc/lib/thread/compat_winthreads.c | 2+-
Msrc/lib/thread/numcpus.c | 2+-
Msrc/lib/time/compat_time.c | 2+-
Msrc/lib/time/tvdiff.c | 2+-
Msrc/lib/tls/buffers_tls.c | 2+-
Msrc/lib/tls/tortls.c | 2+-
Msrc/lib/trace/debug.h | 2+-
Msrc/lib/wallclock/include.am | 4++--
Asrc/lib/wallclock/time_to_tm.c | 200+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/lib/wallclock/time_to_tm.h | 22++++++++++++++++++++++
Dsrc/lib/wallclock/tm_cvt.c | 200-------------------------------------------------------------------------------
Dsrc/lib/wallclock/tm_cvt.h | 22----------------------
Msrc/test/fuzz/fuzz_http.c | 2+-
Msrc/test/fuzz/fuzz_http_connect.c | 2+-
Msrc/test/log_test_helpers.c | 2+-
Msrc/test/test-memwipe.c | 2+-
Msrc/test/test_bt_cl.c | 2+-
Msrc/test/test_crypto_openssl.c | 2+-
Msrc/test/test_logging.c | 2+-
Msrc/test/test_status.c | 2+-
Msrc/test/test_tortls.c | 2+-
Msrc/test/test_util_slow.c | 2+-
Msrc/tools/tor-gencert.c | 4++--
Msrc/tools/tor-resolve.c | 4++--
Msrc/trunnel/trunnel-local.h | 2+-
111 files changed, 2441 insertions(+), 2441 deletions(-)

diff --git a/src/core/crypto/onion_ntor.c b/src/core/crypto/onion_ntor.c @@ -27,7 +27,7 @@ #include "lib/crypt_ops/crypto_hkdf.h" #include "lib/crypt_ops/crypto_util.h" #include "lib/ctime/di_ops.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "core/crypto/onion_ntor.h" diff --git a/src/core/or/or.h b/src/core/or/or.h @@ -47,7 +47,7 @@ #include "lib/log/escape.h" #include "lib/log/ratelim.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/net/address.h" #include "lib/net/inaddr.h" #include "lib/net/socket.h" diff --git a/src/feature/dirauth/keypin.c b/src/feature/dirauth/keypin.c @@ -23,7 +23,7 @@ #include "lib/fdio/fdio.h" #include "lib/fs/files.h" #include "lib/fs/mmap.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/string/compat_ctype.h" #include "lib/string/printf.h" diff --git a/src/feature/nodelist/parsecommon.c b/src/feature/nodelist/parsecommon.c @@ -7,7 +7,7 @@ **/ #include "feature/nodelist/parsecommon.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/encoding/binascii.h" #include "lib/container/smartlist.h" diff --git a/src/feature/nodelist/torcert.c b/src/feature/nodelist/torcert.c @@ -30,7 +30,7 @@ #include "lib/crypt_ops/crypto_util.h" #include "feature/nodelist/torcert.h" #include "trunnel/ed25519_cert.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "trunnel/link_handshake.h" #include "lib/tls/tortls.h" diff --git a/src/lib/compress/compress.c b/src/lib/compress/compress.c @@ -22,7 +22,7 @@ #include <netinet/in.h> #endif -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/arch/bytes.h" #include "lib/ctime/di_ops.h" @@ -32,7 +32,7 @@ #include "lib/compress/compress_zlib.h" #include "lib/compress/compress_zstd.h" #include "lib/intmath/cmp.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/thread/threads.h" /** Total number of bytes allocated for compression state overhead. */ diff --git a/src/lib/compress/compress_lzma.c b/src/lib/compress/compress_lzma.c @@ -15,9 +15,9 @@ #include "lib/compress/compress.h" #include "lib/compress/compress_lzma.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/thread/threads.h" #ifdef HAVE_LZMA diff --git a/src/lib/compress/compress_none.c b/src/lib/compress/compress_none.c @@ -16,7 +16,7 @@ #include "orconfig.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/compress/compress.h" #include "lib/compress/compress_none.h" #include "lib/intmath/cmp.h" diff --git a/src/lib/compress/compress_zlib.c b/src/lib/compress/compress_zlib.c @@ -13,7 +13,7 @@ #include "orconfig.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/compress/compress.h" #include "lib/compress/compress_zlib.h" diff --git a/src/lib/compress/compress_zstd.c b/src/lib/compress/compress_zstd.c @@ -13,7 +13,7 @@ #include "orconfig.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/compress/compress.h" #include "lib/compress/compress_zstd.h" diff --git a/src/lib/container/bitarray.h b/src/lib/container/bitarray.h @@ -15,7 +15,7 @@ #include "orconfig.h" #include <string.h> #include "lib/cc/torint.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #if SIZEOF_INT == 4 #define BITARRAY_SHIFT 5 diff --git a/src/lib/container/bloomfilt.c b/src/lib/container/bloomfilt.c @@ -10,7 +10,7 @@ #include <stdlib.h> -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/container/bloomfilt.h" #include "lib/intmath/bits.h" #include "lib/log/util_bug.h" diff --git a/src/lib/container/buffers.c b/src/lib/container/buffers.c @@ -27,10 +27,10 @@ #include <stddef.h> #include "lib/container/buffers.h" #include "lib/cc/torint.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/ctime/di_ops.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/string/printf.h" #include "lib/time/compat_time.h" diff --git a/src/lib/container/handles.h b/src/lib/container/handles.h @@ -52,7 +52,7 @@ #include "orconfig.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #define HANDLE_ENTRY(name, structname) \ struct name ## _handle_head_t *handle_head diff --git a/src/lib/container/map.c b/src/lib/container/map.c @@ -14,7 +14,7 @@ #include "lib/ctime/di_ops.h" #include "lib/defs/digest_sizes.h" #include "lib/string/util_string.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/log/util_bug.h" diff --git a/src/lib/container/smartlist.c b/src/lib/container/smartlist.c @@ -17,7 +17,7 @@ #include "lib/container/smartlist.h" #include "lib/err/torerr.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/defs/digest_sizes.h" #include "lib/ctime/di_ops.h" #include "lib/string/compat_ctype.h" diff --git a/src/lib/crypt_ops/aes.c b/src/lib/crypt_ops/aes.c @@ -38,7 +38,7 @@ DISABLE_GCC_WARNING(redundant-decls) ENABLE_GCC_WARNING(redundant-decls) #include "lib/crypt_ops/aes.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/ctime/di_ops.h" #ifdef ANDROID diff --git a/src/lib/crypt_ops/aes.h b/src/lib/crypt_ops/aes.h @@ -14,7 +14,7 @@ */ #include "lib/cc/torint.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" typedef struct aes_cnt_cipher aes_cnt_cipher_t; diff --git a/src/lib/crypt_ops/crypto.c b/src/lib/crypt_ops/crypto.c @@ -62,7 +62,7 @@ ENABLE_GCC_WARNING(redundant-decls) #include <unistd.h> #endif -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/cc/torint.h" #include "lib/crypt_ops/aes.h" diff --git a/src/lib/crypt_ops/crypto_curve25519.c b/src/lib/crypt_ops/crypto_curve25519.c @@ -26,7 +26,7 @@ #include "lib/crypt_ops/crypto_format.h" #include "lib/crypt_ops/crypto_rand.h" #include "lib/crypt_ops/crypto_util.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "ed25519/donna/ed25519_donna_tor.h" diff --git a/src/lib/crypt_ops/crypto_dh.c b/src/lib/crypt_ops/crypto_dh.c @@ -14,7 +14,7 @@ #include "lib/crypt_ops/crypto_digest.h" #include "lib/crypt_ops/crypto_hkdf.h" #include "lib/crypt_ops/crypto_util.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" DISABLE_GCC_WARNING(redundant-decls) diff --git a/src/lib/crypt_ops/crypto_digest.c b/src/lib/crypt_ops/crypto_digest.c @@ -14,7 +14,7 @@ #include "lib/crypt_ops/crypto_digest.h" #include "lib/crypt_ops/crypto_openssl_mgt.h" #include "lib/crypt_ops/crypto_util.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "keccak-tiny/keccak-tiny.h" diff --git a/src/lib/crypt_ops/crypto_digest.h b/src/lib/crypt_ops/crypto_digest.h @@ -15,7 +15,7 @@ #include "lib/cc/torint.h" #include "lib/defs/digest_sizes.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" /** Length of a sha1 message digest when encoded in base32 with trailing = * signs removed. */ diff --git a/src/lib/crypt_ops/crypto_ed25519.c b/src/lib/crypt_ops/crypto_ed25519.c @@ -28,7 +28,7 @@ #include "lib/crypt_ops/crypto_format.h" #include "lib/crypt_ops/crypto_rand.h" #include "lib/crypt_ops/crypto_util.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/encoding/binascii.h" #include "lib/string/util_string.h" diff --git a/src/lib/crypt_ops/crypto_format.c b/src/lib/crypt_ops/crypto_format.c @@ -24,7 +24,7 @@ #include "lib/string/util_string.h" #include "lib/string/printf.h" #include "lib/encoding/binascii.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/fs/files.h" diff --git a/src/lib/crypt_ops/crypto_rand.c b/src/lib/crypt_ops/crypto_rand.c @@ -26,9 +26,9 @@ #include "lib/crypt_ops/crypto_util.h" #include "lib/encoding/binascii.h" #include "lib/intmath/weakrng.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/sandbox/sandbox.h" #include "lib/string/compat_string.h" #include "lib/string/util_string.h" diff --git a/src/lib/crypt_ops/crypto_rsa.c b/src/lib/crypt_ops/crypto_rsa.c @@ -36,7 +36,7 @@ DISABLE_GCC_WARNING(redundant-decls) ENABLE_GCC_WARNING(redundant-decls) -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/encoding/binascii.h" #include <string.h> diff --git a/src/lib/crypt_ops/crypto_rsa.h b/src/lib/crypt_ops/crypto_rsa.h @@ -18,7 +18,7 @@ #include "lib/crypt_ops/crypto_digest.h" #include "lib/cc/torint.h" #include "lib/testsupport/testsupport.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" /** Length of our public keys. */ #define PK_BYTES (1024/8) diff --git a/src/lib/crypt_ops/crypto_util.c b/src/lib/crypt_ops/crypto_util.c @@ -31,7 +31,7 @@ DISABLE_GCC_WARNING(redundant-decls) ENABLE_GCC_WARNING(redundant-decls) -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" /** diff --git a/src/lib/ctime/di_ops.c b/src/lib/ctime/di_ops.c @@ -9,7 +9,7 @@ #include "orconfig.h" #include "lib/ctime/di_ops.h" #include "lib/err/torerr.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include <string.h> diff --git a/src/lib/encoding/binascii.c b/src/lib/encoding/binascii.c @@ -14,12 +14,12 @@ #include "orconfig.h" #include "lib/encoding/binascii.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/cc/torint.h" #include "lib/string/compat_ctype.h" #include "lib/intmath/muldiv.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include <stddef.h> #include <string.h> diff --git a/src/lib/encoding/confline.c b/src/lib/encoding/confline.c @@ -17,9 +17,9 @@ #include "lib/encoding/confline.h" #include "lib/encoding/cstring.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/string/compat_ctype.h" #include "lib/string/compat_string.h" #include "lib/string/util_string.h" diff --git a/src/lib/encoding/cstring.c b/src/lib/encoding/cstring.c @@ -11,9 +11,9 @@ **/ #include "lib/encoding/cstring.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/string/compat_ctype.h" #include <string.h> diff --git a/src/lib/encoding/keyval.c b/src/lib/encoding/keyval.c @@ -12,7 +12,7 @@ #include "orconfig.h" #include "lib/encoding/keyval.h" #include "lib/log/escape.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include <stdlib.h> diff --git a/src/lib/encoding/time_fmt.c b/src/lib/encoding/time_fmt.c @@ -15,13 +15,13 @@ **/ #include "lib/encoding/time_fmt.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/escape.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/string/printf.h" #include "lib/string/scanf.h" -#include "lib/wallclock/tm_cvt.h" +#include "lib/wallclock/time_to_tm.h" #include <string.h> #include <time.h> diff --git a/src/lib/evloop/compat_libevent.c b/src/lib/evloop/compat_libevent.c @@ -11,7 +11,7 @@ #include "lib/evloop/compat_libevent.h" #include "lib/crypt_ops/crypto_rand.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/string/compat_string.h" diff --git a/src/lib/evloop/compat_libevent.h b/src/lib/evloop/compat_libevent.h @@ -11,7 +11,7 @@ #include "orconfig.h" #include "lib/testsupport/testsupport.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" void configure_libevent_logging(void); void suppress_libevent_log_msg(const char *msg); diff --git a/src/lib/evloop/procmon.c b/src/lib/evloop/procmon.c @@ -8,10 +8,10 @@ #include "lib/evloop/procmon.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/log/win32err.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/string/parse_int.h" #ifdef HAVE_SIGNAL_H diff --git a/src/lib/evloop/procmon.h b/src/lib/evloop/procmon.h @@ -11,7 +11,7 @@ #include "lib/evloop/compat_libevent.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" typedef struct tor_process_monitor_t tor_process_monitor_t; diff --git a/src/lib/evloop/timers.c b/src/lib/evloop/timers.c @@ -34,9 +34,9 @@ #include "lib/evloop/compat_libevent.h" #include "lib/evloop/timers.h" #include "lib/intmath/muldiv.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/time/compat_time.h" #ifdef _WIN32 diff --git a/src/lib/evloop/workqueue.c b/src/lib/evloop/workqueue.c @@ -30,7 +30,7 @@ #include "lib/crypt_ops/crypto_rand.h" #include "lib/intmath/weakrng.h" #include "lib/log/ratelim.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/net/alertsock.h" #include "lib/net/socket.h" diff --git a/src/lib/fs/conffile.c b/src/lib/fs/conffile.c @@ -17,8 +17,8 @@ #include "lib/fs/dir.h" #include "lib/fs/files.h" #include "lib/fs/path.h" -#include "lib/log/torlog.h" -#include "lib/malloc/util_malloc.h" +#include "lib/log/log.h" +#include "lib/malloc/malloc.h" #include "lib/string/printf.h" static smartlist_t *config_get_file_list(const char *path, diff --git a/src/lib/fs/dir.c b/src/lib/fs/dir.c @@ -14,12 +14,12 @@ #include "lib/fs/path.h" #include "lib/fs/userdb.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/log/win32err.h" #include "lib/container/smartlist.h" #include "lib/sandbox/sandbox.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/string/printf.h" #include "lib/string/compat_string.h" diff --git a/src/lib/fs/files.c b/src/lib/fs/files.c @@ -16,11 +16,11 @@ #include "lib/fs/files.h" #include "lib/fs/path.h" #include "lib/container/smartlist.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/log/escape.h" #include "lib/err/torerr.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/sandbox/sandbox.h" #include "lib/string/printf.h" #include "lib/string/util_string.h" diff --git a/src/lib/fs/lockfile.c b/src/lib/fs/lockfile.c @@ -13,9 +13,9 @@ #include "orconfig.h" #include "lib/fs/files.h" #include "lib/fs/lockfile.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #ifdef HAVE_SYS_FILE_H #include <sys/file.h> diff --git a/src/lib/fs/mmap.c b/src/lib/fs/mmap.c @@ -11,11 +11,11 @@ #include "lib/fs/mmap.h" #include "lib/fs/files.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/log/win32err.h" #include "lib/string/compat_string.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #ifdef HAVE_MMAP #include <sys/mman.h> diff --git a/src/lib/fs/path.c b/src/lib/fs/path.c @@ -10,8 +10,8 @@ **/ #include "lib/fs/path.h" -#include "lib/malloc/util_malloc.h" -#include "lib/log/torlog.h" +#include "lib/malloc/malloc.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/string/printf.h" #include "lib/string/util_string.h" diff --git a/src/lib/fs/storagedir.c b/src/lib/fs/storagedir.c @@ -20,9 +20,9 @@ #include "lib/fs/files.h" #include "lib/fs/mmap.h" #include "lib/log/escape.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/memarea/memarea.h" #include "lib/sandbox/sandbox.h" #include "lib/string/printf.h" diff --git a/src/lib/fs/userdb.c b/src/lib/fs/userdb.c @@ -12,8 +12,8 @@ #include "lib/fs/userdb.h" #ifndef _WIN32 -#include "lib/malloc/util_malloc.h" -#include "lib/log/torlog.h" +#include "lib/malloc/malloc.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include <pwd.h> diff --git a/src/lib/lock/compat_mutex.c b/src/lib/lock/compat_mutex.c @@ -10,7 +10,7 @@ **/ #include "lib/lock/compat_mutex.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" /** Return a newly allocated, ready-for-use mutex. */ tor_mutex_t * diff --git a/src/lib/lock/compat_mutex.h b/src/lib/lock/compat_mutex.h @@ -14,7 +14,7 @@ #include "orconfig.h" #include "lib/cc/torint.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #if defined(HAVE_PTHREAD_H) && !defined(_WIN32) #include <pthread.h> diff --git a/src/lib/log/escape.c b/src/lib/log/escape.c @@ -12,7 +12,7 @@ #include "lib/log/util_bug.h" #include "lib/string/compat_ctype.h" #include "lib/string/printf.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" /** Allocate and return a new string representing the contents of <b>s</b>, * surrounded by quotes and using standard C escapes. diff --git a/src/lib/log/include.am b/src/lib/log/include.am @@ -8,7 +8,7 @@ endif src_lib_libtor_log_a_SOURCES = \ src/lib/log/escape.c \ src/lib/log/ratelim.c \ - src/lib/log/torlog.c \ + src/lib/log/log.c \ src/lib/log/util_bug.c if WIN32 @@ -26,6 +26,6 @@ src/lib/log/torlog.$(OBJEXT) \ noinst_HEADERS += \ src/lib/log/escape.h \ src/lib/log/ratelim.h \ - src/lib/log/torlog.h \ + src/lib/log/log.h \ src/lib/log/util_bug.h \ src/lib/log/win32err.h diff --git a/src/lib/log/log.c b/src/lib/log/log.c @@ -0,0 +1,1488 @@ +/* Copyright (c) 2001, Matej Pfajfar. + * Copyright (c) 2001-2004, Roger Dingledine. + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file log.c + * \brief Functions to send messages to log files or the console. + **/ + +#include "orconfig.h" +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#ifdef HAVE_SYS_TIME_H +#include <sys/time.h> +#endif +#ifdef HAVE_TIME_H +#include <time.h> +#endif +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif +#ifdef HAVE_FCNTL_H +#include <fcntl.h> +#endif + +#define LOG_PRIVATE +#include "lib/log/log.h" +#include "lib/log/ratelim.h" +#include "lib/lock/compat_mutex.h" +#include "lib/smartlist_core/smartlist_core.h" +#include "lib/smartlist_core/smartlist_foreach.h" +#include "lib/smartlist_core/smartlist_split.h" +#include "lib/err/torerr.h" +#include "lib/intmath/bits.h" +#include "lib/string/compat_string.h" +#include "lib/string/printf.h" +#include "lib/malloc/malloc.h" +#include "lib/string/util_string.h" +#include "lib/wallclock/tor_gettimeofday.h" +#include "lib/wallclock/approx_time.h" +#include "lib/wallclock/time_to_tm.h" +#include "lib/fdio/fdio.h" + +#ifdef HAVE_ANDROID_LOG_H +#include <android/log.h> +#endif // HAVE_ANDROID_LOG_H. + +/** Given a severity, yields an index into log_severity_list_t.masks to use + * for that severity. */ +#define SEVERITY_MASK_IDX(sev) ((sev) - LOG_ERR) + +/** @{ */ +/** The string we stick at the end of a log message when it is too long, + * and its length. */ +#define TRUNCATED_STR "[...truncated]" +#define TRUNCATED_STR_LEN 14 +/** @} */ + +/** Defining compile-time constants for Tor log levels (used by the Rust + * log wrapper at src/rust/tor_log) */ +const int LOG_WARN_ = LOG_WARN; +const int LOG_NOTICE_ = LOG_NOTICE; +const log_domain_mask_t LD_GENERAL_ = LD_GENERAL; +const log_domain_mask_t LD_NET_ = LD_NET; + +/** Information for a single logfile; only used in log.c */ +typedef struct logfile_t { + struct logfile_t *next; /**< Next logfile_t in the linked list. */ + char *filename; /**< Filename to open. */ + int fd; /**< fd to receive log messages, or -1 for none. */ + int seems_dead; /**< Boolean: true if the stream seems to be kaput. */ + int needs_close; /**< Boolean: true if the stream gets closed on shutdown. */ + int is_temporary; /**< Boolean: close after initializing logging subsystem.*/ + int is_syslog; /**< Boolean: send messages to syslog. */ + int is_android; /**< Boolean: send messages to Android's log subsystem. */ + char *android_tag; /**< Identity Tag used in Android's log subsystem. */ + log_callback callback; /**< If not NULL, send messages to this function. */ + log_severity_list_t *severities; /**< Which severity of messages should we + * log for each log domain? */ +} logfile_t; + +static void log_free_(logfile_t *victim); +#define log_free(lg) \ + FREE_AND_NULL(logfile_t, log_free_, (lg)) + +/** Helper: map a log severity to descriptive string. */ +static inline const char * +sev_to_string(int severity) +{ + switch (severity) { + case LOG_DEBUG: return "debug"; + case LOG_INFO: return "info"; + case LOG_NOTICE: return "notice"; + case LOG_WARN: return "warn"; + case LOG_ERR: return "err"; + default: /* Call assert, not tor_assert, since tor_assert + * calls log on failure. */ + raw_assert_unreached(); return "UNKNOWN"; // LCOV_EXCL_LINE + } +} + +/** Helper: decide whether to include the function name in the log message. */ +static inline int +should_log_function_name(log_domain_mask_t domain, int severity) +{ + switch (severity) { + case LOG_DEBUG: + case LOG_INFO: + /* All debugging messages occur in interesting places. */ + return (domain & LD_NOFUNCNAME) == 0; + case LOG_NOTICE: + case LOG_WARN: + case LOG_ERR: + /* We care about places where bugs occur. */ + return (domain & (LD_BUG|LD_NOFUNCNAME)) == LD_BUG; + default: + /* Call assert, not tor_assert, since tor_assert calls log on failure. */ + raw_assert(0); return 0; // LCOV_EXCL_LINE + } +} + +#ifdef HAVE_ANDROID_LOG_H +/** Helper function to convert Tor's log severity into the matching + * Android log priority. + */ +static int +severity_to_android_log_priority(int severity) +{ + switch (severity) { + case LOG_DEBUG: + return ANDROID_LOG_VERBOSE; + case LOG_INFO: + return ANDROID_LOG_DEBUG; + case LOG_NOTICE: + return ANDROID_LOG_INFO; + case LOG_WARN: + return ANDROID_LOG_WARN; + case LOG_ERR: + return ANDROID_LOG_ERROR; + default: + // LCOV_EXCL_START + raw_assert(0); + return 0; + // LCOV_EXCL_STOP + } +} +#endif // HAVE_ANDROID_LOG_H. + +/** A mutex to guard changes to logfiles and logging. */ +static tor_mutex_t log_mutex; +/** True iff we have initialized log_mutex */ +static int log_mutex_initialized = 0; + +/** Linked list of logfile_t. */ +static logfile_t *logfiles = NULL; +/** Boolean: do we report logging domains? */ +static int log_domains_are_logged = 0; + +#ifdef HAVE_SYSLOG_H +/** The number of open syslog log handlers that we have. When this reaches 0, + * we can close our connection to the syslog facility. */ +static int syslog_count = 0; +#endif + +/** Represents a log message that we are going to send to callback-driven + * loggers once we can do so in a non-reentrant way. */ +typedef struct pending_log_message_t { + int severity; /**< The severity of the message */ + log_domain_mask_t domain; /**< The domain of the message */ + char *fullmsg; /**< The message, with all decorations */ + char *msg; /**< The content of the message */ +} pending_log_message_t; + +/** Log messages waiting to be replayed onto callback-based logs */ +static smartlist_t *pending_cb_messages = NULL; + +/** Callback to invoke when pending_cb_messages becomes nonempty. */ +static pending_callback_callback pending_cb_cb = NULL; + +/** Log messages waiting to be replayed once the logging system is initialized. + */ +static smartlist_t *pending_startup_messages = NULL; + +/** Number of bytes of messages queued in pending_startup_messages. (This is + * the length of the messages, not the number of bytes used to store + * them.) */ +static size_t pending_startup_messages_len; + +/** True iff we should store messages while waiting for the logs to get + * configured. */ +static int queue_startup_messages = 1; + +/** True iff __PRETTY_FUNCTION__ includes parenthesized arguments. */ +static int pretty_fn_has_parens = 0; + +/** Don't store more than this many bytes of messages while waiting for the + * logs to get configured. */ +#define MAX_STARTUP_MSG_LEN (1<<16) + +/** Lock the log_mutex to prevent others from changing the logfile_t list */ +#define LOCK_LOGS() STMT_BEGIN \ + raw_assert(log_mutex_initialized); \ + tor_mutex_acquire(&log_mutex); \ + STMT_END +/** Unlock the log_mutex */ +#define UNLOCK_LOGS() STMT_BEGIN \ + raw_assert(log_mutex_initialized); \ + tor_mutex_release(&log_mutex); \ + STMT_END + +/** What's the lowest log level anybody cares about? Checking this lets us + * bail out early from log_debug if we aren't debugging. */ +int log_global_min_severity_ = LOG_NOTICE; + +static void delete_log(logfile_t *victim); +static void close_log(logfile_t *victim); + +static char *domain_to_string(log_domain_mask_t domain, + char *buf, size_t buflen); +static inline char *format_msg(char *buf, size_t buf_len, + log_domain_mask_t domain, int severity, const char *funcname, + const char *suffix, + const char *format, va_list ap, size_t *msg_len_out) + CHECK_PRINTF(7,0); + +/** Name of the application: used to generate the message we write at the + * start of each new log. */ +static char *appname = NULL; + +/** Set the "application name" for the logs to <b>name</b>: we'll use this + * name in the message we write when starting up, and at the start of each new + * log. + * + * Tor uses this string to write the version number to the log file. */ +void +log_set_application_name(const char *name) +{ + tor_free(appname); + appname = name ? tor_strdup(name) : NULL; +} + +/** Return true if some of the running logs might be interested in a log + * message of the given severity in the given domains. If this function + * returns true, the log message might be ignored anyway, but if it returns + * false, it is definitely_ safe not to log the message. */ +int +log_message_is_interesting(int severity, log_domain_mask_t domain) +{ + (void) domain; + return (severity <= log_global_min_severity_); +} + +/** + * As tor_log, but takes an optional function name, and does not treat its + * <b>string</b> as a printf format. + * + * For use by Rust integration. + */ +void +tor_log_string(int severity, log_domain_mask_t domain, + const char *function, const char *string) +{ + log_fn_(severity, domain, function, "%s", string); +} + +/** Log time granularity in milliseconds. */ +static int log_time_granularity = 1; + +/** Define log time granularity for all logs to be <b>granularity_msec</b> + * milliseconds. */ +void +set_log_time_granularity(int granularity_msec) +{ + log_time_granularity = granularity_msec; + tor_log_sigsafe_err_set_granularity(granularity_msec); +} + +/** Helper: Write the standard prefix for log lines to a + * <b>buf_len</b> character buffer in <b>buf</b>. + */ +static inline size_t +log_prefix_(char *buf, size_t buf_len, int severity) +{ + time_t t; + struct timeval now; + struct tm tm; + size_t n; + int r, ms; + + tor_gettimeofday(&now); + t = (time_t)now.tv_sec; + ms = (int)now.tv_usec / 1000; + if (log_time_granularity >= 1000) { + t -= t % (log_time_granularity / 1000); + ms = 0; + } else { + ms -= ((int)now.tv_usec / 1000) % log_time_granularity; + } + + n = strftime(buf, buf_len, "%b %d %H:%M:%S", + tor_localtime_r_msg(&t, &tm, NULL)); + r = tor_snprintf(buf+n, buf_len-n, ".%.3i [%s] ", ms, + sev_to_string(severity)); + + if (r<0) + return buf_len-1; + else + return n+r; +} + +/** If lf refers to an actual file that we have just opened, and the file + * contains no data, log an "opening new logfile" message at the top. + * + * Return -1 if the log is broken and needs to be deleted, else return 0. + */ +static int +log_tor_version(logfile_t *lf, int reset) +{ + char buf[256]; + size_t n; + int is_new; + + if (!lf->needs_close) + /* If it doesn't get closed, it isn't really a file. */ + return 0; + if (lf->is_temporary) + /* If it's temporary, it isn't really a file. */ + return 0; + + is_new = lf->fd >= 0 && tor_fd_getpos(lf->fd) == 0; + + if (reset && !is_new) + /* We are resetting, but we aren't at the start of the file; no + * need to log again. */ + return 0; + n = log_prefix_(buf, sizeof(buf), LOG_NOTICE); + if (appname) { + tor_snprintf(buf+n, sizeof(buf)-n, + "%s opening %slog file.\n", appname, is_new?"new ":""); + } else { + tor_snprintf(buf+n, sizeof(buf)-n, + "Tor %s opening %slog file.\n", VERSION, is_new?"new ":""); + } + if (write_all_to_fd_minimal(lf->fd, buf, strlen(buf)) < 0) /* error */ + return -1; /* failed */ + return 0; +} + +static const char bug_suffix[] = " (on Tor " VERSION +#ifndef _MSC_VER + " " +#include "micro-revision.i" +#endif + ")"; + +/** Helper: Format a log message into a fixed-sized buffer. (This is + * factored out of <b>logv</b> so that we never format a message more + * than once.) Return a pointer to the first character of the message + * portion of the formatted string. + */ +static inline char * +format_msg(char *buf, size_t buf_len, + log_domain_mask_t domain, int severity, const char *funcname, + const char *suffix, + const char *format, va_list ap, size_t *msg_len_out) +{ + size_t n; + int r; + char *end_of_prefix; + char *buf_end; + + raw_assert(buf_len >= 16); /* prevent integer underflow and stupidity */ + buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */ + buf_end = buf+buf_len; /* point *after* the last char we can write to */ + + n = log_prefix_(buf, buf_len, severity); + end_of_prefix = buf+n; + + if (log_domains_are_logged) { + char *cp = buf+n; + if (cp == buf_end) goto format_msg_no_room_for_domains; + *cp++ = '{'; + if (cp == buf_end) goto format_msg_no_room_for_domains; + cp = domain_to_string(domain, cp, (buf+buf_len-cp)); + if (cp == buf_end) goto format_msg_no_room_for_domains; + *cp++ = '}'; + if (cp == buf_end) goto format_msg_no_room_for_domains; + *cp++ = ' '; + if (cp == buf_end) goto format_msg_no_room_for_domains; + end_of_prefix = cp; + n = cp-buf; + format_msg_no_room_for_domains: + /* This will leave end_of_prefix and n unchanged, and thus cause + * whatever log domain string we had written to be clobbered. */ + ; + } + + if (funcname && should_log_function_name(domain, severity)) { + r = tor_snprintf(buf+n, buf_len-n, + pretty_fn_has_parens ? "%s: " : "%s(): ", + funcname); + if (r<0) + n = strlen(buf); + else + n += r; + } + + if (domain == LD_BUG && buf_len-n > 6) { + memcpy(buf+n, "Bug: ", 6); + n += 5; + } + + r = tor_vsnprintf(buf+n,buf_len-n,format,ap); + if (r < 0) { + /* The message was too long; overwrite the end of the buffer with + * "[...truncated]" */ + if (buf_len >= TRUNCATED_STR_LEN) { + size_t offset = buf_len-TRUNCATED_STR_LEN; + /* We have an extra 2 characters after buf_len to hold the \n\0, + * so it's safe to add 1 to the size here. */ + strlcpy(buf+offset, TRUNCATED_STR, buf_len-offset+1); + } + /* Set 'n' to the end of the buffer, where we'll be writing \n\0. + * Since we already subtracted 2 from buf_len, this is safe.*/ + n = buf_len; + } else { + n += r; + if (suffix) { + size_t suffix_len = strlen(suffix); + if (buf_len-n >= suffix_len) { + memcpy(buf+n, suffix, suffix_len); + n += suffix_len; + } + } + } + + if (domain == LD_BUG && + buf_len - n > strlen(bug_suffix)+1) { + memcpy(buf+n, bug_suffix, strlen(bug_suffix)); + n += strlen(bug_suffix); + } + + buf[n]='\n'; + buf[n+1]='\0'; + *msg_len_out = n+1; + return end_of_prefix; +} + +/* Create a new pending_log_message_t with appropriate values */ +static pending_log_message_t * +pending_log_message_new(int severity, log_domain_mask_t domain, + const char *fullmsg, const char *shortmsg) +{ + pending_log_message_t *m = tor_malloc(sizeof(pending_log_message_t)); + m->severity = severity; + m->domain = domain; + m->fullmsg = fullmsg ? tor_strdup(fullmsg) : NULL; + m->msg = tor_strdup(shortmsg); + return m; +} + +#define pending_log_message_free(msg) \ + FREE_AND_NULL(pending_log_message_t, pending_log_message_free_, (msg)) + +/** Release all storage held by <b>msg</b>. */ +static void +pending_log_message_free_(pending_log_message_t *msg) +{ + if (!msg) + return; + tor_free(msg->msg); + tor_free(msg->fullmsg); + tor_free(msg); +} + +/** Helper function: returns true iff the log file, given in <b>lf</b>, is + * handled externally via the system log API, the Android logging API, or is an + * external callback function. */ +static inline int +logfile_is_external(const logfile_t *lf) +{ + raw_assert(lf); + return lf->is_syslog || lf->is_android || lf->callback; +} + +/** Return true iff <b>lf</b> would like to receive a message with the + * specified <b>severity</b> in the specified <b>domain</b>. + */ +static inline int +logfile_wants_message(const logfile_t *lf, int severity, + log_domain_mask_t domain) +{ + if (! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) { + return 0; + } + if (! (lf->fd >= 0 || logfile_is_external(lf))) { + return 0; + } + if (lf->seems_dead) { + return 0; + } + + return 1; +} + +/** Send a message to <b>lf</b>. The full message, with time prefix and + * severity, is in <b>buf</b>. The message itself is in + * <b>msg_after_prefix</b>. If <b>callbacks_deferred</b> points to true, then + * we already deferred this message for pending callbacks and don't need to do + * it again. Otherwise, if we need to do it, do it, and set + * <b>callbacks_deferred</b> to 1. */ +static inline void +logfile_deliver(logfile_t *lf, const char *buf, size_t msg_len, + const char *msg_after_prefix, log_domain_mask_t domain, + int severity, int *callbacks_deferred) +{ + + if (lf->is_syslog) { +#ifdef HAVE_SYSLOG_H +#ifdef MAXLINE + /* Some syslog implementations have limits on the length of what you can + * pass them, and some very old ones do not detect overflow so well. + * Regrettably, they call their maximum line length MAXLINE. */ +#if MAXLINE < 64 +#warn "MAXLINE is a very low number; it might not be from syslog.h after all" +#endif + char *m = msg_after_prefix; + if (msg_len >= MAXLINE) + m = tor_strndup(msg_after_prefix, MAXLINE-1); + syslog(severity, "%s", m); + if (m != msg_after_prefix) { + tor_free(m); + } +#else /* !(defined(MAXLINE)) */ + /* We have syslog but not MAXLINE. That's promising! */ + syslog(severity, "%s", msg_after_prefix); +#endif /* defined(MAXLINE) */ +#endif /* defined(HAVE_SYSLOG_H) */ + } else if (lf->is_android) { +#ifdef HAVE_ANDROID_LOG_H + int priority = severity_to_android_log_priority(severity); + __android_log_write(priority, lf->android_tag, msg_after_prefix); +#endif // HAVE_ANDROID_LOG_H. + } else if (lf->callback) { + if (domain & LD_NOCB) { + if (!*callbacks_deferred && pending_cb_messages) { + smartlist_add(pending_cb_messages, + pending_log_message_new(severity,domain,NULL,msg_after_prefix)); + *callbacks_deferred = 1; + if (smartlist_len(pending_cb_messages) == 1 && pending_cb_cb) { + pending_cb_cb(); + } + } + } else { + lf->callback(severity, domain, msg_after_prefix); + } + } else { + if (write_all_to_fd_minimal(lf->fd, buf, msg_len) < 0) { /* error */ + /* don't log the error! mark this log entry to be blown away, and + * continue. */ + lf->seems_dead = 1; + } + } +} + +/** Helper: sends a message to the appropriate logfiles, at loglevel + * <b>severity</b>. If provided, <b>funcname</b> is prepended to the + * message. The actual message is derived as from tor_snprintf(format,ap). + */ +MOCK_IMPL(STATIC void, +logv,(int severity, log_domain_mask_t domain, const char *funcname, + const char *suffix, const char *format, va_list ap)) +{ + char buf[10240]; + size_t msg_len = 0; + int formatted = 0; + logfile_t *lf; + char *end_of_prefix=NULL; + int callbacks_deferred = 0; + + /* Call assert, not raw_assert, since raw_assert calls log on failure. */ + raw_assert(format); + /* check that severity is sane. Overrunning the masks array leads to + * interesting and hard to diagnose effects */ + raw_assert(severity >= LOG_ERR && severity <= LOG_DEBUG); + /* check that we've initialised the log mutex before we try to lock it */ + raw_assert(log_mutex_initialized); + LOCK_LOGS(); + + if ((! (domain & LD_NOCB)) && pending_cb_messages + && smartlist_len(pending_cb_messages)) + flush_pending_log_callbacks(); + + if (queue_startup_messages && + pending_startup_messages_len < MAX_STARTUP_MSG_LEN) { + end_of_prefix = + format_msg(buf, sizeof(buf), domain, severity, funcname, suffix, + format, ap, &msg_len); + formatted = 1; + + smartlist_add(pending_startup_messages, + pending_log_message_new(severity,domain,buf,end_of_prefix)); + pending_startup_messages_len += msg_len; + } + + for (lf = logfiles; lf; lf = lf->next) { + if (! logfile_wants_message(lf, severity, domain)) + continue; + + if (!formatted) { + end_of_prefix = + format_msg(buf, sizeof(buf), domain, severity, funcname, suffix, + format, ap, &msg_len); + formatted = 1; + } + + logfile_deliver(lf, buf, msg_len, end_of_prefix, domain, severity, + &callbacks_deferred); + } + UNLOCK_LOGS(); +} + +/** Output a message to the log. It gets logged to all logfiles that + * care about messages with <b>severity</b> in <b>domain</b>. The content + * is formatted printf-style based on <b>format</b> and extra arguments. + * */ +void +tor_log(int severity, log_domain_mask_t domain, const char *format, ...) +{ + va_list ap; + if (severity > log_global_min_severity_) + return; + va_start(ap,format); +#ifdef TOR_UNIT_TESTS + if (domain & LD_NO_MOCK) + logv__real(severity, domain, NULL, NULL, format, ap); + else +#endif + logv(severity, domain, NULL, NULL, format, ap); + va_end(ap); +} + +/** Helper function; return true iff the <b>n</b>-element array <b>array</b> + * contains <b>item</b>. */ +static int +int_array_contains(const int *array, int n, int item) +{ + int j; + for (j = 0; j < n; ++j) { + if (array[j] == item) + return 1; + } + return 0; +} + +/** Function to call whenever the list of logs changes to get ready to log + * from signal handlers. */ +void +tor_log_update_sigsafe_err_fds(void) +{ + const logfile_t *lf; + int found_real_stderr = 0; + + int fds[TOR_SIGSAFE_LOG_MAX_FDS]; + int n_fds; + + LOCK_LOGS(); + /* Reserve the first one for stderr. This is safe because when we daemonize, + * we dup2 /dev/null to stderr, */ + fds[0] = STDERR_FILENO; + n_fds = 1; + + for (lf = logfiles; lf; lf = lf->next) { + /* Don't try callback to the control port, or syslogs: We can't + * do them from a signal handler. Don't try stdout: we always do stderr. + */ + if (lf->is_temporary || logfile_is_external(lf) + || lf->seems_dead || lf->fd < 0) + continue; + if (lf->severities->masks[SEVERITY_MASK_IDX(LOG_ERR)] & + (LD_BUG|LD_GENERAL)) { + if (lf->fd == STDERR_FILENO) + found_real_stderr = 1; + /* Avoid duplicates */ + if (int_array_contains(fds, n_fds, lf->fd)) + continue; + fds[n_fds++] = lf->fd; + if (n_fds == TOR_SIGSAFE_LOG_MAX_FDS) + break; + } + } + + if (!found_real_stderr && + int_array_contains(fds, n_fds, STDOUT_FILENO)) { + /* Don't use a virtual stderr when we're also logging to stdout. */ + raw_assert(n_fds >= 2); /* Don't raw_assert inside log fns */ + fds[0] = fds[--n_fds]; + } + + UNLOCK_LOGS(); + + tor_log_set_sigsafe_err_fds(fds, n_fds); +} + +/** Add to <b>out</b> a copy of every currently configured log file name. Used + * to enable access to these filenames with the sandbox code. */ +void +tor_log_get_logfile_names(smartlist_t *out) +{ + logfile_t *lf; + raw_assert(out); + + LOCK_LOGS(); + + for (lf = logfiles; lf; lf = lf->next) { + if (lf->is_temporary || logfile_is_external(lf)) + continue; + if (lf->filename == NULL) + continue; + smartlist_add_strdup(out, lf->filename); + } + + UNLOCK_LOGS(); +} + +/** Implementation of the log_fn backend, used when we have + * variadic macros. All arguments are as for log_fn, except for + * <b>fn</b>, which is the name of the calling functions. */ +void +log_fn_(int severity, log_domain_mask_t domain, const char *fn, + const char *format, ...) +{ + va_list ap; + if (severity > log_global_min_severity_) + return; + va_start(ap,format); + logv(severity, domain, fn, NULL, format, ap); + va_end(ap); +} +void +log_fn_ratelim_(ratelim_t *ratelim, int severity, log_domain_mask_t domain, + const char *fn, const char *format, ...) +{ + va_list ap; + char *m; + if (severity > log_global_min_severity_) + return; + m = rate_limit_log(ratelim, approx_time()); + if (m == NULL) + return; + va_start(ap, format); + logv(severity, domain, fn, m, format, ap); + va_end(ap); + tor_free(m); +} + +/** Free all storage held by <b>victim</b>. */ +static void +log_free_(logfile_t *victim) +{ + if (!victim) + return; + tor_free(victim->severities); + tor_free(victim->filename); + tor_free(victim->android_tag); + tor_free(victim); +} + +/** Close all open log files, and free other static memory. */ +void +logs_free_all(void) +{ + logfile_t *victim, *next; + smartlist_t *messages, *messages2; + LOCK_LOGS(); + next = logfiles; + logfiles = NULL; + messages = pending_cb_messages; + pending_cb_messages = NULL; + pending_cb_cb = NULL; + messages2 = pending_startup_messages; + pending_startup_messages = NULL; + UNLOCK_LOGS(); + while (next) { + victim = next; + next = next->next; + close_log(victim); + log_free(victim); + } + tor_free(appname); + + SMARTLIST_FOREACH(messages, pending_log_message_t *, msg, { + pending_log_message_free(msg); + }); + smartlist_free(messages); + + if (messages2) { + SMARTLIST_FOREACH(messages2, pending_log_message_t *, msg, { + pending_log_message_free(msg); + }); + smartlist_free(messages2); + } + + /* We _could_ destroy the log mutex here, but that would screw up any logs + * that happened between here and the end of execution. */ +} + +/** Remove and free the log entry <b>victim</b> from the linked-list + * logfiles (it is probably present, but it might not be due to thread + * racing issues). After this function is called, the caller shouldn't + * refer to <b>victim</b> anymore. + * + * Long-term, we need to do something about races in the log subsystem + * in general. See bug 222 for more details. + */ +static void +delete_log(logfile_t *victim) +{ + logfile_t *tmpl; + if (victim == logfiles) + logfiles = victim->next; + else { + for (tmpl = logfiles; tmpl && tmpl->next != victim; tmpl=tmpl->next) ; +// raw_assert(tmpl); +// raw_assert(tmpl->next == victim); + if (!tmpl) + return; + tmpl->next = victim->next; + } + log_free(victim); +} + +/** Helper: release system resources (but not memory) held by a single + * logfile_t. */ +static void +close_log(logfile_t *victim) +{ + if (victim->needs_close && victim->fd >= 0) { + close(victim->fd); + victim->fd = -1; + } else if (victim->is_syslog) { +#ifdef HAVE_SYSLOG_H + if (--syslog_count == 0) { + /* There are no other syslogs; close the logging facility. */ + closelog(); + } +#endif /* defined(HAVE_SYSLOG_H) */ + } +} + +/** Adjust a log severity configuration in <b>severity_out</b> to contain + * every domain between <b>loglevelMin</b> and <b>loglevelMax</b>, inclusive. + */ +void +set_log_severity_config(int loglevelMin, int loglevelMax, + log_severity_list_t *severity_out) +{ + int i; + raw_assert(loglevelMin >= loglevelMax); + raw_assert(loglevelMin >= LOG_ERR && loglevelMin <= LOG_DEBUG); + raw_assert(loglevelMax >= LOG_ERR && loglevelMax <= LOG_DEBUG); + memset(severity_out, 0, sizeof(log_severity_list_t)); + for (i = loglevelMin; i >= loglevelMax; --i) { + severity_out->masks[SEVERITY_MASK_IDX(i)] = ~0u; + } +} + +/** Add a log handler named <b>name</b> to send all messages in <b>severity</b> + * to <b>fd</b>. Copies <b>severity</b>. Helper: does no locking. */ +static void +add_stream_log_impl(const log_severity_list_t *severity, + const char *name, int fd) +{ + logfile_t *lf; + lf = tor_malloc_zero(sizeof(logfile_t)); + lf->fd = fd; + lf->filename = tor_strdup(name); + lf->severities = tor_memdup(severity, sizeof(log_severity_list_t)); + lf->next = logfiles; + + logfiles = lf; + log_global_min_severity_ = get_min_log_level(); +} + +/** Add a log handler named <b>name</b> to send all messages in <b>severity</b> + * to <b>fd</b>. Steals a reference to <b>severity</b>; the caller must + * not use it after calling this function. */ +void +add_stream_log(const log_severity_list_t *severity, const char *name, int fd) +{ + LOCK_LOGS(); + add_stream_log_impl(severity, name, fd); + UNLOCK_LOGS(); +} + +/** Initialize the global logging facility */ +void +init_logging(int disable_startup_queue) +{ + if (!log_mutex_initialized) { + tor_mutex_init(&log_mutex); + log_mutex_initialized = 1; + } +#ifdef __GNUC__ + if (strchr(__PRETTY_FUNCTION__, '(')) { + pretty_fn_has_parens = 1; + } +#endif + if (pending_cb_messages == NULL) + pending_cb_messages = smartlist_new(); + if (disable_startup_queue) + queue_startup_messages = 0; + if (pending_startup_messages == NULL && queue_startup_messages) { + pending_startup_messages = smartlist_new(); + } +} + +/** Set whether we report logging domains as a part of our log messages. + */ +void +logs_set_domain_logging(int enabled) +{ + LOCK_LOGS(); + log_domains_are_logged = enabled; + UNLOCK_LOGS(); +} + +/** Add a log handler to receive messages during startup (before the real + * logs are initialized). + */ +void +add_temp_log(int min_severity) +{ + log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t)); + set_log_severity_config(min_severity, LOG_ERR, s); + LOCK_LOGS(); + add_stream_log_impl(s, "<temp>", fileno(stdout)); + tor_free(s); + logfiles->is_temporary = 1; + UNLOCK_LOGS(); +} + +/** + * Register "cb" as the callback to call when there are new pending log + * callbacks to be flushed with flush_pending_log_callbacks(). + * + * Note that this callback, if present, can be invoked from any thread. + * + * This callback must not log. + * + * It is intentional that this function contains the name "callback" twice: it + * sets a "callback" to be called on the condition that there is a "pending + * callback". + **/ +void +logs_set_pending_callback_callback(pending_callback_callback cb) +{ + pending_cb_cb = cb; +} + +/** + * Add a log handler to send messages in <b>severity</b> + * to the function <b>cb</b>. + */ +int +add_callback_log(const log_severity_list_t *severity, log_callback cb) +{ + logfile_t *lf; + lf = tor_malloc_zero(sizeof(logfile_t)); + lf->fd = -1; + lf->severities = tor_memdup(severity, sizeof(log_severity_list_t)); + lf->filename = tor_strdup("<callback>"); + lf->callback = cb; + lf->next = logfiles; + + LOCK_LOGS(); + logfiles = lf; + log_global_min_severity_ = get_min_log_level(); + UNLOCK_LOGS(); + return 0; +} + +/** Adjust the configured severity of any logs whose callback function is + * <b>cb</b>. */ +void +change_callback_log_severity(int loglevelMin, int loglevelMax, + log_callback cb) +{ + logfile_t *lf; + log_severity_list_t severities; + set_log_severity_config(loglevelMin, loglevelMax, &severities); + LOCK_LOGS(); + for (lf = logfiles; lf; lf = lf->next) { + if (lf->callback == cb) { + memcpy(lf->severities, &severities, sizeof(severities)); + } + } + log_global_min_severity_ = get_min_log_level(); + UNLOCK_LOGS(); +} + +/** If there are any log messages that were generated with LD_NOCB waiting to + * be sent to callback-based loggers, send them now. */ +void +flush_pending_log_callbacks(void) +{ + logfile_t *lf; + smartlist_t *messages, *messages_tmp; + + LOCK_LOGS(); + if (!pending_cb_messages || 0 == smartlist_len(pending_cb_messages)) { + UNLOCK_LOGS(); + return; + } + + messages = pending_cb_messages; + pending_cb_messages = smartlist_new(); + do { + SMARTLIST_FOREACH_BEGIN(messages, pending_log_message_t *, msg) { + const int severity = msg->severity; + const int domain = msg->domain; + for (lf = logfiles; lf; lf = lf->next) { + if (! lf->callback || lf->seems_dead || + ! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) { + continue; + } + lf->callback(severity, domain, msg->msg); + } + pending_log_message_free(msg); + } SMARTLIST_FOREACH_END(msg); + smartlist_clear(messages); + + messages_tmp = pending_cb_messages; + pending_cb_messages = messages; + messages = messages_tmp; + } while (smartlist_len(messages)); + + smartlist_free(messages); + + UNLOCK_LOGS(); +} + +/** Flush all the messages we stored from startup while waiting for log + * initialization. + */ +void +flush_log_messages_from_startup(void) +{ + logfile_t *lf; + + LOCK_LOGS(); + queue_startup_messages = 0; + pending_startup_messages_len = 0; + if (! pending_startup_messages) + goto out; + + SMARTLIST_FOREACH_BEGIN(pending_startup_messages, pending_log_message_t *, + msg) { + int callbacks_deferred = 0; + for (lf = logfiles; lf; lf = lf->next) { + if (! logfile_wants_message(lf, msg->severity, msg->domain)) + continue; + + /* We configure a temporary startup log that goes to stdout, so we + * shouldn't replay to stdout/stderr*/ + if (lf->fd == STDOUT_FILENO || lf->fd == STDERR_FILENO) { + continue; + } + + logfile_deliver(lf, msg->fullmsg, strlen(msg->fullmsg), msg->msg, + msg->domain, msg->severity, &callbacks_deferred); + } + pending_log_message_free(msg); + } SMARTLIST_FOREACH_END(msg); + smartlist_free(pending_startup_messages); + pending_startup_messages = NULL; + + out: + UNLOCK_LOGS(); +} + +/** Close any log handlers added by add_temp_log() or marked by + * mark_logs_temp(). */ +void +close_temp_logs(void) +{ + logfile_t *lf, **p; + + LOCK_LOGS(); + for (p = &logfiles; *p; ) { + if ((*p)->is_temporary) { + lf = *p; + /* we use *p here to handle the edge case of the head of the list */ + *p = (*p)->next; + close_log(lf); + log_free(lf); + } else { + p = &((*p)->next); + } + } + + log_global_min_severity_ = get_min_log_level(); + UNLOCK_LOGS(); +} + +/** Make all currently temporary logs (set to be closed by close_temp_logs) + * live again, and close all non-temporary logs. */ +void +rollback_log_changes(void) +{ + logfile_t *lf; + LOCK_LOGS(); + for (lf = logfiles; lf; lf = lf->next) + lf->is_temporary = ! lf->is_temporary; + UNLOCK_LOGS(); + close_temp_logs(); +} + +/** Configure all log handles to be closed by close_temp_logs(). */ +void +mark_logs_temp(void) +{ + logfile_t *lf; + LOCK_LOGS(); + for (lf = logfiles; lf; lf = lf->next) + lf->is_temporary = 1; + UNLOCK_LOGS(); +} + +/** + * Add a log handler to send messages to <b>filename</b> via <b>fd</b>. If + * opening the logfile failed, -1 is returned and errno is set appropriately + * (by open(2)). Takes ownership of fd. + */ +int +add_file_log(const log_severity_list_t *severity, + const char *filename, + int fd) +{ + logfile_t *lf; + + if (fd<0) + return -1; + if (tor_fd_seekend(fd)<0) { + close(fd); + return -1; + } + + LOCK_LOGS(); + add_stream_log_impl(severity, filename, fd); + logfiles->needs_close = 1; + lf = logfiles; + log_global_min_severity_ = get_min_log_level(); + + if (log_tor_version(lf, 0) < 0) { + delete_log(lf); + } + UNLOCK_LOGS(); + + return 0; +} + +#ifdef HAVE_SYSLOG_H +/** + * Add a log handler to send messages to they system log facility. + * + * If this is the first log handler, opens syslog with ident Tor or + * Tor-<syslog_identity_tag> if that is not NULL. + */ +int +add_syslog_log(const log_severity_list_t *severity, + const char* syslog_identity_tag) +{ + logfile_t *lf; + if (syslog_count++ == 0) { + /* This is the first syslog. */ + static char buf[256]; + if (syslog_identity_tag) { + tor_snprintf(buf, sizeof(buf), "Tor-%s", syslog_identity_tag); + } else { + tor_snprintf(buf, sizeof(buf), "Tor"); + } + openlog(buf, LOG_PID | LOG_NDELAY, LOGFACILITY); + } + + lf = tor_malloc_zero(sizeof(logfile_t)); + lf->fd = -1; + lf->severities = tor_memdup(severity, sizeof(log_severity_list_t)); + lf->filename = tor_strdup("<syslog>"); + lf->is_syslog = 1; + + LOCK_LOGS(); + lf->next = logfiles; + logfiles = lf; + log_global_min_severity_ = get_min_log_level(); + UNLOCK_LOGS(); + return 0; +} +#endif /* defined(HAVE_SYSLOG_H) */ + +#ifdef HAVE_ANDROID_LOG_H +/** + * Add a log handler to send messages to the Android platform log facility. + */ +int +add_android_log(const log_severity_list_t *severity, + const char *android_tag) +{ + logfile_t *lf = NULL; + + lf = tor_malloc_zero(sizeof(logfile_t)); + lf->fd = -1; + lf->severities = tor_memdup(severity, sizeof(log_severity_list_t)); + lf->filename = tor_strdup("<android>"); + lf->is_android = 1; + + if (android_tag == NULL) + lf->android_tag = tor_strdup("Tor"); + else { + char buf[256]; + tor_snprintf(buf, sizeof(buf), "Tor-%s", android_tag); + lf->android_tag = tor_strdup(buf); + } + + LOCK_LOGS(); + lf->next = logfiles; + logfiles = lf; + log_global_min_severity_ = get_min_log_level(); + UNLOCK_LOGS(); + return 0; +} +#endif // HAVE_ANDROID_LOG_H. + +/** If <b>level</b> is a valid log severity, return the corresponding + * numeric value. Otherwise, return -1. */ +int +parse_log_level(const char *level) +{ + if (!strcasecmp(level, "err")) + return LOG_ERR; + if (!strcasecmp(level, "warn")) + return LOG_WARN; + if (!strcasecmp(level, "notice")) + return LOG_NOTICE; + if (!strcasecmp(level, "info")) + return LOG_INFO; + if (!strcasecmp(level, "debug")) + return LOG_DEBUG; + return -1; +} + +/** Return the string equivalent of a given log level. */ +const char * +log_level_to_string(int level) +{ + return sev_to_string(level); +} + +/** NULL-terminated array of names for log domains such that domain_list[dom] + * is a description of <b>dom</b>. + * + * Remember to update doc/tor.1.txt if you modify this list. + * */ +static const char *domain_list[] = { + "GENERAL", "CRYPTO", "NET", "CONFIG", "FS", "PROTOCOL", "MM", + "HTTP", "APP", "CONTROL", "CIRC", "REND", "BUG", "DIR", "DIRSERV", + "OR", "EDGE", "ACCT", "HIST", "HANDSHAKE", "HEARTBEAT", "CHANNEL", + "SCHED", "GUARD", "CONSDIFF", "DOS", NULL +}; + +/** Return a bitmask for the log domain for which <b>domain</b> is the name, + * or 0 if there is no such name. */ +static log_domain_mask_t +parse_log_domain(const char *domain) +{ + int i; + for (i=0; domain_list[i]; ++i) { + if (!strcasecmp(domain, domain_list[i])) + return (1u<<i); + } + return 0; +} + +/** Translate a bitmask of log domains to a string. */ +static char * +domain_to_string(log_domain_mask_t domain, char *buf, size_t buflen) +{ + char *cp = buf; + char *eos = buf+buflen; + + buf[0] = '\0'; + if (! domain) + return buf; + while (1) { + const char *d; + int bit = tor_log2(domain); + size_t n; + if ((unsigned)bit >= ARRAY_LENGTH(domain_list)-1 || + bit >= N_LOGGING_DOMAINS) { + tor_snprintf(buf, buflen, "<BUG:Unknown domain %lx>", (long)domain); + return buf+strlen(buf); + } + d = domain_list[bit]; + n = strlcpy(cp, d, eos-cp); + if (n >= buflen) { + tor_snprintf(buf, buflen, "<BUG:Truncating domain %lx>", (long)domain); + return buf+strlen(buf); + } + cp += n; + domain &= ~(1<<bit); + + if (domain == 0 || (eos-cp) < 2) + return cp; + + memcpy(cp, ",", 2); /*Nul-terminated ,"*/ + cp++; + } +} + +/** Parse a log severity pattern in *<b>cfg_ptr</b>. Advance cfg_ptr after + * the end of the severityPattern. Set the value of <b>severity_out</b> to + * the parsed pattern. Return 0 on success, -1 on failure. + * + * The syntax for a SeverityPattern is: + * <pre> + * SeverityPattern = *(DomainSeverity SP)* DomainSeverity + * DomainSeverity = (DomainList SP)? SeverityRange + * SeverityRange = MinSeverity ("-" MaxSeverity )? + * DomainList = "[" (SP? DomainSpec SP? ",") SP? DomainSpec "]" + * DomainSpec = "*" | Domain | "~" Domain + * </pre> + * A missing MaxSeverity defaults to ERR. Severities and domains are + * case-insensitive. "~" indicates negation for a domain; negation happens + * last inside a DomainList. Only one SeverityRange without a DomainList is + * allowed per line. + */ +int +parse_log_severity_config(const char **cfg_ptr, + log_severity_list_t *severity_out) +{ + const char *cfg = *cfg_ptr; + int got_anything = 0; + int got_an_unqualified_range = 0; + memset(severity_out, 0, sizeof(*severity_out)); + + cfg = eat_whitespace(cfg); + while (*cfg) { + const char *dash, *space; + char *sev_lo, *sev_hi; + int low, high, i; + log_domain_mask_t domains = ~0u; + + if (*cfg == '[') { + int err = 0; + char *domains_str; + smartlist_t *domains_list; + log_domain_mask_t neg_domains = 0; + const char *closebracket = strchr(cfg, ']'); + if (!closebracket) + return -1; + domains = 0; + domains_str = tor_strndup(cfg+1, closebracket-cfg-1); + domains_list = smartlist_new(); + smartlist_split_string(domains_list, domains_str, ",", SPLIT_SKIP_SPACE, + -1); + tor_free(domains_str); + SMARTLIST_FOREACH_BEGIN(domains_list, const char *, domain) { + if (!strcmp(domain, "*")) { + domains = ~0u; + } else { + int d; + int negate=0; + if (*domain == '~') { + negate = 1; + ++domain; + } + d = parse_log_domain(domain); + if (!d) { + log_warn(LD_CONFIG, "No such logging domain as %s", domain); + err = 1; + } else { + if (negate) + neg_domains |= d; + else + domains |= d; + } + } + } SMARTLIST_FOREACH_END(domain); + SMARTLIST_FOREACH(domains_list, char *, d, tor_free(d)); + smartlist_free(domains_list); + if (err) + return -1; + if (domains == 0 && neg_domains) + domains = ~neg_domains; + else + domains &= ~neg_domains; + cfg = eat_whitespace(closebracket+1); + } else { + ++got_an_unqualified_range; + } + if (!strcasecmpstart(cfg, "file") || + !strcasecmpstart(cfg, "stderr") || + !strcasecmpstart(cfg, "stdout") || + !strcasecmpstart(cfg, "syslog") || + !strcasecmpstart(cfg, "android")) { + goto done; + } + if (got_an_unqualified_range > 1) + return -1; + + space = find_whitespace(cfg); + dash = strchr(cfg, '-'); + if (dash && dash < space) { + sev_lo = tor_strndup(cfg, dash-cfg); + sev_hi = tor_strndup(dash+1, space-(dash+1)); + } else { + sev_lo = tor_strndup(cfg, space-cfg); + sev_hi = tor_strdup("ERR"); + } + low = parse_log_level(sev_lo); + high = parse_log_level(sev_hi); + tor_free(sev_lo); + tor_free(sev_hi); + if (low == -1) + return -1; + if (high == -1) + return -1; + + got_anything = 1; + for (i=low; i >= high; --i) + severity_out->masks[SEVERITY_MASK_IDX(i)] |= domains; + + cfg = eat_whitespace(space); + } + + done: + *cfg_ptr = cfg; + return got_anything ? 0 : -1; +} + +/** Return the least severe log level that any current log is interested in. */ +int +get_min_log_level(void) +{ + logfile_t *lf; + int i; + int min = LOG_ERR; + for (lf = logfiles; lf; lf = lf->next) { + for (i = LOG_DEBUG; i > min; --i) + if (lf->severities->masks[SEVERITY_MASK_IDX(i)]) + min = i; + } + return min; +} + +/** Switch all logs to output at most verbose level. */ +void +switch_logs_debug(void) +{ + logfile_t *lf; + int i; + LOCK_LOGS(); + for (lf = logfiles; lf; lf=lf->next) { + for (i = LOG_DEBUG; i >= LOG_ERR; --i) + lf->severities->masks[SEVERITY_MASK_IDX(i)] = ~0u; + } + log_global_min_severity_ = get_min_log_level(); + UNLOCK_LOGS(); +} + +/** Truncate all the log files. */ +void +truncate_logs(void) +{ + logfile_t *lf; + for (lf = logfiles; lf; lf = lf->next) { + if (lf->fd >= 0) { + tor_ftruncate(lf->fd); + } + } +} diff --git a/src/lib/log/log.h b/src/lib/log/log.h @@ -0,0 +1,276 @@ +/* Copyright (c) 2001, Matej Pfajfar. + * Copyright (c) 2001-2004, Roger Dingledine. + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file log.h + * + * \brief Headers for log.c + **/ + +#ifndef TOR_TORLOG_H + +#include <stdarg.h> +#include "lib/cc/torint.h" +#include "lib/cc/compat_compiler.h" +#include "lib/testsupport/testsupport.h" + +#ifdef HAVE_SYSLOG_H +#include <syslog.h> +#define LOG_WARN LOG_WARNING +#if LOG_DEBUG < LOG_ERR +#error "Your syslog.h thinks high numbers are more important. " \ + "We aren't prepared to deal with that." +#endif +#else /* !(defined(HAVE_SYSLOG_H)) */ +/* Note: Syslog's logging code refers to priorities, with 0 being the most + * important. Thus, all our comparisons needed to be reversed when we added + * syslog support. + * + * The upshot of this is that comments about log levels may be messed up: for + * "maximum severity" read "most severe" and "numerically *lowest* severity". + */ + +/** Debug-level severity: for hyper-verbose messages of no interest to + * anybody but developers. */ +#define LOG_DEBUG 7 +/** Info-level severity: for messages that appear frequently during normal + * operation. */ +#define LOG_INFO 6 +/** Notice-level severity: for messages that appear infrequently + * during normal operation; that the user will probably care about; + * and that are not errors. + */ +#define LOG_NOTICE 5 +/** Warn-level severity: for messages that only appear when something has gone + * wrong. */ +#define LOG_WARN 4 +/** Error-level severity: for messages that only appear when something has gone + * very wrong, and the Tor process can no longer proceed. */ +#define LOG_ERR 3 +#endif /* defined(HAVE_SYSLOG_H) */ + +/* Logging domains */ + +/** Catch-all for miscellaneous events and fatal errors. */ +#define LD_GENERAL (1u<<0) +/** The cryptography subsystem. */ +#define LD_CRYPTO (1u<<1) +/** Networking. */ +#define LD_NET (1u<<2) +/** Parsing and acting on our configuration. */ +#define LD_CONFIG (1u<<3) +/** Reading and writing from the filesystem. */ +#define LD_FS (1u<<4) +/** Other servers' (non)compliance with the Tor protocol. */ +#define LD_PROTOCOL (1u<<5) +/** Memory management. */ +#define LD_MM (1u<<6) +/** HTTP implementation. */ +#define LD_HTTP (1u<<7) +/** Application (socks) requests. */ +#define LD_APP (1u<<8) +/** Communication via the controller protocol. */ +#define LD_CONTROL (1u<<9) +/** Building, using, and managing circuits. */ +#define LD_CIRC (1u<<10) +/** Hidden services. */ +#define LD_REND (1u<<11) +/** Internal errors in this Tor process. */ +#define LD_BUG (1u<<12) +/** Learning and using information about Tor servers. */ +#define LD_DIR (1u<<13) +/** Learning and using information about Tor servers. */ +#define LD_DIRSERV (1u<<14) +/** Onion routing protocol. */ +#define LD_OR (1u<<15) +/** Generic edge-connection functionality. */ +#define LD_EDGE (1u<<16) +#define LD_EXIT LD_EDGE +/** Bandwidth accounting. */ +#define LD_ACCT (1u<<17) +/** Router history */ +#define LD_HIST (1u<<18) +/** OR handshaking */ +#define LD_HANDSHAKE (1u<<19) +/** Heartbeat messages */ +#define LD_HEARTBEAT (1u<<20) +/** Abstract channel_t code */ +#define LD_CHANNEL (1u<<21) +/** Scheduler */ +#define LD_SCHED (1u<<22) +/** Guard nodes */ +#define LD_GUARD (1u<<23) +/** Generation and application of consensus diffs. */ +#define LD_CONSDIFF (1u<<24) +/** Denial of Service mitigation. */ +#define LD_DOS (1u<<25) +/** Number of logging domains in the code. */ +#define N_LOGGING_DOMAINS 26 + +/** This log message is not safe to send to a callback-based logger + * immediately. Used as a flag, not a log domain. */ +#define LD_NOCB (1u<<31) +/** This log message should not include a function name, even if it otherwise + * would. Used as a flag, not a log domain. */ +#define LD_NOFUNCNAME (1u<<30) + +#ifdef TOR_UNIT_TESTS +/** This log message should not be intercepted by mock_saving_logv */ +#define LD_NO_MOCK (1u<<29) +#endif + +/** Mask of zero or more log domains, OR'd together. */ +typedef uint32_t log_domain_mask_t; + +/** Configures which severities are logged for each logging domain for a given + * log target. */ +typedef struct log_severity_list_t { + /** For each log severity, a bitmask of which domains a given logger is + * logging. */ + log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1]; +} log_severity_list_t; + +/** Callback type used for add_callback_log. */ +typedef void (*log_callback)(int severity, uint32_t domain, const char *msg); + +void init_logging(int disable_startup_queue); +int parse_log_level(const char *level); +const char *log_level_to_string(int level); +int parse_log_severity_config(const char **cfg, + log_severity_list_t *severity_out); +void set_log_severity_config(int minSeverity, int maxSeverity, + log_severity_list_t *severity_out); +void add_stream_log(const log_severity_list_t *severity, const char *name, + int fd); +int add_file_log(const log_severity_list_t *severity, + const char *filename, + int fd); + +#ifdef HAVE_SYSLOG_H +int add_syslog_log(const log_severity_list_t *severity, + const char* syslog_identity_tag); +#endif // HAVE_SYSLOG_H. +#ifdef HAVE_ANDROID_LOG_H +int add_android_log(const log_severity_list_t *severity, + const char *android_identity_tag); +#endif // HAVE_ANDROID_LOG_H. +int add_callback_log(const log_severity_list_t *severity, log_callback cb); +typedef void (*pending_callback_callback)(void); +void logs_set_pending_callback_callback(pending_callback_callback cb); +void logs_set_domain_logging(int enabled); +int get_min_log_level(void); +void switch_logs_debug(void); +void logs_free_all(void); +void add_temp_log(int min_severity); +void close_temp_logs(void); +void rollback_log_changes(void); +void mark_logs_temp(void); +void change_callback_log_severity(int loglevelMin, int loglevelMax, + log_callback cb); +void flush_pending_log_callbacks(void); +void flush_log_messages_from_startup(void); +void log_set_application_name(const char *name); +void set_log_time_granularity(int granularity_msec); +void truncate_logs(void); + +void tor_log(int severity, log_domain_mask_t domain, const char *format, ...) + CHECK_PRINTF(3,4); + +void tor_log_update_sigsafe_err_fds(void); + +struct smartlist_t; +void tor_log_get_logfile_names(struct smartlist_t *out); + +extern int log_global_min_severity_; + +void log_fn_(int severity, log_domain_mask_t domain, + const char *funcname, const char *format, ...) + CHECK_PRINTF(4,5); +struct ratelim_t; +void log_fn_ratelim_(struct ratelim_t *ratelim, int severity, + log_domain_mask_t domain, const char *funcname, + const char *format, ...) + CHECK_PRINTF(5,6); + +int log_message_is_interesting(int severity, log_domain_mask_t domain); +void tor_log_string(int severity, log_domain_mask_t domain, + const char *function, const char *string); + +#if defined(__GNUC__) && __GNUC__ <= 3 + +/* These are the GCC varidaic macros, so that older versions of GCC don't + * break. */ + +/** Log a message at level <b>severity</b>, using a pretty-printed version + * of the current function name. */ +#define log_fn(severity, domain, args...) \ + log_fn_(severity, domain, __FUNCTION__, args) +/** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control + * the frequency at which messages can appear. + */ +#define log_fn_ratelim(ratelim, severity, domain, args...) \ + log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, args) +#define log_debug(domain, args...) \ + STMT_BEGIN \ + if (PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG)) \ + log_fn_(LOG_DEBUG, domain, __FUNCTION__, args); \ + STMT_END +#define log_info(domain, args...) \ + log_fn_(LOG_INFO, domain, __FUNCTION__, args) +#define log_notice(domain, args...) \ + log_fn_(LOG_NOTICE, domain, __FUNCTION__, args) +#define log_warn(domain, args...) \ + log_fn_(LOG_WARN, domain, __FUNCTION__, args) +#define log_err(domain, args...) \ + log_fn_(LOG_ERR, domain, __FUNCTION__, args) + +#else /* !(defined(__GNUC__) && __GNUC__ <= 3) */ + +/* Here are the c99 variadic macros, to work with non-GCC compilers */ + +#define log_debug(domain, args, ...) \ + STMT_BEGIN \ + if (PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG)) \ + log_fn_(LOG_DEBUG, domain, __FUNCTION__, args, ##__VA_ARGS__); \ + STMT_END +#define log_info(domain, args,...) \ + log_fn_(LOG_INFO, domain, __FUNCTION__, args, ##__VA_ARGS__) +#define log_notice(domain, args,...) \ + log_fn_(LOG_NOTICE, domain, __FUNCTION__, args, ##__VA_ARGS__) +#define log_warn(domain, args,...) \ + log_fn_(LOG_WARN, domain, __FUNCTION__, args, ##__VA_ARGS__) +#define log_err(domain, args,...) \ + log_fn_(LOG_ERR, domain, __FUNCTION__, args, ##__VA_ARGS__) +/** Log a message at level <b>severity</b>, using a pretty-printed version + * of the current function name. */ +#define log_fn(severity, domain, args,...) \ + log_fn_(severity, domain, __FUNCTION__, args, ##__VA_ARGS__) +/** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control + * the frequency at which messages can appear. + */ +#define log_fn_ratelim(ratelim, severity, domain, args,...) \ + log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, \ + args, ##__VA_ARGS__) +#endif /* defined(__GNUC__) && __GNUC__ <= 3 */ + +/** This defines log levels that are linked in the Rust log module, rather + * than re-defining these in both Rust and C. + * + * C_RUST_COUPLED src/rust/tor_log LogSeverity, LogDomain + */ +extern const int LOG_WARN_; +extern const int LOG_NOTICE_; +extern const log_domain_mask_t LD_NET_; +extern const log_domain_mask_t LD_GENERAL_; + +#ifdef LOG_PRIVATE +MOCK_DECL(STATIC void, logv, (int severity, log_domain_mask_t domain, + const char *funcname, const char *suffix, const char *format, + va_list ap) CHECK_PRINTF(5,0)); +#endif + +# define TOR_TORLOG_H +#endif /* !defined(TOR_TORLOG_H) */ diff --git a/src/lib/log/ratelim.c b/src/lib/log/ratelim.c @@ -9,7 +9,7 @@ **/ #include "lib/log/ratelim.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/string/printf.h" /** If the rate-limiter <b>lim</b> is ready at <b>now</b>, return the number diff --git a/src/lib/log/torlog.c b/src/lib/log/torlog.c @@ -1,1488 +0,0 @@ -/* Copyright (c) 2001, Matej Pfajfar. - * Copyright (c) 2001-2004, Roger Dingledine. - * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2018, The Tor Project, Inc. */ -/* See LICENSE for licensing information */ - -/** - * \file torlog.c - * \brief Functions to send messages to log files or the console. - **/ - -#include "orconfig.h" -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#ifdef HAVE_SYS_TIME_H -#include <sys/time.h> -#endif -#ifdef HAVE_TIME_H -#include <time.h> -#endif -#ifdef HAVE_UNISTD_H -#include <unistd.h> -#endif -#ifdef HAVE_SYS_TYPES_H -#include <sys/types.h> -#endif -#ifdef HAVE_FCNTL_H -#include <fcntl.h> -#endif - -#define LOG_PRIVATE -#include "lib/log/torlog.h" -#include "lib/log/ratelim.h" -#include "lib/lock/compat_mutex.h" -#include "lib/smartlist_core/smartlist_core.h" -#include "lib/smartlist_core/smartlist_foreach.h" -#include "lib/smartlist_core/smartlist_split.h" -#include "lib/err/torerr.h" -#include "lib/intmath/bits.h" -#include "lib/string/compat_string.h" -#include "lib/string/printf.h" -#include "lib/malloc/util_malloc.h" -#include "lib/string/util_string.h" -#include "lib/wallclock/tor_gettimeofday.h" -#include "lib/wallclock/approx_time.h" -#include "lib/wallclock/tm_cvt.h" -#include "lib/fdio/fdio.h" - -#ifdef HAVE_ANDROID_LOG_H -#include <android/log.h> -#endif // HAVE_ANDROID_LOG_H. - -/** Given a severity, yields an index into log_severity_list_t.masks to use - * for that severity. */ -#define SEVERITY_MASK_IDX(sev) ((sev) - LOG_ERR) - -/** @{ */ -/** The string we stick at the end of a log message when it is too long, - * and its length. */ -#define TRUNCATED_STR "[...truncated]" -#define TRUNCATED_STR_LEN 14 -/** @} */ - -/** Defining compile-time constants for Tor log levels (used by the Rust - * log wrapper at src/rust/tor_log) */ -const int LOG_WARN_ = LOG_WARN; -const int LOG_NOTICE_ = LOG_NOTICE; -const log_domain_mask_t LD_GENERAL_ = LD_GENERAL; -const log_domain_mask_t LD_NET_ = LD_NET; - -/** Information for a single logfile; only used in log.c */ -typedef struct logfile_t { - struct logfile_t *next; /**< Next logfile_t in the linked list. */ - char *filename; /**< Filename to open. */ - int fd; /**< fd to receive log messages, or -1 for none. */ - int seems_dead; /**< Boolean: true if the stream seems to be kaput. */ - int needs_close; /**< Boolean: true if the stream gets closed on shutdown. */ - int is_temporary; /**< Boolean: close after initializing logging subsystem.*/ - int is_syslog; /**< Boolean: send messages to syslog. */ - int is_android; /**< Boolean: send messages to Android's log subsystem. */ - char *android_tag; /**< Identity Tag used in Android's log subsystem. */ - log_callback callback; /**< If not NULL, send messages to this function. */ - log_severity_list_t *severities; /**< Which severity of messages should we - * log for each log domain? */ -} logfile_t; - -static void log_free_(logfile_t *victim); -#define log_free(lg) \ - FREE_AND_NULL(logfile_t, log_free_, (lg)) - -/** Helper: map a log severity to descriptive string. */ -static inline const char * -sev_to_string(int severity) -{ - switch (severity) { - case LOG_DEBUG: return "debug"; - case LOG_INFO: return "info"; - case LOG_NOTICE: return "notice"; - case LOG_WARN: return "warn"; - case LOG_ERR: return "err"; - default: /* Call assert, not tor_assert, since tor_assert - * calls log on failure. */ - raw_assert_unreached(); return "UNKNOWN"; // LCOV_EXCL_LINE - } -} - -/** Helper: decide whether to include the function name in the log message. */ -static inline int -should_log_function_name(log_domain_mask_t domain, int severity) -{ - switch (severity) { - case LOG_DEBUG: - case LOG_INFO: - /* All debugging messages occur in interesting places. */ - return (domain & LD_NOFUNCNAME) == 0; - case LOG_NOTICE: - case LOG_WARN: - case LOG_ERR: - /* We care about places where bugs occur. */ - return (domain & (LD_BUG|LD_NOFUNCNAME)) == LD_BUG; - default: - /* Call assert, not tor_assert, since tor_assert calls log on failure. */ - raw_assert(0); return 0; // LCOV_EXCL_LINE - } -} - -#ifdef HAVE_ANDROID_LOG_H -/** Helper function to convert Tor's log severity into the matching - * Android log priority. - */ -static int -severity_to_android_log_priority(int severity) -{ - switch (severity) { - case LOG_DEBUG: - return ANDROID_LOG_VERBOSE; - case LOG_INFO: - return ANDROID_LOG_DEBUG; - case LOG_NOTICE: - return ANDROID_LOG_INFO; - case LOG_WARN: - return ANDROID_LOG_WARN; - case LOG_ERR: - return ANDROID_LOG_ERROR; - default: - // LCOV_EXCL_START - raw_assert(0); - return 0; - // LCOV_EXCL_STOP - } -} -#endif // HAVE_ANDROID_LOG_H. - -/** A mutex to guard changes to logfiles and logging. */ -static tor_mutex_t log_mutex; -/** True iff we have initialized log_mutex */ -static int log_mutex_initialized = 0; - -/** Linked list of logfile_t. */ -static logfile_t *logfiles = NULL; -/** Boolean: do we report logging domains? */ -static int log_domains_are_logged = 0; - -#ifdef HAVE_SYSLOG_H -/** The number of open syslog log handlers that we have. When this reaches 0, - * we can close our connection to the syslog facility. */ -static int syslog_count = 0; -#endif - -/** Represents a log message that we are going to send to callback-driven - * loggers once we can do so in a non-reentrant way. */ -typedef struct pending_log_message_t { - int severity; /**< The severity of the message */ - log_domain_mask_t domain; /**< The domain of the message */ - char *fullmsg; /**< The message, with all decorations */ - char *msg; /**< The content of the message */ -} pending_log_message_t; - -/** Log messages waiting to be replayed onto callback-based logs */ -static smartlist_t *pending_cb_messages = NULL; - -/** Callback to invoke when pending_cb_messages becomes nonempty. */ -static pending_callback_callback pending_cb_cb = NULL; - -/** Log messages waiting to be replayed once the logging system is initialized. - */ -static smartlist_t *pending_startup_messages = NULL; - -/** Number of bytes of messages queued in pending_startup_messages. (This is - * the length of the messages, not the number of bytes used to store - * them.) */ -static size_t pending_startup_messages_len; - -/** True iff we should store messages while waiting for the logs to get - * configured. */ -static int queue_startup_messages = 1; - -/** True iff __PRETTY_FUNCTION__ includes parenthesized arguments. */ -static int pretty_fn_has_parens = 0; - -/** Don't store more than this many bytes of messages while waiting for the - * logs to get configured. */ -#define MAX_STARTUP_MSG_LEN (1<<16) - -/** Lock the log_mutex to prevent others from changing the logfile_t list */ -#define LOCK_LOGS() STMT_BEGIN \ - raw_assert(log_mutex_initialized); \ - tor_mutex_acquire(&log_mutex); \ - STMT_END -/** Unlock the log_mutex */ -#define UNLOCK_LOGS() STMT_BEGIN \ - raw_assert(log_mutex_initialized); \ - tor_mutex_release(&log_mutex); \ - STMT_END - -/** What's the lowest log level anybody cares about? Checking this lets us - * bail out early from log_debug if we aren't debugging. */ -int log_global_min_severity_ = LOG_NOTICE; - -static void delete_log(logfile_t *victim); -static void close_log(logfile_t *victim); - -static char *domain_to_string(log_domain_mask_t domain, - char *buf, size_t buflen); -static inline char *format_msg(char *buf, size_t buf_len, - log_domain_mask_t domain, int severity, const char *funcname, - const char *suffix, - const char *format, va_list ap, size_t *msg_len_out) - CHECK_PRINTF(7,0); - -/** Name of the application: used to generate the message we write at the - * start of each new log. */ -static char *appname = NULL; - -/** Set the "application name" for the logs to <b>name</b>: we'll use this - * name in the message we write when starting up, and at the start of each new - * log. - * - * Tor uses this string to write the version number to the log file. */ -void -log_set_application_name(const char *name) -{ - tor_free(appname); - appname = name ? tor_strdup(name) : NULL; -} - -/** Return true if some of the running logs might be interested in a log - * message of the given severity in the given domains. If this function - * returns true, the log message might be ignored anyway, but if it returns - * false, it is definitely_ safe not to log the message. */ -int -log_message_is_interesting(int severity, log_domain_mask_t domain) -{ - (void) domain; - return (severity <= log_global_min_severity_); -} - -/** - * As tor_log, but takes an optional function name, and does not treat its - * <b>string</b> as a printf format. - * - * For use by Rust integration. - */ -void -tor_log_string(int severity, log_domain_mask_t domain, - const char *function, const char *string) -{ - log_fn_(severity, domain, function, "%s", string); -} - -/** Log time granularity in milliseconds. */ -static int log_time_granularity = 1; - -/** Define log time granularity for all logs to be <b>granularity_msec</b> - * milliseconds. */ -void -set_log_time_granularity(int granularity_msec) -{ - log_time_granularity = granularity_msec; - tor_log_sigsafe_err_set_granularity(granularity_msec); -} - -/** Helper: Write the standard prefix for log lines to a - * <b>buf_len</b> character buffer in <b>buf</b>. - */ -static inline size_t -log_prefix_(char *buf, size_t buf_len, int severity) -{ - time_t t; - struct timeval now; - struct tm tm; - size_t n; - int r, ms; - - tor_gettimeofday(&now); - t = (time_t)now.tv_sec; - ms = (int)now.tv_usec / 1000; - if (log_time_granularity >= 1000) { - t -= t % (log_time_granularity / 1000); - ms = 0; - } else { - ms -= ((int)now.tv_usec / 1000) % log_time_granularity; - } - - n = strftime(buf, buf_len, "%b %d %H:%M:%S", - tor_localtime_r_msg(&t, &tm, NULL)); - r = tor_snprintf(buf+n, buf_len-n, ".%.3i [%s] ", ms, - sev_to_string(severity)); - - if (r<0) - return buf_len-1; - else - return n+r; -} - -/** If lf refers to an actual file that we have just opened, and the file - * contains no data, log an "opening new logfile" message at the top. - * - * Return -1 if the log is broken and needs to be deleted, else return 0. - */ -static int -log_tor_version(logfile_t *lf, int reset) -{ - char buf[256]; - size_t n; - int is_new; - - if (!lf->needs_close) - /* If it doesn't get closed, it isn't really a file. */ - return 0; - if (lf->is_temporary) - /* If it's temporary, it isn't really a file. */ - return 0; - - is_new = lf->fd >= 0 && tor_fd_getpos(lf->fd) == 0; - - if (reset && !is_new) - /* We are resetting, but we aren't at the start of the file; no - * need to log again. */ - return 0; - n = log_prefix_(buf, sizeof(buf), LOG_NOTICE); - if (appname) { - tor_snprintf(buf+n, sizeof(buf)-n, - "%s opening %slog file.\n", appname, is_new?"new ":""); - } else { - tor_snprintf(buf+n, sizeof(buf)-n, - "Tor %s opening %slog file.\n", VERSION, is_new?"new ":""); - } - if (write_all_to_fd_minimal(lf->fd, buf, strlen(buf)) < 0) /* error */ - return -1; /* failed */ - return 0; -} - -static const char bug_suffix[] = " (on Tor " VERSION -#ifndef _MSC_VER - " " -#include "micro-revision.i" -#endif - ")"; - -/** Helper: Format a log message into a fixed-sized buffer. (This is - * factored out of <b>logv</b> so that we never format a message more - * than once.) Return a pointer to the first character of the message - * portion of the formatted string. - */ -static inline char * -format_msg(char *buf, size_t buf_len, - log_domain_mask_t domain, int severity, const char *funcname, - const char *suffix, - const char *format, va_list ap, size_t *msg_len_out) -{ - size_t n; - int r; - char *end_of_prefix; - char *buf_end; - - raw_assert(buf_len >= 16); /* prevent integer underflow and stupidity */ - buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */ - buf_end = buf+buf_len; /* point *after* the last char we can write to */ - - n = log_prefix_(buf, buf_len, severity); - end_of_prefix = buf+n; - - if (log_domains_are_logged) { - char *cp = buf+n; - if (cp == buf_end) goto format_msg_no_room_for_domains; - *cp++ = '{'; - if (cp == buf_end) goto format_msg_no_room_for_domains; - cp = domain_to_string(domain, cp, (buf+buf_len-cp)); - if (cp == buf_end) goto format_msg_no_room_for_domains; - *cp++ = '}'; - if (cp == buf_end) goto format_msg_no_room_for_domains; - *cp++ = ' '; - if (cp == buf_end) goto format_msg_no_room_for_domains; - end_of_prefix = cp; - n = cp-buf; - format_msg_no_room_for_domains: - /* This will leave end_of_prefix and n unchanged, and thus cause - * whatever log domain string we had written to be clobbered. */ - ; - } - - if (funcname && should_log_function_name(domain, severity)) { - r = tor_snprintf(buf+n, buf_len-n, - pretty_fn_has_parens ? "%s: " : "%s(): ", - funcname); - if (r<0) - n = strlen(buf); - else - n += r; - } - - if (domain == LD_BUG && buf_len-n > 6) { - memcpy(buf+n, "Bug: ", 6); - n += 5; - } - - r = tor_vsnprintf(buf+n,buf_len-n,format,ap); - if (r < 0) { - /* The message was too long; overwrite the end of the buffer with - * "[...truncated]" */ - if (buf_len >= TRUNCATED_STR_LEN) { - size_t offset = buf_len-TRUNCATED_STR_LEN; - /* We have an extra 2 characters after buf_len to hold the \n\0, - * so it's safe to add 1 to the size here. */ - strlcpy(buf+offset, TRUNCATED_STR, buf_len-offset+1); - } - /* Set 'n' to the end of the buffer, where we'll be writing \n\0. - * Since we already subtracted 2 from buf_len, this is safe.*/ - n = buf_len; - } else { - n += r; - if (suffix) { - size_t suffix_len = strlen(suffix); - if (buf_len-n >= suffix_len) { - memcpy(buf+n, suffix, suffix_len); - n += suffix_len; - } - } - } - - if (domain == LD_BUG && - buf_len - n > strlen(bug_suffix)+1) { - memcpy(buf+n, bug_suffix, strlen(bug_suffix)); - n += strlen(bug_suffix); - } - - buf[n]='\n'; - buf[n+1]='\0'; - *msg_len_out = n+1; - return end_of_prefix; -} - -/* Create a new pending_log_message_t with appropriate values */ -static pending_log_message_t * -pending_log_message_new(int severity, log_domain_mask_t domain, - const char *fullmsg, const char *shortmsg) -{ - pending_log_message_t *m = tor_malloc(sizeof(pending_log_message_t)); - m->severity = severity; - m->domain = domain; - m->fullmsg = fullmsg ? tor_strdup(fullmsg) : NULL; - m->msg = tor_strdup(shortmsg); - return m; -} - -#define pending_log_message_free(msg) \ - FREE_AND_NULL(pending_log_message_t, pending_log_message_free_, (msg)) - -/** Release all storage held by <b>msg</b>. */ -static void -pending_log_message_free_(pending_log_message_t *msg) -{ - if (!msg) - return; - tor_free(msg->msg); - tor_free(msg->fullmsg); - tor_free(msg); -} - -/** Helper function: returns true iff the log file, given in <b>lf</b>, is - * handled externally via the system log API, the Android logging API, or is an - * external callback function. */ -static inline int -logfile_is_external(const logfile_t *lf) -{ - raw_assert(lf); - return lf->is_syslog || lf->is_android || lf->callback; -} - -/** Return true iff <b>lf</b> would like to receive a message with the - * specified <b>severity</b> in the specified <b>domain</b>. - */ -static inline int -logfile_wants_message(const logfile_t *lf, int severity, - log_domain_mask_t domain) -{ - if (! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) { - return 0; - } - if (! (lf->fd >= 0 || logfile_is_external(lf))) { - return 0; - } - if (lf->seems_dead) { - return 0; - } - - return 1; -} - -/** Send a message to <b>lf</b>. The full message, with time prefix and - * severity, is in <b>buf</b>. The message itself is in - * <b>msg_after_prefix</b>. If <b>callbacks_deferred</b> points to true, then - * we already deferred this message for pending callbacks and don't need to do - * it again. Otherwise, if we need to do it, do it, and set - * <b>callbacks_deferred</b> to 1. */ -static inline void -logfile_deliver(logfile_t *lf, const char *buf, size_t msg_len, - const char *msg_after_prefix, log_domain_mask_t domain, - int severity, int *callbacks_deferred) -{ - - if (lf->is_syslog) { -#ifdef HAVE_SYSLOG_H -#ifdef MAXLINE - /* Some syslog implementations have limits on the length of what you can - * pass them, and some very old ones do not detect overflow so well. - * Regrettably, they call their maximum line length MAXLINE. */ -#if MAXLINE < 64 -#warn "MAXLINE is a very low number; it might not be from syslog.h after all" -#endif - char *m = msg_after_prefix; - if (msg_len >= MAXLINE) - m = tor_strndup(msg_after_prefix, MAXLINE-1); - syslog(severity, "%s", m); - if (m != msg_after_prefix) { - tor_free(m); - } -#else /* !(defined(MAXLINE)) */ - /* We have syslog but not MAXLINE. That's promising! */ - syslog(severity, "%s", msg_after_prefix); -#endif /* defined(MAXLINE) */ -#endif /* defined(HAVE_SYSLOG_H) */ - } else if (lf->is_android) { -#ifdef HAVE_ANDROID_LOG_H - int priority = severity_to_android_log_priority(severity); - __android_log_write(priority, lf->android_tag, msg_after_prefix); -#endif // HAVE_ANDROID_LOG_H. - } else if (lf->callback) { - if (domain & LD_NOCB) { - if (!*callbacks_deferred && pending_cb_messages) { - smartlist_add(pending_cb_messages, - pending_log_message_new(severity,domain,NULL,msg_after_prefix)); - *callbacks_deferred = 1; - if (smartlist_len(pending_cb_messages) == 1 && pending_cb_cb) { - pending_cb_cb(); - } - } - } else { - lf->callback(severity, domain, msg_after_prefix); - } - } else { - if (write_all_to_fd_minimal(lf->fd, buf, msg_len) < 0) { /* error */ - /* don't log the error! mark this log entry to be blown away, and - * continue. */ - lf->seems_dead = 1; - } - } -} - -/** Helper: sends a message to the appropriate logfiles, at loglevel - * <b>severity</b>. If provided, <b>funcname</b> is prepended to the - * message. The actual message is derived as from tor_snprintf(format,ap). - */ -MOCK_IMPL(STATIC void, -logv,(int severity, log_domain_mask_t domain, const char *funcname, - const char *suffix, const char *format, va_list ap)) -{ - char buf[10240]; - size_t msg_len = 0; - int formatted = 0; - logfile_t *lf; - char *end_of_prefix=NULL; - int callbacks_deferred = 0; - - /* Call assert, not raw_assert, since raw_assert calls log on failure. */ - raw_assert(format); - /* check that severity is sane. Overrunning the masks array leads to - * interesting and hard to diagnose effects */ - raw_assert(severity >= LOG_ERR && severity <= LOG_DEBUG); - /* check that we've initialised the log mutex before we try to lock it */ - raw_assert(log_mutex_initialized); - LOCK_LOGS(); - - if ((! (domain & LD_NOCB)) && pending_cb_messages - && smartlist_len(pending_cb_messages)) - flush_pending_log_callbacks(); - - if (queue_startup_messages && - pending_startup_messages_len < MAX_STARTUP_MSG_LEN) { - end_of_prefix = - format_msg(buf, sizeof(buf), domain, severity, funcname, suffix, - format, ap, &msg_len); - formatted = 1; - - smartlist_add(pending_startup_messages, - pending_log_message_new(severity,domain,buf,end_of_prefix)); - pending_startup_messages_len += msg_len; - } - - for (lf = logfiles; lf; lf = lf->next) { - if (! logfile_wants_message(lf, severity, domain)) - continue; - - if (!formatted) { - end_of_prefix = - format_msg(buf, sizeof(buf), domain, severity, funcname, suffix, - format, ap, &msg_len); - formatted = 1; - } - - logfile_deliver(lf, buf, msg_len, end_of_prefix, domain, severity, - &callbacks_deferred); - } - UNLOCK_LOGS(); -} - -/** Output a message to the log. It gets logged to all logfiles that - * care about messages with <b>severity</b> in <b>domain</b>. The content - * is formatted printf-style based on <b>format</b> and extra arguments. - * */ -void -tor_log(int severity, log_domain_mask_t domain, const char *format, ...) -{ - va_list ap; - if (severity > log_global_min_severity_) - return; - va_start(ap,format); -#ifdef TOR_UNIT_TESTS - if (domain & LD_NO_MOCK) - logv__real(severity, domain, NULL, NULL, format, ap); - else -#endif - logv(severity, domain, NULL, NULL, format, ap); - va_end(ap); -} - -/** Helper function; return true iff the <b>n</b>-element array <b>array</b> - * contains <b>item</b>. */ -static int -int_array_contains(const int *array, int n, int item) -{ - int j; - for (j = 0; j < n; ++j) { - if (array[j] == item) - return 1; - } - return 0; -} - -/** Function to call whenever the list of logs changes to get ready to log - * from signal handlers. */ -void -tor_log_update_sigsafe_err_fds(void) -{ - const logfile_t *lf; - int found_real_stderr = 0; - - int fds[TOR_SIGSAFE_LOG_MAX_FDS]; - int n_fds; - - LOCK_LOGS(); - /* Reserve the first one for stderr. This is safe because when we daemonize, - * we dup2 /dev/null to stderr, */ - fds[0] = STDERR_FILENO; - n_fds = 1; - - for (lf = logfiles; lf; lf = lf->next) { - /* Don't try callback to the control port, or syslogs: We can't - * do them from a signal handler. Don't try stdout: we always do stderr. - */ - if (lf->is_temporary || logfile_is_external(lf) - || lf->seems_dead || lf->fd < 0) - continue; - if (lf->severities->masks[SEVERITY_MASK_IDX(LOG_ERR)] & - (LD_BUG|LD_GENERAL)) { - if (lf->fd == STDERR_FILENO) - found_real_stderr = 1; - /* Avoid duplicates */ - if (int_array_contains(fds, n_fds, lf->fd)) - continue; - fds[n_fds++] = lf->fd; - if (n_fds == TOR_SIGSAFE_LOG_MAX_FDS) - break; - } - } - - if (!found_real_stderr && - int_array_contains(fds, n_fds, STDOUT_FILENO)) { - /* Don't use a virtual stderr when we're also logging to stdout. */ - raw_assert(n_fds >= 2); /* Don't raw_assert inside log fns */ - fds[0] = fds[--n_fds]; - } - - UNLOCK_LOGS(); - - tor_log_set_sigsafe_err_fds(fds, n_fds); -} - -/** Add to <b>out</b> a copy of every currently configured log file name. Used - * to enable access to these filenames with the sandbox code. */ -void -tor_log_get_logfile_names(smartlist_t *out) -{ - logfile_t *lf; - raw_assert(out); - - LOCK_LOGS(); - - for (lf = logfiles; lf; lf = lf->next) { - if (lf->is_temporary || logfile_is_external(lf)) - continue; - if (lf->filename == NULL) - continue; - smartlist_add_strdup(out, lf->filename); - } - - UNLOCK_LOGS(); -} - -/** Implementation of the log_fn backend, used when we have - * variadic macros. All arguments are as for log_fn, except for - * <b>fn</b>, which is the name of the calling functions. */ -void -log_fn_(int severity, log_domain_mask_t domain, const char *fn, - const char *format, ...) -{ - va_list ap; - if (severity > log_global_min_severity_) - return; - va_start(ap,format); - logv(severity, domain, fn, NULL, format, ap); - va_end(ap); -} -void -log_fn_ratelim_(ratelim_t *ratelim, int severity, log_domain_mask_t domain, - const char *fn, const char *format, ...) -{ - va_list ap; - char *m; - if (severity > log_global_min_severity_) - return; - m = rate_limit_log(ratelim, approx_time()); - if (m == NULL) - return; - va_start(ap, format); - logv(severity, domain, fn, m, format, ap); - va_end(ap); - tor_free(m); -} - -/** Free all storage held by <b>victim</b>. */ -static void -log_free_(logfile_t *victim) -{ - if (!victim) - return; - tor_free(victim->severities); - tor_free(victim->filename); - tor_free(victim->android_tag); - tor_free(victim); -} - -/** Close all open log files, and free other static memory. */ -void -logs_free_all(void) -{ - logfile_t *victim, *next; - smartlist_t *messages, *messages2; - LOCK_LOGS(); - next = logfiles; - logfiles = NULL; - messages = pending_cb_messages; - pending_cb_messages = NULL; - pending_cb_cb = NULL; - messages2 = pending_startup_messages; - pending_startup_messages = NULL; - UNLOCK_LOGS(); - while (next) { - victim = next; - next = next->next; - close_log(victim); - log_free(victim); - } - tor_free(appname); - - SMARTLIST_FOREACH(messages, pending_log_message_t *, msg, { - pending_log_message_free(msg); - }); - smartlist_free(messages); - - if (messages2) { - SMARTLIST_FOREACH(messages2, pending_log_message_t *, msg, { - pending_log_message_free(msg); - }); - smartlist_free(messages2); - } - - /* We _could_ destroy the log mutex here, but that would screw up any logs - * that happened between here and the end of execution. */ -} - -/** Remove and free the log entry <b>victim</b> from the linked-list - * logfiles (it is probably present, but it might not be due to thread - * racing issues). After this function is called, the caller shouldn't - * refer to <b>victim</b> anymore. - * - * Long-term, we need to do something about races in the log subsystem - * in general. See bug 222 for more details. - */ -static void -delete_log(logfile_t *victim) -{ - logfile_t *tmpl; - if (victim == logfiles) - logfiles = victim->next; - else { - for (tmpl = logfiles; tmpl && tmpl->next != victim; tmpl=tmpl->next) ; -// raw_assert(tmpl); -// raw_assert(tmpl->next == victim); - if (!tmpl) - return; - tmpl->next = victim->next; - } - log_free(victim); -} - -/** Helper: release system resources (but not memory) held by a single - * logfile_t. */ -static void -close_log(logfile_t *victim) -{ - if (victim->needs_close && victim->fd >= 0) { - close(victim->fd); - victim->fd = -1; - } else if (victim->is_syslog) { -#ifdef HAVE_SYSLOG_H - if (--syslog_count == 0) { - /* There are no other syslogs; close the logging facility. */ - closelog(); - } -#endif /* defined(HAVE_SYSLOG_H) */ - } -} - -/** Adjust a log severity configuration in <b>severity_out</b> to contain - * every domain between <b>loglevelMin</b> and <b>loglevelMax</b>, inclusive. - */ -void -set_log_severity_config(int loglevelMin, int loglevelMax, - log_severity_list_t *severity_out) -{ - int i; - raw_assert(loglevelMin >= loglevelMax); - raw_assert(loglevelMin >= LOG_ERR && loglevelMin <= LOG_DEBUG); - raw_assert(loglevelMax >= LOG_ERR && loglevelMax <= LOG_DEBUG); - memset(severity_out, 0, sizeof(log_severity_list_t)); - for (i = loglevelMin; i >= loglevelMax; --i) { - severity_out->masks[SEVERITY_MASK_IDX(i)] = ~0u; - } -} - -/** Add a log handler named <b>name</b> to send all messages in <b>severity</b> - * to <b>fd</b>. Copies <b>severity</b>. Helper: does no locking. */ -static void -add_stream_log_impl(const log_severity_list_t *severity, - const char *name, int fd) -{ - logfile_t *lf; - lf = tor_malloc_zero(sizeof(logfile_t)); - lf->fd = fd; - lf->filename = tor_strdup(name); - lf->severities = tor_memdup(severity, sizeof(log_severity_list_t)); - lf->next = logfiles; - - logfiles = lf; - log_global_min_severity_ = get_min_log_level(); -} - -/** Add a log handler named <b>name</b> to send all messages in <b>severity</b> - * to <b>fd</b>. Steals a reference to <b>severity</b>; the caller must - * not use it after calling this function. */ -void -add_stream_log(const log_severity_list_t *severity, const char *name, int fd) -{ - LOCK_LOGS(); - add_stream_log_impl(severity, name, fd); - UNLOCK_LOGS(); -} - -/** Initialize the global logging facility */ -void -init_logging(int disable_startup_queue) -{ - if (!log_mutex_initialized) { - tor_mutex_init(&log_mutex); - log_mutex_initialized = 1; - } -#ifdef __GNUC__ - if (strchr(__PRETTY_FUNCTION__, '(')) { - pretty_fn_has_parens = 1; - } -#endif - if (pending_cb_messages == NULL) - pending_cb_messages = smartlist_new(); - if (disable_startup_queue) - queue_startup_messages = 0; - if (pending_startup_messages == NULL && queue_startup_messages) { - pending_startup_messages = smartlist_new(); - } -} - -/** Set whether we report logging domains as a part of our log messages. - */ -void -logs_set_domain_logging(int enabled) -{ - LOCK_LOGS(); - log_domains_are_logged = enabled; - UNLOCK_LOGS(); -} - -/** Add a log handler to receive messages during startup (before the real - * logs are initialized). - */ -void -add_temp_log(int min_severity) -{ - log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t)); - set_log_severity_config(min_severity, LOG_ERR, s); - LOCK_LOGS(); - add_stream_log_impl(s, "<temp>", fileno(stdout)); - tor_free(s); - logfiles->is_temporary = 1; - UNLOCK_LOGS(); -} - -/** - * Register "cb" as the callback to call when there are new pending log - * callbacks to be flushed with flush_pending_log_callbacks(). - * - * Note that this callback, if present, can be invoked from any thread. - * - * This callback must not log. - * - * It is intentional that this function contains the name "callback" twice: it - * sets a "callback" to be called on the condition that there is a "pending - * callback". - **/ -void -logs_set_pending_callback_callback(pending_callback_callback cb) -{ - pending_cb_cb = cb; -} - -/** - * Add a log handler to send messages in <b>severity</b> - * to the function <b>cb</b>. - */ -int -add_callback_log(const log_severity_list_t *severity, log_callback cb) -{ - logfile_t *lf; - lf = tor_malloc_zero(sizeof(logfile_t)); - lf->fd = -1; - lf->severities = tor_memdup(severity, sizeof(log_severity_list_t)); - lf->filename = tor_strdup("<callback>"); - lf->callback = cb; - lf->next = logfiles; - - LOCK_LOGS(); - logfiles = lf; - log_global_min_severity_ = get_min_log_level(); - UNLOCK_LOGS(); - return 0; -} - -/** Adjust the configured severity of any logs whose callback function is - * <b>cb</b>. */ -void -change_callback_log_severity(int loglevelMin, int loglevelMax, - log_callback cb) -{ - logfile_t *lf; - log_severity_list_t severities; - set_log_severity_config(loglevelMin, loglevelMax, &severities); - LOCK_LOGS(); - for (lf = logfiles; lf; lf = lf->next) { - if (lf->callback == cb) { - memcpy(lf->severities, &severities, sizeof(severities)); - } - } - log_global_min_severity_ = get_min_log_level(); - UNLOCK_LOGS(); -} - -/** If there are any log messages that were generated with LD_NOCB waiting to - * be sent to callback-based loggers, send them now. */ -void -flush_pending_log_callbacks(void) -{ - logfile_t *lf; - smartlist_t *messages, *messages_tmp; - - LOCK_LOGS(); - if (!pending_cb_messages || 0 == smartlist_len(pending_cb_messages)) { - UNLOCK_LOGS(); - return; - } - - messages = pending_cb_messages; - pending_cb_messages = smartlist_new(); - do { - SMARTLIST_FOREACH_BEGIN(messages, pending_log_message_t *, msg) { - const int severity = msg->severity; - const int domain = msg->domain; - for (lf = logfiles; lf; lf = lf->next) { - if (! lf->callback || lf->seems_dead || - ! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) { - continue; - } - lf->callback(severity, domain, msg->msg); - } - pending_log_message_free(msg); - } SMARTLIST_FOREACH_END(msg); - smartlist_clear(messages); - - messages_tmp = pending_cb_messages; - pending_cb_messages = messages; - messages = messages_tmp; - } while (smartlist_len(messages)); - - smartlist_free(messages); - - UNLOCK_LOGS(); -} - -/** Flush all the messages we stored from startup while waiting for log - * initialization. - */ -void -flush_log_messages_from_startup(void) -{ - logfile_t *lf; - - LOCK_LOGS(); - queue_startup_messages = 0; - pending_startup_messages_len = 0; - if (! pending_startup_messages) - goto out; - - SMARTLIST_FOREACH_BEGIN(pending_startup_messages, pending_log_message_t *, - msg) { - int callbacks_deferred = 0; - for (lf = logfiles; lf; lf = lf->next) { - if (! logfile_wants_message(lf, msg->severity, msg->domain)) - continue; - - /* We configure a temporary startup log that goes to stdout, so we - * shouldn't replay to stdout/stderr*/ - if (lf->fd == STDOUT_FILENO || lf->fd == STDERR_FILENO) { - continue; - } - - logfile_deliver(lf, msg->fullmsg, strlen(msg->fullmsg), msg->msg, - msg->domain, msg->severity, &callbacks_deferred); - } - pending_log_message_free(msg); - } SMARTLIST_FOREACH_END(msg); - smartlist_free(pending_startup_messages); - pending_startup_messages = NULL; - - out: - UNLOCK_LOGS(); -} - -/** Close any log handlers added by add_temp_log() or marked by - * mark_logs_temp(). */ -void -close_temp_logs(void) -{ - logfile_t *lf, **p; - - LOCK_LOGS(); - for (p = &logfiles; *p; ) { - if ((*p)->is_temporary) { - lf = *p; - /* we use *p here to handle the edge case of the head of the list */ - *p = (*p)->next; - close_log(lf); - log_free(lf); - } else { - p = &((*p)->next); - } - } - - log_global_min_severity_ = get_min_log_level(); - UNLOCK_LOGS(); -} - -/** Make all currently temporary logs (set to be closed by close_temp_logs) - * live again, and close all non-temporary logs. */ -void -rollback_log_changes(void) -{ - logfile_t *lf; - LOCK_LOGS(); - for (lf = logfiles; lf; lf = lf->next) - lf->is_temporary = ! lf->is_temporary; - UNLOCK_LOGS(); - close_temp_logs(); -} - -/** Configure all log handles to be closed by close_temp_logs(). */ -void -mark_logs_temp(void) -{ - logfile_t *lf; - LOCK_LOGS(); - for (lf = logfiles; lf; lf = lf->next) - lf->is_temporary = 1; - UNLOCK_LOGS(); -} - -/** - * Add a log handler to send messages to <b>filename</b> via <b>fd</b>. If - * opening the logfile failed, -1 is returned and errno is set appropriately - * (by open(2)). Takes ownership of fd. - */ -int -add_file_log(const log_severity_list_t *severity, - const char *filename, - int fd) -{ - logfile_t *lf; - - if (fd<0) - return -1; - if (tor_fd_seekend(fd)<0) { - close(fd); - return -1; - } - - LOCK_LOGS(); - add_stream_log_impl(severity, filename, fd); - logfiles->needs_close = 1; - lf = logfiles; - log_global_min_severity_ = get_min_log_level(); - - if (log_tor_version(lf, 0) < 0) { - delete_log(lf); - } - UNLOCK_LOGS(); - - return 0; -} - -#ifdef HAVE_SYSLOG_H -/** - * Add a log handler to send messages to they system log facility. - * - * If this is the first log handler, opens syslog with ident Tor or - * Tor-<syslog_identity_tag> if that is not NULL. - */ -int -add_syslog_log(const log_severity_list_t *severity, - const char* syslog_identity_tag) -{ - logfile_t *lf; - if (syslog_count++ == 0) { - /* This is the first syslog. */ - static char buf[256]; - if (syslog_identity_tag) { - tor_snprintf(buf, sizeof(buf), "Tor-%s", syslog_identity_tag); - } else { - tor_snprintf(buf, sizeof(buf), "Tor"); - } - openlog(buf, LOG_PID | LOG_NDELAY, LOGFACILITY); - } - - lf = tor_malloc_zero(sizeof(logfile_t)); - lf->fd = -1; - lf->severities = tor_memdup(severity, sizeof(log_severity_list_t)); - lf->filename = tor_strdup("<syslog>"); - lf->is_syslog = 1; - - LOCK_LOGS(); - lf->next = logfiles; - logfiles = lf; - log_global_min_severity_ = get_min_log_level(); - UNLOCK_LOGS(); - return 0; -} -#endif /* defined(HAVE_SYSLOG_H) */ - -#ifdef HAVE_ANDROID_LOG_H -/** - * Add a log handler to send messages to the Android platform log facility. - */ -int -add_android_log(const log_severity_list_t *severity, - const char *android_tag) -{ - logfile_t *lf = NULL; - - lf = tor_malloc_zero(sizeof(logfile_t)); - lf->fd = -1; - lf->severities = tor_memdup(severity, sizeof(log_severity_list_t)); - lf->filename = tor_strdup("<android>"); - lf->is_android = 1; - - if (android_tag == NULL) - lf->android_tag = tor_strdup("Tor"); - else { - char buf[256]; - tor_snprintf(buf, sizeof(buf), "Tor-%s", android_tag); - lf->android_tag = tor_strdup(buf); - } - - LOCK_LOGS(); - lf->next = logfiles; - logfiles = lf; - log_global_min_severity_ = get_min_log_level(); - UNLOCK_LOGS(); - return 0; -} -#endif // HAVE_ANDROID_LOG_H. - -/** If <b>level</b> is a valid log severity, return the corresponding - * numeric value. Otherwise, return -1. */ -int -parse_log_level(const char *level) -{ - if (!strcasecmp(level, "err")) - return LOG_ERR; - if (!strcasecmp(level, "warn")) - return LOG_WARN; - if (!strcasecmp(level, "notice")) - return LOG_NOTICE; - if (!strcasecmp(level, "info")) - return LOG_INFO; - if (!strcasecmp(level, "debug")) - return LOG_DEBUG; - return -1; -} - -/** Return the string equivalent of a given log level. */ -const char * -log_level_to_string(int level) -{ - return sev_to_string(level); -} - -/** NULL-terminated array of names for log domains such that domain_list[dom] - * is a description of <b>dom</b>. - * - * Remember to update doc/tor.1.txt if you modify this list. - * */ -static const char *domain_list[] = { - "GENERAL", "CRYPTO", "NET", "CONFIG", "FS", "PROTOCOL", "MM", - "HTTP", "APP", "CONTROL", "CIRC", "REND", "BUG", "DIR", "DIRSERV", - "OR", "EDGE", "ACCT", "HIST", "HANDSHAKE", "HEARTBEAT", "CHANNEL", - "SCHED", "GUARD", "CONSDIFF", "DOS", NULL -}; - -/** Return a bitmask for the log domain for which <b>domain</b> is the name, - * or 0 if there is no such name. */ -static log_domain_mask_t -parse_log_domain(const char *domain) -{ - int i; - for (i=0; domain_list[i]; ++i) { - if (!strcasecmp(domain, domain_list[i])) - return (1u<<i); - } - return 0; -} - -/** Translate a bitmask of log domains to a string. */ -static char * -domain_to_string(log_domain_mask_t domain, char *buf, size_t buflen) -{ - char *cp = buf; - char *eos = buf+buflen; - - buf[0] = '\0'; - if (! domain) - return buf; - while (1) { - const char *d; - int bit = tor_log2(domain); - size_t n; - if ((unsigned)bit >= ARRAY_LENGTH(domain_list)-1 || - bit >= N_LOGGING_DOMAINS) { - tor_snprintf(buf, buflen, "<BUG:Unknown domain %lx>", (long)domain); - return buf+strlen(buf); - } - d = domain_list[bit]; - n = strlcpy(cp, d, eos-cp); - if (n >= buflen) { - tor_snprintf(buf, buflen, "<BUG:Truncating domain %lx>", (long)domain); - return buf+strlen(buf); - } - cp += n; - domain &= ~(1<<bit); - - if (domain == 0 || (eos-cp) < 2) - return cp; - - memcpy(cp, ",", 2); /*Nul-terminated ,"*/ - cp++; - } -} - -/** Parse a log severity pattern in *<b>cfg_ptr</b>. Advance cfg_ptr after - * the end of the severityPattern. Set the value of <b>severity_out</b> to - * the parsed pattern. Return 0 on success, -1 on failure. - * - * The syntax for a SeverityPattern is: - * <pre> - * SeverityPattern = *(DomainSeverity SP)* DomainSeverity - * DomainSeverity = (DomainList SP)? SeverityRange - * SeverityRange = MinSeverity ("-" MaxSeverity )? - * DomainList = "[" (SP? DomainSpec SP? ",") SP? DomainSpec "]" - * DomainSpec = "*" | Domain | "~" Domain - * </pre> - * A missing MaxSeverity defaults to ERR. Severities and domains are - * case-insensitive. "~" indicates negation for a domain; negation happens - * last inside a DomainList. Only one SeverityRange without a DomainList is - * allowed per line. - */ -int -parse_log_severity_config(const char **cfg_ptr, - log_severity_list_t *severity_out) -{ - const char *cfg = *cfg_ptr; - int got_anything = 0; - int got_an_unqualified_range = 0; - memset(severity_out, 0, sizeof(*severity_out)); - - cfg = eat_whitespace(cfg); - while (*cfg) { - const char *dash, *space; - char *sev_lo, *sev_hi; - int low, high, i; - log_domain_mask_t domains = ~0u; - - if (*cfg == '[') { - int err = 0; - char *domains_str; - smartlist_t *domains_list; - log_domain_mask_t neg_domains = 0; - const char *closebracket = strchr(cfg, ']'); - if (!closebracket) - return -1; - domains = 0; - domains_str = tor_strndup(cfg+1, closebracket-cfg-1); - domains_list = smartlist_new(); - smartlist_split_string(domains_list, domains_str, ",", SPLIT_SKIP_SPACE, - -1); - tor_free(domains_str); - SMARTLIST_FOREACH_BEGIN(domains_list, const char *, domain) { - if (!strcmp(domain, "*")) { - domains = ~0u; - } else { - int d; - int negate=0; - if (*domain == '~') { - negate = 1; - ++domain; - } - d = parse_log_domain(domain); - if (!d) { - log_warn(LD_CONFIG, "No such logging domain as %s", domain); - err = 1; - } else { - if (negate) - neg_domains |= d; - else - domains |= d; - } - } - } SMARTLIST_FOREACH_END(domain); - SMARTLIST_FOREACH(domains_list, char *, d, tor_free(d)); - smartlist_free(domains_list); - if (err) - return -1; - if (domains == 0 && neg_domains) - domains = ~neg_domains; - else - domains &= ~neg_domains; - cfg = eat_whitespace(closebracket+1); - } else { - ++got_an_unqualified_range; - } - if (!strcasecmpstart(cfg, "file") || - !strcasecmpstart(cfg, "stderr") || - !strcasecmpstart(cfg, "stdout") || - !strcasecmpstart(cfg, "syslog") || - !strcasecmpstart(cfg, "android")) { - goto done; - } - if (got_an_unqualified_range > 1) - return -1; - - space = find_whitespace(cfg); - dash = strchr(cfg, '-'); - if (dash && dash < space) { - sev_lo = tor_strndup(cfg, dash-cfg); - sev_hi = tor_strndup(dash+1, space-(dash+1)); - } else { - sev_lo = tor_strndup(cfg, space-cfg); - sev_hi = tor_strdup("ERR"); - } - low = parse_log_level(sev_lo); - high = parse_log_level(sev_hi); - tor_free(sev_lo); - tor_free(sev_hi); - if (low == -1) - return -1; - if (high == -1) - return -1; - - got_anything = 1; - for (i=low; i >= high; --i) - severity_out->masks[SEVERITY_MASK_IDX(i)] |= domains; - - cfg = eat_whitespace(space); - } - - done: - *cfg_ptr = cfg; - return got_anything ? 0 : -1; -} - -/** Return the least severe log level that any current log is interested in. */ -int -get_min_log_level(void) -{ - logfile_t *lf; - int i; - int min = LOG_ERR; - for (lf = logfiles; lf; lf = lf->next) { - for (i = LOG_DEBUG; i > min; --i) - if (lf->severities->masks[SEVERITY_MASK_IDX(i)]) - min = i; - } - return min; -} - -/** Switch all logs to output at most verbose level. */ -void -switch_logs_debug(void) -{ - logfile_t *lf; - int i; - LOCK_LOGS(); - for (lf = logfiles; lf; lf=lf->next) { - for (i = LOG_DEBUG; i >= LOG_ERR; --i) - lf->severities->masks[SEVERITY_MASK_IDX(i)] = ~0u; - } - log_global_min_severity_ = get_min_log_level(); - UNLOCK_LOGS(); -} - -/** Truncate all the log files. */ -void -truncate_logs(void) -{ - logfile_t *lf; - for (lf = logfiles; lf; lf = lf->next) { - if (lf->fd >= 0) { - tor_ftruncate(lf->fd); - } - } -} diff --git a/src/lib/log/torlog.h b/src/lib/log/torlog.h @@ -1,276 +0,0 @@ -/* Copyright (c) 2001, Matej Pfajfar. - * Copyright (c) 2001-2004, Roger Dingledine. - * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2018, The Tor Project, Inc. */ -/* See LICENSE for licensing information */ - -/** - * \file torlog.h - * - * \brief Headers for log.c - **/ - -#ifndef TOR_TORLOG_H - -#include <stdarg.h> -#include "lib/cc/torint.h" -#include "lib/cc/compat_compiler.h" -#include "lib/testsupport/testsupport.h" - -#ifdef HAVE_SYSLOG_H -#include <syslog.h> -#define LOG_WARN LOG_WARNING -#if LOG_DEBUG < LOG_ERR -#error "Your syslog.h thinks high numbers are more important. " \ - "We aren't prepared to deal with that." -#endif -#else /* !(defined(HAVE_SYSLOG_H)) */ -/* Note: Syslog's logging code refers to priorities, with 0 being the most - * important. Thus, all our comparisons needed to be reversed when we added - * syslog support. - * - * The upshot of this is that comments about log levels may be messed up: for - * "maximum severity" read "most severe" and "numerically *lowest* severity". - */ - -/** Debug-level severity: for hyper-verbose messages of no interest to - * anybody but developers. */ -#define LOG_DEBUG 7 -/** Info-level severity: for messages that appear frequently during normal - * operation. */ -#define LOG_INFO 6 -/** Notice-level severity: for messages that appear infrequently - * during normal operation; that the user will probably care about; - * and that are not errors. - */ -#define LOG_NOTICE 5 -/** Warn-level severity: for messages that only appear when something has gone - * wrong. */ -#define LOG_WARN 4 -/** Error-level severity: for messages that only appear when something has gone - * very wrong, and the Tor process can no longer proceed. */ -#define LOG_ERR 3 -#endif /* defined(HAVE_SYSLOG_H) */ - -/* Logging domains */ - -/** Catch-all for miscellaneous events and fatal errors. */ -#define LD_GENERAL (1u<<0) -/** The cryptography subsystem. */ -#define LD_CRYPTO (1u<<1) -/** Networking. */ -#define LD_NET (1u<<2) -/** Parsing and acting on our configuration. */ -#define LD_CONFIG (1u<<3) -/** Reading and writing from the filesystem. */ -#define LD_FS (1u<<4) -/** Other servers' (non)compliance with the Tor protocol. */ -#define LD_PROTOCOL (1u<<5) -/** Memory management. */ -#define LD_MM (1u<<6) -/** HTTP implementation. */ -#define LD_HTTP (1u<<7) -/** Application (socks) requests. */ -#define LD_APP (1u<<8) -/** Communication via the controller protocol. */ -#define LD_CONTROL (1u<<9) -/** Building, using, and managing circuits. */ -#define LD_CIRC (1u<<10) -/** Hidden services. */ -#define LD_REND (1u<<11) -/** Internal errors in this Tor process. */ -#define LD_BUG (1u<<12) -/** Learning and using information about Tor servers. */ -#define LD_DIR (1u<<13) -/** Learning and using information about Tor servers. */ -#define LD_DIRSERV (1u<<14) -/** Onion routing protocol. */ -#define LD_OR (1u<<15) -/** Generic edge-connection functionality. */ -#define LD_EDGE (1u<<16) -#define LD_EXIT LD_EDGE -/** Bandwidth accounting. */ -#define LD_ACCT (1u<<17) -/** Router history */ -#define LD_HIST (1u<<18) -/** OR handshaking */ -#define LD_HANDSHAKE (1u<<19) -/** Heartbeat messages */ -#define LD_HEARTBEAT (1u<<20) -/** Abstract channel_t code */ -#define LD_CHANNEL (1u<<21) -/** Scheduler */ -#define LD_SCHED (1u<<22) -/** Guard nodes */ -#define LD_GUARD (1u<<23) -/** Generation and application of consensus diffs. */ -#define LD_CONSDIFF (1u<<24) -/** Denial of Service mitigation. */ -#define LD_DOS (1u<<25) -/** Number of logging domains in the code. */ -#define N_LOGGING_DOMAINS 26 - -/** This log message is not safe to send to a callback-based logger - * immediately. Used as a flag, not a log domain. */ -#define LD_NOCB (1u<<31) -/** This log message should not include a function name, even if it otherwise - * would. Used as a flag, not a log domain. */ -#define LD_NOFUNCNAME (1u<<30) - -#ifdef TOR_UNIT_TESTS -/** This log message should not be intercepted by mock_saving_logv */ -#define LD_NO_MOCK (1u<<29) -#endif - -/** Mask of zero or more log domains, OR'd together. */ -typedef uint32_t log_domain_mask_t; - -/** Configures which severities are logged for each logging domain for a given - * log target. */ -typedef struct log_severity_list_t { - /** For each log severity, a bitmask of which domains a given logger is - * logging. */ - log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1]; -} log_severity_list_t; - -/** Callback type used for add_callback_log. */ -typedef void (*log_callback)(int severity, uint32_t domain, const char *msg); - -void init_logging(int disable_startup_queue); -int parse_log_level(const char *level); -const char *log_level_to_string(int level); -int parse_log_severity_config(const char **cfg, - log_severity_list_t *severity_out); -void set_log_severity_config(int minSeverity, int maxSeverity, - log_severity_list_t *severity_out); -void add_stream_log(const log_severity_list_t *severity, const char *name, - int fd); -int add_file_log(const log_severity_list_t *severity, - const char *filename, - int fd); - -#ifdef HAVE_SYSLOG_H -int add_syslog_log(const log_severity_list_t *severity, - const char* syslog_identity_tag); -#endif // HAVE_SYSLOG_H. -#ifdef HAVE_ANDROID_LOG_H -int add_android_log(const log_severity_list_t *severity, - const char *android_identity_tag); -#endif // HAVE_ANDROID_LOG_H. -int add_callback_log(const log_severity_list_t *severity, log_callback cb); -typedef void (*pending_callback_callback)(void); -void logs_set_pending_callback_callback(pending_callback_callback cb); -void logs_set_domain_logging(int enabled); -int get_min_log_level(void); -void switch_logs_debug(void); -void logs_free_all(void); -void add_temp_log(int min_severity); -void close_temp_logs(void); -void rollback_log_changes(void); -void mark_logs_temp(void); -void change_callback_log_severity(int loglevelMin, int loglevelMax, - log_callback cb); -void flush_pending_log_callbacks(void); -void flush_log_messages_from_startup(void); -void log_set_application_name(const char *name); -void set_log_time_granularity(int granularity_msec); -void truncate_logs(void); - -void tor_log(int severity, log_domain_mask_t domain, const char *format, ...) - CHECK_PRINTF(3,4); - -void tor_log_update_sigsafe_err_fds(void); - -struct smartlist_t; -void tor_log_get_logfile_names(struct smartlist_t *out); - -extern int log_global_min_severity_; - -void log_fn_(int severity, log_domain_mask_t domain, - const char *funcname, const char *format, ...) - CHECK_PRINTF(4,5); -struct ratelim_t; -void log_fn_ratelim_(struct ratelim_t *ratelim, int severity, - log_domain_mask_t domain, const char *funcname, - const char *format, ...) - CHECK_PRINTF(5,6); - -int log_message_is_interesting(int severity, log_domain_mask_t domain); -void tor_log_string(int severity, log_domain_mask_t domain, - const char *function, const char *string); - -#if defined(__GNUC__) && __GNUC__ <= 3 - -/* These are the GCC varidaic macros, so that older versions of GCC don't - * break. */ - -/** Log a message at level <b>severity</b>, using a pretty-printed version - * of the current function name. */ -#define log_fn(severity, domain, args...) \ - log_fn_(severity, domain, __FUNCTION__, args) -/** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control - * the frequency at which messages can appear. - */ -#define log_fn_ratelim(ratelim, severity, domain, args...) \ - log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, args) -#define log_debug(domain, args...) \ - STMT_BEGIN \ - if (PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG)) \ - log_fn_(LOG_DEBUG, domain, __FUNCTION__, args); \ - STMT_END -#define log_info(domain, args...) \ - log_fn_(LOG_INFO, domain, __FUNCTION__, args) -#define log_notice(domain, args...) \ - log_fn_(LOG_NOTICE, domain, __FUNCTION__, args) -#define log_warn(domain, args...) \ - log_fn_(LOG_WARN, domain, __FUNCTION__, args) -#define log_err(domain, args...) \ - log_fn_(LOG_ERR, domain, __FUNCTION__, args) - -#else /* !(defined(__GNUC__) && __GNUC__ <= 3) */ - -/* Here are the c99 variadic macros, to work with non-GCC compilers */ - -#define log_debug(domain, args, ...) \ - STMT_BEGIN \ - if (PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG)) \ - log_fn_(LOG_DEBUG, domain, __FUNCTION__, args, ##__VA_ARGS__); \ - STMT_END -#define log_info(domain, args,...) \ - log_fn_(LOG_INFO, domain, __FUNCTION__, args, ##__VA_ARGS__) -#define log_notice(domain, args,...) \ - log_fn_(LOG_NOTICE, domain, __FUNCTION__, args, ##__VA_ARGS__) -#define log_warn(domain, args,...) \ - log_fn_(LOG_WARN, domain, __FUNCTION__, args, ##__VA_ARGS__) -#define log_err(domain, args,...) \ - log_fn_(LOG_ERR, domain, __FUNCTION__, args, ##__VA_ARGS__) -/** Log a message at level <b>severity</b>, using a pretty-printed version - * of the current function name. */ -#define log_fn(severity, domain, args,...) \ - log_fn_(severity, domain, __FUNCTION__, args, ##__VA_ARGS__) -/** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control - * the frequency at which messages can appear. - */ -#define log_fn_ratelim(ratelim, severity, domain, args,...) \ - log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, \ - args, ##__VA_ARGS__) -#endif /* defined(__GNUC__) && __GNUC__ <= 3 */ - -/** This defines log levels that are linked in the Rust log module, rather - * than re-defining these in both Rust and C. - * - * C_RUST_COUPLED src/rust/tor_log LogSeverity, LogDomain - */ -extern const int LOG_WARN_; -extern const int LOG_NOTICE_; -extern const log_domain_mask_t LD_NET_; -extern const log_domain_mask_t LD_GENERAL_; - -#ifdef LOG_PRIVATE -MOCK_DECL(STATIC void, logv, (int severity, log_domain_mask_t domain, - const char *funcname, const char *suffix, const char *format, - va_list ap) CHECK_PRINTF(5,0)); -#endif - -# define TOR_TORLOG_H -#endif /* !defined(TOR_TORLOG_H) */ diff --git a/src/lib/log/util_bug.c b/src/lib/log/util_bug.c @@ -9,13 +9,13 @@ #include "orconfig.h" #include "lib/log/util_bug.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/err/backtrace.h" #ifdef TOR_UNIT_TESTS #include "lib/smartlist_core/smartlist_core.h" #include "lib/smartlist_core/smartlist_foreach.h" #endif -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/string/printf.h" #include <string.h> diff --git a/src/lib/log/util_bug.h b/src/lib/log/util_bug.h @@ -38,7 +38,7 @@ #include "orconfig.h" #include "lib/cc/compat_compiler.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/testsupport/testsupport.h" /* Replace assert() with a variant that sends failures to the log before diff --git a/src/lib/log/win32err.c b/src/lib/log/win32err.c @@ -11,7 +11,7 @@ #ifdef _WIN32 #include "orconfig.h" #include "lib/log/win32err.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include <tchar.h> #include <windows.h> diff --git a/src/lib/malloc/include.am b/src/lib/malloc/include.am @@ -6,7 +6,7 @@ noinst_LIBRARIES += src/lib/libtor-malloc-testing.a endif src_lib_libtor_malloc_a_SOURCES = \ - src/lib/malloc/util_malloc.c + src/lib/malloc/malloc.c if USE_OPENBSD_MALLOC src_lib_libtor_malloc_a_SOURCES += src/ext/OpenBSD_malloc_Linux.c @@ -18,4 +18,4 @@ src_lib_libtor_malloc_testing_a_CPPFLAGS = $(AM_CPPFLAGS) $(TEST_CPPFLAGS) src_lib_libtor_malloc_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS) noinst_HEADERS += \ - src/lib/malloc/util_malloc.h + src/lib/malloc/malloc.h diff --git a/src/lib/malloc/malloc.c b/src/lib/malloc/malloc.c @@ -0,0 +1,230 @@ +/* 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 malloc.c + * \brief Wrappers for C malloc code, and replacements for items that + * may be missing. + **/ + +#include "orconfig.h" + +#include <stdlib.h> +#include <string.h> + +#include "lib/testsupport/testsupport.h" +#define UTIL_MALLOC_PRIVATE +#include "lib/malloc/malloc.h" +#include "lib/cc/torint.h" +#include "lib/err/torerr.h" + +#ifdef __clang_analyzer__ +#undef MALLOC_ZERO_WORKS +#endif + +/** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to + * result. On error, log and terminate the process. (Same as malloc(size), + * but never returns NULL.) + */ +void * +tor_malloc_(size_t size) +{ + void *result; + + raw_assert(size < SIZE_T_CEILING); + +#ifndef MALLOC_ZERO_WORKS + /* Some libc mallocs don't work when size==0. Override them. */ + if (size==0) { + size=1; + } +#endif /* !defined(MALLOC_ZERO_WORKS) */ + + result = raw_malloc(size); + + if (PREDICT_UNLIKELY(result == NULL)) { + /* LCOV_EXCL_START */ + /* If these functions die within a worker process, they won't call + * spawn_exit, but that's ok, since the parent will run out of memory soon + * anyway. */ + raw_assert_unreached_msg("Out of memory on malloc(). Dying."); + /* LCOV_EXCL_STOP */ + } + return result; +} + +/** Allocate a chunk of <b>size</b> bytes of memory, fill the memory with + * zero bytes, and return a pointer to the result. Log and terminate + * the process on error. (Same as calloc(size,1), but never returns NULL.) + */ +void * +tor_malloc_zero_(size_t size) +{ + /* You may ask yourself, "wouldn't it be smart to use calloc instead of + * malloc+memset? Perhaps libc's calloc knows some nifty optimization trick + * we don't!" Indeed it does, but its optimizations are only a big win when + * we're allocating something very big (it knows if it just got the memory + * from the OS in a pre-zeroed state). We don't want to use tor_malloc_zero + * for big stuff, so we don't bother with calloc. */ + void *result = tor_malloc_(size); + memset(result, 0, size); + return result; +} + +/* The square root of SIZE_MAX + 1. If a is less than this, and b is less + * than this, then a*b is less than SIZE_MAX. (For example, if size_t is + * 32 bits, then SIZE_MAX is 0xffffffff and this value is 0x10000. If a and + * b are less than this, then their product is at most (65535*65535) == + * 0xfffe0001. */ +#define SQRT_SIZE_MAX_P1 (((size_t)1) << (sizeof(size_t)*4)) + +/** Return non-zero if and only if the product of the arguments is exact, + * and cannot overflow. */ +STATIC int +size_mul_check(const size_t x, const size_t y) +{ + /* This first check is equivalent to + (x < SQRT_SIZE_MAX_P1 && y < SQRT_SIZE_MAX_P1) + + Rationale: if either one of x or y is >= SQRT_SIZE_MAX_P1, then it + will have some bit set in its most significant half. + */ + return ((x|y) < SQRT_SIZE_MAX_P1 || + y == 0 || + x <= SIZE_MAX / y); +} + +/** Allocate a chunk of <b>nmemb</b>*<b>size</b> bytes of memory, fill + * the memory with zero bytes, and return a pointer to the result. + * Log and terminate the process on error. (Same as + * calloc(<b>nmemb</b>,<b>size</b>), but never returns NULL.) + * The second argument (<b>size</b>) should preferably be non-zero + * and a compile-time constant. + */ +void * +tor_calloc_(size_t nmemb, size_t size) +{ + raw_assert(size_mul_check(nmemb, size)); + return tor_malloc_zero_((nmemb * size)); +} + +/** Change the size of the memory block pointed to by <b>ptr</b> to <b>size</b> + * bytes long; return the new memory block. On error, log and + * terminate. (Like realloc(ptr,size), but never returns NULL.) + */ +void * +tor_realloc_(void *ptr, size_t size) +{ + void *result; + + raw_assert(size < SIZE_T_CEILING); + +#ifndef MALLOC_ZERO_WORKS + /* Some libc mallocs don't work when size==0. Override them. */ + if (size==0) { + size=1; + } +#endif /* !defined(MALLOC_ZERO_WORKS) */ + + result = raw_realloc(ptr, size); + + if (PREDICT_UNLIKELY(result == NULL)) { + /* LCOV_EXCL_START */ + raw_assert_unreached_msg("Out of memory on realloc(). Dying."); + /* LCOV_EXCL_STOP */ + } + return result; +} + +/** + * Try to realloc <b>ptr</b> so that it takes up sz1 * sz2 bytes. Check for + * overflow. Unlike other allocation functions, return NULL on overflow. + */ +void * +tor_reallocarray_(void *ptr, size_t sz1, size_t sz2) +{ + /* XXXX we can make this return 0, but we would need to check all the + * reallocarray users. */ + raw_assert(size_mul_check(sz1, sz2)); + + return tor_realloc(ptr, (sz1 * sz2)); +} + +/** Return a newly allocated copy of the NUL-terminated string s. On + * error, log and terminate. (Like strdup(s), but never returns + * NULL.) + */ +char * +tor_strdup_(const char *s) +{ + char *duplicate; + raw_assert(s); + + duplicate = raw_strdup(s); + + if (PREDICT_UNLIKELY(duplicate == NULL)) { + /* LCOV_EXCL_START */ + raw_assert_unreached_msg("Out of memory on strdup(). Dying."); + /* LCOV_EXCL_STOP */ + } + return duplicate; +} + +/** Allocate and return a new string containing the first <b>n</b> + * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b> + * characters, only the first <b>n</b> are copied. The result is + * always NUL-terminated. (Like strndup(s,n), but never returns + * NULL.) + */ +char * +tor_strndup_(const char *s, size_t n) +{ + char *duplicate; + raw_assert(s); + raw_assert(n < SIZE_T_CEILING); + duplicate = tor_malloc_((n+1)); + /* Performance note: Ordinarily we prefer strlcpy to strncpy. But + * this function gets called a whole lot, and platform strncpy is + * much faster than strlcpy when strlen(s) is much longer than n. + */ + strncpy(duplicate, s, n); + duplicate[n]='\0'; + return duplicate; +} + +/** Allocate a chunk of <b>len</b> bytes, with the same contents as the + * <b>len</b> bytes starting at <b>mem</b>. */ +void * +tor_memdup_(const void *mem, size_t len) +{ + char *duplicate; + raw_assert(len < SIZE_T_CEILING); + raw_assert(mem); + duplicate = tor_malloc_(len); + memcpy(duplicate, mem, len); + return duplicate; +} + +/** As tor_memdup(), but add an extra 0 byte at the end of the resulting + * memory. */ +void * +tor_memdup_nulterm_(const void *mem, size_t len) +{ + char *duplicate; + raw_assert(len < SIZE_T_CEILING+1); + raw_assert(mem); + duplicate = tor_malloc_(len+1); + memcpy(duplicate, mem, len); + duplicate[len] = '\0'; + return duplicate; +} + +/** Helper for places that need to take a function pointer to the right + * spelling of "free()". */ +void +tor_free_(void *mem) +{ + tor_free(mem); +} diff --git a/src/lib/malloc/malloc.h b/src/lib/malloc/malloc.h @@ -0,0 +1,92 @@ +/* Copyright (c) 2003-2004, Roger Dingledine + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file malloc.h + * \brief Headers for util_malloc.c + **/ + +#ifndef TOR_UTIL_MALLOC_H +#define TOR_UTIL_MALLOC_H + +#include <stddef.h> +#include <stdlib.h> +#include "lib/cc/compat_compiler.h" + +/* Memory management */ +void *tor_malloc_(size_t size) ATTR_MALLOC; +void *tor_malloc_zero_(size_t size) ATTR_MALLOC; +void *tor_calloc_(size_t nmemb, size_t size) ATTR_MALLOC; +void *tor_realloc_(void *ptr, size_t size); +void *tor_reallocarray_(void *ptr, size_t size1, size_t size2); +char *tor_strdup_(const char *s) ATTR_MALLOC; +char *tor_strndup_(const char *s, size_t n) + ATTR_MALLOC; +void *tor_memdup_(const void *mem, size_t len) + ATTR_MALLOC; +void *tor_memdup_nulterm_(const void *mem, size_t len) + ATTR_MALLOC; +void tor_free_(void *mem); + +/** Release memory allocated by tor_malloc, tor_realloc, tor_strdup, + * etc. Unlike the free() function, the tor_free() macro sets the + * pointer value to NULL after freeing it. + * + * This is a macro. If you need a function pointer to release memory from + * tor_malloc(), use tor_free_(). + * + * Note that this macro takes the address of the pointer it is going to + * free and clear. If that pointer is stored with a nonstandard + * alignment (eg because of a "packed" pragma) it is not correct to use + * tor_free(). + */ +#ifdef __GNUC__ +#define tor_free(p) STMT_BEGIN \ + typeof(&(p)) tor_free__tmpvar = &(p); \ + raw_free(*tor_free__tmpvar); \ + *tor_free__tmpvar=NULL; \ + STMT_END +#else +#define tor_free(p) STMT_BEGIN \ + raw_free(p); \ + (p)=NULL; \ + STMT_END +#endif + +#define tor_malloc(size) tor_malloc_(size) +#define tor_malloc_zero(size) tor_malloc_zero_(size) +#define tor_calloc(nmemb,size) tor_calloc_(nmemb, size) +#define tor_realloc(ptr, size) tor_realloc_(ptr, size) +#define tor_reallocarray(ptr, sz1, sz2) \ + tor_reallocarray_((ptr), (sz1), (sz2)) +#define tor_strdup(s) tor_strdup_(s) +#define tor_strndup(s, n) tor_strndup_(s, n) +#define tor_memdup(s, n) tor_memdup_(s, n) +#define tor_memdup_nulterm(s, n) tor_memdup_nulterm_(s, n) + +/* Aliases for the underlying system malloc/realloc/free. Only use + * them to indicate "I really want the underlying system function, I know + * what I'm doing." */ +#define raw_malloc malloc +#define raw_realloc realloc +#define raw_free free +#define raw_strdup strdup + +/* Helper macro: free a variable of type 'typename' using freefn, and + * set the variable to NULL. + */ +#define FREE_AND_NULL(typename, freefn, var) \ + do { \ + /* only evaluate (var) once. */ \ + typename **tmp__free__ptr ## freefn = &(var); \ + freefn(*tmp__free__ptr ## freefn); \ + (*tmp__free__ptr ## freefn) = NULL; \ + } while (0) + +#ifdef UTIL_MALLOC_PRIVATE +STATIC int size_mul_check(const size_t x, const size_t y); +#endif + +#endif /* !defined(TOR_UTIL_MALLOC_H) */ diff --git a/src/lib/malloc/util_malloc.c b/src/lib/malloc/util_malloc.c @@ -1,230 +0,0 @@ -/* 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_malloc.c - * \brief Wrappers for C malloc code, and replacements for items that - * may be missing. - **/ - -#include "orconfig.h" - -#include <stdlib.h> -#include <string.h> - -#include "lib/testsupport/testsupport.h" -#define UTIL_MALLOC_PRIVATE -#include "lib/malloc/util_malloc.h" -#include "lib/cc/torint.h" -#include "lib/err/torerr.h" - -#ifdef __clang_analyzer__ -#undef MALLOC_ZERO_WORKS -#endif - -/** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to - * result. On error, log and terminate the process. (Same as malloc(size), - * but never returns NULL.) - */ -void * -tor_malloc_(size_t size) -{ - void *result; - - raw_assert(size < SIZE_T_CEILING); - -#ifndef MALLOC_ZERO_WORKS - /* Some libc mallocs don't work when size==0. Override them. */ - if (size==0) { - size=1; - } -#endif /* !defined(MALLOC_ZERO_WORKS) */ - - result = raw_malloc(size); - - if (PREDICT_UNLIKELY(result == NULL)) { - /* LCOV_EXCL_START */ - /* If these functions die within a worker process, they won't call - * spawn_exit, but that's ok, since the parent will run out of memory soon - * anyway. */ - raw_assert_unreached_msg("Out of memory on malloc(). Dying."); - /* LCOV_EXCL_STOP */ - } - return result; -} - -/** Allocate a chunk of <b>size</b> bytes of memory, fill the memory with - * zero bytes, and return a pointer to the result. Log and terminate - * the process on error. (Same as calloc(size,1), but never returns NULL.) - */ -void * -tor_malloc_zero_(size_t size) -{ - /* You may ask yourself, "wouldn't it be smart to use calloc instead of - * malloc+memset? Perhaps libc's calloc knows some nifty optimization trick - * we don't!" Indeed it does, but its optimizations are only a big win when - * we're allocating something very big (it knows if it just got the memory - * from the OS in a pre-zeroed state). We don't want to use tor_malloc_zero - * for big stuff, so we don't bother with calloc. */ - void *result = tor_malloc_(size); - memset(result, 0, size); - return result; -} - -/* The square root of SIZE_MAX + 1. If a is less than this, and b is less - * than this, then a*b is less than SIZE_MAX. (For example, if size_t is - * 32 bits, then SIZE_MAX is 0xffffffff and this value is 0x10000. If a and - * b are less than this, then their product is at most (65535*65535) == - * 0xfffe0001. */ -#define SQRT_SIZE_MAX_P1 (((size_t)1) << (sizeof(size_t)*4)) - -/** Return non-zero if and only if the product of the arguments is exact, - * and cannot overflow. */ -STATIC int -size_mul_check(const size_t x, const size_t y) -{ - /* This first check is equivalent to - (x < SQRT_SIZE_MAX_P1 && y < SQRT_SIZE_MAX_P1) - - Rationale: if either one of x or y is >= SQRT_SIZE_MAX_P1, then it - will have some bit set in its most significant half. - */ - return ((x|y) < SQRT_SIZE_MAX_P1 || - y == 0 || - x <= SIZE_MAX / y); -} - -/** Allocate a chunk of <b>nmemb</b>*<b>size</b> bytes of memory, fill - * the memory with zero bytes, and return a pointer to the result. - * Log and terminate the process on error. (Same as - * calloc(<b>nmemb</b>,<b>size</b>), but never returns NULL.) - * The second argument (<b>size</b>) should preferably be non-zero - * and a compile-time constant. - */ -void * -tor_calloc_(size_t nmemb, size_t size) -{ - raw_assert(size_mul_check(nmemb, size)); - return tor_malloc_zero_((nmemb * size)); -} - -/** Change the size of the memory block pointed to by <b>ptr</b> to <b>size</b> - * bytes long; return the new memory block. On error, log and - * terminate. (Like realloc(ptr,size), but never returns NULL.) - */ -void * -tor_realloc_(void *ptr, size_t size) -{ - void *result; - - raw_assert(size < SIZE_T_CEILING); - -#ifndef MALLOC_ZERO_WORKS - /* Some libc mallocs don't work when size==0. Override them. */ - if (size==0) { - size=1; - } -#endif /* !defined(MALLOC_ZERO_WORKS) */ - - result = raw_realloc(ptr, size); - - if (PREDICT_UNLIKELY(result == NULL)) { - /* LCOV_EXCL_START */ - raw_assert_unreached_msg("Out of memory on realloc(). Dying."); - /* LCOV_EXCL_STOP */ - } - return result; -} - -/** - * Try to realloc <b>ptr</b> so that it takes up sz1 * sz2 bytes. Check for - * overflow. Unlike other allocation functions, return NULL on overflow. - */ -void * -tor_reallocarray_(void *ptr, size_t sz1, size_t sz2) -{ - /* XXXX we can make this return 0, but we would need to check all the - * reallocarray users. */ - raw_assert(size_mul_check(sz1, sz2)); - - return tor_realloc(ptr, (sz1 * sz2)); -} - -/** Return a newly allocated copy of the NUL-terminated string s. On - * error, log and terminate. (Like strdup(s), but never returns - * NULL.) - */ -char * -tor_strdup_(const char *s) -{ - char *duplicate; - raw_assert(s); - - duplicate = raw_strdup(s); - - if (PREDICT_UNLIKELY(duplicate == NULL)) { - /* LCOV_EXCL_START */ - raw_assert_unreached_msg("Out of memory on strdup(). Dying."); - /* LCOV_EXCL_STOP */ - } - return duplicate; -} - -/** Allocate and return a new string containing the first <b>n</b> - * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b> - * characters, only the first <b>n</b> are copied. The result is - * always NUL-terminated. (Like strndup(s,n), but never returns - * NULL.) - */ -char * -tor_strndup_(const char *s, size_t n) -{ - char *duplicate; - raw_assert(s); - raw_assert(n < SIZE_T_CEILING); - duplicate = tor_malloc_((n+1)); - /* Performance note: Ordinarily we prefer strlcpy to strncpy. But - * this function gets called a whole lot, and platform strncpy is - * much faster than strlcpy when strlen(s) is much longer than n. - */ - strncpy(duplicate, s, n); - duplicate[n]='\0'; - return duplicate; -} - -/** Allocate a chunk of <b>len</b> bytes, with the same contents as the - * <b>len</b> bytes starting at <b>mem</b>. */ -void * -tor_memdup_(const void *mem, size_t len) -{ - char *duplicate; - raw_assert(len < SIZE_T_CEILING); - raw_assert(mem); - duplicate = tor_malloc_(len); - memcpy(duplicate, mem, len); - return duplicate; -} - -/** As tor_memdup(), but add an extra 0 byte at the end of the resulting - * memory. */ -void * -tor_memdup_nulterm_(const void *mem, size_t len) -{ - char *duplicate; - raw_assert(len < SIZE_T_CEILING+1); - raw_assert(mem); - duplicate = tor_malloc_(len+1); - memcpy(duplicate, mem, len); - duplicate[len] = '\0'; - return duplicate; -} - -/** Helper for places that need to take a function pointer to the right - * spelling of "free()". */ -void -tor_free_(void *mem) -{ - tor_free(mem); -} diff --git a/src/lib/malloc/util_malloc.h b/src/lib/malloc/util_malloc.h @@ -1,92 +0,0 @@ -/* Copyright (c) 2003-2004, 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_malloc.h - * \brief Headers for util_malloc.c - **/ - -#ifndef TOR_UTIL_MALLOC_H -#define TOR_UTIL_MALLOC_H - -#include <stddef.h> -#include <stdlib.h> -#include "lib/cc/compat_compiler.h" - -/* Memory management */ -void *tor_malloc_(size_t size) ATTR_MALLOC; -void *tor_malloc_zero_(size_t size) ATTR_MALLOC; -void *tor_calloc_(size_t nmemb, size_t size) ATTR_MALLOC; -void *tor_realloc_(void *ptr, size_t size); -void *tor_reallocarray_(void *ptr, size_t size1, size_t size2); -char *tor_strdup_(const char *s) ATTR_MALLOC; -char *tor_strndup_(const char *s, size_t n) - ATTR_MALLOC; -void *tor_memdup_(const void *mem, size_t len) - ATTR_MALLOC; -void *tor_memdup_nulterm_(const void *mem, size_t len) - ATTR_MALLOC; -void tor_free_(void *mem); - -/** Release memory allocated by tor_malloc, tor_realloc, tor_strdup, - * etc. Unlike the free() function, the tor_free() macro sets the - * pointer value to NULL after freeing it. - * - * This is a macro. If you need a function pointer to release memory from - * tor_malloc(), use tor_free_(). - * - * Note that this macro takes the address of the pointer it is going to - * free and clear. If that pointer is stored with a nonstandard - * alignment (eg because of a "packed" pragma) it is not correct to use - * tor_free(). - */ -#ifdef __GNUC__ -#define tor_free(p) STMT_BEGIN \ - typeof(&(p)) tor_free__tmpvar = &(p); \ - raw_free(*tor_free__tmpvar); \ - *tor_free__tmpvar=NULL; \ - STMT_END -#else -#define tor_free(p) STMT_BEGIN \ - raw_free(p); \ - (p)=NULL; \ - STMT_END -#endif - -#define tor_malloc(size) tor_malloc_(size) -#define tor_malloc_zero(size) tor_malloc_zero_(size) -#define tor_calloc(nmemb,size) tor_calloc_(nmemb, size) -#define tor_realloc(ptr, size) tor_realloc_(ptr, size) -#define tor_reallocarray(ptr, sz1, sz2) \ - tor_reallocarray_((ptr), (sz1), (sz2)) -#define tor_strdup(s) tor_strdup_(s) -#define tor_strndup(s, n) tor_strndup_(s, n) -#define tor_memdup(s, n) tor_memdup_(s, n) -#define tor_memdup_nulterm(s, n) tor_memdup_nulterm_(s, n) - -/* Aliases for the underlying system malloc/realloc/free. Only use - * them to indicate "I really want the underlying system function, I know - * what I'm doing." */ -#define raw_malloc malloc -#define raw_realloc realloc -#define raw_free free -#define raw_strdup strdup - -/* Helper macro: free a variable of type 'typename' using freefn, and - * set the variable to NULL. - */ -#define FREE_AND_NULL(typename, freefn, var) \ - do { \ - /* only evaluate (var) once. */ \ - typename **tmp__free__ptr ## freefn = &(var); \ - freefn(*tmp__free__ptr ## freefn); \ - (*tmp__free__ptr ## freefn) = NULL; \ - } while (0) - -#ifdef UTIL_MALLOC_PRIVATE -STATIC int size_mul_check(const size_t x, const size_t y); -#endif - -#endif /* !defined(TOR_UTIL_MALLOC_H) */ diff --git a/src/lib/memarea/memarea.c b/src/lib/memarea/memarea.c @@ -17,9 +17,9 @@ #include "lib/arch/bytes.h" #include "lib/cc/torint.h" #include "lib/container/smartlist.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #ifndef DISABLE_MEMORY_SENTINELS diff --git a/src/lib/meminfo/meminfo.c b/src/lib/meminfo/meminfo.c @@ -15,8 +15,8 @@ #include "lib/cc/compat_compiler.h" #include "lib/cc/torint.h" #include "lib/fs/files.h" -#include "lib/log/torlog.h" -#include "lib/malloc/util_malloc.h" +#include "lib/log/log.h" +#include "lib/malloc/malloc.h" #ifdef HAVE_SYS_SYSCTL_H #include <sys/sysctl.h> diff --git a/src/lib/net/address.c b/src/lib/net/address.c @@ -42,9 +42,9 @@ #include "lib/net/socket.h" #include "lib/container/smartlist.h" #include "lib/ctime/di_ops.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/escape.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/net/inaddr.h" #include "lib/string/compat_ctype.h" #include "lib/string/compat_string.h" diff --git a/src/lib/net/buffers_net.c b/src/lib/net/buffers_net.c @@ -12,7 +12,7 @@ #define BUFFERS_PRIVATE #include "lib/net/buffers_net.h" #include "lib/container/buffers.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/net/nettypes.h" diff --git a/src/lib/net/resolve.c b/src/lib/net/resolve.c @@ -12,7 +12,7 @@ #include "lib/net/address.h" #include "lib/net/inaddr.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/string/parse_int.h" #include "lib/string/util_string.h" diff --git a/src/lib/net/socket.c b/src/lib/net/socket.c @@ -15,7 +15,7 @@ #include "lib/cc/compat_compiler.h" #include "lib/err/torerr.h" #include "lib/lock/compat_mutex.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #ifdef _WIN32 diff --git a/src/lib/process/daemon.c b/src/lib/process/daemon.c @@ -14,7 +14,7 @@ #ifndef _WIN32 #include "lib/fs/files.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/thread/threads.h" #ifdef HAVE_SYS_TYPES_H diff --git a/src/lib/process/env.c b/src/lib/process/env.c @@ -11,12 +11,12 @@ #include "orconfig.h" #include "lib/process/env.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/ctime/di_ops.h" #include "lib/container/smartlist.h" #include "lib/log/util_bug.h" -#include "lib/log/torlog.h" -#include "lib/malloc/util_malloc.h" +#include "lib/log/log.h" +#include "lib/malloc/malloc.h" #ifdef HAVE_UNISTD_H #include <unistd.h> diff --git a/src/lib/process/pidfile.c b/src/lib/process/pidfile.c @@ -11,7 +11,7 @@ #include "orconfig.h" #include "lib/process/pidfile.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #ifdef HAVE_SYS_TYPES_H #include <sys/types.h> diff --git a/src/lib/process/restrict.c b/src/lib/process/restrict.c @@ -11,7 +11,7 @@ #include "orconfig.h" #include "lib/process/restrict.h" #include "lib/intmath/cmp.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/net/socket.h" diff --git a/src/lib/process/setuid.c b/src/lib/process/setuid.c @@ -17,9 +17,9 @@ #include "lib/container/smartlist.h" #include "lib/fs/userdb.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #ifdef HAVE_SYS_TYPES_H #include <sys/types.h> diff --git a/src/lib/process/subprocess.c b/src/lib/process/subprocess.c @@ -13,10 +13,10 @@ #include "lib/container/smartlist.h" #include "lib/err/torerr.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/log/win32err.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/process/env.h" #include "lib/process/waitpid.h" #include "lib/string/compat_ctype.h" diff --git a/src/lib/process/waitpid.c b/src/lib/process/waitpid.c @@ -13,9 +13,9 @@ #ifndef _WIN32 #include "lib/process/waitpid.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "ht.h" #ifdef HAVE_SYS_WAIT_H diff --git a/src/lib/sandbox/sandbox.c b/src/lib/sandbox/sandbox.c @@ -36,10 +36,10 @@ #include "lib/sandbox/sandbox.h" #include "lib/container/map.h" #include "lib/err/torerr.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/cc/torint.h" #include "lib/net/resolve.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/string/scanf.h" #include "tor_queue.h" diff --git a/src/lib/smartlist_core/smartlist_core.c b/src/lib/smartlist_core/smartlist_core.c @@ -11,7 +11,7 @@ **/ #include "lib/err/torerr.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/smartlist_core/smartlist_core.h" #include <stdlib.h> diff --git a/src/lib/smartlist_core/smartlist_split.c b/src/lib/smartlist_core/smartlist_split.c @@ -14,7 +14,7 @@ #include "lib/err/torerr.h" #include "lib/string/util_string.h" #include "lib/string/compat_ctype.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include <string.h> diff --git a/src/lib/string/printf.c b/src/lib/string/printf.c @@ -11,7 +11,7 @@ #include "lib/string/printf.h" #include "lib/err/torerr.h" #include "lib/cc/torint.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include <stdlib.h> #include <stdio.h> diff --git a/src/lib/term/getpass.c b/src/lib/term/getpass.c @@ -11,7 +11,7 @@ #include "lib/term/getpass.h" #include "lib/log/util_bug.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #ifdef _WIN32 #include <windows.h> diff --git a/src/lib/thread/compat_pthreads.c b/src/lib/thread/compat_pthreads.c @@ -12,7 +12,7 @@ #include "orconfig.h" #include "lib/thread/threads.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include <sys/time.h> diff --git a/src/lib/thread/compat_threads.c b/src/lib/thread/compat_threads.c @@ -15,7 +15,7 @@ #include <stdlib.h> #include "lib/thread/threads.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include <string.h> diff --git a/src/lib/thread/compat_winthreads.c b/src/lib/thread/compat_winthreads.c @@ -15,7 +15,7 @@ #include <windows.h> #include <process.h> #include "lib/thread/threads.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/log/win32err.h" diff --git a/src/lib/thread/numcpus.c b/src/lib/thread/numcpus.c @@ -10,7 +10,7 @@ #include "orconfig.h" #include "lib/thread/numcpus.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #ifdef HAVE_UNISTD_H diff --git a/src/lib/time/compat_time.c b/src/lib/time/compat_time.c @@ -13,7 +13,7 @@ #include "lib/time/compat_time.h" #include "lib/err/torerr.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/intmath/muldiv.h" #include "lib/intmath/bits.h" diff --git a/src/lib/time/tvdiff.c b/src/lib/time/tvdiff.c @@ -11,7 +11,7 @@ #include "lib/time/tvdiff.h" #include "lib/cc/compat_compiler.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #ifdef _WIN32 #include <winsock2.h> diff --git a/src/lib/tls/buffers_tls.c b/src/lib/tls/buffers_tls.c @@ -15,7 +15,7 @@ #include "lib/container/buffers.h" #include "lib/tls/buffers_tls.h" #include "lib/cc/torint.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/tls/tortls.h" diff --git a/src/lib/tls/tortls.c b/src/lib/tls/tortls.c @@ -52,7 +52,7 @@ ENABLE_GCC_WARNING(redundant-decls) #define TORTLS_PRIVATE #include "lib/tls/tortls.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/log/util_bug.h" #include "lib/container/smartlist.h" #include "lib/string/compat_string.h" diff --git a/src/lib/trace/debug.h b/src/lib/trace/debug.h @@ -9,7 +9,7 @@ #ifndef TOR_TRACE_LOG_DEBUG_H #define TOR_TRACE_LOG_DEBUG_H -#include "lib/log/torlog.h" +#include "lib/log/log.h" /* Stringify pre-processor trick. */ #define XSTR(d) STR(d) diff --git a/src/lib/wallclock/include.am b/src/lib/wallclock/include.am @@ -7,7 +7,7 @@ endif src_lib_libtor_wallclock_a_SOURCES = \ src/lib/wallclock/approx_time.c \ - src/lib/wallclock/tm_cvt.c \ + src/lib/wallclock/time_to_tm.c \ src/lib/wallclock/tor_gettimeofday.c src_lib_libtor_wallclock_testing_a_SOURCES = \ @@ -18,5 +18,5 @@ src_lib_libtor_wallclock_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS) noinst_HEADERS += \ src/lib/wallclock/approx_time.h \ src/lib/wallclock/timeval.h \ - src/lib/wallclock/tm_cvt.h \ + src/lib/wallclock/time_to_tm.h \ src/lib/wallclock/tor_gettimeofday.h diff --git a/src/lib/wallclock/time_to_tm.c b/src/lib/wallclock/time_to_tm.c @@ -0,0 +1,200 @@ +/* Copyright (c) 2003-2004, Roger Dingledine + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file time_to_tm.c + * \brief Convert to struct tm, portably. + **/ + +#include "orconfig.h" +#include "lib/cc/torint.h" +#include "lib/cc/compat_compiler.h" +#include "lib/wallclock/time_to_tm.h" +#include "lib/string/printf.h" +#include "lib/err/torerr.h" + +#include <errno.h> +#include <time.h> +#include <string.h> +#include <stdlib.h> + +#if !defined(_WIN32) +/** Defined iff we need to add locks when defining fake versions of reentrant + * versions of time-related functions. */ +#define TIME_FNS_NEED_LOCKS +#endif + +/** Helper: Deal with confused or out-of-bounds values from localtime_r and + * friends. (On some platforms, they can give out-of-bounds values or can + * return NULL.) If <b>islocal</b>, this is a localtime result; otherwise + * it's from gmtime. The function returns <b>r</b>, when given <b>timep</b> + * as its input. If we need to store new results, store them in + * <b>resultbuf</b>. */ +static struct tm * +correct_tm(int islocal, const time_t *timep, struct tm *resultbuf, + struct tm *r, char **err_out) +{ + const char *outcome; + + if (PREDICT_LIKELY(r)) { + /* We can't strftime dates after 9999 CE, and we want to avoid dates + * before 1 CE (avoiding the year 0 issue and negative years). */ + if (r->tm_year > 8099) { + r->tm_year = 8099; + r->tm_mon = 11; + r->tm_mday = 31; + r->tm_yday = 364; + r->tm_wday = 6; + r->tm_hour = 23; + r->tm_min = 59; + r->tm_sec = 59; + } else if (r->tm_year < (1-1900)) { + r->tm_year = (1-1900); + r->tm_mon = 0; + r->tm_mday = 1; + r->tm_yday = 0; + r->tm_wday = 0; + r->tm_hour = 0; + r->tm_min = 0; + r->tm_sec = 0; + } + return r; + } + + /* If we get here, gmtime or localtime returned NULL. It might have done + * this because of overrun or underrun, or it might have done it because of + * some other weird issue. */ + if (timep) { + if (*timep < 0) { + r = resultbuf; + r->tm_year = 70; /* 1970 CE */ + r->tm_mon = 0; + r->tm_mday = 1; + r->tm_yday = 0; + r->tm_wday = 0; + r->tm_hour = 0; + r->tm_min = 0 ; + r->tm_sec = 0; + outcome = "Rounding up to 1970"; + goto done; + } else if (*timep >= INT32_MAX) { + /* Rounding down to INT32_MAX isn't so great, but keep in mind that we + * only do it if gmtime/localtime tells us NULL. */ + r = resultbuf; + r->tm_year = 137; /* 2037 CE */ + r->tm_mon = 11; + r->tm_mday = 31; + r->tm_yday = 364; + r->tm_wday = 6; + r->tm_hour = 23; + r->tm_min = 59; + r->tm_sec = 59; + outcome = "Rounding down to 2037"; + goto done; + } + } + + /* If we get here, then gmtime/localtime failed without getting an extreme + * value for *timep */ + /* LCOV_EXCL_START */ + r = resultbuf; + memset(resultbuf, 0, sizeof(struct tm)); + outcome="can't recover"; + /* LCOV_EXCL_STOP */ + done: + if (err_out) { + tor_asprintf(err_out, "%s(%"PRId64") failed with error %s: %s", + islocal?"localtime":"gmtime", + timep?((int64_t)*timep):0, + strerror(errno), + outcome); + } + return r; +} + +/** @{ */ +/** As localtime_r, but defined for platforms that don't have it: + * + * Convert *<b>timep</b> to a struct tm in local time, and store the value in + * *<b>result</b>. Return the result on success, or NULL on failure. + */ +#ifdef HAVE_LOCALTIME_R +struct tm * +tor_localtime_r_msg(const time_t *timep, struct tm *result, char **err_out) +{ + struct tm *r; + r = localtime_r(timep, result); + return correct_tm(1, timep, result, r, err_out); +} +#elif defined(TIME_FNS_NEED_LOCKS) +struct tm * +tor_localtime_r_msg(const time_t *timep, struct tm *result, char **err_out) +{ + struct tm *r; + static tor_mutex_t *m=NULL; + if (!m) { m=tor_mutex_new(); } + raw_assert(result); + tor_mutex_acquire(m); + r = localtime(timep); + if (r) + memcpy(result, r, sizeof(struct tm)); + tor_mutex_release(m); + return correct_tm(1, timep, result, r, err_out); +} +#else +struct tm * +tor_localtime_r_msg(const time_t *timep, struct tm *result, char **err_out) +{ + struct tm *r; + raw_assert(result); + r = localtime(timep); + if (r) + memcpy(result, r, sizeof(struct tm)); + return correct_tm(1, timep, result, r, err_out); +} +#endif /* defined(HAVE_LOCALTIME_R) || ... */ +/** @} */ + +/** @{ */ +/** As gmtime_r, but defined for platforms that don't have it: + * + * Convert *<b>timep</b> to a struct tm in UTC, and store the value in + * *<b>result</b>. Return the result on success, or NULL on failure. + */ +#ifdef HAVE_GMTIME_R +struct tm * +tor_gmtime_r_msg(const time_t *timep, struct tm *result, char **err_out) +{ + struct tm *r; + r = gmtime_r(timep, result); + return correct_tm(0, timep, result, r, err_out); +} +#elif defined(TIME_FNS_NEED_LOCKS) +struct tm * +tor_gmtime_r_msg(const time_t *timep, struct tm *result, char **err_out) +{ + struct tm *r; + static tor_mutex_t *m=NULL; + if (!m) { m=tor_mutex_new(); } + raw_assert(result); + tor_mutex_acquire(m); + r = gmtime(timep); + if (r) + memcpy(result, r, sizeof(struct tm)); + tor_mutex_release(m); + return correct_tm(0, timep, result, r, err_out); +} +#else +struct tm * +tor_gmtime_r_msg(const time_t *timep, struct tm *result, char **err_out) +{ + struct tm *r; + raw_assert(result); + r = gmtime(timep); + if (r) + memcpy(result, r, sizeof(struct tm)); + return correct_tm(0, timep, result, r, err_out); +} +#endif /* defined(HAVE_GMTIME_R) || ... */ diff --git a/src/lib/wallclock/time_to_tm.h b/src/lib/wallclock/time_to_tm.h @@ -0,0 +1,22 @@ +/* Copyright (c) 2003-2004, Roger Dingledine + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file time_to_tm.h + * \brief Header for time_to_tm.c + **/ + +#ifndef TOR_WALLCLOCK_TIME_TO_TM_H +#define TOR_WALLCLOCK_TIME_TO_TM_H + +#include <sys/types.h> + +struct tm; +struct tm *tor_localtime_r_msg(const time_t *timep, struct tm *result, + char **err_out); +struct tm *tor_gmtime_r_msg(const time_t *timep, struct tm *result, + char **err_out); + +#endif diff --git a/src/lib/wallclock/tm_cvt.c b/src/lib/wallclock/tm_cvt.c @@ -1,200 +0,0 @@ -/* Copyright (c) 2003-2004, Roger Dingledine - * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2018, The Tor Project, Inc. */ -/* See LICENSE for licensing information */ - -/** - * \file tm_cvt.c - * \brief Convert to and from struct tm, portably. - **/ - -#include "orconfig.h" -#include "lib/cc/torint.h" -#include "lib/cc/compat_compiler.h" -#include "lib/wallclock/tm_cvt.h" -#include "lib/string/printf.h" -#include "lib/err/torerr.h" - -#include <errno.h> -#include <time.h> -#include <string.h> -#include <stdlib.h> - -#if !defined(_WIN32) -/** Defined iff we need to add locks when defining fake versions of reentrant - * versions of time-related functions. */ -#define TIME_FNS_NEED_LOCKS -#endif - -/** Helper: Deal with confused or out-of-bounds values from localtime_r and - * friends. (On some platforms, they can give out-of-bounds values or can - * return NULL.) If <b>islocal</b>, this is a localtime result; otherwise - * it's from gmtime. The function returns <b>r</b>, when given <b>timep</b> - * as its input. If we need to store new results, store them in - * <b>resultbuf</b>. */ -static struct tm * -correct_tm(int islocal, const time_t *timep, struct tm *resultbuf, - struct tm *r, char **err_out) -{ - const char *outcome; - - if (PREDICT_LIKELY(r)) { - /* We can't strftime dates after 9999 CE, and we want to avoid dates - * before 1 CE (avoiding the year 0 issue and negative years). */ - if (r->tm_year > 8099) { - r->tm_year = 8099; - r->tm_mon = 11; - r->tm_mday = 31; - r->tm_yday = 364; - r->tm_wday = 6; - r->tm_hour = 23; - r->tm_min = 59; - r->tm_sec = 59; - } else if (r->tm_year < (1-1900)) { - r->tm_year = (1-1900); - r->tm_mon = 0; - r->tm_mday = 1; - r->tm_yday = 0; - r->tm_wday = 0; - r->tm_hour = 0; - r->tm_min = 0; - r->tm_sec = 0; - } - return r; - } - - /* If we get here, gmtime or localtime returned NULL. It might have done - * this because of overrun or underrun, or it might have done it because of - * some other weird issue. */ - if (timep) { - if (*timep < 0) { - r = resultbuf; - r->tm_year = 70; /* 1970 CE */ - r->tm_mon = 0; - r->tm_mday = 1; - r->tm_yday = 0; - r->tm_wday = 0; - r->tm_hour = 0; - r->tm_min = 0 ; - r->tm_sec = 0; - outcome = "Rounding up to 1970"; - goto done; - } else if (*timep >= INT32_MAX) { - /* Rounding down to INT32_MAX isn't so great, but keep in mind that we - * only do it if gmtime/localtime tells us NULL. */ - r = resultbuf; - r->tm_year = 137; /* 2037 CE */ - r->tm_mon = 11; - r->tm_mday = 31; - r->tm_yday = 364; - r->tm_wday = 6; - r->tm_hour = 23; - r->tm_min = 59; - r->tm_sec = 59; - outcome = "Rounding down to 2037"; - goto done; - } - } - - /* If we get here, then gmtime/localtime failed without getting an extreme - * value for *timep */ - /* LCOV_EXCL_START */ - r = resultbuf; - memset(resultbuf, 0, sizeof(struct tm)); - outcome="can't recover"; - /* LCOV_EXCL_STOP */ - done: - if (err_out) { - tor_asprintf(err_out, "%s(%"PRId64") failed with error %s: %s", - islocal?"localtime":"gmtime", - timep?((int64_t)*timep):0, - strerror(errno), - outcome); - } - return r; -} - -/** @{ */ -/** As localtime_r, but defined for platforms that don't have it: - * - * Convert *<b>timep</b> to a struct tm in local time, and store the value in - * *<b>result</b>. Return the result on success, or NULL on failure. - */ -#ifdef HAVE_LOCALTIME_R -struct tm * -tor_localtime_r_msg(const time_t *timep, struct tm *result, char **err_out) -{ - struct tm *r; - r = localtime_r(timep, result); - return correct_tm(1, timep, result, r, err_out); -} -#elif defined(TIME_FNS_NEED_LOCKS) -struct tm * -tor_localtime_r_msg(const time_t *timep, struct tm *result, char **err_out) -{ - struct tm *r; - static tor_mutex_t *m=NULL; - if (!m) { m=tor_mutex_new(); } - raw_assert(result); - tor_mutex_acquire(m); - r = localtime(timep); - if (r) - memcpy(result, r, sizeof(struct tm)); - tor_mutex_release(m); - return correct_tm(1, timep, result, r, err_out); -} -#else -struct tm * -tor_localtime_r_msg(const time_t *timep, struct tm *result, char **err_out) -{ - struct tm *r; - raw_assert(result); - r = localtime(timep); - if (r) - memcpy(result, r, sizeof(struct tm)); - return correct_tm(1, timep, result, r, err_out); -} -#endif /* defined(HAVE_LOCALTIME_R) || ... */ -/** @} */ - -/** @{ */ -/** As gmtime_r, but defined for platforms that don't have it: - * - * Convert *<b>timep</b> to a struct tm in UTC, and store the value in - * *<b>result</b>. Return the result on success, or NULL on failure. - */ -#ifdef HAVE_GMTIME_R -struct tm * -tor_gmtime_r_msg(const time_t *timep, struct tm *result, char **err_out) -{ - struct tm *r; - r = gmtime_r(timep, result); - return correct_tm(0, timep, result, r, err_out); -} -#elif defined(TIME_FNS_NEED_LOCKS) -struct tm * -tor_gmtime_r_msg(const time_t *timep, struct tm *result, char **err_out) -{ - struct tm *r; - static tor_mutex_t *m=NULL; - if (!m) { m=tor_mutex_new(); } - raw_assert(result); - tor_mutex_acquire(m); - r = gmtime(timep); - if (r) - memcpy(result, r, sizeof(struct tm)); - tor_mutex_release(m); - return correct_tm(0, timep, result, r, err_out); -} -#else -struct tm * -tor_gmtime_r_msg(const time_t *timep, struct tm *result, char **err_out) -{ - struct tm *r; - raw_assert(result); - r = gmtime(timep); - if (r) - memcpy(result, r, sizeof(struct tm)); - return correct_tm(0, timep, result, r, err_out); -} -#endif /* defined(HAVE_GMTIME_R) || ... */ diff --git a/src/lib/wallclock/tm_cvt.h b/src/lib/wallclock/tm_cvt.h @@ -1,22 +0,0 @@ -/* Copyright (c) 2003-2004, Roger Dingledine - * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2018, The Tor Project, Inc. */ -/* See LICENSE for licensing information */ - -/** - * \file tm_cvt.h - * \brief Header for tm_cvt.c - **/ - -#ifndef TOR_WALLCLOCK_TM_CVT_H -#define TOR_WALLCLOCK_TM_CVT_H - -#include <sys/types.h> - -struct tm; -struct tm *tor_localtime_r_msg(const time_t *timep, struct tm *result, - char **err_out); -struct tm *tor_gmtime_r_msg(const time_t *timep, struct tm *result, - char **err_out); - -#endif diff --git a/src/test/fuzz/fuzz_http.c b/src/test/fuzz/fuzz_http.c @@ -12,7 +12,7 @@ #include "app/config/config.h" #include "core/mainloop/connection.h" #include "feature/dircache/directory.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "feature/dircommon/dir_connection_st.h" diff --git a/src/test/fuzz/fuzz_http_connect.c b/src/test/fuzz/fuzz_http_connect.c @@ -13,7 +13,7 @@ #include "core/mainloop/connection.h" #include "core/or/connection_edge.h" #include "core/proto/proto_socks.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "core/or/entry_connection_st.h" #include "core/or/socks_request_st.h" diff --git a/src/test/log_test_helpers.c b/src/test/log_test_helpers.c @@ -1,7 +1,7 @@ /* Copyright (c) 2015-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ #define LOG_PRIVATE -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "test/log_test_helpers.h" /** diff --git a/src/test/test-memwipe.c b/src/test/test-memwipe.c @@ -5,7 +5,7 @@ #include "lib/crypt_ops/crypto_util.h" #include "lib/intmath/cmp.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include <string.h> #include <stdio.h> diff --git a/src/test/test_bt_cl.c b/src/test/test_bt_cl.c @@ -9,7 +9,7 @@ #undef TOR_COVERAGE #include "core/or/or.h" #include "lib/err/backtrace.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #ifdef HAVE_UNISTD_H #include <unistd.h> diff --git a/src/test/test_crypto_openssl.c b/src/test/test_crypto_openssl.c @@ -10,7 +10,7 @@ #include "lib/crypt_ops/compat_openssl.h" #include "lib/crypt_ops/crypto_rand.h" #include "lib/encoding/binascii.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "test/test.h" #include <openssl/evp.h> diff --git a/src/test/test_logging.c b/src/test/test_logging.c @@ -7,7 +7,7 @@ #include "core/or/or.h" #include "app/config/config.h" #include "lib/err/torerr.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "test/test.h" #include "lib/process/subprocess.h" diff --git a/src/test/test_status.c b/src/test/test_status.c @@ -12,7 +12,7 @@ #include <math.h> #include "core/or/or.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "tor_queue.h" #include "core/or/status.h" #include "core/or/circuitlist.h" diff --git a/src/test/test_tortls.c b/src/test/test_tortls.c @@ -31,7 +31,7 @@ DISABLE_GCC_WARNING(redundant-decls) ENABLE_GCC_WARNING(redundant-decls) #include "core/or/or.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "app/config/config.h" #include "lib/tls/tortls.h" #include "app/config/or_state_st.h" diff --git a/src/test/test_util_slow.c b/src/test/test_util_slow.c @@ -7,7 +7,7 @@ #define UTIL_PRIVATE #define SUBPROCESS_PRIVATE #include "lib/crypt_ops/crypto.h" -#include "lib/log/torlog.h" +#include "lib/log/log.h" #include "lib/process/subprocess.h" #include "lib/process/waitpid.h" #include "lib/string/printf.h" diff --git a/src/tools/tor-gencert.c b/src/tools/tor-gencert.c @@ -38,8 +38,8 @@ ENABLE_GCC_WARNING(redundant-decls) #include "lib/encoding/binascii.h" #include "lib/encoding/time_fmt.h" #include "lib/fs/files.h" -#include "lib/log/torlog.h" -#include "lib/malloc/util_malloc.h" +#include "lib/log/log.h" +#include "lib/malloc/malloc.h" #include "lib/net/address.h" #include "lib/net/inaddr.h" #include "lib/net/resolve.h" diff --git a/src/tools/tor-resolve.c b/src/tools/tor-resolve.c @@ -6,8 +6,8 @@ #include "orconfig.h" #include "lib/arch/bytes.h" -#include "lib/log/torlog.h" -#include "lib/malloc/util_malloc.h" +#include "lib/log/log.h" +#include "lib/malloc/malloc.h" #include "lib/net/address.h" #include "lib/net/resolve.h" #include "lib/net/socket.h" diff --git a/src/trunnel/trunnel-local.h b/src/trunnel/trunnel-local.h @@ -3,7 +3,7 @@ #define TRUNNEL_LOCAL_H_INCLUDED #include "lib/crypt_ops/crypto_util.h" -#include "lib/malloc/util_malloc.h" +#include "lib/malloc/malloc.h" #include "lib/log/util_bug.h" #define trunnel_malloc tor_malloc