1 /* Copyright (c) 2003, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2013, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
8 * \brief Common functions for strings, IO, network, data structures,
12 /* This is required on rh7 to make strptime not complain.
25 #include "container.h"
40 /* math.h needs this on Linux */
42 #define _USE_ISOC99_ 1
51 #ifdef HAVE_NETINET_IN_H
52 #include <netinet/in.h>
54 #ifdef HAVE_ARPA_INET_H
55 #include <arpa/inet.h>
60 #ifdef HAVE_SYS_SOCKET_H
61 #include <sys/socket.h>
63 #ifdef HAVE_SYS_TIME_H
69 #ifdef HAVE_SYS_STAT_H
72 #ifdef HAVE_SYS_FCNTL_H
73 #include <sys/fcntl.h>
78 #ifdef HAVE_MALLOC_MALLOC_H
79 #include <malloc/malloc.h>
82 #if !defined(OPENBSD) && !defined(__FreeBSD__)
83 /* OpenBSD has a malloc.h, but for our purposes, it only exists in order to
84 * scold us for being so stupid as to autodetect its presence. To be fair,
85 * they've done this since 1996, when autoconf was only 5 years old. */
89 #ifdef HAVE_MALLOC_NP_H
90 #include <malloc_np.h>
92 #ifdef HAVE_SYS_WAIT_H
102 /* Macro to pass the extra dmalloc args to another function. */
103 #define DMALLOC_FN_ARGS , file, line
105 #if defined(HAVE_DMALLOC_STRDUP)
106 /* the dmalloc_strdup should be fine as defined */
107 #elif defined(HAVE_DMALLOC_STRNDUP)
108 #define dmalloc_strdup(file, line, string, xalloc_b) \
109 dmalloc_strndup(file, line, (string), -1, xalloc_b)
111 #error "No dmalloc_strdup or equivalent"
114 #else /* not using dmalloc */
116 #define DMALLOC_FN_ARGS
119 /** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to
120 * result. On error, log and terminate the process. (Same as malloc(size),
121 * but never returns NULL.)
123 * <b>file</b> and <b>line</b> are used if dmalloc is enabled, and
127 tor_malloc_(size_t size DMALLOC_PARAMS
)
131 tor_assert(size
< SIZE_T_CEILING
);
133 #ifndef MALLOC_ZERO_WORKS
134 /* Some libc mallocs don't work when size==0. Override them. */
141 result
= dmalloc_malloc(file
, line
, size
, DMALLOC_FUNC_MALLOC
, 0, 0);
143 result
= malloc(size
);
146 if (PREDICT_UNLIKELY(result
== NULL
)) {
147 log_err(LD_MM
,"Out of memory on malloc(). Dying.");
148 /* If these functions die within a worker process, they won't call
149 * spawn_exit, but that's ok, since the parent will run out of memory soon
156 /** Allocate a chunk of <b>size</b> bytes of memory, fill the memory with
157 * zero bytes, and return a pointer to the result. Log and terminate
158 * the process on error. (Same as calloc(size,1), but never returns NULL.)
161 tor_malloc_zero_(size_t size DMALLOC_PARAMS
)
163 /* You may ask yourself, "wouldn't it be smart to use calloc instead of
164 * malloc+memset? Perhaps libc's calloc knows some nifty optimization trick
165 * we don't!" Indeed it does, but its optimizations are only a big win when
166 * we're allocating something very big (it knows if it just got the memory
167 * from the OS in a pre-zeroed state). We don't want to use tor_malloc_zero
168 * for big stuff, so we don't bother with calloc. */
169 void *result
= tor_malloc_(size DMALLOC_FN_ARGS
);
170 memset(result
, 0, size
);
174 /** Allocate a chunk of <b>nmemb</b>*<b>size</b> bytes of memory, fill
175 * the memory with zero bytes, and return a pointer to the result.
176 * Log and terminate the process on error. (Same as
177 * calloc(<b>nmemb</b>,<b>size</b>), but never returns NULL.)
179 * XXXX This implementation probably asserts in cases where it could
180 * work, because it only tries dividing SIZE_MAX by size (according to
181 * the calloc(3) man page, the size of an element of the nmemb-element
182 * array to be allocated), not by nmemb (which could in theory be
183 * smaller than size). Don't do that then.
186 tor_calloc_(size_t nmemb
, size_t size DMALLOC_PARAMS
)
188 /* You may ask yourself, "wouldn't it be smart to use calloc instead of
189 * malloc+memset? Perhaps libc's calloc knows some nifty optimization trick
190 * we don't!" Indeed it does, but its optimizations are only a big win when
191 * we're allocating something very big (it knows if it just got the memory
192 * from the OS in a pre-zeroed state). We don't want to use tor_malloc_zero
193 * for big stuff, so we don't bother with calloc. */
195 size_t max_nmemb
= (size
== 0) ? SIZE_MAX
: SIZE_MAX
/size
;
197 tor_assert(nmemb
< max_nmemb
);
199 result
= tor_malloc_zero_((nmemb
* size
) DMALLOC_FN_ARGS
);
203 /** Change the size of the memory block pointed to by <b>ptr</b> to <b>size</b>
204 * bytes long; return the new memory block. On error, log and
205 * terminate. (Like realloc(ptr,size), but never returns NULL.)
208 tor_realloc_(void *ptr
, size_t size DMALLOC_PARAMS
)
212 tor_assert(size
< SIZE_T_CEILING
);
215 result
= dmalloc_realloc(file
, line
, ptr
, size
, DMALLOC_FUNC_REALLOC
, 0);
217 result
= realloc(ptr
, size
);
220 if (PREDICT_UNLIKELY(result
== NULL
)) {
221 log_err(LD_MM
,"Out of memory on realloc(). Dying.");
227 /** Return a newly allocated copy of the NUL-terminated string s. On
228 * error, log and terminate. (Like strdup(s), but never returns
232 tor_strdup_(const char *s DMALLOC_PARAMS
)
238 dup
= dmalloc_strdup(file
, line
, s
, 0);
242 if (PREDICT_UNLIKELY(dup
== NULL
)) {
243 log_err(LD_MM
,"Out of memory on strdup(). Dying.");
249 /** Allocate and return a new string containing the first <b>n</b>
250 * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b>
251 * characters, only the first <b>n</b> are copied. The result is
252 * always NUL-terminated. (Like strndup(s,n), but never returns
256 tor_strndup_(const char *s
, size_t n DMALLOC_PARAMS
)
260 tor_assert(n
< SIZE_T_CEILING
);
261 dup
= tor_malloc_((n
+1) DMALLOC_FN_ARGS
);
262 /* Performance note: Ordinarily we prefer strlcpy to strncpy. But
263 * this function gets called a whole lot, and platform strncpy is
264 * much faster than strlcpy when strlen(s) is much longer than n.
271 /** Allocate a chunk of <b>len</b> bytes, with the same contents as the
272 * <b>len</b> bytes starting at <b>mem</b>. */
274 tor_memdup_(const void *mem
, size_t len DMALLOC_PARAMS
)
277 tor_assert(len
< SIZE_T_CEILING
);
279 dup
= tor_malloc_(len DMALLOC_FN_ARGS
);
280 memcpy(dup
, mem
, len
);
284 /** Helper for places that need to take a function pointer to the right
285 * spelling of "free()". */
292 /** Call the platform malloc info function, and dump the results to the log at
293 * level <b>severity</b>. If no such function exists, do nothing. */
295 tor_log_mallinfo(int severity
)
299 memset(&mi
, 0, sizeof(mi
));
301 tor_log(severity
, LD_MM
,
302 "mallinfo() said: arena=%d, ordblks=%d, smblks=%d, hblks=%d, "
303 "hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, "
305 mi
.arena
, mi
.ordblks
, mi
.smblks
, mi
.hblks
,
306 mi
.hblkhd
, mi
.usmblks
, mi
.fsmblks
, mi
.uordblks
, mi
.fordblks
,
312 dmalloc_log_changed(0, /* Since the program started. */
313 1, /* Log info about non-freed pointers. */
314 0, /* Do not log info about freed pointers. */
315 0 /* Do not log individual pointers. */
325 * Returns the natural logarithm of d base e. We defined this wrapper here so
326 * to avoid conflicts with old versions of tor_log(), which were named log().
329 tor_mathlog(double d
)
334 /** Return the long integer closest to <b>d</b>. We define this wrapper
335 * here so that not all users of math.h need to use the right incantations
336 * to get the c99 functions. */
340 #if defined(HAVE_LROUND)
342 #elif defined(HAVE_RINT)
343 return (long)rint(d
);
345 return (long)(d
> 0 ? d
+ 0.5 : ceil(d
- 0.5));
349 /** Return the 64-bit integer closest to d. We define this wrapper here so
350 * that not all users of math.h need to use the right incantations to get the
353 tor_llround(double d
)
355 #if defined(HAVE_LLROUND)
356 return (int64_t)llround(d
);
357 #elif defined(HAVE_RINT)
358 return (int64_t)rint(d
);
360 return (int64_t)(d
> 0 ? d
+ 0.5 : ceil(d
- 0.5));
364 /** Returns floor(log2(u64)). If u64 is 0, (incorrectly) returns 0. */
366 tor_log2(uint64_t u64
)
369 if (u64
>= (U64_LITERAL(1)<<32)) {
373 if (u64
>= (U64_LITERAL(1)<<16)) {
377 if (u64
>= (U64_LITERAL(1)<<8)) {
381 if (u64
>= (U64_LITERAL(1)<<4)) {
385 if (u64
>= (U64_LITERAL(1)<<2)) {
389 if (u64
>= (U64_LITERAL(1)<<1)) {
396 /** Return the power of 2 in range [1,UINT64_MAX] closest to <b>u64</b>. If
397 * there are two powers of 2 equally close, round down. */
399 round_to_power_of_2(uint64_t u64
)
408 low
= U64_LITERAL(1) << lg2
;
413 high
= U64_LITERAL(1) << (lg2
+1);
414 if (high
- u64
< u64
- low
)
420 /** Return the lowest x such that x is at least <b>number</b>, and x modulo
421 * <b>divisor</b> == 0. */
423 round_to_next_multiple_of(unsigned number
, unsigned divisor
)
425 number
+= divisor
- 1;
426 number
-= number
% divisor
;
430 /** Return the lowest x such that x is at least <b>number</b>, and x modulo
431 * <b>divisor</b> == 0. */
433 round_uint32_to_next_multiple_of(uint32_t number
, uint32_t divisor
)
435 number
+= divisor
- 1;
436 number
-= number
% divisor
;
440 /** Return the lowest x such that x is at least <b>number</b>, and x modulo
441 * <b>divisor</b> == 0. */
443 round_uint64_to_next_multiple_of(uint64_t number
, uint64_t divisor
)
445 number
+= divisor
- 1;
446 number
-= number
% divisor
;
450 /** Return the number of bits set in <b>v</b>. */
452 n_bits_set_u8(uint8_t v
)
454 static const int nybble_table
[] = {
473 return nybble_table
[v
& 15] + nybble_table
[v
>>4];
477 * String manipulation
480 /** Remove from the string <b>s</b> every character which appears in
483 tor_strstrip(char *s
, const char *strip
)
487 if (strchr(strip
, *read
)) {
496 /** Return a pointer to a NUL-terminated hexadecimal string encoding
497 * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
498 * result does not need to be deallocated, but repeated calls to
499 * hex_str will trash old results.
502 hex_str(const char *from
, size_t fromlen
)
505 if (fromlen
>(sizeof(buf
)-1)/2)
506 fromlen
= (sizeof(buf
)-1)/2;
507 base16_encode(buf
,sizeof(buf
),from
,fromlen
);
511 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
514 tor_strlower(char *s
)
517 *s
= TOR_TOLOWER(*s
);
522 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
525 tor_strupper(char *s
)
528 *s
= TOR_TOUPPER(*s
);
533 /** Return 1 if every character in <b>s</b> is printable, else return 0.
536 tor_strisprint(const char *s
)
539 if (!TOR_ISPRINT(*s
))
546 /** Return 1 if no character in <b>s</b> is uppercase, else return 0.
549 tor_strisnonupper(const char *s
)
559 /** As strcmp, except that either string may be NULL. The NULL string is
560 * considered to be before any non-NULL string. */
562 strcmp_opt(const char *s1
, const char *s2
)
572 return strcmp(s1
, s2
);
576 /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
580 strcmpstart(const char *s1
, const char *s2
)
582 size_t n
= strlen(s2
);
583 return strncmp(s1
, s2
, n
);
586 /** Compare the s1_len-byte string <b>s1</b> with <b>s2</b>,
587 * without depending on a terminating nul in s1. Sorting order is first by
588 * length, then lexically; return values are as for strcmp.
591 strcmp_len(const char *s1
, const char *s2
, size_t s1_len
)
593 size_t s2_len
= strlen(s2
);
598 return fast_memcmp(s1
, s2
, s2_len
);
601 /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
605 strcasecmpstart(const char *s1
, const char *s2
)
607 size_t n
= strlen(s2
);
608 return strncasecmp(s1
, s2
, n
);
611 /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
615 strcmpend(const char *s1
, const char *s2
)
617 size_t n1
= strlen(s1
), n2
= strlen(s2
);
619 return strcmp(s1
,s2
);
621 return strncmp(s1
+(n1
-n2
), s2
, n2
);
624 /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
628 strcasecmpend(const char *s1
, const char *s2
)
630 size_t n1
= strlen(s1
), n2
= strlen(s2
);
631 if (n2
>n1
) /* then they can't be the same; figure out which is bigger */
632 return strcasecmp(s1
,s2
);
634 return strncasecmp(s1
+(n1
-n2
), s2
, n2
);
637 /** Compare the value of the string <b>prefix</b> with the start of the
638 * <b>memlen</b>-byte memory chunk at <b>mem</b>. Return as for strcmp.
640 * [As fast_memcmp(mem, prefix, strlen(prefix)) but returns -1 if memlen is
641 * less than strlen(prefix).]
644 fast_memcmpstart(const void *mem
, size_t memlen
,
647 size_t plen
= strlen(prefix
);
650 return fast_memcmp(mem
, prefix
, plen
);
653 /** Given a nul-terminated string s, set every character before the nul
656 tor_strclear(char *s
)
663 /** Return a pointer to the first char of s that is not whitespace and
664 * not a comment, or to the terminating NUL if no such character exists.
667 eat_whitespace(const char *s
)
684 while (*s
&& *s
!= '\n')
690 /** Return a pointer to the first char of s that is not whitespace and
691 * not a comment, or to the terminating NUL if no such character exists.
694 eat_whitespace_eos(const char *s
, const char *eos
)
697 tor_assert(eos
&& s
<= eos
);
712 while (s
< eos
&& *s
&& *s
!= '\n')
719 /** Return a pointer to the first char of s that is not a space or a tab
720 * or a \\r, or to the terminating NUL if no such character exists. */
722 eat_whitespace_no_nl(const char *s
)
724 while (*s
== ' ' || *s
== '\t' || *s
== '\r')
729 /** As eat_whitespace_no_nl, but stop at <b>eos</b> whether we have
730 * found a non-whitespace character or not. */
732 eat_whitespace_eos_no_nl(const char *s
, const char *eos
)
734 while (s
< eos
&& (*s
== ' ' || *s
== '\t' || *s
== '\r'))
739 /** Return a pointer to the first char of s that is whitespace or <b>#</b>,
740 * or to the terminating NUL if no such character exists.
743 find_whitespace(const char *s
)
762 /** As find_whitespace, but stop at <b>eos</b> whether we have found a
763 * whitespace or not. */
765 find_whitespace_eos(const char *s
, const char *eos
)
785 /** Return the first occurrence of <b>needle</b> in <b>haystack</b> that
786 * occurs at the start of a line (that is, at the beginning of <b>haystack</b>
787 * or immediately after a newline). Return NULL if no such string is found.
790 find_str_at_start_of_line(const char *haystack
, const char *needle
)
792 size_t needle_len
= strlen(needle
);
795 if (!strncmp(haystack
, needle
, needle_len
))
798 haystack
= strchr(haystack
, '\n');
808 /** Returns true if <b>string</b> could be a C identifier.
809 A C identifier must begin with a letter or an underscore and the
810 rest of its characters can be letters, numbers or underscores. No
811 length limit is imposed. */
813 string_is_C_identifier(const char *string
)
816 size_t length
= strlen(string
);
820 for (iter
= 0; iter
< length
; iter
++) {
822 if (!(TOR_ISALPHA(string
[iter
]) ||
823 string
[iter
] == '_'))
826 if (!(TOR_ISALPHA(string
[iter
]) ||
827 TOR_ISDIGIT(string
[iter
]) ||
828 string
[iter
] == '_'))
836 /** Return true iff the 'len' bytes at 'mem' are all zero. */
838 tor_mem_is_zero(const char *mem
, size_t len
)
840 static const char ZERO
[] = {
841 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
843 while (len
>= sizeof(ZERO
)) {
844 /* It's safe to use fast_memcmp here, since the very worst thing an
845 * attacker could learn is how many initial bytes of a secret were zero */
846 if (fast_memcmp(mem
, ZERO
, sizeof(ZERO
)))
851 /* Deal with leftover bytes. */
853 return fast_memeq(mem
, ZERO
, len
);
858 /** Return true iff the DIGEST_LEN bytes in digest are all zero. */
860 tor_digest_is_zero(const char *digest
)
862 static const uint8_t ZERO_DIGEST
[] = {
863 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
865 return tor_memeq(digest
, ZERO_DIGEST
, DIGEST_LEN
);
868 /** Return true iff the DIGEST256_LEN bytes in digest are all zero. */
870 tor_digest256_is_zero(const char *digest
)
872 return tor_mem_is_zero(digest
, DIGEST256_LEN
);
875 /* Helper: common code to check whether the result of a strtol or strtoul or
876 * strtoll is correct. */
877 #define CHECK_STRTOX_RESULT() \
878 /* Did an overflow occur? */ \
879 if (errno == ERANGE) \
881 /* Was at least one character converted? */ \
884 /* Were there unexpected unconverted characters? */ \
885 if (!next && *endptr) \
887 /* Is r within limits? */ \
888 if (r < min || r > max) \
891 if (next) *next = endptr; \
895 if (next) *next = endptr; \
898 /** Extract a long from the start of <b>s</b>, in the given numeric
899 * <b>base</b>. If <b>base</b> is 0, <b>s</b> is parsed as a decimal,
900 * octal, or hex number in the syntax of a C integer literal. If
901 * there is unconverted data and <b>next</b> is provided, set
902 * *<b>next</b> to the first unconverted character. An error has
903 * occurred if no characters are converted; or if there are
904 * unconverted characters and <b>next</b> is NULL; or if the parsed
905 * value is not between <b>min</b> and <b>max</b>. When no error
906 * occurs, return the parsed value and set *<b>ok</b> (if provided) to
907 * 1. When an error occurs, return 0 and set *<b>ok</b> (if provided)
911 tor_parse_long(const char *s
, int base
, long min
, long max
,
912 int *ok
, char **next
)
924 r
= strtol(s
, &endptr
, base
);
925 CHECK_STRTOX_RESULT();
928 /** As tor_parse_long(), but return an unsigned long. */
930 tor_parse_ulong(const char *s
, int base
, unsigned long min
,
931 unsigned long max
, int *ok
, char **next
)
943 r
= strtoul(s
, &endptr
, base
);
944 CHECK_STRTOX_RESULT();
947 /** As tor_parse_long(), but return a double. */
949 tor_parse_double(const char *s
, double min
, double max
, int *ok
, char **next
)
955 r
= strtod(s
, &endptr
);
956 CHECK_STRTOX_RESULT();
959 /** As tor_parse_long, but return a uint64_t. Only base 10 is guaranteed to
962 tor_parse_uint64(const char *s
, int base
, uint64_t min
,
963 uint64_t max
, int *ok
, char **next
)
976 r
= (uint64_t)strtoull(s
, &endptr
, base
);
977 #elif defined(_WIN32)
978 #if defined(_MSC_VER) && _MSC_VER < 1300
979 tor_assert(base
<= 10);
980 r
= (uint64_t)_atoi64(s
);
982 while (TOR_ISSPACE(*endptr
)) endptr
++;
983 while (TOR_ISDIGIT(*endptr
)) endptr
++;
985 r
= (uint64_t)_strtoui64(s
, &endptr
, base
);
987 #elif SIZEOF_LONG == 8
988 r
= (uint64_t)strtoul(s
, &endptr
, base
);
990 #error "I don't know how to parse 64-bit numbers."
993 CHECK_STRTOX_RESULT();
996 /** Encode the <b>srclen</b> bytes at <b>src</b> in a NUL-terminated,
997 * uppercase hexadecimal string; store it in the <b>destlen</b>-byte buffer
1001 base16_encode(char *dest
, size_t destlen
, const char *src
, size_t srclen
)
1006 tor_assert(destlen
>= srclen
*2+1);
1007 tor_assert(destlen
< SIZE_T_CEILING
);
1012 *cp
++ = "0123456789ABCDEF"[ (*(const uint8_t*)src
) >> 4 ];
1013 *cp
++ = "0123456789ABCDEF"[ (*(const uint8_t*)src
) & 0xf ];
1019 /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
1021 hex_decode_digit_(char c
)
1034 case 'A': case 'a': return 10;
1035 case 'B': case 'b': return 11;
1036 case 'C': case 'c': return 12;
1037 case 'D': case 'd': return 13;
1038 case 'E': case 'e': return 14;
1039 case 'F': case 'f': return 15;
1045 /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
1047 hex_decode_digit(char c
)
1049 return hex_decode_digit_(c
);
1052 /** Given a hexadecimal string of <b>srclen</b> bytes in <b>src</b>, decode it
1053 * and store the result in the <b>destlen</b>-byte buffer at <b>dest</b>.
1054 * Return 0 on success, -1 on failure. */
1056 base16_decode(char *dest
, size_t destlen
, const char *src
, size_t srclen
)
1061 if ((srclen
% 2) != 0)
1063 if (destlen
< srclen
/2 || destlen
> SIZE_T_CEILING
)
1067 v1
= hex_decode_digit_(*src
);
1068 v2
= hex_decode_digit_(*(src
+1));
1071 *(uint8_t*)dest
= (v1
<<4)|v2
;
1078 /** Allocate and return a new string representing the contents of <b>s</b>,
1079 * surrounded by quotes and using standard C escapes.
1081 * Generally, we use this for logging values that come in over the network to
1082 * keep them from tricking users, and for sending certain values to the
1085 * We trust values from the resolver, OS, configuration file, and command line
1086 * to not be maliciously ill-formed. We validate incoming routerdescs and
1087 * SOCKS requests and addresses from BEGIN cells as they're parsed;
1088 * afterwards, we trust them as non-malicious.
1091 esc_for_log(const char *s
)
1094 char *result
, *outp
;
1097 return tor_strdup("(null)");
1100 for (cp
= s
; *cp
; ++cp
) {
1111 if (TOR_ISPRINT(*cp
) && ((uint8_t)*cp
)<127)
1119 result
= outp
= tor_malloc(len
);
1121 for (cp
= s
; *cp
; ++cp
) {
1142 if (TOR_ISPRINT(*cp
) && ((uint8_t)*cp
)<127) {
1145 tor_snprintf(outp
, 5, "\\%03o", (int)(uint8_t) *cp
);
1158 /** Allocate and return a new string representing the contents of <b>s</b>,
1159 * surrounded by quotes and using standard C escapes.
1161 * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
1162 * thread. Also, each call invalidates the last-returned value, so don't
1163 * try log_warn(LD_GENERAL, "%s %s", escaped(a), escaped(b));
1166 escaped(const char *s
)
1168 static char *escaped_val_
= NULL
;
1169 tor_free(escaped_val_
);
1172 escaped_val_
= esc_for_log(s
);
1174 escaped_val_
= NULL
;
1176 return escaped_val_
;
1179 /** Rudimentary string wrapping code: given a un-wrapped <b>string</b> (no
1180 * newlines!), break the string into newline-terminated lines of no more than
1181 * <b>width</b> characters long (not counting newline) and insert them into
1182 * <b>out</b> in order. Precede the first line with prefix0, and subsequent
1183 * lines with prefixRest.
1185 /* This uses a stupid greedy wrapping algorithm right now:
1187 * - Try to fit as much stuff as possible, but break on a space.
1188 * - If the first "word" of the line will extend beyond the allowable
1189 * width, break the word at the end of the width.
1192 wrap_string(smartlist_t
*out
, const char *string
, size_t width
,
1193 const char *prefix0
, const char *prefixRest
)
1195 size_t p0Len
, pRestLen
, pCurLen
;
1196 const char *eos
, *prefixCur
;
1205 p0Len
= strlen(prefix0
);
1206 pRestLen
= strlen(prefixRest
);
1207 tor_assert(width
> p0Len
&& width
> pRestLen
);
1208 eos
= strchr(string
, '\0');
1211 prefixCur
= prefix0
;
1213 while ((eos
-string
)+pCurLen
> width
) {
1214 const char *eol
= string
+ width
- pCurLen
;
1215 while (eol
> string
&& *eol
!= ' ')
1217 /* eol is now the last space that can fit, or the start of the string. */
1219 size_t line_len
= (eol
-string
) + pCurLen
+ 2;
1220 char *line
= tor_malloc(line_len
);
1221 memcpy(line
, prefixCur
, pCurLen
);
1222 memcpy(line
+pCurLen
, string
, eol
-string
);
1223 line
[line_len
-2] = '\n';
1224 line
[line_len
-1] = '\0';
1225 smartlist_add(out
, line
);
1228 size_t line_len
= width
+ 2;
1229 char *line
= tor_malloc(line_len
);
1230 memcpy(line
, prefixCur
, pCurLen
);
1231 memcpy(line
+pCurLen
, string
, width
- pCurLen
);
1232 line
[line_len
-2] = '\n';
1233 line
[line_len
-1] = '\0';
1234 smartlist_add(out
, line
);
1235 string
+= width
-pCurLen
;
1237 prefixCur
= prefixRest
;
1242 size_t line_len
= (eos
-string
) + pCurLen
+ 2;
1243 char *line
= tor_malloc(line_len
);
1244 memcpy(line
, prefixCur
, pCurLen
);
1245 memcpy(line
+pCurLen
, string
, eos
-string
);
1246 line
[line_len
-2] = '\n';
1247 line
[line_len
-1] = '\0';
1248 smartlist_add(out
, line
);
1256 /** Return the number of microseconds elapsed between *start and *end.
1259 tv_udiff(const struct timeval
*start
, const struct timeval
*end
)
1262 long secdiff
= end
->tv_sec
- start
->tv_sec
;
1264 if (labs(secdiff
+1) > LONG_MAX
/1000000) {
1265 log_warn(LD_GENERAL
, "comparing times on microsecond detail too far "
1266 "apart: %ld seconds", secdiff
);
1270 udiff
= secdiff
*1000000L + (end
->tv_usec
- start
->tv_usec
);
1274 /** Return the number of milliseconds elapsed between *start and *end.
1277 tv_mdiff(const struct timeval
*start
, const struct timeval
*end
)
1280 long secdiff
= end
->tv_sec
- start
->tv_sec
;
1282 if (labs(secdiff
+1) > LONG_MAX
/1000) {
1283 log_warn(LD_GENERAL
, "comparing times on millisecond detail too far "
1284 "apart: %ld seconds", secdiff
);
1288 /* Subtract and round */
1289 mdiff
= secdiff
*1000L +
1290 ((long)end
->tv_usec
- (long)start
->tv_usec
+ 500L) / 1000L;
1294 /** Yield true iff <b>y</b> is a leap-year. */
1295 #define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
1296 /** Helper: Return the number of leap-days between Jan 1, y1 and Jan 1, y2. */
1298 n_leapdays(int y1
, int y2
)
1302 return (y2
/4 - y1
/4) - (y2
/100 - y1
/100) + (y2
/400 - y1
/400);
1304 /** Number of days per month in non-leap year; used by tor_timegm. */
1305 static const int days_per_month
[] =
1306 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1308 /** Compute a time_t given a struct tm. The result is given in UTC, and
1309 * does not account for leap seconds. Return 0 on success, -1 on failure.
1312 tor_timegm(const struct tm
*tm
, time_t *time_out
)
1314 /* This is a pretty ironclad timegm implementation, snarfed from Python2.2.
1315 * It's way more brute-force than fiddling with tzset().
1317 time_t year
, days
, hours
, minutes
, seconds
;
1319 year
= tm
->tm_year
+ 1900;
1320 if (year
< 1970 || tm
->tm_mon
< 0 || tm
->tm_mon
> 11 ||
1321 tm
->tm_year
>= INT32_MAX
-1900) {
1322 log_warn(LD_BUG
, "Out-of-range argument to tor_timegm");
1325 days
= 365 * (year
-1970) + n_leapdays(1970,(int)year
);
1326 for (i
= 0; i
< tm
->tm_mon
; ++i
)
1327 days
+= days_per_month
[i
];
1328 if (tm
->tm_mon
> 1 && IS_LEAPYEAR(year
))
1330 days
+= tm
->tm_mday
- 1;
1331 hours
= days
*24 + tm
->tm_hour
;
1333 minutes
= hours
*60 + tm
->tm_min
;
1334 seconds
= minutes
*60 + tm
->tm_sec
;
1335 *time_out
= seconds
;
1339 /* strftime is locale-specific, so we need to replace those parts */
1341 /** A c-locale array of 3-letter names of weekdays, starting with Sun. */
1342 static const char *WEEKDAY_NAMES
[] =
1343 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
1344 /** A c-locale array of 3-letter names of months, starting with Jan. */
1345 static const char *MONTH_NAMES
[] =
1346 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1347 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1349 /** Set <b>buf</b> to the RFC1123 encoding of the UTC value of <b>t</b>.
1350 * The buffer must be at least RFC1123_TIME_LEN+1 bytes long.
1352 * (RFC1123 format is "Fri, 29 Sep 2006 15:54:20 GMT". Note the "GMT"
1353 * rather than "UTC".)
1356 format_rfc1123_time(char *buf
, time_t t
)
1360 tor_gmtime_r(&t
, &tm
);
1362 strftime(buf
, RFC1123_TIME_LEN
+1, "___, %d ___ %Y %H:%M:%S GMT", &tm
);
1363 tor_assert(tm
.tm_wday
>= 0);
1364 tor_assert(tm
.tm_wday
<= 6);
1365 memcpy(buf
, WEEKDAY_NAMES
[tm
.tm_wday
], 3);
1366 tor_assert(tm
.tm_mon
>= 0);
1367 tor_assert(tm
.tm_mon
<= 11);
1368 memcpy(buf
+8, MONTH_NAMES
[tm
.tm_mon
], 3);
1371 /** Parse the (a subset of) the RFC1123 encoding of some time (in UTC) from
1372 * <b>buf</b>, and store the result in *<b>t</b>.
1374 * Note that we only accept the subset generated by format_rfc1123_time above,
1375 * not the full range of formats suggested by RFC 1123.
1377 * Return 0 on success, -1 on failure.
1380 parse_rfc1123_time(const char *buf
, time_t *t
)
1386 unsigned tm_mday
, tm_year
, tm_hour
, tm_min
, tm_sec
;
1388 if (strlen(buf
) != RFC1123_TIME_LEN
)
1390 memset(&tm
, 0, sizeof(tm
));
1391 if (tor_sscanf(buf
, "%3s, %2u %3s %u %2u:%2u:%2u GMT", weekday
,
1392 &tm_mday
, month
, &tm_year
, &tm_hour
,
1393 &tm_min
, &tm_sec
) < 7) {
1394 char *esc
= esc_for_log(buf
);
1395 log_warn(LD_GENERAL
, "Got invalid RFC1123 time %s", esc
);
1399 if (tm_mday
< 1 || tm_mday
> 31 || tm_hour
> 23 || tm_min
> 59 ||
1400 tm_sec
> 60 || tm_year
>= INT32_MAX
|| tm_year
< 1970) {
1401 char *esc
= esc_for_log(buf
);
1402 log_warn(LD_GENERAL
, "Got invalid RFC1123 time %s", esc
);
1406 tm
.tm_mday
= (int)tm_mday
;
1407 tm
.tm_year
= (int)tm_year
;
1408 tm
.tm_hour
= (int)tm_hour
;
1409 tm
.tm_min
= (int)tm_min
;
1410 tm
.tm_sec
= (int)tm_sec
;
1413 for (i
= 0; i
< 12; ++i
) {
1414 if (!strcmp(month
, MONTH_NAMES
[i
])) {
1420 char *esc
= esc_for_log(buf
);
1421 log_warn(LD_GENERAL
, "Got invalid RFC1123 time %s: No such month", esc
);
1427 if (tm
.tm_year
< 1970) {
1428 char *esc
= esc_for_log(buf
);
1429 log_warn(LD_GENERAL
,
1430 "Got invalid RFC1123 time %s. (Before 1970)", esc
);
1436 return tor_timegm(&tm
, t
);
1439 /** Set <b>buf</b> to the ISO8601 encoding of the local value of <b>t</b>.
1440 * The buffer must be at least ISO_TIME_LEN+1 bytes long.
1442 * (ISO8601 format is 2006-10-29 10:57:20)
1445 format_local_iso_time(char *buf
, time_t t
)
1448 strftime(buf
, ISO_TIME_LEN
+1, "%Y-%m-%d %H:%M:%S", tor_localtime_r(&t
, &tm
));
1451 /** Set <b>buf</b> to the ISO8601 encoding of the GMT value of <b>t</b>.
1452 * The buffer must be at least ISO_TIME_LEN+1 bytes long.
1455 format_iso_time(char *buf
, time_t t
)
1458 strftime(buf
, ISO_TIME_LEN
+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t
, &tm
));
1461 /** As format_iso_time, but use the yyyy-mm-ddThh:mm:ss format to avoid
1462 * embedding an internal space. */
1464 format_iso_time_nospace(char *buf
, time_t t
)
1466 format_iso_time(buf
, t
);
1470 /** As format_iso_time_nospace, but include microseconds in decimal
1471 * fixed-point format. Requires that buf be at least ISO_TIME_USEC_LEN+1
1474 format_iso_time_nospace_usec(char *buf
, const struct timeval
*tv
)
1477 format_iso_time_nospace(buf
, tv
->tv_sec
);
1478 tor_snprintf(buf
+ISO_TIME_LEN
, 8, ".%06d", (int)tv
->tv_usec
);
1481 /** Given an ISO-formatted UTC time value (after the epoch) in <b>cp</b>,
1482 * parse it and store its value in *<b>t</b>. Return 0 on success, -1 on
1483 * failure. Ignore extraneous stuff in <b>cp</b> separated by whitespace from
1484 * the end of the time string. */
1486 parse_iso_time(const char *cp
, time_t *t
)
1489 unsigned int year
=0, month
=0, day
=0, hour
=0, minute
=0, second
=0;
1490 if (tor_sscanf(cp
, "%u-%2u-%2u %2u:%2u:%2u", &year
, &month
,
1491 &day
, &hour
, &minute
, &second
) < 6) {
1492 char *esc
= esc_for_log(cp
);
1493 log_warn(LD_GENERAL
, "ISO time %s was unparseable", esc
);
1497 if (year
< 1970 || month
< 1 || month
> 12 || day
< 1 || day
> 31 ||
1498 hour
> 23 || minute
> 59 || second
> 60 || year
>= INT32_MAX
) {
1499 char *esc
= esc_for_log(cp
);
1500 log_warn(LD_GENERAL
, "ISO time %s was nonsensical", esc
);
1504 st_tm
.tm_year
= (int)year
-1900;
1505 st_tm
.tm_mon
= month
-1;
1506 st_tm
.tm_mday
= day
;
1507 st_tm
.tm_hour
= hour
;
1508 st_tm
.tm_min
= minute
;
1509 st_tm
.tm_sec
= second
;
1511 if (st_tm
.tm_year
< 70) {
1512 char *esc
= esc_for_log(cp
);
1513 log_warn(LD_GENERAL
, "Got invalid ISO time %s. (Before 1970)", esc
);
1517 return tor_timegm(&st_tm
, t
);
1520 /** Given a <b>date</b> in one of the three formats allowed by HTTP (ugh),
1521 * parse it into <b>tm</b>. Return 0 on success, negative on failure. */
1523 parse_http_time(const char *date
, struct tm
*tm
)
1529 unsigned tm_mday
, tm_year
, tm_hour
, tm_min
, tm_sec
;
1532 memset(tm
, 0, sizeof(*tm
));
1534 /* First, try RFC1123 or RFC850 format: skip the weekday. */
1535 if ((cp
= strchr(date
, ','))) {
1540 if (tor_sscanf(cp
, "%2u %3s %4u %2u:%2u:%2u GMT",
1541 &tm_mday
, month
, &tm_year
,
1542 &tm_hour
, &tm_min
, &tm_sec
) == 6) {
1545 } else if (tor_sscanf(cp
, "%2u-%3s-%2u %2u:%2u:%2u GMT",
1546 &tm_mday
, month
, &tm_year
,
1547 &tm_hour
, &tm_min
, &tm_sec
) == 6) {
1553 /* No comma; possibly asctime() format. */
1554 if (tor_sscanf(date
, "%3s %3s %2u %2u:%2u:%2u %4u",
1555 wkday
, month
, &tm_mday
,
1556 &tm_hour
, &tm_min
, &tm_sec
, &tm_year
) == 7) {
1562 tm
->tm_mday
= (int)tm_mday
;
1563 tm
->tm_year
= (int)tm_year
;
1564 tm
->tm_hour
= (int)tm_hour
;
1565 tm
->tm_min
= (int)tm_min
;
1566 tm
->tm_sec
= (int)tm_sec
;
1569 /* Okay, now decode the month. */
1570 /* set tm->tm_mon to dummy value so the check below fails. */
1572 for (i
= 0; i
< 12; ++i
) {
1573 if (!strcasecmp(MONTH_NAMES
[i
], month
)) {
1578 if (tm
->tm_year
< 0 ||
1579 tm
->tm_mon
< 0 || tm
->tm_mon
> 11 ||
1580 tm
->tm_mday
< 1 || tm
->tm_mday
> 31 ||
1581 tm
->tm_hour
< 0 || tm
->tm_hour
> 23 ||
1582 tm
->tm_min
< 0 || tm
->tm_min
> 59 ||
1583 tm
->tm_sec
< 0 || tm
->tm_sec
> 60)
1584 return -1; /* Out of range, or bad month. */
1589 /** Given an <b>interval</b> in seconds, try to write it to the
1590 * <b>out_len</b>-byte buffer in <b>out</b> in a human-readable form.
1591 * Return 0 on success, -1 on failure.
1594 format_time_interval(char *out
, size_t out_len
, long interval
)
1596 /* We only report seconds if there's no hours. */
1597 long sec
= 0, min
= 0, hour
= 0, day
= 0;
1599 interval
= -interval
;
1601 if (interval
>= 86400) {
1602 day
= interval
/ 86400;
1605 if (interval
>= 3600) {
1606 hour
= interval
/ 3600;
1609 if (interval
>= 60) {
1610 min
= interval
/ 60;
1616 return tor_snprintf(out
, out_len
, "%ld days, %ld hours, %ld minutes",
1619 return tor_snprintf(out
, out_len
, "%ld hours, %ld minutes", hour
, min
);
1621 return tor_snprintf(out
, out_len
, "%ld minutes, %ld seconds", min
, sec
);
1623 return tor_snprintf(out
, out_len
, "%ld seconds", sec
);
1631 #ifndef TIME_IS_FAST
1632 /** Cached estimate of the current time. Updated around once per second;
1633 * may be a few seconds off if we are really busy. This is a hack to avoid
1634 * calling time(NULL) (which not everybody has optimized) on critical paths.
1636 static time_t cached_approx_time
= 0;
1638 /** Return a cached estimate of the current time from when
1639 * update_approx_time() was last called. This is a hack to avoid calling
1640 * time(NULL) on critical paths: please do not even think of calling it
1645 return cached_approx_time
;
1648 /** Update the cached estimate of the current time. This function SHOULD be
1649 * called once per second, and MUST be called before the first call to
1650 * get_approx_time. */
1652 update_approx_time(time_t now
)
1654 cached_approx_time
= now
;
1662 /** If the rate-limiter <b>lim</b> is ready at <b>now</b>, return the number
1663 * of calls to rate_limit_is_ready (including this one!) since the last time
1664 * rate_limit_is_ready returned nonzero. Otherwise return 0. */
1666 rate_limit_is_ready(ratelim_t
*lim
, time_t now
)
1668 if (lim
->rate
+ lim
->last_allowed
<= now
) {
1669 int res
= lim
->n_calls_since_last_time
+ 1;
1670 lim
->last_allowed
= now
;
1671 lim
->n_calls_since_last_time
= 0;
1674 ++lim
->n_calls_since_last_time
;
1679 /** If the rate-limiter <b>lim</b> is ready at <b>now</b>, return a newly
1680 * allocated string indicating how many messages were suppressed, suitable to
1681 * append to a log message. Otherwise return NULL. */
1683 rate_limit_log(ratelim_t
*lim
, time_t now
)
1686 if ((n
= rate_limit_is_ready(lim
, now
))) {
1688 return tor_strdup("");
1692 " [%d similar message(s) suppressed in last %d seconds]",
1705 /** Write <b>count</b> bytes from <b>buf</b> to <b>fd</b>. <b>isSocket</b>
1706 * must be 1 if fd was returned by socket() or accept(), and 0 if fd
1707 * was returned by open(). Return the number of bytes written, or -1
1708 * on error. Only use if fd is a blocking fd. */
1710 write_all(tor_socket_t fd
, const char *buf
, size_t count
, int isSocket
)
1714 tor_assert(count
< SSIZE_T_MAX
);
1716 while (written
!= count
) {
1718 result
= tor_socket_send(fd
, buf
+written
, count
-written
, 0);
1720 result
= write((int)fd
, buf
+written
, count
-written
);
1725 return (ssize_t
)count
;
1728 /** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes
1729 * or reach the end of the file. <b>isSocket</b> must be 1 if fd
1730 * was returned by socket() or accept(), and 0 if fd was returned by
1731 * open(). Return the number of bytes read, or -1 on error. Only use
1732 * if fd is a blocking fd. */
1734 read_all(tor_socket_t fd
, char *buf
, size_t count
, int isSocket
)
1739 if (count
> SIZE_T_CEILING
|| count
> SSIZE_T_MAX
)
1742 while (numread
!= count
) {
1744 result
= tor_socket_recv(fd
, buf
+numread
, count
-numread
, 0);
1746 result
= read((int)fd
, buf
+numread
, count
-numread
);
1749 else if (result
== 0)
1753 return (ssize_t
)numread
;
1757 * Filesystem operations.
1760 /** Clean up <b>name</b> so that we can use it in a call to "stat". On Unix,
1761 * we do nothing. On Windows, we remove a trailing slash, unless the path is
1762 * the root of a disk. */
1764 clean_name_for_stat(char *name
)
1767 size_t len
= strlen(name
);
1770 if (name
[len
-1]=='\\' || name
[len
-1]=='/') {
1771 if (len
== 1 || (len
==3 && name
[1]==':'))
1780 /** Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
1781 * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
1782 * directory. On FN_ERROR, sets errno. */
1784 file_status(const char *fname
)
1789 f
= tor_strdup(fname
);
1790 clean_name_for_stat(f
);
1794 if (errno
== ENOENT
) {
1799 if (st
.st_mode
& S_IFDIR
)
1801 else if (st
.st_mode
& S_IFREG
)
1804 else if (st
.st_mode
& S_IFIFO
)
1811 /** Check whether <b>dirname</b> exists and is private. If yes return 0. If
1812 * it does not exist, and <b>check</b>&CPD_CREATE is set, try to create it
1813 * and return 0 on success. If it does not exist, and
1814 * <b>check</b>&CPD_CHECK, and we think we can create it, return 0. Else
1815 * return -1. If CPD_GROUP_OK is set, then it's okay if the directory
1816 * is group-readable, but in all cases we create the directory mode 0700.
1817 * If CPD_CHECK_MODE_ONLY is set, then we don't alter the directory permissions
1818 * if they are too permissive: we just return -1.
1819 * When effective_user is not NULL, check permissions against the given user
1820 * and its primary group.
1823 check_private_dir(const char *dirname
, cpd_check_t check
,
1824 const char *effective_user
)
1831 struct passwd
*pw
= NULL
;
1835 (void)effective_user
;
1838 tor_assert(dirname
);
1839 f
= tor_strdup(dirname
);
1840 clean_name_for_stat(f
);
1844 if (errno
!= ENOENT
) {
1845 log_warn(LD_FS
, "Directory %s cannot be read: %s", dirname
,
1849 if (check
& CPD_CREATE
) {
1850 log_info(LD_GENERAL
, "Creating directory %s", dirname
);
1851 #if defined (_WIN32) && !defined (WINCE)
1854 r
= mkdir(dirname
, 0700);
1857 log_warn(LD_FS
, "Error creating directory %s: %s", dirname
,
1861 } else if (!(check
& CPD_CHECK
)) {
1862 log_warn(LD_FS
, "Directory %s does not exist.", dirname
);
1865 /* XXXX In the case where check==CPD_CHECK, we should look at the
1866 * parent directory a little harder. */
1869 if (!(st
.st_mode
& S_IFDIR
)) {
1870 log_warn(LD_FS
, "%s is not a directory", dirname
);
1874 if (effective_user
) {
1875 /* Look up the user and group information.
1876 * If we have a problem, bail out. */
1877 pw
= getpwnam(effective_user
);
1879 log_warn(LD_CONFIG
, "Error setting configured user: %s not found",
1883 running_uid
= pw
->pw_uid
;
1884 running_gid
= pw
->pw_gid
;
1886 running_uid
= getuid();
1887 running_gid
= getgid();
1890 if (st
.st_uid
!= running_uid
) {
1891 struct passwd
*pw
= NULL
;
1892 char *process_ownername
= NULL
;
1894 pw
= getpwuid(running_uid
);
1895 process_ownername
= pw
? tor_strdup(pw
->pw_name
) : tor_strdup("<unknown>");
1897 pw
= getpwuid(st
.st_uid
);
1899 log_warn(LD_FS
, "%s is not owned by this user (%s, %d) but by "
1900 "%s (%d). Perhaps you are running Tor as the wrong user?",
1901 dirname
, process_ownername
, (int)running_uid
,
1902 pw
? pw
->pw_name
: "<unknown>", (int)st
.st_uid
);
1904 tor_free(process_ownername
);
1907 if ((check
& CPD_GROUP_OK
) && st
.st_gid
!= running_gid
) {
1909 char *process_groupname
= NULL
;
1910 gr
= getgrgid(running_gid
);
1911 process_groupname
= gr
? tor_strdup(gr
->gr_name
) : tor_strdup("<unknown>");
1912 gr
= getgrgid(st
.st_gid
);
1914 log_warn(LD_FS
, "%s is not owned by this group (%s, %d) but by group "
1915 "%s (%d). Are you running Tor as the wrong user?",
1916 dirname
, process_groupname
, (int)running_gid
,
1917 gr
? gr
->gr_name
: "<unknown>", (int)st
.st_gid
);
1919 tor_free(process_groupname
);
1922 if (check
& CPD_GROUP_OK
) {
1927 if (st
.st_mode
& mask
) {
1929 if (check
& CPD_CHECK_MODE_ONLY
) {
1930 log_warn(LD_FS
, "Permissions on directory %s are too permissive.",
1934 log_warn(LD_FS
, "Fixing permissions on directory %s", dirname
);
1935 new_mode
= st
.st_mode
;
1936 new_mode
|= 0700; /* Owner should have rwx */
1937 new_mode
&= ~mask
; /* Clear the other bits that we didn't want set...*/
1938 if (chmod(dirname
, new_mode
)) {
1939 log_warn(LD_FS
, "Could not chmod directory %s: %s", dirname
,
1950 /** Create a file named <b>fname</b> with the contents <b>str</b>. Overwrite
1951 * the previous <b>fname</b> if possible. Return 0 on success, -1 on failure.
1953 * This function replaces the old file atomically, if possible. This
1954 * function, and all other functions in util.c that create files, create them
1958 write_str_to_file(const char *fname
, const char *str
, int bin
)
1961 if (!bin
&& strchr(str
, '\r')) {
1963 "We're writing a text string that already contains a CR.");
1966 return write_bytes_to_file(fname
, str
, strlen(str
), bin
);
1969 /** Represents a file that we're writing to, with support for atomic commit:
1970 * we can write into a temporary file, and either remove the file on
1971 * failure, or replace the original file on success. */
1972 struct open_file_t
{
1973 char *tempname
; /**< Name of the temporary file. */
1974 char *filename
; /**< Name of the original file. */
1975 unsigned rename_on_close
:1; /**< Are we using the temporary file or not? */
1976 unsigned binary
:1; /**< Did we open in binary mode? */
1977 int fd
; /**< fd for the open file. */
1978 FILE *stdio_file
; /**< stdio wrapper for <b>fd</b>. */
1981 /** Try to start writing to the file in <b>fname</b>, passing the flags
1982 * <b>open_flags</b> to the open() syscall, creating the file (if needed) with
1983 * access value <b>mode</b>. If the O_APPEND flag is set, we append to the
1984 * original file. Otherwise, we open a new temporary file in the same
1985 * directory, and either replace the original or remove the temporary file
1988 * Return the fd for the newly opened file, and store working data in
1989 * *<b>data_out</b>. The caller should not close the fd manually:
1990 * instead, call finish_writing_to_file() or abort_writing_to_file().
1991 * Returns -1 on failure.
1993 * NOTE: When not appending, the flags O_CREAT and O_TRUNC are treated
1994 * as true and the flag O_EXCL is treated as false.
1996 * NOTE: Ordinarily, O_APPEND means "seek to the end of the file before each
1997 * write()". We don't do that.
2000 start_writing_to_file(const char *fname
, int open_flags
, int mode
,
2001 open_file_t
**data_out
)
2003 open_file_t
*new_file
= tor_malloc_zero(sizeof(open_file_t
));
2004 const char *open_name
;
2008 tor_assert(data_out
);
2009 #if (O_BINARY != 0 && O_TEXT != 0)
2010 tor_assert((open_flags
& (O_BINARY
|O_TEXT
)) != 0);
2013 new_file
->filename
= tor_strdup(fname
);
2014 if (open_flags
& O_APPEND
) {
2016 new_file
->rename_on_close
= 0;
2018 open_flags
&= ~O_APPEND
;
2020 tor_asprintf(&new_file
->tempname
, "%s.tmp", fname
);
2021 open_name
= new_file
->tempname
;
2022 /* We always replace an existing temporary file if there is one. */
2023 open_flags
|= O_CREAT
|O_TRUNC
;
2024 open_flags
&= ~O_EXCL
;
2025 new_file
->rename_on_close
= 1;
2027 if (open_flags
& O_BINARY
)
2028 new_file
->binary
= 1;
2030 new_file
->fd
= tor_open_cloexec(open_name
, open_flags
, mode
);
2031 if (new_file
->fd
< 0) {
2032 log_warn(LD_FS
, "Couldn't open \"%s\" (%s) for writing: %s",
2033 open_name
, fname
, strerror(errno
));
2037 if (tor_fd_seekend(new_file
->fd
) < 0) {
2038 log_warn(LD_FS
, "Couldn't seek to end of file \"%s\": %s", open_name
,
2044 *data_out
= new_file
;
2046 return new_file
->fd
;
2049 if (new_file
->fd
>= 0)
2050 close(new_file
->fd
);
2052 tor_free(new_file
->filename
);
2053 tor_free(new_file
->tempname
);
2058 /** Given <b>file_data</b> from start_writing_to_file(), return a stdio FILE*
2059 * that can be used to write to the same file. The caller should not mix
2060 * stdio calls with non-stdio calls. */
2062 fdopen_file(open_file_t
*file_data
)
2064 tor_assert(file_data
);
2065 if (file_data
->stdio_file
)
2066 return file_data
->stdio_file
;
2067 tor_assert(file_data
->fd
>= 0);
2068 if (!(file_data
->stdio_file
= fdopen(file_data
->fd
,
2069 file_data
->binary
?"ab":"a"))) {
2070 log_warn(LD_FS
, "Couldn't fdopen \"%s\" [%d]: %s", file_data
->filename
,
2071 file_data
->fd
, strerror(errno
));
2073 return file_data
->stdio_file
;
2076 /** Combines start_writing_to_file with fdopen_file(): arguments are as
2077 * for start_writing_to_file, but */
2079 start_writing_to_stdio_file(const char *fname
, int open_flags
, int mode
,
2080 open_file_t
**data_out
)
2083 if (start_writing_to_file(fname
, open_flags
, mode
, data_out
)<0)
2085 if (!(res
= fdopen_file(*data_out
))) {
2086 abort_writing_to_file(*data_out
);
2092 /** Helper function: close and free the underlying file and memory in
2093 * <b>file_data</b>. If we were writing into a temporary file, then delete
2094 * that file (if abort_write is true) or replaces the target file with
2095 * the temporary file (if abort_write is false). */
2097 finish_writing_to_file_impl(open_file_t
*file_data
, int abort_write
)
2100 tor_assert(file_data
&& file_data
->filename
);
2101 if (file_data
->stdio_file
) {
2102 if (fclose(file_data
->stdio_file
)) {
2103 log_warn(LD_FS
, "Error closing \"%s\": %s", file_data
->filename
,
2105 abort_write
= r
= -1;
2107 } else if (file_data
->fd
>= 0 && close(file_data
->fd
) < 0) {
2108 log_warn(LD_FS
, "Error flushing \"%s\": %s", file_data
->filename
,
2110 abort_write
= r
= -1;
2113 if (file_data
->rename_on_close
) {
2114 tor_assert(file_data
->tempname
&& file_data
->filename
);
2116 unlink(file_data
->tempname
);
2118 tor_assert(strcmp(file_data
->filename
, file_data
->tempname
));
2119 if (replace_file(file_data
->tempname
, file_data
->filename
)) {
2120 log_warn(LD_FS
, "Error replacing \"%s\": %s", file_data
->filename
,
2127 tor_free(file_data
->filename
);
2128 tor_free(file_data
->tempname
);
2129 tor_free(file_data
);
2134 /** Finish writing to <b>file_data</b>: close the file handle, free memory as
2135 * needed, and if using a temporary file, replace the original file with
2136 * the temporary file. */
2138 finish_writing_to_file(open_file_t
*file_data
)
2140 return finish_writing_to_file_impl(file_data
, 0);
2143 /** Finish writing to <b>file_data</b>: close the file handle, free memory as
2144 * needed, and if using a temporary file, delete it. */
2146 abort_writing_to_file(open_file_t
*file_data
)
2148 return finish_writing_to_file_impl(file_data
, 1);
2151 /** Helper: given a set of flags as passed to open(2), open the file
2152 * <b>fname</b> and write all the sized_chunk_t structs in <b>chunks</b> to
2153 * the file. Do so as atomically as possible e.g. by opening temp files and
2156 write_chunks_to_file_impl(const char *fname
, const smartlist_t
*chunks
,
2159 open_file_t
*file
= NULL
;
2162 fd
= start_writing_to_file(fname
, open_flags
, 0600, &file
);
2165 SMARTLIST_FOREACH(chunks
, sized_chunk_t
*, chunk
,
2167 result
= write_all(fd
, chunk
->bytes
, chunk
->len
, 0);
2169 log_warn(LD_FS
, "Error writing to \"%s\": %s", fname
,
2173 tor_assert((size_t)result
== chunk
->len
);
2176 return finish_writing_to_file(file
);
2178 abort_writing_to_file(file
);
2182 /** Given a smartlist of sized_chunk_t, write them atomically to a file
2183 * <b>fname</b>, overwriting or creating the file as necessary. */
2185 write_chunks_to_file(const char *fname
, const smartlist_t
*chunks
, int bin
)
2187 int flags
= OPEN_FLAGS_REPLACE
|(bin
?O_BINARY
:O_TEXT
);
2188 return write_chunks_to_file_impl(fname
, chunks
, flags
);
2191 /** Write <b>len</b> bytes, starting at <b>str</b>, to <b>fname</b>
2192 using the open() flags passed in <b>flags</b>. */
2194 write_bytes_to_file_impl(const char *fname
, const char *str
, size_t len
,
2198 sized_chunk_t c
= { str
, len
};
2199 smartlist_t
*chunks
= smartlist_new();
2200 smartlist_add(chunks
, &c
);
2201 r
= write_chunks_to_file_impl(fname
, chunks
, flags
);
2202 smartlist_free(chunks
);
2206 /** As write_str_to_file, but does not assume a NUL-terminated
2207 * string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
2209 write_bytes_to_file(const char *fname
, const char *str
, size_t len
,
2212 return write_bytes_to_file_impl(fname
, str
, len
,
2213 OPEN_FLAGS_REPLACE
|(bin
?O_BINARY
:O_TEXT
));
2216 /** As write_bytes_to_file, but if the file already exists, append the bytes
2217 * to the end of the file instead of overwriting it. */
2219 append_bytes_to_file(const char *fname
, const char *str
, size_t len
,
2222 return write_bytes_to_file_impl(fname
, str
, len
,
2223 OPEN_FLAGS_APPEND
|(bin
?O_BINARY
:O_TEXT
));
2226 /** Like write_str_to_file(), but also return -1 if there was a file
2227 already residing in <b>fname</b>. */
2229 write_bytes_to_new_file(const char *fname
, const char *str
, size_t len
,
2232 return write_bytes_to_file_impl(fname
, str
, len
,
2233 OPEN_FLAGS_DONT_REPLACE
|
2234 (bin
?O_BINARY
:O_TEXT
));
2238 * Read the contents of the open file <b>fd</b> presuming it is a FIFO
2239 * (or similar) file descriptor for which the size of the file isn't
2240 * known ahead of time. Return NULL on failure, and a NUL-terminated
2241 * string on success. On success, set <b>sz_out</b> to the number of
2245 read_file_to_str_until_eof(int fd
, size_t max_bytes_to_read
, size_t *sz_out
)
2249 char *string
= NULL
;
2250 size_t string_max
= 0;
2252 if (max_bytes_to_read
+1 >= SIZE_T_CEILING
)
2256 /* XXXX This "add 1K" approach is a little goofy; if we care about
2257 * performance here, we should be doubling. But in practice we shouldn't
2258 * be using this function on big files anyway. */
2259 string_max
= pos
+ 1024;
2260 if (string_max
> max_bytes_to_read
)
2261 string_max
= max_bytes_to_read
+ 1;
2262 string
= tor_realloc(string
, string_max
);
2263 r
= read(fd
, string
+ pos
, string_max
- pos
- 1);
2270 } while (r
> 0 && pos
< max_bytes_to_read
);
2277 /** Read the contents of <b>filename</b> into a newly allocated
2278 * string; return the string on success or NULL on failure.
2280 * If <b>stat_out</b> is provided, store the result of stat()ing the
2281 * file into <b>stat_out</b>.
2283 * If <b>flags</b> & RFTS_BIN, open the file in binary mode.
2284 * If <b>flags</b> & RFTS_IGNORE_MISSING, don't warn if the file
2288 * This function <em>may</em> return an erroneous result if the file
2289 * is modified while it is running, but must not crash or overflow.
2290 * Right now, the error case occurs when the file length grows between
2291 * the call to stat and the call to read_all: the resulting string will
2295 read_file_to_str(const char *filename
, int flags
, struct stat
*stat_out
)
2297 int fd
; /* router file */
2298 struct stat statbuf
;
2301 int bin
= flags
& RFTS_BIN
;
2303 tor_assert(filename
);
2305 fd
= tor_open_cloexec(filename
,O_RDONLY
|(bin
?O_BINARY
:O_TEXT
),0);
2307 int severity
= LOG_WARN
;
2308 int save_errno
= errno
;
2309 if (errno
== ENOENT
&& (flags
& RFTS_IGNORE_MISSING
))
2310 severity
= LOG_INFO
;
2311 log_fn(severity
, LD_FS
,"Could not open \"%s\": %s",filename
,
2317 if (fstat(fd
, &statbuf
)<0) {
2318 int save_errno
= errno
;
2320 log_warn(LD_FS
,"Could not fstat \"%s\".",filename
);
2326 /** When we detect that we're reading from a FIFO, don't read more than
2327 * this many bytes. It's insane overkill for most uses. */
2328 #define FIFO_READ_MAX (1024*1024)
2329 if (S_ISFIFO(statbuf
.st_mode
)) {
2331 string
= read_file_to_str_until_eof(fd
, FIFO_READ_MAX
, &sz
);
2332 if (string
&& stat_out
) {
2333 statbuf
.st_size
= sz
;
2334 memcpy(stat_out
, &statbuf
, sizeof(struct stat
));
2341 if ((uint64_t)(statbuf
.st_size
)+1 >= SIZE_T_CEILING
) {
2346 string
= tor_malloc((size_t)(statbuf
.st_size
+1));
2348 r
= read_all(fd
,string
,(size_t)statbuf
.st_size
,0);
2350 int save_errno
= errno
;
2351 log_warn(LD_FS
,"Error reading from file \"%s\": %s", filename
,
2358 string
[r
] = '\0'; /* NUL-terminate the result. */
2360 #if defined(_WIN32) || defined(__CYGWIN__)
2361 if (!bin
&& strchr(string
, '\r')) {
2362 log_debug(LD_FS
, "We didn't convert CRLF to LF as well as we hoped "
2363 "when reading %s. Coping.",
2365 tor_strstrip(string
, "\r");
2369 statbuf
.st_size
= (size_t) r
;
2372 if (r
!= statbuf
.st_size
) {
2373 /* Unless we're using text mode on win32, we'd better have an exact
2374 * match for size. */
2375 int save_errno
= errno
;
2376 log_warn(LD_FS
,"Could read only %d of %ld bytes of file \"%s\".",
2377 (int)r
, (long)statbuf
.st_size
,filename
);
2385 memcpy(stat_out
, &statbuf
, sizeof(struct stat
));
2391 #define TOR_ISODIGIT(c) ('0' <= (c) && (c) <= '7')
2393 /** Given a c-style double-quoted escaped string in <b>s</b>, extract and
2394 * decode its contents into a newly allocated string. On success, assign this
2395 * string to *<b>result</b>, assign its length to <b>size_out</b> (if
2396 * provided), and return a pointer to the position in <b>s</b> immediately
2397 * after the string. On failure, return NULL.
2400 unescape_string(const char *s
, char **result
, size_t *size_out
)
2415 if (cp
[1] == 'x' || cp
[1] == 'X') {
2416 if (!(TOR_ISXDIGIT(cp
[2]) && TOR_ISXDIGIT(cp
[3])))
2419 } else if (TOR_ISODIGIT(cp
[1])) {
2421 if (TOR_ISODIGIT(*cp
)) ++cp
;
2422 if (TOR_ISODIGIT(*cp
)) ++cp
;
2423 } else if (cp
[1] == 'n' || cp
[1] == 'r' || cp
[1] == 't' || cp
[1] == '"'
2424 || cp
[1] == '\\' || cp
[1] == '\'') {
2436 out
= *result
= tor_malloc(cp
-s
+ 1);
2443 if (size_out
) *size_out
= out
- *result
;
2446 tor_fragile_assert();
2452 case 'n': *out
++ = '\n'; cp
+= 2; break;
2453 case 'r': *out
++ = '\r'; cp
+= 2; break;
2454 case 't': *out
++ = '\t'; cp
+= 2; break;
2459 x1
= hex_decode_digit(cp
[2]);
2460 x2
= hex_decode_digit(cp
[3]);
2461 if (x1
== -1 || x2
== -1) {
2466 *out
++ = ((x1
<<4) + x2
);
2470 case '0': case '1': case '2': case '3': case '4': case '5':
2475 if (TOR_ISODIGIT(*cp
)) { n
= n
*8 + *cp
-'0'; cp
++; }
2476 if (TOR_ISODIGIT(*cp
)) { n
= n
*8 + *cp
-'0'; cp
++; }
2477 if (n
> 255) { tor_free(*result
); return NULL
; }
2489 tor_free(*result
); return NULL
;
2498 /** Given a string containing part of a configuration file or similar format,
2499 * advance past comments and whitespace and try to parse a single line. If we
2500 * parse a line successfully, set *<b>key_out</b> to a new string holding the
2501 * key portion and *<b>value_out</b> to a new string holding the value portion
2502 * of the line, and return a pointer to the start of the next line. If we run
2503 * out of data, return a pointer to the end of the string. If we encounter an
2504 * error, return NULL.
2507 parse_config_line_from_str(const char *line
, char **key_out
, char **value_out
)
2509 /* I believe the file format here is supposed to be:
2510 FILE = (EMPTYLINE | LINE)* (EMPTYLASTLINE | LASTLINE)?
2512 EMPTYLASTLINE = SPACE* | COMMENT
2513 EMPTYLINE = EMPTYLASTLINE NL
2514 SPACE = ' ' | '\r' | '\t'
2515 COMMENT = '#' NOT-NL*
2516 NOT-NL = Any character except '\n'
2519 LASTLINE = SPACE* KEY SPACE* VALUES
2522 KEYCHAR = Any character except ' ', '\r', '\n', '\t', '#', "\"
2524 VALUES = QUOTEDVALUE | NORMALVALUE
2525 QUOTEDVALUE = QUOTE QVCHAR* QUOTE EOLSPACE?
2527 QVCHAR = KEYCHAR | ESC ('n' | 't' | 'r' | '"' | ESC |'\'' | OCTAL | HEX)
2529 OCTAL = ODIGIT (ODIGIT ODIGIT?)?
2530 HEX = ('x' | 'X') HEXDIGIT HEXDIGIT
2532 HEXDIGIT = '0'..'9' | 'a' .. 'f' | 'A' .. 'F'
2533 EOLSPACE = SPACE* COMMENT?
2535 NORMALVALUE = (VALCHAR | ESC ESC_IGNORE | CONTINUATION)* EOLSPACE?
2536 VALCHAR = Any character except ESC, '#', and '\n'
2537 ESC_IGNORE = Any character except '#' or '\n'
2538 CONTINUATION = ESC NL ( COMMENT NL )*
2541 const char *key
, *val
, *cp
;
2542 int continuation
= 0;
2544 tor_assert(key_out
);
2545 tor_assert(value_out
);
2547 *key_out
= *value_out
= NULL
;
2549 /* Skip until the first keyword. */
2551 while (TOR_ISSPACE(*line
))
2554 while (*line
&& *line
!= '\n')
2561 if (!*line
) { /* End of string? */
2562 *key_out
= *value_out
= NULL
;
2566 /* Skip until the next space or \ followed by newline. */
2568 while (*line
&& !TOR_ISSPACE(*line
) && *line
!= '#' &&
2569 ! (line
[0] == '\\' && line
[1] == '\n'))
2571 *key_out
= tor_strndup(key
, line
-key
);
2573 /* Skip until the value. */
2574 while (*line
== ' ' || *line
== '\t')
2579 /* Find the end of the line. */
2580 if (*line
== '\"') { // XXX No continuation handling is done here
2581 if (!(line
= unescape_string(line
, value_out
, NULL
)))
2583 while (*line
== ' ' || *line
== '\t')
2585 if (*line
&& *line
!= '#' && *line
!= '\n')
2588 /* Look for the end of the line. */
2589 while (*line
&& *line
!= '\n' && (*line
!= '#' || continuation
)) {
2590 if (*line
== '\\' && line
[1] == '\n') {
2593 } else if (*line
== '#') {
2596 } while (*line
&& *line
!= '\n');
2604 if (*line
== '\n') {
2609 /* Now back cp up to be the last nonspace character */
2610 while (cp
>val
&& TOR_ISSPACE(*(cp
-1)))
2613 tor_assert(cp
>= val
);
2615 /* Now copy out and decode the value. */
2616 *value_out
= tor_strndup(val
, cp
-val
);
2619 v_out
= v_in
= *value_out
;
2624 } while (*v_in
&& *v_in
!= '\n');
2627 } else if (v_in
[0] == '\\' && v_in
[1] == '\n') {
2640 } while (*line
&& *line
!= '\n');
2642 while (TOR_ISSPACE(*line
)) ++line
;
2647 /** Expand any homedir prefix on <b>filename</b>; return a newly allocated
2650 expand_filename(const char *filename
)
2652 tor_assert(filename
);
2654 return tor_strdup(filename
);
2656 if (*filename
== '~') {
2657 char *home
, *result
=NULL
;
2660 if (filename
[1] == '/' || filename
[1] == '\0') {
2661 home
= getenv("HOME");
2663 log_warn(LD_CONFIG
, "Couldn't find $HOME environment variable while "
2664 "expanding \"%s\"; defaulting to \"\".", filename
);
2665 home
= tor_strdup("");
2667 home
= tor_strdup(home
);
2669 rest
= strlen(filename
)>=2?(filename
+2):"";
2672 char *username
, *slash
;
2673 slash
= strchr(filename
, '/');
2675 username
= tor_strndup(filename
+1,slash
-filename
-1);
2677 username
= tor_strdup(filename
+1);
2678 if (!(home
= get_user_homedir(username
))) {
2679 log_warn(LD_CONFIG
,"Couldn't get homedir for \"%s\"",username
);
2684 rest
= slash
? (slash
+1) : "";
2686 log_warn(LD_CONFIG
, "Couldn't expend homedir on system without pwd.h");
2687 return tor_strdup(filename
);
2691 /* Remove trailing slash. */
2692 if (strlen(home
)>1 && !strcmpend(home
,PATH_SEPARATOR
)) {
2693 home
[strlen(home
)-1] = '\0';
2695 tor_asprintf(&result
,"%s"PATH_SEPARATOR
"%s",home
,rest
);
2699 return tor_strdup(filename
);
2704 #define MAX_SCANF_WIDTH 9999
2706 /** Helper: given an ASCII-encoded decimal digit, return its numeric value.
2707 * NOTE: requires that its input be in-bounds. */
2709 digit_to_num(char d
)
2711 int num
= ((int)d
) - (int)'0';
2712 tor_assert(num
<= 9 && num
>= 0);
2716 /** Helper: Read an unsigned int from *<b>bufp</b> of up to <b>width</b>
2717 * characters. (Handle arbitrary width if <b>width</b> is less than 0.) On
2718 * success, store the result in <b>out</b>, advance bufp to the next
2719 * character, and return 0. On failure, return -1. */
2721 scan_unsigned(const char **bufp
, unsigned long *out
, int width
, int base
)
2723 unsigned long result
= 0;
2724 int scanned_so_far
= 0;
2725 const int hex
= base
==16;
2726 tor_assert(base
== 10 || base
== 16);
2727 if (!bufp
|| !*bufp
|| !out
)
2730 width
=MAX_SCANF_WIDTH
;
2732 while (**bufp
&& (hex
?TOR_ISXDIGIT(**bufp
):TOR_ISDIGIT(**bufp
))
2733 && scanned_so_far
< width
) {
2734 int digit
= hex
?hex_decode_digit(*(*bufp
)++):digit_to_num(*(*bufp
)++);
2735 unsigned long new_result
= result
* base
+ digit
;
2736 if (new_result
< result
)
2737 return -1; /* over/underflow. */
2738 result
= new_result
;
2742 if (!scanned_so_far
) /* No actual digits scanned */
2749 /** Helper: Read an signed int from *<b>bufp</b> of up to <b>width</b>
2750 * characters. (Handle arbitrary width if <b>width</b> is less than 0.) On
2751 * success, store the result in <b>out</b>, advance bufp to the next
2752 * character, and return 0. On failure, return -1. */
2754 scan_signed(const char **bufp
, long *out
, int width
)
2757 unsigned long result
= 0;
2759 if (!bufp
|| !*bufp
|| !out
)
2762 width
=MAX_SCANF_WIDTH
;
2764 if (**bufp
== '-') {
2770 if (scan_unsigned(bufp
, &result
, width
, 10) < 0)
2774 if (result
> ((unsigned long)LONG_MAX
) + 1)
2775 return -1; /* Underflow */
2776 *out
= -(long)result
;
2778 if (result
> LONG_MAX
)
2779 return -1; /* Overflow */
2780 *out
= (long)result
;
2786 /** Helper: Read a decimal-formatted double from *<b>bufp</b> of up to
2787 * <b>width</b> characters. (Handle arbitrary width if <b>width</b> is less
2788 * than 0.) On success, store the result in <b>out</b>, advance bufp to the
2789 * next character, and return 0. On failure, return -1. */
2791 scan_double(const char **bufp
, double *out
, int width
)
2795 int scanned_so_far
= 0;
2797 if (!bufp
|| !*bufp
|| !out
)
2800 width
=MAX_SCANF_WIDTH
;
2802 if (**bufp
== '-') {
2807 while (**bufp
&& TOR_ISDIGIT(**bufp
) && scanned_so_far
< width
) {
2808 const int digit
= digit_to_num(*(*bufp
)++);
2809 result
= result
* 10 + digit
;
2812 if (**bufp
== '.') {
2813 double fracval
= 0, denominator
= 1;
2816 while (**bufp
&& TOR_ISDIGIT(**bufp
) && scanned_so_far
< width
) {
2817 const int digit
= digit_to_num(*(*bufp
)++);
2818 fracval
= fracval
* 10 + digit
;
2822 result
+= fracval
/ denominator
;
2825 if (!scanned_so_far
) /* No actual digits scanned */
2828 *out
= neg
? -result
: result
;
2832 /** Helper: copy up to <b>width</b> non-space characters from <b>bufp</b> to
2833 * <b>out</b>. Make sure <b>out</b> is nul-terminated. Advance <b>bufp</b>
2834 * to the next non-space character or the EOS. */
2836 scan_string(const char **bufp
, char *out
, int width
)
2838 int scanned_so_far
= 0;
2839 if (!bufp
|| !out
|| width
< 0)
2841 while (**bufp
&& ! TOR_ISSPACE(**bufp
) && scanned_so_far
< width
) {
2842 *out
++ = *(*bufp
)++;
2849 /** Locale-independent, minimal, no-surprises scanf variant, accepting only a
2850 * restricted pattern format. For more info on what it supports, see
2851 * tor_sscanf() documentation. */
2853 tor_vsscanf(const char *buf
, const char *pattern
, va_list ap
)
2858 if (*pattern
!= '%') {
2859 if (*buf
== *pattern
) {
2870 if (TOR_ISDIGIT(*pattern
)) {
2871 width
= digit_to_num(*pattern
++);
2872 while (TOR_ISDIGIT(*pattern
)) {
2874 width
+= digit_to_num(*pattern
++);
2875 if (width
> MAX_SCANF_WIDTH
)
2878 if (!width
) /* No zero-width things. */
2881 if (*pattern
== 'l') {
2885 if (*pattern
== 'u' || *pattern
== 'x') {
2887 const int base
= (*pattern
== 'u') ? 10 : 16;
2890 if (scan_unsigned(&buf
, &u
, width
, base
)<0)
2893 unsigned long *out
= va_arg(ap
, unsigned long *);
2896 unsigned *out
= va_arg(ap
, unsigned *);
2899 *out
= (unsigned) u
;
2903 } else if (*pattern
== 'f') {
2904 double *d
= va_arg(ap
, double *);
2906 return -1; /* float not supported */
2909 if (scan_double(&buf
, d
, width
)<0)
2913 } else if (*pattern
== 'd') {
2915 if (scan_signed(&buf
, &lng
, width
)<0)
2918 long *out
= va_arg(ap
, long *);
2921 int *out
= va_arg(ap
, int *);
2922 if (lng
< INT_MIN
|| lng
> INT_MAX
)
2928 } else if (*pattern
== 's') {
2929 char *s
= va_arg(ap
, char *);
2934 if (scan_string(&buf
, s
, width
)<0)
2938 } else if (*pattern
== 'c') {
2939 char *ch
= va_arg(ap
, char *);
2949 } else if (*pattern
== '%') {
2957 return -1; /* Unrecognized pattern component. */
2965 /** Minimal sscanf replacement: parse <b>buf</b> according to <b>pattern</b>
2966 * and store the results in the corresponding argument fields. Differs from
2968 * <ul><li>It only handles %u, %lu, %x, %lx, %<NUM>s, %d, %ld, %lf, and %c.
2969 * <li>It only handles decimal inputs for %lf. (12.3, not 1.23e1)
2970 * <li>It does not handle arbitrarily long widths.
2971 * <li>Numbers do not consume any space characters.
2972 * <li>It is locale-independent.
2973 * <li>%u and %x do not consume any space.
2974 * <li>It returns -1 on malformed patterns.</ul>
2976 * (As with other locale-independent functions, we need this to parse data that
2977 * is in ASCII without worrying that the C library's locale-handling will make
2978 * miscellaneous characters look like numbers, spaces, and so on.)
2981 tor_sscanf(const char *buf
, const char *pattern
, ...)
2985 va_start(ap
, pattern
);
2986 r
= tor_vsscanf(buf
, pattern
, ap
);
2991 /** Append the string produced by tor_asprintf(<b>pattern</b>, <b>...</b>)
2994 smartlist_add_asprintf(struct smartlist_t
*sl
, const char *pattern
, ...)
2997 va_start(ap
, pattern
);
2998 smartlist_add_vasprintf(sl
, pattern
, ap
);
3002 /** va_list-based backend of smartlist_add_asprintf. */
3004 smartlist_add_vasprintf(struct smartlist_t
*sl
, const char *pattern
,
3009 tor_vasprintf(&str
, pattern
, args
);
3010 tor_assert(str
!= NULL
);
3012 smartlist_add(sl
, str
);
3015 /** Return a new list containing the filenames in the directory <b>dirname</b>.
3016 * Return NULL on error or if <b>dirname</b> is not a directory.
3019 tor_listdir(const char *dirname
)
3021 smartlist_t
*result
;
3024 TCHAR tpattern
[MAX_PATH
] = {0};
3025 char name
[MAX_PATH
*2+1] = {0};
3027 WIN32_FIND_DATA findData
;
3028 tor_asprintf(&pattern
, "%s\\*", dirname
);
3030 mbstowcs(tpattern
,pattern
,MAX_PATH
);
3032 strlcpy(tpattern
, pattern
, MAX_PATH
);
3034 if (INVALID_HANDLE_VALUE
== (handle
= FindFirstFile(tpattern
, &findData
))) {
3038 result
= smartlist_new();
3041 wcstombs(name
,findData
.cFileName
,MAX_PATH
);
3042 name
[sizeof(name
)-1] = '\0';
3044 strlcpy(name
,findData
.cFileName
,sizeof(name
));
3046 if (strcmp(name
, ".") &&
3047 strcmp(name
, "..")) {
3048 smartlist_add(result
, tor_strdup(name
));
3050 if (!FindNextFile(handle
, &findData
)) {
3052 if ((err
= GetLastError()) != ERROR_NO_MORE_FILES
) {
3053 char *errstr
= format_win32_error(err
);
3054 log_warn(LD_FS
, "Error reading directory '%s': %s", dirname
, errstr
);
3065 if (!(d
= opendir(dirname
)))
3068 result
= smartlist_new();
3069 while ((de
= readdir(d
))) {
3070 if (!strcmp(de
->d_name
, ".") ||
3071 !strcmp(de
->d_name
, ".."))
3073 smartlist_add(result
, tor_strdup(de
->d_name
));
3080 /** Return true iff <b>filename</b> is a relative path. */
3082 path_is_relative(const char *filename
)
3084 if (filename
&& filename
[0] == '/')
3087 else if (filename
&& filename
[0] == '\\')
3089 else if (filename
&& strlen(filename
)>3 && TOR_ISALPHA(filename
[0]) &&
3090 filename
[1] == ':' && filename
[2] == '\\')
3102 /* Based on code contributed by christian grothoff */
3103 /** True iff we've called start_daemon(). */
3104 static int start_daemon_called
= 0;
3105 /** True iff we've called finish_daemon(). */
3106 static int finish_daemon_called
= 0;
3107 /** Socketpair used to communicate between parent and child process while
3109 static int daemon_filedes
[2];
3110 /** Start putting the process into daemon mode: fork and drop all resources
3111 * except standard fds. The parent process never returns, but stays around
3112 * until finish_daemon is called. (Note: it's safe to call this more
3113 * than once: calls after the first are ignored.)
3120 if (start_daemon_called
)
3122 start_daemon_called
= 1;
3124 if (pipe(daemon_filedes
)) {
3125 log_err(LD_GENERAL
,"pipe failed; exiting. Error was %s", strerror(errno
));
3130 log_err(LD_GENERAL
,"fork failed. Exiting.");
3133 if (pid
) { /* Parent */
3137 close(daemon_filedes
[1]); /* we only read */
3139 while (0 < read(daemon_filedes
[0], &c
, sizeof(char))) {
3147 exit(1); /* child reported error */
3148 } else { /* Child */
3149 close(daemon_filedes
[0]); /* we only write */
3151 pid
= setsid(); /* Detach from controlling terminal */
3153 * Fork one more time, so the parent (the session group leader) can exit.
3154 * This means that we, as a non-session group leader, can never regain a
3155 * controlling terminal. This part is recommended by Stevens's
3156 * _Advanced Programming in the Unix Environment_.
3161 set_main_thread(); /* We are now the main thread. */
3167 /** Finish putting the process into daemon mode: drop standard fds, and tell
3168 * the parent process to exit. (Note: it's safe to call this more than once:
3169 * calls after the first are ignored. Calls start_daemon first if it hasn't
3170 * been called already.)
3173 finish_daemon(const char *desired_cwd
)
3177 if (finish_daemon_called
)
3179 if (!start_daemon_called
)
3181 finish_daemon_called
= 1;
3185 /* Don't hold the wrong FS mounted */
3186 if (chdir(desired_cwd
) < 0) {
3187 log_err(LD_GENERAL
,"chdir to \"%s\" failed. Exiting.",desired_cwd
);
3191 nullfd
= tor_open_cloexec("/dev/null", O_RDWR
, 0);
3193 log_err(LD_GENERAL
,"/dev/null can't be opened. Exiting.");
3196 /* close fds linking to invoking terminal, but
3197 * close usual incoming fds, but redirect them somewhere
3198 * useful so the fds don't get reallocated elsewhere.
3200 if (dup2(nullfd
,0) < 0 ||
3201 dup2(nullfd
,1) < 0 ||
3202 dup2(nullfd
,2) < 0) {
3203 log_err(LD_GENERAL
,"dup2 failed. Exiting.");
3208 /* signal success */
3209 if (write(daemon_filedes
[1], &c
, sizeof(char)) != sizeof(char)) {
3210 log_err(LD_GENERAL
,"write failed. Exiting.");
3212 close(daemon_filedes
[1]);
3215 /* defined(_WIN32) */
3221 finish_daemon(const char *cp
)
3227 /** Write the current process ID, followed by NL, into <b>filename</b>.
3230 write_pidfile(char *filename
)
3234 if ((pidfile
= fopen(filename
, "w")) == NULL
) {
3235 log_warn(LD_FS
, "Unable to open \"%s\" for writing: %s", filename
,
3239 fprintf(pidfile
, "%d\n", (int)_getpid());
3241 fprintf(pidfile
, "%d\n", (int)getpid());
3249 load_windows_system_library(const TCHAR
*library_name
)
3251 TCHAR path
[MAX_PATH
];
3253 n
= GetSystemDirectory(path
, MAX_PATH
);
3254 if (n
== 0 || n
+ _tcslen(library_name
) + 2 >= MAX_PATH
)
3256 _tcscat(path
, TEXT("\\"));
3257 _tcscat(path
, library_name
);
3258 return LoadLibrary(path
);
3262 /** Format a single argument for being put on a Windows command line.
3263 * Returns a newly allocated string */
3265 format_win_cmdline_argument(const char *arg
)
3267 char *formatted_arg
;
3272 /* Backslash we can point to when one is inserted into the string */
3273 const char backslash
= '\\';
3275 /* Smartlist of *char */
3276 smartlist_t
*arg_chars
;
3277 arg_chars
= smartlist_new();
3279 /* Quote string if it contains whitespace or is empty */
3280 need_quotes
= (strchr(arg
, ' ') || strchr(arg
, '\t') || '\0' == arg
[0]);
3282 /* Build up smartlist of *chars */
3283 for (c
=arg
; *c
!= '\0'; c
++) {
3285 /* Double up backslashes preceding a quote */
3286 for (i
=0; i
<(bs_counter
*2); i
++)
3287 smartlist_add(arg_chars
, (void*)&backslash
);
3289 /* Escape the quote */
3290 smartlist_add(arg_chars
, (void*)&backslash
);
3291 smartlist_add(arg_chars
, (void*)c
);
3292 } else if ('\\' == *c
) {
3293 /* Count backslashes until we know whether to double up */
3296 /* Don't double up slashes preceding a non-quote */
3297 for (i
=0; i
<bs_counter
; i
++)
3298 smartlist_add(arg_chars
, (void*)&backslash
);
3300 smartlist_add(arg_chars
, (void*)c
);
3303 /* Don't double up trailing backslashes */
3304 for (i
=0; i
<bs_counter
; i
++)
3305 smartlist_add(arg_chars
, (void*)&backslash
);
3307 /* Allocate space for argument, quotes (if needed), and terminator */
3308 formatted_arg
= tor_malloc(sizeof(char) *
3309 (smartlist_len(arg_chars
) + (need_quotes
?2:0) + 1));
3311 /* Add leading quote */
3314 formatted_arg
[i
++] = '"';
3316 /* Add characters */
3317 SMARTLIST_FOREACH(arg_chars
, char*, c
,
3319 formatted_arg
[i
++] = *c
;
3322 /* Add trailing quote */
3324 formatted_arg
[i
++] = '"';
3325 formatted_arg
[i
] = '\0';
3327 smartlist_free(arg_chars
);
3328 return formatted_arg
;
3331 /** Format a command line for use on Windows, which takes the command as a
3332 * string rather than string array. Follows the rules from "Parsing C++
3333 * Command-Line Arguments" in MSDN. Algorithm based on list2cmdline in the
3334 * Python subprocess module. Returns a newly allocated string */
3336 tor_join_win_cmdline(const char *argv
[])
3338 smartlist_t
*argv_list
;
3342 /* Format each argument and put the result in a smartlist */
3343 argv_list
= smartlist_new();
3344 for (i
=0; argv
[i
] != NULL
; i
++) {
3345 smartlist_add(argv_list
, (void *)format_win_cmdline_argument(argv
[i
]));
3348 /* Join the arguments with whitespace */
3349 joined_argv
= smartlist_join_strings(argv_list
, " ", 0, NULL
);
3351 /* Free the newly allocated arguments, and the smartlist */
3352 SMARTLIST_FOREACH(argv_list
, char *, arg
,
3356 smartlist_free(argv_list
);
3362 * Helper function to output hex numbers, called by
3363 * format_helper_exit_status(). This writes the hexadecimal digits of x into
3364 * buf, up to max_len digits, and returns the actual number of digits written.
3365 * If there is insufficient space, it will write nothing and return 0.
3367 * This function DOES NOT add a terminating NUL character to its output: be
3370 * This accepts an unsigned int because format_helper_exit_status() needs to
3371 * call it with a signed int and an unsigned char, and since the C standard
3372 * does not guarantee that an int is wider than a char (an int must be at
3373 * least 16 bits but it is permitted for a char to be that wide as well), we
3374 * can't assume a signed int is sufficient to accomodate an unsigned char.
3375 * Thus, format_helper_exit_status() will still need to emit any require '-'
3378 * For most purposes, you'd want to use tor_snprintf("%x") instead of this
3379 * function; it's designed to be used in code paths where you can't call
3380 * arbitrary C functions.
3383 format_hex_number_for_helper_exit_status(unsigned int x
, char *buf
,
3391 if (!buf
|| max_len
<= 0)
3394 /* How many chars do we need for x? */
3406 /* Bail if we would go past the end of the buffer */
3410 /* Point to last one */
3411 cur
= buf
+ len
- 1;
3413 /* Convert x to hex */
3415 *cur
-- = "0123456789ABCDEF"[x
& 0xf];
3417 } while (x
!= 0 && cur
>= buf
);
3423 /** Format <b>child_state</b> and <b>saved_errno</b> as a hex string placed in
3424 * <b>hex_errno</b>. Called between fork and _exit, so must be signal-handler
3427 * <b>hex_errno</b> must have at least HEX_ERRNO_SIZE bytes available.
3429 * The format of <b>hex_errno</b> is: "CHILD_STATE/ERRNO\n", left-padded
3430 * with spaces. Note that there is no trailing \0. CHILD_STATE indicates where
3431 * in the processs of starting the child process did the failure occur (see
3432 * CHILD_STATE_* macros for definition), and SAVED_ERRNO is the value of
3433 * errno when the failure occurred.
3435 * On success return the number of characters added to hex_errno, not counting
3436 * the terminating NUL; return -1 on error.
3439 format_helper_exit_status(unsigned char child_state
, int saved_errno
,
3442 unsigned int unsigned_errno
;
3448 /* Fill hex_errno with spaces, and a trailing newline (memset may
3449 not be signal handler safe, so we can't use it) */
3450 for (i
= 0; i
< (HEX_ERRNO_SIZE
- 1); i
++)
3452 hex_errno
[HEX_ERRNO_SIZE
- 1] = '\n';
3454 /* Convert errno to be unsigned for hex conversion */
3455 if (saved_errno
< 0) {
3456 unsigned_errno
= (unsigned int) -saved_errno
;
3458 unsigned_errno
= (unsigned int) saved_errno
;
3462 * Count how many chars of space we have left, and keep a pointer into the
3463 * current point in the buffer.
3465 left
= HEX_ERRNO_SIZE
;
3468 /* Emit child_state */
3469 written
= format_hex_number_for_helper_exit_status(child_state
,
3474 /* Adjust left and cur */
3483 /* Adjust left and cur */
3490 if (saved_errno
< 0) {
3498 /* Emit unsigned_errno */
3499 written
= format_hex_number_for_helper_exit_status(unsigned_errno
,
3505 /* Adjust left and cur */
3509 /* Check that we have enough space left for a newline */
3513 /* Emit the newline and NUL */
3517 res
= (int)(cur
- hex_errno
- 1);
3523 * In error exit, just write a '\0' in the first char so whatever called
3524 * this at least won't fall off the end.
3532 /* Maximum number of file descriptors, if we cannot get it via sysconf() */
3533 #define DEFAULT_MAX_FD 256
3535 /** Terminate the process of <b>process_handle</b>.
3536 * Code borrowed from Python's os.kill. */
3538 tor_terminate_process(process_handle_t
*process_handle
)
3541 if (tor_get_exit_code(process_handle
, 0, NULL
) == PROCESS_EXIT_RUNNING
) {
3543 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
3544 attempt to open and terminate the process. */
3545 handle
= OpenProcess(PROCESS_ALL_ACCESS
, FALSE
,
3546 process_handle
->pid
.dwProcessId
);
3550 if (!TerminateProcess(handle
, 0))
3556 return kill(process_handle
->pid
, SIGTERM
);
3562 /** Return the Process ID of <b>process_handle</b>. */
3564 tor_process_get_pid(process_handle_t
*process_handle
)
3567 return (int) process_handle
->pid
.dwProcessId
;
3569 return (int) process_handle
->pid
;
3575 tor_process_get_stdout_pipe(process_handle_t
*process_handle
)
3577 return process_handle
->stdout_pipe
;
3580 /* DOCDOC tor_process_get_stdout_pipe */
3582 tor_process_get_stdout_pipe(process_handle_t
*process_handle
)
3584 return process_handle
->stdout_handle
;
3588 /* DOCDOC process_handle_new */
3589 static process_handle_t
*
3590 process_handle_new(void)
3592 process_handle_t
*out
= tor_malloc_zero(sizeof(process_handle_t
));
3595 out
->stdout_pipe
= INVALID_HANDLE_VALUE
;
3596 out
->stderr_pipe
= INVALID_HANDLE_VALUE
;
3598 out
->stdout_pipe
= -1;
3599 out
->stderr_pipe
= -1;
3606 * @name child-process states
3608 * Each of these values represents a possible state that a child process can
3609 * be in. They're used to determine what to say when telling the parent how
3610 * far along we were before failure.
3614 #define CHILD_STATE_INIT 0
3615 #define CHILD_STATE_PIPE 1
3616 #define CHILD_STATE_MAXFD 2
3617 #define CHILD_STATE_FORK 3
3618 #define CHILD_STATE_DUPOUT 4
3619 #define CHILD_STATE_DUPERR 5
3620 #define CHILD_STATE_REDIRECT 6
3621 #define CHILD_STATE_CLOSEFD 7
3622 #define CHILD_STATE_EXEC 8
3623 #define CHILD_STATE_FAILEXEC 9
3625 /** Start a program in the background. If <b>filename</b> contains a '/', then
3626 * it will be treated as an absolute or relative path. Otherwise, on
3627 * non-Windows systems, the system path will be searched for <b>filename</b>.
3628 * On Windows, only the current directory will be searched. Here, to search the
3629 * system path (as well as the application directory, current working
3630 * directory, and system directories), set filename to NULL.
3632 * The strings in <b>argv</b> will be passed as the command line arguments of
3633 * the child program (following convention, argv[0] should normally be the
3634 * filename of the executable, and this must be the case if <b>filename</b> is
3635 * NULL). The last element of argv must be NULL. A handle to the child process
3636 * will be returned in process_handle (which must be non-NULL). Read
3637 * process_handle.status to find out if the process was successfully launched.
3638 * For convenience, process_handle.status is returned by this function.
3640 * Some parts of this code are based on the POSIX subprocess module from
3641 * Python, and example code from
3642 * http://msdn.microsoft.com/en-us/library/ms682499%28v=vs.85%29.aspx.
3645 tor_spawn_background(const char *const filename
, const char **argv
,
3646 process_environment_t
*env
,
3647 process_handle_t
**process_handle_out
)
3650 HANDLE stdout_pipe_read
= NULL
;
3651 HANDLE stdout_pipe_write
= NULL
;
3652 HANDLE stderr_pipe_read
= NULL
;
3653 HANDLE stderr_pipe_write
= NULL
;
3654 process_handle_t
*process_handle
;
3657 STARTUPINFOA siStartInfo
;
3658 BOOL retval
= FALSE
;
3660 SECURITY_ATTRIBUTES saAttr
;
3663 saAttr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
3664 saAttr
.bInheritHandle
= TRUE
;
3665 /* TODO: should we set explicit security attributes? (#2046, comment 5) */
3666 saAttr
.lpSecurityDescriptor
= NULL
;
3668 /* Assume failure to start process */
3669 status
= PROCESS_STATUS_ERROR
;
3671 /* Set up pipe for stdout */
3672 if (!CreatePipe(&stdout_pipe_read
, &stdout_pipe_write
, &saAttr
, 0)) {
3673 log_warn(LD_GENERAL
,
3674 "Failed to create pipe for stdout communication with child process: %s",
3675 format_win32_error(GetLastError()));
3678 if (!SetHandleInformation(stdout_pipe_read
, HANDLE_FLAG_INHERIT
, 0)) {
3679 log_warn(LD_GENERAL
,
3680 "Failed to configure pipe for stdout communication with child "
3681 "process: %s", format_win32_error(GetLastError()));
3685 /* Set up pipe for stderr */
3686 if (!CreatePipe(&stderr_pipe_read
, &stderr_pipe_write
, &saAttr
, 0)) {
3687 log_warn(LD_GENERAL
,
3688 "Failed to create pipe for stderr communication with child process: %s",
3689 format_win32_error(GetLastError()));
3692 if (!SetHandleInformation(stderr_pipe_read
, HANDLE_FLAG_INHERIT
, 0)) {
3693 log_warn(LD_GENERAL
,
3694 "Failed to configure pipe for stderr communication with child "
3695 "process: %s", format_win32_error(GetLastError()));
3699 /* Create the child process */
3701 /* Windows expects argv to be a whitespace delimited string, so join argv up
3703 joined_argv
= tor_join_win_cmdline(argv
);
3705 process_handle
= process_handle_new();
3706 process_handle
->status
= status
;
3708 ZeroMemory(&(process_handle
->pid
), sizeof(PROCESS_INFORMATION
));
3709 ZeroMemory(&siStartInfo
, sizeof(STARTUPINFO
));
3710 siStartInfo
.cb
= sizeof(STARTUPINFO
);
3711 siStartInfo
.hStdError
= stderr_pipe_write
;
3712 siStartInfo
.hStdOutput
= stdout_pipe_write
;
3713 siStartInfo
.hStdInput
= NULL
;
3714 siStartInfo
.dwFlags
|= STARTF_USESTDHANDLES
;
3716 /* Create the child process */
3718 retval
= CreateProcessA(filename
, // module name
3719 joined_argv
, // command line
3720 /* TODO: should we set explicit security attributes? (#2046, comment 5) */
3721 NULL
, // process security attributes
3722 NULL
, // primary thread security attributes
3723 TRUE
, // handles are inherited
3724 /*(TODO: set CREATE_NEW CONSOLE/PROCESS_GROUP to make GetExitCodeProcess()
3726 0, // creation flags
3727 (env
==NULL
) ? NULL
: env
->windows_environment_block
,
3728 NULL
, // use parent's current directory
3729 &siStartInfo
, // STARTUPINFO pointer
3730 &(process_handle
->pid
)); // receives PROCESS_INFORMATION
3732 tor_free(joined_argv
);
3735 log_warn(LD_GENERAL
,
3736 "Failed to create child process %s: %s", filename
?filename
:argv
[0],
3737 format_win32_error(GetLastError()));
3738 tor_free(process_handle
);
3740 /* TODO: Close hProcess and hThread in process_handle->pid? */
3741 process_handle
->stdout_pipe
= stdout_pipe_read
;
3742 process_handle
->stderr_pipe
= stderr_pipe_read
;
3743 status
= process_handle
->status
= PROCESS_STATUS_RUNNING
;
3746 /* TODO: Close pipes on exit */
3747 *process_handle_out
= process_handle
;
3755 process_handle_t
*process_handle
;
3758 const char *error_message
= SPAWN_ERROR_MESSAGE
;
3759 size_t error_message_length
;
3761 /* Represents where in the process of spawning the program is;
3762 this is used for printing out the error message */
3763 unsigned char child_state
= CHILD_STATE_INIT
;
3765 char hex_errno
[HEX_ERRNO_SIZE
];
3767 static int max_fd
= -1;
3769 status
= PROCESS_STATUS_ERROR
;
3771 /* We do the strlen here because strlen() is not signal handler safe,
3772 and we are not allowed to use unsafe functions between fork and exec */
3773 error_message_length
= strlen(error_message
);
3775 child_state
= CHILD_STATE_PIPE
;
3777 /* Set up pipe for redirecting stdout and stderr of child */
3778 retval
= pipe(stdout_pipe
);
3780 log_warn(LD_GENERAL
,
3781 "Failed to set up pipe for stdout communication with child process: %s",
3786 retval
= pipe(stderr_pipe
);
3788 log_warn(LD_GENERAL
,
3789 "Failed to set up pipe for stderr communication with child process: %s",
3792 close(stdout_pipe
[0]);
3793 close(stdout_pipe
[1]);
3798 child_state
= CHILD_STATE_MAXFD
;
3802 max_fd
= (int) sysconf(_SC_OPEN_MAX
);
3804 max_fd
= DEFAULT_MAX_FD
;
3805 log_warn(LD_GENERAL
,
3806 "Cannot find maximum file descriptor, assuming %d", max_fd
);
3810 max_fd
= DEFAULT_MAX_FD
;
3813 child_state
= CHILD_STATE_FORK
;
3819 child_state
= CHILD_STATE_DUPOUT
;
3821 /* Link child stdout to the write end of the pipe */
3822 retval
= dup2(stdout_pipe
[1], STDOUT_FILENO
);
3826 child_state
= CHILD_STATE_DUPERR
;
3828 /* Link child stderr to the write end of the pipe */
3829 retval
= dup2(stderr_pipe
[1], STDERR_FILENO
);
3833 child_state
= CHILD_STATE_REDIRECT
;
3835 /* Link stdin to /dev/null */
3836 fd
= open("/dev/null", O_RDONLY
); /* NOT cloexec, obviously. */
3838 dup2(fd
, STDIN_FILENO
);
3842 child_state
= CHILD_STATE_CLOSEFD
;
3844 close(stderr_pipe
[0]);
3845 close(stderr_pipe
[1]);
3846 close(stdout_pipe
[0]);
3847 close(stdout_pipe
[1]);
3850 /* Close all other fds, including the read end of the pipe */
3851 /* XXX: We should now be doing enough FD_CLOEXEC setting to make
3853 for (fd
= STDERR_FILENO
+ 1; fd
< max_fd
; fd
++) {
3857 child_state
= CHILD_STATE_EXEC
;
3859 /* Call the requested program. We need the cast because
3860 execvp doesn't define argv as const, even though it
3861 does not modify the arguments */
3863 execve(filename
, (char *const *) argv
, env
->unixoid_environment_block
);
3865 execvp(filename
, (char *const *) argv
);
3867 /* If we got here, the exec or open(/dev/null) failed */
3869 child_state
= CHILD_STATE_FAILEXEC
;
3873 /* XXX: are we leaking fds from the pipe? */
3876 n
= format_helper_exit_status(child_state
, errno
, hex_errno
);
3879 /* Write the error message. GCC requires that we check the return
3880 value, but there is nothing we can do if it fails */
3881 /* TODO: Don't use STDOUT, use a pipe set up just for this purpose */
3882 nbytes
= write(STDOUT_FILENO
, error_message
, error_message_length
);
3883 nbytes
= write(STDOUT_FILENO
, hex_errno
, n
);
3890 /* Never reached, but avoids compiler warning */
3897 log_warn(LD_GENERAL
, "Failed to fork child process: %s", strerror(errno
));
3898 close(stdout_pipe
[0]);
3899 close(stdout_pipe
[1]);
3900 close(stderr_pipe
[0]);
3901 close(stderr_pipe
[1]);
3905 process_handle
= process_handle_new();
3906 process_handle
->status
= status
;
3907 process_handle
->pid
= pid
;
3909 /* TODO: If the child process forked but failed to exec, waitpid it */
3911 /* Return read end of the pipes to caller, and close write end */
3912 process_handle
->stdout_pipe
= stdout_pipe
[0];
3913 retval
= close(stdout_pipe
[1]);
3916 log_warn(LD_GENERAL
,
3917 "Failed to close write end of stdout pipe in parent process: %s",
3921 process_handle
->stderr_pipe
= stderr_pipe
[0];
3922 retval
= close(stderr_pipe
[1]);
3925 log_warn(LD_GENERAL
,
3926 "Failed to close write end of stderr pipe in parent process: %s",
3930 status
= process_handle
->status
= PROCESS_STATUS_RUNNING
;
3931 /* Set stdout/stderr pipes to be non-blocking */
3932 fcntl(process_handle
->stdout_pipe
, F_SETFL
, O_NONBLOCK
);
3933 fcntl(process_handle
->stderr_pipe
, F_SETFL
, O_NONBLOCK
);
3934 /* Open the buffered IO streams */
3935 process_handle
->stdout_handle
= fdopen(process_handle
->stdout_pipe
, "r");
3936 process_handle
->stderr_handle
= fdopen(process_handle
->stderr_pipe
, "r");
3938 *process_handle_out
= process_handle
;
3939 return process_handle
->status
;
3943 /** Destroy all resources allocated by the process handle in
3944 * <b>process_handle</b>.
3945 * If <b>also_terminate_process</b> is true, also terminate the
3946 * process of the process handle. */
3948 tor_process_handle_destroy(process_handle_t
*process_handle
,
3949 int also_terminate_process
)
3951 if (!process_handle
)
3954 if (also_terminate_process
) {
3955 if (tor_terminate_process(process_handle
) < 0) {
3956 const char *errstr
=
3958 format_win32_error(GetLastError());
3962 log_notice(LD_GENERAL
, "Failed to terminate process with "
3963 "PID '%d' ('%s').", tor_process_get_pid(process_handle
),
3966 log_info(LD_GENERAL
, "Terminated process with PID '%d'.",
3967 tor_process_get_pid(process_handle
));
3971 process_handle
->status
= PROCESS_STATUS_NOTRUNNING
;
3974 if (process_handle
->stdout_pipe
)
3975 CloseHandle(process_handle
->stdout_pipe
);
3977 if (process_handle
->stderr_pipe
)
3978 CloseHandle(process_handle
->stderr_pipe
);
3980 if (process_handle
->stdout_handle
)
3981 fclose(process_handle
->stdout_handle
);
3983 if (process_handle
->stderr_handle
)
3984 fclose(process_handle
->stderr_handle
);
3987 memset(process_handle
, 0x0f, sizeof(process_handle_t
));
3988 tor_free(process_handle
);
3991 /** Get the exit code of a process specified by <b>process_handle</b> and store
3992 * it in <b>exit_code</b>, if set to a non-NULL value. If <b>block</b> is set
3993 * to true, the call will block until the process has exited. Otherwise if
3994 * the process is still running, the function will return
3995 * PROCESS_EXIT_RUNNING, and exit_code will be left unchanged. Returns
3996 * PROCESS_EXIT_EXITED if the process did exit. If there is a failure,
3997 * PROCESS_EXIT_ERROR will be returned and the contents of exit_code (if
3998 * non-NULL) will be undefined. N.B. Under *nix operating systems, this will
3999 * probably not work in Tor, because waitpid() is called in main.c to reap any
4000 * terminated child processes.*/
4002 tor_get_exit_code(const process_handle_t
*process_handle
,
4003 int block
, int *exit_code
)
4010 /* Wait for the process to exit */
4011 retval
= WaitForSingleObject(process_handle
->pid
.hProcess
, INFINITE
);
4012 if (retval
!= WAIT_OBJECT_0
) {
4013 log_warn(LD_GENERAL
, "WaitForSingleObject() failed (%d): %s",
4014 (int)retval
, format_win32_error(GetLastError()));
4015 return PROCESS_EXIT_ERROR
;
4018 retval
= WaitForSingleObject(process_handle
->pid
.hProcess
, 0);
4019 if (WAIT_TIMEOUT
== retval
) {
4020 /* Process has not exited */
4021 return PROCESS_EXIT_RUNNING
;
4022 } else if (retval
!= WAIT_OBJECT_0
) {
4023 log_warn(LD_GENERAL
, "WaitForSingleObject() failed (%d): %s",
4024 (int)retval
, format_win32_error(GetLastError()));
4025 return PROCESS_EXIT_ERROR
;
4029 if (exit_code
!= NULL
) {
4030 success
= GetExitCodeProcess(process_handle
->pid
.hProcess
,
4033 log_warn(LD_GENERAL
, "GetExitCodeProcess() failed: %s",
4034 format_win32_error(GetLastError()));
4035 return PROCESS_EXIT_ERROR
;
4042 retval
= waitpid(process_handle
->pid
, &stat_loc
, block
?0:WNOHANG
);
4043 if (!block
&& 0 == retval
) {
4044 /* Process has not exited */
4045 return PROCESS_EXIT_RUNNING
;
4046 } else if (retval
!= process_handle
->pid
) {
4047 log_warn(LD_GENERAL
, "waitpid() failed for PID %d: %s",
4048 process_handle
->pid
, strerror(errno
));
4049 return PROCESS_EXIT_ERROR
;
4052 if (!WIFEXITED(stat_loc
)) {
4053 log_warn(LD_GENERAL
, "Process %d did not exit normally",
4054 process_handle
->pid
);
4055 return PROCESS_EXIT_ERROR
;
4058 if (exit_code
!= NULL
)
4059 *exit_code
= WEXITSTATUS(stat_loc
);
4062 return PROCESS_EXIT_EXITED
;
4065 /** Helper: return the number of characters in <b>s</b> preceding the first
4066 * occurrence of <b>ch</b>. If <b>ch</b> does not occur in <b>s</b>, return
4067 * the length of <b>s</b>. Should be equivalent to strspn(s, "ch"). */
4068 static INLINE
size_t
4069 str_num_before(const char *s
, char ch
)
4071 const char *cp
= strchr(s
, ch
);
4078 /** Return non-zero iff getenv would consider <b>s1</b> and <b>s2</b>
4079 * to have the same name as strings in a process's environment. */
4081 environment_variable_names_equal(const char *s1
, const char *s2
)
4083 size_t s1_name_len
= str_num_before(s1
, '=');
4084 size_t s2_name_len
= str_num_before(s2
, '=');
4086 return (s1_name_len
== s2_name_len
&&
4087 tor_memeq(s1
, s2
, s1_name_len
));
4090 /** Free <b>env</b> (assuming it was produced by
4091 * process_environment_make). */
4093 process_environment_free(process_environment_t
*env
)
4095 if (env
== NULL
) return;
4097 /* As both an optimization hack to reduce consing on Unixoid systems
4098 * and a nice way to ensure that some otherwise-Windows-specific
4099 * code will always get tested before changes to it get merged, the
4100 * strings which env->unixoid_environment_block points to are packed
4101 * into env->windows_environment_block. */
4102 tor_free(env
->unixoid_environment_block
);
4103 tor_free(env
->windows_environment_block
);
4108 /** Make a process_environment_t containing the environment variables
4109 * specified in <b>env_vars</b> (as C strings of the form
4111 process_environment_t
*
4112 process_environment_make(struct smartlist_t
*env_vars
)
4114 process_environment_t
*env
= tor_malloc_zero(sizeof(process_environment_t
));
4115 size_t n_env_vars
= smartlist_len(env_vars
);
4117 size_t total_env_length
;
4118 smartlist_t
*env_vars_sorted
;
4120 tor_assert(n_env_vars
+ 1 != 0);
4121 env
->unixoid_environment_block
= tor_calloc(n_env_vars
+ 1, sizeof(char *));
4122 /* env->unixoid_environment_block is already NULL-terminated,
4123 * because we assume that NULL == 0 (and check that during compilation). */
4125 total_env_length
= 1; /* terminating NUL of terminating empty string */
4126 for (i
= 0; i
< n_env_vars
; ++i
) {
4127 const char *s
= smartlist_get(env_vars
, i
);
4128 size_t slen
= strlen(s
);
4130 tor_assert(slen
+ 1 != 0);
4131 tor_assert(slen
+ 1 < SIZE_MAX
- total_env_length
);
4132 total_env_length
+= slen
+ 1;
4135 env
->windows_environment_block
= tor_malloc_zero(total_env_length
);
4136 /* env->windows_environment_block is already
4137 * (NUL-terminated-empty-string)-terminated. */
4139 /* Some versions of Windows supposedly require that environment
4140 * blocks be sorted. Or maybe some Windows programs (or their
4141 * runtime libraries) fail to look up strings in non-sorted
4142 * environment blocks.
4144 * Also, sorting strings makes it easy to find duplicate environment
4145 * variables and environment-variable strings without an '=' on all
4146 * OSes, and they can cause badness. Let's complain about those. */
4147 env_vars_sorted
= smartlist_new();
4148 smartlist_add_all(env_vars_sorted
, env_vars
);
4149 smartlist_sort_strings(env_vars_sorted
);
4151 /* Now copy the strings into the environment blocks. */
4153 char *cp
= env
->windows_environment_block
;
4154 const char *prev_env_var
= NULL
;
4156 for (i
= 0; i
< n_env_vars
; ++i
) {
4157 const char *s
= smartlist_get(env_vars_sorted
, i
);
4158 size_t slen
= strlen(s
);
4159 size_t s_name_len
= str_num_before(s
, '=');
4161 if (s_name_len
== slen
) {
4162 log_warn(LD_GENERAL
,
4163 "Preparing an environment containing a variable "
4164 "without a value: %s",
4167 if (prev_env_var
!= NULL
&&
4168 environment_variable_names_equal(s
, prev_env_var
)) {
4169 log_warn(LD_GENERAL
,
4170 "Preparing an environment containing two variables "
4171 "with the same name: %s and %s",
4177 /* Actually copy the string into the environment. */
4178 memcpy(cp
, s
, slen
+1);
4179 env
->unixoid_environment_block
[i
] = cp
;
4183 tor_assert(cp
== env
->windows_environment_block
+ total_env_length
- 1);
4186 smartlist_free(env_vars_sorted
);
4191 /** Return a newly allocated smartlist containing every variable in
4192 * this process's environment, as a NUL-terminated string of the form
4193 * "NAME=VALUE". Note that on some/many/most/all OSes, the parent
4194 * process can put strings not of that form in our environment;
4195 * callers should try to not get crashed by that.
4197 * The returned strings are heap-allocated, and must be freed by the
4199 struct smartlist_t
*
4200 get_current_process_environment_variables(void)
4202 smartlist_t
*sl
= smartlist_new();
4204 char **environ_tmp
; /* Not const char ** ? Really? */
4205 for (environ_tmp
= get_environment(); *environ_tmp
; ++environ_tmp
) {
4206 smartlist_add(sl
, tor_strdup(*environ_tmp
));
4212 /** For each string s in <b>env_vars</b> such that
4213 * environment_variable_names_equal(s, <b>new_var</b>), remove it; if
4214 * <b>free_p</b> is non-zero, call <b>free_old</b>(s). If
4215 * <b>new_var</b> contains '=', insert it into <b>env_vars</b>. */
4217 set_environment_variable_in_smartlist(struct smartlist_t
*env_vars
,
4218 const char *new_var
,
4219 void (*free_old
)(void*),
4222 SMARTLIST_FOREACH_BEGIN(env_vars
, const char *, s
) {
4223 if (environment_variable_names_equal(s
, new_var
)) {
4224 SMARTLIST_DEL_CURRENT(env_vars
, s
);
4226 free_old((void *)s
);
4229 } SMARTLIST_FOREACH_END(s
);
4231 if (strchr(new_var
, '=') != NULL
) {
4232 smartlist_add(env_vars
, (void *)new_var
);
4237 /** Read from a handle <b>h</b> into <b>buf</b>, up to <b>count</b> bytes. If
4238 * <b>hProcess</b> is NULL, the function will return immediately if there is
4239 * nothing more to read. Otherwise <b>hProcess</b> should be set to the handle
4240 * to the process owning the <b>h</b>. In this case, the function will exit
4241 * only once the process has exited, or <b>count</b> bytes are read. Returns
4242 * the number of bytes read, or -1 on error. */
4244 tor_read_all_handle(HANDLE h
, char *buf
, size_t count
,
4245 const process_handle_t
*process
)
4250 BOOL process_exited
= FALSE
;
4252 if (count
> SIZE_T_CEILING
|| count
> SSIZE_T_MAX
)
4255 while (numread
!= count
) {
4256 /* Check if there is anything to read */
4257 retval
= PeekNamedPipe(h
, NULL
, 0, NULL
, &byte_count
, NULL
);
4259 log_warn(LD_GENERAL
,
4260 "Failed to peek from handle: %s",
4261 format_win32_error(GetLastError()));
4263 } else if (0 == byte_count
) {
4264 /* Nothing available: process exited or it is busy */
4266 /* Exit if we don't know whether the process is running */
4267 if (NULL
== process
)
4270 /* The process exited and there's nothing left to read from it */
4274 /* If process is not running, check for output one more time in case
4275 it wrote something after the peek was performed. Otherwise keep on
4276 waiting for output */
4277 tor_assert(process
!= NULL
);
4278 byte_count
= WaitForSingleObject(process
->pid
.hProcess
, 0);
4279 if (WAIT_TIMEOUT
!= byte_count
)
4280 process_exited
= TRUE
;
4285 /* There is data to read; read it */
4286 retval
= ReadFile(h
, buf
+numread
, count
-numread
, &byte_count
, NULL
);
4287 tor_assert(byte_count
+ numread
<= count
);
4289 log_warn(LD_GENERAL
, "Failed to read from handle: %s",
4290 format_win32_error(GetLastError()));
4292 } else if (0 == byte_count
) {
4296 numread
+= byte_count
;
4298 return (ssize_t
)numread
;
4301 /** Read from a handle <b>h</b> into <b>buf</b>, up to <b>count</b> bytes. If
4302 * <b>process</b> is NULL, the function will return immediately if there is
4303 * nothing more to read. Otherwise data will be read until end of file, or
4304 * <b>count</b> bytes are read. Returns the number of bytes read, or -1 on
4305 * error. Sets <b>eof</b> to true if <b>eof</b> is not NULL and the end of the
4306 * file has been reached. */
4308 tor_read_all_handle(FILE *h
, char *buf
, size_t count
,
4309 const process_handle_t
*process
,
4318 if (count
> SIZE_T_CEILING
|| count
> SSIZE_T_MAX
)
4321 while (numread
!= count
) {
4322 /* Use fgets because that is what we use in log_from_pipe() */
4323 retval
= fgets(buf
+numread
, (int)(count
-numread
), h
);
4324 if (NULL
== retval
) {
4326 log_debug(LD_GENERAL
, "fgets() reached end of file");
4331 if (EAGAIN
== errno
) {
4337 log_warn(LD_GENERAL
, "fgets() from handle failed: %s",
4343 tor_assert(retval
!= NULL
);
4344 tor_assert(strlen(retval
) + numread
<= count
);
4345 numread
+= strlen(retval
);
4348 log_debug(LD_GENERAL
, "fgets() read %d bytes from handle", (int)numread
);
4349 return (ssize_t
)numread
;
4353 /** Read from stdout of a process until the process exits. */
4355 tor_read_all_from_process_stdout(const process_handle_t
*process_handle
,
4356 char *buf
, size_t count
)
4359 return tor_read_all_handle(process_handle
->stdout_pipe
, buf
, count
,
4362 return tor_read_all_handle(process_handle
->stdout_handle
, buf
, count
,
4363 process_handle
, NULL
);
4367 /** Read from stdout of a process until the process exits. */
4369 tor_read_all_from_process_stderr(const process_handle_t
*process_handle
,
4370 char *buf
, size_t count
)
4373 return tor_read_all_handle(process_handle
->stderr_pipe
, buf
, count
,
4376 return tor_read_all_handle(process_handle
->stderr_handle
, buf
, count
,
4377 process_handle
, NULL
);
4381 /** Split buf into lines, and add to smartlist. The buffer <b>buf</b> will be
4382 * modified. The resulting smartlist will consist of pointers to buf, so there
4383 * is no need to free the contents of sl. <b>buf</b> must be a NUL-terminated
4384 * string. <b>len</b> should be set to the length of the buffer excluding the
4385 * NUL. Non-printable characters (including NUL) will be replaced with "." */
4387 tor_split_lines(smartlist_t
*sl
, char *buf
, int len
)
4389 /* Index in buf of the start of the current line */
4391 /* Index in buf of the current character being processed */
4393 /* Are we currently in a line */
4396 /* Loop over string */
4398 /* Loop until end of line or end of string */
4399 for (; cur
< len
; cur
++) {
4401 if ('\r' == buf
[cur
] || '\n' == buf
[cur
]) {
4404 /* Point cur to the next line */
4406 /* Line starts at start and ends with a nul */
4409 if (!TOR_ISPRINT(buf
[cur
]))
4413 if ('\r' == buf
[cur
] || '\n' == buf
[cur
]) {
4414 /* Skip leading vertical space */
4419 if (!TOR_ISPRINT(buf
[cur
]))
4424 /* We are at the end of the line or end of string. If in_line is true there
4425 * is a line which starts at buf+start and ends at a NUL. cur points to
4426 * the character after the NUL. */
4428 smartlist_add(sl
, (void *)(buf
+start
));
4431 return smartlist_len(sl
);
4434 /** Return a string corresponding to <b>stream_status</b>. */
4436 stream_status_to_string(enum stream_status stream_status
)
4438 switch (stream_status
) {
4439 case IO_STREAM_OKAY
:
4441 case IO_STREAM_EAGAIN
:
4442 return "temporarily unavailable";
4443 case IO_STREAM_TERM
:
4444 return "terminated";
4445 case IO_STREAM_CLOSED
:
4448 tor_fragile_assert();
4455 /** Return a smartlist containing lines outputted from
4456 * <b>handle</b>. Return NULL on error, and set
4457 * <b>stream_status_out</b> appropriately. */
4459 tor_get_lines_from_handle(HANDLE
*handle
,
4460 enum stream_status
*stream_status_out
)
4463 char stdout_buf
[600] = {0};
4464 smartlist_t
*lines
= NULL
;
4466 tor_assert(stream_status_out
);
4468 *stream_status_out
= IO_STREAM_TERM
;
4470 pos
= tor_read_all_handle(handle
, stdout_buf
, sizeof(stdout_buf
) - 1, NULL
);
4472 *stream_status_out
= IO_STREAM_TERM
;
4476 *stream_status_out
= IO_STREAM_EAGAIN
;
4480 /* End with a null even if there isn't a \r\n at the end */
4481 /* TODO: What if this is a partial line? */
4482 stdout_buf
[pos
] = '\0';
4484 /* Split up the buffer */
4485 lines
= smartlist_new();
4486 tor_split_lines(lines
, stdout_buf
, pos
);
4488 /* Currently 'lines' is populated with strings residing on the
4489 stack. Replace them with their exact copies on the heap: */
4490 SMARTLIST_FOREACH(lines
, char *, line
,
4491 SMARTLIST_REPLACE_CURRENT(lines
, line
, tor_strdup(line
)));
4493 *stream_status_out
= IO_STREAM_OKAY
;
4498 /** Read from stream, and send lines to log at the specified log level.
4499 * Returns -1 if there is a error reading, and 0 otherwise.
4500 * If the generated stream is flushed more often than on new lines, or
4501 * a read exceeds 256 bytes, lines will be truncated. This should be fixed,
4502 * along with the corresponding problem on *nix (see bug #2045).
4505 log_from_handle(HANDLE
*pipe
, int severity
)
4511 pos
= tor_read_all_handle(pipe
, buf
, sizeof(buf
) - 1, NULL
);
4514 log_warn(LD_GENERAL
, "Failed to read data from subprocess");
4519 /* There's nothing to read (process is busy or has exited) */
4520 log_debug(LD_GENERAL
, "Subprocess had nothing to say");
4524 /* End with a null even if there isn't a \r\n at the end */
4525 /* TODO: What if this is a partial line? */
4527 log_debug(LD_GENERAL
, "Subprocess had %d bytes to say", pos
);
4529 /* Split up the buffer */
4530 lines
= smartlist_new();
4531 tor_split_lines(lines
, buf
, pos
);
4534 SMARTLIST_FOREACH(lines
, char *, line
,
4536 log_fn(severity
, LD_GENERAL
, "Port forwarding helper says: %s", line
);
4538 smartlist_free(lines
);
4545 /** Return a smartlist containing lines outputted from
4546 * <b>handle</b>. Return NULL on error, and set
4547 * <b>stream_status_out</b> appropriately. */
4549 tor_get_lines_from_handle(FILE *handle
, enum stream_status
*stream_status_out
)
4551 enum stream_status stream_status
;
4552 char stdout_buf
[400];
4553 smartlist_t
*lines
= NULL
;
4556 memset(stdout_buf
, 0, sizeof(stdout_buf
));
4558 stream_status
= get_string_from_pipe(handle
,
4559 stdout_buf
, sizeof(stdout_buf
) - 1);
4560 if (stream_status
!= IO_STREAM_OKAY
)
4563 if (!lines
) lines
= smartlist_new();
4564 smartlist_add(lines
, tor_strdup(stdout_buf
));
4568 *stream_status_out
= stream_status
;
4572 /** Read from stream, and send lines to log at the specified log level.
4573 * Returns 1 if stream is closed normally, -1 if there is a error reading, and
4574 * 0 otherwise. Handles lines from tor-fw-helper and
4575 * tor_spawn_background() specially.
4578 log_from_pipe(FILE *stream
, int severity
, const char *executable
,
4582 enum stream_status r
;
4585 r
= get_string_from_pipe(stream
, buf
, sizeof(buf
) - 1);
4587 if (r
== IO_STREAM_CLOSED
) {
4589 } else if (r
== IO_STREAM_EAGAIN
) {
4591 } else if (r
== IO_STREAM_TERM
) {
4595 tor_assert(r
== IO_STREAM_OKAY
);
4597 /* Check if buf starts with SPAWN_ERROR_MESSAGE */
4598 if (strcmpstart(buf
, SPAWN_ERROR_MESSAGE
) == 0) {
4599 /* Parse error message */
4600 int retval
, child_state
, saved_errno
;
4601 retval
= tor_sscanf(buf
, SPAWN_ERROR_MESSAGE
"%x/%x",
4602 &child_state
, &saved_errno
);
4604 log_warn(LD_GENERAL
,
4605 "Failed to start child process \"%s\" in state %d: %s",
4606 executable
, child_state
, strerror(saved_errno
));
4610 /* Failed to parse message from child process, log it as a
4612 log_warn(LD_GENERAL
,
4613 "Unexpected message from port forwarding helper \"%s\": %s",
4617 log_fn(severity
, LD_GENERAL
, "Port forwarding helper says: %s", buf
);
4621 /* We should never get here */
4626 /** Reads from <b>stream</b> and stores input in <b>buf_out</b> making
4627 * sure it's below <b>count</b> bytes.
4628 * If the string has a trailing newline, we strip it off.
4630 * This function is specifically created to handle input from managed
4631 * proxies, according to the pluggable transports spec. Make sure it
4632 * fits your needs before using it.
4635 * IO_STREAM_CLOSED: If the stream is closed.
4636 * IO_STREAM_EAGAIN: If there is nothing to read and we should check back
4638 * IO_STREAM_TERM: If something is wrong with the stream.
4639 * IO_STREAM_OKAY: If everything went okay and we got a string
4640 * in <b>buf_out</b>. */
4642 get_string_from_pipe(FILE *stream
, char *buf_out
, size_t count
)
4647 tor_assert(count
<= INT_MAX
);
4649 retval
= fgets(buf_out
, (int)count
, stream
);
4653 /* Program has closed stream (probably it exited) */
4654 /* TODO: check error */
4655 return IO_STREAM_CLOSED
;
4657 if (EAGAIN
== errno
) {
4658 /* Nothing more to read, try again next time */
4659 return IO_STREAM_EAGAIN
;
4661 /* There was a problem, abandon this child process */
4662 return IO_STREAM_TERM
;
4666 len
= strlen(buf_out
);
4668 /* this probably means we got a NUL at the start of the string. */
4669 return IO_STREAM_EAGAIN
;
4672 if (buf_out
[len
- 1] == '\n') {
4673 /* Remove the trailing newline */
4674 buf_out
[len
- 1] = '\0';
4676 /* No newline; check whether we overflowed the buffer */
4678 log_info(LD_GENERAL
,
4679 "Line from stream was truncated: %s", buf_out
);
4680 /* TODO: What to do with this error? */
4683 return IO_STREAM_OKAY
;
4686 /* We should never get here */
4687 return IO_STREAM_TERM
;
4690 /** Parse a <b>line</b> from tor-fw-helper and issue an appropriate
4691 * log message to our user. */
4693 handle_fw_helper_line(const char *line
)
4695 smartlist_t
*tokens
= smartlist_new();
4696 char *message
= NULL
;
4697 char *message_for_log
= NULL
;
4698 const char *external_port
= NULL
;
4699 const char *internal_port
= NULL
;
4700 const char *result
= NULL
;
4704 smartlist_split_string(tokens
, line
, NULL
,
4705 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, -1);
4707 if (smartlist_len(tokens
) < 5)
4710 if (strcmp(smartlist_get(tokens
, 0), "tor-fw-helper") ||
4711 strcmp(smartlist_get(tokens
, 1), "tcp-forward"))
4714 external_port
= smartlist_get(tokens
, 2);
4715 internal_port
= smartlist_get(tokens
, 3);
4716 result
= smartlist_get(tokens
, 4);
4718 if (smartlist_len(tokens
) > 5) {
4719 /* If there are more than 5 tokens, they are part of [<message>].
4720 Let's use a second smartlist to form the whole message;
4721 strncat loops suck. */
4723 int message_words_n
= smartlist_len(tokens
) - 5;
4724 smartlist_t
*message_sl
= smartlist_new();
4725 for (i
= 0; i
< message_words_n
; i
++)
4726 smartlist_add(message_sl
, smartlist_get(tokens
, 5+i
));
4728 tor_assert(smartlist_len(message_sl
) > 0);
4729 message
= smartlist_join_strings(message_sl
, " ", 0, NULL
);
4731 /* wrap the message in log-friendly wrapping */
4732 tor_asprintf(&message_for_log
, " ('%s')", message
);
4734 smartlist_free(message_sl
);
4737 port
= atoi(external_port
);
4738 if (port
< 1 || port
> 65535)
4741 port
= atoi(internal_port
);
4742 if (port
< 1 || port
> 65535)
4745 if (!strcmp(result
, "SUCCESS"))
4747 else if (!strcmp(result
, "FAIL"))
4753 log_warn(LD_GENERAL
, "Tor was unable to forward TCP port '%s' to '%s'%s. "
4754 "Please make sure that your router supports port "
4755 "forwarding protocols (like NAT-PMP). Note that if '%s' is "
4756 "your ORPort, your relay will be unable to receive inbound "
4757 "traffic.", external_port
, internal_port
,
4758 message_for_log
? message_for_log
: "",
4761 log_info(LD_GENERAL
,
4762 "Tor successfully forwarded TCP port '%s' to '%s'%s.",
4763 external_port
, internal_port
,
4764 message_for_log
? message_for_log
: "");
4770 log_warn(LD_GENERAL
, "tor-fw-helper sent us a string we could not "
4771 "parse (%s).", line
);
4774 SMARTLIST_FOREACH(tokens
, char *, cp
, tor_free(cp
));
4775 smartlist_free(tokens
);
4777 tor_free(message_for_log
);
4780 /** Read what tor-fw-helper has to say in its stdout and handle it
4783 handle_fw_helper_output(process_handle_t
*process_handle
)
4785 smartlist_t
*fw_helper_output
= NULL
;
4786 enum stream_status stream_status
= 0;
4789 tor_get_lines_from_handle(tor_process_get_stdout_pipe(process_handle
),
4791 if (!fw_helper_output
) { /* didn't get any output from tor-fw-helper */
4792 /* if EAGAIN we should retry in the future */
4793 return (stream_status
== IO_STREAM_EAGAIN
) ? 0 : -1;
4796 /* Handle the lines we got: */
4797 SMARTLIST_FOREACH_BEGIN(fw_helper_output
, char *, line
) {
4798 handle_fw_helper_line(line
);
4800 } SMARTLIST_FOREACH_END(line
);
4802 smartlist_free(fw_helper_output
);
4807 /** Spawn tor-fw-helper and ask it to forward the ports in
4808 * <b>ports_to_forward</b>. <b>ports_to_forward</b> contains strings
4809 * of the form "<external port>:<internal port>", which is the format
4810 * that tor-fw-helper expects. */
4812 tor_check_port_forwarding(const char *filename
,
4813 smartlist_t
*ports_to_forward
,
4816 /* When fw-helper succeeds, how long do we wait until running it again */
4817 #define TIME_TO_EXEC_FWHELPER_SUCCESS 300
4818 /* When fw-helper failed to start, how long do we wait until running it again
4820 #define TIME_TO_EXEC_FWHELPER_FAIL 60
4822 /* Static variables are initialized to zero, so child_handle.status=0
4823 * which corresponds to it not running on startup */
4824 static process_handle_t
*child_handle
=NULL
;
4826 static time_t time_to_run_helper
= 0;
4827 int stderr_status
, retval
;
4828 int stdout_status
= 0;
4830 tor_assert(filename
);
4832 /* Start the child, if it is not already running */
4833 if ((!child_handle
|| child_handle
->status
!= PROCESS_STATUS_RUNNING
) &&
4834 time_to_run_helper
< now
) {
4835 /*tor-fw-helper cli looks like this: tor_fw_helper -p :5555 -p 4555:1111 */
4836 const char **argv
; /* cli arguments */
4838 int argv_index
= 0; /* index inside 'argv' */
4840 tor_assert(smartlist_len(ports_to_forward
) > 0);
4842 /* check for overflow during 'argv' allocation:
4843 (len(ports_to_forward)*2 + 2)*sizeof(char*) > SIZE_MAX ==
4844 len(ports_to_forward) > (((SIZE_MAX/sizeof(char*)) - 2)/2) */
4845 if ((size_t) smartlist_len(ports_to_forward
) >
4846 (((SIZE_MAX
/sizeof(char*)) - 2)/2)) {
4847 log_warn(LD_GENERAL
,
4848 "Overflow during argv allocation. This shouldn't happen.");
4851 /* check for overflow during 'argv_index' increase:
4852 ((len(ports_to_forward)*2 + 2) > INT_MAX) ==
4853 len(ports_to_forward) > (INT_MAX - 2)/2 */
4854 if (smartlist_len(ports_to_forward
) > (INT_MAX
- 2)/2) {
4855 log_warn(LD_GENERAL
,
4856 "Overflow during argv_index increase. This shouldn't happen.");
4860 /* Calculate number of cli arguments: one for the filename, two
4861 for each smartlist element (one for "-p" and one for the
4862 ports), and one for the final NULL. */
4863 args_n
= 1 + 2*smartlist_len(ports_to_forward
) + 1;
4864 argv
= tor_malloc_zero(sizeof(char*)*args_n
);
4866 argv
[argv_index
++] = filename
;
4867 SMARTLIST_FOREACH_BEGIN(ports_to_forward
, const char *, port
) {
4868 argv
[argv_index
++] = "-p";
4869 argv
[argv_index
++] = port
;
4870 } SMARTLIST_FOREACH_END(port
);
4871 argv
[argv_index
] = NULL
;
4873 /* Assume tor-fw-helper will succeed, start it later*/
4874 time_to_run_helper
= now
+ TIME_TO_EXEC_FWHELPER_SUCCESS
;
4877 tor_process_handle_destroy(child_handle
, 1);
4878 child_handle
= NULL
;
4882 /* Passing NULL as lpApplicationName makes Windows search for the .exe */
4883 status
= tor_spawn_background(NULL
, argv
, NULL
, &child_handle
);
4885 status
= tor_spawn_background(filename
, argv
, NULL
, &child_handle
);
4888 tor_free_((void*)argv
);
4891 if (PROCESS_STATUS_ERROR
== status
) {
4892 log_warn(LD_GENERAL
, "Failed to start port forwarding helper %s",
4894 time_to_run_helper
= now
+ TIME_TO_EXEC_FWHELPER_FAIL
;
4898 log_info(LD_GENERAL
,
4899 "Started port forwarding helper (%s) with pid '%d'",
4900 filename
, tor_process_get_pid(child_handle
));
4903 /* If child is running, read from its stdout and stderr) */
4904 if (child_handle
&& PROCESS_STATUS_RUNNING
== child_handle
->status
) {
4905 /* Read from stdout/stderr and log result */
4908 stderr_status
= log_from_handle(child_handle
->stderr_pipe
, LOG_INFO
);
4910 stderr_status
= log_from_pipe(child_handle
->stderr_handle
,
4911 LOG_INFO
, filename
, &retval
);
4913 if (handle_fw_helper_output(child_handle
) < 0) {
4914 log_warn(LD_GENERAL
, "Failed to handle fw helper output.");
4920 /* There was a problem in the child process */
4921 time_to_run_helper
= now
+ TIME_TO_EXEC_FWHELPER_FAIL
;
4924 /* Combine the two statuses in order of severity */
4925 if (-1 == stdout_status
|| -1 == stderr_status
)
4926 /* There was a failure */
4929 else if (!child_handle
|| tor_get_exit_code(child_handle
, 0, NULL
) !=
4930 PROCESS_EXIT_RUNNING
) {
4931 /* process has exited or there was an error */
4932 /* TODO: Do something with the process return value */
4933 /* TODO: What if the process output something since
4934 * between log_from_handle and tor_get_exit_code? */
4938 else if (1 == stdout_status
|| 1 == stderr_status
)
4939 /* stdout or stderr was closed, the process probably
4940 * exited. It will be reaped by waitpid() in main.c */
4941 /* TODO: Do something with the process return value */
4948 /* If either pipe indicates a failure, act on it */
4951 log_info(LD_GENERAL
, "Port forwarding helper terminated");
4952 child_handle
->status
= PROCESS_STATUS_NOTRUNNING
;
4954 log_warn(LD_GENERAL
, "Failed to read from port forwarding helper");
4955 child_handle
->status
= PROCESS_STATUS_ERROR
;
4958 /* TODO: The child might not actually be finished (maybe it failed or
4959 closed stdout/stderr), so maybe we shouldn't start another? */
4964 /** Initialize the insecure RNG <b>rng</b> from a seed value <b>seed</b>. */
4966 tor_init_weak_random(tor_weak_rng_t
*rng
, unsigned seed
)
4968 rng
->state
= (uint32_t)(seed
& 0x7fffffff);
4971 /** Return a randomly chosen value in the range 0..TOR_WEAK_RANDOM_MAX based
4972 * on the RNG state of <b>rng</b>. This entropy will not be cryptographically
4973 * strong; do not rely on it for anything an adversary should not be able to
4976 tor_weak_random(tor_weak_rng_t
*rng
)
4978 /* Here's a linear congruential generator. OpenBSD and glibc use these
4979 * parameters; they aren't too bad, and should have maximal period over the
4980 * range 0..INT32_MAX. We don't want to use the platform rand() or random(),
4981 * since some platforms have bad weak RNGs that only return values in the
4982 * range 0..INT16_MAX, which just isn't enough. */
4983 rng
->state
= (rng
->state
* 1103515245 + 12345) & 0x7fffffff;
4984 return (int32_t) rng
->state
;
4987 /** Return a random number in the range [0 , <b>top</b>). {That is, the range
4988 * of integers i such that 0 <= i < top.} Chooses uniformly. Requires that
4989 * top is greater than 0. This randomness is not cryptographically strong; do
4990 * not rely on it for anything an adversary should not be able to predict. */
4992 tor_weak_random_range(tor_weak_rng_t
*rng
, int32_t top
)
4994 /* We don't want to just do tor_weak_random() % top, since random() is often
4995 * implemented with an LCG whose modulus is a power of 2, and those are
4996 * cyclic in their low-order bits. */
4997 int divisor
, result
;
4998 tor_assert(top
> 0);
4999 divisor
= TOR_WEAK_RANDOM_MAX
/ top
;
5001 result
= (int32_t)(tor_weak_random(rng
) / divisor
);
5002 } while (result
>= top
);