mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-12-06 19:41:15 +01:00
56c0baa523
For everything we declare that starts with _, make it end with _ instead.
This is a machine-generated patch. To make it, start by getting the
list of reserved identifiers using:
git ls-tree -r --name-only HEAD | grep '\.[ch]$' | \
xargs ctags --c-kinds=defglmpstuvx -o - | grep '^_' | \
cut -f 1 | sort| uniq
You might need gnu ctags.
Then pipe the output through this script:
==============================
use strict;
BEGIN { print "#!/usr/bin/perl -w -i -p\n\n"; }
chomp;
next if (
/^__attribute__/ or
/^__func__/ or
/^_FILE_OFFSET_BITS/ or
/^_FORTIFY_SOURCE/ or
/^_GNU_SOURCE/ or
/^_WIN32/ or
/^_DARWIN_UNLIMITED/ or
/^_FILE_OFFSET_BITS/ or
/^_LARGEFILE64_SOURCE/ or
/^_LFS64_LARGEFILE/ or
/^__cdecl/ or
/^__attribute__/ or
/^__func__/ or
/^_WIN32_WINNT/);
my $ident = $_;
my $better = $ident;
$better =~ s/^_//;
$better = "${better}_";
print "s/(?<![A-Za-z0-9_])$ident(?![A-Za-z0-9_])/$better/g;\n";
==============================
Then run the resulting script on all the files you want to change.
(That is, all the C except that in src/ext.) The resulting script was:
==============================
s/(?<![A-Za-z0-9_])_address(?![A-Za-z0-9_])/address_/g;
s/(?<![A-Za-z0-9_])_aes_fill_buf(?![A-Za-z0-9_])/aes_fill_buf_/g;
s/(?<![A-Za-z0-9_])_AllowInvalid(?![A-Za-z0-9_])/AllowInvalid_/g;
s/(?<![A-Za-z0-9_])_AP_CONN_STATE_MAX(?![A-Za-z0-9_])/AP_CONN_STATE_MAX_/g;
s/(?<![A-Za-z0-9_])_AP_CONN_STATE_MIN(?![A-Za-z0-9_])/AP_CONN_STATE_MIN_/g;
s/(?<![A-Za-z0-9_])_assert_cache_ok(?![A-Za-z0-9_])/assert_cache_ok_/g;
s/(?<![A-Za-z0-9_])_A_UNKNOWN(?![A-Za-z0-9_])/A_UNKNOWN_/g;
s/(?<![A-Za-z0-9_])_base(?![A-Za-z0-9_])/base_/g;
s/(?<![A-Za-z0-9_])_BridgePassword_AuthDigest(?![A-Za-z0-9_])/BridgePassword_AuthDigest_/g;
s/(?<![A-Za-z0-9_])_buffer_stats_compare_entries(?![A-Za-z0-9_])/buffer_stats_compare_entries_/g;
s/(?<![A-Za-z0-9_])_chan_circid_entries_eq(?![A-Za-z0-9_])/chan_circid_entries_eq_/g;
s/(?<![A-Za-z0-9_])_chan_circid_entry_hash(?![A-Za-z0-9_])/chan_circid_entry_hash_/g;
s/(?<![A-Za-z0-9_])_check_no_tls_errors(?![A-Za-z0-9_])/check_no_tls_errors_/g;
s/(?<![A-Za-z0-9_])_c_hist_compare(?![A-Za-z0-9_])/c_hist_compare_/g;
s/(?<![A-Za-z0-9_])_circ(?![A-Za-z0-9_])/circ_/g;
s/(?<![A-Za-z0-9_])_circuit_get_global_list(?![A-Za-z0-9_])/circuit_get_global_list_/g;
s/(?<![A-Za-z0-9_])_circuit_mark_for_close(?![A-Za-z0-9_])/circuit_mark_for_close_/g;
s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_C_MAX(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_C_MAX_/g;
s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_MAX(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_MAX_/g;
s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_MIN(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_MIN_/g;
s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_OR_MAX(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_OR_MAX_/g;
s/(?<![A-Za-z0-9_])_CIRCUIT_PURPOSE_OR_MIN(?![A-Za-z0-9_])/CIRCUIT_PURPOSE_OR_MIN_/g;
s/(?<![A-Za-z0-9_])_cmp_int_strings(?![A-Za-z0-9_])/cmp_int_strings_/g;
s/(?<![A-Za-z0-9_])_compare_cached_resolves_by_expiry(?![A-Za-z0-9_])/compare_cached_resolves_by_expiry_/g;
s/(?<![A-Za-z0-9_])_compare_digests(?![A-Za-z0-9_])/compare_digests_/g;
s/(?<![A-Za-z0-9_])_compare_digests256(?![A-Za-z0-9_])/compare_digests256_/g;
s/(?<![A-Za-z0-9_])_compare_dir_src_ents_by_authority_id(?![A-Za-z0-9_])/compare_dir_src_ents_by_authority_id_/g;
s/(?<![A-Za-z0-9_])_compare_duration_idx(?![A-Za-z0-9_])/compare_duration_idx_/g;
s/(?<![A-Za-z0-9_])_compare_int(?![A-Za-z0-9_])/compare_int_/g;
s/(?<![A-Za-z0-9_])_compare_networkstatus_v2_published_on(?![A-Za-z0-9_])/compare_networkstatus_v2_published_on_/g;
s/(?<![A-Za-z0-9_])_compare_old_routers_by_identity(?![A-Za-z0-9_])/compare_old_routers_by_identity_/g;
s/(?<![A-Za-z0-9_])_compare_orports(?![A-Za-z0-9_])/compare_orports_/g;
s/(?<![A-Za-z0-9_])_compare_pairs(?![A-Za-z0-9_])/compare_pairs_/g;
s/(?<![A-Za-z0-9_])_compare_routerinfo_by_id_digest(?![A-Za-z0-9_])/compare_routerinfo_by_id_digest_/g;
s/(?<![A-Za-z0-9_])_compare_routerinfo_by_ip_and_bw(?![A-Za-z0-9_])/compare_routerinfo_by_ip_and_bw_/g;
s/(?<![A-Za-z0-9_])_compare_signed_descriptors_by_age(?![A-Za-z0-9_])/compare_signed_descriptors_by_age_/g;
s/(?<![A-Za-z0-9_])_compare_string_ptrs(?![A-Za-z0-9_])/compare_string_ptrs_/g;
s/(?<![A-Za-z0-9_])_compare_strings_for_pqueue(?![A-Za-z0-9_])/compare_strings_for_pqueue_/g;
s/(?<![A-Za-z0-9_])_compare_strs(?![A-Za-z0-9_])/compare_strs_/g;
s/(?<![A-Za-z0-9_])_compare_tor_version_str_ptr(?![A-Za-z0-9_])/compare_tor_version_str_ptr_/g;
s/(?<![A-Za-z0-9_])_compare_vote_rs(?![A-Za-z0-9_])/compare_vote_rs_/g;
s/(?<![A-Za-z0-9_])_compare_votes_by_authority_id(?![A-Za-z0-9_])/compare_votes_by_authority_id_/g;
s/(?<![A-Za-z0-9_])_compare_without_first_ch(?![A-Za-z0-9_])/compare_without_first_ch_/g;
s/(?<![A-Za-z0-9_])_connection_free(?![A-Za-z0-9_])/connection_free_/g;
s/(?<![A-Za-z0-9_])_connection_mark_and_flush(?![A-Za-z0-9_])/connection_mark_and_flush_/g;
s/(?<![A-Za-z0-9_])_connection_mark_for_close(?![A-Za-z0-9_])/connection_mark_for_close_/g;
s/(?<![A-Za-z0-9_])_connection_mark_unattached_ap(?![A-Za-z0-9_])/connection_mark_unattached_ap_/g;
s/(?<![A-Za-z0-9_])_connection_write_to_buf_impl(?![A-Za-z0-9_])/connection_write_to_buf_impl_/g;
s/(?<![A-Za-z0-9_])_ConnLimit(?![A-Za-z0-9_])/ConnLimit_/g;
s/(?<![A-Za-z0-9_])_CONN_TYPE_MAX(?![A-Za-z0-9_])/CONN_TYPE_MAX_/g;
s/(?<![A-Za-z0-9_])_CONN_TYPE_MIN(?![A-Za-z0-9_])/CONN_TYPE_MIN_/g;
s/(?<![A-Za-z0-9_])_CONTROL_CONN_STATE_MAX(?![A-Za-z0-9_])/CONTROL_CONN_STATE_MAX_/g;
s/(?<![A-Za-z0-9_])_CONTROL_CONN_STATE_MIN(?![A-Za-z0-9_])/CONTROL_CONN_STATE_MIN_/g;
s/(?<![A-Za-z0-9_])_CPUWORKER_STATE_MAX(?![A-Za-z0-9_])/CPUWORKER_STATE_MAX_/g;
s/(?<![A-Za-z0-9_])_CPUWORKER_STATE_MIN(?![A-Za-z0-9_])/CPUWORKER_STATE_MIN_/g;
s/(?<![A-Za-z0-9_])_crypto_dh_get_dh(?![A-Za-z0-9_])/crypto_dh_get_dh_/g;
s/(?<![A-Za-z0-9_])_crypto_global_initialized(?![A-Za-z0-9_])/crypto_global_initialized_/g;
s/(?<![A-Za-z0-9_])_crypto_new_pk_from_rsa(?![A-Za-z0-9_])/crypto_new_pk_from_rsa_/g;
s/(?<![A-Za-z0-9_])_crypto_pk_get_evp_pkey(?![A-Za-z0-9_])/crypto_pk_get_evp_pkey_/g;
s/(?<![A-Za-z0-9_])_crypto_pk_get_rsa(?![A-Za-z0-9_])/crypto_pk_get_rsa_/g;
s/(?<![A-Za-z0-9_])_DIR_CONN_STATE_MAX(?![A-Za-z0-9_])/DIR_CONN_STATE_MAX_/g;
s/(?<![A-Za-z0-9_])_DIR_CONN_STATE_MIN(?![A-Za-z0-9_])/DIR_CONN_STATE_MIN_/g;
s/(?<![A-Za-z0-9_])_DIR_PURPOSE_MAX(?![A-Za-z0-9_])/DIR_PURPOSE_MAX_/g;
s/(?<![A-Za-z0-9_])_DIR_PURPOSE_MIN(?![A-Za-z0-9_])/DIR_PURPOSE_MIN_/g;
s/(?<![A-Za-z0-9_])_dirreq_map_get(?![A-Za-z0-9_])/dirreq_map_get_/g;
s/(?<![A-Za-z0-9_])_dirreq_map_put(?![A-Za-z0-9_])/dirreq_map_put_/g;
s/(?<![A-Za-z0-9_])_dns_randfn(?![A-Za-z0-9_])/dns_randfn_/g;
s/(?<![A-Za-z0-9_])_dummy(?![A-Za-z0-9_])/dummy_/g;
s/(?<![A-Za-z0-9_])_edge(?![A-Za-z0-9_])/edge_/g;
s/(?<![A-Za-z0-9_])_END_CIRC_REASON_MAX(?![A-Za-z0-9_])/END_CIRC_REASON_MAX_/g;
s/(?<![A-Za-z0-9_])_END_CIRC_REASON_MIN(?![A-Za-z0-9_])/END_CIRC_REASON_MIN_/g;
s/(?<![A-Za-z0-9_])_EOF(?![A-Za-z0-9_])/EOF_/g;
s/(?<![A-Za-z0-9_])_ERR(?![A-Za-z0-9_])/ERR_/g;
s/(?<![A-Za-z0-9_])_escaped_val(?![A-Za-z0-9_])/escaped_val_/g;
s/(?<![A-Za-z0-9_])_evdns_log(?![A-Za-z0-9_])/evdns_log_/g;
s/(?<![A-Za-z0-9_])_evdns_nameserver_add_impl(?![A-Za-z0-9_])/evdns_nameserver_add_impl_/g;
s/(?<![A-Za-z0-9_])_EVENT_MAX(?![A-Za-z0-9_])/EVENT_MAX_/g;
s/(?<![A-Za-z0-9_])_EVENT_MIN(?![A-Za-z0-9_])/EVENT_MIN_/g;
s/(?<![A-Za-z0-9_])_ExcludeExitNodesUnion(?![A-Za-z0-9_])/ExcludeExitNodesUnion_/g;
s/(?<![A-Za-z0-9_])_EXIT_CONN_STATE_MAX(?![A-Za-z0-9_])/EXIT_CONN_STATE_MAX_/g;
s/(?<![A-Za-z0-9_])_EXIT_CONN_STATE_MIN(?![A-Za-z0-9_])/EXIT_CONN_STATE_MIN_/g;
s/(?<![A-Za-z0-9_])_EXIT_PURPOSE_MAX(?![A-Za-z0-9_])/EXIT_PURPOSE_MAX_/g;
s/(?<![A-Za-z0-9_])_EXIT_PURPOSE_MIN(?![A-Za-z0-9_])/EXIT_PURPOSE_MIN_/g;
s/(?<![A-Za-z0-9_])_extrainfo_free(?![A-Za-z0-9_])/extrainfo_free_/g;
s/(?<![A-Za-z0-9_])_find_by_keyword(?![A-Za-z0-9_])/find_by_keyword_/g;
s/(?<![A-Za-z0-9_])_free_cached_dir(?![A-Za-z0-9_])/free_cached_dir_/g;
s/(?<![A-Za-z0-9_])_free_cached_resolve(?![A-Za-z0-9_])/free_cached_resolve_/g;
s/(?<![A-Za-z0-9_])_free_duplicate_routerstatus_entry(?![A-Za-z0-9_])/free_duplicate_routerstatus_entry_/g;
s/(?<![A-Za-z0-9_])_free_link_history(?![A-Za-z0-9_])/free_link_history_/g;
s/(?<![A-Za-z0-9_])_geoip_compare_entries(?![A-Za-z0-9_])/geoip_compare_entries_/g;
s/(?<![A-Za-z0-9_])_geoip_compare_key_to_entry(?![A-Za-z0-9_])/geoip_compare_key_to_entry_/g;
s/(?<![A-Za-z0-9_])_hex_decode_digit(?![A-Za-z0-9_])/hex_decode_digit_/g;
s/(?<![A-Za-z0-9_])_idxplus1(?![A-Za-z0-9_])/idxplus1_/g;
s/(?<![A-Za-z0-9_])__libc_enable_secure(?![A-Za-z0-9_])/_libc_enable_secure_/g;
s/(?<![A-Za-z0-9_])_log_debug(?![A-Za-z0-9_])/log_debug_/g;
s/(?<![A-Za-z0-9_])_log_err(?![A-Za-z0-9_])/log_err_/g;
s/(?<![A-Za-z0-9_])_log_fn(?![A-Za-z0-9_])/log_fn_/g;
s/(?<![A-Za-z0-9_])_log_fn_function_name(?![A-Za-z0-9_])/log_fn_function_name_/g;
s/(?<![A-Za-z0-9_])_log_global_min_severity(?![A-Za-z0-9_])/log_global_min_severity_/g;
s/(?<![A-Za-z0-9_])_log_info(?![A-Za-z0-9_])/log_info_/g;
s/(?<![A-Za-z0-9_])_log_notice(?![A-Za-z0-9_])/log_notice_/g;
s/(?<![A-Za-z0-9_])_log_prefix(?![A-Za-z0-9_])/log_prefix_/g;
s/(?<![A-Za-z0-9_])_log_warn(?![A-Za-z0-9_])/log_warn_/g;
s/(?<![A-Za-z0-9_])_magic(?![A-Za-z0-9_])/magic_/g;
s/(?<![A-Za-z0-9_])_MALLOC_LOCK(?![A-Za-z0-9_])/MALLOC_LOCK_/g;
s/(?<![A-Za-z0-9_])_MALLOC_LOCK_INIT(?![A-Za-z0-9_])/MALLOC_LOCK_INIT_/g;
s/(?<![A-Za-z0-9_])_MALLOC_UNLOCK(?![A-Za-z0-9_])/MALLOC_UNLOCK_/g;
s/(?<![A-Za-z0-9_])_microdesc_eq(?![A-Za-z0-9_])/microdesc_eq_/g;
s/(?<![A-Za-z0-9_])_microdesc_hash(?![A-Za-z0-9_])/microdesc_hash_/g;
s/(?<![A-Za-z0-9_])_MIN_TOR_TLS_ERROR_VAL(?![A-Za-z0-9_])/MIN_TOR_TLS_ERROR_VAL_/g;
s/(?<![A-Za-z0-9_])_mm_free(?![A-Za-z0-9_])/mm_free_/g;
s/(?<![A-Za-z0-9_])_NIL(?![A-Za-z0-9_])/NIL_/g;
s/(?<![A-Za-z0-9_])_n_openssl_mutexes(?![A-Za-z0-9_])/n_openssl_mutexes_/g;
s/(?<![A-Za-z0-9_])_openssl_dynlock_create_cb(?![A-Za-z0-9_])/openssl_dynlock_create_cb_/g;
s/(?<![A-Za-z0-9_])_openssl_dynlock_destroy_cb(?![A-Za-z0-9_])/openssl_dynlock_destroy_cb_/g;
s/(?<![A-Za-z0-9_])_openssl_dynlock_lock_cb(?![A-Za-z0-9_])/openssl_dynlock_lock_cb_/g;
s/(?<![A-Za-z0-9_])_openssl_locking_cb(?![A-Za-z0-9_])/openssl_locking_cb_/g;
s/(?<![A-Za-z0-9_])_openssl_mutexes(?![A-Za-z0-9_])/openssl_mutexes_/g;
s/(?<![A-Za-z0-9_])_option_abbrevs(?![A-Za-z0-9_])/option_abbrevs_/g;
s/(?<![A-Za-z0-9_])_option_vars(?![A-Za-z0-9_])/option_vars_/g;
s/(?<![A-Za-z0-9_])_OR_CONN_STATE_MAX(?![A-Za-z0-9_])/OR_CONN_STATE_MAX_/g;
s/(?<![A-Za-z0-9_])_OR_CONN_STATE_MIN(?![A-Za-z0-9_])/OR_CONN_STATE_MIN_/g;
s/(?<![A-Za-z0-9_])_OutboundBindAddressIPv4(?![A-Za-z0-9_])/OutboundBindAddressIPv4_/g;
s/(?<![A-Za-z0-9_])_OutboundBindAddressIPv6(?![A-Za-z0-9_])/OutboundBindAddressIPv6_/g;
s/(?<![A-Za-z0-9_])_PDS_PREFER_TUNNELED_DIR_CONNS(?![A-Za-z0-9_])/PDS_PREFER_TUNNELED_DIR_CONNS_/g;
s/(?<![A-Za-z0-9_])_port(?![A-Za-z0-9_])/port_/g;
s/(?<![A-Za-z0-9_])__progname(?![A-Za-z0-9_])/_progname_/g;
s/(?<![A-Za-z0-9_])_PublishServerDescriptor(?![A-Za-z0-9_])/PublishServerDescriptor_/g;
s/(?<![A-Za-z0-9_])_remove_old_client_helper(?![A-Za-z0-9_])/remove_old_client_helper_/g;
s/(?<![A-Za-z0-9_])_rend_cache_entry_free(?![A-Za-z0-9_])/rend_cache_entry_free_/g;
s/(?<![A-Za-z0-9_])_routerlist_find_elt(?![A-Za-z0-9_])/routerlist_find_elt_/g;
s/(?<![A-Za-z0-9_])_SafeLogging(?![A-Za-z0-9_])/SafeLogging_/g;
s/(?<![A-Za-z0-9_])_SHORT_FILE_(?![A-Za-z0-9_])/SHORT_FILE__/g;
s/(?<![A-Za-z0-9_])_state_abbrevs(?![A-Za-z0-9_])/state_abbrevs_/g;
s/(?<![A-Za-z0-9_])_state_vars(?![A-Za-z0-9_])/state_vars_/g;
s/(?<![A-Za-z0-9_])_t(?![A-Za-z0-9_])/t_/g;
s/(?<![A-Za-z0-9_])_t32(?![A-Za-z0-9_])/t32_/g;
s/(?<![A-Za-z0-9_])_test_op_ip6(?![A-Za-z0-9_])/test_op_ip6_/g;
s/(?<![A-Za-z0-9_])_thread1_name(?![A-Za-z0-9_])/thread1_name_/g;
s/(?<![A-Za-z0-9_])_thread2_name(?![A-Za-z0-9_])/thread2_name_/g;
s/(?<![A-Za-z0-9_])_thread_test_func(?![A-Za-z0-9_])/thread_test_func_/g;
s/(?<![A-Za-z0-9_])_thread_test_mutex(?![A-Za-z0-9_])/thread_test_mutex_/g;
s/(?<![A-Za-z0-9_])_thread_test_start1(?![A-Za-z0-9_])/thread_test_start1_/g;
s/(?<![A-Za-z0-9_])_thread_test_start2(?![A-Za-z0-9_])/thread_test_start2_/g;
s/(?<![A-Za-z0-9_])_thread_test_strmap(?![A-Za-z0-9_])/thread_test_strmap_/g;
s/(?<![A-Za-z0-9_])_tor_calloc(?![A-Za-z0-9_])/tor_calloc_/g;
s/(?<![A-Za-z0-9_])_TOR_CHANNEL_INTERNAL(?![A-Za-z0-9_])/TOR_CHANNEL_INTERNAL_/g;
s/(?<![A-Za-z0-9_])_TOR_CIRCUITMUX_EWMA_C(?![A-Za-z0-9_])/TOR_CIRCUITMUX_EWMA_C_/g;
s/(?<![A-Za-z0-9_])_tor_free(?![A-Za-z0-9_])/tor_free_/g;
s/(?<![A-Za-z0-9_])_tor_malloc(?![A-Za-z0-9_])/tor_malloc_/g;
s/(?<![A-Za-z0-9_])_tor_malloc_zero(?![A-Za-z0-9_])/tor_malloc_zero_/g;
s/(?<![A-Za-z0-9_])_tor_memdup(?![A-Za-z0-9_])/tor_memdup_/g;
s/(?<![A-Za-z0-9_])_tor_realloc(?![A-Za-z0-9_])/tor_realloc_/g;
s/(?<![A-Za-z0-9_])_tor_strdup(?![A-Za-z0-9_])/tor_strdup_/g;
s/(?<![A-Za-z0-9_])_tor_strndup(?![A-Za-z0-9_])/tor_strndup_/g;
s/(?<![A-Za-z0-9_])_TOR_TLS_SYSCALL(?![A-Za-z0-9_])/TOR_TLS_SYSCALL_/g;
s/(?<![A-Za-z0-9_])_TOR_TLS_ZERORETURN(?![A-Za-z0-9_])/TOR_TLS_ZERORETURN_/g;
s/(?<![A-Za-z0-9_])__USE_ISOC99(?![A-Za-z0-9_])/_USE_ISOC99_/g;
s/(?<![A-Za-z0-9_])_UsingTestNetworkDefaults(?![A-Za-z0-9_])/UsingTestNetworkDefaults_/g;
s/(?<![A-Za-z0-9_])_val(?![A-Za-z0-9_])/val_/g;
s/(?<![A-Za-z0-9_])_void_for_alignment(?![A-Za-z0-9_])/void_for_alignment_/g;
==============================
504 lines
19 KiB
C
504 lines
19 KiB
C
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
|
* Copyright (c) 2007-2012, The Tor Project, Inc. */
|
|
/* See LICENSE for licensing information */
|
|
|
|
/**
|
|
* \file util.h
|
|
* \brief Headers for util.c
|
|
**/
|
|
|
|
#ifndef TOR_UTIL_H
|
|
#define TOR_UTIL_H
|
|
|
|
#include "orconfig.h"
|
|
#include "torint.h"
|
|
#include "compat.h"
|
|
#include "di_ops.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#ifdef _WIN32
|
|
/* for the correct alias to struct stat */
|
|
#include <sys/stat.h>
|
|
#endif
|
|
|
|
#ifndef O_BINARY
|
|
#define O_BINARY 0
|
|
#endif
|
|
#ifndef O_TEXT
|
|
#define O_TEXT 0
|
|
#endif
|
|
|
|
/* Replace assert() with a variant that sends failures to the log before
|
|
* calling assert() normally.
|
|
*/
|
|
#ifdef NDEBUG
|
|
/* Nobody should ever want to build with NDEBUG set. 99% of our asserts will
|
|
* be outside the critical path anyway, so it's silly to disable bug-checking
|
|
* throughout the entire program just because a few asserts are slowing you
|
|
* down. Profile, optimize the critical path, and keep debugging on.
|
|
*
|
|
* And I'm not just saying that because some of our asserts check
|
|
* security-critical properties.
|
|
*/
|
|
#error "Sorry; we don't support building with NDEBUG."
|
|
#endif
|
|
|
|
/** Like assert(3), but send assertion failures to the log as well as to
|
|
* stderr. */
|
|
#define tor_assert(expr) STMT_BEGIN \
|
|
if (PREDICT_UNLIKELY(!(expr))) { \
|
|
log_err(LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \
|
|
SHORT_FILE__, __LINE__, __func__, #expr); \
|
|
fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
|
|
SHORT_FILE__, __LINE__, __func__, #expr); \
|
|
abort(); \
|
|
} STMT_END
|
|
|
|
/* If we're building with dmalloc, we want all of our memory allocation
|
|
* functions to take an extra file/line pair of arguments. If not, not.
|
|
* We define DMALLOC_PARAMS to the extra parameters to insert in the
|
|
* function prototypes, and DMALLOC_ARGS to the extra arguments to add
|
|
* to calls. */
|
|
#ifdef USE_DMALLOC
|
|
#define DMALLOC_PARAMS , const char *file, const int line
|
|
#define DMALLOC_ARGS , SHORT_FILE__, __LINE__
|
|
#else
|
|
#define DMALLOC_PARAMS
|
|
#define DMALLOC_ARGS
|
|
#endif
|
|
|
|
/** Define this if you want Tor to crash when any problem comes up,
|
|
* so you can get a coredump and track things down. */
|
|
// #define tor_fragile_assert() tor_assert(0)
|
|
#define tor_fragile_assert()
|
|
|
|
/* Memory management */
|
|
void *tor_malloc_(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
|
|
void *tor_malloc_zero_(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
|
|
void *tor_calloc_(size_t nmemb, size_t size DMALLOC_PARAMS) ATTR_MALLOC;
|
|
void *tor_realloc_(void *ptr, size_t size DMALLOC_PARAMS);
|
|
char *tor_strdup_(const char *s DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1));
|
|
char *tor_strndup_(const char *s, size_t n DMALLOC_PARAMS)
|
|
ATTR_MALLOC ATTR_NONNULL((1));
|
|
void *tor_memdup_(const void *mem, size_t len DMALLOC_PARAMS)
|
|
ATTR_MALLOC ATTR_NONNULL((1));
|
|
void tor_free_(void *mem);
|
|
#ifdef USE_DMALLOC
|
|
extern int dmalloc_free(const char *file, const int line, void *pnt,
|
|
const int func_id);
|
|
#define tor_free(p) STMT_BEGIN \
|
|
if (PREDICT_LIKELY((p)!=NULL)) { \
|
|
dmalloc_free(SHORT_FILE__, __LINE__, (p), 0); \
|
|
(p)=NULL; \
|
|
} \
|
|
STMT_END
|
|
#else
|
|
/** Release memory allocated by tor_malloc, tor_realloc, tor_strdup, etc.
|
|
* Unlike the free() function, tor_free() will still work on NULL pointers,
|
|
* and it 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_().
|
|
*/
|
|
#define tor_free(p) STMT_BEGIN \
|
|
if (PREDICT_LIKELY((p)!=NULL)) { \
|
|
free(p); \
|
|
(p)=NULL; \
|
|
} \
|
|
STMT_END
|
|
#endif
|
|
|
|
#define tor_malloc(size) tor_malloc_(size DMALLOC_ARGS)
|
|
#define tor_malloc_zero(size) tor_malloc_zero_(size DMALLOC_ARGS)
|
|
#define tor_calloc(nmemb,size) tor_calloc_(nmemb, size DMALLOC_ARGS)
|
|
#define tor_malloc_roundup(szp) _tor_malloc_roundup(szp DMALLOC_ARGS)
|
|
#define tor_realloc(ptr, size) tor_realloc_(ptr, size DMALLOC_ARGS)
|
|
#define tor_strdup(s) tor_strdup_(s DMALLOC_ARGS)
|
|
#define tor_strndup(s, n) tor_strndup_(s, n DMALLOC_ARGS)
|
|
#define tor_memdup(s, n) tor_memdup_(s, n DMALLOC_ARGS)
|
|
|
|
void tor_log_mallinfo(int severity);
|
|
|
|
/** Return the offset of <b>member</b> within the type <b>tp</b>, in bytes */
|
|
#if defined(__GNUC__) && __GNUC__ > 3
|
|
#define STRUCT_OFFSET(tp, member) __builtin_offsetof(tp, member)
|
|
#else
|
|
#define STRUCT_OFFSET(tp, member) \
|
|
((off_t) (((char*)&((tp*)0)->member)-(char*)0))
|
|
#endif
|
|
|
|
/** Macro: yield a pointer to the field at position <b>off</b> within the
|
|
* structure <b>st</b>. Example:
|
|
* <pre>
|
|
* struct a { int foo; int bar; } x;
|
|
* off_t bar_offset = STRUCT_OFFSET(struct a, bar);
|
|
* int *bar_p = STRUCT_VAR_P(&x, bar_offset);
|
|
* *bar_p = 3;
|
|
* </pre>
|
|
*/
|
|
#define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) )
|
|
|
|
/** Macro: yield a pointer to an enclosing structure given a pointer to
|
|
* a substructure at offset <b>off</b>. Example:
|
|
* <pre>
|
|
* struct base { ... };
|
|
* struct subtype { int x; struct base b; } x;
|
|
* struct base *bp = &x.base;
|
|
* struct *sp = SUBTYPE_P(bp, struct subtype, b);
|
|
* </pre>
|
|
*/
|
|
#define SUBTYPE_P(p, subtype, basemember) \
|
|
((void*) ( ((char*)(p)) - STRUCT_OFFSET(subtype, basemember) ))
|
|
|
|
/* Logic */
|
|
/** Macro: true if two values have the same boolean value. */
|
|
#define bool_eq(a,b) (!(a)==!(b))
|
|
/** Macro: true if two values have different boolean values. */
|
|
#define bool_neq(a,b) (!(a)!=!(b))
|
|
|
|
/* Math functions */
|
|
double tor_mathlog(double d) ATTR_CONST;
|
|
long tor_lround(double d) ATTR_CONST;
|
|
int64_t tor_llround(double d) ATTR_CONST;
|
|
int tor_log2(uint64_t u64) ATTR_CONST;
|
|
uint64_t round_to_power_of_2(uint64_t u64);
|
|
unsigned round_to_next_multiple_of(unsigned number, unsigned divisor);
|
|
uint32_t round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor);
|
|
uint64_t round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor);
|
|
int n_bits_set_u8(uint8_t v);
|
|
|
|
/* Compute the CEIL of <b>a</b> divided by <b>b</b>, for nonnegative <b>a</b>
|
|
* and positive <b>b</b>. Works on integer types only. Not defined if a+b can
|
|
* overflow. */
|
|
#define CEIL_DIV(a,b) (((a)+(b)-1)/(b))
|
|
|
|
/* String manipulation */
|
|
|
|
/** Allowable characters in a hexadecimal string. */
|
|
#define HEX_CHARACTERS "0123456789ABCDEFabcdef"
|
|
void tor_strlower(char *s) ATTR_NONNULL((1));
|
|
void tor_strupper(char *s) ATTR_NONNULL((1));
|
|
int tor_strisprint(const char *s) ATTR_NONNULL((1));
|
|
int tor_strisnonupper(const char *s) ATTR_NONNULL((1));
|
|
int strcmp_opt(const char *s1, const char *s2);
|
|
int strcmpstart(const char *s1, const char *s2) ATTR_NONNULL((1,2));
|
|
int strcmp_len(const char *s1, const char *s2, size_t len) ATTR_NONNULL((1,2));
|
|
int strcasecmpstart(const char *s1, const char *s2) ATTR_NONNULL((1,2));
|
|
int strcmpend(const char *s1, const char *s2) ATTR_NONNULL((1,2));
|
|
int strcasecmpend(const char *s1, const char *s2) ATTR_NONNULL((1,2));
|
|
int fast_memcmpstart(const void *mem, size_t memlen, const char *prefix);
|
|
void tor_strclear(char *s);
|
|
|
|
void tor_strstrip(char *s, const char *strip) ATTR_NONNULL((1,2));
|
|
long tor_parse_long(const char *s, int base, long min,
|
|
long max, int *ok, char **next);
|
|
unsigned long tor_parse_ulong(const char *s, int base, unsigned long min,
|
|
unsigned long max, int *ok, char **next);
|
|
double tor_parse_double(const char *s, double min, double max, int *ok,
|
|
char **next);
|
|
uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
|
|
uint64_t max, int *ok, char **next);
|
|
const char *hex_str(const char *from, size_t fromlen) ATTR_NONNULL((1));
|
|
const char *eat_whitespace(const char *s);
|
|
const char *eat_whitespace_eos(const char *s, const char *eos);
|
|
const char *eat_whitespace_no_nl(const char *s);
|
|
const char *eat_whitespace_eos_no_nl(const char *s, const char *eos);
|
|
const char *find_whitespace(const char *s);
|
|
const char *find_whitespace_eos(const char *s, const char *eos);
|
|
const char *find_str_at_start_of_line(const char *haystack,
|
|
const char *needle);
|
|
int string_is_C_identifier(const char *string);
|
|
|
|
int tor_mem_is_zero(const char *mem, size_t len);
|
|
int tor_digest_is_zero(const char *digest);
|
|
int tor_digest256_is_zero(const char *digest);
|
|
char *esc_for_log(const char *string) ATTR_MALLOC;
|
|
const char *escaped(const char *string);
|
|
struct smartlist_t;
|
|
void wrap_string(struct smartlist_t *out, const char *string, size_t width,
|
|
const char *prefix0, const char *prefixRest);
|
|
int tor_vsscanf(const char *buf, const char *pattern, va_list ap)
|
|
#ifdef __GNUC__
|
|
__attribute__((format(scanf, 2, 0)))
|
|
#endif
|
|
;
|
|
int tor_sscanf(const char *buf, const char *pattern, ...)
|
|
#ifdef __GNUC__
|
|
__attribute__((format(scanf, 2, 3)))
|
|
#endif
|
|
;
|
|
|
|
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern, ...)
|
|
CHECK_PRINTF(2, 3);
|
|
void smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
|
|
va_list args)
|
|
CHECK_PRINTF(2, 0);
|
|
|
|
int hex_decode_digit(char c);
|
|
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen);
|
|
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen);
|
|
|
|
/* Time helpers */
|
|
double tv_to_double(const struct timeval *tv);
|
|
int64_t tv_to_msec(const struct timeval *tv);
|
|
int64_t tv_to_usec(const struct timeval *tv);
|
|
long tv_udiff(const struct timeval *start, const struct timeval *end);
|
|
long tv_mdiff(const struct timeval *start, const struct timeval *end);
|
|
int tor_timegm(const struct tm *tm, time_t *time_out);
|
|
#define RFC1123_TIME_LEN 29
|
|
void format_rfc1123_time(char *buf, time_t t);
|
|
int parse_rfc1123_time(const char *buf, time_t *t);
|
|
#define ISO_TIME_LEN 19
|
|
#define ISO_TIME_USEC_LEN (ISO_TIME_LEN+7)
|
|
void format_local_iso_time(char *buf, time_t t);
|
|
void format_iso_time(char *buf, time_t t);
|
|
void format_iso_time_nospace(char *buf, time_t t);
|
|
void format_iso_time_nospace_usec(char *buf, const struct timeval *tv);
|
|
int parse_iso_time(const char *buf, time_t *t);
|
|
int parse_http_time(const char *buf, struct tm *tm);
|
|
int format_time_interval(char *out, size_t out_len, long interval);
|
|
|
|
/* Cached time */
|
|
#ifdef TIME_IS_FAST
|
|
#define approx_time() time(NULL)
|
|
#define update_approx_time(t) STMT_NIL
|
|
#else
|
|
time_t approx_time(void);
|
|
void update_approx_time(time_t now);
|
|
#endif
|
|
|
|
/* Rate-limiter */
|
|
|
|
/** A ratelim_t remembers how often an event is occurring, and how often
|
|
* it's allowed to occur. Typical usage is something like:
|
|
*
|
|
<pre>
|
|
if (possibly_very_frequent_event()) {
|
|
const int INTERVAL = 300;
|
|
static ratelim_t warning_limit = RATELIM_INIT(INTERVAL);
|
|
char *m;
|
|
if ((m = rate_limit_log(&warning_limit, approx_time()))) {
|
|
log_warn(LD_GENERAL, "The event occurred!%s", m);
|
|
tor_free(m);
|
|
}
|
|
}
|
|
</pre>
|
|
*/
|
|
typedef struct ratelim_t {
|
|
int rate;
|
|
time_t last_allowed;
|
|
int n_calls_since_last_time;
|
|
} ratelim_t;
|
|
|
|
#define RATELIM_INIT(r) { (r), 0, 0 }
|
|
|
|
char *rate_limit_log(ratelim_t *lim, time_t now);
|
|
|
|
/* File helpers */
|
|
ssize_t write_all(tor_socket_t fd, const char *buf, size_t count,int isSocket);
|
|
ssize_t read_all(tor_socket_t fd, char *buf, size_t count, int isSocket);
|
|
|
|
/** Status of an I/O stream. */
|
|
enum stream_status {
|
|
IO_STREAM_OKAY,
|
|
IO_STREAM_EAGAIN,
|
|
IO_STREAM_TERM,
|
|
IO_STREAM_CLOSED
|
|
};
|
|
|
|
enum stream_status get_string_from_pipe(FILE *stream, char *buf, size_t count);
|
|
|
|
/** Return values from file_status(); see that function's documentation
|
|
* for details. */
|
|
typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR } file_status_t;
|
|
file_status_t file_status(const char *filename);
|
|
|
|
/** Possible behaviors for check_private_dir() on encountering a nonexistent
|
|
* directory; see that function's documentation for details. */
|
|
typedef unsigned int cpd_check_t;
|
|
#define CPD_NONE 0
|
|
#define CPD_CREATE 1
|
|
#define CPD_CHECK 2
|
|
#define CPD_GROUP_OK 4
|
|
#define CPD_CHECK_MODE_ONLY 8
|
|
int check_private_dir(const char *dirname, cpd_check_t check,
|
|
const char *effective_user);
|
|
#define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
|
|
#define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
|
|
#define OPEN_FLAGS_DONT_REPLACE (O_CREAT|O_EXCL|O_APPEND|O_WRONLY)
|
|
typedef struct open_file_t open_file_t;
|
|
int start_writing_to_file(const char *fname, int open_flags, int mode,
|
|
open_file_t **data_out);
|
|
FILE *start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
|
|
open_file_t **data_out);
|
|
FILE *fdopen_file(open_file_t *file_data);
|
|
int finish_writing_to_file(open_file_t *file_data);
|
|
int abort_writing_to_file(open_file_t *file_data);
|
|
int write_str_to_file(const char *fname, const char *str, int bin);
|
|
int write_bytes_to_file(const char *fname, const char *str, size_t len,
|
|
int bin);
|
|
/** An ad-hoc type to hold a string of characters and a count; used by
|
|
* write_chunks_to_file. */
|
|
typedef struct sized_chunk_t {
|
|
const char *bytes;
|
|
size_t len;
|
|
} sized_chunk_t;
|
|
int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
|
|
int bin);
|
|
int append_bytes_to_file(const char *fname, const char *str, size_t len,
|
|
int bin);
|
|
int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
|
|
int bin);
|
|
|
|
/** Flag for read_file_to_str: open the file in binary mode. */
|
|
#define RFTS_BIN 1
|
|
/** Flag for read_file_to_str: it's okay if the file doesn't exist. */
|
|
#define RFTS_IGNORE_MISSING 2
|
|
|
|
#ifndef _WIN32
|
|
struct stat;
|
|
#endif
|
|
char *read_file_to_str(const char *filename, int flags, struct stat *stat_out)
|
|
ATTR_MALLOC;
|
|
char *read_file_to_str_until_eof(int fd, size_t max_bytes_to_read,
|
|
size_t *sz_out)
|
|
ATTR_MALLOC;
|
|
const char *parse_config_line_from_str(const char *line,
|
|
char **key_out, char **value_out);
|
|
char *expand_filename(const char *filename);
|
|
struct smartlist_t *tor_listdir(const char *dirname);
|
|
int path_is_relative(const char *filename);
|
|
|
|
/* Process helpers */
|
|
void start_daemon(void);
|
|
void finish_daemon(const char *desired_cwd);
|
|
void write_pidfile(char *filename);
|
|
|
|
/* Port forwarding */
|
|
void tor_check_port_forwarding(const char *filename,
|
|
struct smartlist_t *ports_to_forward,
|
|
time_t now);
|
|
|
|
typedef struct process_handle_t process_handle_t;
|
|
typedef struct process_environment_t process_environment_t;
|
|
int tor_spawn_background(const char *const filename, const char **argv,
|
|
process_environment_t *env,
|
|
process_handle_t **process_handle_out);
|
|
|
|
#define SPAWN_ERROR_MESSAGE "ERR: Failed to spawn background process - code "
|
|
|
|
#ifdef _WIN32
|
|
HANDLE load_windows_system_library(const TCHAR *library_name);
|
|
#endif
|
|
|
|
int environment_variable_names_equal(const char *s1, const char *s2);
|
|
|
|
/* DOCDOC process_environment_t */
|
|
struct process_environment_t {
|
|
/** A pointer to a sorted empty-string-terminated sequence of
|
|
* NUL-terminated strings of the form "NAME=VALUE". */
|
|
char *windows_environment_block;
|
|
/** A pointer to a NULL-terminated array of pointers to
|
|
* NUL-terminated strings of the form "NAME=VALUE". */
|
|
char **unixoid_environment_block;
|
|
};
|
|
|
|
process_environment_t *process_environment_make(struct smartlist_t *env_vars);
|
|
void process_environment_free(process_environment_t *env);
|
|
|
|
struct smartlist_t *get_current_process_environment_variables(void);
|
|
|
|
void set_environment_variable_in_smartlist(struct smartlist_t *env_vars,
|
|
const char *new_var,
|
|
void (*free_old)(void*),
|
|
int free_p);
|
|
|
|
/* Values of process_handle_t.status. PROCESS_STATUS_NOTRUNNING must be
|
|
* 0 because tor_check_port_forwarding depends on this being the initial
|
|
* statue of the static instance of process_handle_t */
|
|
#define PROCESS_STATUS_NOTRUNNING 0
|
|
#define PROCESS_STATUS_RUNNING 1
|
|
#define PROCESS_STATUS_ERROR -1
|
|
|
|
#ifdef UTIL_PRIVATE
|
|
/** Structure to represent the state of a process with which Tor is
|
|
* communicating. The contents of this structure are private to util.c */
|
|
struct process_handle_t {
|
|
/** One of the PROCESS_STATUS_* values */
|
|
int status;
|
|
#ifdef _WIN32
|
|
HANDLE stdout_pipe;
|
|
HANDLE stderr_pipe;
|
|
PROCESS_INFORMATION pid;
|
|
#else
|
|
int stdout_pipe;
|
|
int stderr_pipe;
|
|
FILE *stdout_handle;
|
|
FILE *stderr_handle;
|
|
pid_t pid;
|
|
#endif // _WIN32
|
|
};
|
|
#endif
|
|
|
|
/* Return values of tor_get_exit_code() */
|
|
#define PROCESS_EXIT_RUNNING 1
|
|
#define PROCESS_EXIT_EXITED 0
|
|
#define PROCESS_EXIT_ERROR -1
|
|
int tor_get_exit_code(const process_handle_t *process_handle,
|
|
int block, int *exit_code);
|
|
int tor_split_lines(struct smartlist_t *sl, char *buf, int len);
|
|
#ifdef _WIN32
|
|
ssize_t tor_read_all_handle(HANDLE h, char *buf, size_t count,
|
|
const process_handle_t *process);
|
|
#else
|
|
ssize_t tor_read_all_handle(FILE *h, char *buf, size_t count,
|
|
const process_handle_t *process,
|
|
int *eof);
|
|
#endif
|
|
ssize_t tor_read_all_from_process_stdout(
|
|
const process_handle_t *process_handle, char *buf, size_t count);
|
|
ssize_t tor_read_all_from_process_stderr(
|
|
const process_handle_t *process_handle, char *buf, size_t count);
|
|
char *tor_join_win_cmdline(const char *argv[]);
|
|
|
|
int tor_process_get_pid(process_handle_t *process_handle);
|
|
#ifdef _WIN32
|
|
HANDLE tor_process_get_stdout_pipe(process_handle_t *process_handle);
|
|
#else
|
|
FILE *tor_process_get_stdout_pipe(process_handle_t *process_handle);
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
|
struct smartlist_t *
|
|
tor_get_lines_from_handle(HANDLE *handle,
|
|
enum stream_status *stream_status);
|
|
#else
|
|
struct smartlist_t *
|
|
tor_get_lines_from_handle(FILE *handle,
|
|
enum stream_status *stream_status);
|
|
#endif
|
|
|
|
int tor_terminate_process(process_handle_t *process_handle);
|
|
void tor_process_handle_destroy(process_handle_t *process_handle,
|
|
int also_terminate_process);
|
|
|
|
#ifdef UTIL_PRIVATE
|
|
/* Prototypes for private functions only used by util.c (and unit tests) */
|
|
|
|
int format_hex_number_for_helper_exit_status(unsigned int x, char *buf,
|
|
int max_len);
|
|
int format_helper_exit_status(unsigned char child_state,
|
|
int saved_errno, char *hex_errno);
|
|
|
|
/* Space for hex values of child state, a slash, saved_errno (with
|
|
leading minus) and newline (no null) */
|
|
#define HEX_ERRNO_SIZE (sizeof(char) * 2 + 1 + \
|
|
1 + sizeof(int) * 2 + 1)
|
|
#endif
|
|
|
|
const char *libor_get_digests(void);
|
|
|
|
#endif
|
|
|