Remove some totally unused functions
[tor.git] / src / common / util.c
blobed9a1c938318e73921c5229d032bcf19dbfd6e55
1 /* Copyright (c) 2003, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2013, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
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 #include "crypto.h"
24 #include "torint.h"
25 #include "container.h"
26 #include "address.h"
28 #ifdef _WIN32
29 #include <io.h>
30 #include <direct.h>
31 #include <process.h>
32 #include <tchar.h>
33 #include <winbase.h>
34 #else
35 #include <dirent.h>
36 #include <pwd.h>
37 #include <grp.h>
38 #endif
40 /* math.h needs this on Linux */
41 #ifndef _USE_ISOC99_
42 #define _USE_ISOC99_ 1
43 #endif
44 #include <math.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <assert.h>
49 #include <signal.h>
51 #ifdef HAVE_NETINET_IN_H
52 #include <netinet/in.h>
53 #endif
54 #ifdef HAVE_ARPA_INET_H
55 #include <arpa/inet.h>
56 #endif
57 #ifdef HAVE_ERRNO_H
58 #include <errno.h>
59 #endif
60 #ifdef HAVE_SYS_SOCKET_H
61 #include <sys/socket.h>
62 #endif
63 #ifdef HAVE_SYS_TIME_H
64 #include <sys/time.h>
65 #endif
66 #ifdef HAVE_UNISTD_H
67 #include <unistd.h>
68 #endif
69 #ifdef HAVE_SYS_STAT_H
70 #include <sys/stat.h>
71 #endif
72 #ifdef HAVE_SYS_FCNTL_H
73 #include <sys/fcntl.h>
74 #endif
75 #ifdef HAVE_TIME_H
76 #include <time.h>
77 #endif
78 #ifdef HAVE_MALLOC_MALLOC_H
79 #include <malloc/malloc.h>
80 #endif
81 #ifdef HAVE_MALLOC_H
82 #if !defined(OPENBSD) && !defined(__FreeBSD__)
83 /* OpenBSD has a malloc.h, but for our purposes, it only exists in order to
84 * scold us for being so stupid as to autodetect its presence. To be fair,
85 * they've done this since 1996, when autoconf was only 5 years old. */
86 #include <malloc.h>
87 #endif
88 #endif
89 #ifdef HAVE_MALLOC_NP_H
90 #include <malloc_np.h>
91 #endif
92 #ifdef HAVE_SYS_WAIT_H
93 #include <sys/wait.h>
94 #endif
96 /* =====
97 * Memory management
98 * ===== */
99 #ifdef USE_DMALLOC
100 #undef strndup
101 #include <dmalloc.h>
102 /* Macro to pass the extra dmalloc args to another function. */
103 #define DMALLOC_FN_ARGS , file, line
105 #if defined(HAVE_DMALLOC_STRDUP)
106 /* the dmalloc_strdup should be fine as defined */
107 #elif defined(HAVE_DMALLOC_STRNDUP)
108 #define dmalloc_strdup(file, line, string, xalloc_b) \
109 dmalloc_strndup(file, line, (string), -1, xalloc_b)
110 #else
111 #error "No dmalloc_strdup or equivalent"
112 #endif
114 #else /* not using dmalloc */
116 #define DMALLOC_FN_ARGS
117 #endif
119 /** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to
120 * result. On error, log and terminate the process. (Same as malloc(size),
121 * but never returns NULL.)
123 * <b>file</b> and <b>line</b> are used if dmalloc is enabled, and
124 * ignored otherwise.
126 void *
127 tor_malloc_(size_t size DMALLOC_PARAMS)
129 void *result;
131 tor_assert(size < SIZE_T_CEILING);
133 #ifndef MALLOC_ZERO_WORKS
134 /* Some libc mallocs don't work when size==0. Override them. */
135 if (size==0) {
136 size=1;
138 #endif
140 #ifdef USE_DMALLOC
141 result = dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
142 #else
143 result = malloc(size);
144 #endif
146 if (PREDICT_UNLIKELY(result == NULL)) {
147 log_err(LD_MM,"Out of memory on malloc(). Dying.");
148 /* If these functions die within a worker process, they won't call
149 * spawn_exit, but that's ok, since the parent will run out of memory soon
150 * anyway. */
151 exit(1);
153 return result;
156 /** Allocate a chunk of <b>size</b> bytes of memory, fill the memory with
157 * zero bytes, and return a pointer to the result. Log and terminate
158 * the process on error. (Same as calloc(size,1), but never returns NULL.)
160 void *
161 tor_malloc_zero_(size_t size DMALLOC_PARAMS)
163 /* You may ask yourself, "wouldn't it be smart to use calloc instead of
164 * malloc+memset? Perhaps libc's calloc knows some nifty optimization trick
165 * we don't!" Indeed it does, but its optimizations are only a big win when
166 * we're allocating something very big (it knows if it just got the memory
167 * from the OS in a pre-zeroed state). We don't want to use tor_malloc_zero
168 * for big stuff, so we don't bother with calloc. */
169 void *result = tor_malloc_(size DMALLOC_FN_ARGS);
170 memset(result, 0, size);
171 return result;
174 /** Allocate a chunk of <b>nmemb</b>*<b>size</b> bytes of memory, fill
175 * the memory with zero bytes, and return a pointer to the result.
176 * Log and terminate the process on error. (Same as
177 * calloc(<b>nmemb</b>,<b>size</b>), but never returns NULL.)
179 * XXXX This implementation probably asserts in cases where it could
180 * work, because it only tries dividing SIZE_MAX by size (according to
181 * the calloc(3) man page, the size of an element of the nmemb-element
182 * array to be allocated), not by nmemb (which could in theory be
183 * smaller than size). Don't do that then.
185 void *
186 tor_calloc_(size_t nmemb, size_t size DMALLOC_PARAMS)
188 /* You may ask yourself, "wouldn't it be smart to use calloc instead of
189 * malloc+memset? Perhaps libc's calloc knows some nifty optimization trick
190 * we don't!" Indeed it does, but its optimizations are only a big win when
191 * we're allocating something very big (it knows if it just got the memory
192 * from the OS in a pre-zeroed state). We don't want to use tor_malloc_zero
193 * for big stuff, so we don't bother with calloc. */
194 void *result;
195 size_t max_nmemb = (size == 0) ? SIZE_MAX : SIZE_MAX/size;
197 tor_assert(nmemb < max_nmemb);
199 result = tor_malloc_zero_((nmemb * size) DMALLOC_FN_ARGS);
200 return result;
203 /** Change the size of the memory block pointed to by <b>ptr</b> to <b>size</b>
204 * bytes long; return the new memory block. On error, log and
205 * terminate. (Like realloc(ptr,size), but never returns NULL.)
207 void *
208 tor_realloc_(void *ptr, size_t size DMALLOC_PARAMS)
210 void *result;
212 tor_assert(size < SIZE_T_CEILING);
214 #ifdef USE_DMALLOC
215 result = dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
216 #else
217 result = realloc(ptr, size);
218 #endif
220 if (PREDICT_UNLIKELY(result == NULL)) {
221 log_err(LD_MM,"Out of memory on realloc(). Dying.");
222 exit(1);
224 return result;
227 /** Return a newly allocated copy of the NUL-terminated string s. On
228 * error, log and terminate. (Like strdup(s), but never returns
229 * NULL.)
231 char *
232 tor_strdup_(const char *s DMALLOC_PARAMS)
234 char *dup;
235 tor_assert(s);
237 #ifdef USE_DMALLOC
238 dup = dmalloc_strdup(file, line, s, 0);
239 #else
240 dup = strdup(s);
241 #endif
242 if (PREDICT_UNLIKELY(dup == NULL)) {
243 log_err(LD_MM,"Out of memory on strdup(). Dying.");
244 exit(1);
246 return dup;
249 /** Allocate and return a new string containing the first <b>n</b>
250 * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b>
251 * characters, only the first <b>n</b> are copied. The result is
252 * always NUL-terminated. (Like strndup(s,n), but never returns
253 * NULL.)
255 char *
256 tor_strndup_(const char *s, size_t n DMALLOC_PARAMS)
258 char *dup;
259 tor_assert(s);
260 tor_assert(n < SIZE_T_CEILING);
261 dup = tor_malloc_((n+1) DMALLOC_FN_ARGS);
262 /* Performance note: Ordinarily we prefer strlcpy to strncpy. But
263 * this function gets called a whole lot, and platform strncpy is
264 * much faster than strlcpy when strlen(s) is much longer than n.
266 strncpy(dup, s, n);
267 dup[n]='\0';
268 return dup;
271 /** Allocate a chunk of <b>len</b> bytes, with the same contents as the
272 * <b>len</b> bytes starting at <b>mem</b>. */
273 void *
274 tor_memdup_(const void *mem, size_t len DMALLOC_PARAMS)
276 char *dup;
277 tor_assert(len < SIZE_T_CEILING);
278 tor_assert(mem);
279 dup = tor_malloc_(len DMALLOC_FN_ARGS);
280 memcpy(dup, mem, len);
281 return dup;
284 /** Helper for places that need to take a function pointer to the right
285 * spelling of "free()". */
286 void
287 tor_free_(void *mem)
289 tor_free(mem);
292 /** Call the platform malloc info function, and dump the results to the log at
293 * level <b>severity</b>. If no such function exists, do nothing. */
294 void
295 tor_log_mallinfo(int severity)
297 #ifdef HAVE_MALLINFO
298 struct mallinfo mi;
299 memset(&mi, 0, sizeof(mi));
300 mi = mallinfo();
301 tor_log(severity, LD_MM,
302 "mallinfo() said: arena=%d, ordblks=%d, smblks=%d, hblks=%d, "
303 "hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, "
304 "keepcost=%d",
305 mi.arena, mi.ordblks, mi.smblks, mi.hblks,
306 mi.hblkhd, mi.usmblks, mi.fsmblks, mi.uordblks, mi.fordblks,
307 mi.keepcost);
308 #else
309 (void)severity;
310 #endif
311 #ifdef USE_DMALLOC
312 dmalloc_log_changed(0, /* Since the program started. */
313 1, /* Log info about non-freed pointers. */
314 0, /* Do not log info about freed pointers. */
315 0 /* Do not log individual pointers. */
317 #endif
320 /* =====
321 * Math
322 * ===== */
325 * Returns the natural logarithm of d base e. We defined this wrapper here so
326 * to avoid conflicts with old versions of tor_log(), which were named log().
328 double
329 tor_mathlog(double d)
331 return log(d);
334 /** Return the long integer closest to <b>d</b>. We define this wrapper
335 * here so that not all users of math.h need to use the right incantations
336 * to get the c99 functions. */
337 long
338 tor_lround(double d)
340 #if defined(HAVE_LROUND)
341 return lround(d);
342 #elif defined(HAVE_RINT)
343 return (long)rint(d);
344 #else
345 return (long)(d > 0 ? d + 0.5 : ceil(d - 0.5));
346 #endif
349 /** Return the 64-bit integer closest to d. We define this wrapper here so
350 * that not all users of math.h need to use the right incantations to get the
351 * c99 functions. */
352 int64_t
353 tor_llround(double d)
355 #if defined(HAVE_LLROUND)
356 return (int64_t)llround(d);
357 #elif defined(HAVE_RINT)
358 return (int64_t)rint(d);
359 #else
360 return (int64_t)(d > 0 ? d + 0.5 : ceil(d - 0.5));
361 #endif
364 /** Returns floor(log2(u64)). If u64 is 0, (incorrectly) returns 0. */
366 tor_log2(uint64_t u64)
368 int r = 0;
369 if (u64 >= (U64_LITERAL(1)<<32)) {
370 u64 >>= 32;
371 r = 32;
373 if (u64 >= (U64_LITERAL(1)<<16)) {
374 u64 >>= 16;
375 r += 16;
377 if (u64 >= (U64_LITERAL(1)<<8)) {
378 u64 >>= 8;
379 r += 8;
381 if (u64 >= (U64_LITERAL(1)<<4)) {
382 u64 >>= 4;
383 r += 4;
385 if (u64 >= (U64_LITERAL(1)<<2)) {
386 u64 >>= 2;
387 r += 2;
389 if (u64 >= (U64_LITERAL(1)<<1)) {
390 u64 >>= 1;
391 r += 1;
393 return r;
396 /** Return the power of 2 in range [1,UINT64_MAX] closest to <b>u64</b>. If
397 * there are two powers of 2 equally close, round down. */
398 uint64_t
399 round_to_power_of_2(uint64_t u64)
401 int lg2;
402 uint64_t low;
403 uint64_t high;
404 if (u64 == 0)
405 return 1;
407 lg2 = tor_log2(u64);
408 low = U64_LITERAL(1) << lg2;
410 if (lg2 == 63)
411 return low;
413 high = U64_LITERAL(1) << (lg2+1);
414 if (high - u64 < u64 - low)
415 return high;
416 else
417 return low;
420 /** Return the lowest x such that x is at least <b>number</b>, and x modulo
421 * <b>divisor</b> == 0. */
422 unsigned
423 round_to_next_multiple_of(unsigned number, unsigned divisor)
425 number += divisor - 1;
426 number -= number % divisor;
427 return number;
430 /** Return the lowest x such that x is at least <b>number</b>, and x modulo
431 * <b>divisor</b> == 0. */
432 uint32_t
433 round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor)
435 number += divisor - 1;
436 number -= number % divisor;
437 return number;
440 /** Return the lowest x such that x is at least <b>number</b>, and x modulo
441 * <b>divisor</b> == 0. */
442 uint64_t
443 round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor)
445 number += divisor - 1;
446 number -= number % divisor;
447 return number;
450 /** Return the number of bits set in <b>v</b>. */
452 n_bits_set_u8(uint8_t v)
454 static const int nybble_table[] = {
455 0, /* 0000 */
456 1, /* 0001 */
457 1, /* 0010 */
458 2, /* 0011 */
459 1, /* 0100 */
460 2, /* 0101 */
461 2, /* 0110 */
462 3, /* 0111 */
463 1, /* 1000 */
464 2, /* 1001 */
465 2, /* 1010 */
466 3, /* 1011 */
467 2, /* 1100 */
468 3, /* 1101 */
469 3, /* 1110 */
470 4, /* 1111 */
473 return nybble_table[v & 15] + nybble_table[v>>4];
476 /* =====
477 * String manipulation
478 * ===== */
480 /** Remove from the string <b>s</b> every character which appears in
481 * <b>strip</b>. */
482 void
483 tor_strstrip(char *s, const char *strip)
485 char *read = s;
486 while (*read) {
487 if (strchr(strip, *read)) {
488 ++read;
489 } else {
490 *s++ = *read++;
493 *s = '\0';
496 /** Return a pointer to a NUL-terminated hexadecimal string encoding
497 * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
498 * result does not need to be deallocated, but repeated calls to
499 * hex_str will trash old results.
501 const char *
502 hex_str(const char *from, size_t fromlen)
504 static char buf[65];
505 if (fromlen>(sizeof(buf)-1)/2)
506 fromlen = (sizeof(buf)-1)/2;
507 base16_encode(buf,sizeof(buf),from,fromlen);
508 return buf;
511 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
512 * lowercase. */
513 void
514 tor_strlower(char *s)
516 while (*s) {
517 *s = TOR_TOLOWER(*s);
518 ++s;
522 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
523 * lowercase. */
524 void
525 tor_strupper(char *s)
527 while (*s) {
528 *s = TOR_TOUPPER(*s);
529 ++s;
533 /** Return 1 if every character in <b>s</b> is printable, else return 0.
536 tor_strisprint(const char *s)
538 while (*s) {
539 if (!TOR_ISPRINT(*s))
540 return 0;
541 s++;
543 return 1;
546 /** Return 1 if no character in <b>s</b> is uppercase, else return 0.
549 tor_strisnonupper(const char *s)
551 while (*s) {
552 if (TOR_ISUPPER(*s))
553 return 0;
554 s++;
556 return 1;
559 /** As strcmp, except that either string may be NULL. The NULL string is
560 * considered to be before any non-NULL string. */
562 strcmp_opt(const char *s1, const char *s2)
564 if (!s1) {
565 if (!s2)
566 return 0;
567 else
568 return -1;
569 } else if (!s2) {
570 return 1;
571 } else {
572 return strcmp(s1, s2);
576 /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
577 * strcmp.
580 strcmpstart(const char *s1, const char *s2)
582 size_t n = strlen(s2);
583 return strncmp(s1, s2, n);
586 /** Compare the s1_len-byte string <b>s1</b> with <b>s2</b>,
587 * without depending on a terminating nul in s1. Sorting order is first by
588 * length, then lexically; return values are as for strcmp.
591 strcmp_len(const char *s1, const char *s2, size_t s1_len)
593 size_t s2_len = strlen(s2);
594 if (s1_len < s2_len)
595 return -1;
596 if (s1_len > s2_len)
597 return 1;
598 return fast_memcmp(s1, s2, s2_len);
601 /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
602 * strcasecmp.
605 strcasecmpstart(const char *s1, const char *s2)
607 size_t n = strlen(s2);
608 return strncasecmp(s1, s2, n);
611 /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
612 * strcmp.
615 strcmpend(const char *s1, const char *s2)
617 size_t n1 = strlen(s1), n2 = strlen(s2);
618 if (n2>n1)
619 return strcmp(s1,s2);
620 else
621 return strncmp(s1+(n1-n2), s2, n2);
624 /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
625 * strcasecmp.
628 strcasecmpend(const char *s1, const char *s2)
630 size_t n1 = strlen(s1), n2 = strlen(s2);
631 if (n2>n1) /* then they can't be the same; figure out which is bigger */
632 return strcasecmp(s1,s2);
633 else
634 return strncasecmp(s1+(n1-n2), s2, n2);
637 /** Compare the value of the string <b>prefix</b> with the start of the
638 * <b>memlen</b>-byte memory chunk at <b>mem</b>. Return as for strcmp.
640 * [As fast_memcmp(mem, prefix, strlen(prefix)) but returns -1 if memlen is
641 * less than strlen(prefix).]
644 fast_memcmpstart(const void *mem, size_t memlen,
645 const char *prefix)
647 size_t plen = strlen(prefix);
648 if (memlen < plen)
649 return -1;
650 return fast_memcmp(mem, prefix, plen);
653 /** Given a nul-terminated string s, set every character before the nul
654 * to zero. */
655 void
656 tor_strclear(char *s)
658 while (*s) {
659 *s++ = '\0';
663 /** Return a pointer to the first char of s that is not whitespace and
664 * not a comment, or to the terminating NUL if no such character exists.
666 const char *
667 eat_whitespace(const char *s)
669 tor_assert(s);
671 while (1) {
672 switch (*s) {
673 case '\0':
674 default:
675 return s;
676 case ' ':
677 case '\t':
678 case '\n':
679 case '\r':
680 ++s;
681 break;
682 case '#':
683 ++s;
684 while (*s && *s != '\n')
685 ++s;
690 /** Return a pointer to the first char of s that is not whitespace and
691 * not a comment, or to the terminating NUL if no such character exists.
693 const char *
694 eat_whitespace_eos(const char *s, const char *eos)
696 tor_assert(s);
697 tor_assert(eos && s <= eos);
699 while (s < eos) {
700 switch (*s) {
701 case '\0':
702 default:
703 return s;
704 case ' ':
705 case '\t':
706 case '\n':
707 case '\r':
708 ++s;
709 break;
710 case '#':
711 ++s;
712 while (s < eos && *s && *s != '\n')
713 ++s;
716 return s;
719 /** Return a pointer to the first char of s that is not a space or a tab
720 * or a \\r, or to the terminating NUL if no such character exists. */
721 const char *
722 eat_whitespace_no_nl(const char *s)
724 while (*s == ' ' || *s == '\t' || *s == '\r')
725 ++s;
726 return s;
729 /** As eat_whitespace_no_nl, but stop at <b>eos</b> whether we have
730 * found a non-whitespace character or not. */
731 const char *
732 eat_whitespace_eos_no_nl(const char *s, const char *eos)
734 while (s < eos && (*s == ' ' || *s == '\t' || *s == '\r'))
735 ++s;
736 return s;
739 /** Return a pointer to the first char of s that is whitespace or <b>#</b>,
740 * or to the terminating NUL if no such character exists.
742 const char *
743 find_whitespace(const char *s)
745 /* tor_assert(s); */
746 while (1) {
747 switch (*s)
749 case '\0':
750 case '#':
751 case ' ':
752 case '\r':
753 case '\n':
754 case '\t':
755 return s;
756 default:
757 ++s;
762 /** As find_whitespace, but stop at <b>eos</b> whether we have found a
763 * whitespace or not. */
764 const char *
765 find_whitespace_eos(const char *s, const char *eos)
767 /* tor_assert(s); */
768 while (s < eos) {
769 switch (*s)
771 case '\0':
772 case '#':
773 case ' ':
774 case '\r':
775 case '\n':
776 case '\t':
777 return s;
778 default:
779 ++s;
782 return s;
785 /** Return the first occurrence of <b>needle</b> in <b>haystack</b> that
786 * occurs at the start of a line (that is, at the beginning of <b>haystack</b>
787 * or immediately after a newline). Return NULL if no such string is found.
789 const char *
790 find_str_at_start_of_line(const char *haystack, const char *needle)
792 size_t needle_len = strlen(needle);
794 do {
795 if (!strncmp(haystack, needle, needle_len))
796 return haystack;
798 haystack = strchr(haystack, '\n');
799 if (!haystack)
800 return NULL;
801 else
802 ++haystack;
803 } while (*haystack);
805 return NULL;
808 /** Returns true if <b>string</b> could be a C identifier.
809 A C identifier must begin with a letter or an underscore and the
810 rest of its characters can be letters, numbers or underscores. No
811 length limit is imposed. */
813 string_is_C_identifier(const char *string)
815 size_t iter;
816 size_t length = strlen(string);
817 if (!length)
818 return 0;
820 for (iter = 0; iter < length ; iter++) {
821 if (iter == 0) {
822 if (!(TOR_ISALPHA(string[iter]) ||
823 string[iter] == '_'))
824 return 0;
825 } else {
826 if (!(TOR_ISALPHA(string[iter]) ||
827 TOR_ISDIGIT(string[iter]) ||
828 string[iter] == '_'))
829 return 0;
833 return 1;
836 /** Return true iff the 'len' bytes at 'mem' are all zero. */
838 tor_mem_is_zero(const char *mem, size_t len)
840 static const char ZERO[] = {
841 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
843 while (len >= sizeof(ZERO)) {
844 /* It's safe to use fast_memcmp here, since the very worst thing an
845 * attacker could learn is how many initial bytes of a secret were zero */
846 if (fast_memcmp(mem, ZERO, sizeof(ZERO)))
847 return 0;
848 len -= sizeof(ZERO);
849 mem += sizeof(ZERO);
851 /* Deal with leftover bytes. */
852 if (len)
853 return fast_memeq(mem, ZERO, len);
855 return 1;
858 /** Return true iff the DIGEST_LEN bytes in digest are all zero. */
860 tor_digest_is_zero(const char *digest)
862 static const uint8_t ZERO_DIGEST[] = {
863 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
865 return tor_memeq(digest, ZERO_DIGEST, DIGEST_LEN);
868 /** Return true iff the DIGEST256_LEN bytes in digest are all zero. */
870 tor_digest256_is_zero(const char *digest)
872 return tor_mem_is_zero(digest, DIGEST256_LEN);
875 /* Helper: common code to check whether the result of a strtol or strtoul or
876 * strtoll is correct. */
877 #define CHECK_STRTOX_RESULT() \
878 /* Did an overflow occur? */ \
879 if (errno == ERANGE) \
880 goto err; \
881 /* Was at least one character converted? */ \
882 if (endptr == s) \
883 goto err; \
884 /* Were there unexpected unconverted characters? */ \
885 if (!next && *endptr) \
886 goto err; \
887 /* Is r within limits? */ \
888 if (r < min || r > max) \
889 goto err; \
890 if (ok) *ok = 1; \
891 if (next) *next = endptr; \
892 return r; \
893 err: \
894 if (ok) *ok = 0; \
895 if (next) *next = endptr; \
896 return 0
898 /** Extract a long from the start of <b>s</b>, in the given numeric
899 * <b>base</b>. If <b>base</b> is 0, <b>s</b> is parsed as a decimal,
900 * octal, or hex number in the syntax of a C integer literal. If
901 * there is unconverted data and <b>next</b> is provided, set
902 * *<b>next</b> to the first unconverted character. An error has
903 * occurred if no characters are converted; or if there are
904 * unconverted characters and <b>next</b> is NULL; or if the parsed
905 * value is not between <b>min</b> and <b>max</b>. When no error
906 * occurs, return the parsed value and set *<b>ok</b> (if provided) to
907 * 1. When an error occurs, return 0 and set *<b>ok</b> (if provided)
908 * to 0.
910 long
911 tor_parse_long(const char *s, int base, long min, long max,
912 int *ok, char **next)
914 char *endptr;
915 long r;
917 if (base < 0) {
918 if (ok)
919 *ok = 0;
920 return 0;
923 errno = 0;
924 r = strtol(s, &endptr, base);
925 CHECK_STRTOX_RESULT();
928 /** As tor_parse_long(), but return an unsigned long. */
929 unsigned long
930 tor_parse_ulong(const char *s, int base, unsigned long min,
931 unsigned long max, int *ok, char **next)
933 char *endptr;
934 unsigned long r;
936 if (base < 0) {
937 if (ok)
938 *ok = 0;
939 return 0;
942 errno = 0;
943 r = strtoul(s, &endptr, base);
944 CHECK_STRTOX_RESULT();
947 /** As tor_parse_long(), but return a double. */
948 double
949 tor_parse_double(const char *s, double min, double max, int *ok, char **next)
951 char *endptr;
952 double r;
954 errno = 0;
955 r = strtod(s, &endptr);
956 CHECK_STRTOX_RESULT();
959 /** As tor_parse_long, but return a uint64_t. Only base 10 is guaranteed to
960 * work for now. */
961 uint64_t
962 tor_parse_uint64(const char *s, int base, uint64_t min,
963 uint64_t max, int *ok, char **next)
965 char *endptr;
966 uint64_t r;
968 if (base < 0) {
969 if (ok)
970 *ok = 0;
971 return 0;
974 errno = 0;
975 #ifdef HAVE_STRTOULL
976 r = (uint64_t)strtoull(s, &endptr, base);
977 #elif defined(_WIN32)
978 #if defined(_MSC_VER) && _MSC_VER < 1300
979 tor_assert(base <= 10);
980 r = (uint64_t)_atoi64(s);
981 endptr = (char*)s;
982 while (TOR_ISSPACE(*endptr)) endptr++;
983 while (TOR_ISDIGIT(*endptr)) endptr++;
984 #else
985 r = (uint64_t)_strtoui64(s, &endptr, base);
986 #endif
987 #elif SIZEOF_LONG == 8
988 r = (uint64_t)strtoul(s, &endptr, base);
989 #else
990 #error "I don't know how to parse 64-bit numbers."
991 #endif
993 CHECK_STRTOX_RESULT();
996 /** Encode the <b>srclen</b> bytes at <b>src</b> in a NUL-terminated,
997 * uppercase hexadecimal string; store it in the <b>destlen</b>-byte buffer
998 * <b>dest</b>.
1000 void
1001 base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
1003 const char *end;
1004 char *cp;
1006 tor_assert(destlen >= srclen*2+1);
1007 tor_assert(destlen < SIZE_T_CEILING);
1009 cp = dest;
1010 end = src+srclen;
1011 while (src<end) {
1012 *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) >> 4 ];
1013 *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) & 0xf ];
1014 ++src;
1016 *cp = '\0';
1019 /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
1020 static INLINE int
1021 hex_decode_digit_(char c)
1023 switch (c) {
1024 case '0': return 0;
1025 case '1': return 1;
1026 case '2': return 2;
1027 case '3': return 3;
1028 case '4': return 4;
1029 case '5': return 5;
1030 case '6': return 6;
1031 case '7': return 7;
1032 case '8': return 8;
1033 case '9': return 9;
1034 case 'A': case 'a': return 10;
1035 case 'B': case 'b': return 11;
1036 case 'C': case 'c': return 12;
1037 case 'D': case 'd': return 13;
1038 case 'E': case 'e': return 14;
1039 case 'F': case 'f': return 15;
1040 default:
1041 return -1;
1045 /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
1047 hex_decode_digit(char c)
1049 return hex_decode_digit_(c);
1052 /** Given a hexadecimal string of <b>srclen</b> bytes in <b>src</b>, decode it
1053 * and store the result in the <b>destlen</b>-byte buffer at <b>dest</b>.
1054 * Return 0 on success, -1 on failure. */
1056 base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
1058 const char *end;
1060 int v1,v2;
1061 if ((srclen % 2) != 0)
1062 return -1;
1063 if (destlen < srclen/2 || destlen > SIZE_T_CEILING)
1064 return -1;
1065 end = src+srclen;
1066 while (src<end) {
1067 v1 = hex_decode_digit_(*src);
1068 v2 = hex_decode_digit_(*(src+1));
1069 if (v1<0||v2<0)
1070 return -1;
1071 *(uint8_t*)dest = (v1<<4)|v2;
1072 ++dest;
1073 src+=2;
1075 return 0;
1078 /** Allocate and return a new string representing the contents of <b>s</b>,
1079 * surrounded by quotes and using standard C escapes.
1081 * Generally, we use this for logging values that come in over the network to
1082 * keep them from tricking users, and for sending certain values to the
1083 * controller.
1085 * We trust values from the resolver, OS, configuration file, and command line
1086 * to not be maliciously ill-formed. We validate incoming routerdescs and
1087 * SOCKS requests and addresses from BEGIN cells as they're parsed;
1088 * afterwards, we trust them as non-malicious.
1090 char *
1091 esc_for_log(const char *s)
1093 const char *cp;
1094 char *result, *outp;
1095 size_t len = 3;
1096 if (!s) {
1097 return tor_strdup("(null)");
1100 for (cp = s; *cp; ++cp) {
1101 switch (*cp) {
1102 case '\\':
1103 case '\"':
1104 case '\'':
1105 case '\r':
1106 case '\n':
1107 case '\t':
1108 len += 2;
1109 break;
1110 default:
1111 if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127)
1112 ++len;
1113 else
1114 len += 4;
1115 break;
1119 result = outp = tor_malloc(len);
1120 *outp++ = '\"';
1121 for (cp = s; *cp; ++cp) {
1122 switch (*cp) {
1123 case '\\':
1124 case '\"':
1125 case '\'':
1126 *outp++ = '\\';
1127 *outp++ = *cp;
1128 break;
1129 case '\n':
1130 *outp++ = '\\';
1131 *outp++ = 'n';
1132 break;
1133 case '\t':
1134 *outp++ = '\\';
1135 *outp++ = 't';
1136 break;
1137 case '\r':
1138 *outp++ = '\\';
1139 *outp++ = 'r';
1140 break;
1141 default:
1142 if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127) {
1143 *outp++ = *cp;
1144 } else {
1145 tor_snprintf(outp, 5, "\\%03o", (int)(uint8_t) *cp);
1146 outp += 4;
1148 break;
1152 *outp++ = '\"';
1153 *outp++ = 0;
1155 return result;
1158 /** Allocate and return a new string representing the contents of <b>s</b>,
1159 * surrounded by quotes and using standard C escapes.
1161 * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
1162 * thread. Also, each call invalidates the last-returned value, so don't
1163 * try log_warn(LD_GENERAL, "%s %s", escaped(a), escaped(b));
1165 const char *
1166 escaped(const char *s)
1168 static char *escaped_val_ = NULL;
1169 tor_free(escaped_val_);
1171 if (s)
1172 escaped_val_ = esc_for_log(s);
1173 else
1174 escaped_val_ = NULL;
1176 return escaped_val_;
1179 /** Rudimentary string wrapping code: given a un-wrapped <b>string</b> (no
1180 * newlines!), break the string into newline-terminated lines of no more than
1181 * <b>width</b> characters long (not counting newline) and insert them into
1182 * <b>out</b> in order. Precede the first line with prefix0, and subsequent
1183 * lines with prefixRest.
1185 /* This uses a stupid greedy wrapping algorithm right now:
1186 * - For each line:
1187 * - Try to fit as much stuff as possible, but break on a space.
1188 * - If the first "word" of the line will extend beyond the allowable
1189 * width, break the word at the end of the width.
1191 void
1192 wrap_string(smartlist_t *out, const char *string, size_t width,
1193 const char *prefix0, const char *prefixRest)
1195 size_t p0Len, pRestLen, pCurLen;
1196 const char *eos, *prefixCur;
1197 tor_assert(out);
1198 tor_assert(string);
1199 tor_assert(width);
1200 if (!prefix0)
1201 prefix0 = "";
1202 if (!prefixRest)
1203 prefixRest = "";
1205 p0Len = strlen(prefix0);
1206 pRestLen = strlen(prefixRest);
1207 tor_assert(width > p0Len && width > pRestLen);
1208 eos = strchr(string, '\0');
1209 tor_assert(eos);
1210 pCurLen = p0Len;
1211 prefixCur = prefix0;
1213 while ((eos-string)+pCurLen > width) {
1214 const char *eol = string + width - pCurLen;
1215 while (eol > string && *eol != ' ')
1216 --eol;
1217 /* eol is now the last space that can fit, or the start of the string. */
1218 if (eol > string) {
1219 size_t line_len = (eol-string) + pCurLen + 2;
1220 char *line = tor_malloc(line_len);
1221 memcpy(line, prefixCur, pCurLen);
1222 memcpy(line+pCurLen, string, eol-string);
1223 line[line_len-2] = '\n';
1224 line[line_len-1] = '\0';
1225 smartlist_add(out, line);
1226 string = eol + 1;
1227 } else {
1228 size_t line_len = width + 2;
1229 char *line = tor_malloc(line_len);
1230 memcpy(line, prefixCur, pCurLen);
1231 memcpy(line+pCurLen, string, width - pCurLen);
1232 line[line_len-2] = '\n';
1233 line[line_len-1] = '\0';
1234 smartlist_add(out, line);
1235 string += width-pCurLen;
1237 prefixCur = prefixRest;
1238 pCurLen = pRestLen;
1241 if (string < eos) {
1242 size_t line_len = (eos-string) + pCurLen + 2;
1243 char *line = tor_malloc(line_len);
1244 memcpy(line, prefixCur, pCurLen);
1245 memcpy(line+pCurLen, string, eos-string);
1246 line[line_len-2] = '\n';
1247 line[line_len-1] = '\0';
1248 smartlist_add(out, line);
1252 /* =====
1253 * Time
1254 * ===== */
1256 /** Return the number of microseconds elapsed between *start and *end.
1258 long
1259 tv_udiff(const struct timeval *start, const struct timeval *end)
1261 long udiff;
1262 long secdiff = end->tv_sec - start->tv_sec;
1264 if (labs(secdiff+1) > LONG_MAX/1000000) {
1265 log_warn(LD_GENERAL, "comparing times on microsecond detail too far "
1266 "apart: %ld seconds", secdiff);
1267 return LONG_MAX;
1270 udiff = secdiff*1000000L + (end->tv_usec - start->tv_usec);
1271 return udiff;
1274 /** Return the number of milliseconds elapsed between *start and *end.
1276 long
1277 tv_mdiff(const struct timeval *start, const struct timeval *end)
1279 long mdiff;
1280 long secdiff = end->tv_sec - start->tv_sec;
1282 if (labs(secdiff+1) > LONG_MAX/1000) {
1283 log_warn(LD_GENERAL, "comparing times on millisecond detail too far "
1284 "apart: %ld seconds", secdiff);
1285 return LONG_MAX;
1288 /* Subtract and round */
1289 mdiff = secdiff*1000L +
1290 ((long)end->tv_usec - (long)start->tv_usec + 500L) / 1000L;
1291 return mdiff;
1294 /** Yield true iff <b>y</b> is a leap-year. */
1295 #define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
1296 /** Helper: Return the number of leap-days between Jan 1, y1 and Jan 1, y2. */
1297 static int
1298 n_leapdays(int y1, int y2)
1300 --y1;
1301 --y2;
1302 return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
1304 /** Number of days per month in non-leap year; used by tor_timegm. */
1305 static const int days_per_month[] =
1306 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1308 /** Compute a time_t given a struct tm. The result is given in UTC, and
1309 * does not account for leap seconds. Return 0 on success, -1 on failure.
1312 tor_timegm(const struct tm *tm, time_t *time_out)
1314 /* This is a pretty ironclad timegm implementation, snarfed from Python2.2.
1315 * It's way more brute-force than fiddling with tzset().
1317 time_t year, days, hours, minutes, seconds;
1318 int i;
1319 year = tm->tm_year + 1900;
1320 if (year < 1970 || tm->tm_mon < 0 || tm->tm_mon > 11 ||
1321 tm->tm_year >= INT32_MAX-1900) {
1322 log_warn(LD_BUG, "Out-of-range argument to tor_timegm");
1323 return -1;
1325 days = 365 * (year-1970) + n_leapdays(1970,(int)year);
1326 for (i = 0; i < tm->tm_mon; ++i)
1327 days += days_per_month[i];
1328 if (tm->tm_mon > 1 && IS_LEAPYEAR(year))
1329 ++days;
1330 days += tm->tm_mday - 1;
1331 hours = days*24 + tm->tm_hour;
1333 minutes = hours*60 + tm->tm_min;
1334 seconds = minutes*60 + tm->tm_sec;
1335 *time_out = seconds;
1336 return 0;
1339 /* strftime is locale-specific, so we need to replace those parts */
1341 /** A c-locale array of 3-letter names of weekdays, starting with Sun. */
1342 static const char *WEEKDAY_NAMES[] =
1343 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
1344 /** A c-locale array of 3-letter names of months, starting with Jan. */
1345 static const char *MONTH_NAMES[] =
1346 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1347 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1349 /** Set <b>buf</b> to the RFC1123 encoding of the UTC value of <b>t</b>.
1350 * The buffer must be at least RFC1123_TIME_LEN+1 bytes long.
1352 * (RFC1123 format is "Fri, 29 Sep 2006 15:54:20 GMT". Note the "GMT"
1353 * rather than "UTC".)
1355 void
1356 format_rfc1123_time(char *buf, time_t t)
1358 struct tm tm;
1360 tor_gmtime_r(&t, &tm);
1362 strftime(buf, RFC1123_TIME_LEN+1, "___, %d ___ %Y %H:%M:%S GMT", &tm);
1363 tor_assert(tm.tm_wday >= 0);
1364 tor_assert(tm.tm_wday <= 6);
1365 memcpy(buf, WEEKDAY_NAMES[tm.tm_wday], 3);
1366 tor_assert(tm.tm_mon >= 0);
1367 tor_assert(tm.tm_mon <= 11);
1368 memcpy(buf+8, MONTH_NAMES[tm.tm_mon], 3);
1371 /** Parse the (a subset of) the RFC1123 encoding of some time (in UTC) from
1372 * <b>buf</b>, and store the result in *<b>t</b>.
1374 * Note that we only accept the subset generated by format_rfc1123_time above,
1375 * not the full range of formats suggested by RFC 1123.
1377 * Return 0 on success, -1 on failure.
1380 parse_rfc1123_time(const char *buf, time_t *t)
1382 struct tm tm;
1383 char month[4];
1384 char weekday[4];
1385 int i, m;
1386 unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
1388 if (strlen(buf) != RFC1123_TIME_LEN)
1389 return -1;
1390 memset(&tm, 0, sizeof(tm));
1391 if (tor_sscanf(buf, "%3s, %2u %3s %u %2u:%2u:%2u GMT", weekday,
1392 &tm_mday, month, &tm_year, &tm_hour,
1393 &tm_min, &tm_sec) < 7) {
1394 char *esc = esc_for_log(buf);
1395 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
1396 tor_free(esc);
1397 return -1;
1399 if (tm_mday < 1 || tm_mday > 31 || tm_hour > 23 || tm_min > 59 ||
1400 tm_sec > 60 || tm_year >= INT32_MAX || tm_year < 1970) {
1401 char *esc = esc_for_log(buf);
1402 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
1403 tor_free(esc);
1404 return -1;
1406 tm.tm_mday = (int)tm_mday;
1407 tm.tm_year = (int)tm_year;
1408 tm.tm_hour = (int)tm_hour;
1409 tm.tm_min = (int)tm_min;
1410 tm.tm_sec = (int)tm_sec;
1412 m = -1;
1413 for (i = 0; i < 12; ++i) {
1414 if (!strcmp(month, MONTH_NAMES[i])) {
1415 m = i;
1416 break;
1419 if (m<0) {
1420 char *esc = esc_for_log(buf);
1421 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s: No such month", esc);
1422 tor_free(esc);
1423 return -1;
1425 tm.tm_mon = m;
1427 if (tm.tm_year < 1970) {
1428 char *esc = esc_for_log(buf);
1429 log_warn(LD_GENERAL,
1430 "Got invalid RFC1123 time %s. (Before 1970)", esc);
1431 tor_free(esc);
1432 return -1;
1434 tm.tm_year -= 1900;
1436 return tor_timegm(&tm, t);
1439 /** Set <b>buf</b> to the ISO8601 encoding of the local value of <b>t</b>.
1440 * The buffer must be at least ISO_TIME_LEN+1 bytes long.
1442 * (ISO8601 format is 2006-10-29 10:57:20)
1444 void
1445 format_local_iso_time(char *buf, time_t t)
1447 struct tm tm;
1448 strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_localtime_r(&t, &tm));
1451 /** Set <b>buf</b> to the ISO8601 encoding of the GMT value of <b>t</b>.
1452 * The buffer must be at least ISO_TIME_LEN+1 bytes long.
1454 void
1455 format_iso_time(char *buf, time_t t)
1457 struct tm tm;
1458 strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t, &tm));
1461 /** As format_iso_time, but use the yyyy-mm-ddThh:mm:ss format to avoid
1462 * embedding an internal space. */
1463 void
1464 format_iso_time_nospace(char *buf, time_t t)
1466 format_iso_time(buf, t);
1467 buf[10] = 'T';
1470 /** As format_iso_time_nospace, but include microseconds in decimal
1471 * fixed-point format. Requires that buf be at least ISO_TIME_USEC_LEN+1
1472 * bytes long. */
1473 void
1474 format_iso_time_nospace_usec(char *buf, const struct timeval *tv)
1476 tor_assert(tv);
1477 format_iso_time_nospace(buf, tv->tv_sec);
1478 tor_snprintf(buf+ISO_TIME_LEN, 8, ".%06d", (int)tv->tv_usec);
1481 /** Given an ISO-formatted UTC time value (after the epoch) in <b>cp</b>,
1482 * parse it and store its value in *<b>t</b>. Return 0 on success, -1 on
1483 * failure. Ignore extraneous stuff in <b>cp</b> separated by whitespace from
1484 * the end of the time string. */
1486 parse_iso_time(const char *cp, time_t *t)
1488 struct tm st_tm;
1489 unsigned int year=0, month=0, day=0, hour=0, minute=0, second=0;
1490 if (tor_sscanf(cp, "%u-%2u-%2u %2u:%2u:%2u", &year, &month,
1491 &day, &hour, &minute, &second) < 6) {
1492 char *esc = esc_for_log(cp);
1493 log_warn(LD_GENERAL, "ISO time %s was unparseable", esc);
1494 tor_free(esc);
1495 return -1;
1497 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
1498 hour > 23 || minute > 59 || second > 60 || year >= INT32_MAX) {
1499 char *esc = esc_for_log(cp);
1500 log_warn(LD_GENERAL, "ISO time %s was nonsensical", esc);
1501 tor_free(esc);
1502 return -1;
1504 st_tm.tm_year = (int)year-1900;
1505 st_tm.tm_mon = month-1;
1506 st_tm.tm_mday = day;
1507 st_tm.tm_hour = hour;
1508 st_tm.tm_min = minute;
1509 st_tm.tm_sec = second;
1511 if (st_tm.tm_year < 70) {
1512 char *esc = esc_for_log(cp);
1513 log_warn(LD_GENERAL, "Got invalid ISO time %s. (Before 1970)", esc);
1514 tor_free(esc);
1515 return -1;
1517 return tor_timegm(&st_tm, t);
1520 /** Given a <b>date</b> in one of the three formats allowed by HTTP (ugh),
1521 * parse it into <b>tm</b>. Return 0 on success, negative on failure. */
1523 parse_http_time(const char *date, struct tm *tm)
1525 const char *cp;
1526 char month[4];
1527 char wkday[4];
1528 int i;
1529 unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
1531 tor_assert(tm);
1532 memset(tm, 0, sizeof(*tm));
1534 /* First, try RFC1123 or RFC850 format: skip the weekday. */
1535 if ((cp = strchr(date, ','))) {
1536 ++cp;
1537 if (*cp != ' ')
1538 return -1;
1539 ++cp;
1540 if (tor_sscanf(cp, "%2u %3s %4u %2u:%2u:%2u GMT",
1541 &tm_mday, month, &tm_year,
1542 &tm_hour, &tm_min, &tm_sec) == 6) {
1543 /* rfc1123-date */
1544 tm_year -= 1900;
1545 } else if (tor_sscanf(cp, "%2u-%3s-%2u %2u:%2u:%2u GMT",
1546 &tm_mday, month, &tm_year,
1547 &tm_hour, &tm_min, &tm_sec) == 6) {
1548 /* rfc850-date */
1549 } else {
1550 return -1;
1552 } else {
1553 /* No comma; possibly asctime() format. */
1554 if (tor_sscanf(date, "%3s %3s %2u %2u:%2u:%2u %4u",
1555 wkday, month, &tm_mday,
1556 &tm_hour, &tm_min, &tm_sec, &tm_year) == 7) {
1557 tm_year -= 1900;
1558 } else {
1559 return -1;
1562 tm->tm_mday = (int)tm_mday;
1563 tm->tm_year = (int)tm_year;
1564 tm->tm_hour = (int)tm_hour;
1565 tm->tm_min = (int)tm_min;
1566 tm->tm_sec = (int)tm_sec;
1568 month[3] = '\0';
1569 /* Okay, now decode the month. */
1570 /* set tm->tm_mon to dummy value so the check below fails. */
1571 tm->tm_mon = -1;
1572 for (i = 0; i < 12; ++i) {
1573 if (!strcasecmp(MONTH_NAMES[i], month)) {
1574 tm->tm_mon = i;
1578 if (tm->tm_year < 0 ||
1579 tm->tm_mon < 0 || tm->tm_mon > 11 ||
1580 tm->tm_mday < 1 || tm->tm_mday > 31 ||
1581 tm->tm_hour < 0 || tm->tm_hour > 23 ||
1582 tm->tm_min < 0 || tm->tm_min > 59 ||
1583 tm->tm_sec < 0 || tm->tm_sec > 60)
1584 return -1; /* Out of range, or bad month. */
1586 return 0;
1589 /** Given an <b>interval</b> in seconds, try to write it to the
1590 * <b>out_len</b>-byte buffer in <b>out</b> in a human-readable form.
1591 * Return 0 on success, -1 on failure.
1594 format_time_interval(char *out, size_t out_len, long interval)
1596 /* We only report seconds if there's no hours. */
1597 long sec = 0, min = 0, hour = 0, day = 0;
1598 if (interval < 0)
1599 interval = -interval;
1601 if (interval >= 86400) {
1602 day = interval / 86400;
1603 interval %= 86400;
1605 if (interval >= 3600) {
1606 hour = interval / 3600;
1607 interval %= 3600;
1609 if (interval >= 60) {
1610 min = interval / 60;
1611 interval %= 60;
1613 sec = interval;
1615 if (day) {
1616 return tor_snprintf(out, out_len, "%ld days, %ld hours, %ld minutes",
1617 day, hour, min);
1618 } else if (hour) {
1619 return tor_snprintf(out, out_len, "%ld hours, %ld minutes", hour, min);
1620 } else if (min) {
1621 return tor_snprintf(out, out_len, "%ld minutes, %ld seconds", min, sec);
1622 } else {
1623 return tor_snprintf(out, out_len, "%ld seconds", sec);
1627 /* =====
1628 * Cached time
1629 * ===== */
1631 #ifndef TIME_IS_FAST
1632 /** Cached estimate of the current time. Updated around once per second;
1633 * may be a few seconds off if we are really busy. This is a hack to avoid
1634 * calling time(NULL) (which not everybody has optimized) on critical paths.
1636 static time_t cached_approx_time = 0;
1638 /** Return a cached estimate of the current time from when
1639 * update_approx_time() was last called. This is a hack to avoid calling
1640 * time(NULL) on critical paths: please do not even think of calling it
1641 * anywhere else. */
1642 time_t
1643 approx_time(void)
1645 return cached_approx_time;
1648 /** Update the cached estimate of the current time. This function SHOULD be
1649 * called once per second, and MUST be called before the first call to
1650 * get_approx_time. */
1651 void
1652 update_approx_time(time_t now)
1654 cached_approx_time = now;
1656 #endif
1658 /* =====
1659 * Rate limiting
1660 * ===== */
1662 /** If the rate-limiter <b>lim</b> is ready at <b>now</b>, return the number
1663 * of calls to rate_limit_is_ready (including this one!) since the last time
1664 * rate_limit_is_ready returned nonzero. Otherwise return 0. */
1665 static int
1666 rate_limit_is_ready(ratelim_t *lim, time_t now)
1668 if (lim->rate + lim->last_allowed <= now) {
1669 int res = lim->n_calls_since_last_time + 1;
1670 lim->last_allowed = now;
1671 lim->n_calls_since_last_time = 0;
1672 return res;
1673 } else {
1674 ++lim->n_calls_since_last_time;
1675 return 0;
1679 /** If the rate-limiter <b>lim</b> is ready at <b>now</b>, return a newly
1680 * allocated string indicating how many messages were suppressed, suitable to
1681 * append to a log message. Otherwise return NULL. */
1682 char *
1683 rate_limit_log(ratelim_t *lim, time_t now)
1685 int n;
1686 if ((n = rate_limit_is_ready(lim, now))) {
1687 if (n == 1) {
1688 return tor_strdup("");
1689 } else {
1690 char *cp=NULL;
1691 tor_asprintf(&cp,
1692 " [%d similar message(s) suppressed in last %d seconds]",
1693 n-1, lim->rate);
1694 return cp;
1696 } else {
1697 return NULL;
1701 /* =====
1702 * File helpers
1703 * ===== */
1705 /** Write <b>count</b> bytes from <b>buf</b> to <b>fd</b>. <b>isSocket</b>
1706 * must be 1 if fd was returned by socket() or accept(), and 0 if fd
1707 * was returned by open(). Return the number of bytes written, or -1
1708 * on error. Only use if fd is a blocking fd. */
1709 ssize_t
1710 write_all(tor_socket_t fd, const char *buf, size_t count, int isSocket)
1712 size_t written = 0;
1713 ssize_t result;
1714 tor_assert(count < SSIZE_T_MAX);
1716 while (written != count) {
1717 if (isSocket)
1718 result = tor_socket_send(fd, buf+written, count-written, 0);
1719 else
1720 result = write((int)fd, buf+written, count-written);
1721 if (result<0)
1722 return -1;
1723 written += result;
1725 return (ssize_t)count;
1728 /** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes
1729 * or reach the end of the file. <b>isSocket</b> must be 1 if fd
1730 * was returned by socket() or accept(), and 0 if fd was returned by
1731 * open(). Return the number of bytes read, or -1 on error. Only use
1732 * if fd is a blocking fd. */
1733 ssize_t
1734 read_all(tor_socket_t fd, char *buf, size_t count, int isSocket)
1736 size_t numread = 0;
1737 ssize_t result;
1739 if (count > SIZE_T_CEILING || count > SSIZE_T_MAX)
1740 return -1;
1742 while (numread != count) {
1743 if (isSocket)
1744 result = tor_socket_recv(fd, buf+numread, count-numread, 0);
1745 else
1746 result = read((int)fd, buf+numread, count-numread);
1747 if (result<0)
1748 return -1;
1749 else if (result == 0)
1750 break;
1751 numread += result;
1753 return (ssize_t)numread;
1757 * Filesystem operations.
1760 /** Clean up <b>name</b> so that we can use it in a call to "stat". On Unix,
1761 * we do nothing. On Windows, we remove a trailing slash, unless the path is
1762 * the root of a disk. */
1763 static void
1764 clean_name_for_stat(char *name)
1766 #ifdef _WIN32
1767 size_t len = strlen(name);
1768 if (!len)
1769 return;
1770 if (name[len-1]=='\\' || name[len-1]=='/') {
1771 if (len == 1 || (len==3 && name[1]==':'))
1772 return;
1773 name[len-1]='\0';
1775 #else
1776 (void)name;
1777 #endif
1780 /** Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
1781 * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
1782 * directory. On FN_ERROR, sets errno. */
1783 file_status_t
1784 file_status(const char *fname)
1786 struct stat st;
1787 char *f;
1788 int r;
1789 f = tor_strdup(fname);
1790 clean_name_for_stat(f);
1791 r = stat(f, &st);
1792 tor_free(f);
1793 if (r) {
1794 if (errno == ENOENT) {
1795 return FN_NOENT;
1797 return FN_ERROR;
1799 if (st.st_mode & S_IFDIR)
1800 return FN_DIR;
1801 else if (st.st_mode & S_IFREG)
1802 return FN_FILE;
1803 #ifndef _WIN32
1804 else if (st.st_mode & S_IFIFO)
1805 return FN_FILE;
1806 #endif
1807 else
1808 return FN_ERROR;
1811 /** Check whether <b>dirname</b> exists and is private. If yes return 0. If
1812 * it does not exist, and <b>check</b>&CPD_CREATE is set, try to create it
1813 * and return 0 on success. If it does not exist, and
1814 * <b>check</b>&CPD_CHECK, and we think we can create it, return 0. Else
1815 * return -1. If CPD_GROUP_OK is set, then it's okay if the directory
1816 * is group-readable, but in all cases we create the directory mode 0700.
1817 * If CPD_CHECK_MODE_ONLY is set, then we don't alter the directory permissions
1818 * if they are too permissive: we just return -1.
1819 * When effective_user is not NULL, check permissions against the given user
1820 * and its primary group.
1823 check_private_dir(const char *dirname, cpd_check_t check,
1824 const char *effective_user)
1826 int r;
1827 struct stat st;
1828 char *f;
1829 #ifndef _WIN32
1830 int mask;
1831 struct passwd *pw = NULL;
1832 uid_t running_uid;
1833 gid_t running_gid;
1834 #else
1835 (void)effective_user;
1836 #endif
1838 tor_assert(dirname);
1839 f = tor_strdup(dirname);
1840 clean_name_for_stat(f);
1841 r = stat(f, &st);
1842 tor_free(f);
1843 if (r) {
1844 if (errno != ENOENT) {
1845 log_warn(LD_FS, "Directory %s cannot be read: %s", dirname,
1846 strerror(errno));
1847 return -1;
1849 if (check & CPD_CREATE) {
1850 log_info(LD_GENERAL, "Creating directory %s", dirname);
1851 #if defined (_WIN32) && !defined (WINCE)
1852 r = mkdir(dirname);
1853 #else
1854 r = mkdir(dirname, 0700);
1855 #endif
1856 if (r) {
1857 log_warn(LD_FS, "Error creating directory %s: %s", dirname,
1858 strerror(errno));
1859 return -1;
1861 } else if (!(check & CPD_CHECK)) {
1862 log_warn(LD_FS, "Directory %s does not exist.", dirname);
1863 return -1;
1865 /* XXXX In the case where check==CPD_CHECK, we should look at the
1866 * parent directory a little harder. */
1867 return 0;
1869 if (!(st.st_mode & S_IFDIR)) {
1870 log_warn(LD_FS, "%s is not a directory", dirname);
1871 return -1;
1873 #ifndef _WIN32
1874 if (effective_user) {
1875 /* Look up the user and group information.
1876 * If we have a problem, bail out. */
1877 pw = getpwnam(effective_user);
1878 if (pw == NULL) {
1879 log_warn(LD_CONFIG, "Error setting configured user: %s not found",
1880 effective_user);
1881 return -1;
1883 running_uid = pw->pw_uid;
1884 running_gid = pw->pw_gid;
1885 } else {
1886 running_uid = getuid();
1887 running_gid = getgid();
1890 if (st.st_uid != running_uid) {
1891 struct passwd *pw = NULL;
1892 char *process_ownername = NULL;
1894 pw = getpwuid(running_uid);
1895 process_ownername = pw ? tor_strdup(pw->pw_name) : tor_strdup("<unknown>");
1897 pw = getpwuid(st.st_uid);
1899 log_warn(LD_FS, "%s is not owned by this user (%s, %d) but by "
1900 "%s (%d). Perhaps you are running Tor as the wrong user?",
1901 dirname, process_ownername, (int)running_uid,
1902 pw ? pw->pw_name : "<unknown>", (int)st.st_uid);
1904 tor_free(process_ownername);
1905 return -1;
1907 if ((check & CPD_GROUP_OK) && st.st_gid != running_gid) {
1908 struct group *gr;
1909 char *process_groupname = NULL;
1910 gr = getgrgid(running_gid);
1911 process_groupname = gr ? tor_strdup(gr->gr_name) : tor_strdup("<unknown>");
1912 gr = getgrgid(st.st_gid);
1914 log_warn(LD_FS, "%s is not owned by this group (%s, %d) but by group "
1915 "%s (%d). Are you running Tor as the wrong user?",
1916 dirname, process_groupname, (int)running_gid,
1917 gr ? gr->gr_name : "<unknown>", (int)st.st_gid);
1919 tor_free(process_groupname);
1920 return -1;
1922 if (check & CPD_GROUP_OK) {
1923 mask = 0027;
1924 } else {
1925 mask = 0077;
1927 if (st.st_mode & mask) {
1928 unsigned new_mode;
1929 if (check & CPD_CHECK_MODE_ONLY) {
1930 log_warn(LD_FS, "Permissions on directory %s are too permissive.",
1931 dirname);
1932 return -1;
1934 log_warn(LD_FS, "Fixing permissions on directory %s", dirname);
1935 new_mode = st.st_mode;
1936 new_mode |= 0700; /* Owner should have rwx */
1937 new_mode &= ~mask; /* Clear the other bits that we didn't want set...*/
1938 if (chmod(dirname, new_mode)) {
1939 log_warn(LD_FS, "Could not chmod directory %s: %s", dirname,
1940 strerror(errno));
1941 return -1;
1942 } else {
1943 return 0;
1946 #endif
1947 return 0;
1950 /** Create a file named <b>fname</b> with the contents <b>str</b>. Overwrite
1951 * the previous <b>fname</b> if possible. Return 0 on success, -1 on failure.
1953 * This function replaces the old file atomically, if possible. This
1954 * function, and all other functions in util.c that create files, create them
1955 * with mode 0600.
1958 write_str_to_file(const char *fname, const char *str, int bin)
1960 #ifdef _WIN32
1961 if (!bin && strchr(str, '\r')) {
1962 log_warn(LD_BUG,
1963 "We're writing a text string that already contains a CR.");
1965 #endif
1966 return write_bytes_to_file(fname, str, strlen(str), bin);
1969 /** Represents a file that we're writing to, with support for atomic commit:
1970 * we can write into a temporary file, and either remove the file on
1971 * failure, or replace the original file on success. */
1972 struct open_file_t {
1973 char *tempname; /**< Name of the temporary file. */
1974 char *filename; /**< Name of the original file. */
1975 unsigned rename_on_close:1; /**< Are we using the temporary file or not? */
1976 unsigned binary:1; /**< Did we open in binary mode? */
1977 int fd; /**< fd for the open file. */
1978 FILE *stdio_file; /**< stdio wrapper for <b>fd</b>. */
1981 /** Try to start writing to the file in <b>fname</b>, passing the flags
1982 * <b>open_flags</b> to the open() syscall, creating the file (if needed) with
1983 * access value <b>mode</b>. If the O_APPEND flag is set, we append to the
1984 * original file. Otherwise, we open a new temporary file in the same
1985 * directory, and either replace the original or remove the temporary file
1986 * when we're done.
1988 * Return the fd for the newly opened file, and store working data in
1989 * *<b>data_out</b>. The caller should not close the fd manually:
1990 * instead, call finish_writing_to_file() or abort_writing_to_file().
1991 * Returns -1 on failure.
1993 * NOTE: When not appending, the flags O_CREAT and O_TRUNC are treated
1994 * as true and the flag O_EXCL is treated as false.
1996 * NOTE: Ordinarily, O_APPEND means "seek to the end of the file before each
1997 * write()". We don't do that.
2000 start_writing_to_file(const char *fname, int open_flags, int mode,
2001 open_file_t **data_out)
2003 open_file_t *new_file = tor_malloc_zero(sizeof(open_file_t));
2004 const char *open_name;
2005 int append = 0;
2007 tor_assert(fname);
2008 tor_assert(data_out);
2009 #if (O_BINARY != 0 && O_TEXT != 0)
2010 tor_assert((open_flags & (O_BINARY|O_TEXT)) != 0);
2011 #endif
2012 new_file->fd = -1;
2013 new_file->filename = tor_strdup(fname);
2014 if (open_flags & O_APPEND) {
2015 open_name = fname;
2016 new_file->rename_on_close = 0;
2017 append = 1;
2018 open_flags &= ~O_APPEND;
2019 } else {
2020 tor_asprintf(&new_file->tempname, "%s.tmp", fname);
2021 open_name = new_file->tempname;
2022 /* We always replace an existing temporary file if there is one. */
2023 open_flags |= O_CREAT|O_TRUNC;
2024 open_flags &= ~O_EXCL;
2025 new_file->rename_on_close = 1;
2027 if (open_flags & O_BINARY)
2028 new_file->binary = 1;
2030 new_file->fd = tor_open_cloexec(open_name, open_flags, mode);
2031 if (new_file->fd < 0) {
2032 log_warn(LD_FS, "Couldn't open \"%s\" (%s) for writing: %s",
2033 open_name, fname, strerror(errno));
2034 goto err;
2036 if (append) {
2037 if (tor_fd_seekend(new_file->fd) < 0) {
2038 log_warn(LD_FS, "Couldn't seek to end of file \"%s\": %s", open_name,
2039 strerror(errno));
2040 goto err;
2044 *data_out = new_file;
2046 return new_file->fd;
2048 err:
2049 if (new_file->fd >= 0)
2050 close(new_file->fd);
2051 *data_out = NULL;
2052 tor_free(new_file->filename);
2053 tor_free(new_file->tempname);
2054 tor_free(new_file);
2055 return -1;
2058 /** Given <b>file_data</b> from start_writing_to_file(), return a stdio FILE*
2059 * that can be used to write to the same file. The caller should not mix
2060 * stdio calls with non-stdio calls. */
2061 FILE *
2062 fdopen_file(open_file_t *file_data)
2064 tor_assert(file_data);
2065 if (file_data->stdio_file)
2066 return file_data->stdio_file;
2067 tor_assert(file_data->fd >= 0);
2068 if (!(file_data->stdio_file = fdopen(file_data->fd,
2069 file_data->binary?"ab":"a"))) {
2070 log_warn(LD_FS, "Couldn't fdopen \"%s\" [%d]: %s", file_data->filename,
2071 file_data->fd, strerror(errno));
2073 return file_data->stdio_file;
2076 /** Combines start_writing_to_file with fdopen_file(): arguments are as
2077 * for start_writing_to_file, but */
2078 FILE *
2079 start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
2080 open_file_t **data_out)
2082 FILE *res;
2083 if (start_writing_to_file(fname, open_flags, mode, data_out)<0)
2084 return NULL;
2085 if (!(res = fdopen_file(*data_out))) {
2086 abort_writing_to_file(*data_out);
2087 *data_out = NULL;
2089 return res;
2092 /** Helper function: close and free the underlying file and memory in
2093 * <b>file_data</b>. If we were writing into a temporary file, then delete
2094 * that file (if abort_write is true) or replaces the target file with
2095 * the temporary file (if abort_write is false). */
2096 static int
2097 finish_writing_to_file_impl(open_file_t *file_data, int abort_write)
2099 int r = 0;
2100 tor_assert(file_data && file_data->filename);
2101 if (file_data->stdio_file) {
2102 if (fclose(file_data->stdio_file)) {
2103 log_warn(LD_FS, "Error closing \"%s\": %s", file_data->filename,
2104 strerror(errno));
2105 abort_write = r = -1;
2107 } else if (file_data->fd >= 0 && close(file_data->fd) < 0) {
2108 log_warn(LD_FS, "Error flushing \"%s\": %s", file_data->filename,
2109 strerror(errno));
2110 abort_write = r = -1;
2113 if (file_data->rename_on_close) {
2114 tor_assert(file_data->tempname && file_data->filename);
2115 if (abort_write) {
2116 unlink(file_data->tempname);
2117 } else {
2118 tor_assert(strcmp(file_data->filename, file_data->tempname));
2119 if (replace_file(file_data->tempname, file_data->filename)) {
2120 log_warn(LD_FS, "Error replacing \"%s\": %s", file_data->filename,
2121 strerror(errno));
2122 r = -1;
2127 tor_free(file_data->filename);
2128 tor_free(file_data->tempname);
2129 tor_free(file_data);
2131 return r;
2134 /** Finish writing to <b>file_data</b>: close the file handle, free memory as
2135 * needed, and if using a temporary file, replace the original file with
2136 * the temporary file. */
2138 finish_writing_to_file(open_file_t *file_data)
2140 return finish_writing_to_file_impl(file_data, 0);
2143 /** Finish writing to <b>file_data</b>: close the file handle, free memory as
2144 * needed, and if using a temporary file, delete it. */
2146 abort_writing_to_file(open_file_t *file_data)
2148 return finish_writing_to_file_impl(file_data, 1);
2151 /** Helper: given a set of flags as passed to open(2), open the file
2152 * <b>fname</b> and write all the sized_chunk_t structs in <b>chunks</b> to
2153 * the file. Do so as atomically as possible e.g. by opening temp files and
2154 * renaming. */
2155 static int
2156 write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
2157 int open_flags)
2159 open_file_t *file = NULL;
2160 int fd;
2161 ssize_t result;
2162 fd = start_writing_to_file(fname, open_flags, 0600, &file);
2163 if (fd<0)
2164 return -1;
2165 SMARTLIST_FOREACH(chunks, sized_chunk_t *, chunk,
2167 result = write_all(fd, chunk->bytes, chunk->len, 0);
2168 if (result < 0) {
2169 log_warn(LD_FS, "Error writing to \"%s\": %s", fname,
2170 strerror(errno));
2171 goto err;
2173 tor_assert((size_t)result == chunk->len);
2176 return finish_writing_to_file(file);
2177 err:
2178 abort_writing_to_file(file);
2179 return -1;
2182 /** Given a smartlist of sized_chunk_t, write them atomically to a file
2183 * <b>fname</b>, overwriting or creating the file as necessary. */
2185 write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin)
2187 int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
2188 return write_chunks_to_file_impl(fname, chunks, flags);
2191 /** Write <b>len</b> bytes, starting at <b>str</b>, to <b>fname</b>
2192 using the open() flags passed in <b>flags</b>. */
2193 static int
2194 write_bytes_to_file_impl(const char *fname, const char *str, size_t len,
2195 int flags)
2197 int r;
2198 sized_chunk_t c = { str, len };
2199 smartlist_t *chunks = smartlist_new();
2200 smartlist_add(chunks, &c);
2201 r = write_chunks_to_file_impl(fname, chunks, flags);
2202 smartlist_free(chunks);
2203 return r;
2206 /** As write_str_to_file, but does not assume a NUL-terminated
2207 * string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
2209 write_bytes_to_file(const char *fname, const char *str, size_t len,
2210 int bin)
2212 return write_bytes_to_file_impl(fname, str, len,
2213 OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT));
2216 /** As write_bytes_to_file, but if the file already exists, append the bytes
2217 * to the end of the file instead of overwriting it. */
2219 append_bytes_to_file(const char *fname, const char *str, size_t len,
2220 int bin)
2222 return write_bytes_to_file_impl(fname, str, len,
2223 OPEN_FLAGS_APPEND|(bin?O_BINARY:O_TEXT));
2226 /** Like write_str_to_file(), but also return -1 if there was a file
2227 already residing in <b>fname</b>. */
2229 write_bytes_to_new_file(const char *fname, const char *str, size_t len,
2230 int bin)
2232 return write_bytes_to_file_impl(fname, str, len,
2233 OPEN_FLAGS_DONT_REPLACE|
2234 (bin?O_BINARY:O_TEXT));
2238 * Read the contents of the open file <b>fd</b> presuming it is a FIFO
2239 * (or similar) file descriptor for which the size of the file isn't
2240 * known ahead of time. Return NULL on failure, and a NUL-terminated
2241 * string on success. On success, set <b>sz_out</b> to the number of
2242 * bytes read.
2244 char *
2245 read_file_to_str_until_eof(int fd, size_t max_bytes_to_read, size_t *sz_out)
2247 ssize_t r;
2248 size_t pos = 0;
2249 char *string = NULL;
2250 size_t string_max = 0;
2252 if (max_bytes_to_read+1 >= SIZE_T_CEILING)
2253 return NULL;
2255 do {
2256 /* XXXX This "add 1K" approach is a little goofy; if we care about
2257 * performance here, we should be doubling. But in practice we shouldn't
2258 * be using this function on big files anyway. */
2259 string_max = pos + 1024;
2260 if (string_max > max_bytes_to_read)
2261 string_max = max_bytes_to_read + 1;
2262 string = tor_realloc(string, string_max);
2263 r = read(fd, string + pos, string_max - pos - 1);
2264 if (r < 0) {
2265 tor_free(string);
2266 return NULL;
2269 pos += r;
2270 } while (r > 0 && pos < max_bytes_to_read);
2272 *sz_out = pos;
2273 string[pos] = '\0';
2274 return string;
2277 /** Read the contents of <b>filename</b> into a newly allocated
2278 * string; return the string on success or NULL on failure.
2280 * If <b>stat_out</b> is provided, store the result of stat()ing the
2281 * file into <b>stat_out</b>.
2283 * If <b>flags</b> &amp; RFTS_BIN, open the file in binary mode.
2284 * If <b>flags</b> &amp; RFTS_IGNORE_MISSING, don't warn if the file
2285 * doesn't exist.
2288 * This function <em>may</em> return an erroneous result if the file
2289 * is modified while it is running, but must not crash or overflow.
2290 * Right now, the error case occurs when the file length grows between
2291 * the call to stat and the call to read_all: the resulting string will
2292 * be truncated.
2294 char *
2295 read_file_to_str(const char *filename, int flags, struct stat *stat_out)
2297 int fd; /* router file */
2298 struct stat statbuf;
2299 char *string;
2300 ssize_t r;
2301 int bin = flags & RFTS_BIN;
2303 tor_assert(filename);
2305 fd = tor_open_cloexec(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
2306 if (fd<0) {
2307 int severity = LOG_WARN;
2308 int save_errno = errno;
2309 if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING))
2310 severity = LOG_INFO;
2311 log_fn(severity, LD_FS,"Could not open \"%s\": %s",filename,
2312 strerror(errno));
2313 errno = save_errno;
2314 return NULL;
2317 if (fstat(fd, &statbuf)<0) {
2318 int save_errno = errno;
2319 close(fd);
2320 log_warn(LD_FS,"Could not fstat \"%s\".",filename);
2321 errno = save_errno;
2322 return NULL;
2325 #ifndef _WIN32
2326 /** When we detect that we're reading from a FIFO, don't read more than
2327 * this many bytes. It's insane overkill for most uses. */
2328 #define FIFO_READ_MAX (1024*1024)
2329 if (S_ISFIFO(statbuf.st_mode)) {
2330 size_t sz = 0;
2331 string = read_file_to_str_until_eof(fd, FIFO_READ_MAX, &sz);
2332 if (string && stat_out) {
2333 statbuf.st_size = sz;
2334 memcpy(stat_out, &statbuf, sizeof(struct stat));
2336 close(fd);
2337 return string;
2339 #endif
2341 if ((uint64_t)(statbuf.st_size)+1 >= SIZE_T_CEILING) {
2342 close(fd);
2343 return NULL;
2346 string = tor_malloc((size_t)(statbuf.st_size+1));
2348 r = read_all(fd,string,(size_t)statbuf.st_size,0);
2349 if (r<0) {
2350 int save_errno = errno;
2351 log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
2352 strerror(errno));
2353 tor_free(string);
2354 close(fd);
2355 errno = save_errno;
2356 return NULL;
2358 string[r] = '\0'; /* NUL-terminate the result. */
2360 #if defined(_WIN32) || defined(__CYGWIN__)
2361 if (!bin && strchr(string, '\r')) {
2362 log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
2363 "when reading %s. Coping.",
2364 filename);
2365 tor_strstrip(string, "\r");
2366 r = strlen(string);
2368 if (!bin) {
2369 statbuf.st_size = (size_t) r;
2370 } else
2371 #endif
2372 if (r != statbuf.st_size) {
2373 /* Unless we're using text mode on win32, we'd better have an exact
2374 * match for size. */
2375 int save_errno = errno;
2376 log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
2377 (int)r, (long)statbuf.st_size,filename);
2378 tor_free(string);
2379 close(fd);
2380 errno = save_errno;
2381 return NULL;
2383 close(fd);
2384 if (stat_out) {
2385 memcpy(stat_out, &statbuf, sizeof(struct stat));
2388 return string;
2391 #define TOR_ISODIGIT(c) ('0' <= (c) && (c) <= '7')
2393 /** Given a c-style double-quoted escaped string in <b>s</b>, extract and
2394 * decode its contents into a newly allocated string. On success, assign this
2395 * string to *<b>result</b>, assign its length to <b>size_out</b> (if
2396 * provided), and return a pointer to the position in <b>s</b> immediately
2397 * after the string. On failure, return NULL.
2399 static const char *
2400 unescape_string(const char *s, char **result, size_t *size_out)
2402 const char *cp;
2403 char *out;
2404 if (s[0] != '\"')
2405 return NULL;
2406 cp = s+1;
2407 while (1) {
2408 switch (*cp) {
2409 case '\0':
2410 case '\n':
2411 return NULL;
2412 case '\"':
2413 goto end_of_loop;
2414 case '\\':
2415 if (cp[1] == 'x' || cp[1] == 'X') {
2416 if (!(TOR_ISXDIGIT(cp[2]) && TOR_ISXDIGIT(cp[3])))
2417 return NULL;
2418 cp += 4;
2419 } else if (TOR_ISODIGIT(cp[1])) {
2420 cp += 2;
2421 if (TOR_ISODIGIT(*cp)) ++cp;
2422 if (TOR_ISODIGIT(*cp)) ++cp;
2423 } else if (cp[1] == 'n' || cp[1] == 'r' || cp[1] == 't' || cp[1] == '"'
2424 || cp[1] == '\\' || cp[1] == '\'') {
2425 cp += 2;
2426 } else {
2427 return NULL;
2429 break;
2430 default:
2431 ++cp;
2432 break;
2435 end_of_loop:
2436 out = *result = tor_malloc(cp-s + 1);
2437 cp = s+1;
2438 while (1) {
2439 switch (*cp)
2441 case '\"':
2442 *out = '\0';
2443 if (size_out) *size_out = out - *result;
2444 return cp+1;
2445 case '\0':
2446 tor_fragile_assert();
2447 tor_free(*result);
2448 return NULL;
2449 case '\\':
2450 switch (cp[1])
2452 case 'n': *out++ = '\n'; cp += 2; break;
2453 case 'r': *out++ = '\r'; cp += 2; break;
2454 case 't': *out++ = '\t'; cp += 2; break;
2455 case 'x': case 'X':
2457 int x1, x2;
2459 x1 = hex_decode_digit(cp[2]);
2460 x2 = hex_decode_digit(cp[3]);
2461 if (x1 == -1 || x2 == -1) {
2462 tor_free(*result);
2463 return NULL;
2466 *out++ = ((x1<<4) + x2);
2467 cp += 4;
2469 break;
2470 case '0': case '1': case '2': case '3': case '4': case '5':
2471 case '6': case '7':
2473 int n = cp[1]-'0';
2474 cp += 2;
2475 if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
2476 if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
2477 if (n > 255) { tor_free(*result); return NULL; }
2478 *out++ = (char)n;
2480 break;
2481 case '\'':
2482 case '\"':
2483 case '\\':
2484 case '\?':
2485 *out++ = cp[1];
2486 cp += 2;
2487 break;
2488 default:
2489 tor_free(*result); return NULL;
2491 break;
2492 default:
2493 *out++ = *cp++;
2498 /** Given a string containing part of a configuration file or similar format,
2499 * advance past comments and whitespace and try to parse a single line. If we
2500 * parse a line successfully, set *<b>key_out</b> to a new string holding the
2501 * key portion and *<b>value_out</b> to a new string holding the value portion
2502 * of the line, and return a pointer to the start of the next line. If we run
2503 * out of data, return a pointer to the end of the string. If we encounter an
2504 * error, return NULL.
2506 const char *
2507 parse_config_line_from_str(const char *line, char **key_out, char **value_out)
2509 /* I believe the file format here is supposed to be:
2510 FILE = (EMPTYLINE | LINE)* (EMPTYLASTLINE | LASTLINE)?
2512 EMPTYLASTLINE = SPACE* | COMMENT
2513 EMPTYLINE = EMPTYLASTLINE NL
2514 SPACE = ' ' | '\r' | '\t'
2515 COMMENT = '#' NOT-NL*
2516 NOT-NL = Any character except '\n'
2517 NL = '\n'
2519 LASTLINE = SPACE* KEY SPACE* VALUES
2520 LINE = LASTLINE NL
2521 KEY = KEYCHAR+
2522 KEYCHAR = Any character except ' ', '\r', '\n', '\t', '#', "\"
2524 VALUES = QUOTEDVALUE | NORMALVALUE
2525 QUOTEDVALUE = QUOTE QVCHAR* QUOTE EOLSPACE?
2526 QUOTE = '"'
2527 QVCHAR = KEYCHAR | ESC ('n' | 't' | 'r' | '"' | ESC |'\'' | OCTAL | HEX)
2528 ESC = "\\"
2529 OCTAL = ODIGIT (ODIGIT ODIGIT?)?
2530 HEX = ('x' | 'X') HEXDIGIT HEXDIGIT
2531 ODIGIT = '0' .. '7'
2532 HEXDIGIT = '0'..'9' | 'a' .. 'f' | 'A' .. 'F'
2533 EOLSPACE = SPACE* COMMENT?
2535 NORMALVALUE = (VALCHAR | ESC ESC_IGNORE | CONTINUATION)* EOLSPACE?
2536 VALCHAR = Any character except ESC, '#', and '\n'
2537 ESC_IGNORE = Any character except '#' or '\n'
2538 CONTINUATION = ESC NL ( COMMENT NL )*
2541 const char *key, *val, *cp;
2542 int continuation = 0;
2544 tor_assert(key_out);
2545 tor_assert(value_out);
2547 *key_out = *value_out = NULL;
2548 key = val = NULL;
2549 /* Skip until the first keyword. */
2550 while (1) {
2551 while (TOR_ISSPACE(*line))
2552 ++line;
2553 if (*line == '#') {
2554 while (*line && *line != '\n')
2555 ++line;
2556 } else {
2557 break;
2561 if (!*line) { /* End of string? */
2562 *key_out = *value_out = NULL;
2563 return line;
2566 /* Skip until the next space or \ followed by newline. */
2567 key = line;
2568 while (*line && !TOR_ISSPACE(*line) && *line != '#' &&
2569 ! (line[0] == '\\' && line[1] == '\n'))
2570 ++line;
2571 *key_out = tor_strndup(key, line-key);
2573 /* Skip until the value. */
2574 while (*line == ' ' || *line == '\t')
2575 ++line;
2577 val = line;
2579 /* Find the end of the line. */
2580 if (*line == '\"') { // XXX No continuation handling is done here
2581 if (!(line = unescape_string(line, value_out, NULL)))
2582 return NULL;
2583 while (*line == ' ' || *line == '\t')
2584 ++line;
2585 if (*line && *line != '#' && *line != '\n')
2586 return NULL;
2587 } else {
2588 /* Look for the end of the line. */
2589 while (*line && *line != '\n' && (*line != '#' || continuation)) {
2590 if (*line == '\\' && line[1] == '\n') {
2591 continuation = 1;
2592 line += 2;
2593 } else if (*line == '#') {
2594 do {
2595 ++line;
2596 } while (*line && *line != '\n');
2597 if (*line == '\n')
2598 ++line;
2599 } else {
2600 ++line;
2604 if (*line == '\n') {
2605 cp = line++;
2606 } else {
2607 cp = line;
2609 /* Now back cp up to be the last nonspace character */
2610 while (cp>val && TOR_ISSPACE(*(cp-1)))
2611 --cp;
2613 tor_assert(cp >= val);
2615 /* Now copy out and decode the value. */
2616 *value_out = tor_strndup(val, cp-val);
2617 if (continuation) {
2618 char *v_out, *v_in;
2619 v_out = v_in = *value_out;
2620 while (*v_in) {
2621 if (*v_in == '#') {
2622 do {
2623 ++v_in;
2624 } while (*v_in && *v_in != '\n');
2625 if (*v_in == '\n')
2626 ++v_in;
2627 } else if (v_in[0] == '\\' && v_in[1] == '\n') {
2628 v_in += 2;
2629 } else {
2630 *v_out++ = *v_in++;
2633 *v_out = '\0';
2637 if (*line == '#') {
2638 do {
2639 ++line;
2640 } while (*line && *line != '\n');
2642 while (TOR_ISSPACE(*line)) ++line;
2644 return line;
2647 /** Expand any homedir prefix on <b>filename</b>; return a newly allocated
2648 * string. */
2649 char *
2650 expand_filename(const char *filename)
2652 tor_assert(filename);
2653 #ifdef _WIN32
2654 return tor_strdup(filename);
2655 #else
2656 if (*filename == '~') {
2657 char *home, *result=NULL;
2658 const char *rest;
2660 if (filename[1] == '/' || filename[1] == '\0') {
2661 home = getenv("HOME");
2662 if (!home) {
2663 log_warn(LD_CONFIG, "Couldn't find $HOME environment variable while "
2664 "expanding \"%s\"; defaulting to \"\".", filename);
2665 home = tor_strdup("");
2666 } else {
2667 home = tor_strdup(home);
2669 rest = strlen(filename)>=2?(filename+2):"";
2670 } else {
2671 #ifdef HAVE_PWD_H
2672 char *username, *slash;
2673 slash = strchr(filename, '/');
2674 if (slash)
2675 username = tor_strndup(filename+1,slash-filename-1);
2676 else
2677 username = tor_strdup(filename+1);
2678 if (!(home = get_user_homedir(username))) {
2679 log_warn(LD_CONFIG,"Couldn't get homedir for \"%s\"",username);
2680 tor_free(username);
2681 return NULL;
2683 tor_free(username);
2684 rest = slash ? (slash+1) : "";
2685 #else
2686 log_warn(LD_CONFIG, "Couldn't expend homedir on system without pwd.h");
2687 return tor_strdup(filename);
2688 #endif
2690 tor_assert(home);
2691 /* Remove trailing slash. */
2692 if (strlen(home)>1 && !strcmpend(home,PATH_SEPARATOR)) {
2693 home[strlen(home)-1] = '\0';
2695 tor_asprintf(&result,"%s"PATH_SEPARATOR"%s",home,rest);
2696 tor_free(home);
2697 return result;
2698 } else {
2699 return tor_strdup(filename);
2701 #endif
2704 #define MAX_SCANF_WIDTH 9999
2706 /** Helper: given an ASCII-encoded decimal digit, return its numeric value.
2707 * NOTE: requires that its input be in-bounds. */
2708 static int
2709 digit_to_num(char d)
2711 int num = ((int)d) - (int)'0';
2712 tor_assert(num <= 9 && num >= 0);
2713 return num;
2716 /** Helper: Read an unsigned int from *<b>bufp</b> of up to <b>width</b>
2717 * characters. (Handle arbitrary width if <b>width</b> is less than 0.) On
2718 * success, store the result in <b>out</b>, advance bufp to the next
2719 * character, and return 0. On failure, return -1. */
2720 static int
2721 scan_unsigned(const char **bufp, unsigned long *out, int width, int base)
2723 unsigned long result = 0;
2724 int scanned_so_far = 0;
2725 const int hex = base==16;
2726 tor_assert(base == 10 || base == 16);
2727 if (!bufp || !*bufp || !out)
2728 return -1;
2729 if (width<0)
2730 width=MAX_SCANF_WIDTH;
2732 while (**bufp && (hex?TOR_ISXDIGIT(**bufp):TOR_ISDIGIT(**bufp))
2733 && scanned_so_far < width) {
2734 int digit = hex?hex_decode_digit(*(*bufp)++):digit_to_num(*(*bufp)++);
2735 unsigned long new_result = result * base + digit;
2736 if (new_result < result)
2737 return -1; /* over/underflow. */
2738 result = new_result;
2739 ++scanned_so_far;
2742 if (!scanned_so_far) /* No actual digits scanned */
2743 return -1;
2745 *out = result;
2746 return 0;
2749 /** Helper: Read an signed int from *<b>bufp</b> of up to <b>width</b>
2750 * characters. (Handle arbitrary width if <b>width</b> is less than 0.) On
2751 * success, store the result in <b>out</b>, advance bufp to the next
2752 * character, and return 0. On failure, return -1. */
2753 static int
2754 scan_signed(const char **bufp, long *out, int width)
2756 int neg = 0;
2757 unsigned long result = 0;
2759 if (!bufp || !*bufp || !out)
2760 return -1;
2761 if (width<0)
2762 width=MAX_SCANF_WIDTH;
2764 if (**bufp == '-') {
2765 neg = 1;
2766 ++*bufp;
2767 --width;
2770 if (scan_unsigned(bufp, &result, width, 10) < 0)
2771 return -1;
2773 if (neg) {
2774 if (result > ((unsigned long)LONG_MAX) + 1)
2775 return -1; /* Underflow */
2776 *out = -(long)result;
2777 } else {
2778 if (result > LONG_MAX)
2779 return -1; /* Overflow */
2780 *out = (long)result;
2783 return 0;
2786 /** Helper: Read a decimal-formatted double from *<b>bufp</b> of up to
2787 * <b>width</b> characters. (Handle arbitrary width if <b>width</b> is less
2788 * than 0.) On success, store the result in <b>out</b>, advance bufp to the
2789 * next character, and return 0. On failure, return -1. */
2790 static int
2791 scan_double(const char **bufp, double *out, int width)
2793 int neg = 0;
2794 double result = 0;
2795 int scanned_so_far = 0;
2797 if (!bufp || !*bufp || !out)
2798 return -1;
2799 if (width<0)
2800 width=MAX_SCANF_WIDTH;
2802 if (**bufp == '-') {
2803 neg = 1;
2804 ++*bufp;
2807 while (**bufp && TOR_ISDIGIT(**bufp) && scanned_so_far < width) {
2808 const int digit = digit_to_num(*(*bufp)++);
2809 result = result * 10 + digit;
2810 ++scanned_so_far;
2812 if (**bufp == '.') {
2813 double fracval = 0, denominator = 1;
2814 ++*bufp;
2815 ++scanned_so_far;
2816 while (**bufp && TOR_ISDIGIT(**bufp) && scanned_so_far < width) {
2817 const int digit = digit_to_num(*(*bufp)++);
2818 fracval = fracval * 10 + digit;
2819 denominator *= 10;
2820 ++scanned_so_far;
2822 result += fracval / denominator;
2825 if (!scanned_so_far) /* No actual digits scanned */
2826 return -1;
2828 *out = neg ? -result : result;
2829 return 0;
2832 /** Helper: copy up to <b>width</b> non-space characters from <b>bufp</b> to
2833 * <b>out</b>. Make sure <b>out</b> is nul-terminated. Advance <b>bufp</b>
2834 * to the next non-space character or the EOS. */
2835 static int
2836 scan_string(const char **bufp, char *out, int width)
2838 int scanned_so_far = 0;
2839 if (!bufp || !out || width < 0)
2840 return -1;
2841 while (**bufp && ! TOR_ISSPACE(**bufp) && scanned_so_far < width) {
2842 *out++ = *(*bufp)++;
2843 ++scanned_so_far;
2845 *out = '\0';
2846 return 0;
2849 /** Locale-independent, minimal, no-surprises scanf variant, accepting only a
2850 * restricted pattern format. For more info on what it supports, see
2851 * tor_sscanf() documentation. */
2853 tor_vsscanf(const char *buf, const char *pattern, va_list ap)
2855 int n_matched = 0;
2857 while (*pattern) {
2858 if (*pattern != '%') {
2859 if (*buf == *pattern) {
2860 ++buf;
2861 ++pattern;
2862 continue;
2863 } else {
2864 return n_matched;
2866 } else {
2867 int width = -1;
2868 int longmod = 0;
2869 ++pattern;
2870 if (TOR_ISDIGIT(*pattern)) {
2871 width = digit_to_num(*pattern++);
2872 while (TOR_ISDIGIT(*pattern)) {
2873 width *= 10;
2874 width += digit_to_num(*pattern++);
2875 if (width > MAX_SCANF_WIDTH)
2876 return -1;
2878 if (!width) /* No zero-width things. */
2879 return -1;
2881 if (*pattern == 'l') {
2882 longmod = 1;
2883 ++pattern;
2885 if (*pattern == 'u' || *pattern == 'x') {
2886 unsigned long u;
2887 const int base = (*pattern == 'u') ? 10 : 16;
2888 if (!*buf)
2889 return n_matched;
2890 if (scan_unsigned(&buf, &u, width, base)<0)
2891 return n_matched;
2892 if (longmod) {
2893 unsigned long *out = va_arg(ap, unsigned long *);
2894 *out = u;
2895 } else {
2896 unsigned *out = va_arg(ap, unsigned *);
2897 if (u > UINT_MAX)
2898 return n_matched;
2899 *out = (unsigned) u;
2901 ++pattern;
2902 ++n_matched;
2903 } else if (*pattern == 'f') {
2904 double *d = va_arg(ap, double *);
2905 if (!longmod)
2906 return -1; /* float not supported */
2907 if (!*buf)
2908 return n_matched;
2909 if (scan_double(&buf, d, width)<0)
2910 return n_matched;
2911 ++pattern;
2912 ++n_matched;
2913 } else if (*pattern == 'd') {
2914 long lng=0;
2915 if (scan_signed(&buf, &lng, width)<0)
2916 return n_matched;
2917 if (longmod) {
2918 long *out = va_arg(ap, long *);
2919 *out = lng;
2920 } else {
2921 int *out = va_arg(ap, int *);
2922 if (lng < INT_MIN || lng > INT_MAX)
2923 return n_matched;
2924 *out = (int)lng;
2926 ++pattern;
2927 ++n_matched;
2928 } else if (*pattern == 's') {
2929 char *s = va_arg(ap, char *);
2930 if (longmod)
2931 return -1;
2932 if (width < 0)
2933 return -1;
2934 if (scan_string(&buf, s, width)<0)
2935 return n_matched;
2936 ++pattern;
2937 ++n_matched;
2938 } else if (*pattern == 'c') {
2939 char *ch = va_arg(ap, char *);
2940 if (longmod)
2941 return -1;
2942 if (width != -1)
2943 return -1;
2944 if (!*buf)
2945 return n_matched;
2946 *ch = *buf++;
2947 ++pattern;
2948 ++n_matched;
2949 } else if (*pattern == '%') {
2950 if (*buf != '%')
2951 return n_matched;
2952 if (longmod)
2953 return -1;
2954 ++buf;
2955 ++pattern;
2956 } else {
2957 return -1; /* Unrecognized pattern component. */
2962 return n_matched;
2965 /** Minimal sscanf replacement: parse <b>buf</b> according to <b>pattern</b>
2966 * and store the results in the corresponding argument fields. Differs from
2967 * sscanf in that:
2968 * <ul><li>It only handles %u, %lu, %x, %lx, %<NUM>s, %d, %ld, %lf, and %c.
2969 * <li>It only handles decimal inputs for %lf. (12.3, not 1.23e1)
2970 * <li>It does not handle arbitrarily long widths.
2971 * <li>Numbers do not consume any space characters.
2972 * <li>It is locale-independent.
2973 * <li>%u and %x do not consume any space.
2974 * <li>It returns -1 on malformed patterns.</ul>
2976 * (As with other locale-independent functions, we need this to parse data that
2977 * is in ASCII without worrying that the C library's locale-handling will make
2978 * miscellaneous characters look like numbers, spaces, and so on.)
2981 tor_sscanf(const char *buf, const char *pattern, ...)
2983 int r;
2984 va_list ap;
2985 va_start(ap, pattern);
2986 r = tor_vsscanf(buf, pattern, ap);
2987 va_end(ap);
2988 return r;
2991 /** Append the string produced by tor_asprintf(<b>pattern</b>, <b>...</b>)
2992 * to <b>sl</b>. */
2993 void
2994 smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern, ...)
2996 va_list ap;
2997 va_start(ap, pattern);
2998 smartlist_add_vasprintf(sl, pattern, ap);
2999 va_end(ap);
3002 /** va_list-based backend of smartlist_add_asprintf. */
3003 void
3004 smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
3005 va_list args)
3007 char *str = NULL;
3009 tor_vasprintf(&str, pattern, args);
3010 tor_assert(str != NULL);
3012 smartlist_add(sl, str);
3015 /** Return a new list containing the filenames in the directory <b>dirname</b>.
3016 * Return NULL on error or if <b>dirname</b> is not a directory.
3018 smartlist_t *
3019 tor_listdir(const char *dirname)
3021 smartlist_t *result;
3022 #ifdef _WIN32
3023 char *pattern=NULL;
3024 TCHAR tpattern[MAX_PATH] = {0};
3025 char name[MAX_PATH*2+1] = {0};
3026 HANDLE handle;
3027 WIN32_FIND_DATA findData;
3028 tor_asprintf(&pattern, "%s\\*", dirname);
3029 #ifdef UNICODE
3030 mbstowcs(tpattern,pattern,MAX_PATH);
3031 #else
3032 strlcpy(tpattern, pattern, MAX_PATH);
3033 #endif
3034 if (INVALID_HANDLE_VALUE == (handle = FindFirstFile(tpattern, &findData))) {
3035 tor_free(pattern);
3036 return NULL;
3038 result = smartlist_new();
3039 while (1) {
3040 #ifdef UNICODE
3041 wcstombs(name,findData.cFileName,MAX_PATH);
3042 name[sizeof(name)-1] = '\0';
3043 #else
3044 strlcpy(name,findData.cFileName,sizeof(name));
3045 #endif
3046 if (strcmp(name, ".") &&
3047 strcmp(name, "..")) {
3048 smartlist_add(result, tor_strdup(name));
3050 if (!FindNextFile(handle, &findData)) {
3051 DWORD err;
3052 if ((err = GetLastError()) != ERROR_NO_MORE_FILES) {
3053 char *errstr = format_win32_error(err);
3054 log_warn(LD_FS, "Error reading directory '%s': %s", dirname, errstr);
3055 tor_free(errstr);
3057 break;
3060 FindClose(handle);
3061 tor_free(pattern);
3062 #else
3063 DIR *d;
3064 struct dirent *de;
3065 if (!(d = opendir(dirname)))
3066 return NULL;
3068 result = smartlist_new();
3069 while ((de = readdir(d))) {
3070 if (!strcmp(de->d_name, ".") ||
3071 !strcmp(de->d_name, ".."))
3072 continue;
3073 smartlist_add(result, tor_strdup(de->d_name));
3075 closedir(d);
3076 #endif
3077 return result;
3080 /** Return true iff <b>filename</b> is a relative path. */
3082 path_is_relative(const char *filename)
3084 if (filename && filename[0] == '/')
3085 return 0;
3086 #ifdef _WIN32
3087 else if (filename && filename[0] == '\\')
3088 return 0;
3089 else if (filename && strlen(filename)>3 && TOR_ISALPHA(filename[0]) &&
3090 filename[1] == ':' && filename[2] == '\\')
3091 return 0;
3092 #endif
3093 else
3094 return 1;
3097 /* =====
3098 * Process helpers
3099 * ===== */
3101 #ifndef _WIN32
3102 /* Based on code contributed by christian grothoff */
3103 /** True iff we've called start_daemon(). */
3104 static int start_daemon_called = 0;
3105 /** True iff we've called finish_daemon(). */
3106 static int finish_daemon_called = 0;
3107 /** Socketpair used to communicate between parent and child process while
3108 * daemonizing. */
3109 static int daemon_filedes[2];
3110 /** Start putting the process into daemon mode: fork and drop all resources
3111 * except standard fds. The parent process never returns, but stays around
3112 * until finish_daemon is called. (Note: it's safe to call this more
3113 * than once: calls after the first are ignored.)
3115 void
3116 start_daemon(void)
3118 pid_t pid;
3120 if (start_daemon_called)
3121 return;
3122 start_daemon_called = 1;
3124 if (pipe(daemon_filedes)) {
3125 log_err(LD_GENERAL,"pipe failed; exiting. Error was %s", strerror(errno));
3126 exit(1);
3128 pid = fork();
3129 if (pid < 0) {
3130 log_err(LD_GENERAL,"fork failed. Exiting.");
3131 exit(1);
3133 if (pid) { /* Parent */
3134 int ok;
3135 char c;
3137 close(daemon_filedes[1]); /* we only read */
3138 ok = -1;
3139 while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
3140 if (c == '.')
3141 ok = 1;
3143 fflush(stdout);
3144 if (ok == 1)
3145 exit(0);
3146 else
3147 exit(1); /* child reported error */
3148 } else { /* Child */
3149 close(daemon_filedes[0]); /* we only write */
3151 pid = setsid(); /* Detach from controlling terminal */
3153 * Fork one more time, so the parent (the session group leader) can exit.
3154 * This means that we, as a non-session group leader, can never regain a
3155 * controlling terminal. This part is recommended by Stevens's
3156 * _Advanced Programming in the Unix Environment_.
3158 if (fork() != 0) {
3159 exit(0);
3161 set_main_thread(); /* We are now the main thread. */
3163 return;
3167 /** Finish putting the process into daemon mode: drop standard fds, and tell
3168 * the parent process to exit. (Note: it's safe to call this more than once:
3169 * calls after the first are ignored. Calls start_daemon first if it hasn't
3170 * been called already.)
3172 void
3173 finish_daemon(const char *desired_cwd)
3175 int nullfd;
3176 char c = '.';
3177 if (finish_daemon_called)
3178 return;
3179 if (!start_daemon_called)
3180 start_daemon();
3181 finish_daemon_called = 1;
3183 if (!desired_cwd)
3184 desired_cwd = "/";
3185 /* Don't hold the wrong FS mounted */
3186 if (chdir(desired_cwd) < 0) {
3187 log_err(LD_GENERAL,"chdir to \"%s\" failed. Exiting.",desired_cwd);
3188 exit(1);
3191 nullfd = tor_open_cloexec("/dev/null", O_RDWR, 0);
3192 if (nullfd < 0) {
3193 log_err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
3194 exit(1);
3196 /* close fds linking to invoking terminal, but
3197 * close usual incoming fds, but redirect them somewhere
3198 * useful so the fds don't get reallocated elsewhere.
3200 if (dup2(nullfd,0) < 0 ||
3201 dup2(nullfd,1) < 0 ||
3202 dup2(nullfd,2) < 0) {
3203 log_err(LD_GENERAL,"dup2 failed. Exiting.");
3204 exit(1);
3206 if (nullfd > 2)
3207 close(nullfd);
3208 /* signal success */
3209 if (write(daemon_filedes[1], &c, sizeof(char)) != sizeof(char)) {
3210 log_err(LD_GENERAL,"write failed. Exiting.");
3212 close(daemon_filedes[1]);
3214 #else
3215 /* defined(_WIN32) */
3216 void
3217 start_daemon(void)
3220 void
3221 finish_daemon(const char *cp)
3223 (void)cp;
3225 #endif
3227 /** Write the current process ID, followed by NL, into <b>filename</b>.
3229 void
3230 write_pidfile(char *filename)
3232 FILE *pidfile;
3234 if ((pidfile = fopen(filename, "w")) == NULL) {
3235 log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
3236 strerror(errno));
3237 } else {
3238 #ifdef _WIN32
3239 fprintf(pidfile, "%d\n", (int)_getpid());
3240 #else
3241 fprintf(pidfile, "%d\n", (int)getpid());
3242 #endif
3243 fclose(pidfile);
3247 #ifdef _WIN32
3248 HANDLE
3249 load_windows_system_library(const TCHAR *library_name)
3251 TCHAR path[MAX_PATH];
3252 unsigned n;
3253 n = GetSystemDirectory(path, MAX_PATH);
3254 if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH)
3255 return 0;
3256 _tcscat(path, TEXT("\\"));
3257 _tcscat(path, library_name);
3258 return LoadLibrary(path);
3260 #endif
3262 /** Format a single argument for being put on a Windows command line.
3263 * Returns a newly allocated string */
3264 static char *
3265 format_win_cmdline_argument(const char *arg)
3267 char *formatted_arg;
3268 char need_quotes;
3269 const char *c;
3270 int i;
3271 int bs_counter = 0;
3272 /* Backslash we can point to when one is inserted into the string */
3273 const char backslash = '\\';
3275 /* Smartlist of *char */
3276 smartlist_t *arg_chars;
3277 arg_chars = smartlist_new();
3279 /* Quote string if it contains whitespace or is empty */
3280 need_quotes = (strchr(arg, ' ') || strchr(arg, '\t') || '\0' == arg[0]);
3282 /* Build up smartlist of *chars */
3283 for (c=arg; *c != '\0'; c++) {
3284 if ('"' == *c) {
3285 /* Double up backslashes preceding a quote */
3286 for (i=0; i<(bs_counter*2); i++)
3287 smartlist_add(arg_chars, (void*)&backslash);
3288 bs_counter = 0;
3289 /* Escape the quote */
3290 smartlist_add(arg_chars, (void*)&backslash);
3291 smartlist_add(arg_chars, (void*)c);
3292 } else if ('\\' == *c) {
3293 /* Count backslashes until we know whether to double up */
3294 bs_counter++;
3295 } else {
3296 /* Don't double up slashes preceding a non-quote */
3297 for (i=0; i<bs_counter; i++)
3298 smartlist_add(arg_chars, (void*)&backslash);
3299 bs_counter = 0;
3300 smartlist_add(arg_chars, (void*)c);
3303 /* Don't double up trailing backslashes */
3304 for (i=0; i<bs_counter; i++)
3305 smartlist_add(arg_chars, (void*)&backslash);
3307 /* Allocate space for argument, quotes (if needed), and terminator */
3308 formatted_arg = tor_malloc(sizeof(char) *
3309 (smartlist_len(arg_chars) + (need_quotes?2:0) + 1));
3311 /* Add leading quote */
3312 i=0;
3313 if (need_quotes)
3314 formatted_arg[i++] = '"';
3316 /* Add characters */
3317 SMARTLIST_FOREACH(arg_chars, char*, c,
3319 formatted_arg[i++] = *c;
3322 /* Add trailing quote */
3323 if (need_quotes)
3324 formatted_arg[i++] = '"';
3325 formatted_arg[i] = '\0';
3327 smartlist_free(arg_chars);
3328 return formatted_arg;
3331 /** Format a command line for use on Windows, which takes the command as a
3332 * string rather than string array. Follows the rules from "Parsing C++
3333 * Command-Line Arguments" in MSDN. Algorithm based on list2cmdline in the
3334 * Python subprocess module. Returns a newly allocated string */
3335 char *
3336 tor_join_win_cmdline(const char *argv[])
3338 smartlist_t *argv_list;
3339 char *joined_argv;
3340 int i;
3342 /* Format each argument and put the result in a smartlist */
3343 argv_list = smartlist_new();
3344 for (i=0; argv[i] != NULL; i++) {
3345 smartlist_add(argv_list, (void *)format_win_cmdline_argument(argv[i]));
3348 /* Join the arguments with whitespace */
3349 joined_argv = smartlist_join_strings(argv_list, " ", 0, NULL);
3351 /* Free the newly allocated arguments, and the smartlist */
3352 SMARTLIST_FOREACH(argv_list, char *, arg,
3354 tor_free(arg);
3356 smartlist_free(argv_list);
3358 return joined_argv;
3362 * Helper function to output hex numbers, called by
3363 * format_helper_exit_status(). This writes the hexadecimal digits of x into
3364 * buf, up to max_len digits, and returns the actual number of digits written.
3365 * If there is insufficient space, it will write nothing and return 0.
3367 * This function DOES NOT add a terminating NUL character to its output: be
3368 * careful!
3370 * This accepts an unsigned int because format_helper_exit_status() needs to
3371 * call it with a signed int and an unsigned char, and since the C standard
3372 * does not guarantee that an int is wider than a char (an int must be at
3373 * least 16 bits but it is permitted for a char to be that wide as well), we
3374 * can't assume a signed int is sufficient to accomodate an unsigned char.
3375 * Thus, format_helper_exit_status() will still need to emit any require '-'
3376 * on its own.
3378 * For most purposes, you'd want to use tor_snprintf("%x") instead of this
3379 * function; it's designed to be used in code paths where you can't call
3380 * arbitrary C functions.
3383 format_hex_number_for_helper_exit_status(unsigned int x, char *buf,
3384 int max_len)
3386 int len;
3387 unsigned int tmp;
3388 char *cur;
3390 /* Sanity check */
3391 if (!buf || max_len <= 0)
3392 return 0;
3394 /* How many chars do we need for x? */
3395 if (x > 0) {
3396 len = 0;
3397 tmp = x;
3398 while (tmp > 0) {
3399 tmp >>= 4;
3400 ++len;
3402 } else {
3403 len = 1;
3406 /* Bail if we would go past the end of the buffer */
3407 if (len > max_len)
3408 return 0;
3410 /* Point to last one */
3411 cur = buf + len - 1;
3413 /* Convert x to hex */
3414 do {
3415 *cur-- = "0123456789ABCDEF"[x & 0xf];
3416 x >>= 4;
3417 } while (x != 0 && cur >= buf);
3419 /* Return len */
3420 return len;
3423 /** Format <b>child_state</b> and <b>saved_errno</b> as a hex string placed in
3424 * <b>hex_errno</b>. Called between fork and _exit, so must be signal-handler
3425 * safe.
3427 * <b>hex_errno</b> must have at least HEX_ERRNO_SIZE bytes available.
3429 * The format of <b>hex_errno</b> is: "CHILD_STATE/ERRNO\n", left-padded
3430 * with spaces. Note that there is no trailing \0. CHILD_STATE indicates where
3431 * in the processs of starting the child process did the failure occur (see
3432 * CHILD_STATE_* macros for definition), and SAVED_ERRNO is the value of
3433 * errno when the failure occurred.
3435 * On success return the number of characters added to hex_errno, not counting
3436 * the terminating NUL; return -1 on error.
3439 format_helper_exit_status(unsigned char child_state, int saved_errno,
3440 char *hex_errno)
3442 unsigned int unsigned_errno;
3443 int written, left;
3444 char *cur;
3445 size_t i;
3446 int res = -1;
3448 /* Fill hex_errno with spaces, and a trailing newline (memset may
3449 not be signal handler safe, so we can't use it) */
3450 for (i = 0; i < (HEX_ERRNO_SIZE - 1); i++)
3451 hex_errno[i] = ' ';
3452 hex_errno[HEX_ERRNO_SIZE - 1] = '\n';
3454 /* Convert errno to be unsigned for hex conversion */
3455 if (saved_errno < 0) {
3456 unsigned_errno = (unsigned int) -saved_errno;
3457 } else {
3458 unsigned_errno = (unsigned int) saved_errno;
3462 * Count how many chars of space we have left, and keep a pointer into the
3463 * current point in the buffer.
3465 left = HEX_ERRNO_SIZE;
3466 cur = hex_errno;
3468 /* Emit child_state */
3469 written = format_hex_number_for_helper_exit_status(child_state,
3470 cur, left);
3471 if (written <= 0)
3472 goto err;
3474 /* Adjust left and cur */
3475 left -= written;
3476 cur += written;
3477 if (left <= 0)
3478 goto err;
3480 /* Now the '/' */
3481 *cur = '/';
3483 /* Adjust left and cur */
3484 ++cur;
3485 --left;
3486 if (left <= 0)
3487 goto err;
3489 /* Need minus? */
3490 if (saved_errno < 0) {
3491 *cur = '-';
3492 ++cur;
3493 --left;
3494 if (left <= 0)
3495 goto err;
3498 /* Emit unsigned_errno */
3499 written = format_hex_number_for_helper_exit_status(unsigned_errno,
3500 cur, left);
3502 if (written <= 0)
3503 goto err;
3505 /* Adjust left and cur */
3506 left -= written;
3507 cur += written;
3509 /* Check that we have enough space left for a newline */
3510 if (left <= 0)
3511 goto err;
3513 /* Emit the newline and NUL */
3514 *cur++ = '\n';
3515 *cur++ = '\0';
3517 res = (int)(cur - hex_errno - 1);
3519 goto done;
3521 err:
3523 * In error exit, just write a '\0' in the first char so whatever called
3524 * this at least won't fall off the end.
3526 *hex_errno = '\0';
3528 done:
3529 return res;
3532 /* Maximum number of file descriptors, if we cannot get it via sysconf() */
3533 #define DEFAULT_MAX_FD 256
3535 /** Terminate the process of <b>process_handle</b>.
3536 * Code borrowed from Python's os.kill. */
3538 tor_terminate_process(process_handle_t *process_handle)
3540 #ifdef _WIN32
3541 if (tor_get_exit_code(process_handle, 0, NULL) == PROCESS_EXIT_RUNNING) {
3542 HANDLE handle;
3543 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
3544 attempt to open and terminate the process. */
3545 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE,
3546 process_handle->pid.dwProcessId);
3547 if (!handle)
3548 return -1;
3550 if (!TerminateProcess(handle, 0))
3551 return -1;
3552 else
3553 return 0;
3555 #else /* Unix */
3556 return kill(process_handle->pid, SIGTERM);
3557 #endif
3559 return -1;
3562 /** Return the Process ID of <b>process_handle</b>. */
3564 tor_process_get_pid(process_handle_t *process_handle)
3566 #ifdef _WIN32
3567 return (int) process_handle->pid.dwProcessId;
3568 #else
3569 return (int) process_handle->pid;
3570 #endif
3573 #ifdef _WIN32
3574 HANDLE
3575 tor_process_get_stdout_pipe(process_handle_t *process_handle)
3577 return process_handle->stdout_pipe;
3579 #else
3580 /* DOCDOC tor_process_get_stdout_pipe */
3581 FILE *
3582 tor_process_get_stdout_pipe(process_handle_t *process_handle)
3584 return process_handle->stdout_handle;
3586 #endif
3588 /* DOCDOC process_handle_new */
3589 static process_handle_t *
3590 process_handle_new(void)
3592 process_handle_t *out = tor_malloc_zero(sizeof(process_handle_t));
3594 #ifdef _WIN32
3595 out->stdout_pipe = INVALID_HANDLE_VALUE;
3596 out->stderr_pipe = INVALID_HANDLE_VALUE;
3597 #else
3598 out->stdout_pipe = -1;
3599 out->stderr_pipe = -1;
3600 #endif
3602 return out;
3606 * @name child-process states
3608 * Each of these values represents a possible state that a child process can
3609 * be in. They're used to determine what to say when telling the parent how
3610 * far along we were before failure.
3612 * @{
3614 #define CHILD_STATE_INIT 0
3615 #define CHILD_STATE_PIPE 1
3616 #define CHILD_STATE_MAXFD 2
3617 #define CHILD_STATE_FORK 3
3618 #define CHILD_STATE_DUPOUT 4
3619 #define CHILD_STATE_DUPERR 5
3620 #define CHILD_STATE_REDIRECT 6
3621 #define CHILD_STATE_CLOSEFD 7
3622 #define CHILD_STATE_EXEC 8
3623 #define CHILD_STATE_FAILEXEC 9
3624 /** @} */
3625 /** Start a program in the background. If <b>filename</b> contains a '/', then
3626 * it will be treated as an absolute or relative path. Otherwise, on
3627 * non-Windows systems, the system path will be searched for <b>filename</b>.
3628 * On Windows, only the current directory will be searched. Here, to search the
3629 * system path (as well as the application directory, current working
3630 * directory, and system directories), set filename to NULL.
3632 * The strings in <b>argv</b> will be passed as the command line arguments of
3633 * the child program (following convention, argv[0] should normally be the
3634 * filename of the executable, and this must be the case if <b>filename</b> is
3635 * NULL). The last element of argv must be NULL. A handle to the child process
3636 * will be returned in process_handle (which must be non-NULL). Read
3637 * process_handle.status to find out if the process was successfully launched.
3638 * For convenience, process_handle.status is returned by this function.
3640 * Some parts of this code are based on the POSIX subprocess module from
3641 * Python, and example code from
3642 * http://msdn.microsoft.com/en-us/library/ms682499%28v=vs.85%29.aspx.
3645 tor_spawn_background(const char *const filename, const char **argv,
3646 process_environment_t *env,
3647 process_handle_t **process_handle_out)
3649 #ifdef _WIN32
3650 HANDLE stdout_pipe_read = NULL;
3651 HANDLE stdout_pipe_write = NULL;
3652 HANDLE stderr_pipe_read = NULL;
3653 HANDLE stderr_pipe_write = NULL;
3654 process_handle_t *process_handle;
3655 int status;
3657 STARTUPINFOA siStartInfo;
3658 BOOL retval = FALSE;
3660 SECURITY_ATTRIBUTES saAttr;
3661 char *joined_argv;
3663 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
3664 saAttr.bInheritHandle = TRUE;
3665 /* TODO: should we set explicit security attributes? (#2046, comment 5) */
3666 saAttr.lpSecurityDescriptor = NULL;
3668 /* Assume failure to start process */
3669 status = PROCESS_STATUS_ERROR;
3671 /* Set up pipe for stdout */
3672 if (!CreatePipe(&stdout_pipe_read, &stdout_pipe_write, &saAttr, 0)) {
3673 log_warn(LD_GENERAL,
3674 "Failed to create pipe for stdout communication with child process: %s",
3675 format_win32_error(GetLastError()));
3676 return status;
3678 if (!SetHandleInformation(stdout_pipe_read, HANDLE_FLAG_INHERIT, 0)) {
3679 log_warn(LD_GENERAL,
3680 "Failed to configure pipe for stdout communication with child "
3681 "process: %s", format_win32_error(GetLastError()));
3682 return status;
3685 /* Set up pipe for stderr */
3686 if (!CreatePipe(&stderr_pipe_read, &stderr_pipe_write, &saAttr, 0)) {
3687 log_warn(LD_GENERAL,
3688 "Failed to create pipe for stderr communication with child process: %s",
3689 format_win32_error(GetLastError()));
3690 return status;
3692 if (!SetHandleInformation(stderr_pipe_read, HANDLE_FLAG_INHERIT, 0)) {
3693 log_warn(LD_GENERAL,
3694 "Failed to configure pipe for stderr communication with child "
3695 "process: %s", format_win32_error(GetLastError()));
3696 return status;
3699 /* Create the child process */
3701 /* Windows expects argv to be a whitespace delimited string, so join argv up
3703 joined_argv = tor_join_win_cmdline(argv);
3705 process_handle = process_handle_new();
3706 process_handle->status = status;
3708 ZeroMemory(&(process_handle->pid), sizeof(PROCESS_INFORMATION));
3709 ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
3710 siStartInfo.cb = sizeof(STARTUPINFO);
3711 siStartInfo.hStdError = stderr_pipe_write;
3712 siStartInfo.hStdOutput = stdout_pipe_write;
3713 siStartInfo.hStdInput = NULL;
3714 siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
3716 /* Create the child process */
3718 retval = CreateProcessA(filename, // module name
3719 joined_argv, // command line
3720 /* TODO: should we set explicit security attributes? (#2046, comment 5) */
3721 NULL, // process security attributes
3722 NULL, // primary thread security attributes
3723 TRUE, // handles are inherited
3724 /*(TODO: set CREATE_NEW CONSOLE/PROCESS_GROUP to make GetExitCodeProcess()
3725 * work?) */
3726 0, // creation flags
3727 (env==NULL) ? NULL : env->windows_environment_block,
3728 NULL, // use parent's current directory
3729 &siStartInfo, // STARTUPINFO pointer
3730 &(process_handle->pid)); // receives PROCESS_INFORMATION
3732 tor_free(joined_argv);
3734 if (!retval) {
3735 log_warn(LD_GENERAL,
3736 "Failed to create child process %s: %s", filename?filename:argv[0],
3737 format_win32_error(GetLastError()));
3738 tor_free(process_handle);
3739 } else {
3740 /* TODO: Close hProcess and hThread in process_handle->pid? */
3741 process_handle->stdout_pipe = stdout_pipe_read;
3742 process_handle->stderr_pipe = stderr_pipe_read;
3743 status = process_handle->status = PROCESS_STATUS_RUNNING;
3746 /* TODO: Close pipes on exit */
3747 *process_handle_out = process_handle;
3748 return status;
3749 #else // _WIN32
3750 pid_t pid;
3751 int stdout_pipe[2];
3752 int stderr_pipe[2];
3753 int fd, retval;
3754 ssize_t nbytes;
3755 process_handle_t *process_handle;
3756 int status;
3758 const char *error_message = SPAWN_ERROR_MESSAGE;
3759 size_t error_message_length;
3761 /* Represents where in the process of spawning the program is;
3762 this is used for printing out the error message */
3763 unsigned char child_state = CHILD_STATE_INIT;
3765 char hex_errno[HEX_ERRNO_SIZE];
3767 static int max_fd = -1;
3769 status = PROCESS_STATUS_ERROR;
3771 /* We do the strlen here because strlen() is not signal handler safe,
3772 and we are not allowed to use unsafe functions between fork and exec */
3773 error_message_length = strlen(error_message);
3775 child_state = CHILD_STATE_PIPE;
3777 /* Set up pipe for redirecting stdout and stderr of child */
3778 retval = pipe(stdout_pipe);
3779 if (-1 == retval) {
3780 log_warn(LD_GENERAL,
3781 "Failed to set up pipe for stdout communication with child process: %s",
3782 strerror(errno));
3783 return status;
3786 retval = pipe(stderr_pipe);
3787 if (-1 == retval) {
3788 log_warn(LD_GENERAL,
3789 "Failed to set up pipe for stderr communication with child process: %s",
3790 strerror(errno));
3792 close(stdout_pipe[0]);
3793 close(stdout_pipe[1]);
3795 return status;
3798 child_state = CHILD_STATE_MAXFD;
3800 #ifdef _SC_OPEN_MAX
3801 if (-1 == max_fd) {
3802 max_fd = (int) sysconf(_SC_OPEN_MAX);
3803 if (max_fd == -1) {
3804 max_fd = DEFAULT_MAX_FD;
3805 log_warn(LD_GENERAL,
3806 "Cannot find maximum file descriptor, assuming %d", max_fd);
3809 #else
3810 max_fd = DEFAULT_MAX_FD;
3811 #endif
3813 child_state = CHILD_STATE_FORK;
3815 pid = fork();
3816 if (0 == pid) {
3817 /* In child */
3819 child_state = CHILD_STATE_DUPOUT;
3821 /* Link child stdout to the write end of the pipe */
3822 retval = dup2(stdout_pipe[1], STDOUT_FILENO);
3823 if (-1 == retval)
3824 goto error;
3826 child_state = CHILD_STATE_DUPERR;
3828 /* Link child stderr to the write end of the pipe */
3829 retval = dup2(stderr_pipe[1], STDERR_FILENO);
3830 if (-1 == retval)
3831 goto error;
3833 child_state = CHILD_STATE_REDIRECT;
3835 /* Link stdin to /dev/null */
3836 fd = open("/dev/null", O_RDONLY); /* NOT cloexec, obviously. */
3837 if (fd != -1)
3838 dup2(fd, STDIN_FILENO);
3839 else
3840 goto error;
3842 child_state = CHILD_STATE_CLOSEFD;
3844 close(stderr_pipe[0]);
3845 close(stderr_pipe[1]);
3846 close(stdout_pipe[0]);
3847 close(stdout_pipe[1]);
3848 close(fd);
3850 /* Close all other fds, including the read end of the pipe */
3851 /* XXX: We should now be doing enough FD_CLOEXEC setting to make
3852 * this needless. */
3853 for (fd = STDERR_FILENO + 1; fd < max_fd; fd++) {
3854 close(fd);
3857 child_state = CHILD_STATE_EXEC;
3859 /* Call the requested program. We need the cast because
3860 execvp doesn't define argv as const, even though it
3861 does not modify the arguments */
3862 if (env)
3863 execve(filename, (char *const *) argv, env->unixoid_environment_block);
3864 else
3865 execvp(filename, (char *const *) argv);
3867 /* If we got here, the exec or open(/dev/null) failed */
3869 child_state = CHILD_STATE_FAILEXEC;
3871 error:
3873 /* XXX: are we leaking fds from the pipe? */
3874 int n;
3876 n = format_helper_exit_status(child_state, errno, hex_errno);
3878 if (n >= 0) {
3879 /* Write the error message. GCC requires that we check the return
3880 value, but there is nothing we can do if it fails */
3881 /* TODO: Don't use STDOUT, use a pipe set up just for this purpose */
3882 nbytes = write(STDOUT_FILENO, error_message, error_message_length);
3883 nbytes = write(STDOUT_FILENO, hex_errno, n);
3887 (void) nbytes;
3889 _exit(255);
3890 /* Never reached, but avoids compiler warning */
3891 return status;
3894 /* In parent */
3896 if (-1 == pid) {
3897 log_warn(LD_GENERAL, "Failed to fork child process: %s", strerror(errno));
3898 close(stdout_pipe[0]);
3899 close(stdout_pipe[1]);
3900 close(stderr_pipe[0]);
3901 close(stderr_pipe[1]);
3902 return status;
3905 process_handle = process_handle_new();
3906 process_handle->status = status;
3907 process_handle->pid = pid;
3909 /* TODO: If the child process forked but failed to exec, waitpid it */
3911 /* Return read end of the pipes to caller, and close write end */
3912 process_handle->stdout_pipe = stdout_pipe[0];
3913 retval = close(stdout_pipe[1]);
3915 if (-1 == retval) {
3916 log_warn(LD_GENERAL,
3917 "Failed to close write end of stdout pipe in parent process: %s",
3918 strerror(errno));
3921 process_handle->stderr_pipe = stderr_pipe[0];
3922 retval = close(stderr_pipe[1]);
3924 if (-1 == retval) {
3925 log_warn(LD_GENERAL,
3926 "Failed to close write end of stderr pipe in parent process: %s",
3927 strerror(errno));
3930 status = process_handle->status = PROCESS_STATUS_RUNNING;
3931 /* Set stdout/stderr pipes to be non-blocking */
3932 fcntl(process_handle->stdout_pipe, F_SETFL, O_NONBLOCK);
3933 fcntl(process_handle->stderr_pipe, F_SETFL, O_NONBLOCK);
3934 /* Open the buffered IO streams */
3935 process_handle->stdout_handle = fdopen(process_handle->stdout_pipe, "r");
3936 process_handle->stderr_handle = fdopen(process_handle->stderr_pipe, "r");
3938 *process_handle_out = process_handle;
3939 return process_handle->status;
3940 #endif // _WIN32
3943 /** Destroy all resources allocated by the process handle in
3944 * <b>process_handle</b>.
3945 * If <b>also_terminate_process</b> is true, also terminate the
3946 * process of the process handle. */
3947 void
3948 tor_process_handle_destroy(process_handle_t *process_handle,
3949 int also_terminate_process)
3951 if (!process_handle)
3952 return;
3954 if (also_terminate_process) {
3955 if (tor_terminate_process(process_handle) < 0) {
3956 const char *errstr =
3957 #ifdef _WIN32
3958 format_win32_error(GetLastError());
3959 #else
3960 strerror(errno);
3961 #endif
3962 log_notice(LD_GENERAL, "Failed to terminate process with "
3963 "PID '%d' ('%s').", tor_process_get_pid(process_handle),
3964 errstr);
3965 } else {
3966 log_info(LD_GENERAL, "Terminated process with PID '%d'.",
3967 tor_process_get_pid(process_handle));
3971 process_handle->status = PROCESS_STATUS_NOTRUNNING;
3973 #ifdef _WIN32
3974 if (process_handle->stdout_pipe)
3975 CloseHandle(process_handle->stdout_pipe);
3977 if (process_handle->stderr_pipe)
3978 CloseHandle(process_handle->stderr_pipe);
3979 #else
3980 if (process_handle->stdout_handle)
3981 fclose(process_handle->stdout_handle);
3983 if (process_handle->stderr_handle)
3984 fclose(process_handle->stderr_handle);
3985 #endif
3987 memset(process_handle, 0x0f, sizeof(process_handle_t));
3988 tor_free(process_handle);
3991 /** Get the exit code of a process specified by <b>process_handle</b> and store
3992 * it in <b>exit_code</b>, if set to a non-NULL value. If <b>block</b> is set
3993 * to true, the call will block until the process has exited. Otherwise if
3994 * the process is still running, the function will return
3995 * PROCESS_EXIT_RUNNING, and exit_code will be left unchanged. Returns
3996 * PROCESS_EXIT_EXITED if the process did exit. If there is a failure,
3997 * PROCESS_EXIT_ERROR will be returned and the contents of exit_code (if
3998 * non-NULL) will be undefined. N.B. Under *nix operating systems, this will
3999 * probably not work in Tor, because waitpid() is called in main.c to reap any
4000 * terminated child processes.*/
4002 tor_get_exit_code(const process_handle_t *process_handle,
4003 int block, int *exit_code)
4005 #ifdef _WIN32
4006 DWORD retval;
4007 BOOL success;
4009 if (block) {
4010 /* Wait for the process to exit */
4011 retval = WaitForSingleObject(process_handle->pid.hProcess, INFINITE);
4012 if (retval != WAIT_OBJECT_0) {
4013 log_warn(LD_GENERAL, "WaitForSingleObject() failed (%d): %s",
4014 (int)retval, format_win32_error(GetLastError()));
4015 return PROCESS_EXIT_ERROR;
4017 } else {
4018 retval = WaitForSingleObject(process_handle->pid.hProcess, 0);
4019 if (WAIT_TIMEOUT == retval) {
4020 /* Process has not exited */
4021 return PROCESS_EXIT_RUNNING;
4022 } else if (retval != WAIT_OBJECT_0) {
4023 log_warn(LD_GENERAL, "WaitForSingleObject() failed (%d): %s",
4024 (int)retval, format_win32_error(GetLastError()));
4025 return PROCESS_EXIT_ERROR;
4029 if (exit_code != NULL) {
4030 success = GetExitCodeProcess(process_handle->pid.hProcess,
4031 (PDWORD)exit_code);
4032 if (!success) {
4033 log_warn(LD_GENERAL, "GetExitCodeProcess() failed: %s",
4034 format_win32_error(GetLastError()));
4035 return PROCESS_EXIT_ERROR;
4038 #else
4039 int stat_loc;
4040 int retval;
4042 retval = waitpid(process_handle->pid, &stat_loc, block?0:WNOHANG);
4043 if (!block && 0 == retval) {
4044 /* Process has not exited */
4045 return PROCESS_EXIT_RUNNING;
4046 } else if (retval != process_handle->pid) {
4047 log_warn(LD_GENERAL, "waitpid() failed for PID %d: %s",
4048 process_handle->pid, strerror(errno));
4049 return PROCESS_EXIT_ERROR;
4052 if (!WIFEXITED(stat_loc)) {
4053 log_warn(LD_GENERAL, "Process %d did not exit normally",
4054 process_handle->pid);
4055 return PROCESS_EXIT_ERROR;
4058 if (exit_code != NULL)
4059 *exit_code = WEXITSTATUS(stat_loc);
4060 #endif // _WIN32
4062 return PROCESS_EXIT_EXITED;
4065 /** Helper: return the number of characters in <b>s</b> preceding the first
4066 * occurrence of <b>ch</b>. If <b>ch</b> does not occur in <b>s</b>, return
4067 * the length of <b>s</b>. Should be equivalent to strspn(s, "ch"). */
4068 static INLINE size_t
4069 str_num_before(const char *s, char ch)
4071 const char *cp = strchr(s, ch);
4072 if (cp)
4073 return cp - s;
4074 else
4075 return strlen(s);
4078 /** Return non-zero iff getenv would consider <b>s1</b> and <b>s2</b>
4079 * to have the same name as strings in a process's environment. */
4081 environment_variable_names_equal(const char *s1, const char *s2)
4083 size_t s1_name_len = str_num_before(s1, '=');
4084 size_t s2_name_len = str_num_before(s2, '=');
4086 return (s1_name_len == s2_name_len &&
4087 tor_memeq(s1, s2, s1_name_len));
4090 /** Free <b>env</b> (assuming it was produced by
4091 * process_environment_make). */
4092 void
4093 process_environment_free(process_environment_t *env)
4095 if (env == NULL) return;
4097 /* As both an optimization hack to reduce consing on Unixoid systems
4098 * and a nice way to ensure that some otherwise-Windows-specific
4099 * code will always get tested before changes to it get merged, the
4100 * strings which env->unixoid_environment_block points to are packed
4101 * into env->windows_environment_block. */
4102 tor_free(env->unixoid_environment_block);
4103 tor_free(env->windows_environment_block);
4105 tor_free(env);
4108 /** Make a process_environment_t containing the environment variables
4109 * specified in <b>env_vars</b> (as C strings of the form
4110 * "NAME=VALUE"). */
4111 process_environment_t *
4112 process_environment_make(struct smartlist_t *env_vars)
4114 process_environment_t *env = tor_malloc_zero(sizeof(process_environment_t));
4115 size_t n_env_vars = smartlist_len(env_vars);
4116 size_t i;
4117 size_t total_env_length;
4118 smartlist_t *env_vars_sorted;
4120 tor_assert(n_env_vars + 1 != 0);
4121 env->unixoid_environment_block = tor_calloc(n_env_vars + 1, sizeof(char *));
4122 /* env->unixoid_environment_block is already NULL-terminated,
4123 * because we assume that NULL == 0 (and check that during compilation). */
4125 total_env_length = 1; /* terminating NUL of terminating empty string */
4126 for (i = 0; i < n_env_vars; ++i) {
4127 const char *s = smartlist_get(env_vars, i);
4128 size_t slen = strlen(s);
4130 tor_assert(slen + 1 != 0);
4131 tor_assert(slen + 1 < SIZE_MAX - total_env_length);
4132 total_env_length += slen + 1;
4135 env->windows_environment_block = tor_malloc_zero(total_env_length);
4136 /* env->windows_environment_block is already
4137 * (NUL-terminated-empty-string)-terminated. */
4139 /* Some versions of Windows supposedly require that environment
4140 * blocks be sorted. Or maybe some Windows programs (or their
4141 * runtime libraries) fail to look up strings in non-sorted
4142 * environment blocks.
4144 * Also, sorting strings makes it easy to find duplicate environment
4145 * variables and environment-variable strings without an '=' on all
4146 * OSes, and they can cause badness. Let's complain about those. */
4147 env_vars_sorted = smartlist_new();
4148 smartlist_add_all(env_vars_sorted, env_vars);
4149 smartlist_sort_strings(env_vars_sorted);
4151 /* Now copy the strings into the environment blocks. */
4153 char *cp = env->windows_environment_block;
4154 const char *prev_env_var = NULL;
4156 for (i = 0; i < n_env_vars; ++i) {
4157 const char *s = smartlist_get(env_vars_sorted, i);
4158 size_t slen = strlen(s);
4159 size_t s_name_len = str_num_before(s, '=');
4161 if (s_name_len == slen) {
4162 log_warn(LD_GENERAL,
4163 "Preparing an environment containing a variable "
4164 "without a value: %s",
4167 if (prev_env_var != NULL &&
4168 environment_variable_names_equal(s, prev_env_var)) {
4169 log_warn(LD_GENERAL,
4170 "Preparing an environment containing two variables "
4171 "with the same name: %s and %s",
4172 prev_env_var, s);
4175 prev_env_var = s;
4177 /* Actually copy the string into the environment. */
4178 memcpy(cp, s, slen+1);
4179 env->unixoid_environment_block[i] = cp;
4180 cp += slen+1;
4183 tor_assert(cp == env->windows_environment_block + total_env_length - 1);
4186 smartlist_free(env_vars_sorted);
4188 return env;
4191 /** Return a newly allocated smartlist containing every variable in
4192 * this process's environment, as a NUL-terminated string of the form
4193 * "NAME=VALUE". Note that on some/many/most/all OSes, the parent
4194 * process can put strings not of that form in our environment;
4195 * callers should try to not get crashed by that.
4197 * The returned strings are heap-allocated, and must be freed by the
4198 * caller. */
4199 struct smartlist_t *
4200 get_current_process_environment_variables(void)
4202 smartlist_t *sl = smartlist_new();
4204 char **environ_tmp; /* Not const char ** ? Really? */
4205 for (environ_tmp = get_environment(); *environ_tmp; ++environ_tmp) {
4206 smartlist_add(sl, tor_strdup(*environ_tmp));
4209 return sl;
4212 /** For each string s in <b>env_vars</b> such that
4213 * environment_variable_names_equal(s, <b>new_var</b>), remove it; if
4214 * <b>free_p</b> is non-zero, call <b>free_old</b>(s). If
4215 * <b>new_var</b> contains '=', insert it into <b>env_vars</b>. */
4216 void
4217 set_environment_variable_in_smartlist(struct smartlist_t *env_vars,
4218 const char *new_var,
4219 void (*free_old)(void*),
4220 int free_p)
4222 SMARTLIST_FOREACH_BEGIN(env_vars, const char *, s) {
4223 if (environment_variable_names_equal(s, new_var)) {
4224 SMARTLIST_DEL_CURRENT(env_vars, s);
4225 if (free_p) {
4226 free_old((void *)s);
4229 } SMARTLIST_FOREACH_END(s);
4231 if (strchr(new_var, '=') != NULL) {
4232 smartlist_add(env_vars, (void *)new_var);
4236 #ifdef _WIN32
4237 /** Read from a handle <b>h</b> into <b>buf</b>, up to <b>count</b> bytes. If
4238 * <b>hProcess</b> is NULL, the function will return immediately if there is
4239 * nothing more to read. Otherwise <b>hProcess</b> should be set to the handle
4240 * to the process owning the <b>h</b>. In this case, the function will exit
4241 * only once the process has exited, or <b>count</b> bytes are read. Returns
4242 * the number of bytes read, or -1 on error. */
4243 ssize_t
4244 tor_read_all_handle(HANDLE h, char *buf, size_t count,
4245 const process_handle_t *process)
4247 size_t numread = 0;
4248 BOOL retval;
4249 DWORD byte_count;
4250 BOOL process_exited = FALSE;
4252 if (count > SIZE_T_CEILING || count > SSIZE_T_MAX)
4253 return -1;
4255 while (numread != count) {
4256 /* Check if there is anything to read */
4257 retval = PeekNamedPipe(h, NULL, 0, NULL, &byte_count, NULL);
4258 if (!retval) {
4259 log_warn(LD_GENERAL,
4260 "Failed to peek from handle: %s",
4261 format_win32_error(GetLastError()));
4262 return -1;
4263 } else if (0 == byte_count) {
4264 /* Nothing available: process exited or it is busy */
4266 /* Exit if we don't know whether the process is running */
4267 if (NULL == process)
4268 break;
4270 /* The process exited and there's nothing left to read from it */
4271 if (process_exited)
4272 break;
4274 /* If process is not running, check for output one more time in case
4275 it wrote something after the peek was performed. Otherwise keep on
4276 waiting for output */
4277 tor_assert(process != NULL);
4278 byte_count = WaitForSingleObject(process->pid.hProcess, 0);
4279 if (WAIT_TIMEOUT != byte_count)
4280 process_exited = TRUE;
4282 continue;
4285 /* There is data to read; read it */
4286 retval = ReadFile(h, buf+numread, count-numread, &byte_count, NULL);
4287 tor_assert(byte_count + numread <= count);
4288 if (!retval) {
4289 log_warn(LD_GENERAL, "Failed to read from handle: %s",
4290 format_win32_error(GetLastError()));
4291 return -1;
4292 } else if (0 == byte_count) {
4293 /* End of file */
4294 break;
4296 numread += byte_count;
4298 return (ssize_t)numread;
4300 #else
4301 /** Read from a handle <b>h</b> into <b>buf</b>, up to <b>count</b> bytes. If
4302 * <b>process</b> is NULL, the function will return immediately if there is
4303 * nothing more to read. Otherwise data will be read until end of file, or
4304 * <b>count</b> bytes are read. Returns the number of bytes read, or -1 on
4305 * error. Sets <b>eof</b> to true if <b>eof</b> is not NULL and the end of the
4306 * file has been reached. */
4307 ssize_t
4308 tor_read_all_handle(FILE *h, char *buf, size_t count,
4309 const process_handle_t *process,
4310 int *eof)
4312 size_t numread = 0;
4313 char *retval;
4315 if (eof)
4316 *eof = 0;
4318 if (count > SIZE_T_CEILING || count > SSIZE_T_MAX)
4319 return -1;
4321 while (numread != count) {
4322 /* Use fgets because that is what we use in log_from_pipe() */
4323 retval = fgets(buf+numread, (int)(count-numread), h);
4324 if (NULL == retval) {
4325 if (feof(h)) {
4326 log_debug(LD_GENERAL, "fgets() reached end of file");
4327 if (eof)
4328 *eof = 1;
4329 break;
4330 } else {
4331 if (EAGAIN == errno) {
4332 if (process)
4333 continue;
4334 else
4335 break;
4336 } else {
4337 log_warn(LD_GENERAL, "fgets() from handle failed: %s",
4338 strerror(errno));
4339 return -1;
4343 tor_assert(retval != NULL);
4344 tor_assert(strlen(retval) + numread <= count);
4345 numread += strlen(retval);
4348 log_debug(LD_GENERAL, "fgets() read %d bytes from handle", (int)numread);
4349 return (ssize_t)numread;
4351 #endif
4353 /** Read from stdout of a process until the process exits. */
4354 ssize_t
4355 tor_read_all_from_process_stdout(const process_handle_t *process_handle,
4356 char *buf, size_t count)
4358 #ifdef _WIN32
4359 return tor_read_all_handle(process_handle->stdout_pipe, buf, count,
4360 process_handle);
4361 #else
4362 return tor_read_all_handle(process_handle->stdout_handle, buf, count,
4363 process_handle, NULL);
4364 #endif
4367 /** Read from stdout of a process until the process exits. */
4368 ssize_t
4369 tor_read_all_from_process_stderr(const process_handle_t *process_handle,
4370 char *buf, size_t count)
4372 #ifdef _WIN32
4373 return tor_read_all_handle(process_handle->stderr_pipe, buf, count,
4374 process_handle);
4375 #else
4376 return tor_read_all_handle(process_handle->stderr_handle, buf, count,
4377 process_handle, NULL);
4378 #endif
4381 /** Split buf into lines, and add to smartlist. The buffer <b>buf</b> will be
4382 * modified. The resulting smartlist will consist of pointers to buf, so there
4383 * is no need to free the contents of sl. <b>buf</b> must be a NUL-terminated
4384 * string. <b>len</b> should be set to the length of the buffer excluding the
4385 * NUL. Non-printable characters (including NUL) will be replaced with "." */
4387 tor_split_lines(smartlist_t *sl, char *buf, int len)
4389 /* Index in buf of the start of the current line */
4390 int start = 0;
4391 /* Index in buf of the current character being processed */
4392 int cur = 0;
4393 /* Are we currently in a line */
4394 char in_line = 0;
4396 /* Loop over string */
4397 while (cur < len) {
4398 /* Loop until end of line or end of string */
4399 for (; cur < len; cur++) {
4400 if (in_line) {
4401 if ('\r' == buf[cur] || '\n' == buf[cur]) {
4402 /* End of line */
4403 buf[cur] = '\0';
4404 /* Point cur to the next line */
4405 cur++;
4406 /* Line starts at start and ends with a nul */
4407 break;
4408 } else {
4409 if (!TOR_ISPRINT(buf[cur]))
4410 buf[cur] = '.';
4412 } else {
4413 if ('\r' == buf[cur] || '\n' == buf[cur]) {
4414 /* Skip leading vertical space */
4416 } else {
4417 in_line = 1;
4418 start = cur;
4419 if (!TOR_ISPRINT(buf[cur]))
4420 buf[cur] = '.';
4424 /* We are at the end of the line or end of string. If in_line is true there
4425 * is a line which starts at buf+start and ends at a NUL. cur points to
4426 * the character after the NUL. */
4427 if (in_line)
4428 smartlist_add(sl, (void *)(buf+start));
4429 in_line = 0;
4431 return smartlist_len(sl);
4434 /** Return a string corresponding to <b>stream_status</b>. */
4435 const char *
4436 stream_status_to_string(enum stream_status stream_status)
4438 switch (stream_status) {
4439 case IO_STREAM_OKAY:
4440 return "okay";
4441 case IO_STREAM_EAGAIN:
4442 return "temporarily unavailable";
4443 case IO_STREAM_TERM:
4444 return "terminated";
4445 case IO_STREAM_CLOSED:
4446 return "closed";
4447 default:
4448 tor_fragile_assert();
4449 return "unknown";
4453 #ifdef _WIN32
4455 /** Return a smartlist containing lines outputted from
4456 * <b>handle</b>. Return NULL on error, and set
4457 * <b>stream_status_out</b> appropriately. */
4458 smartlist_t *
4459 tor_get_lines_from_handle(HANDLE *handle,
4460 enum stream_status *stream_status_out)
4462 int pos;
4463 char stdout_buf[600] = {0};
4464 smartlist_t *lines = NULL;
4466 tor_assert(stream_status_out);
4468 *stream_status_out = IO_STREAM_TERM;
4470 pos = tor_read_all_handle(handle, stdout_buf, sizeof(stdout_buf) - 1, NULL);
4471 if (pos < 0) {
4472 *stream_status_out = IO_STREAM_TERM;
4473 return NULL;
4475 if (pos == 0) {
4476 *stream_status_out = IO_STREAM_EAGAIN;
4477 return NULL;
4480 /* End with a null even if there isn't a \r\n at the end */
4481 /* TODO: What if this is a partial line? */
4482 stdout_buf[pos] = '\0';
4484 /* Split up the buffer */
4485 lines = smartlist_new();
4486 tor_split_lines(lines, stdout_buf, pos);
4488 /* Currently 'lines' is populated with strings residing on the
4489 stack. Replace them with their exact copies on the heap: */
4490 SMARTLIST_FOREACH(lines, char *, line,
4491 SMARTLIST_REPLACE_CURRENT(lines, line, tor_strdup(line)));
4493 *stream_status_out = IO_STREAM_OKAY;
4495 return lines;
4498 /** Read from stream, and send lines to log at the specified log level.
4499 * Returns -1 if there is a error reading, and 0 otherwise.
4500 * If the generated stream is flushed more often than on new lines, or
4501 * a read exceeds 256 bytes, lines will be truncated. This should be fixed,
4502 * along with the corresponding problem on *nix (see bug #2045).
4504 static int
4505 log_from_handle(HANDLE *pipe, int severity)
4507 char buf[256];
4508 int pos;
4509 smartlist_t *lines;
4511 pos = tor_read_all_handle(pipe, buf, sizeof(buf) - 1, NULL);
4512 if (pos < 0) {
4513 /* Error */
4514 log_warn(LD_GENERAL, "Failed to read data from subprocess");
4515 return -1;
4518 if (0 == pos) {
4519 /* There's nothing to read (process is busy or has exited) */
4520 log_debug(LD_GENERAL, "Subprocess had nothing to say");
4521 return 0;
4524 /* End with a null even if there isn't a \r\n at the end */
4525 /* TODO: What if this is a partial line? */
4526 buf[pos] = '\0';
4527 log_debug(LD_GENERAL, "Subprocess had %d bytes to say", pos);
4529 /* Split up the buffer */
4530 lines = smartlist_new();
4531 tor_split_lines(lines, buf, pos);
4533 /* Log each line */
4534 SMARTLIST_FOREACH(lines, char *, line,
4536 log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", line);
4538 smartlist_free(lines);
4540 return 0;
4543 #else
4545 /** Return a smartlist containing lines outputted from
4546 * <b>handle</b>. Return NULL on error, and set
4547 * <b>stream_status_out</b> appropriately. */
4548 smartlist_t *
4549 tor_get_lines_from_handle(FILE *handle, enum stream_status *stream_status_out)
4551 enum stream_status stream_status;
4552 char stdout_buf[400];
4553 smartlist_t *lines = NULL;
4555 while (1) {
4556 memset(stdout_buf, 0, sizeof(stdout_buf));
4558 stream_status = get_string_from_pipe(handle,
4559 stdout_buf, sizeof(stdout_buf) - 1);
4560 if (stream_status != IO_STREAM_OKAY)
4561 goto done;
4563 if (!lines) lines = smartlist_new();
4564 smartlist_add(lines, tor_strdup(stdout_buf));
4567 done:
4568 *stream_status_out = stream_status;
4569 return lines;
4572 /** Read from stream, and send lines to log at the specified log level.
4573 * Returns 1 if stream is closed normally, -1 if there is a error reading, and
4574 * 0 otherwise. Handles lines from tor-fw-helper and
4575 * tor_spawn_background() specially.
4577 static int
4578 log_from_pipe(FILE *stream, int severity, const char *executable,
4579 int *child_status)
4581 char buf[256];
4582 enum stream_status r;
4584 for (;;) {
4585 r = get_string_from_pipe(stream, buf, sizeof(buf) - 1);
4587 if (r == IO_STREAM_CLOSED) {
4588 return 1;
4589 } else if (r == IO_STREAM_EAGAIN) {
4590 return 0;
4591 } else if (r == IO_STREAM_TERM) {
4592 return -1;
4595 tor_assert(r == IO_STREAM_OKAY);
4597 /* Check if buf starts with SPAWN_ERROR_MESSAGE */
4598 if (strcmpstart(buf, SPAWN_ERROR_MESSAGE) == 0) {
4599 /* Parse error message */
4600 int retval, child_state, saved_errno;
4601 retval = tor_sscanf(buf, SPAWN_ERROR_MESSAGE "%x/%x",
4602 &child_state, &saved_errno);
4603 if (retval == 2) {
4604 log_warn(LD_GENERAL,
4605 "Failed to start child process \"%s\" in state %d: %s",
4606 executable, child_state, strerror(saved_errno));
4607 if (child_status)
4608 *child_status = 1;
4609 } else {
4610 /* Failed to parse message from child process, log it as a
4611 warning */
4612 log_warn(LD_GENERAL,
4613 "Unexpected message from port forwarding helper \"%s\": %s",
4614 executable, buf);
4616 } else {
4617 log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", buf);
4621 /* We should never get here */
4622 return -1;
4624 #endif
4626 /** Reads from <b>stream</b> and stores input in <b>buf_out</b> making
4627 * sure it's below <b>count</b> bytes.
4628 * If the string has a trailing newline, we strip it off.
4630 * This function is specifically created to handle input from managed
4631 * proxies, according to the pluggable transports spec. Make sure it
4632 * fits your needs before using it.
4634 * Returns:
4635 * IO_STREAM_CLOSED: If the stream is closed.
4636 * IO_STREAM_EAGAIN: If there is nothing to read and we should check back
4637 * later.
4638 * IO_STREAM_TERM: If something is wrong with the stream.
4639 * IO_STREAM_OKAY: If everything went okay and we got a string
4640 * in <b>buf_out</b>. */
4641 enum stream_status
4642 get_string_from_pipe(FILE *stream, char *buf_out, size_t count)
4644 char *retval;
4645 size_t len;
4647 tor_assert(count <= INT_MAX);
4649 retval = fgets(buf_out, (int)count, stream);
4651 if (!retval) {
4652 if (feof(stream)) {
4653 /* Program has closed stream (probably it exited) */
4654 /* TODO: check error */
4655 return IO_STREAM_CLOSED;
4656 } else {
4657 if (EAGAIN == errno) {
4658 /* Nothing more to read, try again next time */
4659 return IO_STREAM_EAGAIN;
4660 } else {
4661 /* There was a problem, abandon this child process */
4662 return IO_STREAM_TERM;
4665 } else {
4666 len = strlen(buf_out);
4667 if (len == 0) {
4668 /* this probably means we got a NUL at the start of the string. */
4669 return IO_STREAM_EAGAIN;
4672 if (buf_out[len - 1] == '\n') {
4673 /* Remove the trailing newline */
4674 buf_out[len - 1] = '\0';
4675 } else {
4676 /* No newline; check whether we overflowed the buffer */
4677 if (!feof(stream))
4678 log_info(LD_GENERAL,
4679 "Line from stream was truncated: %s", buf_out);
4680 /* TODO: What to do with this error? */
4683 return IO_STREAM_OKAY;
4686 /* We should never get here */
4687 return IO_STREAM_TERM;
4690 /** Parse a <b>line</b> from tor-fw-helper and issue an appropriate
4691 * log message to our user. */
4692 static void
4693 handle_fw_helper_line(const char *line)
4695 smartlist_t *tokens = smartlist_new();
4696 char *message = NULL;
4697 char *message_for_log = NULL;
4698 const char *external_port = NULL;
4699 const char *internal_port = NULL;
4700 const char *result = NULL;
4701 int port = 0;
4702 int success = 0;
4704 smartlist_split_string(tokens, line, NULL,
4705 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
4707 if (smartlist_len(tokens) < 5)
4708 goto err;
4710 if (strcmp(smartlist_get(tokens, 0), "tor-fw-helper") ||
4711 strcmp(smartlist_get(tokens, 1), "tcp-forward"))
4712 goto err;
4714 external_port = smartlist_get(tokens, 2);
4715 internal_port = smartlist_get(tokens, 3);
4716 result = smartlist_get(tokens, 4);
4718 if (smartlist_len(tokens) > 5) {
4719 /* If there are more than 5 tokens, they are part of [<message>].
4720 Let's use a second smartlist to form the whole message;
4721 strncat loops suck. */
4722 int i;
4723 int message_words_n = smartlist_len(tokens) - 5;
4724 smartlist_t *message_sl = smartlist_new();
4725 for (i = 0; i < message_words_n; i++)
4726 smartlist_add(message_sl, smartlist_get(tokens, 5+i));
4728 tor_assert(smartlist_len(message_sl) > 0);
4729 message = smartlist_join_strings(message_sl, " ", 0, NULL);
4731 /* wrap the message in log-friendly wrapping */
4732 tor_asprintf(&message_for_log, " ('%s')", message);
4734 smartlist_free(message_sl);
4737 port = atoi(external_port);
4738 if (port < 1 || port > 65535)
4739 goto err;
4741 port = atoi(internal_port);
4742 if (port < 1 || port > 65535)
4743 goto err;
4745 if (!strcmp(result, "SUCCESS"))
4746 success = 1;
4747 else if (!strcmp(result, "FAIL"))
4748 success = 0;
4749 else
4750 goto err;
4752 if (!success) {
4753 log_warn(LD_GENERAL, "Tor was unable to forward TCP port '%s' to '%s'%s. "
4754 "Please make sure that your router supports port "
4755 "forwarding protocols (like NAT-PMP). Note that if '%s' is "
4756 "your ORPort, your relay will be unable to receive inbound "
4757 "traffic.", external_port, internal_port,
4758 message_for_log ? message_for_log : "",
4759 internal_port);
4760 } else {
4761 log_info(LD_GENERAL,
4762 "Tor successfully forwarded TCP port '%s' to '%s'%s.",
4763 external_port, internal_port,
4764 message_for_log ? message_for_log : "");
4767 goto done;
4769 err:
4770 log_warn(LD_GENERAL, "tor-fw-helper sent us a string we could not "
4771 "parse (%s).", line);
4773 done:
4774 SMARTLIST_FOREACH(tokens, char *, cp, tor_free(cp));
4775 smartlist_free(tokens);
4776 tor_free(message);
4777 tor_free(message_for_log);
4780 /** Read what tor-fw-helper has to say in its stdout and handle it
4781 * appropriately */
4782 static int
4783 handle_fw_helper_output(process_handle_t *process_handle)
4785 smartlist_t *fw_helper_output = NULL;
4786 enum stream_status stream_status = 0;
4788 fw_helper_output =
4789 tor_get_lines_from_handle(tor_process_get_stdout_pipe(process_handle),
4790 &stream_status);
4791 if (!fw_helper_output) { /* didn't get any output from tor-fw-helper */
4792 /* if EAGAIN we should retry in the future */
4793 return (stream_status == IO_STREAM_EAGAIN) ? 0 : -1;
4796 /* Handle the lines we got: */
4797 SMARTLIST_FOREACH_BEGIN(fw_helper_output, char *, line) {
4798 handle_fw_helper_line(line);
4799 tor_free(line);
4800 } SMARTLIST_FOREACH_END(line);
4802 smartlist_free(fw_helper_output);
4804 return 0;
4807 /** Spawn tor-fw-helper and ask it to forward the ports in
4808 * <b>ports_to_forward</b>. <b>ports_to_forward</b> contains strings
4809 * of the form "<external port>:<internal port>", which is the format
4810 * that tor-fw-helper expects. */
4811 void
4812 tor_check_port_forwarding(const char *filename,
4813 smartlist_t *ports_to_forward,
4814 time_t now)
4816 /* When fw-helper succeeds, how long do we wait until running it again */
4817 #define TIME_TO_EXEC_FWHELPER_SUCCESS 300
4818 /* When fw-helper failed to start, how long do we wait until running it again
4820 #define TIME_TO_EXEC_FWHELPER_FAIL 60
4822 /* Static variables are initialized to zero, so child_handle.status=0
4823 * which corresponds to it not running on startup */
4824 static process_handle_t *child_handle=NULL;
4826 static time_t time_to_run_helper = 0;
4827 int stderr_status, retval;
4828 int stdout_status = 0;
4830 tor_assert(filename);
4832 /* Start the child, if it is not already running */
4833 if ((!child_handle || child_handle->status != PROCESS_STATUS_RUNNING) &&
4834 time_to_run_helper < now) {
4835 /*tor-fw-helper cli looks like this: tor_fw_helper -p :5555 -p 4555:1111 */
4836 const char **argv; /* cli arguments */
4837 int args_n, status;
4838 int argv_index = 0; /* index inside 'argv' */
4840 tor_assert(smartlist_len(ports_to_forward) > 0);
4842 /* check for overflow during 'argv' allocation:
4843 (len(ports_to_forward)*2 + 2)*sizeof(char*) > SIZE_MAX ==
4844 len(ports_to_forward) > (((SIZE_MAX/sizeof(char*)) - 2)/2) */
4845 if ((size_t) smartlist_len(ports_to_forward) >
4846 (((SIZE_MAX/sizeof(char*)) - 2)/2)) {
4847 log_warn(LD_GENERAL,
4848 "Overflow during argv allocation. This shouldn't happen.");
4849 return;
4851 /* check for overflow during 'argv_index' increase:
4852 ((len(ports_to_forward)*2 + 2) > INT_MAX) ==
4853 len(ports_to_forward) > (INT_MAX - 2)/2 */
4854 if (smartlist_len(ports_to_forward) > (INT_MAX - 2)/2) {
4855 log_warn(LD_GENERAL,
4856 "Overflow during argv_index increase. This shouldn't happen.");
4857 return;
4860 /* Calculate number of cli arguments: one for the filename, two
4861 for each smartlist element (one for "-p" and one for the
4862 ports), and one for the final NULL. */
4863 args_n = 1 + 2*smartlist_len(ports_to_forward) + 1;
4864 argv = tor_malloc_zero(sizeof(char*)*args_n);
4866 argv[argv_index++] = filename;
4867 SMARTLIST_FOREACH_BEGIN(ports_to_forward, const char *, port) {
4868 argv[argv_index++] = "-p";
4869 argv[argv_index++] = port;
4870 } SMARTLIST_FOREACH_END(port);
4871 argv[argv_index] = NULL;
4873 /* Assume tor-fw-helper will succeed, start it later*/
4874 time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_SUCCESS;
4876 if (child_handle) {
4877 tor_process_handle_destroy(child_handle, 1);
4878 child_handle = NULL;
4881 #ifdef _WIN32
4882 /* Passing NULL as lpApplicationName makes Windows search for the .exe */
4883 status = tor_spawn_background(NULL, argv, NULL, &child_handle);
4884 #else
4885 status = tor_spawn_background(filename, argv, NULL, &child_handle);
4886 #endif
4888 tor_free_((void*)argv);
4889 argv=NULL;
4891 if (PROCESS_STATUS_ERROR == status) {
4892 log_warn(LD_GENERAL, "Failed to start port forwarding helper %s",
4893 filename);
4894 time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_FAIL;
4895 return;
4898 log_info(LD_GENERAL,
4899 "Started port forwarding helper (%s) with pid '%d'",
4900 filename, tor_process_get_pid(child_handle));
4903 /* If child is running, read from its stdout and stderr) */
4904 if (child_handle && PROCESS_STATUS_RUNNING == child_handle->status) {
4905 /* Read from stdout/stderr and log result */
4906 retval = 0;
4907 #ifdef _WIN32
4908 stderr_status = log_from_handle(child_handle->stderr_pipe, LOG_INFO);
4909 #else
4910 stderr_status = log_from_pipe(child_handle->stderr_handle,
4911 LOG_INFO, filename, &retval);
4912 #endif
4913 if (handle_fw_helper_output(child_handle) < 0) {
4914 log_warn(LD_GENERAL, "Failed to handle fw helper output.");
4915 stdout_status = -1;
4916 retval = -1;
4919 if (retval) {
4920 /* There was a problem in the child process */
4921 time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_FAIL;
4924 /* Combine the two statuses in order of severity */
4925 if (-1 == stdout_status || -1 == stderr_status)
4926 /* There was a failure */
4927 retval = -1;
4928 #ifdef _WIN32
4929 else if (!child_handle || tor_get_exit_code(child_handle, 0, NULL) !=
4930 PROCESS_EXIT_RUNNING) {
4931 /* process has exited or there was an error */
4932 /* TODO: Do something with the process return value */
4933 /* TODO: What if the process output something since
4934 * between log_from_handle and tor_get_exit_code? */
4935 retval = 1;
4937 #else
4938 else if (1 == stdout_status || 1 == stderr_status)
4939 /* stdout or stderr was closed, the process probably
4940 * exited. It will be reaped by waitpid() in main.c */
4941 /* TODO: Do something with the process return value */
4942 retval = 1;
4943 #endif
4944 else
4945 /* Both are fine */
4946 retval = 0;
4948 /* If either pipe indicates a failure, act on it */
4949 if (0 != retval) {
4950 if (1 == retval) {
4951 log_info(LD_GENERAL, "Port forwarding helper terminated");
4952 child_handle->status = PROCESS_STATUS_NOTRUNNING;
4953 } else {
4954 log_warn(LD_GENERAL, "Failed to read from port forwarding helper");
4955 child_handle->status = PROCESS_STATUS_ERROR;
4958 /* TODO: The child might not actually be finished (maybe it failed or
4959 closed stdout/stderr), so maybe we shouldn't start another? */
4964 /** Initialize the insecure RNG <b>rng</b> from a seed value <b>seed</b>. */
4965 void
4966 tor_init_weak_random(tor_weak_rng_t *rng, unsigned seed)
4968 rng->state = (uint32_t)(seed & 0x7fffffff);
4971 /** Return a randomly chosen value in the range 0..TOR_WEAK_RANDOM_MAX based
4972 * on the RNG state of <b>rng</b>. This entropy will not be cryptographically
4973 * strong; do not rely on it for anything an adversary should not be able to
4974 * predict. */
4975 int32_t
4976 tor_weak_random(tor_weak_rng_t *rng)
4978 /* Here's a linear congruential generator. OpenBSD and glibc use these
4979 * parameters; they aren't too bad, and should have maximal period over the
4980 * range 0..INT32_MAX. We don't want to use the platform rand() or random(),
4981 * since some platforms have bad weak RNGs that only return values in the
4982 * range 0..INT16_MAX, which just isn't enough. */
4983 rng->state = (rng->state * 1103515245 + 12345) & 0x7fffffff;
4984 return (int32_t) rng->state;
4987 /** Return a random number in the range [0 , <b>top</b>). {That is, the range
4988 * of integers i such that 0 <= i < top.} Chooses uniformly. Requires that
4989 * top is greater than 0. This randomness is not cryptographically strong; do
4990 * not rely on it for anything an adversary should not be able to predict. */
4991 int32_t
4992 tor_weak_random_range(tor_weak_rng_t *rng, int32_t top)
4994 /* We don't want to just do tor_weak_random() % top, since random() is often
4995 * implemented with an LCG whose modulus is a power of 2, and those are
4996 * cyclic in their low-order bits. */
4997 int divisor, result;
4998 tor_assert(top > 0);
4999 divisor = TOR_WEAK_RANDOM_MAX / top;
5000 do {
5001 result = (int32_t)(tor_weak_random(rng) / divisor);
5002 } while (result >= top);
5003 return result;