Avoid assertion on read_file_to_str() with size==SIZE_T_CEILING-1
[tor/rransom.git] / src / common / util.c
blobf206d00c49509277af58082e557467f43d0e85cc
1 /* Copyright (c) 2003, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2011, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 /**
7 * \file util.c
8 * \brief Common functions for strings, IO, network, data structures,
9 * process control.
10 **/
12 /* This is required on rh7 to make strptime not complain.
14 #define _GNU_SOURCE
16 #include "orconfig.h"
17 #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 tor_assert(size < SIZE_T_CEILING);
172 #ifdef USE_DMALLOC
173 result = dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
174 #else
175 result = realloc(ptr, size);
176 #endif
178 if (PREDICT_UNLIKELY(result == NULL)) {
179 log_err(LD_MM,"Out of memory on realloc(). Dying.");
180 exit(1);
182 return result;
185 /** Return a newly allocated copy of the NUL-terminated string s. On
186 * error, log and terminate. (Like strdup(s), but never returns
187 * NULL.)
189 char *
190 _tor_strdup(const char *s DMALLOC_PARAMS)
192 char *dup;
193 tor_assert(s);
195 #ifdef USE_DMALLOC
196 dup = dmalloc_strdup(file, line, s, 0);
197 #else
198 dup = strdup(s);
199 #endif
200 if (PREDICT_UNLIKELY(dup == NULL)) {
201 log_err(LD_MM,"Out of memory on strdup(). Dying.");
202 exit(1);
204 return dup;
207 /** Allocate and return a new string containing the first <b>n</b>
208 * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b>
209 * characters, only the first <b>n</b> are copied. The result is
210 * always NUL-terminated. (Like strndup(s,n), but never returns
211 * NULL.)
213 char *
214 _tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
216 char *dup;
217 tor_assert(s);
218 tor_assert(n < SIZE_T_CEILING);
219 dup = _tor_malloc((n+1) DMALLOC_FN_ARGS);
220 /* Performance note: Ordinarily we prefer strlcpy to strncpy. But
221 * this function gets called a whole lot, and platform strncpy is
222 * much faster than strlcpy when strlen(s) is much longer than n.
224 strncpy(dup, s, n);
225 dup[n]='\0';
226 return dup;
229 /** Allocate a chunk of <b>len</b> bytes, with the same contents as the
230 * <b>len</b> bytes starting at <b>mem</b>. */
231 void *
232 _tor_memdup(const void *mem, size_t len DMALLOC_PARAMS)
234 char *dup;
235 tor_assert(len < SIZE_T_CEILING);
236 tor_assert(mem);
237 dup = _tor_malloc(len DMALLOC_FN_ARGS);
238 memcpy(dup, mem, len);
239 return dup;
242 /** Helper for places that need to take a function pointer to the right
243 * spelling of "free()". */
244 void
245 _tor_free(void *mem)
247 tor_free(mem);
250 #if defined(HAVE_MALLOC_GOOD_SIZE) && !defined(HAVE_MALLOC_GOOD_SIZE_PROTOTYPE)
251 /* Some version of Mac OSX have malloc_good_size in their libc, but not
252 * actually defined in malloc/malloc.h. We detect this and work around it by
253 * prototyping.
255 extern size_t malloc_good_size(size_t size);
256 #endif
258 /** Allocate and return a chunk of memory of size at least *<b>size</b>, using
259 * the same resources we would use to malloc *<b>sizep</b>. Set *<b>sizep</b>
260 * to the number of usable bytes in the chunk of memory. */
261 void *
262 _tor_malloc_roundup(size_t *sizep DMALLOC_PARAMS)
264 #ifdef HAVE_MALLOC_GOOD_SIZE
265 tor_assert(*sizep < SIZE_T_CEILING);
266 *sizep = malloc_good_size(*sizep);
267 return _tor_malloc(*sizep DMALLOC_FN_ARGS);
268 #elif 0 && defined(HAVE_MALLOC_USABLE_SIZE) && !defined(USE_DMALLOC)
269 /* Never use malloc_usable_size(); it makes valgrind really unhappy,
270 * and doesn't win much in terms of usable space where it exists. */
271 void *result;
272 tor_assert(*sizep < SIZE_T_CEILING);
273 result = _tor_malloc(*sizep DMALLOC_FN_ARGS);
274 *sizep = malloc_usable_size(result);
275 return result;
276 #else
277 return _tor_malloc(*sizep DMALLOC_FN_ARGS);
278 #endif
281 /** Call the platform malloc info function, and dump the results to the log at
282 * level <b>severity</b>. If no such function exists, do nothing. */
283 void
284 tor_log_mallinfo(int severity)
286 #ifdef HAVE_MALLINFO
287 struct mallinfo mi;
288 memset(&mi, 0, sizeof(mi));
289 mi = mallinfo();
290 log(severity, LD_MM,
291 "mallinfo() said: arena=%d, ordblks=%d, smblks=%d, hblks=%d, "
292 "hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, "
293 "keepcost=%d",
294 mi.arena, mi.ordblks, mi.smblks, mi.hblks,
295 mi.hblkhd, mi.usmblks, mi.fsmblks, mi.uordblks, mi.fordblks,
296 mi.keepcost);
297 #else
298 (void)severity;
299 #endif
300 #ifdef USE_DMALLOC
301 dmalloc_log_changed(0, /* Since the program started. */
302 1, /* Log info about non-freed pointers. */
303 0, /* Do not log info about freed pointers. */
304 0 /* Do not log individual pointers. */
306 #endif
309 /* =====
310 * Math
311 * ===== */
313 /** Returns floor(log2(u64)). If u64 is 0, (incorrectly) returns 0. */
315 tor_log2(uint64_t u64)
317 int r = 0;
318 if (u64 >= (U64_LITERAL(1)<<32)) {
319 u64 >>= 32;
320 r = 32;
322 if (u64 >= (U64_LITERAL(1)<<16)) {
323 u64 >>= 16;
324 r += 16;
326 if (u64 >= (U64_LITERAL(1)<<8)) {
327 u64 >>= 8;
328 r += 8;
330 if (u64 >= (U64_LITERAL(1)<<4)) {
331 u64 >>= 4;
332 r += 4;
334 if (u64 >= (U64_LITERAL(1)<<2)) {
335 u64 >>= 2;
336 r += 2;
338 if (u64 >= (U64_LITERAL(1)<<1)) {
339 u64 >>= 1;
340 r += 1;
342 return r;
345 /** Return the power of 2 closest to <b>u64</b>. */
346 uint64_t
347 round_to_power_of_2(uint64_t u64)
349 int lg2 = tor_log2(u64);
350 uint64_t low = U64_LITERAL(1) << lg2, high = U64_LITERAL(1) << (lg2+1);
351 if (high - u64 < u64 - low)
352 return high;
353 else
354 return low;
357 /* =====
358 * String manipulation
359 * ===== */
361 /** Remove from the string <b>s</b> every character which appears in
362 * <b>strip</b>. */
363 void
364 tor_strstrip(char *s, const char *strip)
366 char *read = s;
367 while (*read) {
368 if (strchr(strip, *read)) {
369 ++read;
370 } else {
371 *s++ = *read++;
374 *s = '\0';
377 /** Return a pointer to a NUL-terminated hexadecimal string encoding
378 * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
379 * result does not need to be deallocated, but repeated calls to
380 * hex_str will trash old results.
382 const char *
383 hex_str(const char *from, size_t fromlen)
385 static char buf[65];
386 if (fromlen>(sizeof(buf)-1)/2)
387 fromlen = (sizeof(buf)-1)/2;
388 base16_encode(buf,sizeof(buf),from,fromlen);
389 return buf;
392 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
393 * lowercase. */
394 void
395 tor_strlower(char *s)
397 while (*s) {
398 *s = TOR_TOLOWER(*s);
399 ++s;
403 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
404 * lowercase. */
405 void
406 tor_strupper(char *s)
408 while (*s) {
409 *s = TOR_TOUPPER(*s);
410 ++s;
414 /** Return 1 if every character in <b>s</b> is printable, else return 0.
417 tor_strisprint(const char *s)
419 while (*s) {
420 if (!TOR_ISPRINT(*s))
421 return 0;
422 s++;
424 return 1;
427 /** Return 1 if no character in <b>s</b> is uppercase, else return 0.
430 tor_strisnonupper(const char *s)
432 while (*s) {
433 if (TOR_ISUPPER(*s))
434 return 0;
435 s++;
437 return 1;
440 /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
441 * strcmp.
444 strcmpstart(const char *s1, const char *s2)
446 size_t n = strlen(s2);
447 return strncmp(s1, s2, n);
450 /** Compare the s1_len-byte string <b>s1</b> with <b>s2</b>,
451 * without depending on a terminating nul in s1. Sorting order is first by
452 * length, then lexically; return values are as for strcmp.
455 strcmp_len(const char *s1, const char *s2, size_t s1_len)
457 size_t s2_len = strlen(s2);
458 if (s1_len < s2_len)
459 return -1;
460 if (s1_len > s2_len)
461 return 1;
462 return memcmp(s1, s2, s2_len);
465 /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
466 * strcasecmp.
469 strcasecmpstart(const char *s1, const char *s2)
471 size_t n = strlen(s2);
472 return strncasecmp(s1, s2, n);
475 /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
476 * strcmp.
479 strcmpend(const char *s1, const char *s2)
481 size_t n1 = strlen(s1), n2 = strlen(s2);
482 if (n2>n1)
483 return strcmp(s1,s2);
484 else
485 return strncmp(s1+(n1-n2), s2, n2);
488 /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
489 * strcasecmp.
492 strcasecmpend(const char *s1, const char *s2)
494 size_t n1 = strlen(s1), n2 = strlen(s2);
495 if (n2>n1) /* then they can't be the same; figure out which is bigger */
496 return strcasecmp(s1,s2);
497 else
498 return strncasecmp(s1+(n1-n2), s2, n2);
501 /** Compare the value of the string <b>prefix</b> with the start of the
502 * <b>memlen</b>-byte memory chunk at <b>mem</b>. Return as for strcmp.
504 * [As memcmp(mem, prefix, strlen(prefix)) but returns -1 if memlen is less
505 * than strlen(prefix).]
508 memcmpstart(const void *mem, size_t memlen,
509 const char *prefix)
511 size_t plen = strlen(prefix);
512 if (memlen < plen)
513 return -1;
514 return memcmp(mem, prefix, plen);
517 /** Return a pointer to the first char of s that is not whitespace and
518 * not a comment, or to the terminating NUL if no such character exists.
520 const char *
521 eat_whitespace(const char *s)
523 tor_assert(s);
525 while (1) {
526 switch (*s) {
527 case '\0':
528 default:
529 return s;
530 case ' ':
531 case '\t':
532 case '\n':
533 case '\r':
534 ++s;
535 break;
536 case '#':
537 ++s;
538 while (*s && *s != '\n')
539 ++s;
544 /** Return a pointer to the first char of s that is not whitespace and
545 * not a comment, or to the terminating NUL if no such character exists.
547 const char *
548 eat_whitespace_eos(const char *s, const char *eos)
550 tor_assert(s);
551 tor_assert(eos && s <= eos);
553 while (s < eos) {
554 switch (*s) {
555 case '\0':
556 default:
557 return s;
558 case ' ':
559 case '\t':
560 case '\n':
561 case '\r':
562 ++s;
563 break;
564 case '#':
565 ++s;
566 while (s < eos && *s && *s != '\n')
567 ++s;
570 return s;
573 /** Return a pointer to the first char of s that is not a space or a tab
574 * or a \\r, or to the terminating NUL if no such character exists. */
575 const char *
576 eat_whitespace_no_nl(const char *s)
578 while (*s == ' ' || *s == '\t' || *s == '\r')
579 ++s;
580 return s;
583 /** As eat_whitespace_no_nl, but stop at <b>eos</b> whether we have
584 * found a non-whitespace character or not. */
585 const char *
586 eat_whitespace_eos_no_nl(const char *s, const char *eos)
588 while (s < eos && (*s == ' ' || *s == '\t' || *s == '\r'))
589 ++s;
590 return s;
593 /** Return a pointer to the first char of s that is whitespace or <b>#</b>,
594 * or to the terminating NUL if no such character exists.
596 const char *
597 find_whitespace(const char *s)
599 /* tor_assert(s); */
600 while (1) {
601 switch (*s)
603 case '\0':
604 case '#':
605 case ' ':
606 case '\r':
607 case '\n':
608 case '\t':
609 return s;
610 default:
611 ++s;
616 /** As find_whitespace, but stop at <b>eos</b> whether we have found a
617 * whitespace or not. */
618 const char *
619 find_whitespace_eos(const char *s, const char *eos)
621 /* tor_assert(s); */
622 while (s < eos) {
623 switch (*s)
625 case '\0':
626 case '#':
627 case ' ':
628 case '\r':
629 case '\n':
630 case '\t':
631 return s;
632 default:
633 ++s;
636 return s;
639 /** Return true iff the 'len' bytes at 'mem' are all zero. */
641 tor_mem_is_zero(const char *mem, size_t len)
643 static const char ZERO[] = {
644 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,
646 while (len >= sizeof(ZERO)) {
647 if (memcmp(mem, ZERO, sizeof(ZERO)))
648 return 0;
649 len -= sizeof(ZERO);
650 mem += sizeof(ZERO);
652 /* Deal with leftover bytes. */
653 if (len)
654 return ! memcmp(mem, ZERO, len);
656 return 1;
659 /** Return true iff the DIGEST_LEN bytes in digest are all zero. */
661 tor_digest_is_zero(const char *digest)
663 return tor_mem_is_zero(digest, DIGEST_LEN);
666 /* Helper: common code to check whether the result of a strtol or strtoul or
667 * strtoll is correct. */
668 #define CHECK_STRTOX_RESULT() \
669 /* Was at least one character converted? */ \
670 if (endptr == s) \
671 goto err; \
672 /* Were there unexpected unconverted characters? */ \
673 if (!next && *endptr) \
674 goto err; \
675 /* Is r within limits? */ \
676 if (r < min || r > max) \
677 goto err; \
678 if (ok) *ok = 1; \
679 if (next) *next = endptr; \
680 return r; \
681 err: \
682 if (ok) *ok = 0; \
683 if (next) *next = endptr; \
684 return 0
686 /** Extract a long from the start of s, in the given numeric base. If
687 * there is unconverted data and next is provided, set *next to the
688 * first unconverted character. An error has occurred if no characters
689 * are converted; or if there are unconverted characters and next is NULL; or
690 * if the parsed value is not between min and max. When no error occurs,
691 * return the parsed value and set *ok (if provided) to 1. When an error
692 * occurs, return 0 and set *ok (if provided) to 0.
694 long
695 tor_parse_long(const char *s, int base, long min, long max,
696 int *ok, char **next)
698 char *endptr;
699 long r;
701 r = strtol(s, &endptr, base);
702 CHECK_STRTOX_RESULT();
705 /** As tor_parse_long(), but return an unsigned long. */
706 unsigned long
707 tor_parse_ulong(const char *s, int base, unsigned long min,
708 unsigned long max, int *ok, char **next)
710 char *endptr;
711 unsigned long r;
713 r = strtoul(s, &endptr, base);
714 CHECK_STRTOX_RESULT();
717 /** As tor_parse_log, but return a unit64_t. Only base 10 is guaranteed to
718 * work for now. */
719 uint64_t
720 tor_parse_uint64(const char *s, int base, uint64_t min,
721 uint64_t max, int *ok, char **next)
723 char *endptr;
724 uint64_t r;
726 #ifdef HAVE_STRTOULL
727 r = (uint64_t)strtoull(s, &endptr, base);
728 #elif defined(MS_WINDOWS)
729 #if defined(_MSC_VER) && _MSC_VER < 1300
730 tor_assert(base <= 10);
731 r = (uint64_t)_atoi64(s);
732 endptr = (char*)s;
733 while (TOR_ISSPACE(*endptr)) endptr++;
734 while (TOR_ISDIGIT(*endptr)) endptr++;
735 #else
736 r = (uint64_t)_strtoui64(s, &endptr, base);
737 #endif
738 #elif SIZEOF_LONG == 8
739 r = (uint64_t)strtoul(s, &endptr, base);
740 #else
741 #error "I don't know how to parse 64-bit numbers."
742 #endif
744 CHECK_STRTOX_RESULT();
747 /** Encode the <b>srclen</b> bytes at <b>src</b> in a NUL-terminated,
748 * uppercase hexadecimal string; store it in the <b>destlen</b>-byte buffer
749 * <b>dest</b>.
751 void
752 base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
754 const char *end;
755 char *cp;
757 tor_assert(destlen >= srclen*2+1);
758 tor_assert(destlen < SIZE_T_CEILING);
760 cp = dest;
761 end = src+srclen;
762 while (src<end) {
763 *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) >> 4 ];
764 *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) & 0xf ];
765 ++src;
767 *cp = '\0';
770 /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
771 static INLINE int
772 _hex_decode_digit(char c)
774 switch (c) {
775 case '0': return 0;
776 case '1': return 1;
777 case '2': return 2;
778 case '3': return 3;
779 case '4': return 4;
780 case '5': return 5;
781 case '6': return 6;
782 case '7': return 7;
783 case '8': return 8;
784 case '9': return 9;
785 case 'A': case 'a': return 10;
786 case 'B': case 'b': return 11;
787 case 'C': case 'c': return 12;
788 case 'D': case 'd': return 13;
789 case 'E': case 'e': return 14;
790 case 'F': case 'f': return 15;
791 default:
792 return -1;
796 /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
798 hex_decode_digit(char c)
800 return _hex_decode_digit(c);
803 /** Given a hexadecimal string of <b>srclen</b> bytes in <b>src</b>, decode it
804 * and store the result in the <b>destlen</b>-byte buffer at <b>dest</b>.
805 * Return 0 on success, -1 on failure. */
807 base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
809 const char *end;
811 int v1,v2;
812 if ((srclen % 2) != 0)
813 return -1;
814 if (destlen < srclen/2 || destlen > SIZE_T_CEILING)
815 return -1;
816 end = src+srclen;
817 while (src<end) {
818 v1 = _hex_decode_digit(*src);
819 v2 = _hex_decode_digit(*(src+1));
820 if (v1<0||v2<0)
821 return -1;
822 *(uint8_t*)dest = (v1<<4)|v2;
823 ++dest;
824 src+=2;
826 return 0;
829 /** Allocate and return a new string representing the contents of <b>s</b>,
830 * surrounded by quotes and using standard C escapes.
832 * Generally, we use this for logging values that come in over the network to
833 * keep them from tricking users, and for sending certain values to the
834 * controller.
836 * We trust values from the resolver, OS, configuration file, and command line
837 * to not be maliciously ill-formed. We validate incoming routerdescs and
838 * SOCKS requests and addresses from BEGIN cells as they're parsed;
839 * afterwards, we trust them as non-malicious.
841 char *
842 esc_for_log(const char *s)
844 const char *cp;
845 char *result, *outp;
846 size_t len = 3;
847 if (!s) {
848 return tor_strdup("");
851 for (cp = s; *cp; ++cp) {
852 switch (*cp) {
853 case '\\':
854 case '\"':
855 case '\'':
856 len += 2;
857 break;
858 default:
859 if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127)
860 ++len;
861 else
862 len += 4;
863 break;
867 result = outp = tor_malloc(len);
868 *outp++ = '\"';
869 for (cp = s; *cp; ++cp) {
870 switch (*cp) {
871 case '\\':
872 case '\"':
873 case '\'':
874 *outp++ = '\\';
875 *outp++ = *cp;
876 break;
877 case '\n':
878 *outp++ = '\\';
879 *outp++ = 'n';
880 break;
881 case '\t':
882 *outp++ = '\\';
883 *outp++ = 't';
884 break;
885 case '\r':
886 *outp++ = '\\';
887 *outp++ = 'r';
888 break;
889 default:
890 if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127) {
891 *outp++ = *cp;
892 } else {
893 tor_snprintf(outp, 5, "\\%03o", (int)(uint8_t) *cp);
894 outp += 4;
896 break;
900 *outp++ = '\"';
901 *outp++ = 0;
903 return result;
906 /** Allocate and return a new string representing the contents of <b>s</b>,
907 * surrounded by quotes and using standard C escapes.
909 * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
910 * thread. Also, each call invalidates the last-returned value, so don't
911 * try log_warn(LD_GENERAL, "%s %s", escaped(a), escaped(b));
913 const char *
914 escaped(const char *s)
916 static char *_escaped_val = NULL;
917 if (_escaped_val)
918 tor_free(_escaped_val);
920 if (s)
921 _escaped_val = esc_for_log(s);
922 else
923 _escaped_val = NULL;
925 return _escaped_val;
928 /** Rudimentary string wrapping code: given a un-wrapped <b>string</b> (no
929 * newlines!), break the string into newline-terminated lines of no more than
930 * <b>width</b> characters long (not counting newline) and insert them into
931 * <b>out</b> in order. Precede the first line with prefix0, and subsequent
932 * lines with prefixRest.
934 /* This uses a stupid greedy wrapping algorithm right now:
935 * - For each line:
936 * - Try to fit as much stuff as possible, but break on a space.
937 * - If the first "word" of the line will extend beyond the allowable
938 * width, break the word at the end of the width.
940 void
941 wrap_string(smartlist_t *out, const char *string, size_t width,
942 const char *prefix0, const char *prefixRest)
944 size_t p0Len, pRestLen, pCurLen;
945 const char *eos, *prefixCur;
946 tor_assert(out);
947 tor_assert(string);
948 tor_assert(width);
949 if (!prefix0)
950 prefix0 = "";
951 if (!prefixRest)
952 prefixRest = "";
954 p0Len = strlen(prefix0);
955 pRestLen = strlen(prefixRest);
956 tor_assert(width > p0Len && width > pRestLen);
957 eos = strchr(string, '\0');
958 tor_assert(eos);
959 pCurLen = p0Len;
960 prefixCur = prefix0;
962 while ((eos-string)+pCurLen > width) {
963 const char *eol = string + width - pCurLen;
964 while (eol > string && *eol != ' ')
965 --eol;
966 /* eol is now the last space that can fit, or the start of the string. */
967 if (eol > string) {
968 size_t line_len = (eol-string) + pCurLen + 2;
969 char *line = tor_malloc(line_len);
970 memcpy(line, prefixCur, pCurLen);
971 memcpy(line+pCurLen, string, eol-string);
972 line[line_len-2] = '\n';
973 line[line_len-1] = '\0';
974 smartlist_add(out, line);
975 string = eol + 1;
976 } else {
977 size_t line_len = width + 2;
978 char *line = tor_malloc(line_len);
979 memcpy(line, prefixCur, pCurLen);
980 memcpy(line+pCurLen, string, width - pCurLen);
981 line[line_len-2] = '\n';
982 line[line_len-1] = '\0';
983 smartlist_add(out, line);
984 string += width-pCurLen;
986 prefixCur = prefixRest;
987 pCurLen = pRestLen;
990 if (string < eos) {
991 size_t line_len = (eos-string) + pCurLen + 2;
992 char *line = tor_malloc(line_len);
993 memcpy(line, prefixCur, pCurLen);
994 memcpy(line+pCurLen, string, eos-string);
995 line[line_len-2] = '\n';
996 line[line_len-1] = '\0';
997 smartlist_add(out, line);
1001 /* =====
1002 * Time
1003 * ===== */
1005 /** Return the number of microseconds elapsed between *start and *end.
1007 long
1008 tv_udiff(const struct timeval *start, const struct timeval *end)
1010 long udiff;
1011 long secdiff = end->tv_sec - start->tv_sec;
1013 if (labs(secdiff+1) > LONG_MAX/1000000) {
1014 log_warn(LD_GENERAL, "comparing times too far apart.");
1015 return LONG_MAX;
1018 udiff = secdiff*1000000L + (end->tv_usec - start->tv_usec);
1019 return udiff;
1022 /** Yield true iff <b>y</b> is a leap-year. */
1023 #define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
1024 /** Helper: Return the number of leap-days between Jan 1, y1 and Jan 1, y2. */
1025 static int
1026 n_leapdays(int y1, int y2)
1028 --y1;
1029 --y2;
1030 return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
1032 /** Number of days per month in non-leap year; used by tor_timegm. */
1033 static const int days_per_month[] =
1034 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1036 /** Return a time_t given a struct tm. The result is given in GMT, and
1037 * does not account for leap seconds.
1039 time_t
1040 tor_timegm(struct tm *tm)
1042 /* This is a pretty ironclad timegm implementation, snarfed from Python2.2.
1043 * It's way more brute-force than fiddling with tzset().
1045 time_t year, days, hours, minutes, seconds;
1046 int i;
1047 year = tm->tm_year + 1900;
1048 if (year < 1970 || tm->tm_mon < 0 || tm->tm_mon > 11) {
1049 log_warn(LD_BUG, "Out-of-range argument to tor_timegm");
1050 return -1;
1052 tor_assert(year < INT_MAX);
1053 days = 365 * (year-1970) + n_leapdays(1970,(int)year);
1054 for (i = 0; i < tm->tm_mon; ++i)
1055 days += days_per_month[i];
1056 if (tm->tm_mon > 1 && IS_LEAPYEAR(year))
1057 ++days;
1058 days += tm->tm_mday - 1;
1059 hours = days*24 + tm->tm_hour;
1061 minutes = hours*60 + tm->tm_min;
1062 seconds = minutes*60 + tm->tm_sec;
1063 return seconds;
1066 /* strftime is locale-specific, so we need to replace those parts */
1068 /** A c-locale array of 3-letter names of weekdays, starting with Sun. */
1069 static const char *WEEKDAY_NAMES[] =
1070 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
1071 /** A c-locale array of 3-letter names of months, starting with Jan. */
1072 static const char *MONTH_NAMES[] =
1073 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1074 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1076 /** Set <b>buf</b> to the RFC1123 encoding of the GMT value of <b>t</b>.
1077 * The buffer must be at least RFC1123_TIME_LEN+1 bytes long.
1079 * (RFC1123 format is Fri, 29 Sep 2006 15:54:20 GMT)
1081 void
1082 format_rfc1123_time(char *buf, time_t t)
1084 struct tm tm;
1086 tor_gmtime_r(&t, &tm);
1088 strftime(buf, RFC1123_TIME_LEN+1, "___, %d ___ %Y %H:%M:%S GMT", &tm);
1089 tor_assert(tm.tm_wday >= 0);
1090 tor_assert(tm.tm_wday <= 6);
1091 memcpy(buf, WEEKDAY_NAMES[tm.tm_wday], 3);
1092 tor_assert(tm.tm_wday >= 0);
1093 tor_assert(tm.tm_mon <= 11);
1094 memcpy(buf+8, MONTH_NAMES[tm.tm_mon], 3);
1097 /** Parse the the RFC1123 encoding of some time (in GMT) from <b>buf</b>,
1098 * and store the result in *<b>t</b>.
1100 * Return 0 on success, -1 on failure.
1103 parse_rfc1123_time(const char *buf, time_t *t)
1105 struct tm tm;
1106 char month[4];
1107 char weekday[4];
1108 int i, m;
1109 unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
1111 if (strlen(buf) != RFC1123_TIME_LEN)
1112 return -1;
1113 memset(&tm, 0, sizeof(tm));
1114 if (tor_sscanf(buf, "%3s, %2u %3s %u %2u:%2u:%2u GMT", weekday,
1115 &tm_mday, month, &tm_year, &tm_hour,
1116 &tm_min, &tm_sec) < 7) {
1117 char *esc = esc_for_log(buf);
1118 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
1119 tor_free(esc);
1120 return -1;
1122 if (tm_mday > 31 || tm_hour > 23 || tm_min > 59 || tm_sec > 61) {
1123 char *esc = esc_for_log(buf);
1124 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
1125 tor_free(esc);
1126 return -1;
1128 tm.tm_mday = (int)tm_mday;
1129 tm.tm_year = (int)tm_year;
1130 tm.tm_hour = (int)tm_hour;
1131 tm.tm_min = (int)tm_min;
1132 tm.tm_sec = (int)tm_sec;
1134 m = -1;
1135 for (i = 0; i < 12; ++i) {
1136 if (!strcmp(month, MONTH_NAMES[i])) {
1137 m = i;
1138 break;
1141 if (m<0) {
1142 char *esc = esc_for_log(buf);
1143 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s: No such month", esc);
1144 tor_free(esc);
1145 return -1;
1147 tm.tm_mon = m;
1149 if (tm.tm_year < 1970) {
1150 char *esc = esc_for_log(buf);
1151 log_warn(LD_GENERAL,
1152 "Got invalid RFC1123 time %s. (Before 1970)", esc);
1153 tor_free(esc);
1154 return -1;
1156 tm.tm_year -= 1900;
1158 *t = tor_timegm(&tm);
1159 return 0;
1162 /** Set <b>buf</b> to the ISO8601 encoding of the local value of <b>t</b>.
1163 * The buffer must be at least ISO_TIME_LEN+1 bytes long.
1165 * (ISO8601 format is 2006-10-29 10:57:20)
1167 void
1168 format_local_iso_time(char *buf, time_t t)
1170 struct tm tm;
1171 strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_localtime_r(&t, &tm));
1174 /** Set <b>buf</b> to the ISO8601 encoding of the GMT value of <b>t</b>.
1175 * The buffer must be at least ISO_TIME_LEN+1 bytes long.
1177 void
1178 format_iso_time(char *buf, time_t t)
1180 struct tm tm;
1181 strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t, &tm));
1184 /** Given an ISO-formatted UTC time value (after the epoch) in <b>cp</b>,
1185 * parse it and store its value in *<b>t</b>. Return 0 on success, -1 on
1186 * failure. Ignore extraneous stuff in <b>cp</b> separated by whitespace from
1187 * the end of the time string. */
1189 parse_iso_time(const char *cp, time_t *t)
1191 struct tm st_tm;
1192 unsigned int year=0, month=0, day=0, hour=100, minute=100, second=100;
1193 if (tor_sscanf(cp, "%u-%2u-%2u %2u:%2u:%2u", &year, &month,
1194 &day, &hour, &minute, &second) < 6) {
1195 char *esc = esc_for_log(cp);
1196 log_warn(LD_GENERAL, "ISO time %s was unparseable", esc);
1197 tor_free(esc);
1198 return -1;
1200 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
1201 hour > 23 || minute > 59 || second > 61) {
1202 char *esc = esc_for_log(cp);
1203 log_warn(LD_GENERAL, "ISO time %s was nonsensical", esc);
1204 tor_free(esc);
1205 return -1;
1207 st_tm.tm_year = year-1900;
1208 st_tm.tm_mon = month-1;
1209 st_tm.tm_mday = day;
1210 st_tm.tm_hour = hour;
1211 st_tm.tm_min = minute;
1212 st_tm.tm_sec = second;
1214 if (st_tm.tm_year < 70) {
1215 char *esc = esc_for_log(cp);
1216 log_warn(LD_GENERAL, "Got invalid ISO time %s. (Before 1970)", esc);
1217 tor_free(esc);
1218 return -1;
1220 *t = tor_timegm(&st_tm);
1221 return 0;
1224 /** Given a <b>date</b> in one of the three formats allowed by HTTP (ugh),
1225 * parse it into <b>tm</b>. Return 0 on success, negative on failure. */
1227 parse_http_time(const char *date, struct tm *tm)
1229 const char *cp;
1230 char month[4];
1231 char wkday[4];
1232 int i;
1233 unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
1235 tor_assert(tm);
1236 memset(tm, 0, sizeof(*tm));
1238 /* First, try RFC1123 or RFC850 format: skip the weekday. */
1239 if ((cp = strchr(date, ','))) {
1240 ++cp;
1241 if (tor_sscanf(date, "%2u %3s %4u %2u:%2u:%2u GMT",
1242 &tm_mday, month, &tm_year,
1243 &tm_hour, &tm_min, &tm_sec) == 6) {
1244 /* rfc1123-date */
1245 tm_year -= 1900;
1246 } else if (tor_sscanf(date, "%2u-%3s-%2u %2u:%2u:%2u GMT",
1247 &tm_mday, month, &tm_year,
1248 &tm_hour, &tm_min, &tm_sec) == 6) {
1249 /* rfc850-date */
1250 } else {
1251 return -1;
1253 } else {
1254 /* No comma; possibly asctime() format. */
1255 if (tor_sscanf(date, "%3s %3s %2u %2u:%2u:%2u %4u",
1256 wkday, month, &tm_mday,
1257 &tm_hour, &tm_min, &tm_sec, &tm_year) == 7) {
1258 tm_year -= 1900;
1259 } else {
1260 return -1;
1263 tm->tm_mday = (int)tm_mday;
1264 tm->tm_year = (int)tm_year;
1265 tm->tm_hour = (int)tm_hour;
1266 tm->tm_min = (int)tm_min;
1267 tm->tm_sec = (int)tm_sec;
1269 month[3] = '\0';
1270 /* Okay, now decode the month. */
1271 for (i = 0; i < 12; ++i) {
1272 if (!strcasecmp(MONTH_NAMES[i], month)) {
1273 tm->tm_mon = i+1;
1277 if (tm->tm_year < 0 ||
1278 tm->tm_mon < 1 || tm->tm_mon > 12 ||
1279 tm->tm_mday < 0 || tm->tm_mday > 31 ||
1280 tm->tm_hour < 0 || tm->tm_hour > 23 ||
1281 tm->tm_min < 0 || tm->tm_min > 59 ||
1282 tm->tm_sec < 0 || tm->tm_sec > 61)
1283 return -1; /* Out of range, or bad month. */
1285 return 0;
1288 /** Given an <b>interval</b> in seconds, try to write it to the
1289 * <b>out_len</b>-byte buffer in <b>out</b> in a human-readable form.
1290 * Return 0 on success, -1 on failure.
1293 format_time_interval(char *out, size_t out_len, long interval)
1295 /* We only report seconds if there's no hours. */
1296 long sec = 0, min = 0, hour = 0, day = 0;
1297 if (interval < 0)
1298 interval = -interval;
1300 if (interval >= 86400) {
1301 day = interval / 86400;
1302 interval %= 86400;
1304 if (interval >= 3600) {
1305 hour = interval / 3600;
1306 interval %= 3600;
1308 if (interval >= 60) {
1309 min = interval / 60;
1310 interval %= 60;
1312 sec = interval;
1314 if (day) {
1315 return tor_snprintf(out, out_len, "%ld days, %ld hours, %ld minutes",
1316 day, hour, min);
1317 } else if (hour) {
1318 return tor_snprintf(out, out_len, "%ld hours, %ld minutes", hour, min);
1319 } else if (min) {
1320 return tor_snprintf(out, out_len, "%ld minutes, %ld seconds", min, sec);
1321 } else {
1322 return tor_snprintf(out, out_len, "%ld seconds", sec);
1326 /* =====
1327 * Cached time
1328 * ===== */
1330 #ifndef TIME_IS_FAST
1331 /** Cached estimate of the current time. Updated around once per second;
1332 * may be a few seconds off if we are really busy. This is a hack to avoid
1333 * calling time(NULL) (which not everybody has optimized) on critical paths.
1335 static time_t cached_approx_time = 0;
1337 /** Return a cached estimate of the current time from when
1338 * update_approx_time() was last called. This is a hack to avoid calling
1339 * time(NULL) on critical paths: please do not even think of calling it
1340 * anywhere else. */
1341 time_t
1342 approx_time(void)
1344 return cached_approx_time;
1347 /** Update the cached estimate of the current time. This function SHOULD be
1348 * called once per second, and MUST be called before the first call to
1349 * get_approx_time. */
1350 void
1351 update_approx_time(time_t now)
1353 cached_approx_time = now;
1355 #endif
1357 /* =====
1358 * Fuzzy time
1359 * XXXX022 Use this consistently or rip most of it out.
1360 * ===== */
1362 /* In a perfect world, everybody would run NTP, and NTP would be perfect, so
1363 * if we wanted to know "Is the current time before time X?" we could just say
1364 * "time(NULL) < X".
1366 * But unfortunately, many users are running Tor in an imperfect world, on
1367 * even more imperfect computers. Hence, we need to track time oddly. We
1368 * model the user's computer as being "skewed" from accurate time by
1369 * -<b>ftime_skew</b> seconds, such that our best guess of the current time is
1370 * time(NULL)+ftime_skew. We also assume that our measurements of time may
1371 * have up to <b>ftime_slop</b> seconds of inaccuracy; IOW, our window of
1372 * estimate for the current time is now + ftime_skew +/- ftime_slop.
1374 /** Our current estimate of our skew, such that we think the current time is
1375 * closest to time(NULL)+ftime_skew. */
1376 static int ftime_skew = 0;
1377 /** Tolerance during time comparisons, in seconds. */
1378 static int ftime_slop = 60;
1379 /** Set the largest amount of sloppiness we'll allow in fuzzy time
1380 * comparisons. */
1381 void
1382 ftime_set_maximum_sloppiness(int seconds)
1384 tor_assert(seconds >= 0);
1385 ftime_slop = seconds;
1387 /** Set the amount by which we believe our system clock to differ from
1388 * real time. */
1389 void
1390 ftime_set_estimated_skew(int seconds)
1392 ftime_skew = seconds;
1394 #if 0
1395 void
1396 ftime_get_window(time_t now, ftime_t *ft_out)
1398 ft_out->earliest = now + ftime_skew - ftime_slop;
1399 ft_out->latest = now + ftime_skew + ftime_slop;
1401 #endif
1402 /** Return true iff we think that <b>now</b> might be after <b>when</b>. */
1404 ftime_maybe_after(time_t now, time_t when)
1406 /* It may be after when iff the latest possible current time is after when */
1407 return (now + ftime_skew + ftime_slop) >= when;
1409 /** Return true iff we think that <b>now</b> might be before <b>when</b>. */
1411 ftime_maybe_before(time_t now, time_t when)
1413 /* It may be before when iff the earliest possible current time is before */
1414 return (now + ftime_skew - ftime_slop) < when;
1416 /** Return true if we think that <b>now</b> is definitely after <b>when</b>. */
1418 ftime_definitely_after(time_t now, time_t when)
1420 /* It is definitely after when if the earliest time it could be is still
1421 * after when. */
1422 return (now + ftime_skew - ftime_slop) >= when;
1424 /** Return true if we think that <b>now</b> is definitely before <b>when</b>.
1427 ftime_definitely_before(time_t now, time_t when)
1429 /* It is definitely before when if the latest time it could be is still
1430 * before when. */
1431 return (now + ftime_skew + ftime_slop) < when;
1434 /* =====
1435 * File helpers
1436 * ===== */
1438 /** Write <b>count</b> bytes from <b>buf</b> to <b>fd</b>. <b>isSocket</b>
1439 * must be 1 if fd was returned by socket() or accept(), and 0 if fd
1440 * was returned by open(). Return the number of bytes written, or -1
1441 * on error. Only use if fd is a blocking fd. */
1442 ssize_t
1443 write_all(int fd, const char *buf, size_t count, int isSocket)
1445 size_t written = 0;
1446 ssize_t result;
1447 tor_assert(count < SSIZE_T_MAX);
1449 while (written != count) {
1450 if (isSocket)
1451 result = tor_socket_send(fd, buf+written, count-written, 0);
1452 else
1453 result = write(fd, buf+written, count-written);
1454 if (result<0)
1455 return -1;
1456 written += result;
1458 return (ssize_t)count;
1461 /** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes
1462 * or reach the end of the file. <b>isSocket</b> must be 1 if fd
1463 * was returned by socket() or accept(), and 0 if fd was returned by
1464 * open(). Return the number of bytes read, or -1 on error. Only use
1465 * if fd is a blocking fd. */
1466 ssize_t
1467 read_all(int fd, char *buf, size_t count, int isSocket)
1469 size_t numread = 0;
1470 ssize_t result;
1472 if (count > SIZE_T_CEILING || count > SSIZE_T_MAX)
1473 return -1;
1475 while (numread != count) {
1476 if (isSocket)
1477 result = tor_socket_recv(fd, buf+numread, count-numread, 0);
1478 else
1479 result = read(fd, buf+numread, count-numread);
1480 if (result<0)
1481 return -1;
1482 else if (result == 0)
1483 break;
1484 numread += result;
1486 return (ssize_t)numread;
1490 * Filesystem operations.
1493 /** Clean up <b>name</b> so that we can use it in a call to "stat". On Unix,
1494 * we do nothing. On Windows, we remove a trailing slash, unless the path is
1495 * the root of a disk. */
1496 static void
1497 clean_name_for_stat(char *name)
1499 #ifdef MS_WINDOWS
1500 size_t len = strlen(name);
1501 if (!len)
1502 return;
1503 if (name[len-1]=='\\' || name[len-1]=='/') {
1504 if (len == 1 || (len==3 && name[1]==':'))
1505 return;
1506 name[len-1]='\0';
1508 #else
1509 (void)name;
1510 #endif
1513 /** Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
1514 * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
1515 * directory. On FN_ERROR, sets errno. */
1516 file_status_t
1517 file_status(const char *fname)
1519 struct stat st;
1520 char *f;
1521 int r;
1522 f = tor_strdup(fname);
1523 clean_name_for_stat(f);
1524 r = stat(f, &st);
1525 tor_free(f);
1526 if (r) {
1527 if (errno == ENOENT) {
1528 return FN_NOENT;
1530 return FN_ERROR;
1532 if (st.st_mode & S_IFDIR)
1533 return FN_DIR;
1534 else if (st.st_mode & S_IFREG)
1535 return FN_FILE;
1536 else
1537 return FN_ERROR;
1540 /** Check whether dirname exists and is private. If yes return 0. If
1541 * it does not exist, and check==CPD_CREATE is set, try to create it
1542 * and return 0 on success. If it does not exist, and
1543 * check==CPD_CHECK, and we think we can create it, return 0. Else
1544 * return -1. */
1546 check_private_dir(const char *dirname, cpd_check_t check)
1548 int r;
1549 struct stat st;
1550 char *f;
1551 tor_assert(dirname);
1552 f = tor_strdup(dirname);
1553 clean_name_for_stat(f);
1554 r = stat(f, &st);
1555 tor_free(f);
1556 if (r) {
1557 if (errno != ENOENT) {
1558 log(LOG_WARN, LD_FS, "Directory %s cannot be read: %s", dirname,
1559 strerror(errno));
1560 return -1;
1562 if (check == CPD_NONE) {
1563 log(LOG_WARN, LD_FS, "Directory %s does not exist.", dirname);
1564 return -1;
1565 } else if (check == CPD_CREATE) {
1566 log_info(LD_GENERAL, "Creating directory %s", dirname);
1567 #ifdef MS_WINDOWS
1568 r = mkdir(dirname);
1569 #else
1570 r = mkdir(dirname, 0700);
1571 #endif
1572 if (r) {
1573 log(LOG_WARN, LD_FS, "Error creating directory %s: %s", dirname,
1574 strerror(errno));
1575 return -1;
1578 /* XXXX In the case where check==CPD_CHECK, we should look at the
1579 * parent directory a little harder. */
1580 return 0;
1582 if (!(st.st_mode & S_IFDIR)) {
1583 log(LOG_WARN, LD_FS, "%s is not a directory", dirname);
1584 return -1;
1586 #ifndef MS_WINDOWS
1587 if (st.st_uid != getuid()) {
1588 struct passwd *pw = NULL;
1589 char *process_ownername = NULL;
1591 pw = getpwuid(getuid());
1592 process_ownername = pw ? tor_strdup(pw->pw_name) : tor_strdup("<unknown>");
1594 pw = getpwuid(st.st_uid);
1596 log(LOG_WARN, LD_FS, "%s is not owned by this user (%s, %d) but by "
1597 "%s (%d). Perhaps you are running Tor as the wrong user?",
1598 dirname, process_ownername, (int)getuid(),
1599 pw ? pw->pw_name : "<unknown>", (int)st.st_uid);
1601 tor_free(process_ownername);
1602 return -1;
1604 if (st.st_mode & 0077) {
1605 log(LOG_WARN, LD_FS, "Fixing permissions on directory %s", dirname);
1606 if (chmod(dirname, 0700)) {
1607 log(LOG_WARN, LD_FS, "Could not chmod directory %s: %s", dirname,
1608 strerror(errno));
1609 return -1;
1610 } else {
1611 return 0;
1614 #endif
1615 return 0;
1618 /** Create a file named <b>fname</b> with the contents <b>str</b>. Overwrite
1619 * the previous <b>fname</b> if possible. Return 0 on success, -1 on failure.
1621 * This function replaces the old file atomically, if possible. This
1622 * function, and all other functions in util.c that create files, create them
1623 * with mode 0600.
1626 write_str_to_file(const char *fname, const char *str, int bin)
1628 #ifdef MS_WINDOWS
1629 if (!bin && strchr(str, '\r')) {
1630 log_warn(LD_BUG,
1631 "We're writing a text string that already contains a CR.");
1633 #endif
1634 return write_bytes_to_file(fname, str, strlen(str), bin);
1637 /** Represents a file that we're writing to, with support for atomic commit:
1638 * we can write into a a temporary file, and either remove the file on
1639 * failure, or replace the original file on success. */
1640 struct open_file_t {
1641 char *tempname; /**< Name of the temporary file. */
1642 char *filename; /**< Name of the original file. */
1643 int rename_on_close; /**< Are we using the temporary file or not? */
1644 int fd; /**< fd for the open file. */
1645 FILE *stdio_file; /**< stdio wrapper for <b>fd</b>. */
1648 /** Try to start writing to the file in <b>fname</b>, passing the flags
1649 * <b>open_flags</b> to the open() syscall, creating the file (if needed) with
1650 * access value <b>mode</b>. If the O_APPEND flag is set, we append to the
1651 * original file. Otherwise, we open a new temporary file in the same
1652 * directory, and either replace the original or remove the temporary file
1653 * when we're done.
1655 * Return the fd for the newly opened file, and store working data in
1656 * *<b>data_out</b>. The caller should not close the fd manually:
1657 * instead, call finish_writing_to_file() or abort_writing_to_file().
1658 * Returns -1 on failure.
1660 * NOTE: When not appending, the flags O_CREAT and O_TRUNC are treated
1661 * as true and the flag O_EXCL is treated as false.
1663 * NOTE: Ordinarily, O_APPEND means "seek to the end of the file before each
1664 * write()". We don't do that.
1667 start_writing_to_file(const char *fname, int open_flags, int mode,
1668 open_file_t **data_out)
1670 size_t tempname_len = strlen(fname)+16;
1671 open_file_t *new_file = tor_malloc_zero(sizeof(open_file_t));
1672 const char *open_name;
1673 int append = 0;
1675 tor_assert(fname);
1676 tor_assert(data_out);
1677 #if (O_BINARY != 0 && O_TEXT != 0)
1678 tor_assert((open_flags & (O_BINARY|O_TEXT)) != 0);
1679 #endif
1680 new_file->fd = -1;
1681 tor_assert(tempname_len > strlen(fname)); /*check for overflow*/
1682 new_file->filename = tor_strdup(fname);
1683 if (open_flags & O_APPEND) {
1684 open_name = fname;
1685 new_file->rename_on_close = 0;
1686 append = 1;
1687 open_flags &= ~O_APPEND;
1688 } else {
1689 open_name = new_file->tempname = tor_malloc(tempname_len);
1690 if (tor_snprintf(new_file->tempname, tempname_len, "%s.tmp", fname)<0) {
1691 log(LOG_WARN, LD_GENERAL, "Failed to generate filename");
1692 goto err;
1694 /* We always replace an existing temporary file if there is one. */
1695 open_flags |= O_CREAT|O_TRUNC;
1696 open_flags &= ~O_EXCL;
1697 new_file->rename_on_close = 1;
1700 if ((new_file->fd = open(open_name, open_flags, mode)) < 0) {
1701 log(LOG_WARN, LD_FS, "Couldn't open \"%s\" (%s) for writing: %s",
1702 open_name, fname, strerror(errno));
1703 goto err;
1705 if (append) {
1706 if (tor_fd_seekend(new_file->fd) < 0) {
1707 log_warn(LD_FS, "Couldn't seek to end of file \"%s\": %s", open_name,
1708 strerror(errno));
1709 goto err;
1713 *data_out = new_file;
1715 return new_file->fd;
1717 err:
1718 if (new_file->fd >= 0)
1719 close(new_file->fd);
1720 *data_out = NULL;
1721 tor_free(new_file->filename);
1722 tor_free(new_file->tempname);
1723 tor_free(new_file);
1724 return -1;
1727 /** Given <b>file_data</b> from start_writing_to_file(), return a stdio FILE*
1728 * that can be used to write to the same file. The caller should not mix
1729 * stdio calls with non-stdio calls. */
1730 FILE *
1731 fdopen_file(open_file_t *file_data)
1733 tor_assert(file_data);
1734 if (file_data->stdio_file)
1735 return file_data->stdio_file;
1736 tor_assert(file_data->fd >= 0);
1737 if (!(file_data->stdio_file = fdopen(file_data->fd, "a"))) {
1738 log_warn(LD_FS, "Couldn't fdopen \"%s\" [%d]: %s", file_data->filename,
1739 file_data->fd, strerror(errno));
1741 return file_data->stdio_file;
1744 /** Combines start_writing_to_file with fdopen_file(): arguments are as
1745 * for start_writing_to_file, but */
1746 FILE *
1747 start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
1748 open_file_t **data_out)
1750 FILE *res;
1751 if (start_writing_to_file(fname, open_flags, mode, data_out)<0)
1752 return NULL;
1753 if (!(res = fdopen_file(*data_out))) {
1754 abort_writing_to_file(*data_out);
1755 *data_out = NULL;
1757 return res;
1760 /** Helper function: close and free the underlying file and memory in
1761 * <b>file_data</b>. If we were writing into a temporary file, then delete
1762 * that file (if abort_write is true) or replaces the target file with
1763 * the temporary file (if abort_write is false). */
1764 static int
1765 finish_writing_to_file_impl(open_file_t *file_data, int abort_write)
1767 int r = 0;
1768 tor_assert(file_data && file_data->filename);
1769 if (file_data->stdio_file) {
1770 if (fclose(file_data->stdio_file)) {
1771 log_warn(LD_FS, "Error closing \"%s\": %s", file_data->filename,
1772 strerror(errno));
1773 abort_write = r = -1;
1775 } else if (file_data->fd >= 0 && close(file_data->fd) < 0) {
1776 log_warn(LD_FS, "Error flushing \"%s\": %s", file_data->filename,
1777 strerror(errno));
1778 abort_write = r = -1;
1781 if (file_data->rename_on_close) {
1782 tor_assert(file_data->tempname && file_data->filename);
1783 if (abort_write) {
1784 unlink(file_data->tempname);
1785 } else {
1786 tor_assert(strcmp(file_data->filename, file_data->tempname));
1787 if (replace_file(file_data->tempname, file_data->filename)) {
1788 log_warn(LD_FS, "Error replacing \"%s\": %s", file_data->filename,
1789 strerror(errno));
1790 r = -1;
1795 tor_free(file_data->filename);
1796 tor_free(file_data->tempname);
1797 tor_free(file_data);
1799 return r;
1802 /** Finish writing to <b>file_data</b>: close the file handle, free memory as
1803 * needed, and if using a temporary file, replace the original file with
1804 * the temporary file. */
1806 finish_writing_to_file(open_file_t *file_data)
1808 return finish_writing_to_file_impl(file_data, 0);
1811 /** Finish writing to <b>file_data</b>: close the file handle, free memory as
1812 * needed, and if using a temporary file, delete it. */
1814 abort_writing_to_file(open_file_t *file_data)
1816 return finish_writing_to_file_impl(file_data, 1);
1819 /** Helper: given a set of flags as passed to open(2), open the file
1820 * <b>fname</b> and write all the sized_chunk_t structs in <b>chunks</b> to
1821 * the file. Do so as atomically as possible e.g. by opening temp files and
1822 * renaming. */
1823 static int
1824 write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
1825 int open_flags)
1827 open_file_t *file = NULL;
1828 int fd;
1829 ssize_t result;
1830 fd = start_writing_to_file(fname, open_flags, 0600, &file);
1831 if (fd<0)
1832 return -1;
1833 SMARTLIST_FOREACH(chunks, sized_chunk_t *, chunk,
1835 result = write_all(fd, chunk->bytes, chunk->len, 0);
1836 if (result < 0) {
1837 log(LOG_WARN, LD_FS, "Error writing to \"%s\": %s", fname,
1838 strerror(errno));
1839 goto err;
1841 tor_assert((size_t)result == chunk->len);
1844 return finish_writing_to_file(file);
1845 err:
1846 abort_writing_to_file(file);
1847 return -1;
1850 /** Given a smartlist of sized_chunk_t, write them atomically to a file
1851 * <b>fname</b>, overwriting or creating the file as necessary. */
1853 write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin)
1855 int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
1856 return write_chunks_to_file_impl(fname, chunks, flags);
1859 /** As write_str_to_file, but does not assume a NUL-terminated
1860 * string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
1862 write_bytes_to_file(const char *fname, const char *str, size_t len,
1863 int bin)
1865 int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
1866 int r;
1867 sized_chunk_t c = { str, len };
1868 smartlist_t *chunks = smartlist_create();
1869 smartlist_add(chunks, &c);
1870 r = write_chunks_to_file_impl(fname, chunks, flags);
1871 smartlist_free(chunks);
1872 return r;
1875 /** As write_bytes_to_file, but if the file already exists, append the bytes
1876 * to the end of the file instead of overwriting it. */
1878 append_bytes_to_file(const char *fname, const char *str, size_t len,
1879 int bin)
1881 int flags = OPEN_FLAGS_APPEND|(bin?O_BINARY:O_TEXT);
1882 int r;
1883 sized_chunk_t c = { str, len };
1884 smartlist_t *chunks = smartlist_create();
1885 smartlist_add(chunks, &c);
1886 r = write_chunks_to_file_impl(fname, chunks, flags);
1887 smartlist_free(chunks);
1888 return r;
1891 /** Read the contents of <b>filename</b> into a newly allocated
1892 * string; return the string on success or NULL on failure.
1894 * If <b>stat_out</b> is provided, store the result of stat()ing the
1895 * file into <b>stat_out</b>.
1897 * If <b>flags</b> &amp; RFTS_BIN, open the file in binary mode.
1898 * If <b>flags</b> &amp; RFTS_IGNORE_MISSING, don't warn if the file
1899 * doesn't exist.
1902 * This function <em>may</em> return an erroneous result if the file
1903 * is modified while it is running, but must not crash or overflow.
1904 * Right now, the error case occurs when the file length grows between
1905 * the call to stat and the call to read_all: the resulting string will
1906 * be truncated.
1908 char *
1909 read_file_to_str(const char *filename, int flags, struct stat *stat_out)
1911 int fd; /* router file */
1912 struct stat statbuf;
1913 char *string;
1914 ssize_t r;
1915 int bin = flags & RFTS_BIN;
1917 tor_assert(filename);
1919 fd = open(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
1920 if (fd<0) {
1921 int severity = LOG_WARN;
1922 int save_errno = errno;
1923 if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING))
1924 severity = LOG_INFO;
1925 log_fn(severity, LD_FS,"Could not open \"%s\": %s ",filename,
1926 strerror(errno));
1927 errno = save_errno;
1928 return NULL;
1931 if (fstat(fd, &statbuf)<0) {
1932 int save_errno = errno;
1933 close(fd);
1934 log_warn(LD_FS,"Could not fstat \"%s\".",filename);
1935 errno = save_errno;
1936 return NULL;
1939 if ((uint64_t)(statbuf.st_size)+1 >= SIZE_T_CEILING)
1940 return NULL;
1942 string = tor_malloc((size_t)(statbuf.st_size+1));
1944 r = read_all(fd,string,(size_t)statbuf.st_size,0);
1945 if (r<0) {
1946 int save_errno = errno;
1947 log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
1948 strerror(errno));
1949 tor_free(string);
1950 close(fd);
1951 errno = save_errno;
1952 return NULL;
1954 string[r] = '\0'; /* NUL-terminate the result. */
1956 #ifdef MS_WINDOWS
1957 if (!bin && strchr(string, '\r')) {
1958 log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
1959 "when reading %s. Coping.",
1960 filename);
1961 tor_strstrip(string, "\r");
1962 r = strlen(string);
1964 if (!bin) {
1965 statbuf.st_size = (size_t) r;
1966 } else
1967 #endif
1968 if (r != statbuf.st_size) {
1969 /* Unless we're using text mode on win32, we'd better have an exact
1970 * match for size. */
1971 int save_errno = errno;
1972 log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
1973 (int)r, (long)statbuf.st_size,filename);
1974 tor_free(string);
1975 close(fd);
1976 errno = save_errno;
1977 return NULL;
1979 close(fd);
1980 if (stat_out) {
1981 memcpy(stat_out, &statbuf, sizeof(struct stat));
1984 return string;
1987 #define TOR_ISODIGIT(c) ('0' <= (c) && (c) <= '7')
1989 /** Given a c-style double-quoted escaped string in <b>s</b>, extract and
1990 * decode its contents into a newly allocated string. On success, assign this
1991 * string to *<b>result</b>, assign its length to <b>size_out</b> (if
1992 * provided), and return a pointer to the position in <b>s</b> immediately
1993 * after the string. On failure, return NULL.
1995 static const char *
1996 unescape_string(const char *s, char **result, size_t *size_out)
1998 const char *cp;
1999 char *out;
2000 if (s[0] != '\"')
2001 return NULL;
2002 cp = s+1;
2003 while (1) {
2004 switch (*cp) {
2005 case '\0':
2006 case '\n':
2007 return NULL;
2008 case '\"':
2009 goto end_of_loop;
2010 case '\\':
2011 if ((cp[1] == 'x' || cp[1] == 'X')
2012 && TOR_ISXDIGIT(cp[2]) && TOR_ISXDIGIT(cp[3])) {
2013 cp += 4;
2014 } else if (TOR_ISODIGIT(cp[1])) {
2015 cp += 2;
2016 if (TOR_ISODIGIT(*cp)) ++cp;
2017 if (TOR_ISODIGIT(*cp)) ++cp;
2018 } else if (cp[1]) {
2019 cp += 2;
2020 } else {
2021 return NULL;
2023 break;
2024 default:
2025 ++cp;
2026 break;
2029 end_of_loop:
2030 out = *result = tor_malloc(cp-s + 1);
2031 cp = s+1;
2032 while (1) {
2033 switch (*cp)
2035 case '\"':
2036 *out = '\0';
2037 if (size_out) *size_out = out - *result;
2038 return cp+1;
2039 case '\0':
2040 tor_fragile_assert();
2041 tor_free(*result);
2042 return NULL;
2043 case '\\':
2044 switch (cp[1])
2046 case 'n': *out++ = '\n'; cp += 2; break;
2047 case 'r': *out++ = '\r'; cp += 2; break;
2048 case 't': *out++ = '\t'; cp += 2; break;
2049 case 'x': case 'X':
2050 *out++ = ((hex_decode_digit(cp[2])<<4) +
2051 hex_decode_digit(cp[3]));
2052 cp += 4;
2053 break;
2054 case '0': case '1': case '2': case '3': case '4': case '5':
2055 case '6': case '7':
2057 int n = cp[1]-'0';
2058 cp += 2;
2059 if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
2060 if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
2061 if (n > 255) { tor_free(*result); return NULL; }
2062 *out++ = (char)n;
2064 break;
2065 case '\'':
2066 case '\"':
2067 case '\\':
2068 case '\?':
2069 *out++ = cp[1];
2070 cp += 2;
2071 break;
2072 default:
2073 tor_free(*result); return NULL;
2075 break;
2076 default:
2077 *out++ = *cp++;
2082 /** Given a string containing part of a configuration file or similar format,
2083 * advance past comments and whitespace and try to parse a single line. If we
2084 * parse a line successfully, set *<b>key_out</b> to a new string holding the
2085 * key portion and *<b>value_out</b> to a new string holding the value portion
2086 * of the line, and return a pointer to the start of the next line. If we run
2087 * out of data, return a pointer to the end of the string. If we encounter an
2088 * error, return NULL.
2090 const char *
2091 parse_config_line_from_str(const char *line, char **key_out, char **value_out)
2093 const char *key, *val, *cp;
2095 tor_assert(key_out);
2096 tor_assert(value_out);
2098 *key_out = *value_out = NULL;
2099 key = val = NULL;
2100 /* Skip until the first keyword. */
2101 while (1) {
2102 while (TOR_ISSPACE(*line))
2103 ++line;
2104 if (*line == '#') {
2105 while (*line && *line != '\n')
2106 ++line;
2107 } else {
2108 break;
2112 if (!*line) { /* End of string? */
2113 *key_out = *value_out = NULL;
2114 return line;
2117 /* Skip until the next space. */
2118 key = line;
2119 while (*line && !TOR_ISSPACE(*line) && *line != '#')
2120 ++line;
2121 *key_out = tor_strndup(key, line-key);
2123 /* Skip until the value. */
2124 while (*line == ' ' || *line == '\t')
2125 ++line;
2127 val = line;
2129 /* Find the end of the line. */
2130 if (*line == '\"') {
2131 if (!(line = unescape_string(line, value_out, NULL)))
2132 return NULL;
2133 while (*line == ' ' || *line == '\t')
2134 ++line;
2135 if (*line && *line != '#' && *line != '\n')
2136 return NULL;
2137 } else {
2138 while (*line && *line != '\n' && *line != '#')
2139 ++line;
2140 if (*line == '\n') {
2141 cp = line++;
2142 } else {
2143 cp = line;
2145 while (cp>val && TOR_ISSPACE(*(cp-1)))
2146 --cp;
2148 tor_assert(cp >= val);
2149 *value_out = tor_strndup(val, cp-val);
2152 if (*line == '#') {
2153 do {
2154 ++line;
2155 } while (*line && *line != '\n');
2157 while (TOR_ISSPACE(*line)) ++line;
2159 return line;
2162 /** Expand any homedir prefix on <b>filename</b>; return a newly allocated
2163 * string. */
2164 char *
2165 expand_filename(const char *filename)
2167 tor_assert(filename);
2168 if (*filename == '~') {
2169 size_t len;
2170 char *home, *result;
2171 const char *rest;
2173 if (filename[1] == '/' || filename[1] == '\0') {
2174 home = getenv("HOME");
2175 if (!home) {
2176 log_warn(LD_CONFIG, "Couldn't find $HOME environment variable while "
2177 "expanding \"%s\"", filename);
2178 return NULL;
2180 home = tor_strdup(home);
2181 rest = strlen(filename)>=2?(filename+2):"";
2182 } else {
2183 #ifdef HAVE_PWD_H
2184 char *username, *slash;
2185 slash = strchr(filename, '/');
2186 if (slash)
2187 username = tor_strndup(filename+1,slash-filename-1);
2188 else
2189 username = tor_strdup(filename+1);
2190 if (!(home = get_user_homedir(username))) {
2191 log_warn(LD_CONFIG,"Couldn't get homedir for \"%s\"",username);
2192 tor_free(username);
2193 return NULL;
2195 tor_free(username);
2196 rest = slash ? (slash+1) : "";
2197 #else
2198 log_warn(LD_CONFIG, "Couldn't expend homedir on system without pwd.h");
2199 return tor_strdup(filename);
2200 #endif
2202 tor_assert(home);
2203 /* Remove trailing slash. */
2204 if (strlen(home)>1 && !strcmpend(home,PATH_SEPARATOR)) {
2205 home[strlen(home)-1] = '\0';
2207 /* Plus one for /, plus one for NUL.
2208 * Round up to 16 in case we can't do math. */
2209 len = strlen(home)+strlen(rest)+16;
2210 result = tor_malloc(len);
2211 tor_snprintf(result,len,"%s"PATH_SEPARATOR"%s",home,rest);
2212 tor_free(home);
2213 return result;
2214 } else {
2215 return tor_strdup(filename);
2219 #define MAX_SCANF_WIDTH 9999
2221 /** DOCDOC */
2222 static int
2223 digit_to_num(char d)
2225 int num = ((int)d) - (int)'0';
2226 tor_assert(num <= 9 && num >= 0);
2227 return num;
2230 /** DOCDOC */
2231 static int
2232 scan_unsigned(const char **bufp, unsigned *out, int width)
2234 unsigned result = 0;
2235 int scanned_so_far = 0;
2236 if (!bufp || !*bufp || !out)
2237 return -1;
2238 if (width<0)
2239 width=MAX_SCANF_WIDTH;
2241 while (**bufp && TOR_ISDIGIT(**bufp) && scanned_so_far < width) {
2242 int digit = digit_to_num(*(*bufp)++);
2243 unsigned new_result = result * 10 + digit;
2244 if (new_result > UINT32_MAX || new_result < result)
2245 return -1; /* over/underflow. */
2246 result = new_result;
2247 ++scanned_so_far;
2250 if (!scanned_so_far) /* No actual digits scanned */
2251 return -1;
2253 *out = result;
2254 return 0;
2257 /** DOCDOC */
2258 static int
2259 scan_string(const char **bufp, char *out, int width)
2261 int scanned_so_far = 0;
2262 if (!bufp || !out || width < 0)
2263 return -1;
2264 while (**bufp && ! TOR_ISSPACE(**bufp) && scanned_so_far < width) {
2265 *out++ = *(*bufp)++;
2266 ++scanned_so_far;
2268 *out = '\0';
2269 return 0;
2272 /** Locale-independent, minimal, no-surprises scanf variant, accepting only a
2273 * restricted pattern format. For more info on what it supports, see
2274 * tor_sscanf() documentation. */
2276 tor_vsscanf(const char *buf, const char *pattern, va_list ap)
2278 int n_matched = 0;
2280 while (*pattern) {
2281 if (*pattern != '%') {
2282 if (*buf == *pattern) {
2283 ++buf;
2284 ++pattern;
2285 continue;
2286 } else {
2287 return n_matched;
2289 } else {
2290 int width = -1;
2291 ++pattern;
2292 if (TOR_ISDIGIT(*pattern)) {
2293 width = digit_to_num(*pattern++);
2294 while (TOR_ISDIGIT(*pattern)) {
2295 width *= 10;
2296 width += digit_to_num(*pattern++);
2297 if (width > MAX_SCANF_WIDTH)
2298 return -1;
2300 if (!width) /* No zero-width things. */
2301 return -1;
2303 if (*pattern == 'u') {
2304 unsigned *u = va_arg(ap, unsigned *);
2305 if (!*buf)
2306 return n_matched;
2307 if (scan_unsigned(&buf, u, width)<0)
2308 return n_matched;
2309 ++pattern;
2310 ++n_matched;
2311 } else if (*pattern == 's') {
2312 char *s = va_arg(ap, char *);
2313 if (width < 0)
2314 return -1;
2315 if (scan_string(&buf, s, width)<0)
2316 return n_matched;
2317 ++pattern;
2318 ++n_matched;
2319 } else if (*pattern == 'c') {
2320 char *ch = va_arg(ap, char *);
2321 if (width != -1)
2322 return -1;
2323 if (!*buf)
2324 return n_matched;
2325 *ch = *buf++;
2326 ++pattern;
2327 ++n_matched;
2328 } else if (*pattern == '%') {
2329 if (*buf != '%')
2330 return -1;
2331 ++buf;
2332 ++pattern;
2333 } else {
2334 return -1; /* Unrecognized pattern component. */
2339 return n_matched;
2342 /** Minimal sscanf replacement: parse <b>buf</b> according to <b>pattern</b>
2343 * and store the results in the corresponding argument fields. Differs from
2344 * sscanf in that it: Only handles %u and %Ns. Does not handle arbitrarily
2345 * long widths. %u does not consume any space. Is locale-independent.
2346 * Returns -1 on malformed patterns. */
2348 tor_sscanf(const char *buf, const char *pattern, ...)
2350 int r;
2351 va_list ap;
2352 va_start(ap, pattern);
2353 r = tor_vsscanf(buf, pattern, ap);
2354 va_end(ap);
2355 return r;
2358 /** Return a new list containing the filenames in the directory <b>dirname</b>.
2359 * Return NULL on error or if <b>dirname</b> is not a directory.
2361 smartlist_t *
2362 tor_listdir(const char *dirname)
2364 smartlist_t *result;
2365 #ifdef MS_WINDOWS
2366 char *pattern;
2367 HANDLE handle;
2368 WIN32_FIND_DATA findData;
2369 size_t pattern_len = strlen(dirname)+16;
2370 pattern = tor_malloc(pattern_len);
2371 tor_snprintf(pattern, pattern_len, "%s\\*", dirname);
2372 if (INVALID_HANDLE_VALUE == (handle = FindFirstFile(pattern, &findData))) {
2373 tor_free(pattern);
2374 return NULL;
2376 result = smartlist_create();
2377 while (1) {
2378 if (strcmp(findData.cFileName, ".") &&
2379 strcmp(findData.cFileName, "..")) {
2380 smartlist_add(result, tor_strdup(findData.cFileName));
2382 if (!FindNextFile(handle, &findData)) {
2383 DWORD err;
2384 if ((err = GetLastError()) != ERROR_NO_MORE_FILES) {
2385 char *errstr = format_win32_error(err);
2386 log_warn(LD_FS, "Error reading directory '%s': %s", dirname, errstr);
2387 tor_free(errstr);
2389 break;
2392 FindClose(handle);
2393 tor_free(pattern);
2394 #else
2395 DIR *d;
2396 struct dirent *de;
2397 if (!(d = opendir(dirname)))
2398 return NULL;
2400 result = smartlist_create();
2401 while ((de = readdir(d))) {
2402 if (!strcmp(de->d_name, ".") ||
2403 !strcmp(de->d_name, ".."))
2404 continue;
2405 smartlist_add(result, tor_strdup(de->d_name));
2407 closedir(d);
2408 #endif
2409 return result;
2412 /** Return true iff <b>filename</b> is a relative path. */
2414 path_is_relative(const char *filename)
2416 if (filename && filename[0] == '/')
2417 return 0;
2418 #ifdef MS_WINDOWS
2419 else if (filename && filename[0] == '\\')
2420 return 0;
2421 else if (filename && strlen(filename)>3 && TOR_ISALPHA(filename[0]) &&
2422 filename[1] == ':' && filename[2] == '\\')
2423 return 0;
2424 #endif
2425 else
2426 return 1;
2429 /* =====
2430 * Process helpers
2431 * ===== */
2433 #ifndef MS_WINDOWS
2434 /* Based on code contributed by christian grothoff */
2435 /** True iff we've called start_daemon(). */
2436 static int start_daemon_called = 0;
2437 /** True iff we've called finish_daemon(). */
2438 static int finish_daemon_called = 0;
2439 /** Socketpair used to communicate between parent and child process while
2440 * daemonizing. */
2441 static int daemon_filedes[2];
2442 /** Start putting the process into daemon mode: fork and drop all resources
2443 * except standard fds. The parent process never returns, but stays around
2444 * until finish_daemon is called. (Note: it's safe to call this more
2445 * than once: calls after the first are ignored.)
2447 void
2448 start_daemon(void)
2450 pid_t pid;
2452 if (start_daemon_called)
2453 return;
2454 start_daemon_called = 1;
2456 if (pipe(daemon_filedes)) {
2457 log_err(LD_GENERAL,"pipe failed; exiting. Error was %s", strerror(errno));
2458 exit(1);
2460 pid = fork();
2461 if (pid < 0) {
2462 log_err(LD_GENERAL,"fork failed. Exiting.");
2463 exit(1);
2465 if (pid) { /* Parent */
2466 int ok;
2467 char c;
2469 close(daemon_filedes[1]); /* we only read */
2470 ok = -1;
2471 while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
2472 if (c == '.')
2473 ok = 1;
2475 fflush(stdout);
2476 if (ok == 1)
2477 exit(0);
2478 else
2479 exit(1); /* child reported error */
2480 } else { /* Child */
2481 close(daemon_filedes[0]); /* we only write */
2483 pid = setsid(); /* Detach from controlling terminal */
2485 * Fork one more time, so the parent (the session group leader) can exit.
2486 * This means that we, as a non-session group leader, can never regain a
2487 * controlling terminal. This part is recommended by Stevens's
2488 * _Advanced Programming in the Unix Environment_.
2490 if (fork() != 0) {
2491 exit(0);
2493 set_main_thread(); /* We are now the main thread. */
2495 return;
2499 /** Finish putting the process into daemon mode: drop standard fds, and tell
2500 * the parent process to exit. (Note: it's safe to call this more than once:
2501 * calls after the first are ignored. Calls start_daemon first if it hasn't
2502 * been called already.)
2504 void
2505 finish_daemon(const char *desired_cwd)
2507 int nullfd;
2508 char c = '.';
2509 if (finish_daemon_called)
2510 return;
2511 if (!start_daemon_called)
2512 start_daemon();
2513 finish_daemon_called = 1;
2515 if (!desired_cwd)
2516 desired_cwd = "/";
2517 /* Don't hold the wrong FS mounted */
2518 if (chdir(desired_cwd) < 0) {
2519 log_err(LD_GENERAL,"chdir to \"%s\" failed. Exiting.",desired_cwd);
2520 exit(1);
2523 nullfd = open("/dev/null", O_RDWR);
2524 if (nullfd < 0) {
2525 log_err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
2526 exit(1);
2528 /* close fds linking to invoking terminal, but
2529 * close usual incoming fds, but redirect them somewhere
2530 * useful so the fds don't get reallocated elsewhere.
2532 if (dup2(nullfd,0) < 0 ||
2533 dup2(nullfd,1) < 0 ||
2534 dup2(nullfd,2) < 0) {
2535 log_err(LD_GENERAL,"dup2 failed. Exiting.");
2536 exit(1);
2538 if (nullfd > 2)
2539 close(nullfd);
2540 /* signal success */
2541 if (write(daemon_filedes[1], &c, sizeof(char)) != sizeof(char)) {
2542 log_err(LD_GENERAL,"write failed. Exiting.");
2544 close(daemon_filedes[1]);
2546 #else
2547 /* defined(MS_WINDOWS) */
2548 void
2549 start_daemon(void)
2552 void
2553 finish_daemon(const char *cp)
2555 (void)cp;
2557 #endif
2559 /** Write the current process ID, followed by NL, into <b>filename</b>.
2561 void
2562 write_pidfile(char *filename)
2564 FILE *pidfile;
2566 if ((pidfile = fopen(filename, "w")) == NULL) {
2567 log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
2568 strerror(errno));
2569 } else {
2570 #ifdef MS_WINDOWS
2571 fprintf(pidfile, "%d\n", (int)_getpid());
2572 #else
2573 fprintf(pidfile, "%d\n", (int)getpid());
2574 #endif
2575 fclose(pidfile);