18 #include "testsupport.h" 43 #define DMALLOC_PARAMS , const char *file, const int line 44 #define DMALLOC_ARGS , SHORT_FILE__, __LINE__ 46 #define DMALLOC_PARAMS 51 void *
tor_malloc_(
size_t size DMALLOC_PARAMS) ATTR_MALLOC;
53 void *
tor_calloc_(
size_t nmemb,
size_t size DMALLOC_PARAMS) ATTR_MALLOC;
54 void *
tor_realloc_(
void *ptr,
size_t size DMALLOC_PARAMS);
56 char *
tor_strdup_(
const char *s DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1));
57 char *
tor_strndup_(
const char *s,
size_t n DMALLOC_PARAMS)
58 ATTR_MALLOC ATTR_NONNULL((1));
59 void *
tor_memdup_(
const void *mem,
size_t len DMALLOC_PARAMS)
60 ATTR_MALLOC ATTR_NONNULL((1));
62 ATTR_MALLOC ATTR_NONNULL((1));
67 extern int dmalloc_free(
const char *file,
const int line,
void *pnt,
69 #define tor_free(p) STMT_BEGIN \ 70 if (PREDICT_LIKELY((p)!=NULL)) { \ 71 dmalloc_free(SHORT_FILE__, __LINE__, (p), 0); \ 89 #define tor_free(p) STMT_BEGIN \ 90 typeof(&(p)) tor_free__tmpvar = &(p); \ 91 raw_free(*tor_free__tmpvar); \ 92 *tor_free__tmpvar=NULL; \ 95 #define tor_free(p) STMT_BEGIN \ 102 #define tor_malloc(size) tor_malloc_(size DMALLOC_ARGS) 103 #define tor_malloc_zero(size) tor_malloc_zero_(size DMALLOC_ARGS) 104 #define tor_calloc(nmemb,size) tor_calloc_(nmemb, size DMALLOC_ARGS) 105 #define tor_realloc(ptr, size) tor_realloc_(ptr, size DMALLOC_ARGS) 106 #define tor_reallocarray(ptr, sz1, sz2) \ 107 tor_reallocarray_((ptr), (sz1), (sz2) DMALLOC_ARGS) 108 #define tor_strdup(s) tor_strdup_(s DMALLOC_ARGS) 109 #define tor_strndup(s, n) tor_strndup_(s, n DMALLOC_ARGS) 110 #define tor_memdup(s, n) tor_memdup_(s, n DMALLOC_ARGS) 111 #define tor_memdup_nulterm(s, n) tor_memdup_nulterm_(s, n DMALLOC_ARGS) 116 #define raw_malloc malloc 117 #define raw_realloc realloc 118 #define raw_free free 119 #define raw_strdup strdup 126 #define FREE_AND_NULL(typename, freefn, var) \ 129 typename **tmp__free__ptr ## freefn = &(var); \ 130 freefn(*tmp__free__ptr ## freefn); \ 131 (*tmp__free__ptr ## freefn) = NULL; \ 143 #define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) ) 154 #define SUBTYPE_P(p, subtype, basemember) \ 155 ((void*) ( ((char*)(p)) - offsetof(subtype, basemember) )) 159 #define bool_eq(a,b) (!(a)==!(b)) 161 #define bool_neq(a,b) (!(a)!=!(b)) 167 int tor_log2(uint64_t u64) ATTR_CONST;
177 void simplify_fraction64(uint64_t *numer, uint64_t *denom);
179 uint32_t tor_add_u32_nowrap(uint32_t a, uint32_t b);
184 #define CEIL_DIV(a,b) (((a)+((b)-1))/(b)) 192 #define CLAMP(min,v,max) \ 193 ( ((v) < (min)) ? (min) : \ 194 ((v) > (max)) ? (max) : \ 200 #define HEX_CHARACTERS "0123456789ABCDEFabcdef" 206 int strcmp_opt(
const char *s1,
const char *s2);
207 int strcmpstart(
const char *s1,
const char *s2) ATTR_NONNULL((1,2));
208 int strcmp_len(
const char *s1,
const char *s2,
size_t len) ATTR_NONNULL((1,2));
209 int strcasecmpstart(
const char *s1,
const char *s2) ATTR_NONNULL((1,2));
210 int strcmpend(
const char *s1,
const char *s2) ATTR_NONNULL((1,2));
211 int strcasecmpend(
const char *s1,
const char *s2) ATTR_NONNULL((1,2));
214 void tor_strstrip(
char *s,
const char *strip) ATTR_NONNULL((1,2));
216 long max,
int *ok,
char **next);
218 unsigned long max,
int *ok,
char **next);
222 uint64_t max,
int *ok,
char **next);
223 const char *
hex_str(
const char *from,
size_t fromlen) ATTR_NONNULL((1));
244 const char *
escaped(
const char *
string);
247 const char *chars_to_escape);
250 int tor_vsscanf(
const char *buf,
const char *pattern, va_list ap) \
252 int tor_sscanf(
const char *buf,
const char *pattern, ...)
266 int tor_timegm(const struct tm *tm, time_t *time_out);
267 #define RFC1123_TIME_LEN 29 270 #define ISO_TIME_LEN 19 271 #define ISO_TIME_USEC_LEN (ISO_TIME_LEN+7) 277 int parse_iso_time_(
const char *cp, time_t *t,
int strict,
int nospace);
285 #define approx_time() time(NULL) 286 #define update_approx_time(t) STMT_NIL 321 int n_calls_since_last_time;
324 #define RATELIM_INIT(r) { (r), 0, 0 } 325 #define RATELIM_TOOMANY (16*1000*1000) 330 ssize_t
write_all(tor_socket_t fd,
const char *buf,
size_t count,
int isSocket);
331 ssize_t
read_all(tor_socket_t fd,
char *buf,
size_t count,
int isSocket);
345 MOCK_DECL(
int,tor_unlink,(
const char *pathname));
349 typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR, FN_EMPTY }
file_status_t;
356 #define CPD_CREATE (1u << 0) 357 #define CPD_CHECK (1u << 1) 358 #define CPD_GROUP_OK (1u << 2) 359 #define CPD_GROUP_READ (1u << 3) 360 #define CPD_CHECK_MODE_ONLY (1u << 4) 361 #define CPD_RELAX_DIRMODE_CHECK (1u << 5) 363 (
const char *dirname, cpd_check_t check,
364 const char *effective_user));
366 #define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC) 367 #define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND) 368 #define OPEN_FLAGS_DONT_REPLACE (O_CREAT|O_EXCL|O_APPEND|O_WRONLY) 378 write_str_to_file,(
const char *fname,
const char *str,
int bin));
380 write_bytes_to_file,(
const char *fname,
const char *str,
size_t len,
389 int bin,
int no_tempfile);
398 #define RFTS_IGNORE_MISSING 2 403 MOCK_DECL_ATTR(
char *, read_file_to_str,
404 (
const char *filename,
int flags,
struct stat *stat_out),
409 const char *
unescape_string(
const char *s,
char **result,
size_t *size_out);
422 typedef struct process_handle_t process_handle_t;
426 process_handle_t **process_handle_out);
428 #define SPAWN_ERROR_MESSAGE "ERR: Failed to spawn background process - code " 431 HANDLE load_windows_system_library(
const TCHAR *library_name);
448 #define process_environment_free(env) \ 449 FREE_AND_NULL(process_environment_t, process_environment_free_, (env)) 455 void (*free_old)(
void*),
459 #define PROCESS_STATUS_NOTRUNNING 0 460 #define PROCESS_STATUS_RUNNING 1 461 #define PROCESS_STATUS_ERROR -1 467 struct process_handle_t {
474 PROCESS_INFORMATION pid;
485 int waitpid_exit_status;
491 #define PROCESS_EXIT_RUNNING 1 492 #define PROCESS_EXIT_EXITED 0 493 #define PROCESS_EXIT_ERROR -1 495 int block,
int *exit_code);
499 const process_handle_t *process);
502 const process_handle_t *process,
506 const process_handle_t *process_handle,
char *buf,
size_t count);
508 const process_handle_t *process_handle,
char *buf,
size_t count);
513 HANDLE tor_process_get_stdout_pipe(process_handle_t *process_handle);
515 int tor_process_get_stdout_pipe(process_handle_t *process_handle);
520 tor_get_lines_from_handle,(HANDLE *handle,
521 enum stream_status *stream_status));
524 tor_get_lines_from_handle,(
int fd,
525 enum stream_status *stream_status));
532 tor_process_handle_destroy,(process_handle_t *process_handle,
533 int also_terminate_process));
540 #define TOR_WEAK_RNG_INIT {383745623} 541 #define TOR_WEAK_RANDOM_MAX (INT_MAX) 547 #define tor_weak_random_one_in_n(rng, n) (0==tor_weak_random_range((rng),(n))) 557 int saved_errno,
char *hex_errno);
561 #define HEX_ERRNO_SIZE (sizeof(char) * 2 + 1 + \ 562 1 + sizeof(int) * 2 + 1) 569 #define ARRAY_LENGTH(x) ((sizeof(x)) / sizeof(x[0])) uint64_t round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor)
Definition: util.c:511
int write_bytes_to_new_file(const char *fname, const char *str, size_t len, int bin)
Definition: util.c:2824
int parse_iso_time(const char *buf, time_t *t)
Definition: util.c:1928
void set_environment_variable_in_smartlist(struct smartlist_t *env_vars, const char *new_var, void(*free_old)(void *), int free_p)
Definition: util.c:4906
int format_time_interval(char *out, size_t out_len, long interval)
Definition: util.c:2017
int tor_strisnonupper(const char *s) ATTR_NONNULL((1))
Definition: util.c:710
char * windows_environment_block
Definition: util.h:440
int strcmp_opt(const char *s1, const char *s2)
Definition: util.c:736
char ** unixoid_environment_block
Definition: util.h:443
Definition: util_process.c:38
int64_t sample_laplace_distribution(double mu, double b, double p)
Definition: util.c:526
char * rate_limit_log(ratelim_t *lim, time_t now)
Definition: util.c:2115
int start_writing_to_file(const char *fname, int open_flags, int mode, open_file_t **data_out)
Definition: util.c:2577
void tor_free_(void *mem)
Definition: util.c:343
double tor_parse_double(const char *s, double min, double max, int *ok, char **next)
Definition: util.c:1264
const char * find_str_at_start_of_line(const char *haystack, const char *needle)
Definition: util.c:954
int64_t tor_llround(double d) ATTR_CONST
Definition: util.c:411
int write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin, int no_tempfile)
Definition: util.c:2774
uint64_t round_to_power_of_2(uint64_t u64)
Definition: util.c:457
int tor_strisprint(const char *s) ATTR_NONNULL((1))
Definition: util.c:697
void tor_strstrip(char *s, const char *strip)
Definition: util.c:644
int format_hex_number_sigsafe(unsigned long x, char *buf, int max_len)
Definition: util.c:3976
const char * unescape_string(const char *s, char **result, size_t *size_out)
Definition: util.c:3004
int parse_http_time(const char *buf, struct tm *tm)
Definition: util.c:1945
long tv_udiff(const struct timeval *start, const struct timeval *end)
Definition: util.c:1497
void update_approx_time(time_t now)
Definition: util.c:2079
void finish_daemon(const char *desired_cwd)
Definition: util.c:3709
void * tor_memdup_nulterm_(const void *mem, size_t len DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1))
Definition: util.c:329
enum stream_status get_string_from_pipe(int fd, char *buf, size_t count)
Definition: util.c:5228
int string_is_valid_nonrfc_hostname(const char *string)
Definition: util.c:1124
stream_status
Definition: util.h:334
int n_bits_set_u8(uint8_t v)
Definition: util.c:613
const char * hex_str(const char *from, size_t fromlen) ATTR_NONNULL((1))
Definition: util.c:663
const char * eat_whitespace_eos(const char *s, const char *eos)
Definition: util.c:858
void format_local_iso_time_nospace(char *buf, time_t t)
Definition: util.c:1840
void tor_strlower(char *s) ATTR_NONNULL((1))
Definition: util.c:675
MOCK_DECL(int, router_have_minimum_dir_info,(void))
process_environment_t * process_environment_make(struct smartlist_t *env_vars)
Definition: util.c:4801
uint32_t round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor)
Definition: util.c:496
int tor_digest_is_zero(const char *digest)
Definition: util.c:1024
Definition: container.h:18
int strcmpstart(const char *s1, const char *s2)
Definition: util.c:754
Header file to define uint32_t and friends.
int parse_iso_time_(const char *cp, time_t *t, int strict, int nospace)
Definition: util.c:1872
int string_is_C_identifier(const char *string)
Definition: util.c:977
void format_local_iso_time(char *buf, time_t t)
Definition: util.c:1821
long tv_mdiff(const struct timeval *start, const struct timeval *end)
Definition: util.c:1546
char * tor_strdup_(const char *s DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1))
Definition: util.c:272
int tor_process_get_pid(process_handle_t *process_handle)
Definition: util.c:4138
ssize_t tor_read_all_from_process_stdout(const process_handle_t *process_handle, char *buf, size_t count)
Definition: util.c:5038
int32_t tor_weak_random(tor_weak_rng_t *weak_rng)
Definition: util.c:5264
void format_iso_time_nospace_usec(char *buf, const struct timeval *tv)
Definition: util.c:1859
int write_pidfile(const char *filename)
Definition: util.c:3771
int int void void void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
Definition: util.c:3537
unsigned long tor_parse_ulong(const char *s, int base, unsigned long min, unsigned long max, int *ok, char **next)
Definition: util.c:1245
unsigned int cpd_check_t
Definition: util.h:354
uint64_t tor_htonll(uint64_t a)
Definition: util.c:5360
void tor_disable_spawning_background_processes(void)
Definition: util.c:4228
int tor_digest256_is_zero(const char *digest)
Definition: util.c:1182
unsigned round_to_next_multiple_of(unsigned number, unsigned divisor)
Definition: util.c:482
void * tor_memdup_(const void *mem, size_t len DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1))
Definition: util.c:316
void * tor_malloc_zero_(size_t size DMALLOC_PARAMS) ATTR_MALLOC
Definition: util.c:170
char * tor_strndup_(const char *s, size_t n DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1))
Definition: util.c:298
void * tor_malloc_(size_t size DMALLOC_PARAMS) ATTR_MALLOC
Definition: util.c:134
struct sized_chunk_t sized_chunk_t
int tor_spawn_background(const char *const filename, const char **argv, process_environment_t *env, process_handle_t **process_handle_out)
Definition: util.c:4252
int int int int int int fast_memcmpstart(const void *mem, size_t memlen, const char *prefix)
Definition: util.c:818
int append_bytes_to_file(const char *fname, const char *str, size_t len, int bin)
Definition: util.c:2814
file_status_t file_status(const char *filename)
Definition: util.c:2237
const char * stream_status_to_string(enum stream_status stream_status)
Definition: util.c:5119
const char * find_whitespace_eos(const char *s, const char *eos)
Definition: util.c:929
int string_is_valid_ipv6_address(const char *string)
Definition: util.c:1080
ssize_t write_all(tor_socket_t fd, const char *buf, size_t count, int isSocket)
Definition: util.c:2145
uint64_t tor_parse_uint64(const char *s, int base, uint64_t min, uint64_t max, int *ok, char **next)
Definition: util.c:1277
int tor_sscanf(const char *buf, const char *pattern,...)
Definition: util.c:3501
FILE * start_writing_to_stdio_file(const char *fname, int open_flags, int mode, open_file_t **data_out)
Definition: util.c:2658
ssize_t tor_read_all_from_process_stderr(const process_handle_t *process_handle, char *buf, size_t count)
Definition: util.c:5052
int strcasecmpend(const char *s1, const char *s2)
Definition: util.c:802
struct smartlist_t * get_current_process_environment_variables(void)
Definition: util.c:4889
const char * eat_whitespace(const char *s)
Definition: util.c:831
int tor_timegm(const struct tm *tm, time_t *time_out)
Definition: util.c:1632
long tor_lround(double d) ATTR_CONST
Definition: util.c:396
char * esc_for_log_len(const char *chars, size_t n) ATTR_MALLOC
Definition: util.c:1396
const char * eat_whitespace_no_nl(const char *s)
Definition: util.c:886
const char * find_whitespace(const char *s)
Definition: util.c:907
void * tor_reallocarray_(void *ptr, size_t sz1, size_t sz2 DMALLOC_PARAMS)
Definition: util.c:258
void start_daemon(void)
Definition: util.c:3648
Definition: compat_time.h:33
int32_t tor_weak_random_range(tor_weak_rng_t *rng, int32_t top)
Definition: util.c:5280
void tor_log_mallinfo(int severity)
Definition: util.c:352
int64_t add_laplace_noise(int64_t signal, double random, double delta_f, double epsilon)
Definition: util.c:551
int parse_iso_time_nospace(const char *cp, time_t *t)
Definition: util.c:1937
struct ratelim_t ratelim_t
void * tor_calloc_(size_t nmemb, size_t size DMALLOC_PARAMS) ATTR_MALLOC
Definition: util.c:214
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: util.c:3514
FILE * fdopen_file(open_file_t *file_data)
Definition: util.c:2641
int tor_log2(uint64_t u64) ATTR_CONST
Definition: util.c:424
int tor_vsscanf(const char *buf, const char *pattern, va_list ap)
Definition: util.c:3371
int strcmpend(const char *s1, const char *s2)
Definition: util.c:789
int tor_strisspace(const char *s)
Definition: util.c:723
void long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next)
Definition: util.c:1226
int string_is_valid_dest(const char *string)
Definition: util.c:1091
const char * eat_whitespace_eos_no_nl(const char *s, const char *eos)
Definition: util.c:896
STATIC int format_helper_exit_status(unsigned char child_state, int saved_errno, char *hex_errno)
Definition: util.c:4005
int string_is_valid_ipv4_address(const char *string)
Definition: util.c:1069
char * tor_join_win_cmdline(const char *argv[])
Definition: util.c:3884
const char * escaped(const char *string)
Definition: util.c:1412
void tor_strupper(char *s) ATTR_NONNULL((1))
Definition: util.c:686
int string_is_key_value(int severity, const char *string)
Definition: util.c:1036
int strcasecmpstart(const char *s1, const char *s2)
Definition: util.c:779
int finish_writing_to_file(open_file_t *file_data)
Definition: util.c:2725
int environment_variable_names_equal(const char *s1, const char *s2)
Definition: util.c:4770
ssize_t tor_read_all_handle(int fd, char *buf, size_t count, const process_handle_t *process, int *eof)
Definition: util.c:4997
uint64_t tor_ntohll(uint64_t a)
Definition: util.c:5374
char * expand_filename(const char *filename)
Definition: util.c:3152
int parse_rfc1123_time(const char *buf, time_t *t)
Definition: util.c:1742
int tor_split_lines(struct smartlist_t *sl, char *buf, int len)
Definition: util.c:5070
void process_environment_free_(process_environment_t *env)
Definition: util.c:4782
void format_iso_time_nospace(char *buf, time_t t)
Definition: util.c:1849
int tor_mem_is_zero(const char *mem, size_t len)
Definition: util.c:1002
char * read_file_to_str_until_eof(int fd, size_t max_bytes_to_read, size_t *sz_out) ATTR_MALLOC
Definition: util.c:2840
char * tor_escape_str_for_pt_args(const char *string, const char *chars_to_escape)
Definition: util.c:1428
void smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern, va_list args)
Definition: util.c:3524
int64_t tv_to_msec(const struct timeval *tv)
Definition: util.c:1604
int format_dec_number_sigsafe(unsigned long x, char *buf, int max_len)
Definition: util.c:3983
int strcmp_len(const char *s1, const char *s2, size_t s1_len)
Definition: util.c:765
time_t approx_time(void)
Definition: util.c:2070
int path_is_relative(const char *filename)
Definition: util.c:3614
int abort_writing_to_file(open_file_t *file_data)
Definition: util.c:2733
void tor_init_weak_random(tor_weak_rng_t *weak_rng, unsigned seed)
Definition: util.c:5254
char * esc_for_log(const char *string) ATTR_MALLOC
Definition: util.c:1316
int tor_get_exit_code(process_handle_t *process_handle, int block, int *exit_code)
Definition: util.c:4678
void format_iso_time(char *buf, time_t t)
Definition: util.c:1831
ssize_t read_all(tor_socket_t fd, char *buf, size_t count, int isSocket)
Definition: util.c:2169
Macros to manage assertions, fatal and non-fatal.
char * get_unquoted_path(const char *path)
Definition: util.c:3118
file_status_t
Definition: util.h:349
int size_mul_check(const size_t x, const size_t y)
Definition: util.c:193
double tor_mathlog(double d) ATTR_CONST
Definition: util.c:387
int64_t clamp_double_to_int64(double number)
Definition: util.c:5298
void format_rfc1123_time(char *buf, time_t t)
Definition: util.c:1718
void * tor_realloc_(void *ptr, size_t size DMALLOC_PARAMS)
Definition: util.c:225
int tor_terminate_process(process_handle_t *process_handle)
Definition: util.c:4115