Have all of our allocation functions and a few others check for underflow
[tor.git] / src / common / util.c
blob0571a532cda19ba193a3e48fae8078fc9e1490ae
1 /* Copyright (c) 2003, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2010, 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 #include "util.h"
18 #include "log.h"
19 #include "crypto.h"
20 #include "torint.h"
21 #include "container.h"
22 #include "address.h"
24 #ifdef MS_WINDOWS
25 #include <io.h>
26 #include <direct.h>
27 #include <process.h>
28 #else
29 #include <dirent.h>
30 #include <pwd.h>
31 #endif
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <assert.h>
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
40 #endif
41 #ifdef HAVE_ARPA_INET_H
42 #include <arpa/inet.h>
43 #endif
44 #ifdef HAVE_ERRNO_H
45 #include <errno.h>
46 #endif
47 #ifdef HAVE_SYS_SOCKET_H
48 #include <sys/socket.h>
49 #endif
50 #ifdef HAVE_SYS_TIME_H
51 #include <sys/time.h>
52 #endif
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif
56 #ifdef HAVE_SYS_STAT_H
57 #include <sys/stat.h>
58 #endif
59 #ifdef HAVE_SYS_FCNTL_H
60 #include <sys/fcntl.h>
61 #endif
62 #ifdef HAVE_FCNTL_H
63 #include <fcntl.h>
64 #endif
65 #ifdef HAVE_TIME_H
66 #include <time.h>
67 #endif
68 #ifdef HAVE_MALLOC_MALLOC_H
69 #include <malloc/malloc.h>
70 #endif
71 #ifdef HAVE_MALLOC_H
72 #ifndef OPENBSD
73 /* OpenBSD has a malloc.h, but for our purposes, it only exists in order to
74 * scold us for being so stupid as to autodetect its presence. To be fair,
75 * they've done this since 1996, when autoconf was only 5 years old. */
76 #include <malloc.h>
77 #endif
78 #endif
79 #ifdef HAVE_MALLOC_NP_H
80 #include <malloc_np.h>
81 #endif
83 /* =====
84 * Memory management
85 * ===== */
86 #ifdef USE_DMALLOC
87 #undef strndup
88 #include <dmalloc.h>
89 /* Macro to pass the extra dmalloc args to another function. */
90 #define DMALLOC_FN_ARGS , file, line
92 #if defined(HAVE_DMALLOC_STRDUP)
93 /* the dmalloc_strdup should be fine as defined */
94 #elif defined(HAVE_DMALLOC_STRNDUP)
95 #define dmalloc_strdup(file, line, string, xalloc_b) \
96 dmalloc_strndup(file, line, (string), -1, xalloc_b)
97 #else
98 #error "No dmalloc_strdup or equivalent"
99 #endif
101 #else /* not using dmalloc */
103 #define DMALLOC_FN_ARGS
104 #endif
106 /** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to
107 * result. On error, log and terminate the process. (Same as malloc(size),
108 * but never returns NULL.)
110 * <b>file</b> and <b>line</b> are used if dmalloc is enabled, and
111 * ignored otherwise.
113 void *
114 _tor_malloc(size_t size DMALLOC_PARAMS)
116 void *result;
118 tor_assert(size < SIZE_T_CEILING);
120 #ifndef MALLOC_ZERO_WORKS
121 /* Some libc mallocs don't work when size==0. Override them. */
122 if (size==0) {
123 size=1;
125 #endif
127 #ifdef USE_DMALLOC
128 result = dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
129 #else
130 result = malloc(size);
131 #endif
133 if (PREDICT_UNLIKELY(result == NULL)) {
134 log_err(LD_MM,"Out of memory on malloc(). Dying.");
135 /* If these functions die within a worker process, they won't call
136 * spawn_exit, but that's ok, since the parent will run out of memory soon
137 * anyway. */
138 exit(1);
140 return result;
143 /** Allocate a chunk of <b>size</b> bytes of memory, fill the memory with
144 * zero bytes, and return a pointer to the result. Log and terminate
145 * the process on error. (Same as calloc(size,1), but never returns NULL.)
147 void *
148 _tor_malloc_zero(size_t size DMALLOC_PARAMS)
150 /* You may ask yourself, "wouldn't it be smart to use calloc instead of
151 * malloc+memset? Perhaps libc's calloc knows some nifty optimization trick
152 * we don't!" Indeed it does, but its optimizations are only a big win when
153 * we're allocating something very big (it knows if it just got the memory
154 * from the OS in a pre-zeroed state). We don't want to use tor_malloc_zero
155 * for big stuff, so we don't bother with calloc. */
156 void *result = _tor_malloc(size DMALLOC_FN_ARGS);
157 memset(result, 0, size);
158 return result;
161 /** Change the size of the memory block pointed to by <b>ptr</b> to <b>size</b>
162 * bytes long; return the new memory block. On error, log and
163 * terminate. (Like realloc(ptr,size), but never returns NULL.)
165 void *
166 _tor_realloc(void *ptr, size_t size DMALLOC_PARAMS)
168 void *result;
170 #ifdef USE_DMALLOC
171 result = dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
172 #else
173 result = realloc(ptr, size);
174 #endif
176 if (PREDICT_UNLIKELY(result == NULL)) {
177 log_err(LD_MM,"Out of memory on realloc(). Dying.");
178 exit(1);
180 return result;
183 /** Return a newly allocated copy of the NUL-terminated string s. On
184 * error, log and terminate. (Like strdup(s), but never returns
185 * NULL.)
187 char *
188 _tor_strdup(const char *s DMALLOC_PARAMS)
190 char *dup;
191 tor_assert(s);
193 #ifdef USE_DMALLOC
194 dup = dmalloc_strdup(file, line, s, 0);
195 #else
196 dup = strdup(s);
197 #endif
198 if (PREDICT_UNLIKELY(dup == NULL)) {
199 log_err(LD_MM,"Out of memory on strdup(). Dying.");
200 exit(1);
202 return dup;
205 /** Allocate and return a new string containing the first <b>n</b>
206 * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b>
207 * characters, only the first <b>n</b> are copied. The result is
208 * always NUL-terminated. (Like strndup(s,n), but never returns
209 * NULL.)
211 char *
212 _tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
214 char *dup;
215 tor_assert(s);
216 tor_assert(n < SIZE_T_CEILING);
217 dup = _tor_malloc((n+1) DMALLOC_FN_ARGS);
218 /* Performance note: Ordinarily we prefer strlcpy to strncpy. But
219 * this function gets called a whole lot, and platform strncpy is
220 * much faster than strlcpy when strlen(s) is much longer than n.
222 strncpy(dup, s, n);
223 dup[n]='\0';
224 return dup;
227 /** Allocate a chunk of <b>len</b> bytes, with the same contents as the
228 * <b>len</b> bytes starting at <b>mem</b>. */
229 void *
230 _tor_memdup(const void *mem, size_t len DMALLOC_PARAMS)
232 char *dup;
233 tor_assert(len < SIZE_T_CEILING);
234 tor_assert(mem);
235 dup = _tor_malloc(len DMALLOC_FN_ARGS);
236 memcpy(dup, mem, len);
237 return dup;
240 /** Helper for places that need to take a function pointer to the right
241 * spelling of "free()". */
242 void
243 _tor_free(void *mem)
245 tor_free(mem);
248 #if defined(HAVE_MALLOC_GOOD_SIZE) && !defined(HAVE_MALLOC_GOOD_SIZE_PROTOTYPE)
249 /* Some version of Mac OSX have malloc_good_size in their libc, but not
250 * actually defined in malloc/malloc.h. We detect this and work around it by
251 * prototyping.
253 extern size_t malloc_good_size(size_t size);
254 #endif
256 /** Allocate and return a chunk of memory of size at least *<b>size</b>, using
257 * the same resources we would use to malloc *<b>sizep</b>. Set *<b>sizep</b>
258 * to the number of usable bytes in the chunk of memory. */
259 void *
260 _tor_malloc_roundup(size_t *sizep DMALLOC_PARAMS)
262 #ifdef HAVE_MALLOC_GOOD_SIZE
263 tor_assert(*sizep < SIZE_T_CEILING);
264 *sizep = malloc_good_size(*sizep);
265 return _tor_malloc(*sizep DMALLOC_FN_ARGS);
266 #elif 0 && defined(HAVE_MALLOC_USABLE_SIZE) && !defined(USE_DMALLOC)
267 /* Never use malloc_usable_size(); it makes valgrind really unhappy,
268 * and doesn't win much in terms of usable space where it exists. */
269 void *result;
270 tor_assert(*sizep < SIZE_T_CEILING);
271 result = _tor_malloc(*sizep DMALLOC_FN_ARGS);
272 *sizep = malloc_usable_size(result);
273 return result;
274 #else
275 return _tor_malloc(*sizep DMALLOC_FN_ARGS);
276 #endif
279 /** Call the platform malloc info function, and dump the results to the log at
280 * level <b>severity</b>. If no such function exists, do nothing. */
281 void
282 tor_log_mallinfo(int severity)
284 #ifdef HAVE_MALLINFO
285 struct mallinfo mi;
286 memset(&mi, 0, sizeof(mi));
287 mi = mallinfo();
288 log(severity, LD_MM,
289 "mallinfo() said: arena=%d, ordblks=%d, smblks=%d, hblks=%d, "
290 "hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, "
291 "keepcost=%d",
292 mi.arena, mi.ordblks, mi.smblks, mi.hblks,
293 mi.hblkhd, mi.usmblks, mi.fsmblks, mi.uordblks, mi.fordblks,
294 mi.keepcost);
295 #else
296 (void)severity;
297 #endif
298 #ifdef USE_DMALLOC
299 dmalloc_log_changed(0, /* Since the program started. */
300 1, /* Log info about non-freed pointers. */
301 0, /* Do not log info about freed pointers. */
302 0 /* Do not log individual pointers. */
304 #endif
307 /* =====
308 * Math
309 * ===== */
311 /** Returns floor(log2(u64)). If u64 is 0, (incorrectly) returns 0. */
313 tor_log2(uint64_t u64)
315 int r = 0;
316 if (u64 >= (U64_LITERAL(1)<<32)) {
317 u64 >>= 32;
318 r = 32;
320 if (u64 >= (U64_LITERAL(1)<<16)) {
321 u64 >>= 16;
322 r += 16;
324 if (u64 >= (U64_LITERAL(1)<<8)) {
325 u64 >>= 8;
326 r += 8;
328 if (u64 >= (U64_LITERAL(1)<<4)) {
329 u64 >>= 4;
330 r += 4;
332 if (u64 >= (U64_LITERAL(1)<<2)) {
333 u64 >>= 2;
334 r += 2;
336 if (u64 >= (U64_LITERAL(1)<<1)) {
337 u64 >>= 1;
338 r += 1;
340 return r;
343 /** Return the power of 2 closest to <b>u64</b>. */
344 uint64_t
345 round_to_power_of_2(uint64_t u64)
347 int lg2 = tor_log2(u64);
348 uint64_t low = U64_LITERAL(1) << lg2, high = U64_LITERAL(1) << (lg2+1);
349 if (high - u64 < u64 - low)
350 return high;
351 else
352 return low;
355 /* =====
356 * String manipulation
357 * ===== */
359 /** Remove from the string <b>s</b> every character which appears in
360 * <b>strip</b>. */
361 void
362 tor_strstrip(char *s, const char *strip)
364 char *read = s;
365 while (*read) {
366 if (strchr(strip, *read)) {
367 ++read;
368 } else {
369 *s++ = *read++;
372 *s = '\0';
375 /** Return a pointer to a NUL-terminated hexadecimal string encoding
376 * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
377 * result does not need to be deallocated, but repeated calls to
378 * hex_str will trash old results.
380 const char *
381 hex_str(const char *from, size_t fromlen)
383 static char buf[65];
384 if (fromlen>(sizeof(buf)-1)/2)
385 fromlen = (sizeof(buf)-1)/2;
386 base16_encode(buf,sizeof(buf),from,fromlen);
387 return buf;
390 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
391 * lowercase. */
392 void
393 tor_strlower(char *s)
395 while (*s) {
396 *s = TOR_TOLOWER(*s);
397 ++s;
401 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
402 * lowercase. */
403 void
404 tor_strupper(char *s)
406 while (*s) {
407 *s = TOR_TOUPPER(*s);
408 ++s;
412 /** Return 1 if every character in <b>s</b> is printable, else return 0.
415 tor_strisprint(const char *s)
417 while (*s) {
418 if (!TOR_ISPRINT(*s))
419 return 0;
420 s++;
422 return 1;
425 /** Return 1 if no character in <b>s</b> is uppercase, else return 0.
428 tor_strisnonupper(const char *s)
430 while (*s) {
431 if (TOR_ISUPPER(*s))
432 return 0;
433 s++;
435 return 1;
438 /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
439 * strcmp.
442 strcmpstart(const char *s1, const char *s2)
444 size_t n = strlen(s2);
445 return strncmp(s1, s2, n);
448 /** Compare the s1_len-byte string <b>s1</b> with <b>s2</b>,
449 * without depending on a terminating nul in s1. Sorting order is first by
450 * length, then lexically; return values are as for strcmp.
453 strcmp_len(const char *s1, const char *s2, size_t s1_len)
455 size_t s2_len = strlen(s2);
456 if (s1_len < s2_len)
457 return -1;
458 if (s1_len > s2_len)
459 return 1;
460 return memcmp(s1, s2, s2_len);
463 /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
464 * strcasecmp.
467 strcasecmpstart(const char *s1, const char *s2)
469 size_t n = strlen(s2);
470 return strncasecmp(s1, s2, n);
473 /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
474 * strcmp.
477 strcmpend(const char *s1, const char *s2)
479 size_t n1 = strlen(s1), n2 = strlen(s2);
480 if (n2>n1)
481 return strcmp(s1,s2);
482 else
483 return strncmp(s1+(n1-n2), s2, n2);
486 /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
487 * strcasecmp.
490 strcasecmpend(const char *s1, const char *s2)
492 size_t n1 = strlen(s1), n2 = strlen(s2);
493 if (n2>n1) /* then they can't be the same; figure out which is bigger */
494 return strcasecmp(s1,s2);
495 else
496 return strncasecmp(s1+(n1-n2), s2, n2);
499 /** Compare the value of the string <b>prefix</b> with the start of the
500 * <b>memlen</b>-byte memory chunk at <b>mem</b>. Return as for strcmp.
502 * [As memcmp(mem, prefix, strlen(prefix)) but returns -1 if memlen is less
503 * than strlen(prefix).]
506 memcmpstart(const void *mem, size_t memlen,
507 const char *prefix)
509 size_t plen = strlen(prefix);
510 if (memlen < plen)
511 return -1;
512 return memcmp(mem, prefix, plen);
515 /** Return a pointer to the first char of s that is not whitespace and
516 * not a comment, or to the terminating NUL if no such character exists.
518 const char *
519 eat_whitespace(const char *s)
521 tor_assert(s);
523 while (1) {
524 switch (*s) {
525 case '\0':
526 default:
527 return s;
528 case ' ':
529 case '\t':
530 case '\n':
531 case '\r':
532 ++s;
533 break;
534 case '#':
535 ++s;
536 while (*s && *s != '\n')
537 ++s;
542 /** Return a pointer to the first char of s that is not whitespace and
543 * not a comment, or to the terminating NUL if no such character exists.
545 const char *
546 eat_whitespace_eos(const char *s, const char *eos)
548 tor_assert(s);
549 tor_assert(eos && s <= eos);
551 while (s < eos) {
552 switch (*s) {
553 case '\0':
554 default:
555 return s;
556 case ' ':
557 case '\t':
558 case '\n':
559 case '\r':
560 ++s;
561 break;
562 case '#':
563 ++s;
564 while (s < eos && *s && *s != '\n')
565 ++s;
568 return s;
571 /** Return a pointer to the first char of s that is not a space or a tab
572 * or a \\r, or to the terminating NUL if no such character exists. */
573 const char *
574 eat_whitespace_no_nl(const char *s)
576 while (*s == ' ' || *s == '\t' || *s == '\r')
577 ++s;
578 return s;
581 /** As eat_whitespace_no_nl, but stop at <b>eos</b> whether we have
582 * found a non-whitespace character or not. */
583 const char *
584 eat_whitespace_eos_no_nl(const char *s, const char *eos)
586 while (s < eos && (*s == ' ' || *s == '\t' || *s == '\r'))
587 ++s;
588 return s;
591 /** Return a pointer to the first char of s that is whitespace or <b>#</b>,
592 * or to the terminating NUL if no such character exists.
594 const char *
595 find_whitespace(const char *s)
597 /* tor_assert(s); */
598 while (1) {
599 switch (*s)
601 case '\0':
602 case '#':
603 case ' ':
604 case '\r':
605 case '\n':
606 case '\t':
607 return s;
608 default:
609 ++s;
614 /** As find_whitespace, but stop at <b>eos</b> whether we have found a
615 * whitespace or not. */
616 const char *
617 find_whitespace_eos(const char *s, const char *eos)
619 /* tor_assert(s); */
620 while (s < eos) {
621 switch (*s)
623 case '\0':
624 case '#':
625 case ' ':
626 case '\r':
627 case '\n':
628 case '\t':
629 return s;
630 default:
631 ++s;
634 return s;
637 /** Return true iff the 'len' bytes at 'mem' are all zero. */
639 tor_mem_is_zero(const char *mem, size_t len)
641 static const char ZERO[] = {
642 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,
644 while (len >= sizeof(ZERO)) {
645 if (memcmp(mem, ZERO, sizeof(ZERO)))
646 return 0;
647 len -= sizeof(ZERO);
648 mem += sizeof(ZERO);
650 /* Deal with leftover bytes. */
651 if (len)
652 return ! memcmp(mem, ZERO, len);
654 return 1;
657 /** Return true iff the DIGEST_LEN bytes in digest are all zero. */
659 tor_digest_is_zero(const char *digest)
661 return tor_mem_is_zero(digest, DIGEST_LEN);
664 /* Helper: common code to check whether the result of a strtol or strtoul or
665 * strtoll is correct. */
666 #define CHECK_STRTOX_RESULT() \
667 /* Was at least one character converted? */ \
668 if (endptr == s) \
669 goto err; \
670 /* Were there unexpected unconverted characters? */ \
671 if (!next && *endptr) \
672 goto err; \
673 /* Is r within limits? */ \
674 if (r < min || r > max) \
675 goto err; \
676 if (ok) *ok = 1; \
677 if (next) *next = endptr; \
678 return r; \
679 err: \
680 if (ok) *ok = 0; \
681 if (next) *next = endptr; \
682 return 0
684 /** Extract a long from the start of s, in the given numeric base. If
685 * there is unconverted data and next is provided, set *next to the
686 * first unconverted character. An error has occurred if no characters
687 * are converted; or if there are unconverted characters and next is NULL; or
688 * if the parsed value is not between min and max. When no error occurs,
689 * return the parsed value and set *ok (if provided) to 1. When an error
690 * occurs, return 0 and set *ok (if provided) to 0.
692 long
693 tor_parse_long(const char *s, int base, long min, long max,
694 int *ok, char **next)
696 char *endptr;
697 long r;
699 r = strtol(s, &endptr, base);
700 CHECK_STRTOX_RESULT();
703 /** As tor_parse_long(), but return an unsigned long. */
704 unsigned long
705 tor_parse_ulong(const char *s, int base, unsigned long min,
706 unsigned long max, int *ok, char **next)
708 char *endptr;
709 unsigned long r;
711 r = strtoul(s, &endptr, base);
712 CHECK_STRTOX_RESULT();
715 /** As tor_parse_log, but return a unit64_t. Only base 10 is guaranteed to
716 * work for now. */
717 uint64_t
718 tor_parse_uint64(const char *s, int base, uint64_t min,
719 uint64_t max, int *ok, char **next)
721 char *endptr;
722 uint64_t r;
724 #ifdef HAVE_STRTOULL
725 r = (uint64_t)strtoull(s, &endptr, base);
726 #elif defined(MS_WINDOWS)
727 #if defined(_MSC_VER) && _MSC_VER < 1300
728 tor_assert(base <= 10);
729 r = (uint64_t)_atoi64(s);
730 endptr = (char*)s;
731 while (TOR_ISSPACE(*endptr)) endptr++;
732 while (TOR_ISDIGIT(*endptr)) endptr++;
733 #else
734 r = (uint64_t)_strtoui64(s, &endptr, base);
735 #endif
736 #elif SIZEOF_LONG == 8
737 r = (uint64_t)strtoul(s, &endptr, base);
738 #else
739 #error "I don't know how to parse 64-bit numbers."
740 #endif
742 CHECK_STRTOX_RESULT();
745 /** Encode the <b>srclen</b> bytes at <b>src</b> in a NUL-terminated,
746 * uppercase hexadecimal string; store it in the <b>destlen</b>-byte buffer
747 * <b>dest</b>.
749 void
750 base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
752 const char *end;
753 char *cp;
755 tor_assert(destlen >= srclen*2+1);
756 tor_assert(destlen < SIZE_T_CEILING);
758 cp = dest;
759 end = src+srclen;
760 while (src<end) {
761 *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) >> 4 ];
762 *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) & 0xf ];
763 ++src;
765 *cp = '\0';
768 /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
769 static INLINE int
770 _hex_decode_digit(char c)
772 switch (c) {
773 case '0': return 0;
774 case '1': return 1;
775 case '2': return 2;
776 case '3': return 3;
777 case '4': return 4;
778 case '5': return 5;
779 case '6': return 6;
780 case '7': return 7;
781 case '8': return 8;
782 case '9': return 9;
783 case 'A': case 'a': return 10;
784 case 'B': case 'b': return 11;
785 case 'C': case 'c': return 12;
786 case 'D': case 'd': return 13;
787 case 'E': case 'e': return 14;
788 case 'F': case 'f': return 15;
789 default:
790 return -1;
794 /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
796 hex_decode_digit(char c)
798 return _hex_decode_digit(c);
801 /** Given a hexadecimal string of <b>srclen</b> bytes in <b>src</b>, decode it
802 * and store the result in the <b>destlen</b>-byte buffer at <b>dest</b>.
803 * Return 0 on success, -1 on failure. */
805 base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
807 const char *end;
809 int v1,v2;
810 if ((srclen % 2) != 0)
811 return -1;
812 if (destlen < srclen/2 || destlen > SIZE_T_CEILING)
813 return -1;
814 end = src+srclen;
815 while (src<end) {
816 v1 = _hex_decode_digit(*src);
817 v2 = _hex_decode_digit(*(src+1));
818 if (v1<0||v2<0)
819 return -1;
820 *(uint8_t*)dest = (v1<<4)|v2;
821 ++dest;
822 src+=2;
824 return 0;
827 /** Allocate and return a new string representing the contents of <b>s</b>,
828 * surrounded by quotes and using standard C escapes.
830 * Generally, we use this for logging values that come in over the network to
831 * keep them from tricking users, and for sending certain values to the
832 * controller.
834 * We trust values from the resolver, OS, configuration file, and command line
835 * to not be maliciously ill-formed. We validate incoming routerdescs and
836 * SOCKS requests and addresses from BEGIN cells as they're parsed;
837 * afterwards, we trust them as non-malicious.
839 char *
840 esc_for_log(const char *s)
842 const char *cp;
843 char *result, *outp;
844 size_t len = 3;
845 if (!s) {
846 return tor_strdup("");
849 for (cp = s; *cp; ++cp) {
850 switch (*cp) {
851 case '\\':
852 case '\"':
853 case '\'':
854 len += 2;
855 break;
856 default:
857 if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127)
858 ++len;
859 else
860 len += 4;
861 break;
865 result = outp = tor_malloc(len);
866 *outp++ = '\"';
867 for (cp = s; *cp; ++cp) {
868 switch (*cp) {
869 case '\\':
870 case '\"':
871 case '\'':
872 *outp++ = '\\';
873 *outp++ = *cp;
874 break;
875 case '\n':
876 *outp++ = '\\';
877 *outp++ = 'n';
878 break;
879 case '\t':
880 *outp++ = '\\';
881 *outp++ = 't';
882 break;
883 case '\r':
884 *outp++ = '\\';
885 *outp++ = 'r';
886 break;
887 default:
888 if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127) {
889 *outp++ = *cp;
890 } else {
891 tor_snprintf(outp, 5, "\\%03o", (int)(uint8_t) *cp);
892 outp += 4;
894 break;
898 *outp++ = '\"';
899 *outp++ = 0;
901 return result;
904 /** Allocate and return a new string representing the contents of <b>s</b>,
905 * surrounded by quotes and using standard C escapes.
907 * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
908 * thread. Also, each call invalidates the last-returned value, so don't
909 * try log_warn(LD_GENERAL, "%s %s", escaped(a), escaped(b));
911 const char *
912 escaped(const char *s)
914 static char *_escaped_val = NULL;
915 if (_escaped_val)
916 tor_free(_escaped_val);
918 if (s)
919 _escaped_val = esc_for_log(s);
920 else
921 _escaped_val = NULL;
923 return _escaped_val;
926 /** Rudimentary string wrapping code: given a un-wrapped <b>string</b> (no
927 * newlines!), break the string into newline-terminated lines of no more than
928 * <b>width</b> characters long (not counting newline) and insert them into
929 * <b>out</b> in order. Precede the first line with prefix0, and subsequent
930 * lines with prefixRest.
932 /* This uses a stupid greedy wrapping algorithm right now:
933 * - For each line:
934 * - Try to fit as much stuff as possible, but break on a space.
935 * - If the first "word" of the line will extend beyond the allowable
936 * width, break the word at the end of the width.
938 void
939 wrap_string(smartlist_t *out, const char *string, size_t width,
940 const char *prefix0, const char *prefixRest)
942 size_t p0Len, pRestLen, pCurLen;
943 const char *eos, *prefixCur;
944 tor_assert(out);
945 tor_assert(string);
946 tor_assert(width);
947 if (!prefix0)
948 prefix0 = "";
949 if (!prefixRest)
950 prefixRest = "";
952 p0Len = strlen(prefix0);
953 pRestLen = strlen(prefixRest);
954 tor_assert(width > p0Len && width > pRestLen);
955 eos = strchr(string, '\0');
956 tor_assert(eos);
957 pCurLen = p0Len;
958 prefixCur = prefix0;
960 while ((eos-string)+pCurLen > width) {
961 const char *eol = string + width - pCurLen;
962 while (eol > string && *eol != ' ')
963 --eol;
964 /* eol is now the last space that can fit, or the start of the string. */
965 if (eol > string) {
966 size_t line_len = (eol-string) + pCurLen + 2;
967 char *line = tor_malloc(line_len);
968 memcpy(line, prefixCur, pCurLen);
969 memcpy(line+pCurLen, string, eol-string);
970 line[line_len-2] = '\n';
971 line[line_len-1] = '\0';
972 smartlist_add(out, line);
973 string = eol + 1;
974 } else {
975 size_t line_len = width + 2;
976 char *line = tor_malloc(line_len);
977 memcpy(line, prefixCur, pCurLen);
978 memcpy(line+pCurLen, string, width - pCurLen);
979 line[line_len-2] = '\n';
980 line[line_len-1] = '\0';
981 smartlist_add(out, line);
982 string += width-pCurLen;
984 prefixCur = prefixRest;
985 pCurLen = pRestLen;
988 if (string < eos) {
989 size_t line_len = (eos-string) + pCurLen + 2;
990 char *line = tor_malloc(line_len);
991 memcpy(line, prefixCur, pCurLen);
992 memcpy(line+pCurLen, string, eos-string);
993 line[line_len-2] = '\n';
994 line[line_len-1] = '\0';
995 smartlist_add(out, line);
999 /* =====
1000 * Time
1001 * ===== */
1003 /** Return the number of microseconds elapsed between *start and *end.
1005 long
1006 tv_udiff(const struct timeval *start, const struct timeval *end)
1008 long udiff;
1009 long secdiff = end->tv_sec - start->tv_sec;
1011 if (labs(secdiff+1) > LONG_MAX/1000000) {
1012 log_warn(LD_GENERAL, "comparing times too far apart.");
1013 return LONG_MAX;
1016 udiff = secdiff*1000000L + (end->tv_usec - start->tv_usec);
1017 return udiff;
1020 /** Yield true iff <b>y</b> is a leap-year. */
1021 #define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
1022 /** Helper: Return the number of leap-days between Jan 1, y1 and Jan 1, y2. */
1023 static int
1024 n_leapdays(int y1, int y2)
1026 --y1;
1027 --y2;
1028 return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
1030 /** Number of days per month in non-leap year; used by tor_timegm. */
1031 static const int days_per_month[] =
1032 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1034 /** Return a time_t given a struct tm. The result is given in GMT, and
1035 * does not account for leap seconds.
1037 time_t
1038 tor_timegm(struct tm *tm)
1040 /* This is a pretty ironclad timegm implementation, snarfed from Python2.2.
1041 * It's way more brute-force than fiddling with tzset().
1043 time_t year, days, hours, minutes, seconds;
1044 int i;
1045 year = tm->tm_year + 1900;
1046 if (year < 1970 || tm->tm_mon < 0 || tm->tm_mon > 11) {
1047 log_warn(LD_BUG, "Out-of-range argument to tor_timegm");
1048 return -1;
1050 tor_assert(year < INT_MAX);
1051 days = 365 * (year-1970) + n_leapdays(1970,(int)year);
1052 for (i = 0; i < tm->tm_mon; ++i)
1053 days += days_per_month[i];
1054 if (tm->tm_mon > 1 && IS_LEAPYEAR(year))
1055 ++days;
1056 days += tm->tm_mday - 1;
1057 hours = days*24 + tm->tm_hour;
1059 minutes = hours*60 + tm->tm_min;
1060 seconds = minutes*60 + tm->tm_sec;
1061 return seconds;
1064 /* strftime is locale-specific, so we need to replace those parts */
1066 /** A c-locale array of 3-letter names of weekdays, starting with Sun. */
1067 static const char *WEEKDAY_NAMES[] =
1068 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
1069 /** A c-locale array of 3-letter names of months, starting with Jan. */
1070 static const char *MONTH_NAMES[] =
1071 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1072 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1074 /** Set <b>buf</b> to the RFC1123 encoding of the GMT value of <b>t</b>.
1075 * The buffer must be at least RFC1123_TIME_LEN+1 bytes long.
1077 * (RFC1123 format is Fri, 29 Sep 2006 15:54:20 GMT)
1079 void
1080 format_rfc1123_time(char *buf, time_t t)
1082 struct tm tm;
1084 tor_gmtime_r(&t, &tm);
1086 strftime(buf, RFC1123_TIME_LEN+1, "___, %d ___ %Y %H:%M:%S GMT", &tm);
1087 tor_assert(tm.tm_wday >= 0);
1088 tor_assert(tm.tm_wday <= 6);
1089 memcpy(buf, WEEKDAY_NAMES[tm.tm_wday], 3);
1090 tor_assert(tm.tm_wday >= 0);
1091 tor_assert(tm.tm_mon <= 11);
1092 memcpy(buf+8, MONTH_NAMES[tm.tm_mon], 3);
1095 /** Parse the the RFC1123 encoding of some time (in GMT) from <b>buf</b>,
1096 * and store the result in *<b>t</b>.
1098 * Return 0 on success, -1 on failure.
1101 parse_rfc1123_time(const char *buf, time_t *t)
1103 struct tm tm;
1104 char month[4];
1105 char weekday[4];
1106 int i, m;
1107 unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
1109 if (strlen(buf) != RFC1123_TIME_LEN)
1110 return -1;
1111 memset(&tm, 0, sizeof(tm));
1112 if (tor_sscanf(buf, "%3s, %2u %3s %u %2u:%2u:%2u GMT", weekday,
1113 &tm_mday, month, &tm_year, &tm_hour,
1114 &tm_min, &tm_sec) < 7) {
1115 char *esc = esc_for_log(buf);
1116 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
1117 tor_free(esc);
1118 return -1;
1120 if (tm_mday > 31 || tm_hour > 23 || tm_min > 59 || tm_sec > 61) {
1121 char *esc = esc_for_log(buf);
1122 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
1123 tor_free(esc);
1124 return -1;
1126 tm.tm_mday = (int)tm_mday;
1127 tm.tm_year = (int)tm_year;
1128 tm.tm_hour = (int)tm_hour;
1129 tm.tm_min = (int)tm_min;
1130 tm.tm_sec = (int)tm_sec;
1132 m = -1;
1133 for (i = 0; i < 12; ++i) {
1134 if (!strcmp(month, MONTH_NAMES[i])) {
1135 m = i;
1136 break;
1139 if (m<0) {
1140 char *esc = esc_for_log(buf);
1141 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s: No such month", esc);
1142 tor_free(esc);
1143 return -1;
1145 tm.tm_mon = m;
1147 if (tm.tm_year < 1970) {
1148 char *esc = esc_for_log(buf);
1149 log_warn(LD_GENERAL,
1150 "Got invalid RFC1123 time %s. (Before 1970)", esc);
1151 tor_free(esc);
1152 return -1;
1154 tm.tm_year -= 1900;
1156 *t = tor_timegm(&tm);
1157 return 0;
1160 /** Set <b>buf</b> to the ISO8601 encoding of the local value of <b>t</b>.
1161 * The buffer must be at least ISO_TIME_LEN+1 bytes long.
1163 * (ISO8601 format is 2006-10-29 10:57:20)
1165 void
1166 format_local_iso_time(char *buf, time_t t)
1168 struct tm tm;
1169 strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_localtime_r(&t, &tm));
1172 /** Set <b>buf</b> to the ISO8601 encoding of the GMT value of <b>t</b>.
1173 * The buffer must be at least ISO_TIME_LEN+1 bytes long.
1175 void
1176 format_iso_time(char *buf, time_t t)
1178 struct tm tm;
1179 strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t, &tm));
1182 /** Given an ISO-formatted UTC time value (after the epoch) in <b>cp</b>,
1183 * parse it and store its value in *<b>t</b>. Return 0 on success, -1 on
1184 * failure. Ignore extraneous stuff in <b>cp</b> separated by whitespace from
1185 * the end of the time string. */
1187 parse_iso_time(const char *cp, time_t *t)
1189 struct tm st_tm;
1190 unsigned int year=0, month=0, day=0, hour=100, minute=100, second=100;
1191 if (tor_sscanf(cp, "%u-%2u-%2u %2u:%2u:%2u", &year, &month,
1192 &day, &hour, &minute, &second) < 6) {
1193 char *esc = esc_for_log(cp);
1194 log_warn(LD_GENERAL, "ISO time %s was unparseable", esc);
1195 tor_free(esc);
1196 return -1;
1198 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
1199 hour > 23 || minute > 59 || second > 61) {
1200 char *esc = esc_for_log(cp);
1201 log_warn(LD_GENERAL, "ISO time %s was nonsensical", esc);
1202 tor_free(esc);
1203 return -1;
1205 st_tm.tm_year = year-1900;
1206 st_tm.tm_mon = month-1;
1207 st_tm.tm_mday = day;
1208 st_tm.tm_hour = hour;
1209 st_tm.tm_min = minute;
1210 st_tm.tm_sec = second;
1212 if (st_tm.tm_year < 70) {
1213 char *esc = esc_for_log(cp);
1214 log_warn(LD_GENERAL, "Got invalid ISO time %s. (Before 1970)", esc);
1215 tor_free(esc);
1216 return -1;
1218 *t = tor_timegm(&st_tm);
1219 return 0;
1222 /** Given a <b>date</b> in one of the three formats allowed by HTTP (ugh),
1223 * parse it into <b>tm</b>. Return 0 on success, negative on failure. */
1225 parse_http_time(const char *date, struct tm *tm)
1227 const char *cp;
1228 char month[4];
1229 char wkday[4];
1230 int i;
1231 unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
1233 tor_assert(tm);
1234 memset(tm, 0, sizeof(*tm));
1236 /* First, try RFC1123 or RFC850 format: skip the weekday. */
1237 if ((cp = strchr(date, ','))) {
1238 ++cp;
1239 if (tor_sscanf(date, "%2u %3s %4u %2u:%2u:%2u GMT",
1240 &tm_mday, month, &tm_year,
1241 &tm_hour, &tm_min, &tm_sec) == 6) {
1242 /* rfc1123-date */
1243 tm_year -= 1900;
1244 } else if (tor_sscanf(date, "%2u-%3s-%2u %2u:%2u:%2u GMT",
1245 &tm_mday, month, &tm_year,
1246 &tm_hour, &tm_min, &tm_sec) == 6) {
1247 /* rfc850-date */
1248 } else {
1249 return -1;
1251 } else {
1252 /* No comma; possibly asctime() format. */
1253 if (tor_sscanf(date, "%3s %3s %2u %2u:%2u:%2u %4u",
1254 wkday, month, &tm_mday,
1255 &tm_hour, &tm_min, &tm_sec, &tm_year) == 7) {
1256 tm_year -= 1900;
1257 } else {
1258 return -1;
1261 tm->tm_mday = (int)tm_mday;
1262 tm->tm_year = (int)tm_year;
1263 tm->tm_hour = (int)tm_hour;
1264 tm->tm_min = (int)tm_min;
1265 tm->tm_sec = (int)tm_sec;
1267 month[3] = '\0';
1268 /* Okay, now decode the month. */
1269 for (i = 0; i < 12; ++i) {
1270 if (!strcasecmp(MONTH_NAMES[i], month)) {
1271 tm->tm_mon = i+1;
1275 if (tm->tm_year < 0 ||
1276 tm->tm_mon < 1 || tm->tm_mon > 12 ||
1277 tm->tm_mday < 0 || tm->tm_mday > 31 ||
1278 tm->tm_hour < 0 || tm->tm_hour > 23 ||
1279 tm->tm_min < 0 || tm->tm_min > 59 ||
1280 tm->tm_sec < 0 || tm->tm_sec > 61)
1281 return -1; /* Out of range, or bad month. */
1283 return 0;
1286 /** Given an <b>interval</b> in seconds, try to write it to the
1287 * <b>out_len</b>-byte buffer in <b>out</b> in a human-readable form.
1288 * Return 0 on success, -1 on failure.
1291 format_time_interval(char *out, size_t out_len, long interval)
1293 /* We only report seconds if there's no hours. */
1294 long sec = 0, min = 0, hour = 0, day = 0;
1295 if (interval < 0)
1296 interval = -interval;
1298 if (interval >= 86400) {
1299 day = interval / 86400;
1300 interval %= 86400;
1302 if (interval >= 3600) {
1303 hour = interval / 3600;
1304 interval %= 3600;
1306 if (interval >= 60) {
1307 min = interval / 60;
1308 interval %= 60;
1310 sec = interval;
1312 if (day) {
1313 return tor_snprintf(out, out_len, "%ld days, %ld hours, %ld minutes",
1314 day, hour, min);
1315 } else if (hour) {
1316 return tor_snprintf(out, out_len, "%ld hours, %ld minutes", hour, min);
1317 } else if (min) {
1318 return tor_snprintf(out, out_len, "%ld minutes, %ld seconds", min, sec);
1319 } else {
1320 return tor_snprintf(out, out_len, "%ld seconds", sec);
1324 /* =====
1325 * Cached time
1326 * ===== */
1328 #ifndef TIME_IS_FAST
1329 /** Cached estimate of the current time. Updated around once per second;
1330 * may be a few seconds off if we are really busy. This is a hack to avoid
1331 * calling time(NULL) (which not everybody has optimized) on critical paths.
1333 static time_t cached_approx_time = 0;
1335 /** Return a cached estimate of the current time from when
1336 * update_approx_time() was last called. This is a hack to avoid calling
1337 * time(NULL) on critical paths: please do not even think of calling it
1338 * anywhere else. */
1339 time_t
1340 approx_time(void)
1342 return cached_approx_time;
1345 /** Update the cached estimate of the current time. This function SHOULD be
1346 * called once per second, and MUST be called before the first call to
1347 * get_approx_time. */
1348 void
1349 update_approx_time(time_t now)
1351 cached_approx_time = now;
1353 #endif
1355 /* =====
1356 * Fuzzy time
1357 * XXXX022 Use this consistently or rip most of it out.
1358 * ===== */
1360 /* In a perfect world, everybody would run NTP, and NTP would be perfect, so
1361 * if we wanted to know "Is the current time before time X?" we could just say
1362 * "time(NULL) < X".
1364 * But unfortunately, many users are running Tor in an imperfect world, on
1365 * even more imperfect computers. Hence, we need to track time oddly. We
1366 * model the user's computer as being "skewed" from accurate time by
1367 * -<b>ftime_skew</b> seconds, such that our best guess of the current time is
1368 * time(NULL)+ftime_skew. We also assume that our measurements of time may
1369 * have up to <b>ftime_slop</b> seconds of inaccuracy; IOW, our window of
1370 * estimate for the current time is now + ftime_skew +/- ftime_slop.
1372 /** Our current estimate of our skew, such that we think the current time is
1373 * closest to time(NULL)+ftime_skew. */
1374 static int ftime_skew = 0;
1375 /** Tolerance during time comparisons, in seconds. */
1376 static int ftime_slop = 60;
1377 /** Set the largest amount of sloppiness we'll allow in fuzzy time
1378 * comparisons. */
1379 void
1380 ftime_set_maximum_sloppiness(int seconds)
1382 tor_assert(seconds >= 0);
1383 ftime_slop = seconds;
1385 /** Set the amount by which we believe our system clock to differ from
1386 * real time. */
1387 void
1388 ftime_set_estimated_skew(int seconds)
1390 ftime_skew = seconds;
1392 #if 0
1393 void
1394 ftime_get_window(time_t now, ftime_t *ft_out)
1396 ft_out->earliest = now + ftime_skew - ftime_slop;
1397 ft_out->latest = now + ftime_skew + ftime_slop;
1399 #endif
1400 /** Return true iff we think that <b>now</b> might be after <b>when</b>. */
1402 ftime_maybe_after(time_t now, time_t when)
1404 /* It may be after when iff the latest possible current time is after when */
1405 return (now + ftime_skew + ftime_slop) >= when;
1407 /** Return true iff we think that <b>now</b> might be before <b>when</b>. */
1409 ftime_maybe_before(time_t now, time_t when)
1411 /* It may be before when iff the earliest possible current time is before */
1412 return (now + ftime_skew - ftime_slop) < when;
1414 /** Return true if we think that <b>now</b> is definitely after <b>when</b>. */
1416 ftime_definitely_after(time_t now, time_t when)
1418 /* It is definitely after when if the earliest time it could be is still
1419 * after when. */
1420 return (now + ftime_skew - ftime_slop) >= when;
1422 /** Return true if we think that <b>now</b> is definitely before <b>when</b>.
1425 ftime_definitely_before(time_t now, time_t when)
1427 /* It is definitely before when if the latest time it could be is still
1428 * before when. */
1429 return (now + ftime_skew + ftime_slop) < when;
1432 /* =====
1433 * File helpers
1434 * ===== */
1436 /** Write <b>count</b> bytes from <b>buf</b> to <b>fd</b>. <b>isSocket</b>
1437 * must be 1 if fd was returned by socket() or accept(), and 0 if fd
1438 * was returned by open(). Return the number of bytes written, or -1
1439 * on error. Only use if fd is a blocking fd. */
1440 ssize_t
1441 write_all(int fd, const char *buf, size_t count, int isSocket)
1443 size_t written = 0;
1444 ssize_t result;
1445 tor_assert(count < SSIZE_T_MAX);
1447 while (written != count) {
1448 if (isSocket)
1449 result = tor_socket_send(fd, buf+written, count-written, 0);
1450 else
1451 result = write(fd, buf+written, count-written);
1452 if (result<0)
1453 return -1;
1454 written += result;
1456 return (ssize_t)count;
1459 /** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes
1460 * or reach the end of the file. <b>isSocket</b> must be 1 if fd
1461 * was returned by socket() or accept(), and 0 if fd was returned by
1462 * open(). Return the number of bytes read, or -1 on error. Only use
1463 * if fd is a blocking fd. */
1464 ssize_t
1465 read_all(int fd, char *buf, size_t count, int isSocket)
1467 size_t numread = 0;
1468 ssize_t result;
1470 if (count > SIZE_T_CEILING || count > SSIZE_T_MAX)
1471 return -1;
1473 while (numread != count) {
1474 if (isSocket)
1475 result = tor_socket_recv(fd, buf+numread, count-numread, 0);
1476 else
1477 result = read(fd, buf+numread, count-numread);
1478 if (result<0)
1479 return -1;
1480 else if (result == 0)
1481 break;
1482 numread += result;
1484 return (ssize_t)numread;
1488 * Filesystem operations.
1491 /** Clean up <b>name</b> so that we can use it in a call to "stat". On Unix,
1492 * we do nothing. On Windows, we remove a trailing slash, unless the path is
1493 * the root of a disk. */
1494 static void
1495 clean_name_for_stat(char *name)
1497 #ifdef MS_WINDOWS
1498 size_t len = strlen(name);
1499 if (!len)
1500 return;
1501 if (name[len-1]=='\\' || name[len-1]=='/') {
1502 if (len == 1 || (len==3 && name[1]==':'))
1503 return;
1504 name[len-1]='\0';
1506 #else
1507 (void)name;
1508 #endif
1511 /** Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
1512 * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
1513 * directory. On FN_ERROR, sets errno. */
1514 file_status_t
1515 file_status(const char *fname)
1517 struct stat st;
1518 char *f;
1519 int r;
1520 f = tor_strdup(fname);
1521 clean_name_for_stat(f);
1522 r = stat(f, &st);
1523 tor_free(f);
1524 if (r) {
1525 if (errno == ENOENT) {
1526 return FN_NOENT;
1528 return FN_ERROR;
1530 if (st.st_mode & S_IFDIR)
1531 return FN_DIR;
1532 else if (st.st_mode & S_IFREG)
1533 return FN_FILE;
1534 else
1535 return FN_ERROR;
1538 /** Check whether dirname exists and is private. If yes return 0. If
1539 * it does not exist, and check==CPD_CREATE is set, try to create it
1540 * and return 0 on success. If it does not exist, and
1541 * check==CPD_CHECK, and we think we can create it, return 0. Else
1542 * return -1. */
1544 check_private_dir(const char *dirname, cpd_check_t check)
1546 int r;
1547 struct stat st;
1548 char *f;
1549 tor_assert(dirname);
1550 f = tor_strdup(dirname);
1551 clean_name_for_stat(f);
1552 r = stat(f, &st);
1553 tor_free(f);
1554 if (r) {
1555 if (errno != ENOENT) {
1556 log(LOG_WARN, LD_FS, "Directory %s cannot be read: %s", dirname,
1557 strerror(errno));
1558 return -1;
1560 if (check == CPD_NONE) {
1561 log(LOG_WARN, LD_FS, "Directory %s does not exist.", dirname);
1562 return -1;
1563 } else if (check == CPD_CREATE) {
1564 log_info(LD_GENERAL, "Creating directory %s", dirname);
1565 #ifdef MS_WINDOWS
1566 r = mkdir(dirname);
1567 #else
1568 r = mkdir(dirname, 0700);
1569 #endif
1570 if (r) {
1571 log(LOG_WARN, LD_FS, "Error creating directory %s: %s", dirname,
1572 strerror(errno));
1573 return -1;
1576 /* XXXX In the case where check==CPD_CHECK, we should look at the
1577 * parent directory a little harder. */
1578 return 0;
1580 if (!(st.st_mode & S_IFDIR)) {
1581 log(LOG_WARN, LD_FS, "%s is not a directory", dirname);
1582 return -1;
1584 #ifndef MS_WINDOWS
1585 if (st.st_uid != getuid()) {
1586 struct passwd *pw = NULL;
1587 char *process_ownername = NULL;
1589 pw = getpwuid(getuid());
1590 process_ownername = pw ? tor_strdup(pw->pw_name) : tor_strdup("<unknown>");
1592 pw = getpwuid(st.st_uid);
1594 log(LOG_WARN, LD_FS, "%s is not owned by this user (%s, %d) but by "
1595 "%s (%d). Perhaps you are running Tor as the wrong user?",
1596 dirname, process_ownername, (int)getuid(),
1597 pw ? pw->pw_name : "<unknown>", (int)st.st_uid);
1599 tor_free(process_ownername);
1600 return -1;
1602 if (st.st_mode & 0077) {
1603 log(LOG_WARN, LD_FS, "Fixing permissions on directory %s", dirname);
1604 if (chmod(dirname, 0700)) {
1605 log(LOG_WARN, LD_FS, "Could not chmod directory %s: %s", dirname,
1606 strerror(errno));
1607 return -1;
1608 } else {
1609 return 0;
1612 #endif
1613 return 0;
1616 /** Create a file named <b>fname</b> with the contents <b>str</b>. Overwrite
1617 * the previous <b>fname</b> if possible. Return 0 on success, -1 on failure.
1619 * This function replaces the old file atomically, if possible. This
1620 * function, and all other functions in util.c that create files, create them
1621 * with mode 0600.
1624 write_str_to_file(const char *fname, const char *str, int bin)
1626 #ifdef MS_WINDOWS
1627 if (!bin && strchr(str, '\r')) {
1628 log_warn(LD_BUG,
1629 "We're writing a text string that already contains a CR.");
1631 #endif
1632 return write_bytes_to_file(fname, str, strlen(str), bin);
1635 /** Represents a file that we're writing to, with support for atomic commit:
1636 * we can write into a a temporary file, and either remove the file on
1637 * failure, or replace the original file on success. */
1638 struct open_file_t {
1639 char *tempname; /**< Name of the temporary file. */
1640 char *filename; /**< Name of the original file. */
1641 int rename_on_close; /**< Are we using the temporary file or not? */
1642 int fd; /**< fd for the open file. */
1643 FILE *stdio_file; /**< stdio wrapper for <b>fd</b>. */
1646 /** Try to start writing to the file in <b>fname</b>, passing the flags
1647 * <b>open_flags</b> to the open() syscall, creating the file (if needed) with
1648 * access value <b>mode</b>. If the O_APPEND flag is set, we append to the
1649 * original file. Otherwise, we open a new temporary file in the same
1650 * directory, and either replace the original or remove the temporary file
1651 * when we're done.
1653 * Return the fd for the newly opened file, and store working data in
1654 * *<b>data_out</b>. The caller should not close the fd manually:
1655 * instead, call finish_writing_to_file() or abort_writing_to_file().
1656 * Returns -1 on failure.
1658 * NOTE: When not appending, the flags O_CREAT and O_TRUNC are treated
1659 * as true and the flag O_EXCL is treated as false.
1661 * NOTE: Ordinarily, O_APPEND means "seek to the end of the file before each
1662 * write()". We don't do that.
1665 start_writing_to_file(const char *fname, int open_flags, int mode,
1666 open_file_t **data_out)
1668 size_t tempname_len = strlen(fname)+16;
1669 open_file_t *new_file = tor_malloc_zero(sizeof(open_file_t));
1670 const char *open_name;
1671 int append = 0;
1673 tor_assert(fname);
1674 tor_assert(data_out);
1675 #if (O_BINARY != 0 && O_TEXT != 0)
1676 tor_assert((open_flags & (O_BINARY|O_TEXT)) != 0);
1677 #endif
1678 new_file->fd = -1;
1679 tor_assert(tempname_len > strlen(fname)); /*check for overflow*/
1680 new_file->filename = tor_strdup(fname);
1681 if (open_flags & O_APPEND) {
1682 open_name = fname;
1683 new_file->rename_on_close = 0;
1684 append = 1;
1685 open_flags &= ~O_APPEND;
1686 } else {
1687 open_name = new_file->tempname = tor_malloc(tempname_len);
1688 if (tor_snprintf(new_file->tempname, tempname_len, "%s.tmp", fname)<0) {
1689 log(LOG_WARN, LD_GENERAL, "Failed to generate filename");
1690 goto err;
1692 /* We always replace an existing temporary file if there is one. */
1693 open_flags |= O_CREAT|O_TRUNC;
1694 open_flags &= ~O_EXCL;
1695 new_file->rename_on_close = 1;
1698 if ((new_file->fd = open(open_name, open_flags, mode)) < 0) {
1699 log(LOG_WARN, LD_FS, "Couldn't open \"%s\" (%s) for writing: %s",
1700 open_name, fname, strerror(errno));
1701 goto err;
1703 if (append) {
1704 if (tor_fd_seekend(new_file->fd) < 0) {
1705 log_warn(LD_FS, "Couldn't seek to end of file \"%s\": %s", open_name,
1706 strerror(errno));
1707 goto err;
1711 *data_out = new_file;
1713 return new_file->fd;
1715 err:
1716 if (new_file->fd >= 0)
1717 close(new_file->fd);
1718 *data_out = NULL;
1719 tor_free(new_file->filename);
1720 tor_free(new_file->tempname);
1721 tor_free(new_file);
1722 return -1;
1725 /** Given <b>file_data</b> from start_writing_to_file(), return a stdio FILE*
1726 * that can be used to write to the same file. The caller should not mix
1727 * stdio calls with non-stdio calls. */
1728 FILE *
1729 fdopen_file(open_file_t *file_data)
1731 tor_assert(file_data);
1732 if (file_data->stdio_file)
1733 return file_data->stdio_file;
1734 tor_assert(file_data->fd >= 0);
1735 if (!(file_data->stdio_file = fdopen(file_data->fd, "a"))) {
1736 log_warn(LD_FS, "Couldn't fdopen \"%s\" [%d]: %s", file_data->filename,
1737 file_data->fd, strerror(errno));
1739 return file_data->stdio_file;
1742 /** Combines start_writing_to_file with fdopen_file(): arguments are as
1743 * for start_writing_to_file, but */
1744 FILE *
1745 start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
1746 open_file_t **data_out)
1748 FILE *res;
1749 if (start_writing_to_file(fname, open_flags, mode, data_out)<0)
1750 return NULL;
1751 if (!(res = fdopen_file(*data_out))) {
1752 abort_writing_to_file(*data_out);
1753 *data_out = NULL;
1755 return res;
1758 /** Helper function: close and free the underlying file and memory in
1759 * <b>file_data</b>. If we were writing into a temporary file, then delete
1760 * that file (if abort_write is true) or replaces the target file with
1761 * the temporary file (if abort_write is false). */
1762 static int
1763 finish_writing_to_file_impl(open_file_t *file_data, int abort_write)
1765 int r = 0;
1766 tor_assert(file_data && file_data->filename);
1767 if (file_data->stdio_file) {
1768 if (fclose(file_data->stdio_file)) {
1769 log_warn(LD_FS, "Error closing \"%s\": %s", file_data->filename,
1770 strerror(errno));
1771 abort_write = r = -1;
1773 } else if (file_data->fd >= 0 && close(file_data->fd) < 0) {
1774 log_warn(LD_FS, "Error flushing \"%s\": %s", file_data->filename,
1775 strerror(errno));
1776 abort_write = r = -1;
1779 if (file_data->rename_on_close) {
1780 tor_assert(file_data->tempname && file_data->filename);
1781 if (abort_write) {
1782 unlink(file_data->tempname);
1783 } else {
1784 tor_assert(strcmp(file_data->filename, file_data->tempname));
1785 if (replace_file(file_data->tempname, file_data->filename)) {
1786 log_warn(LD_FS, "Error replacing \"%s\": %s", file_data->filename,
1787 strerror(errno));
1788 r = -1;
1793 tor_free(file_data->filename);
1794 tor_free(file_data->tempname);
1795 tor_free(file_data);
1797 return r;
1800 /** Finish writing to <b>file_data</b>: close the file handle, free memory as
1801 * needed, and if using a temporary file, replace the original file with
1802 * the temporary file. */
1804 finish_writing_to_file(open_file_t *file_data)
1806 return finish_writing_to_file_impl(file_data, 0);
1809 /** Finish writing to <b>file_data</b>: close the file handle, free memory as
1810 * needed, and if using a temporary file, delete it. */
1812 abort_writing_to_file(open_file_t *file_data)
1814 return finish_writing_to_file_impl(file_data, 1);
1817 /** Helper: given a set of flags as passed to open(2), open the file
1818 * <b>fname</b> and write all the sized_chunk_t structs in <b>chunks</b> to
1819 * the file. Do so as atomically as possible e.g. by opening temp files and
1820 * renaming. */
1821 static int
1822 write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
1823 int open_flags)
1825 open_file_t *file = NULL;
1826 int fd;
1827 ssize_t result;
1828 fd = start_writing_to_file(fname, open_flags, 0600, &file);
1829 if (fd<0)
1830 return -1;
1831 SMARTLIST_FOREACH(chunks, sized_chunk_t *, chunk,
1833 result = write_all(fd, chunk->bytes, chunk->len, 0);
1834 if (result < 0) {
1835 log(LOG_WARN, LD_FS, "Error writing to \"%s\": %s", fname,
1836 strerror(errno));
1837 goto err;
1839 tor_assert((size_t)result == chunk->len);
1842 return finish_writing_to_file(file);
1843 err:
1844 abort_writing_to_file(file);
1845 return -1;
1848 /** Given a smartlist of sized_chunk_t, write them atomically to a file
1849 * <b>fname</b>, overwriting or creating the file as necessary. */
1851 write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin)
1853 int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
1854 return write_chunks_to_file_impl(fname, chunks, flags);
1857 /** As write_str_to_file, but does not assume a NUL-terminated
1858 * string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
1860 write_bytes_to_file(const char *fname, const char *str, size_t len,
1861 int bin)
1863 int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
1864 int r;
1865 sized_chunk_t c = { str, len };
1866 smartlist_t *chunks = smartlist_create();
1867 smartlist_add(chunks, &c);
1868 r = write_chunks_to_file_impl(fname, chunks, flags);
1869 smartlist_free(chunks);
1870 return r;
1873 /** As write_bytes_to_file, but if the file already exists, append the bytes
1874 * to the end of the file instead of overwriting it. */
1876 append_bytes_to_file(const char *fname, const char *str, size_t len,
1877 int bin)
1879 int flags = OPEN_FLAGS_APPEND|(bin?O_BINARY:O_TEXT);
1880 int r;
1881 sized_chunk_t c = { str, len };
1882 smartlist_t *chunks = smartlist_create();
1883 smartlist_add(chunks, &c);
1884 r = write_chunks_to_file_impl(fname, chunks, flags);
1885 smartlist_free(chunks);
1886 return r;
1889 /** Read the contents of <b>filename</b> into a newly allocated
1890 * string; return the string on success or NULL on failure.
1892 * If <b>stat_out</b> is provided, store the result of stat()ing the
1893 * file into <b>stat_out</b>.
1895 * If <b>flags</b> &amp; RFTS_BIN, open the file in binary mode.
1896 * If <b>flags</b> &amp; RFTS_IGNORE_MISSING, don't warn if the file
1897 * doesn't exist.
1900 * This function <em>may</em> return an erroneous result if the file
1901 * is modified while it is running, but must not crash or overflow.
1902 * Right now, the error case occurs when the file length grows between
1903 * the call to stat and the call to read_all: the resulting string will
1904 * be truncated.
1906 char *
1907 read_file_to_str(const char *filename, int flags, struct stat *stat_out)
1909 int fd; /* router file */
1910 struct stat statbuf;
1911 char *string;
1912 ssize_t r;
1913 int bin = flags & RFTS_BIN;
1915 tor_assert(filename);
1917 fd = open(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
1918 if (fd<0) {
1919 int severity = LOG_WARN;
1920 int save_errno = errno;
1921 if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING))
1922 severity = LOG_INFO;
1923 log_fn(severity, LD_FS,"Could not open \"%s\": %s ",filename,
1924 strerror(errno));
1925 errno = save_errno;
1926 return NULL;
1929 if (fstat(fd, &statbuf)<0) {
1930 int save_errno = errno;
1931 close(fd);
1932 log_warn(LD_FS,"Could not fstat \"%s\".",filename);
1933 errno = save_errno;
1934 return NULL;
1937 if ((uint64_t)(statbuf.st_size)+1 > SIZE_T_CEILING)
1938 return NULL;
1940 string = tor_malloc((size_t)(statbuf.st_size+1));
1942 r = read_all(fd,string,(size_t)statbuf.st_size,0);
1943 if (r<0) {
1944 int save_errno = errno;
1945 log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
1946 strerror(errno));
1947 tor_free(string);
1948 close(fd);
1949 errno = save_errno;
1950 return NULL;
1952 string[r] = '\0'; /* NUL-terminate the result. */
1954 #ifdef MS_WINDOWS
1955 if (!bin && strchr(string, '\r')) {
1956 log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
1957 "when reading %s. Coping.",
1958 filename);
1959 tor_strstrip(string, "\r");
1960 r = strlen(string);
1962 if (!bin) {
1963 statbuf.st_size = (size_t) r;
1964 } else
1965 #endif
1966 if (r != statbuf.st_size) {
1967 /* Unless we're using text mode on win32, we'd better have an exact
1968 * match for size. */
1969 int save_errno = errno;
1970 log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
1971 (int)r, (long)statbuf.st_size,filename);
1972 tor_free(string);
1973 close(fd);
1974 errno = save_errno;
1975 return NULL;
1977 close(fd);
1978 if (stat_out) {
1979 memcpy(stat_out, &statbuf, sizeof(struct stat));
1982 return string;
1985 #define TOR_ISODIGIT(c) ('0' <= (c) && (c) <= '7')
1987 /** Given a c-style double-quoted escaped string in <b>s</b>, extract and
1988 * decode its contents into a newly allocated string. On success, assign this
1989 * string to *<b>result</b>, assign its length to <b>size_out</b> (if
1990 * provided), and return a pointer to the position in <b>s</b> immediately
1991 * after the string. On failure, return NULL.
1993 static const char *
1994 unescape_string(const char *s, char **result, size_t *size_out)
1996 const char *cp;
1997 char *out;
1998 if (s[0] != '\"')
1999 return NULL;
2000 cp = s+1;
2001 while (1) {
2002 switch (*cp) {
2003 case '\0':
2004 case '\n':
2005 return NULL;
2006 case '\"':
2007 goto end_of_loop;
2008 case '\\':
2009 if ((cp[1] == 'x' || cp[1] == 'X')
2010 && TOR_ISXDIGIT(cp[2]) && TOR_ISXDIGIT(cp[3])) {
2011 cp += 4;
2012 } else if (TOR_ISODIGIT(cp[1])) {
2013 cp += 2;
2014 if (TOR_ISODIGIT(*cp)) ++cp;
2015 if (TOR_ISODIGIT(*cp)) ++cp;
2016 } else if (cp[1]) {
2017 cp += 2;
2018 } else {
2019 return NULL;
2021 break;
2022 default:
2023 ++cp;
2024 break;
2027 end_of_loop:
2028 out = *result = tor_malloc(cp-s + 1);
2029 cp = s+1;
2030 while (1) {
2031 switch (*cp)
2033 case '\"':
2034 *out = '\0';
2035 if (size_out) *size_out = out - *result;
2036 return cp+1;
2037 case '\0':
2038 tor_fragile_assert();
2039 tor_free(*result);
2040 return NULL;
2041 case '\\':
2042 switch (cp[1])
2044 case 'n': *out++ = '\n'; cp += 2; break;
2045 case 'r': *out++ = '\r'; cp += 2; break;
2046 case 't': *out++ = '\t'; cp += 2; break;
2047 case 'x': case 'X':
2048 *out++ = ((hex_decode_digit(cp[2])<<4) +
2049 hex_decode_digit(cp[3]));
2050 cp += 4;
2051 break;
2052 case '0': case '1': case '2': case '3': case '4': case '5':
2053 case '6': case '7':
2055 int n = cp[1]-'0';
2056 cp += 2;
2057 if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
2058 if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
2059 if (n > 255) { tor_free(*result); return NULL; }
2060 *out++ = (char)n;
2062 break;
2063 case '\'':
2064 case '\"':
2065 case '\\':
2066 case '\?':
2067 *out++ = cp[1];
2068 cp += 2;
2069 break;
2070 default:
2071 tor_free(*result); return NULL;
2073 break;
2074 default:
2075 *out++ = *cp++;
2080 /** Given a string containing part of a configuration file or similar format,
2081 * advance past comments and whitespace and try to parse a single line. If we
2082 * parse a line successfully, set *<b>key_out</b> to a new string holding the
2083 * key portion and *<b>value_out</b> to a new string holding the value portion
2084 * of the line, and return a pointer to the start of the next line. If we run
2085 * out of data, return a pointer to the end of the string. If we encounter an
2086 * error, return NULL.
2088 const char *
2089 parse_config_line_from_str(const char *line, char **key_out, char **value_out)
2091 const char *key, *val, *cp;
2093 tor_assert(key_out);
2094 tor_assert(value_out);
2096 *key_out = *value_out = NULL;
2097 key = val = NULL;
2098 /* Skip until the first keyword. */
2099 while (1) {
2100 while (TOR_ISSPACE(*line))
2101 ++line;
2102 if (*line == '#') {
2103 while (*line && *line != '\n')
2104 ++line;
2105 } else {
2106 break;
2110 if (!*line) { /* End of string? */
2111 *key_out = *value_out = NULL;
2112 return line;
2115 /* Skip until the next space. */
2116 key = line;
2117 while (*line && !TOR_ISSPACE(*line) && *line != '#')
2118 ++line;
2119 *key_out = tor_strndup(key, line-key);
2121 /* Skip until the value. */
2122 while (*line == ' ' || *line == '\t')
2123 ++line;
2125 val = line;
2127 /* Find the end of the line. */
2128 if (*line == '\"') {
2129 if (!(line = unescape_string(line, value_out, NULL)))
2130 return NULL;
2131 while (*line == ' ' || *line == '\t')
2132 ++line;
2133 if (*line && *line != '#' && *line != '\n')
2134 return NULL;
2135 } else {
2136 while (*line && *line != '\n' && *line != '#')
2137 ++line;
2138 if (*line == '\n') {
2139 cp = line++;
2140 } else {
2141 cp = line;
2143 while (cp>val && TOR_ISSPACE(*(cp-1)))
2144 --cp;
2146 tor_assert(cp >= val);
2147 *value_out = tor_strndup(val, cp-val);
2150 if (*line == '#') {
2151 do {
2152 ++line;
2153 } while (*line && *line != '\n');
2155 while (TOR_ISSPACE(*line)) ++line;
2157 return line;
2160 /** Expand any homedir prefix on <b>filename</b>; return a newly allocated
2161 * string. */
2162 char *
2163 expand_filename(const char *filename)
2165 tor_assert(filename);
2166 if (*filename == '~') {
2167 size_t len;
2168 char *home, *result;
2169 const char *rest;
2171 if (filename[1] == '/' || filename[1] == '\0') {
2172 home = getenv("HOME");
2173 if (!home) {
2174 log_warn(LD_CONFIG, "Couldn't find $HOME environment variable while "
2175 "expanding \"%s\"", filename);
2176 return NULL;
2178 home = tor_strdup(home);
2179 rest = strlen(filename)>=2?(filename+2):"";
2180 } else {
2181 #ifdef HAVE_PWD_H
2182 char *username, *slash;
2183 slash = strchr(filename, '/');
2184 if (slash)
2185 username = tor_strndup(filename+1,slash-filename-1);
2186 else
2187 username = tor_strdup(filename+1);
2188 if (!(home = get_user_homedir(username))) {
2189 log_warn(LD_CONFIG,"Couldn't get homedir for \"%s\"",username);
2190 tor_free(username);
2191 return NULL;
2193 tor_free(username);
2194 rest = slash ? (slash+1) : "";
2195 #else
2196 log_warn(LD_CONFIG, "Couldn't expend homedir on system without pwd.h");
2197 return tor_strdup(filename);
2198 #endif
2200 tor_assert(home);
2201 /* Remove trailing slash. */
2202 if (strlen(home)>1 && !strcmpend(home,PATH_SEPARATOR)) {
2203 home[strlen(home)-1] = '\0';
2205 /* Plus one for /, plus one for NUL.
2206 * Round up to 16 in case we can't do math. */
2207 len = strlen(home)+strlen(rest)+16;
2208 result = tor_malloc(len);
2209 tor_snprintf(result,len,"%s"PATH_SEPARATOR"%s",home,rest);
2210 tor_free(home);
2211 return result;
2212 } else {
2213 return tor_strdup(filename);
2217 #define MAX_SCANF_WIDTH 9999
2219 /** DOCDOC */
2220 static int
2221 digit_to_num(char d)
2223 int num = ((int)d) - (int)'0';
2224 tor_assert(num <= 9 && num >= 0);
2225 return num;
2228 /** DOCDOC */
2229 static int
2230 scan_unsigned(const char **bufp, unsigned *out, int width)
2232 unsigned result = 0;
2233 int scanned_so_far = 0;
2234 if (!bufp || !*bufp || !out)
2235 return -1;
2236 if (width<0)
2237 width=MAX_SCANF_WIDTH;
2239 while (**bufp && TOR_ISDIGIT(**bufp) && scanned_so_far < width) {
2240 int digit = digit_to_num(*(*bufp)++);
2241 unsigned new_result = result * 10 + digit;
2242 if (new_result > UINT32_MAX || new_result < result)
2243 return -1; /* over/underflow. */
2244 result = new_result;
2245 ++scanned_so_far;
2248 if (!scanned_so_far) /* No actual digits scanned */
2249 return -1;
2251 *out = result;
2252 return 0;
2255 /** DOCDOC */
2256 static int
2257 scan_string(const char **bufp, char *out, int width)
2259 int scanned_so_far = 0;
2260 if (!bufp || !out || width < 0)
2261 return -1;
2262 while (**bufp && ! TOR_ISSPACE(**bufp) && scanned_so_far < width) {
2263 *out++ = *(*bufp)++;
2264 ++scanned_so_far;
2266 *out = '\0';
2267 return 0;
2270 /** Locale-independent, minimal, no-surprises scanf variant, accepting only a
2271 * restricted pattern format. For more info on what it supports, see
2272 * tor_sscanf() documentation. */
2274 tor_vsscanf(const char *buf, const char *pattern, va_list ap)
2276 int n_matched = 0;
2278 while (*pattern) {
2279 if (*pattern != '%') {
2280 if (*buf == *pattern) {
2281 ++buf;
2282 ++pattern;
2283 continue;
2284 } else {
2285 return n_matched;
2287 } else {
2288 int width = -1;
2289 ++pattern;
2290 if (TOR_ISDIGIT(*pattern)) {
2291 width = digit_to_num(*pattern++);
2292 while (TOR_ISDIGIT(*pattern)) {
2293 width *= 10;
2294 width += digit_to_num(*pattern++);
2295 if (width > MAX_SCANF_WIDTH)
2296 return -1;
2298 if (!width) /* No zero-width things. */
2299 return -1;
2301 if (*pattern == 'u') {
2302 unsigned *u = va_arg(ap, unsigned *);
2303 if (!*buf)
2304 return n_matched;
2305 if (scan_unsigned(&buf, u, width)<0)
2306 return n_matched;
2307 ++pattern;
2308 ++n_matched;
2309 } else if (*pattern == 's') {
2310 char *s = va_arg(ap, char *);
2311 if (width < 0)
2312 return -1;
2313 if (scan_string(&buf, s, width)<0)
2314 return n_matched;
2315 ++pattern;
2316 ++n_matched;
2317 } else if (*pattern == 'c') {
2318 char *ch = va_arg(ap, char *);
2319 if (width != -1)
2320 return -1;
2321 if (!*buf)
2322 return n_matched;
2323 *ch = *buf++;
2324 ++pattern;
2325 ++n_matched;
2326 } else if (*pattern == '%') {
2327 if (*buf != '%')
2328 return -1;
2329 ++buf;
2330 ++pattern;
2331 } else {
2332 return -1; /* Unrecognized pattern component. */
2337 return n_matched;
2340 /** Minimal sscanf replacement: parse <b>buf</b> according to <b>pattern</b>
2341 * and store the results in the corresponding argument fields. Differs from
2342 * sscanf in that it: Only handles %u and %Ns. Does not handle arbitrarily
2343 * long widths. %u does not consume any space. Is locale-independent.
2344 * Returns -1 on malformed patterns. */
2346 tor_sscanf(const char *buf, const char *pattern, ...)
2348 int r;
2349 va_list ap;
2350 va_start(ap, pattern);
2351 r = tor_vsscanf(buf, pattern, ap);
2352 va_end(ap);
2353 return r;
2356 /** Return a new list containing the filenames in the directory <b>dirname</b>.
2357 * Return NULL on error or if <b>dirname</b> is not a directory.
2359 smartlist_t *
2360 tor_listdir(const char *dirname)
2362 smartlist_t *result;
2363 #ifdef MS_WINDOWS
2364 char *pattern;
2365 HANDLE handle;
2366 WIN32_FIND_DATA findData;
2367 size_t pattern_len = strlen(dirname)+16;
2368 pattern = tor_malloc(pattern_len);
2369 tor_snprintf(pattern, pattern_len, "%s\\*", dirname);
2370 if (INVALID_HANDLE_VALUE == (handle = FindFirstFile(pattern, &findData))) {
2371 tor_free(pattern);
2372 return NULL;
2374 result = smartlist_create();
2375 while (1) {
2376 if (strcmp(findData.cFileName, ".") &&
2377 strcmp(findData.cFileName, "..")) {
2378 smartlist_add(result, tor_strdup(findData.cFileName));
2380 if (!FindNextFile(handle, &findData)) {
2381 DWORD err;
2382 if ((err = GetLastError()) != ERROR_NO_MORE_FILES) {
2383 char *errstr = format_win32_error(err);
2384 log_warn(LD_FS, "Error reading directory '%s': %s", dirname, errstr);
2385 tor_free(errstr);
2387 break;
2390 FindClose(handle);
2391 tor_free(pattern);
2392 #else
2393 DIR *d;
2394 struct dirent *de;
2395 if (!(d = opendir(dirname)))
2396 return NULL;
2398 result = smartlist_create();
2399 while ((de = readdir(d))) {
2400 if (!strcmp(de->d_name, ".") ||
2401 !strcmp(de->d_name, ".."))
2402 continue;
2403 smartlist_add(result, tor_strdup(de->d_name));
2405 closedir(d);
2406 #endif
2407 return result;
2410 /** Return true iff <b>filename</b> is a relative path. */
2412 path_is_relative(const char *filename)
2414 if (filename && filename[0] == '/')
2415 return 0;
2416 #ifdef MS_WINDOWS
2417 else if (filename && filename[0] == '\\')
2418 return 0;
2419 else if (filename && strlen(filename)>3 && TOR_ISALPHA(filename[0]) &&
2420 filename[1] == ':' && filename[2] == '\\')
2421 return 0;
2422 #endif
2423 else
2424 return 1;
2427 /* =====
2428 * Process helpers
2429 * ===== */
2431 #ifndef MS_WINDOWS
2432 /* Based on code contributed by christian grothoff */
2433 /** True iff we've called start_daemon(). */
2434 static int start_daemon_called = 0;
2435 /** True iff we've called finish_daemon(). */
2436 static int finish_daemon_called = 0;
2437 /** Socketpair used to communicate between parent and child process while
2438 * daemonizing. */
2439 static int daemon_filedes[2];
2440 /** Start putting the process into daemon mode: fork and drop all resources
2441 * except standard fds. The parent process never returns, but stays around
2442 * until finish_daemon is called. (Note: it's safe to call this more
2443 * than once: calls after the first are ignored.)
2445 void
2446 start_daemon(void)
2448 pid_t pid;
2450 if (start_daemon_called)
2451 return;
2452 start_daemon_called = 1;
2454 if (pipe(daemon_filedes)) {
2455 log_err(LD_GENERAL,"pipe failed; exiting. Error was %s", strerror(errno));
2456 exit(1);
2458 pid = fork();
2459 if (pid < 0) {
2460 log_err(LD_GENERAL,"fork failed. Exiting.");
2461 exit(1);
2463 if (pid) { /* Parent */
2464 int ok;
2465 char c;
2467 close(daemon_filedes[1]); /* we only read */
2468 ok = -1;
2469 while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
2470 if (c == '.')
2471 ok = 1;
2473 fflush(stdout);
2474 if (ok == 1)
2475 exit(0);
2476 else
2477 exit(1); /* child reported error */
2478 } else { /* Child */
2479 close(daemon_filedes[0]); /* we only write */
2481 pid = setsid(); /* Detach from controlling terminal */
2483 * Fork one more time, so the parent (the session group leader) can exit.
2484 * This means that we, as a non-session group leader, can never regain a
2485 * controlling terminal. This part is recommended by Stevens's
2486 * _Advanced Programming in the Unix Environment_.
2488 if (fork() != 0) {
2489 exit(0);
2491 set_main_thread(); /* We are now the main thread. */
2493 return;
2497 /** Finish putting the process into daemon mode: drop standard fds, and tell
2498 * the parent process to exit. (Note: it's safe to call this more than once:
2499 * calls after the first are ignored. Calls start_daemon first if it hasn't
2500 * been called already.)
2502 void
2503 finish_daemon(const char *desired_cwd)
2505 int nullfd;
2506 char c = '.';
2507 if (finish_daemon_called)
2508 return;
2509 if (!start_daemon_called)
2510 start_daemon();
2511 finish_daemon_called = 1;
2513 if (!desired_cwd)
2514 desired_cwd = "/";
2515 /* Don't hold the wrong FS mounted */
2516 if (chdir(desired_cwd) < 0) {
2517 log_err(LD_GENERAL,"chdir to \"%s\" failed. Exiting.",desired_cwd);
2518 exit(1);
2521 nullfd = open("/dev/null", O_RDWR);
2522 if (nullfd < 0) {
2523 log_err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
2524 exit(1);
2526 /* close fds linking to invoking terminal, but
2527 * close usual incoming fds, but redirect them somewhere
2528 * useful so the fds don't get reallocated elsewhere.
2530 if (dup2(nullfd,0) < 0 ||
2531 dup2(nullfd,1) < 0 ||
2532 dup2(nullfd,2) < 0) {
2533 log_err(LD_GENERAL,"dup2 failed. Exiting.");
2534 exit(1);
2536 if (nullfd > 2)
2537 close(nullfd);
2538 /* signal success */
2539 if (write(daemon_filedes[1], &c, sizeof(char)) != sizeof(char)) {
2540 log_err(LD_GENERAL,"write failed. Exiting.");
2542 close(daemon_filedes[1]);
2544 #else
2545 /* defined(MS_WINDOWS) */
2546 void
2547 start_daemon(void)
2550 void
2551 finish_daemon(const char *cp)
2553 (void)cp;
2555 #endif
2557 /** Write the current process ID, followed by NL, into <b>filename</b>.
2559 void
2560 write_pidfile(char *filename)
2562 FILE *pidfile;
2564 if ((pidfile = fopen(filename, "w")) == NULL) {
2565 log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
2566 strerror(errno));
2567 } else {
2568 #ifdef MS_WINDOWS
2569 fprintf(pidfile, "%d\n", (int)_getpid());
2570 #else
2571 fprintf(pidfile, "%d\n", (int)getpid());
2572 #endif
2573 fclose(pidfile);