Fix compile warnings on windows
[tor.git] / src / common / util.c
blobc44a4aa3b1080828eb696338db94ef49a836a20f
1 /* Copyright (c) 2003, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2011, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 /**
7 * \file util.c
8 * \brief Common functions for strings, IO, network, data structures,
9 * process control.
10 **/
12 /* This is required on rh7 to make strptime not complain.
14 #define _GNU_SOURCE
16 #include "orconfig.h"
17 #ifdef HAVE_FCNTL_H
18 #include <fcntl.h>
19 #endif
20 #define UTIL_PRIVATE
21 #include "util.h"
22 #include "torlog.h"
23 #undef log
24 #include "crypto.h"
25 #include "torint.h"
26 #include "container.h"
27 #include "address.h"
29 #ifdef MS_WINDOWS
30 #include <io.h>
31 #include <direct.h>
32 #include <process.h>
33 #include <tchar.h>
34 #include <Winbase.h>
35 #else
36 #include <dirent.h>
37 #include <pwd.h>
38 #include <grp.h>
39 #endif
41 /* math.h needs this on Linux */
42 #ifndef __USE_ISOC99
43 #define __USE_ISOC99 1
44 #endif
45 #include <math.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <assert.h>
50 #include <signal.h>
52 #ifdef HAVE_NETINET_IN_H
53 #include <netinet/in.h>
54 #endif
55 #ifdef HAVE_ARPA_INET_H
56 #include <arpa/inet.h>
57 #endif
58 #ifdef HAVE_ERRNO_H
59 #include <errno.h>
60 #endif
61 #ifdef HAVE_SYS_SOCKET_H
62 #include <sys/socket.h>
63 #endif
64 #ifdef HAVE_SYS_TIME_H
65 #include <sys/time.h>
66 #endif
67 #ifdef HAVE_UNISTD_H
68 #include <unistd.h>
69 #endif
70 #ifdef HAVE_SYS_STAT_H
71 #include <sys/stat.h>
72 #endif
73 #ifdef HAVE_SYS_FCNTL_H
74 #include <sys/fcntl.h>
75 #endif
76 #ifdef HAVE_TIME_H
77 #include <time.h>
78 #endif
79 #ifdef HAVE_MALLOC_MALLOC_H
80 #include <malloc/malloc.h>
81 #endif
82 #ifdef HAVE_MALLOC_H
83 #ifndef OPENBSD
84 /* OpenBSD has a malloc.h, but for our purposes, it only exists in order to
85 * scold us for being so stupid as to autodetect its presence. To be fair,
86 * they've done this since 1996, when autoconf was only 5 years old. */
87 #include <malloc.h>
88 #endif
89 #endif
90 #ifdef HAVE_MALLOC_NP_H
91 #include <malloc_np.h>
92 #endif
93 #ifdef HAVE_SYS_WAIT_H
94 #include <sys/wait.h>
95 #endif
97 /* =====
98 * Memory management
99 * ===== */
100 #ifdef USE_DMALLOC
101 #undef strndup
102 #include <dmalloc.h>
103 /* Macro to pass the extra dmalloc args to another function. */
104 #define DMALLOC_FN_ARGS , file, line
106 #if defined(HAVE_DMALLOC_STRDUP)
107 /* the dmalloc_strdup should be fine as defined */
108 #elif defined(HAVE_DMALLOC_STRNDUP)
109 #define dmalloc_strdup(file, line, string, xalloc_b) \
110 dmalloc_strndup(file, line, (string), -1, xalloc_b)
111 #else
112 #error "No dmalloc_strdup or equivalent"
113 #endif
115 #else /* not using dmalloc */
117 #define DMALLOC_FN_ARGS
118 #endif
120 /** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to
121 * result. On error, log and terminate the process. (Same as malloc(size),
122 * but never returns NULL.)
124 * <b>file</b> and <b>line</b> are used if dmalloc is enabled, and
125 * ignored otherwise.
127 void *
128 _tor_malloc(size_t size DMALLOC_PARAMS)
130 void *result;
132 tor_assert(size < SIZE_T_CEILING);
134 #ifndef MALLOC_ZERO_WORKS
135 /* Some libc mallocs don't work when size==0. Override them. */
136 if (size==0) {
137 size=1;
139 #endif
141 #ifdef USE_DMALLOC
142 result = dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
143 #else
144 result = malloc(size);
145 #endif
147 if (PREDICT_UNLIKELY(result == NULL)) {
148 log_err(LD_MM,"Out of memory on malloc(). Dying.");
149 /* If these functions die within a worker process, they won't call
150 * spawn_exit, but that's ok, since the parent will run out of memory soon
151 * anyway. */
152 exit(1);
154 return result;
157 /** Allocate a chunk of <b>size</b> bytes of memory, fill the memory with
158 * zero bytes, and return a pointer to the result. Log and terminate
159 * the process on error. (Same as calloc(size,1), but never returns NULL.)
161 void *
162 _tor_malloc_zero(size_t size DMALLOC_PARAMS)
164 /* You may ask yourself, "wouldn't it be smart to use calloc instead of
165 * malloc+memset? Perhaps libc's calloc knows some nifty optimization trick
166 * we don't!" Indeed it does, but its optimizations are only a big win when
167 * we're allocating something very big (it knows if it just got the memory
168 * from the OS in a pre-zeroed state). We don't want to use tor_malloc_zero
169 * for big stuff, so we don't bother with calloc. */
170 void *result = _tor_malloc(size DMALLOC_FN_ARGS);
171 memset(result, 0, size);
172 return result;
175 /** Change the size of the memory block pointed to by <b>ptr</b> to <b>size</b>
176 * bytes long; return the new memory block. On error, log and
177 * terminate. (Like realloc(ptr,size), but never returns NULL.)
179 void *
180 _tor_realloc(void *ptr, size_t size DMALLOC_PARAMS)
182 void *result;
184 tor_assert(size < SIZE_T_CEILING);
186 #ifdef USE_DMALLOC
187 result = dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
188 #else
189 result = realloc(ptr, size);
190 #endif
192 if (PREDICT_UNLIKELY(result == NULL)) {
193 log_err(LD_MM,"Out of memory on realloc(). Dying.");
194 exit(1);
196 return result;
199 /** Return a newly allocated copy of the NUL-terminated string s. On
200 * error, log and terminate. (Like strdup(s), but never returns
201 * NULL.)
203 char *
204 _tor_strdup(const char *s DMALLOC_PARAMS)
206 char *dup;
207 tor_assert(s);
209 #ifdef USE_DMALLOC
210 dup = dmalloc_strdup(file, line, s, 0);
211 #else
212 dup = strdup(s);
213 #endif
214 if (PREDICT_UNLIKELY(dup == NULL)) {
215 log_err(LD_MM,"Out of memory on strdup(). Dying.");
216 exit(1);
218 return dup;
221 /** Allocate and return a new string containing the first <b>n</b>
222 * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b>
223 * characters, only the first <b>n</b> are copied. The result is
224 * always NUL-terminated. (Like strndup(s,n), but never returns
225 * NULL.)
227 char *
228 _tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
230 char *dup;
231 tor_assert(s);
232 tor_assert(n < SIZE_T_CEILING);
233 dup = _tor_malloc((n+1) DMALLOC_FN_ARGS);
234 /* Performance note: Ordinarily we prefer strlcpy to strncpy. But
235 * this function gets called a whole lot, and platform strncpy is
236 * much faster than strlcpy when strlen(s) is much longer than n.
238 strncpy(dup, s, n);
239 dup[n]='\0';
240 return dup;
243 /** Allocate a chunk of <b>len</b> bytes, with the same contents as the
244 * <b>len</b> bytes starting at <b>mem</b>. */
245 void *
246 _tor_memdup(const void *mem, size_t len DMALLOC_PARAMS)
248 char *dup;
249 tor_assert(len < SIZE_T_CEILING);
250 tor_assert(mem);
251 dup = _tor_malloc(len DMALLOC_FN_ARGS);
252 memcpy(dup, mem, len);
253 return dup;
256 /** Helper for places that need to take a function pointer to the right
257 * spelling of "free()". */
258 void
259 _tor_free(void *mem)
261 tor_free(mem);
264 #if defined(HAVE_MALLOC_GOOD_SIZE) && !defined(HAVE_MALLOC_GOOD_SIZE_PROTOTYPE)
265 /* Some version of Mac OSX have malloc_good_size in their libc, but not
266 * actually defined in malloc/malloc.h. We detect this and work around it by
267 * prototyping.
269 extern size_t malloc_good_size(size_t size);
270 #endif
272 /** Allocate and return a chunk of memory of size at least *<b>size</b>, using
273 * the same resources we would use to malloc *<b>sizep</b>. Set *<b>sizep</b>
274 * to the number of usable bytes in the chunk of memory. */
275 void *
276 _tor_malloc_roundup(size_t *sizep DMALLOC_PARAMS)
278 #ifdef HAVE_MALLOC_GOOD_SIZE
279 tor_assert(*sizep < SIZE_T_CEILING);
280 *sizep = malloc_good_size(*sizep);
281 return _tor_malloc(*sizep DMALLOC_FN_ARGS);
282 #elif 0 && defined(HAVE_MALLOC_USABLE_SIZE) && !defined(USE_DMALLOC)
283 /* Never use malloc_usable_size(); it makes valgrind really unhappy,
284 * and doesn't win much in terms of usable space where it exists. */
285 void *result;
286 tor_assert(*sizep < SIZE_T_CEILING);
287 result = _tor_malloc(*sizep DMALLOC_FN_ARGS);
288 *sizep = malloc_usable_size(result);
289 return result;
290 #else
291 return _tor_malloc(*sizep DMALLOC_FN_ARGS);
292 #endif
295 /** Call the platform malloc info function, and dump the results to the log at
296 * level <b>severity</b>. If no such function exists, do nothing. */
297 void
298 tor_log_mallinfo(int severity)
300 #ifdef HAVE_MALLINFO
301 struct mallinfo mi;
302 memset(&mi, 0, sizeof(mi));
303 mi = mallinfo();
304 tor_log(severity, LD_MM,
305 "mallinfo() said: arena=%d, ordblks=%d, smblks=%d, hblks=%d, "
306 "hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, "
307 "keepcost=%d",
308 mi.arena, mi.ordblks, mi.smblks, mi.hblks,
309 mi.hblkhd, mi.usmblks, mi.fsmblks, mi.uordblks, mi.fordblks,
310 mi.keepcost);
311 #else
312 (void)severity;
313 #endif
314 #ifdef USE_DMALLOC
315 dmalloc_log_changed(0, /* Since the program started. */
316 1, /* Log info about non-freed pointers. */
317 0, /* Do not log info about freed pointers. */
318 0 /* Do not log individual pointers. */
320 #endif
323 /* =====
324 * Math
325 * ===== */
328 * Returns the natural logarithm of d base 2. We define this wrapper here so
329 * as to make it easier not to conflict with Tor's log() macro.
331 double
332 tor_mathlog(double d)
334 return log(d);
337 /** Return the long integer closest to d. We define this wrapper here so
338 * that not all users of math.h need to use the right incancations to get
339 * the c99 functions. */
340 long
341 tor_lround(double d)
343 #if defined(HAVE_LROUND)
344 return lround(d);
345 #elif defined(HAVE_RINT)
346 return (long)rint(d);
347 #else
348 return (long)(d > 0 ? d + 0.5 : ceil(d - 0.5));
349 #endif
352 /** Returns floor(log2(u64)). If u64 is 0, (incorrectly) returns 0. */
354 tor_log2(uint64_t u64)
356 int r = 0;
357 if (u64 >= (U64_LITERAL(1)<<32)) {
358 u64 >>= 32;
359 r = 32;
361 if (u64 >= (U64_LITERAL(1)<<16)) {
362 u64 >>= 16;
363 r += 16;
365 if (u64 >= (U64_LITERAL(1)<<8)) {
366 u64 >>= 8;
367 r += 8;
369 if (u64 >= (U64_LITERAL(1)<<4)) {
370 u64 >>= 4;
371 r += 4;
373 if (u64 >= (U64_LITERAL(1)<<2)) {
374 u64 >>= 2;
375 r += 2;
377 if (u64 >= (U64_LITERAL(1)<<1)) {
378 u64 >>= 1;
379 r += 1;
381 return r;
384 /** Return the power of 2 closest to <b>u64</b>. */
385 uint64_t
386 round_to_power_of_2(uint64_t u64)
388 int lg2 = tor_log2(u64);
389 uint64_t low = U64_LITERAL(1) << lg2, high = U64_LITERAL(1) << (lg2+1);
390 if (high - u64 < u64 - low)
391 return high;
392 else
393 return low;
396 /** Return the lowest x such that x is at least <b>number</b>, and x modulo
397 * <b>divisor</b> == 0. */
398 unsigned
399 round_to_next_multiple_of(unsigned number, unsigned divisor)
401 number += divisor - 1;
402 number -= number % divisor;
403 return number;
406 /** Return the lowest x such that x is at least <b>number</b>, and x modulo
407 * <b>divisor</b> == 0. */
408 uint32_t
409 round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor)
411 number += divisor - 1;
412 number -= number % divisor;
413 return number;
416 /** Return the lowest x such that x is at least <b>number</b>, and x modulo
417 * <b>divisor</b> == 0. */
418 uint64_t
419 round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor)
421 number += divisor - 1;
422 number -= number % divisor;
423 return number;
426 /** Return the number of bits set in <b>v</b>. */
428 n_bits_set_u8(uint8_t v)
430 static const int nybble_table[] = {
431 0, /* 0000 */
432 1, /* 0001 */
433 1, /* 0010 */
434 2, /* 0011 */
435 1, /* 0100 */
436 2, /* 0101 */
437 2, /* 0110 */
438 3, /* 0111 */
439 1, /* 1000 */
440 2, /* 1001 */
441 2, /* 1010 */
442 3, /* 1011 */
443 2, /* 1100 */
444 3, /* 1101 */
445 3, /* 1110 */
446 4, /* 1111 */
449 return nybble_table[v & 15] + nybble_table[v>>4];
452 /* =====
453 * String manipulation
454 * ===== */
456 /** Remove from the string <b>s</b> every character which appears in
457 * <b>strip</b>. */
458 void
459 tor_strstrip(char *s, const char *strip)
461 char *read = s;
462 while (*read) {
463 if (strchr(strip, *read)) {
464 ++read;
465 } else {
466 *s++ = *read++;
469 *s = '\0';
472 /** Return a pointer to a NUL-terminated hexadecimal string encoding
473 * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
474 * result does not need to be deallocated, but repeated calls to
475 * hex_str will trash old results.
477 const char *
478 hex_str(const char *from, size_t fromlen)
480 static char buf[65];
481 if (fromlen>(sizeof(buf)-1)/2)
482 fromlen = (sizeof(buf)-1)/2;
483 base16_encode(buf,sizeof(buf),from,fromlen);
484 return buf;
487 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
488 * lowercase. */
489 void
490 tor_strlower(char *s)
492 while (*s) {
493 *s = TOR_TOLOWER(*s);
494 ++s;
498 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
499 * lowercase. */
500 void
501 tor_strupper(char *s)
503 while (*s) {
504 *s = TOR_TOUPPER(*s);
505 ++s;
509 /** Return 1 if every character in <b>s</b> is printable, else return 0.
512 tor_strisprint(const char *s)
514 while (*s) {
515 if (!TOR_ISPRINT(*s))
516 return 0;
517 s++;
519 return 1;
522 /** Return 1 if no character in <b>s</b> is uppercase, else return 0.
525 tor_strisnonupper(const char *s)
527 while (*s) {
528 if (TOR_ISUPPER(*s))
529 return 0;
530 s++;
532 return 1;
535 /** As strcmp, except that either string may be NULL. The NULL string is
536 * considered to be before any non-NULL string. */
538 strcmp_opt(const char *s1, const char *s2)
540 if (!s1) {
541 if (!s2)
542 return 0;
543 else
544 return -1;
545 } else if (!s2) {
546 return 1;
547 } else {
548 return strcmp(s1, s2);
552 /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
553 * strcmp.
556 strcmpstart(const char *s1, const char *s2)
558 size_t n = strlen(s2);
559 return strncmp(s1, s2, n);
562 /** Compare the s1_len-byte string <b>s1</b> with <b>s2</b>,
563 * without depending on a terminating nul in s1. Sorting order is first by
564 * length, then lexically; return values are as for strcmp.
567 strcmp_len(const char *s1, const char *s2, size_t s1_len)
569 size_t s2_len = strlen(s2);
570 if (s1_len < s2_len)
571 return -1;
572 if (s1_len > s2_len)
573 return 1;
574 return fast_memcmp(s1, s2, s2_len);
577 /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
578 * strcasecmp.
581 strcasecmpstart(const char *s1, const char *s2)
583 size_t n = strlen(s2);
584 return strncasecmp(s1, s2, n);
587 /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
588 * strcmp.
591 strcmpend(const char *s1, const char *s2)
593 size_t n1 = strlen(s1), n2 = strlen(s2);
594 if (n2>n1)
595 return strcmp(s1,s2);
596 else
597 return strncmp(s1+(n1-n2), s2, n2);
600 /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
601 * strcasecmp.
604 strcasecmpend(const char *s1, const char *s2)
606 size_t n1 = strlen(s1), n2 = strlen(s2);
607 if (n2>n1) /* then they can't be the same; figure out which is bigger */
608 return strcasecmp(s1,s2);
609 else
610 return strncasecmp(s1+(n1-n2), s2, n2);
613 /** Compare the value of the string <b>prefix</b> with the start of the
614 * <b>memlen</b>-byte memory chunk at <b>mem</b>. Return as for strcmp.
616 * [As fast_memcmp(mem, prefix, strlen(prefix)) but returns -1 if memlen is
617 * less than strlen(prefix).]
620 fast_memcmpstart(const void *mem, size_t memlen,
621 const char *prefix)
623 size_t plen = strlen(prefix);
624 if (memlen < plen)
625 return -1;
626 return fast_memcmp(mem, prefix, plen);
629 /** Return a pointer to the first char of s that is not whitespace and
630 * not a comment, or to the terminating NUL if no such character exists.
632 const char *
633 eat_whitespace(const char *s)
635 tor_assert(s);
637 while (1) {
638 switch (*s) {
639 case '\0':
640 default:
641 return s;
642 case ' ':
643 case '\t':
644 case '\n':
645 case '\r':
646 ++s;
647 break;
648 case '#':
649 ++s;
650 while (*s && *s != '\n')
651 ++s;
656 /** Return a pointer to the first char of s that is not whitespace and
657 * not a comment, or to the terminating NUL if no such character exists.
659 const char *
660 eat_whitespace_eos(const char *s, const char *eos)
662 tor_assert(s);
663 tor_assert(eos && s <= eos);
665 while (s < eos) {
666 switch (*s) {
667 case '\0':
668 default:
669 return s;
670 case ' ':
671 case '\t':
672 case '\n':
673 case '\r':
674 ++s;
675 break;
676 case '#':
677 ++s;
678 while (s < eos && *s && *s != '\n')
679 ++s;
682 return s;
685 /** Return a pointer to the first char of s that is not a space or a tab
686 * or a \\r, or to the terminating NUL if no such character exists. */
687 const char *
688 eat_whitespace_no_nl(const char *s)
690 while (*s == ' ' || *s == '\t' || *s == '\r')
691 ++s;
692 return s;
695 /** As eat_whitespace_no_nl, but stop at <b>eos</b> whether we have
696 * found a non-whitespace character or not. */
697 const char *
698 eat_whitespace_eos_no_nl(const char *s, const char *eos)
700 while (s < eos && (*s == ' ' || *s == '\t' || *s == '\r'))
701 ++s;
702 return s;
705 /** Return a pointer to the first char of s that is whitespace or <b>#</b>,
706 * or to the terminating NUL if no such character exists.
708 const char *
709 find_whitespace(const char *s)
711 /* tor_assert(s); */
712 while (1) {
713 switch (*s)
715 case '\0':
716 case '#':
717 case ' ':
718 case '\r':
719 case '\n':
720 case '\t':
721 return s;
722 default:
723 ++s;
728 /** As find_whitespace, but stop at <b>eos</b> whether we have found a
729 * whitespace or not. */
730 const char *
731 find_whitespace_eos(const char *s, const char *eos)
733 /* tor_assert(s); */
734 while (s < eos) {
735 switch (*s)
737 case '\0':
738 case '#':
739 case ' ':
740 case '\r':
741 case '\n':
742 case '\t':
743 return s;
744 default:
745 ++s;
748 return s;
751 /** Return the first occurrence of <b>needle</b> in <b>haystack</b> that
752 * occurs at the start of a line (that is, at the beginning of <b>haystack</b>
753 * or immediately after a newline). Return NULL if no such string is found.
755 const char *
756 find_str_at_start_of_line(const char *haystack, const char *needle)
758 size_t needle_len = strlen(needle);
760 do {
761 if (!strncmp(haystack, needle, needle_len))
762 return haystack;
764 haystack = strchr(haystack, '\n');
765 if (!haystack)
766 return NULL;
767 else
768 ++haystack;
769 } while (*haystack);
771 return NULL;
774 /** Returns true if <b>string</b> could be a C identifier.
775 A C identifier must begin with a letter or an underscore and the
776 rest of its characters can be letters, numbers or underscores. No
777 length limit is imposed. */
779 string_is_C_identifier(const char *string)
781 size_t iter;
782 size_t length = strlen(string);
783 if (!length)
784 return 0;
786 for (iter = 0; iter < length ; iter++) {
787 if (iter == 0) {
788 if (!(TOR_ISALPHA(string[iter]) ||
789 string[iter] == '_'))
790 return 0;
791 } else {
792 if (!(TOR_ISALPHA(string[iter]) ||
793 TOR_ISDIGIT(string[iter]) ||
794 string[iter] == '_'))
795 return 0;
799 return 1;
802 /** Return true iff the 'len' bytes at 'mem' are all zero. */
804 tor_mem_is_zero(const char *mem, size_t len)
806 static const char ZERO[] = {
807 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,
809 while (len >= sizeof(ZERO)) {
810 /* It's safe to use fast_memcmp here, since the very worst thing an
811 * attacker could learn is how many initial bytes of a secret were zero */
812 if (fast_memcmp(mem, ZERO, sizeof(ZERO)))
813 return 0;
814 len -= sizeof(ZERO);
815 mem += sizeof(ZERO);
817 /* Deal with leftover bytes. */
818 if (len)
819 return fast_memeq(mem, ZERO, len);
821 return 1;
824 /** Return true iff the DIGEST_LEN bytes in digest are all zero. */
826 tor_digest_is_zero(const char *digest)
828 static const uint8_t ZERO_DIGEST[] = {
829 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
831 return tor_memeq(digest, ZERO_DIGEST, DIGEST_LEN);
834 /** Return true iff the DIGEST256_LEN bytes in digest are all zero. */
836 tor_digest256_is_zero(const char *digest)
838 return tor_mem_is_zero(digest, DIGEST256_LEN);
841 /* Helper: common code to check whether the result of a strtol or strtoul or
842 * strtoll is correct. */
843 #define CHECK_STRTOX_RESULT() \
844 /* Was at least one character converted? */ \
845 if (endptr == s) \
846 goto err; \
847 /* Were there unexpected unconverted characters? */ \
848 if (!next && *endptr) \
849 goto err; \
850 /* Is r within limits? */ \
851 if (r < min || r > max) \
852 goto err; \
853 if (ok) *ok = 1; \
854 if (next) *next = endptr; \
855 return r; \
856 err: \
857 if (ok) *ok = 0; \
858 if (next) *next = endptr; \
859 return 0
861 /** Extract a long from the start of <b>s</b>, in the given numeric
862 * <b>base</b>. If <b>base</b> is 0, <b>s</b> is parsed as a decimal,
863 * octal, or hex number in the syntax of a C integer literal. If
864 * there is unconverted data and <b>next</b> is provided, set
865 * *<b>next</b> to the first unconverted character. An error has
866 * occurred if no characters are converted; or if there are
867 * unconverted characters and <b>next</b> is NULL; or if the parsed
868 * value is not between <b>min</b> and <b>max</b>. When no error
869 * occurs, return the parsed value and set *<b>ok</b> (if provided) to
870 * 1. When an error occurs, return 0 and set *<b>ok</b> (if provided)
871 * to 0.
873 long
874 tor_parse_long(const char *s, int base, long min, long max,
875 int *ok, char **next)
877 char *endptr;
878 long r;
880 r = strtol(s, &endptr, base);
881 CHECK_STRTOX_RESULT();
884 /** As tor_parse_long(), but return an unsigned long. */
885 unsigned long
886 tor_parse_ulong(const char *s, int base, unsigned long min,
887 unsigned long max, int *ok, char **next)
889 char *endptr;
890 unsigned long r;
892 r = strtoul(s, &endptr, base);
893 CHECK_STRTOX_RESULT();
896 /** As tor_parse_long(), but return a double. */
897 double
898 tor_parse_double(const char *s, double min, double max, int *ok, char **next)
900 char *endptr;
901 double r;
903 r = strtod(s, &endptr);
904 CHECK_STRTOX_RESULT();
907 /** As tor_parse_long, but return a uint64_t. Only base 10 is guaranteed to
908 * work for now. */
909 uint64_t
910 tor_parse_uint64(const char *s, int base, uint64_t min,
911 uint64_t max, int *ok, char **next)
913 char *endptr;
914 uint64_t r;
916 #ifdef HAVE_STRTOULL
917 r = (uint64_t)strtoull(s, &endptr, base);
918 #elif defined(MS_WINDOWS)
919 #if defined(_MSC_VER) && _MSC_VER < 1300
920 tor_assert(base <= 10);
921 r = (uint64_t)_atoi64(s);
922 endptr = (char*)s;
923 while (TOR_ISSPACE(*endptr)) endptr++;
924 while (TOR_ISDIGIT(*endptr)) endptr++;
925 #else
926 r = (uint64_t)_strtoui64(s, &endptr, base);
927 #endif
928 #elif SIZEOF_LONG == 8
929 r = (uint64_t)strtoul(s, &endptr, base);
930 #else
931 #error "I don't know how to parse 64-bit numbers."
932 #endif
934 CHECK_STRTOX_RESULT();
937 /** Encode the <b>srclen</b> bytes at <b>src</b> in a NUL-terminated,
938 * uppercase hexadecimal string; store it in the <b>destlen</b>-byte buffer
939 * <b>dest</b>.
941 void
942 base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
944 const char *end;
945 char *cp;
947 tor_assert(destlen >= srclen*2+1);
948 tor_assert(destlen < SIZE_T_CEILING);
950 cp = dest;
951 end = src+srclen;
952 while (src<end) {
953 *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) >> 4 ];
954 *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) & 0xf ];
955 ++src;
957 *cp = '\0';
960 /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
961 static INLINE int
962 _hex_decode_digit(char c)
964 switch (c) {
965 case '0': return 0;
966 case '1': return 1;
967 case '2': return 2;
968 case '3': return 3;
969 case '4': return 4;
970 case '5': return 5;
971 case '6': return 6;
972 case '7': return 7;
973 case '8': return 8;
974 case '9': return 9;
975 case 'A': case 'a': return 10;
976 case 'B': case 'b': return 11;
977 case 'C': case 'c': return 12;
978 case 'D': case 'd': return 13;
979 case 'E': case 'e': return 14;
980 case 'F': case 'f': return 15;
981 default:
982 return -1;
986 /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
988 hex_decode_digit(char c)
990 return _hex_decode_digit(c);
993 /** Given a hexadecimal string of <b>srclen</b> bytes in <b>src</b>, decode it
994 * and store the result in the <b>destlen</b>-byte buffer at <b>dest</b>.
995 * Return 0 on success, -1 on failure. */
997 base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
999 const char *end;
1001 int v1,v2;
1002 if ((srclen % 2) != 0)
1003 return -1;
1004 if (destlen < srclen/2 || destlen > SIZE_T_CEILING)
1005 return -1;
1006 end = src+srclen;
1007 while (src<end) {
1008 v1 = _hex_decode_digit(*src);
1009 v2 = _hex_decode_digit(*(src+1));
1010 if (v1<0||v2<0)
1011 return -1;
1012 *(uint8_t*)dest = (v1<<4)|v2;
1013 ++dest;
1014 src+=2;
1016 return 0;
1019 /** Allocate and return a new string representing the contents of <b>s</b>,
1020 * surrounded by quotes and using standard C escapes.
1022 * Generally, we use this for logging values that come in over the network to
1023 * keep them from tricking users, and for sending certain values to the
1024 * controller.
1026 * We trust values from the resolver, OS, configuration file, and command line
1027 * to not be maliciously ill-formed. We validate incoming routerdescs and
1028 * SOCKS requests and addresses from BEGIN cells as they're parsed;
1029 * afterwards, we trust them as non-malicious.
1031 char *
1032 esc_for_log(const char *s)
1034 const char *cp;
1035 char *result, *outp;
1036 size_t len = 3;
1037 if (!s) {
1038 return tor_strdup("(null)");
1041 for (cp = s; *cp; ++cp) {
1042 switch (*cp) {
1043 case '\\':
1044 case '\"':
1045 case '\'':
1046 case '\r':
1047 case '\n':
1048 case '\t':
1049 len += 2;
1050 break;
1051 default:
1052 if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127)
1053 ++len;
1054 else
1055 len += 4;
1056 break;
1060 result = outp = tor_malloc(len);
1061 *outp++ = '\"';
1062 for (cp = s; *cp; ++cp) {
1063 switch (*cp) {
1064 case '\\':
1065 case '\"':
1066 case '\'':
1067 *outp++ = '\\';
1068 *outp++ = *cp;
1069 break;
1070 case '\n':
1071 *outp++ = '\\';
1072 *outp++ = 'n';
1073 break;
1074 case '\t':
1075 *outp++ = '\\';
1076 *outp++ = 't';
1077 break;
1078 case '\r':
1079 *outp++ = '\\';
1080 *outp++ = 'r';
1081 break;
1082 default:
1083 if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127) {
1084 *outp++ = *cp;
1085 } else {
1086 tor_snprintf(outp, 5, "\\%03o", (int)(uint8_t) *cp);
1087 outp += 4;
1089 break;
1093 *outp++ = '\"';
1094 *outp++ = 0;
1096 return result;
1099 /** Allocate and return a new string representing the contents of <b>s</b>,
1100 * surrounded by quotes and using standard C escapes.
1102 * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
1103 * thread. Also, each call invalidates the last-returned value, so don't
1104 * try log_warn(LD_GENERAL, "%s %s", escaped(a), escaped(b));
1106 const char *
1107 escaped(const char *s)
1109 static char *_escaped_val = NULL;
1110 tor_free(_escaped_val);
1112 if (s)
1113 _escaped_val = esc_for_log(s);
1114 else
1115 _escaped_val = NULL;
1117 return _escaped_val;
1120 /** Rudimentary string wrapping code: given a un-wrapped <b>string</b> (no
1121 * newlines!), break the string into newline-terminated lines of no more than
1122 * <b>width</b> characters long (not counting newline) and insert them into
1123 * <b>out</b> in order. Precede the first line with prefix0, and subsequent
1124 * lines with prefixRest.
1126 /* This uses a stupid greedy wrapping algorithm right now:
1127 * - For each line:
1128 * - Try to fit as much stuff as possible, but break on a space.
1129 * - If the first "word" of the line will extend beyond the allowable
1130 * width, break the word at the end of the width.
1132 void
1133 wrap_string(smartlist_t *out, const char *string, size_t width,
1134 const char *prefix0, const char *prefixRest)
1136 size_t p0Len, pRestLen, pCurLen;
1137 const char *eos, *prefixCur;
1138 tor_assert(out);
1139 tor_assert(string);
1140 tor_assert(width);
1141 if (!prefix0)
1142 prefix0 = "";
1143 if (!prefixRest)
1144 prefixRest = "";
1146 p0Len = strlen(prefix0);
1147 pRestLen = strlen(prefixRest);
1148 tor_assert(width > p0Len && width > pRestLen);
1149 eos = strchr(string, '\0');
1150 tor_assert(eos);
1151 pCurLen = p0Len;
1152 prefixCur = prefix0;
1154 while ((eos-string)+pCurLen > width) {
1155 const char *eol = string + width - pCurLen;
1156 while (eol > string && *eol != ' ')
1157 --eol;
1158 /* eol is now the last space that can fit, or the start of the string. */
1159 if (eol > string) {
1160 size_t line_len = (eol-string) + pCurLen + 2;
1161 char *line = tor_malloc(line_len);
1162 memcpy(line, prefixCur, pCurLen);
1163 memcpy(line+pCurLen, string, eol-string);
1164 line[line_len-2] = '\n';
1165 line[line_len-1] = '\0';
1166 smartlist_add(out, line);
1167 string = eol + 1;
1168 } else {
1169 size_t line_len = width + 2;
1170 char *line = tor_malloc(line_len);
1171 memcpy(line, prefixCur, pCurLen);
1172 memcpy(line+pCurLen, string, width - pCurLen);
1173 line[line_len-2] = '\n';
1174 line[line_len-1] = '\0';
1175 smartlist_add(out, line);
1176 string += width-pCurLen;
1178 prefixCur = prefixRest;
1179 pCurLen = pRestLen;
1182 if (string < eos) {
1183 size_t line_len = (eos-string) + pCurLen + 2;
1184 char *line = tor_malloc(line_len);
1185 memcpy(line, prefixCur, pCurLen);
1186 memcpy(line+pCurLen, string, eos-string);
1187 line[line_len-2] = '\n';
1188 line[line_len-1] = '\0';
1189 smartlist_add(out, line);
1193 /* =====
1194 * Time
1195 * ===== */
1198 * Converts struct timeval to a double value.
1199 * Preserves microsecond precision, but just barely.
1200 * Error is approx +/- 0.1 usec when dealing with epoch values.
1202 double
1203 tv_to_double(const struct timeval *tv)
1205 double conv = tv->tv_sec;
1206 conv += tv->tv_usec/1000000.0;
1207 return conv;
1211 * Converts timeval to milliseconds.
1213 int64_t
1214 tv_to_msec(const struct timeval *tv)
1216 int64_t conv = ((int64_t)tv->tv_sec)*1000L;
1217 /* Round ghetto-style */
1218 conv += ((int64_t)tv->tv_usec+500)/1000L;
1219 return conv;
1223 * Converts timeval to microseconds.
1225 int64_t
1226 tv_to_usec(const struct timeval *tv)
1228 int64_t conv = ((int64_t)tv->tv_sec)*1000000L;
1229 conv += tv->tv_usec;
1230 return conv;
1233 /** Return the number of microseconds elapsed between *start and *end.
1235 long
1236 tv_udiff(const struct timeval *start, const struct timeval *end)
1238 long udiff;
1239 long secdiff = end->tv_sec - start->tv_sec;
1241 if (labs(secdiff+1) > LONG_MAX/1000000) {
1242 log_warn(LD_GENERAL, "comparing times on microsecond detail too far "
1243 "apart: %ld seconds", secdiff);
1244 return LONG_MAX;
1247 udiff = secdiff*1000000L + (end->tv_usec - start->tv_usec);
1248 return udiff;
1251 /** Return the number of milliseconds elapsed between *start and *end.
1253 long
1254 tv_mdiff(const struct timeval *start, const struct timeval *end)
1256 long mdiff;
1257 long secdiff = end->tv_sec - start->tv_sec;
1259 if (labs(secdiff+1) > LONG_MAX/1000) {
1260 log_warn(LD_GENERAL, "comparing times on millisecond detail too far "
1261 "apart: %ld seconds", secdiff);
1262 return LONG_MAX;
1265 /* Subtract and round */
1266 mdiff = secdiff*1000L +
1267 ((long)end->tv_usec - (long)start->tv_usec + 500L) / 1000L;
1268 return mdiff;
1271 /** Yield true iff <b>y</b> is a leap-year. */
1272 #define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
1273 /** Helper: Return the number of leap-days between Jan 1, y1 and Jan 1, y2. */
1274 static int
1275 n_leapdays(int y1, int y2)
1277 --y1;
1278 --y2;
1279 return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
1281 /** Number of days per month in non-leap year; used by tor_timegm. */
1282 static const int days_per_month[] =
1283 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1285 /** Return a time_t given a struct tm. The result is given in GMT, and
1286 * does not account for leap seconds.
1288 time_t
1289 tor_timegm(struct tm *tm)
1291 /* This is a pretty ironclad timegm implementation, snarfed from Python2.2.
1292 * It's way more brute-force than fiddling with tzset().
1294 time_t year, days, hours, minutes, seconds;
1295 int i;
1296 year = tm->tm_year + 1900;
1297 if (year < 1970 || tm->tm_mon < 0 || tm->tm_mon > 11) {
1298 log_warn(LD_BUG, "Out-of-range argument to tor_timegm");
1299 return -1;
1301 tor_assert(year < INT_MAX);
1302 days = 365 * (year-1970) + n_leapdays(1970,(int)year);
1303 for (i = 0; i < tm->tm_mon; ++i)
1304 days += days_per_month[i];
1305 if (tm->tm_mon > 1 && IS_LEAPYEAR(year))
1306 ++days;
1307 days += tm->tm_mday - 1;
1308 hours = days*24 + tm->tm_hour;
1310 minutes = hours*60 + tm->tm_min;
1311 seconds = minutes*60 + tm->tm_sec;
1312 return seconds;
1315 /* strftime is locale-specific, so we need to replace those parts */
1317 /** A c-locale array of 3-letter names of weekdays, starting with Sun. */
1318 static const char *WEEKDAY_NAMES[] =
1319 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
1320 /** A c-locale array of 3-letter names of months, starting with Jan. */
1321 static const char *MONTH_NAMES[] =
1322 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1323 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1325 /** Set <b>buf</b> to the RFC1123 encoding of the GMT value of <b>t</b>.
1326 * The buffer must be at least RFC1123_TIME_LEN+1 bytes long.
1328 * (RFC1123 format is Fri, 29 Sep 2006 15:54:20 GMT)
1330 void
1331 format_rfc1123_time(char *buf, time_t t)
1333 struct tm tm;
1335 tor_gmtime_r(&t, &tm);
1337 strftime(buf, RFC1123_TIME_LEN+1, "___, %d ___ %Y %H:%M:%S GMT", &tm);
1338 tor_assert(tm.tm_wday >= 0);
1339 tor_assert(tm.tm_wday <= 6);
1340 memcpy(buf, WEEKDAY_NAMES[tm.tm_wday], 3);
1341 tor_assert(tm.tm_wday >= 0);
1342 tor_assert(tm.tm_mon <= 11);
1343 memcpy(buf+8, MONTH_NAMES[tm.tm_mon], 3);
1346 /** Parse the RFC1123 encoding of some time (in GMT) from <b>buf</b>,
1347 * and store the result in *<b>t</b>.
1349 * Return 0 on success, -1 on failure.
1352 parse_rfc1123_time(const char *buf, time_t *t)
1354 struct tm tm;
1355 char month[4];
1356 char weekday[4];
1357 int i, m;
1358 unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
1360 if (strlen(buf) != RFC1123_TIME_LEN)
1361 return -1;
1362 memset(&tm, 0, sizeof(tm));
1363 if (tor_sscanf(buf, "%3s, %2u %3s %u %2u:%2u:%2u GMT", weekday,
1364 &tm_mday, month, &tm_year, &tm_hour,
1365 &tm_min, &tm_sec) < 7) {
1366 char *esc = esc_for_log(buf);
1367 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
1368 tor_free(esc);
1369 return -1;
1371 if (tm_mday > 31 || tm_hour > 23 || tm_min > 59 || tm_sec > 61) {
1372 char *esc = esc_for_log(buf);
1373 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
1374 tor_free(esc);
1375 return -1;
1377 tm.tm_mday = (int)tm_mday;
1378 tm.tm_year = (int)tm_year;
1379 tm.tm_hour = (int)tm_hour;
1380 tm.tm_min = (int)tm_min;
1381 tm.tm_sec = (int)tm_sec;
1383 m = -1;
1384 for (i = 0; i < 12; ++i) {
1385 if (!strcmp(month, MONTH_NAMES[i])) {
1386 m = i;
1387 break;
1390 if (m<0) {
1391 char *esc = esc_for_log(buf);
1392 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s: No such month", esc);
1393 tor_free(esc);
1394 return -1;
1396 tm.tm_mon = m;
1398 if (tm.tm_year < 1970) {
1399 char *esc = esc_for_log(buf);
1400 log_warn(LD_GENERAL,
1401 "Got invalid RFC1123 time %s. (Before 1970)", esc);
1402 tor_free(esc);
1403 return -1;
1405 tm.tm_year -= 1900;
1407 *t = tor_timegm(&tm);
1408 return 0;
1411 /** Set <b>buf</b> to the ISO8601 encoding of the local value of <b>t</b>.
1412 * The buffer must be at least ISO_TIME_LEN+1 bytes long.
1414 * (ISO8601 format is 2006-10-29 10:57:20)
1416 void
1417 format_local_iso_time(char *buf, time_t t)
1419 struct tm tm;
1420 strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_localtime_r(&t, &tm));
1423 /** Set <b>buf</b> to the ISO8601 encoding of the GMT value of <b>t</b>.
1424 * The buffer must be at least ISO_TIME_LEN+1 bytes long.
1426 void
1427 format_iso_time(char *buf, time_t t)
1429 struct tm tm;
1430 strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t, &tm));
1433 /** Given an ISO-formatted UTC time value (after the epoch) in <b>cp</b>,
1434 * parse it and store its value in *<b>t</b>. Return 0 on success, -1 on
1435 * failure. Ignore extraneous stuff in <b>cp</b> separated by whitespace from
1436 * the end of the time string. */
1438 parse_iso_time(const char *cp, time_t *t)
1440 struct tm st_tm;
1441 unsigned int year=0, month=0, day=0, hour=100, minute=100, second=100;
1442 if (tor_sscanf(cp, "%u-%2u-%2u %2u:%2u:%2u", &year, &month,
1443 &day, &hour, &minute, &second) < 6) {
1444 char *esc = esc_for_log(cp);
1445 log_warn(LD_GENERAL, "ISO time %s was unparseable", esc);
1446 tor_free(esc);
1447 return -1;
1449 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
1450 hour > 23 || minute > 59 || second > 61) {
1451 char *esc = esc_for_log(cp);
1452 log_warn(LD_GENERAL, "ISO time %s was nonsensical", esc);
1453 tor_free(esc);
1454 return -1;
1456 st_tm.tm_year = year-1900;
1457 st_tm.tm_mon = month-1;
1458 st_tm.tm_mday = day;
1459 st_tm.tm_hour = hour;
1460 st_tm.tm_min = minute;
1461 st_tm.tm_sec = second;
1463 if (st_tm.tm_year < 70) {
1464 char *esc = esc_for_log(cp);
1465 log_warn(LD_GENERAL, "Got invalid ISO time %s. (Before 1970)", esc);
1466 tor_free(esc);
1467 return -1;
1469 *t = tor_timegm(&st_tm);
1470 return 0;
1473 /** Given a <b>date</b> in one of the three formats allowed by HTTP (ugh),
1474 * parse it into <b>tm</b>. Return 0 on success, negative on failure. */
1476 parse_http_time(const char *date, struct tm *tm)
1478 const char *cp;
1479 char month[4];
1480 char wkday[4];
1481 int i;
1482 unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
1484 tor_assert(tm);
1485 memset(tm, 0, sizeof(*tm));
1487 /* First, try RFC1123 or RFC850 format: skip the weekday. */
1488 if ((cp = strchr(date, ','))) {
1489 ++cp;
1490 if (tor_sscanf(date, "%2u %3s %4u %2u:%2u:%2u GMT",
1491 &tm_mday, month, &tm_year,
1492 &tm_hour, &tm_min, &tm_sec) == 6) {
1493 /* rfc1123-date */
1494 tm_year -= 1900;
1495 } else if (tor_sscanf(date, "%2u-%3s-%2u %2u:%2u:%2u GMT",
1496 &tm_mday, month, &tm_year,
1497 &tm_hour, &tm_min, &tm_sec) == 6) {
1498 /* rfc850-date */
1499 } else {
1500 return -1;
1502 } else {
1503 /* No comma; possibly asctime() format. */
1504 if (tor_sscanf(date, "%3s %3s %2u %2u:%2u:%2u %4u",
1505 wkday, month, &tm_mday,
1506 &tm_hour, &tm_min, &tm_sec, &tm_year) == 7) {
1507 tm_year -= 1900;
1508 } else {
1509 return -1;
1512 tm->tm_mday = (int)tm_mday;
1513 tm->tm_year = (int)tm_year;
1514 tm->tm_hour = (int)tm_hour;
1515 tm->tm_min = (int)tm_min;
1516 tm->tm_sec = (int)tm_sec;
1518 month[3] = '\0';
1519 /* Okay, now decode the month. */
1520 for (i = 0; i < 12; ++i) {
1521 if (!strcasecmp(MONTH_NAMES[i], month)) {
1522 tm->tm_mon = i+1;
1526 if (tm->tm_year < 0 ||
1527 tm->tm_mon < 1 || tm->tm_mon > 12 ||
1528 tm->tm_mday < 0 || tm->tm_mday > 31 ||
1529 tm->tm_hour < 0 || tm->tm_hour > 23 ||
1530 tm->tm_min < 0 || tm->tm_min > 59 ||
1531 tm->tm_sec < 0 || tm->tm_sec > 61)
1532 return -1; /* Out of range, or bad month. */
1534 return 0;
1537 /** Given an <b>interval</b> in seconds, try to write it to the
1538 * <b>out_len</b>-byte buffer in <b>out</b> in a human-readable form.
1539 * Return 0 on success, -1 on failure.
1542 format_time_interval(char *out, size_t out_len, long interval)
1544 /* We only report seconds if there's no hours. */
1545 long sec = 0, min = 0, hour = 0, day = 0;
1546 if (interval < 0)
1547 interval = -interval;
1549 if (interval >= 86400) {
1550 day = interval / 86400;
1551 interval %= 86400;
1553 if (interval >= 3600) {
1554 hour = interval / 3600;
1555 interval %= 3600;
1557 if (interval >= 60) {
1558 min = interval / 60;
1559 interval %= 60;
1561 sec = interval;
1563 if (day) {
1564 return tor_snprintf(out, out_len, "%ld days, %ld hours, %ld minutes",
1565 day, hour, min);
1566 } else if (hour) {
1567 return tor_snprintf(out, out_len, "%ld hours, %ld minutes", hour, min);
1568 } else if (min) {
1569 return tor_snprintf(out, out_len, "%ld minutes, %ld seconds", min, sec);
1570 } else {
1571 return tor_snprintf(out, out_len, "%ld seconds", sec);
1575 /* =====
1576 * Cached time
1577 * ===== */
1579 #ifndef TIME_IS_FAST
1580 /** Cached estimate of the current time. Updated around once per second;
1581 * may be a few seconds off if we are really busy. This is a hack to avoid
1582 * calling time(NULL) (which not everybody has optimized) on critical paths.
1584 static time_t cached_approx_time = 0;
1586 /** Return a cached estimate of the current time from when
1587 * update_approx_time() was last called. This is a hack to avoid calling
1588 * time(NULL) on critical paths: please do not even think of calling it
1589 * anywhere else. */
1590 time_t
1591 approx_time(void)
1593 return cached_approx_time;
1596 /** Update the cached estimate of the current time. This function SHOULD be
1597 * called once per second, and MUST be called before the first call to
1598 * get_approx_time. */
1599 void
1600 update_approx_time(time_t now)
1602 cached_approx_time = now;
1604 #endif
1606 /* =====
1607 * Rate limiting
1608 * ===== */
1610 /** If the rate-limiter <b>lim</b> is ready at <b>now</b>, return the number
1611 * of calls to rate_limit_is_ready (including this one!) since the last time
1612 * rate_limit_is_ready returned nonzero. Otherwise return 0. */
1613 static int
1614 rate_limit_is_ready(ratelim_t *lim, time_t now)
1616 if (lim->rate + lim->last_allowed <= now) {
1617 int res = lim->n_calls_since_last_time + 1;
1618 lim->last_allowed = now;
1619 lim->n_calls_since_last_time = 0;
1620 return res;
1621 } else {
1622 ++lim->n_calls_since_last_time;
1623 return 0;
1627 /** If the rate-limiter <b>lim</b> is ready at <b>now</b>, return a newly
1628 * allocated string indicating how many messages were suppressed, suitable to
1629 * append to a log message. Otherwise return NULL. */
1630 char *
1631 rate_limit_log(ratelim_t *lim, time_t now)
1633 int n;
1634 if ((n = rate_limit_is_ready(lim, now))) {
1635 if (n == 1) {
1636 return tor_strdup("");
1637 } else {
1638 char *cp=NULL;
1639 tor_asprintf(&cp,
1640 " [%d similar message(s) suppressed in last %d seconds]",
1641 n-1, lim->rate);
1642 return cp;
1644 } else {
1645 return NULL;
1649 /* =====
1650 * File helpers
1651 * ===== */
1653 /** Write <b>count</b> bytes from <b>buf</b> to <b>fd</b>. <b>isSocket</b>
1654 * must be 1 if fd was returned by socket() or accept(), and 0 if fd
1655 * was returned by open(). Return the number of bytes written, or -1
1656 * on error. Only use if fd is a blocking fd. */
1657 ssize_t
1658 write_all(tor_socket_t fd, const char *buf, size_t count, int isSocket)
1660 size_t written = 0;
1661 ssize_t result;
1662 tor_assert(count < SSIZE_T_MAX);
1664 while (written != count) {
1665 if (isSocket)
1666 result = tor_socket_send(fd, buf+written, count-written, 0);
1667 else
1668 result = write((int)fd, buf+written, count-written);
1669 if (result<0)
1670 return -1;
1671 written += result;
1673 return (ssize_t)count;
1676 /** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes
1677 * or reach the end of the file. <b>isSocket</b> must be 1 if fd
1678 * was returned by socket() or accept(), and 0 if fd was returned by
1679 * open(). Return the number of bytes read, or -1 on error. Only use
1680 * if fd is a blocking fd. */
1681 ssize_t
1682 read_all(tor_socket_t fd, char *buf, size_t count, int isSocket)
1684 size_t numread = 0;
1685 ssize_t result;
1687 if (count > SIZE_T_CEILING || count > SSIZE_T_MAX)
1688 return -1;
1690 while (numread != count) {
1691 if (isSocket)
1692 result = tor_socket_recv(fd, buf+numread, count-numread, 0);
1693 else
1694 result = read((int)fd, buf+numread, count-numread);
1695 if (result<0)
1696 return -1;
1697 else if (result == 0)
1698 break;
1699 numread += result;
1701 return (ssize_t)numread;
1705 * Filesystem operations.
1708 /** Clean up <b>name</b> so that we can use it in a call to "stat". On Unix,
1709 * we do nothing. On Windows, we remove a trailing slash, unless the path is
1710 * the root of a disk. */
1711 static void
1712 clean_name_for_stat(char *name)
1714 #ifdef MS_WINDOWS
1715 size_t len = strlen(name);
1716 if (!len)
1717 return;
1718 if (name[len-1]=='\\' || name[len-1]=='/') {
1719 if (len == 1 || (len==3 && name[1]==':'))
1720 return;
1721 name[len-1]='\0';
1723 #else
1724 (void)name;
1725 #endif
1728 /** Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
1729 * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
1730 * directory. On FN_ERROR, sets errno. */
1731 file_status_t
1732 file_status(const char *fname)
1734 struct stat st;
1735 char *f;
1736 int r;
1737 f = tor_strdup(fname);
1738 clean_name_for_stat(f);
1739 r = stat(f, &st);
1740 tor_free(f);
1741 if (r) {
1742 if (errno == ENOENT) {
1743 return FN_NOENT;
1745 return FN_ERROR;
1747 if (st.st_mode & S_IFDIR)
1748 return FN_DIR;
1749 else if (st.st_mode & S_IFREG)
1750 return FN_FILE;
1751 else
1752 return FN_ERROR;
1755 /** Check whether <b>dirname</b> exists and is private. If yes return 0. If
1756 * it does not exist, and <b>check</b>&CPD_CREATE is set, try to create it
1757 * and return 0 on success. If it does not exist, and
1758 * <b>check</b>&CPD_CHECK, and we think we can create it, return 0. Else
1759 * return -1. If CPD_GROUP_OK is set, then it's okay if the directory
1760 * is group-readable, but in all cases we create the directory mode 0700.
1761 * If CPD_CHECK_MODE_ONLY is set, then we don't alter the directory permissions
1762 * if they are too permissive: we just return -1.
1763 * When effective_user is not NULL, check permissions against the given user
1764 * and its primary group.
1767 check_private_dir(const char *dirname, cpd_check_t check,
1768 const char *effective_user)
1770 int r;
1771 struct stat st;
1772 char *f;
1773 #ifndef MS_WINDOWS
1774 int mask;
1775 struct passwd *pw = NULL;
1776 uid_t running_uid;
1777 gid_t running_gid;
1778 #else
1779 (void)effective_user;
1780 #endif
1782 tor_assert(dirname);
1783 f = tor_strdup(dirname);
1784 clean_name_for_stat(f);
1785 r = stat(f, &st);
1786 tor_free(f);
1787 if (r) {
1788 if (errno != ENOENT) {
1789 log_warn(LD_FS, "Directory %s cannot be read: %s", dirname,
1790 strerror(errno));
1791 return -1;
1793 if (check & CPD_CREATE) {
1794 log_info(LD_GENERAL, "Creating directory %s", dirname);
1795 #if defined (MS_WINDOWS) && !defined (WINCE)
1796 r = mkdir(dirname);
1797 #else
1798 r = mkdir(dirname, 0700);
1799 #endif
1800 if (r) {
1801 log_warn(LD_FS, "Error creating directory %s: %s", dirname,
1802 strerror(errno));
1803 return -1;
1805 } else if (!(check & CPD_CHECK)) {
1806 log_warn(LD_FS, "Directory %s does not exist.", dirname);
1807 return -1;
1809 /* XXXX In the case where check==CPD_CHECK, we should look at the
1810 * parent directory a little harder. */
1811 return 0;
1813 if (!(st.st_mode & S_IFDIR)) {
1814 log_warn(LD_FS, "%s is not a directory", dirname);
1815 return -1;
1817 #ifndef MS_WINDOWS
1818 if (effective_user) {
1819 /* Look up the user and group information.
1820 * If we have a problem, bail out. */
1821 pw = getpwnam(effective_user);
1822 if (pw == NULL) {
1823 log_warn(LD_CONFIG, "Error setting configured user: %s not found",
1824 effective_user);
1825 return -1;
1827 running_uid = pw->pw_uid;
1828 running_gid = pw->pw_gid;
1829 } else {
1830 running_uid = getuid();
1831 running_gid = getgid();
1834 if (st.st_uid != running_uid) {
1835 struct passwd *pw = NULL;
1836 char *process_ownername = NULL;
1838 pw = getpwuid(running_uid);
1839 process_ownername = pw ? tor_strdup(pw->pw_name) : tor_strdup("<unknown>");
1841 pw = getpwuid(st.st_uid);
1843 log_warn(LD_FS, "%s is not owned by this user (%s, %d) but by "
1844 "%s (%d). Perhaps you are running Tor as the wrong user?",
1845 dirname, process_ownername, (int)running_uid,
1846 pw ? pw->pw_name : "<unknown>", (int)st.st_uid);
1848 tor_free(process_ownername);
1849 return -1;
1851 if ((check & CPD_GROUP_OK) && st.st_gid != running_gid) {
1852 struct group *gr;
1853 char *process_groupname = NULL;
1854 gr = getgrgid(running_gid);
1855 process_groupname = gr ? tor_strdup(gr->gr_name) : tor_strdup("<unknown>");
1856 gr = getgrgid(st.st_gid);
1858 log_warn(LD_FS, "%s is not owned by this group (%s, %d) but by group "
1859 "%s (%d). Are you running Tor as the wrong user?",
1860 dirname, process_groupname, (int)running_gid,
1861 gr ? gr->gr_name : "<unknown>", (int)st.st_gid);
1863 tor_free(process_groupname);
1864 return -1;
1866 if (check & CPD_GROUP_OK) {
1867 mask = 0027;
1868 } else {
1869 mask = 0077;
1871 if (st.st_mode & mask) {
1872 unsigned new_mode;
1873 if (check & CPD_CHECK_MODE_ONLY) {
1874 log_warn(LD_FS, "Permissions on directory %s are too permissive.",
1875 dirname);
1876 return -1;
1878 log_warn(LD_FS, "Fixing permissions on directory %s", dirname);
1879 new_mode = st.st_mode;
1880 new_mode |= 0700; /* Owner should have rwx */
1881 new_mode &= ~mask; /* Clear the other bits that we didn't want set...*/
1882 if (chmod(dirname, new_mode)) {
1883 log_warn(LD_FS, "Could not chmod directory %s: %s", dirname,
1884 strerror(errno));
1885 return -1;
1886 } else {
1887 return 0;
1890 #endif
1891 return 0;
1894 /** Create a file named <b>fname</b> with the contents <b>str</b>. Overwrite
1895 * the previous <b>fname</b> if possible. Return 0 on success, -1 on failure.
1897 * This function replaces the old file atomically, if possible. This
1898 * function, and all other functions in util.c that create files, create them
1899 * with mode 0600.
1902 write_str_to_file(const char *fname, const char *str, int bin)
1904 #ifdef MS_WINDOWS
1905 if (!bin && strchr(str, '\r')) {
1906 log_warn(LD_BUG,
1907 "We're writing a text string that already contains a CR.");
1909 #endif
1910 return write_bytes_to_file(fname, str, strlen(str), bin);
1913 /** Represents a file that we're writing to, with support for atomic commit:
1914 * we can write into a temporary file, and either remove the file on
1915 * failure, or replace the original file on success. */
1916 struct open_file_t {
1917 char *tempname; /**< Name of the temporary file. */
1918 char *filename; /**< Name of the original file. */
1919 unsigned rename_on_close:1; /**< Are we using the temporary file or not? */
1920 unsigned binary:1; /**< Did we open in binary mode? */
1921 int fd; /**< fd for the open file. */
1922 FILE *stdio_file; /**< stdio wrapper for <b>fd</b>. */
1925 /** Try to start writing to the file in <b>fname</b>, passing the flags
1926 * <b>open_flags</b> to the open() syscall, creating the file (if needed) with
1927 * access value <b>mode</b>. If the O_APPEND flag is set, we append to the
1928 * original file. Otherwise, we open a new temporary file in the same
1929 * directory, and either replace the original or remove the temporary file
1930 * when we're done.
1932 * Return the fd for the newly opened file, and store working data in
1933 * *<b>data_out</b>. The caller should not close the fd manually:
1934 * instead, call finish_writing_to_file() or abort_writing_to_file().
1935 * Returns -1 on failure.
1937 * NOTE: When not appending, the flags O_CREAT and O_TRUNC are treated
1938 * as true and the flag O_EXCL is treated as false.
1940 * NOTE: Ordinarily, O_APPEND means "seek to the end of the file before each
1941 * write()". We don't do that.
1944 start_writing_to_file(const char *fname, int open_flags, int mode,
1945 open_file_t **data_out)
1947 size_t tempname_len = strlen(fname)+16;
1948 open_file_t *new_file = tor_malloc_zero(sizeof(open_file_t));
1949 const char *open_name;
1950 int append = 0;
1952 tor_assert(fname);
1953 tor_assert(data_out);
1954 #if (O_BINARY != 0 && O_TEXT != 0)
1955 tor_assert((open_flags & (O_BINARY|O_TEXT)) != 0);
1956 #endif
1957 new_file->fd = -1;
1958 tor_assert(tempname_len > strlen(fname)); /*check for overflow*/
1959 new_file->filename = tor_strdup(fname);
1960 if (open_flags & O_APPEND) {
1961 open_name = fname;
1962 new_file->rename_on_close = 0;
1963 append = 1;
1964 open_flags &= ~O_APPEND;
1965 } else {
1966 open_name = new_file->tempname = tor_malloc(tempname_len);
1967 if (tor_snprintf(new_file->tempname, tempname_len, "%s.tmp", fname)<0) {
1968 log_warn(LD_GENERAL, "Failed to generate filename");
1969 goto err;
1971 /* We always replace an existing temporary file if there is one. */
1972 open_flags |= O_CREAT|O_TRUNC;
1973 open_flags &= ~O_EXCL;
1974 new_file->rename_on_close = 1;
1976 if (open_flags & O_BINARY)
1977 new_file->binary = 1;
1979 new_file->fd = tor_open_cloexec(open_name, open_flags, mode);
1980 if (new_file->fd < 0) {
1981 log_warn(LD_FS, "Couldn't open \"%s\" (%s) for writing: %s",
1982 open_name, fname, strerror(errno));
1983 goto err;
1985 if (append) {
1986 if (tor_fd_seekend(new_file->fd) < 0) {
1987 log_warn(LD_FS, "Couldn't seek to end of file \"%s\": %s", open_name,
1988 strerror(errno));
1989 goto err;
1993 *data_out = new_file;
1995 return new_file->fd;
1997 err:
1998 if (new_file->fd >= 0)
1999 close(new_file->fd);
2000 *data_out = NULL;
2001 tor_free(new_file->filename);
2002 tor_free(new_file->tempname);
2003 tor_free(new_file);
2004 return -1;
2007 /** Given <b>file_data</b> from start_writing_to_file(), return a stdio FILE*
2008 * that can be used to write to the same file. The caller should not mix
2009 * stdio calls with non-stdio calls. */
2010 FILE *
2011 fdopen_file(open_file_t *file_data)
2013 tor_assert(file_data);
2014 if (file_data->stdio_file)
2015 return file_data->stdio_file;
2016 tor_assert(file_data->fd >= 0);
2017 if (!(file_data->stdio_file = fdopen(file_data->fd,
2018 file_data->binary?"ab":"a"))) {
2019 log_warn(LD_FS, "Couldn't fdopen \"%s\" [%d]: %s", file_data->filename,
2020 file_data->fd, strerror(errno));
2022 return file_data->stdio_file;
2025 /** Combines start_writing_to_file with fdopen_file(): arguments are as
2026 * for start_writing_to_file, but */
2027 FILE *
2028 start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
2029 open_file_t **data_out)
2031 FILE *res;
2032 if (start_writing_to_file(fname, open_flags, mode, data_out)<0)
2033 return NULL;
2034 if (!(res = fdopen_file(*data_out))) {
2035 abort_writing_to_file(*data_out);
2036 *data_out = NULL;
2038 return res;
2041 /** Helper function: close and free the underlying file and memory in
2042 * <b>file_data</b>. If we were writing into a temporary file, then delete
2043 * that file (if abort_write is true) or replaces the target file with
2044 * the temporary file (if abort_write is false). */
2045 static int
2046 finish_writing_to_file_impl(open_file_t *file_data, int abort_write)
2048 int r = 0;
2049 tor_assert(file_data && file_data->filename);
2050 if (file_data->stdio_file) {
2051 if (fclose(file_data->stdio_file)) {
2052 log_warn(LD_FS, "Error closing \"%s\": %s", file_data->filename,
2053 strerror(errno));
2054 abort_write = r = -1;
2056 } else if (file_data->fd >= 0 && close(file_data->fd) < 0) {
2057 log_warn(LD_FS, "Error flushing \"%s\": %s", file_data->filename,
2058 strerror(errno));
2059 abort_write = r = -1;
2062 if (file_data->rename_on_close) {
2063 tor_assert(file_data->tempname && file_data->filename);
2064 if (abort_write) {
2065 unlink(file_data->tempname);
2066 } else {
2067 tor_assert(strcmp(file_data->filename, file_data->tempname));
2068 if (replace_file(file_data->tempname, file_data->filename)) {
2069 log_warn(LD_FS, "Error replacing \"%s\": %s", file_data->filename,
2070 strerror(errno));
2071 r = -1;
2076 tor_free(file_data->filename);
2077 tor_free(file_data->tempname);
2078 tor_free(file_data);
2080 return r;
2083 /** Finish writing to <b>file_data</b>: close the file handle, free memory as
2084 * needed, and if using a temporary file, replace the original file with
2085 * the temporary file. */
2087 finish_writing_to_file(open_file_t *file_data)
2089 return finish_writing_to_file_impl(file_data, 0);
2092 /** Finish writing to <b>file_data</b>: close the file handle, free memory as
2093 * needed, and if using a temporary file, delete it. */
2095 abort_writing_to_file(open_file_t *file_data)
2097 return finish_writing_to_file_impl(file_data, 1);
2100 /** Helper: given a set of flags as passed to open(2), open the file
2101 * <b>fname</b> and write all the sized_chunk_t structs in <b>chunks</b> to
2102 * the file. Do so as atomically as possible e.g. by opening temp files and
2103 * renaming. */
2104 static int
2105 write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
2106 int open_flags)
2108 open_file_t *file = NULL;
2109 int fd;
2110 ssize_t result;
2111 fd = start_writing_to_file(fname, open_flags, 0600, &file);
2112 if (fd<0)
2113 return -1;
2114 SMARTLIST_FOREACH(chunks, sized_chunk_t *, chunk,
2116 result = write_all(fd, chunk->bytes, chunk->len, 0);
2117 if (result < 0) {
2118 log_warn(LD_FS, "Error writing to \"%s\": %s", fname,
2119 strerror(errno));
2120 goto err;
2122 tor_assert((size_t)result == chunk->len);
2125 return finish_writing_to_file(file);
2126 err:
2127 abort_writing_to_file(file);
2128 return -1;
2131 /** Given a smartlist of sized_chunk_t, write them atomically to a file
2132 * <b>fname</b>, overwriting or creating the file as necessary. */
2134 write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin)
2136 int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
2137 return write_chunks_to_file_impl(fname, chunks, flags);
2140 /** As write_str_to_file, but does not assume a NUL-terminated
2141 * string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
2143 write_bytes_to_file(const char *fname, const char *str, size_t len,
2144 int bin)
2146 int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
2147 int r;
2148 sized_chunk_t c = { str, len };
2149 smartlist_t *chunks = smartlist_create();
2150 smartlist_add(chunks, &c);
2151 r = write_chunks_to_file_impl(fname, chunks, flags);
2152 smartlist_free(chunks);
2153 return r;
2156 /** As write_bytes_to_file, but if the file already exists, append the bytes
2157 * to the end of the file instead of overwriting it. */
2159 append_bytes_to_file(const char *fname, const char *str, size_t len,
2160 int bin)
2162 int flags = OPEN_FLAGS_APPEND|(bin?O_BINARY:O_TEXT);
2163 int r;
2164 sized_chunk_t c = { str, len };
2165 smartlist_t *chunks = smartlist_create();
2166 smartlist_add(chunks, &c);
2167 r = write_chunks_to_file_impl(fname, chunks, flags);
2168 smartlist_free(chunks);
2169 return r;
2172 /** Read the contents of <b>filename</b> into a newly allocated
2173 * string; return the string on success or NULL on failure.
2175 * If <b>stat_out</b> is provided, store the result of stat()ing the
2176 * file into <b>stat_out</b>.
2178 * If <b>flags</b> &amp; RFTS_BIN, open the file in binary mode.
2179 * If <b>flags</b> &amp; RFTS_IGNORE_MISSING, don't warn if the file
2180 * doesn't exist.
2183 * This function <em>may</em> return an erroneous result if the file
2184 * is modified while it is running, but must not crash or overflow.
2185 * Right now, the error case occurs when the file length grows between
2186 * the call to stat and the call to read_all: the resulting string will
2187 * be truncated.
2189 char *
2190 read_file_to_str(const char *filename, int flags, struct stat *stat_out)
2192 int fd; /* router file */
2193 struct stat statbuf;
2194 char *string;
2195 ssize_t r;
2196 int bin = flags & RFTS_BIN;
2198 tor_assert(filename);
2200 fd = tor_open_cloexec(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
2201 if (fd<0) {
2202 int severity = LOG_WARN;
2203 int save_errno = errno;
2204 if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING))
2205 severity = LOG_INFO;
2206 log_fn(severity, LD_FS,"Could not open \"%s\": %s",filename,
2207 strerror(errno));
2208 errno = save_errno;
2209 return NULL;
2212 if (fstat(fd, &statbuf)<0) {
2213 int save_errno = errno;
2214 close(fd);
2215 log_warn(LD_FS,"Could not fstat \"%s\".",filename);
2216 errno = save_errno;
2217 return NULL;
2220 if ((uint64_t)(statbuf.st_size)+1 >= SIZE_T_CEILING)
2221 return NULL;
2223 string = tor_malloc((size_t)(statbuf.st_size+1));
2225 r = read_all(fd,string,(size_t)statbuf.st_size,0);
2226 if (r<0) {
2227 int save_errno = errno;
2228 log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
2229 strerror(errno));
2230 tor_free(string);
2231 close(fd);
2232 errno = save_errno;
2233 return NULL;
2235 string[r] = '\0'; /* NUL-terminate the result. */
2237 #ifdef MS_WINDOWS
2238 if (!bin && strchr(string, '\r')) {
2239 log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
2240 "when reading %s. Coping.",
2241 filename);
2242 tor_strstrip(string, "\r");
2243 r = strlen(string);
2245 if (!bin) {
2246 statbuf.st_size = (size_t) r;
2247 } else
2248 #endif
2249 if (r != statbuf.st_size) {
2250 /* Unless we're using text mode on win32, we'd better have an exact
2251 * match for size. */
2252 int save_errno = errno;
2253 log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
2254 (int)r, (long)statbuf.st_size,filename);
2255 tor_free(string);
2256 close(fd);
2257 errno = save_errno;
2258 return NULL;
2260 close(fd);
2261 if (stat_out) {
2262 memcpy(stat_out, &statbuf, sizeof(struct stat));
2265 return string;
2268 #define TOR_ISODIGIT(c) ('0' <= (c) && (c) <= '7')
2270 /** Given a c-style double-quoted escaped string in <b>s</b>, extract and
2271 * decode its contents into a newly allocated string. On success, assign this
2272 * string to *<b>result</b>, assign its length to <b>size_out</b> (if
2273 * provided), and return a pointer to the position in <b>s</b> immediately
2274 * after the string. On failure, return NULL.
2276 static const char *
2277 unescape_string(const char *s, char **result, size_t *size_out)
2279 const char *cp;
2280 char *out;
2281 if (s[0] != '\"')
2282 return NULL;
2283 cp = s+1;
2284 while (1) {
2285 switch (*cp) {
2286 case '\0':
2287 case '\n':
2288 return NULL;
2289 case '\"':
2290 goto end_of_loop;
2291 case '\\':
2292 if ((cp[1] == 'x' || cp[1] == 'X')
2293 && TOR_ISXDIGIT(cp[2]) && TOR_ISXDIGIT(cp[3])) {
2294 cp += 4;
2295 } else if (TOR_ISODIGIT(cp[1])) {
2296 cp += 2;
2297 if (TOR_ISODIGIT(*cp)) ++cp;
2298 if (TOR_ISODIGIT(*cp)) ++cp;
2299 } else if (cp[1]) {
2300 cp += 2;
2301 } else {
2302 return NULL;
2304 break;
2305 default:
2306 ++cp;
2307 break;
2310 end_of_loop:
2311 out = *result = tor_malloc(cp-s + 1);
2312 cp = s+1;
2313 while (1) {
2314 switch (*cp)
2316 case '\"':
2317 *out = '\0';
2318 if (size_out) *size_out = out - *result;
2319 return cp+1;
2320 case '\0':
2321 tor_fragile_assert();
2322 tor_free(*result);
2323 return NULL;
2324 case '\\':
2325 switch (cp[1])
2327 case 'n': *out++ = '\n'; cp += 2; break;
2328 case 'r': *out++ = '\r'; cp += 2; break;
2329 case 't': *out++ = '\t'; cp += 2; break;
2330 case 'x': case 'X':
2331 *out++ = ((hex_decode_digit(cp[2])<<4) +
2332 hex_decode_digit(cp[3]));
2333 cp += 4;
2334 break;
2335 case '0': case '1': case '2': case '3': case '4': case '5':
2336 case '6': case '7':
2338 int n = cp[1]-'0';
2339 cp += 2;
2340 if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
2341 if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
2342 if (n > 255) { tor_free(*result); return NULL; }
2343 *out++ = (char)n;
2345 break;
2346 case '\'':
2347 case '\"':
2348 case '\\':
2349 case '\?':
2350 *out++ = cp[1];
2351 cp += 2;
2352 break;
2353 default:
2354 tor_free(*result); return NULL;
2356 break;
2357 default:
2358 *out++ = *cp++;
2363 /** Given a string containing part of a configuration file or similar format,
2364 * advance past comments and whitespace and try to parse a single line. If we
2365 * parse a line successfully, set *<b>key_out</b> to a new string holding the
2366 * key portion and *<b>value_out</b> to a new string holding the value portion
2367 * of the line, and return a pointer to the start of the next line. If we run
2368 * out of data, return a pointer to the end of the string. If we encounter an
2369 * error, return NULL.
2371 const char *
2372 parse_config_line_from_str(const char *line, char **key_out, char **value_out)
2374 /* I believe the file format here is supposed to be:
2375 FILE = (EMPTYLINE | LINE)* (EMPTYLASTLINE | LASTLINE)?
2377 EMPTYLASTLINE = SPACE* | COMMENT
2378 EMPTYLINE = EMPTYLASTLINE NL
2379 SPACE = ' ' | '\r' | '\t'
2380 COMMENT = '#' NOT-NL*
2381 NOT-NL = Any character except '\n'
2382 NL = '\n'
2384 LASTLINE = SPACE* KEY SPACE* VALUES
2385 LINE = LASTLINE NL
2386 KEY = KEYCHAR+
2387 KEYCHAR = Any character except ' ', '\r', '\n', '\t', '#', "\"
2389 VALUES = QUOTEDVALUE | NORMALVALUE
2390 QUOTEDVALUE = QUOTE QVITEM* QUOTE EOLSPACE?
2391 QUOTE = '"'
2392 QVCHAR = KEYCHAR | ESC ('n' | 't' | 'r' | '"' | ESC |'\'' | OCTAL | HEX)
2393 ESC = "\\"
2394 OCTAL = ODIGIT (ODIGIT ODIGIT?)?
2395 HEX = ('x' | 'X') HEXDIGIT HEXDIGIT
2396 ODIGIT = '0' .. '7'
2397 HEXDIGIT = '0'..'9' | 'a' .. 'f' | 'A' .. 'F'
2398 EOLSPACE = SPACE* COMMENT?
2400 NORMALVALUE = (VALCHAR | ESC ESC_IGNORE | CONTINUATION)* EOLSPACE?
2401 VALCHAR = Any character except ESC, '#', and '\n'
2402 ESC_IGNORE = Any character except '#' or '\n'
2403 CONTINUATION = ESC NL ( COMMENT NL )*
2406 const char *key, *val, *cp;
2407 int continuation = 0;
2409 tor_assert(key_out);
2410 tor_assert(value_out);
2412 *key_out = *value_out = NULL;
2413 key = val = NULL;
2414 /* Skip until the first keyword. */
2415 while (1) {
2416 while (TOR_ISSPACE(*line))
2417 ++line;
2418 if (*line == '#') {
2419 while (*line && *line != '\n')
2420 ++line;
2421 } else {
2422 break;
2426 if (!*line) { /* End of string? */
2427 *key_out = *value_out = NULL;
2428 return line;
2431 /* Skip until the next space or \ followed by newline. */
2432 key = line;
2433 while (*line && !TOR_ISSPACE(*line) && *line != '#' &&
2434 ! (line[0] == '\\' && line[1] == '\n'))
2435 ++line;
2436 *key_out = tor_strndup(key, line-key);
2438 /* Skip until the value. */
2439 while (*line == ' ' || *line == '\t')
2440 ++line;
2442 val = line;
2444 /* Find the end of the line. */
2445 if (*line == '\"') { // XXX No continuation handling is done here
2446 if (!(line = unescape_string(line, value_out, NULL)))
2447 return NULL;
2448 while (*line == ' ' || *line == '\t')
2449 ++line;
2450 if (*line && *line != '#' && *line != '\n')
2451 return NULL;
2452 } else {
2453 /* Look for the end of the line. */
2454 while (*line && *line != '\n' && (*line != '#' || continuation)) {
2455 if (*line == '\\' && line[1] == '\n') {
2456 continuation = 1;
2457 line += 2;
2458 } else if (*line == '#') {
2459 do {
2460 ++line;
2461 } while (*line && *line != '\n');
2462 if (*line == '\n')
2463 ++line;
2464 } else {
2465 ++line;
2469 if (*line == '\n') {
2470 cp = line++;
2471 } else {
2472 cp = line;
2474 /* Now back cp up to be the last nonspace character */
2475 while (cp>val && TOR_ISSPACE(*(cp-1)))
2476 --cp;
2478 tor_assert(cp >= val);
2480 /* Now copy out and decode the value. */
2481 *value_out = tor_strndup(val, cp-val);
2482 if (continuation) {
2483 char *v_out, *v_in;
2484 v_out = v_in = *value_out;
2485 while (*v_in) {
2486 if (*v_in == '#') {
2487 do {
2488 ++v_in;
2489 } while (*v_in && *v_in != '\n');
2490 if (*v_in == '\n')
2491 ++v_in;
2492 } else if (v_in[0] == '\\' && v_in[1] == '\n') {
2493 v_in += 2;
2494 } else {
2495 *v_out++ = *v_in++;
2498 *v_out = '\0';
2502 if (*line == '#') {
2503 do {
2504 ++line;
2505 } while (*line && *line != '\n');
2507 while (TOR_ISSPACE(*line)) ++line;
2509 return line;
2512 /** Expand any homedir prefix on <b>filename</b>; return a newly allocated
2513 * string. */
2514 char *
2515 expand_filename(const char *filename)
2517 tor_assert(filename);
2518 #ifdef MS_WINDOWS
2519 return tor_strdup(filename);
2520 #else
2521 if (*filename == '~') {
2522 char *home, *result=NULL;
2523 const char *rest;
2525 if (filename[1] == '/' || filename[1] == '\0') {
2526 home = getenv("HOME");
2527 if (!home) {
2528 log_warn(LD_CONFIG, "Couldn't find $HOME environment variable while "
2529 "expanding \"%s\"; defaulting to \"\".", filename);
2530 home = tor_strdup("");
2531 } else {
2532 home = tor_strdup(home);
2534 rest = strlen(filename)>=2?(filename+2):"";
2535 } else {
2536 #ifdef HAVE_PWD_H
2537 char *username, *slash;
2538 slash = strchr(filename, '/');
2539 if (slash)
2540 username = tor_strndup(filename+1,slash-filename-1);
2541 else
2542 username = tor_strdup(filename+1);
2543 if (!(home = get_user_homedir(username))) {
2544 log_warn(LD_CONFIG,"Couldn't get homedir for \"%s\"",username);
2545 tor_free(username);
2546 return NULL;
2548 tor_free(username);
2549 rest = slash ? (slash+1) : "";
2550 #else
2551 log_warn(LD_CONFIG, "Couldn't expend homedir on system without pwd.h");
2552 return tor_strdup(filename);
2553 #endif
2555 tor_assert(home);
2556 /* Remove trailing slash. */
2557 if (strlen(home)>1 && !strcmpend(home,PATH_SEPARATOR)) {
2558 home[strlen(home)-1] = '\0';
2560 tor_asprintf(&result,"%s"PATH_SEPARATOR"%s",home,rest);
2561 tor_free(home);
2562 return result;
2563 } else {
2564 return tor_strdup(filename);
2566 #endif
2569 #define MAX_SCANF_WIDTH 9999
2571 /** Helper: given an ASCII-encoded decimal digit, return its numeric value.
2572 * NOTE: requires that its input be in-bounds. */
2573 static int
2574 digit_to_num(char d)
2576 int num = ((int)d) - (int)'0';
2577 tor_assert(num <= 9 && num >= 0);
2578 return num;
2581 /** Helper: Read an unsigned int from *<b>bufp</b> of up to <b>width</b>
2582 * characters. (Handle arbitrary width if <b>width</b> is less than 0.) On
2583 * success, store the result in <b>out</b>, advance bufp to the next
2584 * character, and return 0. On failure, return -1. */
2585 static int
2586 scan_unsigned(const char **bufp, unsigned *out, int width, int base)
2588 unsigned result = 0;
2589 int scanned_so_far = 0;
2590 const int hex = base==16;
2591 tor_assert(base == 10 || base == 16);
2592 if (!bufp || !*bufp || !out)
2593 return -1;
2594 if (width<0)
2595 width=MAX_SCANF_WIDTH;
2597 while (**bufp && (hex?TOR_ISXDIGIT(**bufp):TOR_ISDIGIT(**bufp))
2598 && scanned_so_far < width) {
2599 int digit = hex?hex_decode_digit(*(*bufp)++):digit_to_num(*(*bufp)++);
2600 unsigned new_result = result * base + digit;
2601 if (new_result > UINT32_MAX || new_result < result)
2602 return -1; /* over/underflow. */
2603 result = new_result;
2604 ++scanned_so_far;
2607 if (!scanned_so_far) /* No actual digits scanned */
2608 return -1;
2610 *out = result;
2611 return 0;
2614 /** Helper: copy up to <b>width</b> non-space characters from <b>bufp</b> to
2615 * <b>out</b>. Make sure <b>out</b> is nul-terminated. Advance <b>bufp</b>
2616 * to the next non-space character or the EOS. */
2617 static int
2618 scan_string(const char **bufp, char *out, int width)
2620 int scanned_so_far = 0;
2621 if (!bufp || !out || width < 0)
2622 return -1;
2623 while (**bufp && ! TOR_ISSPACE(**bufp) && scanned_so_far < width) {
2624 *out++ = *(*bufp)++;
2625 ++scanned_so_far;
2627 *out = '\0';
2628 return 0;
2631 /** Locale-independent, minimal, no-surprises scanf variant, accepting only a
2632 * restricted pattern format. For more info on what it supports, see
2633 * tor_sscanf() documentation. */
2635 tor_vsscanf(const char *buf, const char *pattern, va_list ap)
2637 int n_matched = 0;
2639 while (*pattern) {
2640 if (*pattern != '%') {
2641 if (*buf == *pattern) {
2642 ++buf;
2643 ++pattern;
2644 continue;
2645 } else {
2646 return n_matched;
2648 } else {
2649 int width = -1;
2650 ++pattern;
2651 if (TOR_ISDIGIT(*pattern)) {
2652 width = digit_to_num(*pattern++);
2653 while (TOR_ISDIGIT(*pattern)) {
2654 width *= 10;
2655 width += digit_to_num(*pattern++);
2656 if (width > MAX_SCANF_WIDTH)
2657 return -1;
2659 if (!width) /* No zero-width things. */
2660 return -1;
2662 if (*pattern == 'u' || *pattern == 'x') {
2663 unsigned *u = va_arg(ap, unsigned *);
2664 const int base = (*pattern == 'u') ? 10 : 16;
2665 if (!*buf)
2666 return n_matched;
2667 if (scan_unsigned(&buf, u, width, base)<0)
2668 return n_matched;
2669 ++pattern;
2670 ++n_matched;
2671 } else if (*pattern == 's') {
2672 char *s = va_arg(ap, char *);
2673 if (width < 0)
2674 return -1;
2675 if (scan_string(&buf, s, width)<0)
2676 return n_matched;
2677 ++pattern;
2678 ++n_matched;
2679 } else if (*pattern == 'c') {
2680 char *ch = va_arg(ap, char *);
2681 if (width != -1)
2682 return -1;
2683 if (!*buf)
2684 return n_matched;
2685 *ch = *buf++;
2686 ++pattern;
2687 ++n_matched;
2688 } else if (*pattern == '%') {
2689 if (*buf != '%')
2690 return -1;
2691 ++buf;
2692 ++pattern;
2693 } else {
2694 return -1; /* Unrecognized pattern component. */
2699 return n_matched;
2702 /** Minimal sscanf replacement: parse <b>buf</b> according to <b>pattern</b>
2703 * and store the results in the corresponding argument fields. Differs from
2704 * sscanf in that it: Only handles %u and %x and %Ns. Does not handle
2705 * arbitrarily long widths. %u and %x do not consume any space. Is
2706 * locale-independent. Returns -1 on malformed patterns.
2708 * (As with other locale-independent functions, we need this to parse data that
2709 * is in ASCII without worrying that the C library's locale-handling will make
2710 * miscellaneous characters look like numbers, spaces, and so on.)
2713 tor_sscanf(const char *buf, const char *pattern, ...)
2715 int r;
2716 va_list ap;
2717 va_start(ap, pattern);
2718 r = tor_vsscanf(buf, pattern, ap);
2719 va_end(ap);
2720 return r;
2723 /** Append the string produced by tor_asprintf(<b>pattern</b>, <b>...</b>)
2724 * to <b>sl</b>. */
2725 void
2726 smartlist_asprintf_add(struct smartlist_t *sl, const char *pattern, ...)
2728 va_list ap;
2729 va_start(ap, pattern);
2730 smartlist_vasprintf_add(sl, pattern, ap);
2731 va_end(ap);
2734 /** va_list-based backend of smartlist_asprintf_add. */
2735 void
2736 smartlist_vasprintf_add(struct smartlist_t *sl, const char *pattern,
2737 va_list args)
2739 char *str = NULL;
2741 tor_vasprintf(&str, pattern, args);
2742 tor_assert(str != NULL);
2744 smartlist_add(sl, str);
2747 /** Return a new list containing the filenames in the directory <b>dirname</b>.
2748 * Return NULL on error or if <b>dirname</b> is not a directory.
2750 smartlist_t *
2751 tor_listdir(const char *dirname)
2753 smartlist_t *result;
2754 #ifdef MS_WINDOWS
2755 char *pattern;
2756 TCHAR tpattern[MAX_PATH] = {0};
2757 char name[MAX_PATH] = {0};
2758 HANDLE handle;
2759 WIN32_FIND_DATA findData;
2760 size_t pattern_len = strlen(dirname)+16;
2761 pattern = tor_malloc(pattern_len);
2762 tor_snprintf(pattern, pattern_len, "%s\\*", dirname);
2763 #ifdef UNICODE
2764 mbstowcs(tpattern,pattern,MAX_PATH);
2765 #else
2766 strlcpy(tpattern, pattern, MAX_PATH);
2767 #endif
2768 if (INVALID_HANDLE_VALUE == (handle = FindFirstFile(tpattern, &findData))) {
2769 tor_free(pattern);
2770 return NULL;
2772 result = smartlist_create();
2773 while (1) {
2774 #ifdef UNICODE
2775 wcstombs(name,findData.cFileName,MAX_PATH);
2776 #else
2777 strlcpy(name,findData.cFileName,sizeof(name));
2778 #endif
2779 if (strcmp(name, ".") &&
2780 strcmp(name, "..")) {
2781 smartlist_add(result, tor_strdup(name));
2783 if (!FindNextFile(handle, &findData)) {
2784 DWORD err;
2785 if ((err = GetLastError()) != ERROR_NO_MORE_FILES) {
2786 char *errstr = format_win32_error(err);
2787 log_warn(LD_FS, "Error reading directory '%s': %s", dirname, errstr);
2788 tor_free(errstr);
2790 break;
2793 FindClose(handle);
2794 tor_free(pattern);
2795 #else
2796 DIR *d;
2797 struct dirent *de;
2798 if (!(d = opendir(dirname)))
2799 return NULL;
2801 result = smartlist_create();
2802 while ((de = readdir(d))) {
2803 if (!strcmp(de->d_name, ".") ||
2804 !strcmp(de->d_name, ".."))
2805 continue;
2806 smartlist_add(result, tor_strdup(de->d_name));
2808 closedir(d);
2809 #endif
2810 return result;
2813 /** Return true iff <b>filename</b> is a relative path. */
2815 path_is_relative(const char *filename)
2817 if (filename && filename[0] == '/')
2818 return 0;
2819 #ifdef MS_WINDOWS
2820 else if (filename && filename[0] == '\\')
2821 return 0;
2822 else if (filename && strlen(filename)>3 && TOR_ISALPHA(filename[0]) &&
2823 filename[1] == ':' && filename[2] == '\\')
2824 return 0;
2825 #endif
2826 else
2827 return 1;
2830 /* =====
2831 * Process helpers
2832 * ===== */
2834 #ifndef MS_WINDOWS
2835 /* Based on code contributed by christian grothoff */
2836 /** True iff we've called start_daemon(). */
2837 static int start_daemon_called = 0;
2838 /** True iff we've called finish_daemon(). */
2839 static int finish_daemon_called = 0;
2840 /** Socketpair used to communicate between parent and child process while
2841 * daemonizing. */
2842 static int daemon_filedes[2];
2843 /** Start putting the process into daemon mode: fork and drop all resources
2844 * except standard fds. The parent process never returns, but stays around
2845 * until finish_daemon is called. (Note: it's safe to call this more
2846 * than once: calls after the first are ignored.)
2848 void
2849 start_daemon(void)
2851 pid_t pid;
2853 if (start_daemon_called)
2854 return;
2855 start_daemon_called = 1;
2857 if (pipe(daemon_filedes)) {
2858 log_err(LD_GENERAL,"pipe failed; exiting. Error was %s", strerror(errno));
2859 exit(1);
2861 pid = fork();
2862 if (pid < 0) {
2863 log_err(LD_GENERAL,"fork failed. Exiting.");
2864 exit(1);
2866 if (pid) { /* Parent */
2867 int ok;
2868 char c;
2870 close(daemon_filedes[1]); /* we only read */
2871 ok = -1;
2872 while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
2873 if (c == '.')
2874 ok = 1;
2876 fflush(stdout);
2877 if (ok == 1)
2878 exit(0);
2879 else
2880 exit(1); /* child reported error */
2881 } else { /* Child */
2882 close(daemon_filedes[0]); /* we only write */
2884 pid = setsid(); /* Detach from controlling terminal */
2886 * Fork one more time, so the parent (the session group leader) can exit.
2887 * This means that we, as a non-session group leader, can never regain a
2888 * controlling terminal. This part is recommended by Stevens's
2889 * _Advanced Programming in the Unix Environment_.
2891 if (fork() != 0) {
2892 exit(0);
2894 set_main_thread(); /* We are now the main thread. */
2896 return;
2900 /** Finish putting the process into daemon mode: drop standard fds, and tell
2901 * the parent process to exit. (Note: it's safe to call this more than once:
2902 * calls after the first are ignored. Calls start_daemon first if it hasn't
2903 * been called already.)
2905 void
2906 finish_daemon(const char *desired_cwd)
2908 int nullfd;
2909 char c = '.';
2910 if (finish_daemon_called)
2911 return;
2912 if (!start_daemon_called)
2913 start_daemon();
2914 finish_daemon_called = 1;
2916 if (!desired_cwd)
2917 desired_cwd = "/";
2918 /* Don't hold the wrong FS mounted */
2919 if (chdir(desired_cwd) < 0) {
2920 log_err(LD_GENERAL,"chdir to \"%s\" failed. Exiting.",desired_cwd);
2921 exit(1);
2924 nullfd = tor_open_cloexec("/dev/null", O_RDWR, 0);
2925 if (nullfd < 0) {
2926 log_err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
2927 exit(1);
2929 /* close fds linking to invoking terminal, but
2930 * close usual incoming fds, but redirect them somewhere
2931 * useful so the fds don't get reallocated elsewhere.
2933 if (dup2(nullfd,0) < 0 ||
2934 dup2(nullfd,1) < 0 ||
2935 dup2(nullfd,2) < 0) {
2936 log_err(LD_GENERAL,"dup2 failed. Exiting.");
2937 exit(1);
2939 if (nullfd > 2)
2940 close(nullfd);
2941 /* signal success */
2942 if (write(daemon_filedes[1], &c, sizeof(char)) != sizeof(char)) {
2943 log_err(LD_GENERAL,"write failed. Exiting.");
2945 close(daemon_filedes[1]);
2947 #else
2948 /* defined(MS_WINDOWS) */
2949 void
2950 start_daemon(void)
2953 void
2954 finish_daemon(const char *cp)
2956 (void)cp;
2958 #endif
2960 /** Write the current process ID, followed by NL, into <b>filename</b>.
2962 void
2963 write_pidfile(char *filename)
2965 FILE *pidfile;
2967 if ((pidfile = fopen(filename, "w")) == NULL) {
2968 log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
2969 strerror(errno));
2970 } else {
2971 #ifdef MS_WINDOWS
2972 fprintf(pidfile, "%d\n", (int)_getpid());
2973 #else
2974 fprintf(pidfile, "%d\n", (int)getpid());
2975 #endif
2976 fclose(pidfile);
2980 #ifdef MS_WINDOWS
2981 HANDLE
2982 load_windows_system_library(const TCHAR *library_name)
2984 TCHAR path[MAX_PATH];
2985 unsigned n;
2986 n = GetSystemDirectory(path, MAX_PATH);
2987 if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH)
2988 return 0;
2989 _tcscat(path, TEXT("\\"));
2990 _tcscat(path, library_name);
2991 return LoadLibrary(path);
2993 #endif
2995 /** Format a single argument for being put on a Windows command line.
2996 * Returns a newly allocated string */
2997 static char *
2998 format_win_cmdline_argument(const char *arg)
3000 char *formatted_arg;
3001 char need_quotes;
3002 const char *c;
3003 int i;
3004 int bs_counter = 0;
3005 /* Backslash we can point to when one is inserted into the string */
3006 const char backslash = '\\';
3008 /* Smartlist of *char */
3009 smartlist_t *arg_chars;
3010 arg_chars = smartlist_create();
3012 /* Quote string if it contains whitespace or is empty */
3013 need_quotes = (strchr(arg, ' ') || strchr(arg, '\t') || '\0' == arg[0]);
3015 /* Build up smartlist of *chars */
3016 for (c=arg; *c != '\0'; c++) {
3017 if ('"' == *c) {
3018 /* Double up backslashes preceding a quote */
3019 for (i=0; i<(bs_counter*2); i++)
3020 smartlist_add(arg_chars, (void*)&backslash);
3021 bs_counter = 0;
3022 /* Escape the quote */
3023 smartlist_add(arg_chars, (void*)&backslash);
3024 smartlist_add(arg_chars, (void*)c);
3025 } else if ('\\' == *c) {
3026 /* Count backslashes until we know whether to double up */
3027 bs_counter++;
3028 } else {
3029 /* Don't double up slashes preceding a non-quote */
3030 for (i=0; i<bs_counter; i++)
3031 smartlist_add(arg_chars, (void*)&backslash);
3032 bs_counter = 0;
3033 smartlist_add(arg_chars, (void*)c);
3036 /* Don't double up trailing backslashes */
3037 for (i=0; i<bs_counter; i++)
3038 smartlist_add(arg_chars, (void*)&backslash);
3040 /* Allocate space for argument, quotes (if needed), and terminator */
3041 formatted_arg = tor_malloc(sizeof(char) *
3042 (smartlist_len(arg_chars) + (need_quotes?2:0) + 1));
3044 /* Add leading quote */
3045 i=0;
3046 if (need_quotes)
3047 formatted_arg[i++] = '"';
3049 /* Add characters */
3050 SMARTLIST_FOREACH(arg_chars, char*, c,
3052 formatted_arg[i++] = *c;
3055 /* Add trailing quote */
3056 if (need_quotes)
3057 formatted_arg[i++] = '"';
3058 formatted_arg[i] = '\0';
3060 smartlist_free(arg_chars);
3061 return formatted_arg;
3064 /** Format a command line for use on Windows, which takes the command as a
3065 * string rather than string array. Follows the rules from "Parsing C++
3066 * Command-Line Arguments" in MSDN. Algorithm based on list2cmdline in the
3067 * Python subprocess module. Returns a newly allocated string */
3068 char *
3069 tor_join_win_cmdline(const char *argv[])
3071 smartlist_t *argv_list;
3072 char *joined_argv;
3073 int i;
3075 /* Format each argument and put the result in a smartlist */
3076 argv_list = smartlist_create();
3077 for (i=0; argv[i] != NULL; i++) {
3078 smartlist_add(argv_list, (void *)format_win_cmdline_argument(argv[i]));
3081 /* Join the arguments with whitespace */
3082 joined_argv = smartlist_join_strings(argv_list, " ", 0, NULL);
3084 /* Free the newly allocated arguments, and the smartlist */
3085 SMARTLIST_FOREACH(argv_list, char *, arg,
3087 tor_free(arg);
3089 smartlist_free(argv_list);
3091 return joined_argv;
3094 /** Format <b>child_state</b> and <b>saved_errno</b> as a hex string placed in
3095 * <b>hex_errno</b>. Called between fork and _exit, so must be signal-handler
3096 * safe.
3098 * <b>hex_errno</b> must have at least HEX_ERRNO_SIZE bytes available.
3100 * The format of <b>hex_errno</b> is: "CHILD_STATE/ERRNO\n", left-padded
3101 * with spaces. Note that there is no trailing \0. CHILD_STATE indicates where
3102 * in the processs of starting the child process did the failure occur (see
3103 * CHILD_STATE_* macros for definition), and SAVED_ERRNO is the value of
3104 * errno when the failure occurred.
3107 void
3108 format_helper_exit_status(unsigned char child_state, int saved_errno,
3109 char *hex_errno)
3111 unsigned int unsigned_errno;
3112 char *cur;
3113 size_t i;
3115 /* Fill hex_errno with spaces, and a trailing newline (memset may
3116 not be signal handler safe, so we can't use it) */
3117 for (i = 0; i < (HEX_ERRNO_SIZE - 1); i++)
3118 hex_errno[i] = ' ';
3119 hex_errno[HEX_ERRNO_SIZE - 1] = '\n';
3121 /* Convert errno to be unsigned for hex conversion */
3122 if (saved_errno < 0) {
3123 unsigned_errno = (unsigned int) -saved_errno;
3124 } else {
3125 unsigned_errno = (unsigned int) saved_errno;
3128 /* Convert errno to hex (start before \n) */
3129 cur = hex_errno + HEX_ERRNO_SIZE - 2;
3131 /* Check for overflow on first iteration of the loop */
3132 if (cur < hex_errno)
3133 return;
3135 do {
3136 *cur-- = "0123456789ABCDEF"[unsigned_errno % 16];
3137 unsigned_errno /= 16;
3138 } while (unsigned_errno != 0 && cur >= hex_errno);
3140 /* Prepend the minus sign if errno was negative */
3141 if (saved_errno < 0 && cur >= hex_errno)
3142 *cur-- = '-';
3144 /* Leave a gap */
3145 if (cur >= hex_errno)
3146 *cur-- = '/';
3148 /* Check for overflow on first iteration of the loop */
3149 if (cur < hex_errno)
3150 return;
3152 /* Convert child_state to hex */
3153 do {
3154 *cur-- = "0123456789ABCDEF"[child_state % 16];
3155 child_state /= 16;
3156 } while (child_state != 0 && cur >= hex_errno);
3159 /* Maximum number of file descriptors, if we cannot get it via sysconf() */
3160 #define DEFAULT_MAX_FD 256
3162 /** Terminate process running at PID <b>pid</b>.
3163 * Code borrowed from Python's os.kill. */
3165 tor_terminate_process(pid_t pid)
3167 #ifdef MS_WINDOWS
3168 HANDLE handle;
3169 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
3170 attempt to open and terminate the process. */
3171 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
3172 if (!handle)
3173 return -1;
3175 if (!TerminateProcess(handle, 0))
3176 return -1;
3177 else
3178 return 0;
3179 #else /* Unix */
3180 return kill(pid, SIGTERM);
3181 #endif
3184 #define CHILD_STATE_INIT 0
3185 #define CHILD_STATE_PIPE 1
3186 #define CHILD_STATE_MAXFD 2
3187 #define CHILD_STATE_FORK 3
3188 #define CHILD_STATE_DUPOUT 4
3189 #define CHILD_STATE_DUPERR 5
3190 #define CHILD_STATE_REDIRECT 6
3191 #define CHILD_STATE_CLOSEFD 7
3192 #define CHILD_STATE_EXEC 8
3193 #define CHILD_STATE_FAILEXEC 9
3195 /** Start a program in the background. If <b>filename</b> contains a '/', then
3196 * it will be treated as an absolute or relative path. Otherwise, on
3197 * non-Windows systems, the system path will be searched for <b>filename</b>.
3198 * On Windows, only the current directory will be searched. Here, to search the
3199 * system path (as well as the application directory, current working
3200 * directory, and system directories), set filename to NULL.
3202 * The strings in <b>argv</b> will be passed as the command line arguments of
3203 * the child program (following convention, argv[0] should normally be the
3204 * filename of the executable, and this must be the case if <b>filename</b> is
3205 * NULL). The last element of argv must be NULL. A handle to the child process
3206 * will be returned in process_handle (which must be non-NULL). Read
3207 * process_handle.status to find out if the process was successfully launched.
3208 * For convenience, process_handle.status is returned by this function.
3210 * Some parts of this code are based on the POSIX subprocess module from
3211 * Python, and example code from
3212 * http://msdn.microsoft.com/en-us/library/ms682499%28v=vs.85%29.aspx.
3215 tor_spawn_background(const char *const filename, const char **argv,
3216 const char **envp,
3217 process_handle_t *process_handle)
3219 #ifdef MS_WINDOWS
3220 HANDLE stdout_pipe_read = NULL;
3221 HANDLE stdout_pipe_write = NULL;
3222 HANDLE stderr_pipe_read = NULL;
3223 HANDLE stderr_pipe_write = NULL;
3225 STARTUPINFO siStartInfo;
3226 BOOL retval = FALSE;
3228 SECURITY_ATTRIBUTES saAttr;
3229 char *joined_argv;
3231 (void)envp; // Unused on Windows
3233 /* process_handle must not be NULL */
3234 tor_assert(process_handle != NULL);
3236 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
3237 saAttr.bInheritHandle = TRUE;
3238 /* TODO: should we set explicit security attributes? (#2046, comment 5) */
3239 saAttr.lpSecurityDescriptor = NULL;
3241 /* Assume failure to start process */
3242 memset(process_handle, 0, sizeof(process_handle_t));
3243 process_handle->status = PROCESS_STATUS_ERROR;
3245 /* Set up pipe for stdout */
3246 if (!CreatePipe(&stdout_pipe_read, &stdout_pipe_write, &saAttr, 0)) {
3247 log_warn(LD_GENERAL,
3248 "Failed to create pipe for stdout communication with child process: %s",
3249 format_win32_error(GetLastError()));
3250 return process_handle->status;
3252 if (!SetHandleInformation(stdout_pipe_read, HANDLE_FLAG_INHERIT, 0)) {
3253 log_warn(LD_GENERAL,
3254 "Failed to configure pipe for stdout communication with child "
3255 "process: %s", format_win32_error(GetLastError()));
3256 return process_handle->status;
3259 /* Set up pipe for stderr */
3260 if (!CreatePipe(&stderr_pipe_read, &stderr_pipe_write, &saAttr, 0)) {
3261 log_warn(LD_GENERAL,
3262 "Failed to create pipe for stderr communication with child process: %s",
3263 format_win32_error(GetLastError()));
3264 return process_handle->status;
3266 if (!SetHandleInformation(stderr_pipe_read, HANDLE_FLAG_INHERIT, 0)) {
3267 log_warn(LD_GENERAL,
3268 "Failed to configure pipe for stderr communication with child "
3269 "process: %s", format_win32_error(GetLastError()));
3270 return process_handle->status;
3273 /* Create the child process */
3275 /* Windows expects argv to be a whitespace delimited string, so join argv up
3277 joined_argv = tor_join_win_cmdline(argv);
3279 ZeroMemory(&(process_handle->pid), sizeof(PROCESS_INFORMATION));
3280 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
3281 siStartInfo.cb = sizeof(STARTUPINFO);
3282 siStartInfo.hStdError = stderr_pipe_write;
3283 siStartInfo.hStdOutput = stdout_pipe_write;
3284 siStartInfo.hStdInput = NULL;
3285 siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
3287 /* Create the child process */
3289 retval = CreateProcess(filename, // module name
3290 joined_argv, // command line
3291 /* TODO: should we set explicit security attributes? (#2046, comment 5) */
3292 NULL, // process security attributes
3293 NULL, // primary thread security attributes
3294 TRUE, // handles are inherited
3295 /*(TODO: set CREATE_NEW CONSOLE/PROCESS_GROUP to make GetExitCodeProcess()
3296 * work?) */
3297 0, // creation flags
3298 NULL, // use parent's environment
3299 NULL, // use parent's current directory
3300 &siStartInfo, // STARTUPINFO pointer
3301 &(process_handle->pid)); // receives PROCESS_INFORMATION
3303 tor_free(joined_argv);
3305 if (!retval) {
3306 log_warn(LD_GENERAL,
3307 "Failed to create child process %s: %s", filename?filename:argv[0],
3308 format_win32_error(GetLastError()));
3309 } else {
3310 /* TODO: Close hProcess and hThread in process_handle->pid? */
3311 process_handle->stdout_pipe = stdout_pipe_read;
3312 process_handle->stderr_pipe = stderr_pipe_read;
3313 process_handle->status = PROCESS_STATUS_RUNNING;
3316 /* TODO: Close pipes on exit */
3318 return process_handle->status;
3319 #else // MS_WINDOWS
3320 pid_t pid;
3321 int stdout_pipe[2];
3322 int stderr_pipe[2];
3323 int fd, retval;
3324 ssize_t nbytes;
3326 const char *error_message = SPAWN_ERROR_MESSAGE;
3327 size_t error_message_length;
3329 /* Represents where in the process of spawning the program is;
3330 this is used for printing out the error message */
3331 unsigned char child_state = CHILD_STATE_INIT;
3333 char hex_errno[HEX_ERRNO_SIZE];
3335 static int max_fd = -1;
3337 /* Assume failure to start */
3338 memset(process_handle, 0, sizeof(process_handle_t));
3339 process_handle->status = PROCESS_STATUS_ERROR;
3341 /* We do the strlen here because strlen() is not signal handler safe,
3342 and we are not allowed to use unsafe functions between fork and exec */
3343 error_message_length = strlen(error_message);
3345 child_state = CHILD_STATE_PIPE;
3347 /* Set up pipe for redirecting stdout and stderr of child */
3348 retval = pipe(stdout_pipe);
3349 if (-1 == retval) {
3350 log_warn(LD_GENERAL,
3351 "Failed to set up pipe for stdout communication with child process: %s",
3352 strerror(errno));
3353 return process_handle->status;
3356 retval = pipe(stderr_pipe);
3357 if (-1 == retval) {
3358 log_warn(LD_GENERAL,
3359 "Failed to set up pipe for stderr communication with child process: %s",
3360 strerror(errno));
3361 return process_handle->status;
3364 child_state = CHILD_STATE_MAXFD;
3366 #ifdef _SC_OPEN_MAX
3367 if (-1 != max_fd) {
3368 max_fd = (int) sysconf(_SC_OPEN_MAX);
3369 if (max_fd == -1)
3370 max_fd = DEFAULT_MAX_FD;
3371 log_warn(LD_GENERAL,
3372 "Cannot find maximum file descriptor, assuming %d", max_fd);
3374 #else
3375 max_fd = DEFAULT_MAX_FD;
3376 #endif
3378 child_state = CHILD_STATE_FORK;
3380 pid = fork();
3381 if (0 == pid) {
3382 /* In child */
3384 child_state = CHILD_STATE_DUPOUT;
3386 /* Link child stdout to the write end of the pipe */
3387 retval = dup2(stdout_pipe[1], STDOUT_FILENO);
3388 if (-1 == retval)
3389 goto error;
3391 child_state = CHILD_STATE_DUPERR;
3393 /* Link child stderr to the write end of the pipe */
3394 retval = dup2(stderr_pipe[1], STDERR_FILENO);
3395 if (-1 == retval)
3396 goto error;
3398 child_state = CHILD_STATE_REDIRECT;
3400 /* Link stdin to /dev/null */
3401 fd = open("/dev/null", O_RDONLY); /* NOT cloexec, obviously. */
3402 if (fd != -1)
3403 dup2(fd, STDIN_FILENO);
3404 else
3405 goto error;
3407 child_state = CHILD_STATE_CLOSEFD;
3409 close(stderr_pipe[0]);
3410 close(stderr_pipe[1]);
3411 close(stdout_pipe[0]);
3412 close(stdout_pipe[1]);
3413 close(fd);
3415 /* Close all other fds, including the read end of the pipe */
3416 /* XXX: We should now be doing enough FD_CLOEXEC setting to make
3417 * this needless. */
3418 for (fd = STDERR_FILENO + 1; fd < max_fd; fd++) {
3419 close(fd);
3422 child_state = CHILD_STATE_EXEC;
3424 /* Call the requested program. We need the cast because
3425 execvp doesn't define argv as const, even though it
3426 does not modify the arguments */
3427 if (envp)
3428 execve(filename, (char *const *) argv, (char*const*)envp);
3429 else
3430 execvp(filename, (char *const *) argv);
3432 /* If we got here, the exec or open(/dev/null) failed */
3434 child_state = CHILD_STATE_FAILEXEC;
3436 error:
3437 /* XXX: are we leaking fds from the pipe? */
3439 format_helper_exit_status(child_state, errno, hex_errno);
3441 /* Write the error message. GCC requires that we check the return
3442 value, but there is nothing we can do if it fails */
3443 /* TODO: Don't use STDOUT, use a pipe set up just for this purpose */
3444 nbytes = write(STDOUT_FILENO, error_message, error_message_length);
3445 nbytes = write(STDOUT_FILENO, hex_errno, sizeof(hex_errno));
3447 (void) nbytes;
3449 _exit(255);
3450 /* Never reached, but avoids compiler warning */
3451 return process_handle->status;
3454 /* In parent */
3456 if (-1 == pid) {
3457 log_warn(LD_GENERAL, "Failed to fork child process: %s", strerror(errno));
3458 close(stdout_pipe[0]);
3459 close(stdout_pipe[1]);
3460 close(stderr_pipe[0]);
3461 close(stderr_pipe[1]);
3462 return process_handle->status;
3465 process_handle->pid = pid;
3467 /* TODO: If the child process forked but failed to exec, waitpid it */
3469 /* Return read end of the pipes to caller, and close write end */
3470 process_handle->stdout_pipe = stdout_pipe[0];
3471 retval = close(stdout_pipe[1]);
3473 if (-1 == retval) {
3474 log_warn(LD_GENERAL,
3475 "Failed to close write end of stdout pipe in parent process: %s",
3476 strerror(errno));
3479 process_handle->stderr_pipe = stderr_pipe[0];
3480 retval = close(stderr_pipe[1]);
3482 if (-1 == retval) {
3483 log_warn(LD_GENERAL,
3484 "Failed to close write end of stderr pipe in parent process: %s",
3485 strerror(errno));
3488 process_handle->status = PROCESS_STATUS_RUNNING;
3489 /* Set stdout/stderr pipes to be non-blocking */
3490 fcntl(process_handle->stdout_pipe, F_SETFL, O_NONBLOCK);
3491 fcntl(process_handle->stderr_pipe, F_SETFL, O_NONBLOCK);
3492 /* Open the buffered IO streams */
3493 process_handle->stdout_handle = fdopen(process_handle->stdout_pipe, "r");
3494 process_handle->stderr_handle = fdopen(process_handle->stderr_pipe, "r");
3496 return process_handle->status;
3497 #endif // MS_WINDOWS
3500 /** Get the exit code of a process specified by <b>process_handle</b> and store
3501 * it in <b>exit_code</b>, if set to a non-NULL value. If <b>block</b> is set
3502 * to true, the call will block until the process has exited. Otherwise if
3503 * the process is still running, the function will return
3504 * PROCESS_EXIT_RUNNING, and exit_code will be left unchanged. Returns
3505 * PROCESS_EXIT_EXITED if the process did exit. If there is a failure,
3506 * PROCESS_EXIT_ERROR will be returned and the contents of exit_code (if
3507 * non-NULL) will be undefined. N.B. Under *nix operating systems, this will
3508 * probably not work in Tor, because waitpid() is called in main.c to reap any
3509 * terminated child processes.*/
3511 tor_get_exit_code(const process_handle_t process_handle,
3512 int block, int *exit_code)
3514 #ifdef MS_WINDOWS
3515 DWORD retval;
3516 BOOL success;
3518 if (block) {
3519 /* Wait for the process to exit */
3520 retval = WaitForSingleObject(process_handle.pid.hProcess, INFINITE);
3521 if (retval != WAIT_OBJECT_0) {
3522 log_warn(LD_GENERAL, "WaitForSingleObject() failed (%d): %s",
3523 (int)retval, format_win32_error(GetLastError()));
3524 return PROCESS_EXIT_ERROR;
3526 } else {
3527 retval = WaitForSingleObject(process_handle.pid.hProcess, 0);
3528 if (WAIT_TIMEOUT == retval) {
3529 /* Process has not exited */
3530 return PROCESS_EXIT_RUNNING;
3531 } else if (retval != WAIT_OBJECT_0) {
3532 log_warn(LD_GENERAL, "WaitForSingleObject() failed (%d): %s",
3533 (int)retval, format_win32_error(GetLastError()));
3534 return PROCESS_EXIT_ERROR;
3538 if (exit_code != NULL) {
3539 success = GetExitCodeProcess(process_handle.pid.hProcess,
3540 (PDWORD)exit_code);
3541 if (!success) {
3542 log_warn(LD_GENERAL, "GetExitCodeProcess() failed: %s",
3543 format_win32_error(GetLastError()));
3544 return PROCESS_EXIT_ERROR;
3547 #else
3548 int stat_loc;
3549 int retval;
3551 retval = waitpid(process_handle.pid, &stat_loc, block?0:WNOHANG);
3552 if (!block && 0 == retval) {
3553 /* Process has not exited */
3554 return PROCESS_EXIT_RUNNING;
3555 } else if (retval != process_handle.pid) {
3556 log_warn(LD_GENERAL, "waitpid() failed for PID %d: %s", process_handle.pid,
3557 strerror(errno));
3558 return PROCESS_EXIT_ERROR;
3561 if (!WIFEXITED(stat_loc)) {
3562 log_warn(LD_GENERAL, "Process %d did not exit normally",
3563 process_handle.pid);
3564 return PROCESS_EXIT_ERROR;
3567 if (exit_code != NULL)
3568 *exit_code = WEXITSTATUS(stat_loc);
3569 #endif // MS_WINDOWS
3571 return PROCESS_EXIT_EXITED;
3574 #ifdef MS_WINDOWS
3575 /** Read from a handle <b>h</b> into <b>buf</b>, up to <b>count</b> bytes. If
3576 * <b>hProcess</b> is NULL, the function will return immediately if there is
3577 * nothing more to read. Otherwise <b>hProcess</b> should be set to the handle
3578 * to the process owning the <b>h</b>. In this case, the function will exit
3579 * only once the process has exited, or <b>count</b> bytes are read. Returns
3580 * the number of bytes read, or -1 on error. */
3581 ssize_t
3582 tor_read_all_handle(HANDLE h, char *buf, size_t count,
3583 const process_handle_t *process)
3585 size_t numread = 0;
3586 BOOL retval;
3587 DWORD byte_count;
3588 BOOL process_exited = FALSE;
3590 if (count > SIZE_T_CEILING || count > SSIZE_T_MAX)
3591 return -1;
3593 while (numread != count) {
3594 /* Check if there is anything to read */
3595 retval = PeekNamedPipe(h, NULL, 0, NULL, &byte_count, NULL);
3596 if (!retval) {
3597 log_warn(LD_GENERAL,
3598 "Failed to peek from handle: %s",
3599 format_win32_error(GetLastError()));
3600 return -1;
3601 } else if (0 == byte_count) {
3602 /* Nothing available: process exited or it is busy */
3604 /* Exit if we don't know whether the process is running */
3605 if (NULL == process)
3606 break;
3608 /* The process exited and there's nothing left to read from it */
3609 if (process_exited)
3610 break;
3612 /* If process is not running, check for output one more time in case
3613 it wrote something after the peek was performed. Otherwise keep on
3614 waiting for output */
3615 tor_assert(process != NULL);
3616 byte_count = WaitForSingleObject(process->pid.hProcess, 0);
3617 if (WAIT_TIMEOUT != byte_count)
3618 process_exited = TRUE;
3620 continue;
3623 /* There is data to read; read it */
3624 retval = ReadFile(h, buf+numread, count-numread, &byte_count, NULL);
3625 tor_assert(byte_count + numread <= count);
3626 if (!retval) {
3627 log_warn(LD_GENERAL, "Failed to read from handle: %s",
3628 format_win32_error(GetLastError()));
3629 return -1;
3630 } else if (0 == byte_count) {
3631 /* End of file */
3632 break;
3634 numread += byte_count;
3636 return (ssize_t)numread;
3638 #else
3639 /** Read from a handle <b>h</b> into <b>buf</b>, up to <b>count</b> bytes. If
3640 * <b>process</b> is NULL, the function will return immediately if there is
3641 * nothing more to read. Otherwise data will be read until end of file, or
3642 * <b>count</b> bytes are read. Returns the number of bytes read, or -1 on
3643 * error. Sets <b>eof</b> to true if <b>eof</b> is not NULL and the end of the
3644 * file has been reached. */
3645 ssize_t
3646 tor_read_all_handle(FILE *h, char *buf, size_t count,
3647 const process_handle_t *process,
3648 int *eof)
3650 size_t numread = 0;
3651 char *retval;
3653 if (eof)
3654 *eof = 0;
3656 if (count > SIZE_T_CEILING || count > SSIZE_T_MAX)
3657 return -1;
3659 while (numread != count) {
3660 /* Use fgets because that is what we use in log_from_pipe() */
3661 retval = fgets(buf+numread, (int)(count-numread), h);
3662 if (NULL == retval) {
3663 if (feof(h)) {
3664 log_debug(LD_GENERAL, "fgets() reached end of file");
3665 fclose(h);
3666 if (eof)
3667 *eof = 1;
3668 break;
3669 } else {
3670 if (EAGAIN == errno) {
3671 if (process)
3672 continue;
3673 else
3674 break;
3675 } else {
3676 log_warn(LD_GENERAL, "fgets() from handle failed: %s",
3677 strerror(errno));
3678 fclose(h);
3679 return -1;
3683 tor_assert(retval != NULL);
3684 tor_assert(strlen(retval) + numread <= count);
3685 numread += strlen(retval);
3688 log_debug(LD_GENERAL, "fgets() read %d bytes from handle", (int)numread);
3689 return (ssize_t)numread;
3691 #endif
3693 /** Read from stdout of a process until the process exits. */
3694 ssize_t
3695 tor_read_all_from_process_stdout(const process_handle_t *process_handle,
3696 char *buf, size_t count)
3698 #ifdef MS_WINDOWS
3699 return tor_read_all_handle(process_handle->stdout_pipe, buf, count,
3700 process_handle);
3701 #else
3702 return tor_read_all_handle(process_handle->stdout_handle, buf, count,
3703 process_handle, NULL);
3704 #endif
3707 /** Read from stdout of a process until the process exits. */
3708 ssize_t
3709 tor_read_all_from_process_stderr(const process_handle_t *process_handle,
3710 char *buf, size_t count)
3712 #ifdef MS_WINDOWS
3713 return tor_read_all_handle(process_handle->stderr_pipe, buf, count,
3714 process_handle);
3715 #else
3716 return tor_read_all_handle(process_handle->stderr_handle, buf, count,
3717 process_handle, NULL);
3718 #endif
3721 /** Split buf into lines, and add to smartlist. The buffer <b>buf</b> will be
3722 * modified. The resulting smartlist will consist of pointers to buf, so there
3723 * is no need to free the contents of sl. <b>buf</b> must be a NUL-terminated
3724 * string. <b>len</b> should be set to the length of the buffer excluding the
3725 * NUL. Non-printable characters (including NUL) will be replaced with "." */
3727 tor_split_lines(smartlist_t *sl, char *buf, int len)
3729 /* Index in buf of the start of the current line */
3730 int start = 0;
3731 /* Index in buf of the current character being processed */
3732 int cur = 0;
3733 /* Are we currently in a line */
3734 char in_line = 0;
3736 /* Loop over string */
3737 while (cur < len) {
3738 /* Loop until end of line or end of string */
3739 for (; cur < len; cur++) {
3740 if (in_line) {
3741 if ('\r' == buf[cur] || '\n' == buf[cur]) {
3742 /* End of line */
3743 buf[cur] = '\0';
3744 /* Point cur to the next line */
3745 cur++;
3746 /* Line starts at start and ends with a nul */
3747 break;
3748 } else {
3749 if (!TOR_ISPRINT(buf[cur]))
3750 buf[cur] = '.';
3752 } else {
3753 if ('\r' == buf[cur] || '\n' == buf[cur]) {
3754 /* Skip leading vertical space */
3756 } else {
3757 in_line = 1;
3758 start = cur;
3759 if (!TOR_ISPRINT(buf[cur]))
3760 buf[cur] = '.';
3764 /* We are at the end of the line or end of string. If in_line is true there
3765 * is a line which starts at buf+start and ends at a NUL. cur points to
3766 * the character after the NUL. */
3767 if (in_line)
3768 smartlist_add(sl, (void *)(buf+start));
3769 in_line = 0;
3771 return smartlist_len(sl);
3774 #ifdef MS_WINDOWS
3775 /** Read from stream, and send lines to log at the specified log level.
3776 * Returns -1 if there is a error reading, and 0 otherwise.
3777 * If the generated stream is flushed more often than on new lines, or
3778 * a read exceeds 256 bytes, lines will be truncated. This should be fixed,
3779 * along with the corresponding problem on *nix (see bug #2045).
3781 static int
3782 log_from_handle(HANDLE *pipe, int severity)
3784 char buf[256];
3785 int pos;
3786 smartlist_t *lines;
3788 pos = tor_read_all_handle(pipe, buf, sizeof(buf) - 1, NULL);
3789 if (pos < 0) {
3790 /* Error */
3791 log_warn(LD_GENERAL, "Failed to read data from subprocess");
3792 return -1;
3795 if (0 == pos) {
3796 /* There's nothing to read (process is busy or has exited) */
3797 log_debug(LD_GENERAL, "Subprocess had nothing to say");
3798 return 0;
3801 /* End with a null even if there isn't a \r\n at the end */
3802 /* TODO: What if this is a partial line? */
3803 buf[pos] = '\0';
3804 log_debug(LD_GENERAL, "Subprocess had %d bytes to say", pos);
3806 /* Split up the buffer */
3807 lines = smartlist_create();
3808 tor_split_lines(lines, buf, pos);
3810 /* Log each line */
3811 SMARTLIST_FOREACH(lines, char *, line,
3813 log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", line);
3815 smartlist_free(lines);
3817 return 0;
3820 #else
3822 /** Read from stream, and send lines to log at the specified log level.
3823 * Returns 1 if stream is closed normally, -1 if there is a error reading, and
3824 * 0 otherwise. Handles lines from tor-fw-helper and
3825 * tor_spawn_background() specially.
3827 static int
3828 log_from_pipe(FILE *stream, int severity, const char *executable,
3829 int *child_status)
3831 char buf[256];
3832 enum stream_status r;
3834 for (;;) {
3835 r = get_string_from_pipe(stream, buf, sizeof(buf) - 1);
3837 if (r == IO_STREAM_CLOSED) {
3838 fclose(stream);
3839 return 1;
3840 } else if (r == IO_STREAM_EAGAIN) {
3841 return 0;
3842 } else if (r == IO_STREAM_TERM) {
3843 fclose(stream);
3844 return -1;
3847 tor_assert(r == IO_STREAM_OKAY);
3849 /* Check if buf starts with SPAWN_ERROR_MESSAGE */
3850 if (strcmpstart(buf, SPAWN_ERROR_MESSAGE) == 0) {
3851 /* Parse error message */
3852 int retval, child_state, saved_errno;
3853 retval = tor_sscanf(buf, SPAWN_ERROR_MESSAGE "%x/%x",
3854 &child_state, &saved_errno);
3855 if (retval == 2) {
3856 log_warn(LD_GENERAL,
3857 "Failed to start child process \"%s\" in state %d: %s",
3858 executable, child_state, strerror(saved_errno));
3859 if (child_status)
3860 *child_status = 1;
3861 } else {
3862 /* Failed to parse message from child process, log it as a
3863 warning */
3864 log_warn(LD_GENERAL,
3865 "Unexpected message from port forwarding helper \"%s\": %s",
3866 executable, buf);
3868 } else {
3869 log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", buf);
3873 /* We should never get here */
3874 return -1;
3876 #endif
3878 /** Reads from <b>stream</b> and stores input in <b>buf_out</b> making
3879 * sure it's below <b>count</b> bytes.
3880 * If the string has a trailing newline, we strip it off.
3882 * This function is specifically created to handle input from managed
3883 * proxies, according to the pluggable transports spec. Make sure it
3884 * fits your needs before using it.
3886 * Returns:
3887 * IO_STREAM_CLOSED: If the stream is closed.
3888 * IO_STREAM_EAGAIN: If there is nothing to read and we should check back
3889 * later.
3890 * IO_STREAM_TERM: If something is wrong with the stream.
3891 * IO_STREAM_OKAY: If everything went okay and we got a string
3892 * in <b>buf_out</b>. */
3893 enum stream_status
3894 get_string_from_pipe(FILE *stream, char *buf_out, size_t count)
3896 char *retval;
3897 size_t len;
3899 tor_assert(count <= INT_MAX);
3901 retval = fgets(buf_out, (int)count, stream);
3903 if (!retval) {
3904 if (feof(stream)) {
3905 /* Program has closed stream (probably it exited) */
3906 /* TODO: check error */
3907 return IO_STREAM_CLOSED;
3908 } else {
3909 if (EAGAIN == errno) {
3910 /* Nothing more to read, try again next time */
3911 return IO_STREAM_EAGAIN;
3912 } else {
3913 /* There was a problem, abandon this child process */
3914 return IO_STREAM_TERM;
3917 } else {
3918 len = strlen(buf_out);
3919 tor_assert(len>0);
3921 if (buf_out[len - 1] == '\n') {
3922 /* Remove the trailing newline */
3923 buf_out[len - 1] = '\0';
3924 } else {
3925 /* No newline; check whether we overflowed the buffer */
3926 if (!feof(stream))
3927 log_info(LD_GENERAL,
3928 "Line from stream was truncated: %s", buf_out);
3929 /* TODO: What to do with this error? */
3932 return IO_STREAM_OKAY;
3935 /* We should never get here */
3936 return IO_STREAM_TERM;
3939 void
3940 tor_check_port_forwarding(const char *filename, int dir_port, int or_port,
3941 time_t now)
3943 /* When fw-helper succeeds, how long do we wait until running it again */
3944 #define TIME_TO_EXEC_FWHELPER_SUCCESS 300
3945 /* When fw-helper failed to start, how long do we wait until running it again
3947 #define TIME_TO_EXEC_FWHELPER_FAIL 60
3949 /* Static variables are initialized to zero, so child_handle.status=0
3950 * which corresponds to it not running on startup */
3951 static process_handle_t child_handle;
3953 static time_t time_to_run_helper = 0;
3954 int stdout_status, stderr_status, retval;
3955 const char *argv[10];
3956 char s_dirport[6], s_orport[6];
3958 tor_assert(filename);
3960 /* Set up command line for tor-fw-helper */
3961 snprintf(s_dirport, sizeof s_dirport, "%d", dir_port);
3962 snprintf(s_orport, sizeof s_orport, "%d", or_port);
3964 /* TODO: Allow different internal and external ports */
3965 argv[0] = filename;
3966 argv[1] = "--internal-or-port";
3967 argv[2] = s_orport;
3968 argv[3] = "--external-or-port";
3969 argv[4] = s_orport;
3970 argv[5] = "--internal-dir-port";
3971 argv[6] = s_dirport;
3972 argv[7] = "--external-dir-port";
3973 argv[8] = s_dirport;
3974 argv[9] = NULL;
3976 /* Start the child, if it is not already running */
3977 if (child_handle.status != PROCESS_STATUS_RUNNING &&
3978 time_to_run_helper < now) {
3979 /* Assume tor-fw-helper will succeed, start it later*/
3980 time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_SUCCESS;
3982 #ifdef MS_WINDOWS
3983 /* Passing NULL as lpApplicationName makes Windows search for the .exe */
3984 tor_spawn_background(NULL, argv, NULL, &child_handle);
3985 #else
3986 tor_spawn_background(filename, argv, NULL, &child_handle);
3987 #endif
3988 if (PROCESS_STATUS_ERROR == child_handle.status) {
3989 log_warn(LD_GENERAL, "Failed to start port forwarding helper %s",
3990 filename);
3991 time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_FAIL;
3992 return;
3994 #ifdef MS_WINDOWS
3995 log_info(LD_GENERAL,
3996 "Started port forwarding helper (%s)", filename);
3997 #else
3998 log_info(LD_GENERAL,
3999 "Started port forwarding helper (%s) with pid %d", filename,
4000 child_handle.pid);
4001 #endif
4004 /* If child is running, read from its stdout and stderr) */
4005 if (PROCESS_STATUS_RUNNING == child_handle.status) {
4006 /* Read from stdout/stderr and log result */
4007 retval = 0;
4008 #ifdef MS_WINDOWS
4009 stdout_status = log_from_handle(child_handle.stdout_pipe, LOG_INFO);
4010 stderr_status = log_from_handle(child_handle.stderr_pipe, LOG_WARN);
4011 /* If we got this far (on Windows), the process started */
4012 retval = 0;
4013 #else
4014 stdout_status = log_from_pipe(child_handle.stdout_handle,
4015 LOG_INFO, filename, &retval);
4016 stderr_status = log_from_pipe(child_handle.stderr_handle,
4017 LOG_WARN, filename, &retval);
4018 #endif
4019 if (retval) {
4020 /* There was a problem in the child process */
4021 time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_FAIL;
4024 /* Combine the two statuses in order of severity */
4025 if (-1 == stdout_status || -1 == stderr_status)
4026 /* There was a failure */
4027 retval = -1;
4028 #ifdef MS_WINDOWS
4029 else if (tor_get_exit_code(child_handle, 0, NULL) !=
4030 PROCESS_EXIT_RUNNING) {
4031 /* process has exited or there was an error */
4032 /* TODO: Do something with the process return value */
4033 /* TODO: What if the process output something since
4034 * between log_from_handle and tor_get_exit_code? */
4035 retval = 1;
4037 #else
4038 else if (1 == stdout_status || 1 == stderr_status)
4039 /* stdout or stderr was closed, the process probably
4040 * exited. It will be reaped by waitpid() in main.c */
4041 /* TODO: Do something with the process return value */
4042 retval = 1;
4043 #endif
4044 else
4045 /* Both are fine */
4046 retval = 0;
4048 /* If either pipe indicates a failure, act on it */
4049 if (0 != retval) {
4050 if (1 == retval) {
4051 log_info(LD_GENERAL, "Port forwarding helper terminated");
4052 child_handle.status = PROCESS_STATUS_NOTRUNNING;
4053 } else {
4054 log_warn(LD_GENERAL, "Failed to read from port forwarding helper");
4055 child_handle.status = PROCESS_STATUS_ERROR;
4058 /* TODO: The child might not actually be finished (maybe it failed or
4059 closed stdout/stderr), so maybe we shouldn't start another? */