2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2012-06-05 02:58:17 +02:00
|
|
|
* Copyright (c) 2007-2012, The Tor Project, Inc. */
|
2003-04-16 19:04:58 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2004-05-07 19:04:12 +02:00
|
|
|
/**
|
|
|
|
* \file util.h
|
|
|
|
* \brief Headers for util.c
|
2004-05-10 09:54:13 +02:00
|
|
|
**/
|
2004-05-07 19:04:12 +02:00
|
|
|
|
2008-12-29 03:21:02 +01:00
|
|
|
#ifndef _TOR_UTIL_H
|
|
|
|
#define _TOR_UTIL_H
|
2003-04-16 19:04:58 +02:00
|
|
|
|
2004-04-03 06:05:12 +02:00
|
|
|
#include "orconfig.h"
|
|
|
|
#include "torint.h"
|
2004-12-22 03:32:26 +01:00
|
|
|
#include "compat.h"
|
2011-05-10 22:19:58 +02:00
|
|
|
#include "di_ops.h"
|
2004-04-03 06:05:12 +02:00
|
|
|
#include <stdio.h>
|
2005-10-18 06:51:07 +02:00
|
|
|
#include <stdlib.h>
|
2012-06-05 17:06:26 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* for the correct alias to struct stat */
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2003-08-12 05:08:41 +02:00
|
|
|
|
2007-08-29 21:02:43 +02:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
#ifndef O_TEXT
|
|
|
|
#define O_TEXT 0
|
|
|
|
#endif
|
|
|
|
|
2005-04-26 20:52:16 +02:00
|
|
|
/* Replace assert() with a variant that sends failures to the log before
|
2004-05-01 22:46:28 +02:00
|
|
|
* calling assert() normally.
|
|
|
|
*/
|
2004-04-25 21:37:39 +02:00
|
|
|
#ifdef NDEBUG
|
2006-07-04 05:25:07 +02:00
|
|
|
/* Nobody should ever want to build with NDEBUG set. 99% of our asserts will
|
2009-05-27 23:55:51 +02:00
|
|
|
* be outside the critical path anyway, so it's silly to disable bug-checking
|
2004-11-29 08:22:57 +01:00
|
|
|
* 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."
|
2008-10-27 17:30:52 +01:00
|
|
|
#endif
|
|
|
|
|
2007-02-05 19:33:52 +01:00
|
|
|
/** Like assert(3), but send assertion failures to the log as well as to
|
|
|
|
* stderr. */
|
2007-06-17 20:22:39 +02:00
|
|
|
#define tor_assert(expr) STMT_BEGIN \
|
2008-12-11 21:23:46 +01:00
|
|
|
if (PREDICT_UNLIKELY(!(expr))) { \
|
2009-12-15 20:32:55 +01:00
|
|
|
log_err(LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \
|
2006-08-11 09:09:35 +02:00
|
|
|
_SHORT_FILE_, __LINE__, __func__, #expr); \
|
2005-10-25 09:05:03 +02:00
|
|
|
fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
|
2006-08-11 09:09:35 +02:00
|
|
|
_SHORT_FILE_, __LINE__, __func__, #expr); \
|
2005-11-01 07:12:51 +01:00
|
|
|
abort(); \
|
2007-06-17 20:22:39 +02:00
|
|
|
} STMT_END
|
2004-04-25 21:37:39 +02:00
|
|
|
|
2008-10-27 17:30:52 +01:00
|
|
|
/* 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. */
|
2005-09-30 00:59:17 +02:00
|
|
|
#ifdef USE_DMALLOC
|
2005-10-03 22:20:38 +02:00
|
|
|
#define DMALLOC_PARAMS , const char *file, const int line
|
|
|
|
#define DMALLOC_ARGS , _SHORT_FILE_, __LINE__
|
2005-09-30 00:59:17 +02:00
|
|
|
#else
|
|
|
|
#define DMALLOC_PARAMS
|
|
|
|
#define DMALLOC_ARGS
|
|
|
|
#endif
|
|
|
|
|
2005-04-26 20:52:16 +02:00
|
|
|
/** 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()
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/* Memory management */
|
2006-08-11 09:09:17 +02:00
|
|
|
void *_tor_malloc(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
|
|
|
|
void *_tor_malloc_zero(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
|
2007-12-26 01:12:01 +01:00
|
|
|
void *_tor_malloc_roundup(size_t *size DMALLOC_PARAMS) ATTR_MALLOC;
|
2012-02-13 05:09:56 +01:00
|
|
|
void *_tor_calloc(size_t nmemb, size_t size DMALLOC_PARAMS) ATTR_MALLOC;
|
2005-10-03 22:20:38 +02:00
|
|
|
void *_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS);
|
2006-08-11 09:09:17 +02:00
|
|
|
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));
|
2006-09-20 01:48:14 +02:00
|
|
|
void _tor_free(void *mem);
|
2005-09-30 03:39:24 +02:00
|
|
|
#ifdef USE_DMALLOC
|
|
|
|
extern int dmalloc_free(const char *file, const int line, void *pnt,
|
|
|
|
const int func_id);
|
2007-06-17 20:22:39 +02:00
|
|
|
#define tor_free(p) STMT_BEGIN \
|
2007-03-06 21:25:44 +01:00
|
|
|
if (PREDICT_LIKELY((p)!=NULL)) { \
|
2005-09-30 03:39:24 +02:00
|
|
|
dmalloc_free(_SHORT_FILE_, __LINE__, (p), 0); \
|
|
|
|
(p)=NULL; \
|
|
|
|
} \
|
2007-06-17 20:22:39 +02:00
|
|
|
STMT_END
|
2005-09-30 03:39:24 +02:00
|
|
|
#else
|
2008-10-27 17:30:52 +01:00
|
|
|
/** 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().
|
|
|
|
*/
|
2007-06-17 20:22:39 +02:00
|
|
|
#define tor_free(p) STMT_BEGIN \
|
2007-03-06 21:25:44 +01:00
|
|
|
if (PREDICT_LIKELY((p)!=NULL)) { \
|
|
|
|
free(p); \
|
|
|
|
(p)=NULL; \
|
|
|
|
} \
|
2007-06-17 20:22:39 +02:00
|
|
|
STMT_END
|
2005-09-30 03:39:24 +02:00
|
|
|
#endif
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2005-10-03 22:20:38 +02:00
|
|
|
#define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS)
|
|
|
|
#define tor_malloc_zero(size) _tor_malloc_zero(size DMALLOC_ARGS)
|
2012-02-13 05:09:56 +01:00
|
|
|
#define tor_calloc(nmemb,size) _tor_calloc(nmemb, size DMALLOC_ARGS)
|
2007-12-26 01:12:01 +01:00
|
|
|
#define tor_malloc_roundup(szp) _tor_malloc_roundup(szp DMALLOC_ARGS)
|
2005-10-03 22:20:38 +02:00
|
|
|
#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)
|
2006-06-18 09:32:31 +02:00
|
|
|
#define tor_memdup(s, n) _tor_memdup(s, n DMALLOC_ARGS)
|
2005-02-03 08:25:10 +01:00
|
|
|
|
2007-07-30 20:14:14 +02:00
|
|
|
void tor_log_mallinfo(int severity);
|
|
|
|
|
2006-07-23 07:32:35 +02:00
|
|
|
/** Return the offset of <b>member</b> within the type <b>tp</b>, in bytes */
|
2006-08-11 09:42:11 +02:00
|
|
|
#if defined(__GNUC__) && __GNUC__ > 3
|
2006-08-11 09:09:28 +02:00
|
|
|
#define STRUCT_OFFSET(tp, member) __builtin_offsetof(tp, member)
|
|
|
|
#else
|
|
|
|
#define STRUCT_OFFSET(tp, member) \
|
|
|
|
((off_t) (((char*)&((tp*)0)->member)-(char*)0))
|
|
|
|
#endif
|
2006-07-23 07:32:35 +02:00
|
|
|
|
2007-02-16 21:00:43 +01:00
|
|
|
/** 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) ))
|
|
|
|
|
2007-03-29 04:37:06 +02:00
|
|
|
/* 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))
|
|
|
|
|
2007-05-24 19:12:57 +02:00
|
|
|
/* Math functions */
|
2009-12-15 20:32:55 +01:00
|
|
|
double tor_mathlog(double d) ATTR_CONST;
|
|
|
|
long tor_lround(double d) ATTR_CONST;
|
2007-05-24 19:12:57 +02:00
|
|
|
int tor_log2(uint64_t u64) ATTR_CONST;
|
|
|
|
uint64_t round_to_power_of_2(uint64_t u64);
|
2009-07-13 22:43:06 +02:00
|
|
|
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);
|
2011-07-06 23:08:24 +02:00
|
|
|
int n_bits_set_u8(uint8_t v);
|
2007-05-24 19:12:57 +02:00
|
|
|
|
2010-09-15 04:32:36 +02:00
|
|
|
/* 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))
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/* String manipulation */
|
2007-02-02 21:06:43 +01:00
|
|
|
|
2007-02-05 19:33:52 +01:00
|
|
|
/** Allowable characters in a hexadecimal string. */
|
2004-11-01 21:41:47 +01:00
|
|
|
#define HEX_CHARACTERS "0123456789ABCDEFabcdef"
|
2006-08-11 09:09:17 +02:00
|
|
|
void tor_strlower(char *s) ATTR_NONNULL((1));
|
|
|
|
void tor_strupper(char *s) ATTR_NONNULL((1));
|
2011-11-25 17:56:00 +01:00
|
|
|
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);
|
2008-11-03 17:35:48 +01:00
|
|
|
|
2008-02-21 22:57:42 +01:00
|
|
|
void tor_strstrip(char *s, const char *strip) ATTR_NONNULL((1,2));
|
2004-10-12 21:32:41 +02:00
|
|
|
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);
|
2009-08-31 06:18:55 +02:00
|
|
|
double tor_parse_double(const char *s, double min, double max, int *ok,
|
|
|
|
char **next);
|
2004-11-05 18:54:50 +01:00
|
|
|
uint64_t tor_parse_uint64(const char *s, int base, uint64_t min,
|
|
|
|
uint64_t max, int *ok, char **next);
|
2006-08-11 09:09:17 +02:00
|
|
|
const char *hex_str(const char *from, size_t fromlen) ATTR_NONNULL((1));
|
2011-11-25 17:56:00 +01:00
|
|
|
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);
|
2011-09-11 23:34:11 +02:00
|
|
|
int string_is_C_identifier(const char *string);
|
|
|
|
|
2011-11-25 17:56:00 +01:00
|
|
|
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);
|
2006-08-11 09:09:17 +02:00
|
|
|
char *esc_for_log(const char *string) ATTR_MALLOC;
|
2006-03-05 10:50:26 +01:00
|
|
|
const char *escaped(const char *string);
|
2006-12-15 22:26:23 +01:00
|
|
|
struct smartlist_t;
|
|
|
|
void wrap_string(struct smartlist_t *out, const char *string, size_t width,
|
|
|
|
const char *prefix0, const char *prefixRest);
|
2012-05-30 18:14:38 +02:00
|
|
|
int tor_vsscanf(const char *buf, const char *pattern, va_list ap)
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__attribute__((format(scanf, 2, 0)))
|
|
|
|
#endif
|
|
|
|
;
|
2009-03-03 19:02:31 +01:00
|
|
|
int tor_sscanf(const char *buf, const char *pattern, ...)
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__attribute__((format(scanf, 2, 3)))
|
|
|
|
#endif
|
|
|
|
;
|
2003-11-12 20:34:19 +01:00
|
|
|
|
2012-01-11 19:15:52 +01:00
|
|
|
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern, ...)
|
2011-08-04 00:49:39 +02:00
|
|
|
CHECK_PRINTF(2, 3);
|
2012-01-11 19:15:52 +01:00
|
|
|
void smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
|
2012-06-13 16:51:56 +02:00
|
|
|
va_list args)
|
|
|
|
CHECK_PRINTF(2, 0);
|
2011-08-04 00:49:39 +02:00
|
|
|
|
2008-12-19 19:51:52 +01:00
|
|
|
int hex_decode_digit(char c);
|
2004-11-02 03:28:51 +01:00
|
|
|
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);
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2004-05-01 22:46:28 +02:00
|
|
|
/* Time helpers */
|
2009-09-21 03:03:39 +02:00
|
|
|
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);
|
2007-03-06 21:25:44 +01:00
|
|
|
long tv_udiff(const struct timeval *start, const struct timeval *end);
|
2009-07-27 16:23:53 +02:00
|
|
|
long tv_mdiff(const struct timeval *start, const struct timeval *end);
|
2004-04-03 06:08:54 +02:00
|
|
|
time_t tor_timegm(struct tm *tm);
|
2004-08-04 03:11:15 +02:00
|
|
|
#define RFC1123_TIME_LEN 29
|
2004-08-07 04:46:16 +02:00
|
|
|
void format_rfc1123_time(char *buf, time_t t);
|
|
|
|
int parse_rfc1123_time(const char *buf, time_t *t);
|
|
|
|
#define ISO_TIME_LEN 19
|
2012-01-11 16:48:05 +01:00
|
|
|
#define ISO_TIME_USEC_LEN (ISO_TIME_LEN+7)
|
2004-11-22 22:38:44 +01:00
|
|
|
void format_local_iso_time(char *buf, time_t t);
|
2004-08-07 04:46:16 +02:00
|
|
|
void format_iso_time(char *buf, time_t t);
|
2012-01-11 16:48:05 +01:00
|
|
|
void format_iso_time_nospace(char *buf, time_t t);
|
|
|
|
void format_iso_time_nospace_usec(char *buf, const struct timeval *tv);
|
2004-08-07 04:46:16 +02:00
|
|
|
int parse_iso_time(const char *buf, time_t *t);
|
2007-06-02 17:26:57 +02:00
|
|
|
int parse_http_time(const char *buf, struct tm *tm);
|
2007-10-30 16:17:07 +01:00
|
|
|
int format_time_interval(char *out, size_t out_len, long interval);
|
2008-12-18 18:19:04 +01:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
2010-08-18 21:55:49 +02:00
|
|
|
/* 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);
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/* File helpers */
|
2011-05-30 21:21:06 +02:00
|
|
|
ssize_t write_all(tor_socket_t fd, const char *buf, size_t count,int isSocket);
|
2011-05-23 06:17:48 +02:00
|
|
|
ssize_t read_all(tor_socket_t fd, char *buf, size_t count, int isSocket);
|
2003-08-21 01:05:22 +02:00
|
|
|
|
2011-07-13 18:59:52 +02:00
|
|
|
/** Status of an I/O stream. */
|
|
|
|
enum stream_status {
|
2011-07-18 02:35:29 +02:00
|
|
|
IO_STREAM_OKAY,
|
|
|
|
IO_STREAM_EAGAIN,
|
|
|
|
IO_STREAM_TERM,
|
|
|
|
IO_STREAM_CLOSED
|
2011-07-13 18:59:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
enum stream_status get_string_from_pipe(FILE *stream, char *buf, size_t count);
|
|
|
|
|
2007-02-05 19:33:52 +01:00
|
|
|
/** 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;
|
2003-09-26 20:27:35 +02:00
|
|
|
file_status_t file_status(const char *filename);
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2007-02-05 19:33:52 +01:00
|
|
|
/** Possible behaviors for check_private_dir() on encountering a nonexistent
|
|
|
|
* directory; see that function's documentation for details. */
|
2011-05-13 20:55:40 +02:00
|
|
|
typedef unsigned int cpd_check_t;
|
|
|
|
#define CPD_NONE 0
|
|
|
|
#define CPD_CREATE 1
|
|
|
|
#define CPD_CHECK 2
|
|
|
|
#define CPD_GROUP_OK 4
|
2011-05-13 21:40:03 +02:00
|
|
|
#define CPD_CHECK_MODE_ONLY 8
|
2011-06-14 18:18:32 +02:00
|
|
|
int check_private_dir(const char *dirname, cpd_check_t check,
|
|
|
|
const char *effective_user);
|
2007-08-29 21:02:43 +02:00
|
|
|
#define OPEN_FLAGS_REPLACE (O_WRONLY|O_CREAT|O_TRUNC)
|
|
|
|
#define OPEN_FLAGS_APPEND (O_WRONLY|O_CREAT|O_APPEND)
|
2011-11-26 18:56:49 +01:00
|
|
|
#define OPEN_FLAGS_DONT_REPLACE (O_CREAT|O_EXCL|O_APPEND|O_WRONLY)
|
2007-08-29 21:02:37 +02:00
|
|
|
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);
|
2007-08-29 21:02:43 +02:00
|
|
|
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);
|
2007-08-29 21:02:37 +02:00
|
|
|
int finish_writing_to_file(open_file_t *file_data);
|
|
|
|
int abort_writing_to_file(open_file_t *file_data);
|
2004-09-08 09:16:34 +02:00
|
|
|
int write_str_to_file(const char *fname, const char *str, int bin);
|
2004-11-03 20:02:48 +01:00
|
|
|
int write_bytes_to_file(const char *fname, const char *str, size_t len,
|
|
|
|
int bin);
|
2005-10-06 06:33:40 +02:00
|
|
|
/** An ad-hoc type to hold a string of characters and a count; used by
|
|
|
|
* write_chunks_to_file. */
|
2005-09-13 08:19:31 +02:00
|
|
|
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);
|
2011-11-26 18:56:49 +01:00
|
|
|
int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
|
|
|
|
int bin);
|
2005-09-13 08:19:31 +02:00
|
|
|
|
2007-02-12 22:39:44 +01:00
|
|
|
/** Flag for read_file_to_str: open the file in binary mode. */
|
|
|
|
#define RFTS_BIN 1
|
2007-02-24 02:12:53 +01:00
|
|
|
/** Flag for read_file_to_str: it's okay if the file doesn't exist. */
|
2007-02-12 22:39:44 +01:00
|
|
|
#define RFTS_IGNORE_MISSING 2
|
|
|
|
|
2012-06-05 17:06:26 +02:00
|
|
|
#ifndef _WIN32
|
2007-02-01 19:09:27 +01:00
|
|
|
struct stat;
|
2012-06-05 17:06:26 +02:00
|
|
|
#endif
|
2007-02-12 22:39:44 +01:00
|
|
|
char *read_file_to_str(const char *filename, int flags, struct stat *stat_out)
|
2006-10-20 01:05:02 +02:00
|
|
|
ATTR_MALLOC;
|
2008-01-02 07:59:12 +01:00
|
|
|
const char *parse_config_line_from_str(const char *line,
|
|
|
|
char **key_out, char **value_out);
|
2004-07-15 04:00:43 +02:00
|
|
|
char *expand_filename(const char *filename);
|
2005-09-03 06:40:05 +02:00
|
|
|
struct smartlist_t *tor_listdir(const char *dirname);
|
2011-11-25 17:56:00 +01:00
|
|
|
int path_is_relative(const char *filename);
|
2003-08-12 09:43:15 +02:00
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
/* Process helpers */
|
2005-04-06 21:07:38 +02:00
|
|
|
void start_daemon(void);
|
|
|
|
void finish_daemon(const char *desired_cwd);
|
2004-11-01 21:41:47 +01:00
|
|
|
void write_pidfile(char *filename);
|
2003-08-14 19:13:52 +02:00
|
|
|
|
2010-06-16 20:47:06 +02:00
|
|
|
/* Port forwarding */
|
|
|
|
void tor_check_port_forwarding(const char *filename,
|
|
|
|
int dir_port, int or_port, time_t now);
|
|
|
|
|
2011-11-25 22:47:25 +01:00
|
|
|
typedef struct process_handle_t process_handle_t;
|
2012-02-13 09:59:49 +01:00
|
|
|
typedef struct process_environment_t process_environment_t;
|
2011-10-07 22:05:13 +02:00
|
|
|
int tor_spawn_background(const char *const filename, const char **argv,
|
2012-02-13 09:59:49 +01:00
|
|
|
process_environment_t *env,
|
2011-11-25 22:47:25 +01:00
|
|
|
process_handle_t **process_handle_out);
|
2011-10-07 22:05:13 +02:00
|
|
|
|
2011-08-15 17:26:03 +02:00
|
|
|
#define SPAWN_ERROR_MESSAGE "ERR: Failed to spawn background process - code "
|
2011-07-13 18:59:52 +02:00
|
|
|
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2010-09-21 19:07:11 +02:00
|
|
|
HANDLE load_windows_system_library(const TCHAR *library_name);
|
|
|
|
#endif
|
|
|
|
|
2012-02-13 05:14:48 +01:00
|
|
|
int environment_variable_names_equal(const char *s1, const char *s2);
|
|
|
|
|
2012-06-05 01:51:00 +02:00
|
|
|
/* DOCDOC process_environment_t */
|
2012-02-13 05:14:48 +01:00
|
|
|
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
|
2012-02-13 09:59:49 +01:00
|
|
|
* NUL-terminated strings of the form "NAME=VALUE". */
|
|
|
|
char **unixoid_environment_block;
|
2012-02-13 05:14:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
process_environment_t *process_environment_make(struct smartlist_t *env_vars);
|
|
|
|
void process_environment_free(process_environment_t *env);
|
|
|
|
|
2012-02-13 05:34:52 +01:00
|
|
|
struct smartlist_t *get_current_process_environment_variables(void);
|
|
|
|
|
2012-02-13 06:17:11 +01:00
|
|
|
void set_environment_variable_in_smartlist(struct smartlist_t *env_vars,
|
|
|
|
const char *new_var,
|
|
|
|
void (*free_old)(void*),
|
|
|
|
int free_p);
|
|
|
|
|
2011-08-29 01:30:18 +02:00
|
|
|
/* 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
|
2011-11-25 22:47:25 +01:00
|
|
|
|
|
|
|
#ifdef UTIL_PRIVATE
|
2012-06-05 00:50:13 +02:00
|
|
|
/** Structure to represent the state of a process with which Tor is
|
|
|
|
* communicating. The contents of this structure are private to util.c */
|
2011-11-25 22:47:25 +01:00
|
|
|
struct process_handle_t {
|
2012-06-05 00:50:13 +02:00
|
|
|
/** One of the PROCESS_STATUS_* values */
|
2011-08-29 01:30:18 +02:00
|
|
|
int status;
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2011-07-21 17:34:48 +02:00
|
|
|
HANDLE stdout_pipe;
|
|
|
|
HANDLE stderr_pipe;
|
2011-07-21 20:26:19 +02:00
|
|
|
PROCESS_INFORMATION pid;
|
2011-07-21 17:34:48 +02:00
|
|
|
#else
|
|
|
|
int stdout_pipe;
|
|
|
|
int stderr_pipe;
|
2011-08-18 19:41:23 +02:00
|
|
|
FILE *stdout_handle;
|
|
|
|
FILE *stderr_handle;
|
2011-08-24 22:33:53 +02:00
|
|
|
pid_t pid;
|
2012-01-31 16:59:42 +01:00
|
|
|
#endif // _WIN32
|
2011-10-07 22:05:13 +02:00
|
|
|
};
|
2011-11-25 22:47:25 +01:00
|
|
|
#endif
|
2011-08-29 01:30:18 +02:00
|
|
|
|
|
|
|
/* Return values of tor_get_exit_code() */
|
|
|
|
#define PROCESS_EXIT_RUNNING 1
|
|
|
|
#define PROCESS_EXIT_EXITED 0
|
|
|
|
#define PROCESS_EXIT_ERROR -1
|
2011-11-25 22:47:25 +01:00
|
|
|
int tor_get_exit_code(const process_handle_t *process_handle,
|
2011-08-22 20:43:38 +02:00
|
|
|
int block, int *exit_code);
|
2011-08-30 15:55:51 +02:00
|
|
|
int tor_split_lines(struct smartlist_t *sl, char *buf, int len);
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2011-08-24 22:34:13 +02:00
|
|
|
ssize_t tor_read_all_handle(HANDLE h, char *buf, size_t count,
|
2011-09-01 02:06:12 +02:00
|
|
|
const process_handle_t *process);
|
|
|
|
#else
|
|
|
|
ssize_t tor_read_all_handle(FILE *h, char *buf, size_t count,
|
2011-09-01 14:09:38 +02:00
|
|
|
const process_handle_t *process,
|
|
|
|
int *eof);
|
2011-08-23 02:09:24 +02:00
|
|
|
#endif
|
2011-09-01 02:06:12 +02:00
|
|
|
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);
|
2011-08-30 22:00:08 +02:00
|
|
|
char *tor_join_win_cmdline(const char *argv[]);
|
2011-10-07 22:05:13 +02:00
|
|
|
|
2011-10-24 16:01:24 +02:00
|
|
|
int tor_process_get_pid(process_handle_t *process_handle);
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2011-11-25 22:47:25 +01:00
|
|
|
HANDLE tor_process_get_stdout_pipe(process_handle_t *process_handle);
|
|
|
|
#else
|
|
|
|
FILE *tor_process_get_stdout_pipe(process_handle_t *process_handle);
|
|
|
|
#endif
|
2011-10-24 16:01:24 +02:00
|
|
|
|
2011-10-24 15:55:53 +02:00
|
|
|
int tor_terminate_process(process_handle_t *process_handle);
|
2011-10-24 16:04:31 +02:00
|
|
|
void tor_process_handle_destroy(process_handle_t *process_handle,
|
|
|
|
int also_terminate_process);
|
2011-10-24 15:55:53 +02:00
|
|
|
|
2011-10-17 22:46:44 +02:00
|
|
|
#ifdef UTIL_PRIVATE
|
|
|
|
/* Prototypes for private functions only used by util.c (and unit tests) */
|
|
|
|
|
2010-06-16 20:47:06 +02:00
|
|
|
void 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
|
|
|
|
|
2009-01-04 03:43:05 +01:00
|
|
|
const char *libor_get_digests(void);
|
|
|
|
|
2003-04-16 19:04:58 +02:00
|
|
|
#endif
|
2005-06-09 21:03:31 +02:00
|
|
|
|