Spelling fixes in comments and strings
[tor/rransom.git] / src / common / util.c
bloba3338b1d18f7d91557ff3798ac06f2e177d5ae53
1 /* Copyright (c) 2003, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2009, 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 #ifndef MALLOC_ZERO_WORKS
119 /* Some libc mallocs don't work when size==0. Override them. */
120 if (size==0) {
121 size=1;
123 #endif
125 #ifdef USE_DMALLOC
126 result = dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
127 #else
128 result = malloc(size);
129 #endif
131 if (PREDICT_UNLIKELY(result == NULL)) {
132 log_err(LD_MM,"Out of memory on malloc(). Dying.");
133 /* If these functions die within a worker process, they won't call
134 * spawn_exit, but that's ok, since the parent will run out of memory soon
135 * anyway. */
136 exit(1);
138 return result;
141 /** Allocate a chunk of <b>size</b> bytes of memory, fill the memory with
142 * zero bytes, and return a pointer to the result. Log and terminate
143 * the process on error. (Same as calloc(size,1), but never returns NULL.)
145 void *
146 _tor_malloc_zero(size_t size DMALLOC_PARAMS)
148 /* You may ask yourself, "wouldn't it be smart to use calloc instead of
149 * malloc+memset? Perhaps libc's calloc knows some nifty optimization trick
150 * we don't!" Indeed it does, but its optimizations are only a big win when
151 * we're allocating something very big (it knows if it just got the memory
152 * from the OS in a pre-zeroed state). We don't want to use tor_malloc_zero
153 * for big stuff, so we don't bother with calloc. */
154 void *result = _tor_malloc(size DMALLOC_FN_ARGS);
155 memset(result, 0, size);
156 return result;
159 /** Change the size of the memory block pointed to by <b>ptr</b> to <b>size</b>
160 * bytes long; return the new memory block. On error, log and
161 * terminate. (Like realloc(ptr,size), but never returns NULL.)
163 void *
164 _tor_realloc(void *ptr, size_t size DMALLOC_PARAMS)
166 void *result;
168 #ifdef USE_DMALLOC
169 result = dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
170 #else
171 result = realloc(ptr, size);
172 #endif
174 if (PREDICT_UNLIKELY(result == NULL)) {
175 log_err(LD_MM,"Out of memory on realloc(). Dying.");
176 exit(1);
178 return result;
181 /** Return a newly allocated copy of the NUL-terminated string s. On
182 * error, log and terminate. (Like strdup(s), but never returns
183 * NULL.)
185 char *
186 _tor_strdup(const char *s DMALLOC_PARAMS)
188 char *dup;
189 tor_assert(s);
191 #ifdef USE_DMALLOC
192 dup = dmalloc_strdup(file, line, s, 0);
193 #else
194 dup = strdup(s);
195 #endif
196 if (PREDICT_UNLIKELY(dup == NULL)) {
197 log_err(LD_MM,"Out of memory on strdup(). Dying.");
198 exit(1);
200 return dup;
203 /** Allocate and return a new string containing the first <b>n</b>
204 * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b>
205 * characters, only the first <b>n</b> are copied. The result is
206 * always NUL-terminated. (Like strndup(s,n), but never returns
207 * NULL.)
209 char *
210 _tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
212 char *dup;
213 tor_assert(s);
214 dup = _tor_malloc((n+1) DMALLOC_FN_ARGS);
215 /* Performance note: Ordinarily we prefer strlcpy to strncpy. But
216 * this function gets called a whole lot, and platform strncpy is
217 * much faster than strlcpy when strlen(s) is much longer than n.
219 strncpy(dup, s, n);
220 dup[n]='\0';
221 return dup;
224 /** Allocate a chunk of <b>len</b> bytes, with the same contents as the
225 * <b>len</b> bytes starting at <b>mem</b>. */
226 void *
227 _tor_memdup(const void *mem, size_t len DMALLOC_PARAMS)
229 char *dup;
230 tor_assert(mem);
231 dup = _tor_malloc(len DMALLOC_FN_ARGS);
232 memcpy(dup, mem, len);
233 return dup;
236 /** Helper for places that need to take a function pointer to the right
237 * spelling of "free()". */
238 void
239 _tor_free(void *mem)
241 tor_free(mem);
244 #if defined(HAVE_MALLOC_GOOD_SIZE) && !defined(HAVE_MALLOC_GOOD_SIZE_PROTOTYPE)
245 /* Some version of Mac OSX have malloc_good_size in their libc, but not
246 * actually defined in malloc/malloc.h. We detect this and work around it by
247 * prototyping.
249 extern size_t malloc_good_size(size_t size);
250 #endif
252 /** Allocate and return a chunk of memory of size at least *<b>size</b>, using
253 * the same resources we would use to malloc *<b>sizep</b>. Set *<b>sizep</b>
254 * to the number of usable bytes in the chunk of memory. */
255 void *
256 _tor_malloc_roundup(size_t *sizep DMALLOC_PARAMS)
258 #ifdef HAVE_MALLOC_GOOD_SIZE
259 *sizep = malloc_good_size(*sizep);
260 return _tor_malloc(*sizep DMALLOC_FN_ARGS);
261 #elif 0 && defined(HAVE_MALLOC_USABLE_SIZE) && !defined(USE_DMALLOC)
262 /* Never use malloc_usable_size(); it makes valgrind really unhappy,
263 * and doesn't win much in terms of usable space where it exists. */
264 void *result = _tor_malloc(*sizep DMALLOC_FN_ARGS);
265 *sizep = malloc_usable_size(result);
266 return result;
267 #else
268 return _tor_malloc(*sizep DMALLOC_FN_ARGS);
269 #endif
272 /** Call the platform malloc info function, and dump the results to the log at
273 * level <b>severity</b>. If no such function exists, do nothing. */
274 void
275 tor_log_mallinfo(int severity)
277 #ifdef HAVE_MALLINFO
278 struct mallinfo mi;
279 memset(&mi, 0, sizeof(mi));
280 mi = mallinfo();
281 log(severity, LD_MM,
282 "mallinfo() said: arena=%d, ordblks=%d, smblks=%d, hblks=%d, "
283 "hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, "
284 "keepcost=%d",
285 mi.arena, mi.ordblks, mi.smblks, mi.hblks,
286 mi.hblkhd, mi.usmblks, mi.fsmblks, mi.uordblks, mi.fordblks,
287 mi.keepcost);
288 #else
289 (void)severity;
290 #endif
291 #ifdef USE_DMALLOC
292 dmalloc_log_changed(0, /* Since the program started. */
293 1, /* Log info about non-freed pointers. */
294 0, /* Do not log info about freed pointers. */
295 0 /* Do not log individual pointers. */
297 #endif
300 /* =====
301 * Math
302 * ===== */
304 /** Returns floor(log2(u64)). If u64 is 0, (incorrectly) returns 0. */
306 tor_log2(uint64_t u64)
308 int r = 0;
309 if (u64 >= (U64_LITERAL(1)<<32)) {
310 u64 >>= 32;
311 r = 32;
313 if (u64 >= (U64_LITERAL(1)<<16)) {
314 u64 >>= 16;
315 r += 16;
317 if (u64 >= (U64_LITERAL(1)<<8)) {
318 u64 >>= 8;
319 r += 8;
321 if (u64 >= (U64_LITERAL(1)<<4)) {
322 u64 >>= 4;
323 r += 4;
325 if (u64 >= (U64_LITERAL(1)<<2)) {
326 u64 >>= 2;
327 r += 2;
329 if (u64 >= (U64_LITERAL(1)<<1)) {
330 u64 >>= 1;
331 r += 1;
333 return r;
336 /** Return the power of 2 closest to <b>u64</b>. */
337 uint64_t
338 round_to_power_of_2(uint64_t u64)
340 int lg2 = tor_log2(u64);
341 uint64_t low = U64_LITERAL(1) << lg2, high = U64_LITERAL(1) << (lg2+1);
342 if (high - u64 < u64 - low)
343 return high;
344 else
345 return low;
348 /* =====
349 * String manipulation
350 * ===== */
352 /** Remove from the string <b>s</b> every character which appears in
353 * <b>strip</b>. */
354 void
355 tor_strstrip(char *s, const char *strip)
357 char *read = s;
358 while (*read) {
359 if (strchr(strip, *read)) {
360 ++read;
361 } else {
362 *s++ = *read++;
365 *s = '\0';
368 /** Return a pointer to a NUL-terminated hexadecimal string encoding
369 * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
370 * result does not need to be deallocated, but repeated calls to
371 * hex_str will trash old results.
373 const char *
374 hex_str(const char *from, size_t fromlen)
376 static char buf[65];
377 if (fromlen>(sizeof(buf)-1)/2)
378 fromlen = (sizeof(buf)-1)/2;
379 base16_encode(buf,sizeof(buf),from,fromlen);
380 return buf;
383 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
384 * lowercase. */
385 void
386 tor_strlower(char *s)
388 while (*s) {
389 *s = TOR_TOLOWER(*s);
390 ++s;
394 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
395 * lowercase. */
396 void
397 tor_strupper(char *s)
399 while (*s) {
400 *s = TOR_TOUPPER(*s);
401 ++s;
405 /** Return 1 if every character in <b>s</b> is printable, else return 0.
408 tor_strisprint(const char *s)
410 while (*s) {
411 if (!TOR_ISPRINT(*s))
412 return 0;
413 s++;
415 return 1;
418 /** Return 1 if no character in <b>s</b> is uppercase, else return 0.
421 tor_strisnonupper(const char *s)
423 while (*s) {
424 if (TOR_ISUPPER(*s))
425 return 0;
426 s++;
428 return 1;
431 /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
432 * strcmp.
435 strcmpstart(const char *s1, const char *s2)
437 size_t n = strlen(s2);
438 return strncmp(s1, s2, n);
441 /** Compare the s1_len-byte string <b>s1</b> with <b>s2</b>,
442 * without depending on a terminating nul in s1. Sorting order is first by
443 * length, then lexically; return values are as for strcmp.
446 strcmp_len(const char *s1, const char *s2, size_t s1_len)
448 size_t s2_len = strlen(s2);
449 if (s1_len < s2_len)
450 return -1;
451 if (s1_len > s2_len)
452 return 1;
453 return memcmp(s1, s2, s2_len);
456 /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
457 * strcasecmp.
460 strcasecmpstart(const char *s1, const char *s2)
462 size_t n = strlen(s2);
463 return strncasecmp(s1, s2, n);
466 /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
467 * strcmp.
470 strcmpend(const char *s1, const char *s2)
472 size_t n1 = strlen(s1), n2 = strlen(s2);
473 if (n2>n1)
474 return strcmp(s1,s2);
475 else
476 return strncmp(s1+(n1-n2), s2, n2);
479 /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
480 * strcasecmp.
483 strcasecmpend(const char *s1, const char *s2)
485 size_t n1 = strlen(s1), n2 = strlen(s2);
486 if (n2>n1) /* then they can't be the same; figure out which is bigger */
487 return strcasecmp(s1,s2);
488 else
489 return strncasecmp(s1+(n1-n2), s2, n2);
492 /** Compare the value of the string <b>prefix</b> with the start of the
493 * <b>memlen</b>-byte memory chunk at <b>mem</b>. Return as for strcmp.
495 * [As memcmp(mem, prefix, strlen(prefix)) but returns -1 if memlen is less
496 * than strlen(prefix).]
499 memcmpstart(const void *mem, size_t memlen,
500 const char *prefix)
502 size_t plen = strlen(prefix);
503 if (memlen < plen)
504 return -1;
505 return memcmp(mem, prefix, plen);
508 /** Return a pointer to the first char of s that is not whitespace and
509 * not a comment, or to the terminating NUL if no such character exists.
511 const char *
512 eat_whitespace(const char *s)
514 tor_assert(s);
516 while (1) {
517 switch (*s) {
518 case '\0':
519 default:
520 return s;
521 case ' ':
522 case '\t':
523 case '\n':
524 case '\r':
525 ++s;
526 break;
527 case '#':
528 ++s;
529 while (*s && *s != '\n')
530 ++s;
535 /** Return a pointer to the first char of s that is not whitespace and
536 * not a comment, or to the terminating NUL if no such character exists.
538 const char *
539 eat_whitespace_eos(const char *s, const char *eos)
541 tor_assert(s);
542 tor_assert(eos && s <= eos);
544 while (s < eos) {
545 switch (*s) {
546 case '\0':
547 default:
548 return s;
549 case ' ':
550 case '\t':
551 case '\n':
552 case '\r':
553 ++s;
554 break;
555 case '#':
556 ++s;
557 while (s < eos && *s && *s != '\n')
558 ++s;
561 return s;
564 /** Return a pointer to the first char of s that is not a space or a tab
565 * or a \\r, or to the terminating NUL if no such character exists. */
566 const char *
567 eat_whitespace_no_nl(const char *s)
569 while (*s == ' ' || *s == '\t' || *s == '\r')
570 ++s;
571 return s;
574 /** As eat_whitespace_no_nl, but stop at <b>eos</b> whether we have
575 * found a non-whitespace character or not. */
576 const char *
577 eat_whitespace_eos_no_nl(const char *s, const char *eos)
579 while (s < eos && (*s == ' ' || *s == '\t' || *s == '\r'))
580 ++s;
581 return s;
584 /** Return a pointer to the first char of s that is whitespace or <b>#</b>,
585 * or to the terminating NUL if no such character exists.
587 const char *
588 find_whitespace(const char *s)
590 /* tor_assert(s); */
591 while (1) {
592 switch (*s)
594 case '\0':
595 case '#':
596 case ' ':
597 case '\r':
598 case '\n':
599 case '\t':
600 return s;
601 default:
602 ++s;
607 /** As find_whitespace, but stop at <b>eos</b> whether we have found a
608 * whitespace or not. */
609 const char *
610 find_whitespace_eos(const char *s, const char *eos)
612 /* tor_assert(s); */
613 while (s < eos) {
614 switch (*s)
616 case '\0':
617 case '#':
618 case ' ':
619 case '\r':
620 case '\n':
621 case '\t':
622 return s;
623 default:
624 ++s;
627 return s;
630 /** Return true iff the 'len' bytes at 'mem' are all zero. */
632 tor_mem_is_zero(const char *mem, size_t len)
634 static const char ZERO[] = {
635 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,
637 while (len >= sizeof(ZERO)) {
638 if (memcmp(mem, ZERO, sizeof(ZERO)))
639 return 0;
640 len -= sizeof(ZERO);
641 mem += sizeof(ZERO);
643 /* Deal with leftover bytes. */
644 if (len)
645 return ! memcmp(mem, ZERO, len);
647 return 1;
650 /** Return true iff the DIGEST_LEN bytes in digest are all zero. */
652 tor_digest_is_zero(const char *digest)
654 return tor_mem_is_zero(digest, DIGEST_LEN);
657 /* Helper: common code to check whether the result of a strtol or strtoul or
658 * strtoll is correct. */
659 #define CHECK_STRTOX_RESULT() \
660 /* Was at least one character converted? */ \
661 if (endptr == s) \
662 goto err; \
663 /* Were there unexpected unconverted characters? */ \
664 if (!next && *endptr) \
665 goto err; \
666 /* Is r within limits? */ \
667 if (r < min || r > max) \
668 goto err; \
669 if (ok) *ok = 1; \
670 if (next) *next = endptr; \
671 return r; \
672 err: \
673 if (ok) *ok = 0; \
674 if (next) *next = endptr; \
675 return 0
677 /** Extract a long from the start of s, in the given numeric base. If
678 * there is unconverted data and next is provided, set *next to the
679 * first unconverted character. An error has occurred if no characters
680 * are converted; or if there are unconverted characters and next is NULL; or
681 * if the parsed value is not between min and max. When no error occurs,
682 * return the parsed value and set *ok (if provided) to 1. When an error
683 * occurs, return 0 and set *ok (if provided) to 0.
685 long
686 tor_parse_long(const char *s, int base, long min, long max,
687 int *ok, char **next)
689 char *endptr;
690 long r;
692 r = strtol(s, &endptr, base);
693 CHECK_STRTOX_RESULT();
696 /** As tor_parse_long(), but return an unsigned long. */
697 unsigned long
698 tor_parse_ulong(const char *s, int base, unsigned long min,
699 unsigned long max, int *ok, char **next)
701 char *endptr;
702 unsigned long r;
704 r = strtoul(s, &endptr, base);
705 CHECK_STRTOX_RESULT();
708 /** As tor_parse_log, but return a unit64_t. Only base 10 is guaranteed to
709 * work for now. */
710 uint64_t
711 tor_parse_uint64(const char *s, int base, uint64_t min,
712 uint64_t max, int *ok, char **next)
714 char *endptr;
715 uint64_t r;
717 #ifdef HAVE_STRTOULL
718 r = (uint64_t)strtoull(s, &endptr, base);
719 #elif defined(MS_WINDOWS)
720 #if defined(_MSC_VER) && _MSC_VER < 1300
721 tor_assert(base <= 10);
722 r = (uint64_t)_atoi64(s);
723 endptr = (char*)s;
724 while (TOR_ISSPACE(*endptr)) endptr++;
725 while (TOR_ISDIGIT(*endptr)) endptr++;
726 #else
727 r = (uint64_t)_strtoui64(s, &endptr, base);
728 #endif
729 #elif SIZEOF_LONG == 8
730 r = (uint64_t)strtoul(s, &endptr, base);
731 #else
732 #error "I don't know how to parse 64-bit numbers."
733 #endif
735 CHECK_STRTOX_RESULT();
738 /** Encode the <b>srclen</b> bytes at <b>src</b> in a NUL-terminated,
739 * uppercase hexadecimal string; store it in the <b>destlen</b>-byte buffer
740 * <b>dest</b>.
742 void
743 base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
745 const char *end;
746 char *cp;
748 tor_assert(destlen >= srclen*2+1);
749 tor_assert(destlen < SIZE_T_CEILING);
751 cp = dest;
752 end = src+srclen;
753 while (src<end) {
754 *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) >> 4 ];
755 *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) & 0xf ];
756 ++src;
758 *cp = '\0';
761 /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
762 static INLINE int
763 _hex_decode_digit(char c)
765 switch (c) {
766 case '0': return 0;
767 case '1': return 1;
768 case '2': return 2;
769 case '3': return 3;
770 case '4': return 4;
771 case '5': return 5;
772 case '6': return 6;
773 case '7': return 7;
774 case '8': return 8;
775 case '9': return 9;
776 case 'A': case 'a': return 10;
777 case 'B': case 'b': return 11;
778 case 'C': case 'c': return 12;
779 case 'D': case 'd': return 13;
780 case 'E': case 'e': return 14;
781 case 'F': case 'f': return 15;
782 default:
783 return -1;
787 /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
789 hex_decode_digit(char c)
791 return _hex_decode_digit(c);
794 /** Given a hexadecimal string of <b>srclen</b> bytes in <b>src</b>, decode it
795 * and store the result in the <b>destlen</b>-byte buffer at <b>dest</b>.
796 * Return 0 on success, -1 on failure. */
798 base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
800 const char *end;
802 int v1,v2;
803 if ((srclen % 2) != 0)
804 return -1;
805 if (destlen < srclen/2 || destlen > SIZE_T_CEILING)
806 return -1;
807 end = src+srclen;
808 while (src<end) {
809 v1 = _hex_decode_digit(*src);
810 v2 = _hex_decode_digit(*(src+1));
811 if (v1<0||v2<0)
812 return -1;
813 *(uint8_t*)dest = (v1<<4)|v2;
814 ++dest;
815 src+=2;
817 return 0;
820 /** Allocate and return a new string representing the contents of <b>s</b>,
821 * surrounded by quotes and using standard C escapes.
823 * Generally, we use this for logging values that come in over the network to
824 * keep them from tricking users, and for sending certain values to the
825 * controller.
827 * We trust values from the resolver, OS, configuration file, and command line
828 * to not be maliciously ill-formed. We validate incoming routerdescs and
829 * SOCKS requests and addresses from BEGIN cells as they're parsed;
830 * afterwards, we trust them as non-malicious.
832 char *
833 esc_for_log(const char *s)
835 const char *cp;
836 char *result, *outp;
837 size_t len = 3;
838 if (!s) {
839 return tor_strdup("");
842 for (cp = s; *cp; ++cp) {
843 switch (*cp) {
844 case '\\':
845 case '\"':
846 case '\'':
847 len += 2;
848 break;
849 default:
850 if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127)
851 ++len;
852 else
853 len += 4;
854 break;
858 result = outp = tor_malloc(len);
859 *outp++ = '\"';
860 for (cp = s; *cp; ++cp) {
861 switch (*cp) {
862 case '\\':
863 case '\"':
864 case '\'':
865 *outp++ = '\\';
866 *outp++ = *cp;
867 break;
868 case '\n':
869 *outp++ = '\\';
870 *outp++ = 'n';
871 break;
872 case '\t':
873 *outp++ = '\\';
874 *outp++ = 't';
875 break;
876 case '\r':
877 *outp++ = '\\';
878 *outp++ = 'r';
879 break;
880 default:
881 if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127) {
882 *outp++ = *cp;
883 } else {
884 tor_snprintf(outp, 5, "\\%03o", (int)(uint8_t) *cp);
885 outp += 4;
887 break;
891 *outp++ = '\"';
892 *outp++ = 0;
894 return result;
897 /** Allocate and return a new string representing the contents of <b>s</b>,
898 * surrounded by quotes and using standard C escapes.
900 * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
901 * thread. Also, each call invalidates the last-returned value, so don't
902 * try log_warn(LD_GENERAL, "%s %s", escaped(a), escaped(b));
904 const char *
905 escaped(const char *s)
907 static char *_escaped_val = NULL;
908 if (_escaped_val)
909 tor_free(_escaped_val);
911 if (s)
912 _escaped_val = esc_for_log(s);
913 else
914 _escaped_val = NULL;
916 return _escaped_val;
919 /** Rudimentary string wrapping code: given a un-wrapped <b>string</b> (no
920 * newlines!), break the string into newline-terminated lines of no more than
921 * <b>width</b> characters long (not counting newline) and insert them into
922 * <b>out</b> in order. Precede the first line with prefix0, and subsequent
923 * lines with prefixRest.
925 /* This uses a stupid greedy wrapping algorithm right now:
926 * - For each line:
927 * - Try to fit as much stuff as possible, but break on a space.
928 * - If the first "word" of the line will extend beyond the allowable
929 * width, break the word at the end of the width.
931 void
932 wrap_string(smartlist_t *out, const char *string, size_t width,
933 const char *prefix0, const char *prefixRest)
935 size_t p0Len, pRestLen, pCurLen;
936 const char *eos, *prefixCur;
937 tor_assert(out);
938 tor_assert(string);
939 tor_assert(width);
940 if (!prefix0)
941 prefix0 = "";
942 if (!prefixRest)
943 prefixRest = "";
945 p0Len = strlen(prefix0);
946 pRestLen = strlen(prefixRest);
947 tor_assert(width > p0Len && width > pRestLen);
948 eos = strchr(string, '\0');
949 tor_assert(eos);
950 pCurLen = p0Len;
951 prefixCur = prefix0;
953 while ((eos-string)+pCurLen > width) {
954 const char *eol = string + width - pCurLen;
955 while (eol > string && *eol != ' ')
956 --eol;
957 /* eol is now the last space that can fit, or the start of the string. */
958 if (eol > string) {
959 size_t line_len = (eol-string) + pCurLen + 2;
960 char *line = tor_malloc(line_len);
961 memcpy(line, prefixCur, pCurLen);
962 memcpy(line+pCurLen, string, eol-string);
963 line[line_len-2] = '\n';
964 line[line_len-1] = '\0';
965 smartlist_add(out, line);
966 string = eol + 1;
967 } else {
968 size_t line_len = width + 2;
969 char *line = tor_malloc(line_len);
970 memcpy(line, prefixCur, pCurLen);
971 memcpy(line+pCurLen, string, width - pCurLen);
972 line[line_len-2] = '\n';
973 line[line_len-1] = '\0';
974 smartlist_add(out, line);
975 string += width-pCurLen;
977 prefixCur = prefixRest;
978 pCurLen = pRestLen;
981 if (string < eos) {
982 size_t line_len = (eos-string) + pCurLen + 2;
983 char *line = tor_malloc(line_len);
984 memcpy(line, prefixCur, pCurLen);
985 memcpy(line+pCurLen, string, eos-string);
986 line[line_len-2] = '\n';
987 line[line_len-1] = '\0';
988 smartlist_add(out, line);
992 /* =====
993 * Time
994 * ===== */
996 /** Return the number of microseconds elapsed between *start and *end.
998 long
999 tv_udiff(const struct timeval *start, const struct timeval *end)
1001 long udiff;
1002 long secdiff = end->tv_sec - start->tv_sec;
1004 if (labs(secdiff+1) > LONG_MAX/1000000) {
1005 log_warn(LD_GENERAL, "comparing times too far apart.");
1006 return LONG_MAX;
1009 udiff = secdiff*1000000L + (end->tv_usec - start->tv_usec);
1010 return udiff;
1013 /** Yield true iff <b>y</b> is a leap-year. */
1014 #define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
1015 /** Helper: Return the number of leap-days between Jan 1, y1 and Jan 1, y2. */
1016 static int
1017 n_leapdays(int y1, int y2)
1019 --y1;
1020 --y2;
1021 return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
1023 /** Number of days per month in non-leap year; used by tor_timegm. */
1024 static const int days_per_month[] =
1025 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1027 /** Return a time_t given a struct tm. The result is given in GMT, and
1028 * does not account for leap seconds.
1030 time_t
1031 tor_timegm(struct tm *tm)
1033 /* This is a pretty ironclad timegm implementation, snarfed from Python2.2.
1034 * It's way more brute-force than fiddling with tzset().
1036 time_t year, days, hours, minutes, seconds;
1037 int i;
1038 year = tm->tm_year + 1900;
1039 if (year < 1970 || tm->tm_mon < 0 || tm->tm_mon > 11) {
1040 log_warn(LD_BUG, "Out-of-range argument to tor_timegm");
1041 return -1;
1043 tor_assert(year < INT_MAX);
1044 days = 365 * (year-1970) + n_leapdays(1970,(int)year);
1045 for (i = 0; i < tm->tm_mon; ++i)
1046 days += days_per_month[i];
1047 if (tm->tm_mon > 1 && IS_LEAPYEAR(year))
1048 ++days;
1049 days += tm->tm_mday - 1;
1050 hours = days*24 + tm->tm_hour;
1052 minutes = hours*60 + tm->tm_min;
1053 seconds = minutes*60 + tm->tm_sec;
1054 return seconds;
1057 /* strftime is locale-specific, so we need to replace those parts */
1059 /** A c-locale array of 3-letter names of weekdays, starting with Sun. */
1060 static const char *WEEKDAY_NAMES[] =
1061 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
1062 /** A c-locale array of 3-letter names of months, starting with Jan. */
1063 static const char *MONTH_NAMES[] =
1064 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1065 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1067 /** Set <b>buf</b> to the RFC1123 encoding of the GMT value of <b>t</b>.
1068 * The buffer must be at least RFC1123_TIME_LEN+1 bytes long.
1070 * (RFC1123 format is Fri, 29 Sep 2006 15:54:20 GMT)
1072 void
1073 format_rfc1123_time(char *buf, time_t t)
1075 struct tm tm;
1077 tor_gmtime_r(&t, &tm);
1079 strftime(buf, RFC1123_TIME_LEN+1, "___, %d ___ %Y %H:%M:%S GMT", &tm);
1080 tor_assert(tm.tm_wday >= 0);
1081 tor_assert(tm.tm_wday <= 6);
1082 memcpy(buf, WEEKDAY_NAMES[tm.tm_wday], 3);
1083 tor_assert(tm.tm_wday >= 0);
1084 tor_assert(tm.tm_mon <= 11);
1085 memcpy(buf+8, MONTH_NAMES[tm.tm_mon], 3);
1088 /** Parse the the RFC1123 encoding of some time (in GMT) from <b>buf</b>,
1089 * and store the result in *<b>t</b>.
1091 * Return 0 on success, -1 on failure.
1094 parse_rfc1123_time(const char *buf, time_t *t)
1096 struct tm tm;
1097 char month[4];
1098 char weekday[4];
1099 int i, m;
1100 unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
1102 if (strlen(buf) != RFC1123_TIME_LEN)
1103 return -1;
1104 memset(&tm, 0, sizeof(tm));
1105 if (tor_sscanf(buf, "%3s, %2u %3s %u %2u:%2u:%2u GMT", weekday,
1106 &tm_mday, month, &tm_year, &tm_hour,
1107 &tm_min, &tm_sec) < 7) {
1108 char *esc = esc_for_log(buf);
1109 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
1110 tor_free(esc);
1111 return -1;
1113 if (tm_mday > 31 || tm_hour > 23 || tm_min > 59 || tm_sec > 61) {
1114 char *esc = esc_for_log(buf);
1115 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
1116 tor_free(esc);
1117 return -1;
1119 tm.tm_mday = (int)tm_mday;
1120 tm.tm_year = (int)tm_year;
1121 tm.tm_hour = (int)tm_hour;
1122 tm.tm_min = (int)tm_min;
1123 tm.tm_sec = (int)tm_sec;
1125 m = -1;
1126 for (i = 0; i < 12; ++i) {
1127 if (!strcmp(month, MONTH_NAMES[i])) {
1128 m = i;
1129 break;
1132 if (m<0) {
1133 char *esc = esc_for_log(buf);
1134 log_warn(LD_GENERAL, "Got invalid RFC1123 time %s: No such month", esc);
1135 tor_free(esc);
1136 return -1;
1138 tm.tm_mon = m;
1140 if (tm.tm_year < 1970) {
1141 char *esc = esc_for_log(buf);
1142 log_warn(LD_GENERAL,
1143 "Got invalid RFC1123 time %s. (Before 1970)", esc);
1144 tor_free(esc);
1145 return -1;
1147 tm.tm_year -= 1900;
1149 *t = tor_timegm(&tm);
1150 return 0;
1153 /** Set <b>buf</b> to the ISO8601 encoding of the local value of <b>t</b>.
1154 * The buffer must be at least ISO_TIME_LEN+1 bytes long.
1156 * (ISO8601 format is 2006-10-29 10:57:20)
1158 void
1159 format_local_iso_time(char *buf, time_t t)
1161 struct tm tm;
1162 strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_localtime_r(&t, &tm));
1165 /** Set <b>buf</b> to the ISO8601 encoding of the GMT value of <b>t</b>.
1166 * The buffer must be at least ISO_TIME_LEN+1 bytes long.
1168 void
1169 format_iso_time(char *buf, time_t t)
1171 struct tm tm;
1172 strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t, &tm));
1175 /** Given an ISO-formatted UTC time value (after the epoch) in <b>cp</b>,
1176 * parse it and store its value in *<b>t</b>. Return 0 on success, -1 on
1177 * failure. Ignore extraneous stuff in <b>cp</b> separated by whitespace from
1178 * the end of the time string. */
1180 parse_iso_time(const char *cp, time_t *t)
1182 struct tm st_tm;
1183 unsigned int year=0, month=0, day=0, hour=100, minute=100, second=100;
1184 if (tor_sscanf(cp, "%u-%2u-%2u %2u:%2u:%2u", &year, &month,
1185 &day, &hour, &minute, &second) < 6) {
1186 char *esc = esc_for_log(cp);
1187 log_warn(LD_GENERAL, "ISO time %s was unparseable", esc);
1188 tor_free(esc);
1189 return -1;
1191 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
1192 hour > 23 || minute > 59 || second > 61) {
1193 char *esc = esc_for_log(cp);
1194 log_warn(LD_GENERAL, "ISO time %s was nonsensical", esc);
1195 tor_free(esc);
1196 return -1;
1198 st_tm.tm_year = year-1900;
1199 st_tm.tm_mon = month-1;
1200 st_tm.tm_mday = day;
1201 st_tm.tm_hour = hour;
1202 st_tm.tm_min = minute;
1203 st_tm.tm_sec = second;
1205 if (st_tm.tm_year < 70) {
1206 char *esc = esc_for_log(cp);
1207 log_warn(LD_GENERAL, "Got invalid ISO time %s. (Before 1970)", esc);
1208 tor_free(esc);
1209 return -1;
1211 *t = tor_timegm(&st_tm);
1212 return 0;
1215 /** Given a <b>date</b> in one of the three formats allowed by HTTP (ugh),
1216 * parse it into <b>tm</b>. Return 0 on success, negative on failure. */
1218 parse_http_time(const char *date, struct tm *tm)
1220 const char *cp;
1221 char month[4];
1222 char wkday[4];
1223 int i;
1224 unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
1226 tor_assert(tm);
1227 memset(tm, 0, sizeof(*tm));
1229 /* First, try RFC1123 or RFC850 format: skip the weekday. */
1230 if ((cp = strchr(date, ','))) {
1231 ++cp;
1232 if (tor_sscanf(date, "%2u %3s %4u %2u:%2u:%2u GMT",
1233 &tm_mday, month, &tm_year,
1234 &tm_hour, &tm_min, &tm_sec) == 6) {
1235 /* rfc1123-date */
1236 tm_year -= 1900;
1237 } else if (tor_sscanf(date, "%2u-%3s-%2u %2u:%2u:%2u GMT",
1238 &tm_mday, month, &tm_year,
1239 &tm_hour, &tm_min, &tm_sec) == 6) {
1240 /* rfc850-date */
1241 } else {
1242 return -1;
1244 } else {
1245 /* No comma; possibly asctime() format. */
1246 if (tor_sscanf(date, "%3s %3s %2u %2u:%2u:%2u %4u",
1247 wkday, month, &tm_mday,
1248 &tm_hour, &tm_min, &tm_sec, &tm_year) == 7) {
1249 tm_year -= 1900;
1250 } else {
1251 return -1;
1254 tm->tm_mday = (int)tm_mday;
1255 tm->tm_year = (int)tm_year;
1256 tm->tm_hour = (int)tm_hour;
1257 tm->tm_min = (int)tm_min;
1258 tm->tm_sec = (int)tm_sec;
1260 month[3] = '\0';
1261 /* Okay, now decode the month. */
1262 for (i = 0; i < 12; ++i) {
1263 if (!strcasecmp(MONTH_NAMES[i], month)) {
1264 tm->tm_mon = i+1;
1268 if (tm->tm_year < 0 ||
1269 tm->tm_mon < 1 || tm->tm_mon > 12 ||
1270 tm->tm_mday < 0 || tm->tm_mday > 31 ||
1271 tm->tm_hour < 0 || tm->tm_hour > 23 ||
1272 tm->tm_min < 0 || tm->tm_min > 59 ||
1273 tm->tm_sec < 0 || tm->tm_sec > 61)
1274 return -1; /* Out of range, or bad month. */
1276 return 0;
1279 /** Given an <b>interval</b> in seconds, try to write it to the
1280 * <b>out_len</b>-byte buffer in <b>out</b> in a human-readable form.
1281 * Return 0 on success, -1 on failure.
1284 format_time_interval(char *out, size_t out_len, long interval)
1286 /* We only report seconds if there's no hours. */
1287 long sec = 0, min = 0, hour = 0, day = 0;
1288 if (interval < 0)
1289 interval = -interval;
1291 if (interval >= 86400) {
1292 day = interval / 86400;
1293 interval %= 86400;
1295 if (interval >= 3600) {
1296 hour = interval / 3600;
1297 interval %= 3600;
1299 if (interval >= 60) {
1300 min = interval / 60;
1301 interval %= 60;
1303 sec = interval;
1305 if (day) {
1306 return tor_snprintf(out, out_len, "%ld days, %ld hours, %ld minutes",
1307 day, hour, min);
1308 } else if (hour) {
1309 return tor_snprintf(out, out_len, "%ld hours, %ld minutes", hour, min);
1310 } else if (min) {
1311 return tor_snprintf(out, out_len, "%ld minutes, %ld seconds", min, sec);
1312 } else {
1313 return tor_snprintf(out, out_len, "%ld seconds", sec);
1317 /* =====
1318 * Cached time
1319 * ===== */
1321 #ifndef TIME_IS_FAST
1322 /** Cached estimate of the current time. Updated around once per second;
1323 * may be a few seconds off if we are really busy. This is a hack to avoid
1324 * calling time(NULL) (which not everybody has optimized) on critical paths.
1326 static time_t cached_approx_time = 0;
1328 /** Return a cached estimate of the current time from when
1329 * update_approx_time() was last called. This is a hack to avoid calling
1330 * time(NULL) on critical paths: please do not even think of calling it
1331 * anywhere else. */
1332 time_t
1333 approx_time(void)
1335 return cached_approx_time;
1338 /** Update the cached estimate of the current time. This function SHOULD be
1339 * called once per second, and MUST be called before the first call to
1340 * get_approx_time. */
1341 void
1342 update_approx_time(time_t now)
1344 cached_approx_time = now;
1346 #endif
1348 /* =====
1349 * Fuzzy time
1350 * XXXX022 Use this consistently or rip most of it out.
1351 * ===== */
1353 /* In a perfect world, everybody would run NTP, and NTP would be perfect, so
1354 * if we wanted to know "Is the current time before time X?" we could just say
1355 * "time(NULL) < X".
1357 * But unfortunately, many users are running Tor in an imperfect world, on
1358 * even more imperfect computers. Hence, we need to track time oddly. We
1359 * model the user's computer as being "skewed" from accurate time by
1360 * -<b>ftime_skew</b> seconds, such that our best guess of the current time is
1361 * time(NULL)+ftime_skew. We also assume that our measurements of time may
1362 * have up to <b>ftime_slop</b> seconds of inaccuracy; IOW, our window of
1363 * estimate for the current time is now + ftime_skew +/- ftime_slop.
1365 /** Our current estimate of our skew, such that we think the current time is
1366 * closest to time(NULL)+ftime_skew. */
1367 static int ftime_skew = 0;
1368 /** Tolerance during time comparisons, in seconds. */
1369 static int ftime_slop = 60;
1370 /** Set the largest amount of sloppiness we'll allow in fuzzy time
1371 * comparisons. */
1372 void
1373 ftime_set_maximum_sloppiness(int seconds)
1375 tor_assert(seconds >= 0);
1376 ftime_slop = seconds;
1378 /** Set the amount by which we believe our system clock to differ from
1379 * real time. */
1380 void
1381 ftime_set_estimated_skew(int seconds)
1383 ftime_skew = seconds;
1385 #if 0
1386 void
1387 ftime_get_window(time_t now, ftime_t *ft_out)
1389 ft_out->earliest = now + ftime_skew - ftime_slop;
1390 ft_out->latest = now + ftime_skew + ftime_slop;
1392 #endif
1393 /** Return true iff we think that <b>now</b> might be after <b>when</b>. */
1395 ftime_maybe_after(time_t now, time_t when)
1397 /* It may be after when iff the latest possible current time is after when */
1398 return (now + ftime_skew + ftime_slop) >= when;
1400 /** Return true iff we think that <b>now</b> might be before <b>when</b>. */
1402 ftime_maybe_before(time_t now, time_t when)
1404 /* It may be before when iff the earliest possible current time is before */
1405 return (now + ftime_skew - ftime_slop) < when;
1407 /** Return true if we think that <b>now</b> is definitely after <b>when</b>. */
1409 ftime_definitely_after(time_t now, time_t when)
1411 /* It is definitely after when if the earliest time it could be is still
1412 * after when. */
1413 return (now + ftime_skew - ftime_slop) >= when;
1415 /** Return true if we think that <b>now</b> is definitely before <b>when</b>.
1418 ftime_definitely_before(time_t now, time_t when)
1420 /* It is definitely before when if the latest time it could be is still
1421 * before when. */
1422 return (now + ftime_skew + ftime_slop) < when;
1425 /* =====
1426 * File helpers
1427 * ===== */
1429 /** Write <b>count</b> bytes from <b>buf</b> to <b>fd</b>. <b>isSocket</b>
1430 * must be 1 if fd was returned by socket() or accept(), and 0 if fd
1431 * was returned by open(). Return the number of bytes written, or -1
1432 * on error. Only use if fd is a blocking fd. */
1433 ssize_t
1434 write_all(int fd, const char *buf, size_t count, int isSocket)
1436 size_t written = 0;
1437 ssize_t result;
1438 tor_assert(count < SSIZE_T_MAX);
1440 while (written != count) {
1441 if (isSocket)
1442 result = tor_socket_send(fd, buf+written, count-written, 0);
1443 else
1444 result = write(fd, buf+written, count-written);
1445 if (result<0)
1446 return -1;
1447 written += result;
1449 return (ssize_t)count;
1452 /** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes
1453 * or reach the end of the file. <b>isSocket</b> must be 1 if fd
1454 * was returned by socket() or accept(), and 0 if fd was returned by
1455 * open(). Return the number of bytes read, or -1 on error. Only use
1456 * if fd is a blocking fd. */
1457 ssize_t
1458 read_all(int fd, char *buf, size_t count, int isSocket)
1460 size_t numread = 0;
1461 ssize_t result;
1463 if (count > SIZE_T_CEILING || count > SSIZE_T_MAX)
1464 return -1;
1466 while (numread != count) {
1467 if (isSocket)
1468 result = tor_socket_recv(fd, buf+numread, count-numread, 0);
1469 else
1470 result = read(fd, buf+numread, count-numread);
1471 if (result<0)
1472 return -1;
1473 else if (result == 0)
1474 break;
1475 numread += result;
1477 return (ssize_t)numread;
1481 * Filesystem operations.
1484 /** Clean up <b>name</b> so that we can use it in a call to "stat". On Unix,
1485 * we do nothing. On Windows, we remove a trailing slash, unless the path is
1486 * the root of a disk. */
1487 static void
1488 clean_name_for_stat(char *name)
1490 #ifdef MS_WINDOWS
1491 size_t len = strlen(name);
1492 if (!len)
1493 return;
1494 if (name[len-1]=='\\' || name[len-1]=='/') {
1495 if (len == 1 || (len==3 && name[1]==':'))
1496 return;
1497 name[len-1]='\0';
1499 #else
1500 (void)name;
1501 #endif
1504 /** Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
1505 * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
1506 * directory. On FN_ERROR, sets errno. */
1507 file_status_t
1508 file_status(const char *fname)
1510 struct stat st;
1511 char *f;
1512 int r;
1513 f = tor_strdup(fname);
1514 clean_name_for_stat(f);
1515 r = stat(f, &st);
1516 tor_free(f);
1517 if (r) {
1518 if (errno == ENOENT) {
1519 return FN_NOENT;
1521 return FN_ERROR;
1523 if (st.st_mode & S_IFDIR)
1524 return FN_DIR;
1525 else if (st.st_mode & S_IFREG)
1526 return FN_FILE;
1527 else
1528 return FN_ERROR;
1531 /** Check whether dirname exists and is private. If yes return 0. If
1532 * it does not exist, and check==CPD_CREATE is set, try to create it
1533 * and return 0 on success. If it does not exist, and
1534 * check==CPD_CHECK, and we think we can create it, return 0. Else
1535 * return -1. */
1537 check_private_dir(const char *dirname, cpd_check_t check)
1539 int r;
1540 struct stat st;
1541 char *f;
1542 tor_assert(dirname);
1543 f = tor_strdup(dirname);
1544 clean_name_for_stat(f);
1545 r = stat(f, &st);
1546 tor_free(f);
1547 if (r) {
1548 if (errno != ENOENT) {
1549 log(LOG_WARN, LD_FS, "Directory %s cannot be read: %s", dirname,
1550 strerror(errno));
1551 return -1;
1553 if (check == CPD_NONE) {
1554 log(LOG_WARN, LD_FS, "Directory %s does not exist.", dirname);
1555 return -1;
1556 } else if (check == CPD_CREATE) {
1557 log_info(LD_GENERAL, "Creating directory %s", dirname);
1558 #ifdef MS_WINDOWS
1559 r = mkdir(dirname);
1560 #else
1561 r = mkdir(dirname, 0700);
1562 #endif
1563 if (r) {
1564 log(LOG_WARN, LD_FS, "Error creating directory %s: %s", dirname,
1565 strerror(errno));
1566 return -1;
1569 /* XXXX In the case where check==CPD_CHECK, we should look at the
1570 * parent directory a little harder. */
1571 return 0;
1573 if (!(st.st_mode & S_IFDIR)) {
1574 log(LOG_WARN, LD_FS, "%s is not a directory", dirname);
1575 return -1;
1577 #ifndef MS_WINDOWS
1578 if (st.st_uid != getuid()) {
1579 struct passwd *pw = NULL;
1580 char *process_ownername = NULL;
1582 pw = getpwuid(getuid());
1583 process_ownername = pw ? tor_strdup(pw->pw_name) : tor_strdup("<unknown>");
1585 pw = getpwuid(st.st_uid);
1587 log(LOG_WARN, LD_FS, "%s is not owned by this user (%s, %d) but by "
1588 "%s (%d). Perhaps you are running Tor as the wrong user?",
1589 dirname, process_ownername, (int)getuid(),
1590 pw ? pw->pw_name : "<unknown>", (int)st.st_uid);
1592 tor_free(process_ownername);
1593 return -1;
1595 if (st.st_mode & 0077) {
1596 log(LOG_WARN, LD_FS, "Fixing permissions on directory %s", dirname);
1597 if (chmod(dirname, 0700)) {
1598 log(LOG_WARN, LD_FS, "Could not chmod directory %s: %s", dirname,
1599 strerror(errno));
1600 return -1;
1601 } else {
1602 return 0;
1605 #endif
1606 return 0;
1609 /** Create a file named <b>fname</b> with the contents <b>str</b>. Overwrite
1610 * the previous <b>fname</b> if possible. Return 0 on success, -1 on failure.
1612 * This function replaces the old file atomically, if possible. This
1613 * function, and all other functions in util.c that create files, create them
1614 * with mode 0600.
1617 write_str_to_file(const char *fname, const char *str, int bin)
1619 #ifdef MS_WINDOWS
1620 if (!bin && strchr(str, '\r')) {
1621 log_warn(LD_BUG,
1622 "We're writing a text string that already contains a CR.");
1624 #endif
1625 return write_bytes_to_file(fname, str, strlen(str), bin);
1628 /** Represents a file that we're writing to, with support for atomic commit:
1629 * we can write into a a temporary file, and either remove the file on
1630 * failure, or replace the original file on success. */
1631 struct open_file_t {
1632 char *tempname; /**< Name of the temporary file. */
1633 char *filename; /**< Name of the original file. */
1634 int rename_on_close; /**< Are we using the temporary file or not? */
1635 int fd; /**< fd for the open file. */
1636 FILE *stdio_file; /**< stdio wrapper for <b>fd</b>. */
1639 /** Try to start writing to the file in <b>fname</b>, passing the flags
1640 * <b>open_flags</b> to the open() syscall, creating the file (if needed) with
1641 * access value <b>mode</b>. If the O_APPEND flag is set, we append to the
1642 * original file. Otherwise, we open a new temporary file in the same
1643 * directory, and either replace the original or remove the temporary file
1644 * when we're done.
1646 * Return the fd for the newly opened file, and store working data in
1647 * *<b>data_out</b>. The caller should not close the fd manually:
1648 * instead, call finish_writing_to_file() or abort_writing_to_file().
1649 * Returns -1 on failure.
1651 * NOTE: When not appending, the flags O_CREAT and O_TRUNC are treated
1652 * as true and the flag O_EXCL is treated as false.
1654 * NOTE: Ordinarily, O_APPEND means "seek to the end of the file before each
1655 * write()". We don't do that.
1658 start_writing_to_file(const char *fname, int open_flags, int mode,
1659 open_file_t **data_out)
1661 size_t tempname_len = strlen(fname)+16;
1662 open_file_t *new_file = tor_malloc_zero(sizeof(open_file_t));
1663 const char *open_name;
1664 int append = 0;
1666 tor_assert(fname);
1667 tor_assert(data_out);
1668 #if (O_BINARY != 0 && O_TEXT != 0)
1669 tor_assert((open_flags & (O_BINARY|O_TEXT)) != 0);
1670 #endif
1671 new_file->fd = -1;
1672 tor_assert(tempname_len > strlen(fname)); /*check for overflow*/
1673 new_file->filename = tor_strdup(fname);
1674 if (open_flags & O_APPEND) {
1675 open_name = fname;
1676 new_file->rename_on_close = 0;
1677 append = 1;
1678 open_flags &= ~O_APPEND;
1679 } else {
1680 open_name = new_file->tempname = tor_malloc(tempname_len);
1681 if (tor_snprintf(new_file->tempname, tempname_len, "%s.tmp", fname)<0) {
1682 log(LOG_WARN, LD_GENERAL, "Failed to generate filename");
1683 goto err;
1685 /* We always replace an existing temporary file if there is one. */
1686 open_flags |= O_CREAT|O_TRUNC;
1687 open_flags &= ~O_EXCL;
1688 new_file->rename_on_close = 1;
1691 if ((new_file->fd = open(open_name, open_flags, mode)) < 0) {
1692 log(LOG_WARN, LD_FS, "Couldn't open \"%s\" (%s) for writing: %s",
1693 open_name, fname, strerror(errno));
1694 goto err;
1696 if (append) {
1697 if (tor_fd_seekend(new_file->fd) < 0) {
1698 log_warn(LD_FS, "Couldn't seek to end of file \"%s\": %s", open_name,
1699 strerror(errno));
1700 goto err;
1704 *data_out = new_file;
1706 return new_file->fd;
1708 err:
1709 if (new_file->fd >= 0)
1710 close(new_file->fd);
1711 *data_out = NULL;
1712 tor_free(new_file->filename);
1713 tor_free(new_file->tempname);
1714 tor_free(new_file);
1715 return -1;
1718 /** Given <b>file_data</b> from start_writing_to_file(), return a stdio FILE*
1719 * that can be used to write to the same file. The caller should not mix
1720 * stdio calls with non-stdio calls. */
1721 FILE *
1722 fdopen_file(open_file_t *file_data)
1724 tor_assert(file_data);
1725 if (file_data->stdio_file)
1726 return file_data->stdio_file;
1727 tor_assert(file_data->fd >= 0);
1728 if (!(file_data->stdio_file = fdopen(file_data->fd, "a"))) {
1729 log_warn(LD_FS, "Couldn't fdopen \"%s\" [%d]: %s", file_data->filename,
1730 file_data->fd, strerror(errno));
1732 return file_data->stdio_file;
1735 /** Combines start_writing_to_file with fdopen_file(): arguments are as
1736 * for start_writing_to_file, but */
1737 FILE *
1738 start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
1739 open_file_t **data_out)
1741 FILE *res;
1742 if (start_writing_to_file(fname, open_flags, mode, data_out)<0)
1743 return NULL;
1744 if (!(res = fdopen_file(*data_out))) {
1745 abort_writing_to_file(*data_out);
1746 *data_out = NULL;
1748 return res;
1751 /** Helper function: close and free the underlying file and memory in
1752 * <b>file_data</b>. If we were writing into a temporary file, then delete
1753 * that file (if abort_write is true) or replaces the target file with
1754 * the temporary file (if abort_write is false). */
1755 static int
1756 finish_writing_to_file_impl(open_file_t *file_data, int abort_write)
1758 int r = 0;
1759 tor_assert(file_data && file_data->filename);
1760 if (file_data->stdio_file) {
1761 if (fclose(file_data->stdio_file)) {
1762 log_warn(LD_FS, "Error closing \"%s\": %s", file_data->filename,
1763 strerror(errno));
1764 abort_write = r = -1;
1766 } else if (file_data->fd >= 0 && close(file_data->fd) < 0) {
1767 log_warn(LD_FS, "Error flushing \"%s\": %s", file_data->filename,
1768 strerror(errno));
1769 abort_write = r = -1;
1772 if (file_data->rename_on_close) {
1773 tor_assert(file_data->tempname && file_data->filename);
1774 if (abort_write) {
1775 unlink(file_data->tempname);
1776 } else {
1777 tor_assert(strcmp(file_data->filename, file_data->tempname));
1778 if (replace_file(file_data->tempname, file_data->filename)) {
1779 log_warn(LD_FS, "Error replacing \"%s\": %s", file_data->filename,
1780 strerror(errno));
1781 r = -1;
1786 tor_free(file_data->filename);
1787 tor_free(file_data->tempname);
1788 tor_free(file_data);
1790 return r;
1793 /** Finish writing to <b>file_data</b>: close the file handle, free memory as
1794 * needed, and if using a temporary file, replace the original file with
1795 * the temporary file. */
1797 finish_writing_to_file(open_file_t *file_data)
1799 return finish_writing_to_file_impl(file_data, 0);
1802 /** Finish writing to <b>file_data</b>: close the file handle, free memory as
1803 * needed, and if using a temporary file, delete it. */
1805 abort_writing_to_file(open_file_t *file_data)
1807 return finish_writing_to_file_impl(file_data, 1);
1810 /** Helper: given a set of flags as passed to open(2), open the file
1811 * <b>fname</b> and write all the sized_chunk_t structs in <b>chunks</b> to
1812 * the file. Do so as atomically as possible e.g. by opening temp files and
1813 * renaming. */
1814 static int
1815 write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
1816 int open_flags)
1818 open_file_t *file = NULL;
1819 int fd, result;
1820 fd = start_writing_to_file(fname, open_flags, 0600, &file);
1821 if (fd<0)
1822 return -1;
1823 SMARTLIST_FOREACH(chunks, sized_chunk_t *, chunk,
1825 result = write_all(fd, chunk->bytes, chunk->len, 0);
1826 if (result < 0) {
1827 log(LOG_WARN, LD_FS, "Error writing to \"%s\": %s", fname,
1828 strerror(errno));
1829 goto err;
1831 tor_assert((size_t)result == chunk->len);
1834 return finish_writing_to_file(file);
1835 err:
1836 abort_writing_to_file(file);
1837 return -1;
1840 /** Given a smartlist of sized_chunk_t, write them atomically to a file
1841 * <b>fname</b>, overwriting or creating the file as necessary. */
1843 write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin)
1845 int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
1846 return write_chunks_to_file_impl(fname, chunks, flags);
1849 /** As write_str_to_file, but does not assume a NUL-terminated
1850 * string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
1852 write_bytes_to_file(const char *fname, const char *str, size_t len,
1853 int bin)
1855 int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
1856 int r;
1857 sized_chunk_t c = { str, len };
1858 smartlist_t *chunks = smartlist_create();
1859 smartlist_add(chunks, &c);
1860 r = write_chunks_to_file_impl(fname, chunks, flags);
1861 smartlist_free(chunks);
1862 return r;
1865 /** As write_bytes_to_file, but if the file already exists, append the bytes
1866 * to the end of the file instead of overwriting it. */
1868 append_bytes_to_file(const char *fname, const char *str, size_t len,
1869 int bin)
1871 int flags = OPEN_FLAGS_APPEND|(bin?O_BINARY:O_TEXT);
1872 int r;
1873 sized_chunk_t c = { str, len };
1874 smartlist_t *chunks = smartlist_create();
1875 smartlist_add(chunks, &c);
1876 r = write_chunks_to_file_impl(fname, chunks, flags);
1877 smartlist_free(chunks);
1878 return r;
1881 /** Read the contents of <b>filename</b> into a newly allocated
1882 * string; return the string on success or NULL on failure.
1884 * If <b>stat_out</b> is provided, store the result of stat()ing the
1885 * file into <b>stat_out</b>.
1887 * If <b>flags</b> &amp; RFTS_BIN, open the file in binary mode.
1888 * If <b>flags</b> &amp; RFTS_IGNORE_MISSING, don't warn if the file
1889 * doesn't exist.
1892 * This function <em>may</em> return an erroneous result if the file
1893 * is modified while it is running, but must not crash or overflow.
1894 * Right now, the error case occurs when the file length grows between
1895 * the call to stat and the call to read_all: the resulting string will
1896 * be truncated.
1898 char *
1899 read_file_to_str(const char *filename, int flags, struct stat *stat_out)
1901 int fd; /* router file */
1902 struct stat statbuf;
1903 char *string;
1904 int r;
1905 int bin = flags & RFTS_BIN;
1907 tor_assert(filename);
1909 fd = open(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
1910 if (fd<0) {
1911 int severity = LOG_WARN;
1912 int save_errno = errno;
1913 if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING))
1914 severity = LOG_INFO;
1915 log_fn(severity, LD_FS,"Could not open \"%s\": %s ",filename,
1916 strerror(errno));
1917 errno = save_errno;
1918 return NULL;
1921 if (fstat(fd, &statbuf)<0) {
1922 int save_errno = errno;
1923 close(fd);
1924 log_warn(LD_FS,"Could not fstat \"%s\".",filename);
1925 errno = save_errno;
1926 return NULL;
1929 if ((uint64_t)(statbuf.st_size)+1 > SIZE_T_MAX)
1930 return NULL;
1932 string = tor_malloc((size_t)(statbuf.st_size+1));
1934 r = read_all(fd,string,(size_t)statbuf.st_size,0);
1935 if (r<0) {
1936 int save_errno = errno;
1937 log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
1938 strerror(errno));
1939 tor_free(string);
1940 close(fd);
1941 errno = save_errno;
1942 return NULL;
1944 string[r] = '\0'; /* NUL-terminate the result. */
1946 #ifdef MS_WINDOWS
1947 if (!bin && strchr(string, '\r')) {
1948 log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
1949 "when reading %s. Coping.",
1950 filename);
1951 tor_strstrip(string, "\r");
1952 r = strlen(string);
1954 if (!bin) {
1955 statbuf.st_size = (size_t) r;
1956 } else
1957 #endif
1958 if (r != statbuf.st_size) {
1959 /* Unless we're using text mode on win32, we'd better have an exact
1960 * match for size. */
1961 int save_errno = errno;
1962 log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
1963 r, (long)statbuf.st_size,filename);
1964 tor_free(string);
1965 close(fd);
1966 errno = save_errno;
1967 return NULL;
1969 close(fd);
1970 if (stat_out) {
1971 memcpy(stat_out, &statbuf, sizeof(struct stat));
1974 return string;
1977 #define TOR_ISODIGIT(c) ('0' <= (c) && (c) <= '7')
1979 /** Given a c-style double-quoted escaped string in <b>s</b>, extract and
1980 * decode its contents into a newly allocated string. On success, assign this
1981 * string to *<b>result</b>, assign its length to <b>size_out</b> (if
1982 * provided), and return a pointer to the position in <b>s</b> immediately
1983 * after the string. On failure, return NULL.
1985 static const char *
1986 unescape_string(const char *s, char **result, size_t *size_out)
1988 const char *cp;
1989 char *out;
1990 if (s[0] != '\"')
1991 return NULL;
1992 cp = s+1;
1993 while (1) {
1994 switch (*cp) {
1995 case '\0':
1996 case '\n':
1997 return NULL;
1998 case '\"':
1999 goto end_of_loop;
2000 case '\\':
2001 if ((cp[1] == 'x' || cp[1] == 'X')
2002 && TOR_ISXDIGIT(cp[2]) && TOR_ISXDIGIT(cp[3])) {
2003 cp += 4;
2004 } else if (TOR_ISODIGIT(cp[1])) {
2005 cp += 2;
2006 if (TOR_ISODIGIT(*cp)) ++cp;
2007 if (TOR_ISODIGIT(*cp)) ++cp;
2008 } else if (cp[1]) {
2009 cp += 2;
2010 } else {
2011 return NULL;
2013 break;
2014 default:
2015 ++cp;
2016 break;
2019 end_of_loop:
2020 out = *result = tor_malloc(cp-s + 1);
2021 cp = s+1;
2022 while (1) {
2023 switch (*cp)
2025 case '\"':
2026 *out = '\0';
2027 if (size_out) *size_out = out - *result;
2028 return cp+1;
2029 case '\0':
2030 tor_fragile_assert();
2031 tor_free(*result);
2032 return NULL;
2033 case '\\':
2034 switch (cp[1])
2036 case 'n': *out++ = '\n'; cp += 2; break;
2037 case 'r': *out++ = '\r'; cp += 2; break;
2038 case 't': *out++ = '\t'; cp += 2; break;
2039 case 'x': case 'X':
2040 *out++ = ((hex_decode_digit(cp[2])<<4) +
2041 hex_decode_digit(cp[3]));
2042 cp += 4;
2043 break;
2044 case '0': case '1': case '2': case '3': case '4': case '5':
2045 case '6': case '7':
2047 int n = cp[1]-'0';
2048 cp += 2;
2049 if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
2050 if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
2051 if (n > 255) { tor_free(*result); return NULL; }
2052 *out++ = (char)n;
2054 break;
2055 case '\'':
2056 case '\"':
2057 case '\\':
2058 case '\?':
2059 *out++ = cp[1];
2060 cp += 2;
2061 break;
2062 default:
2063 tor_free(*result); return NULL;
2065 break;
2066 default:
2067 *out++ = *cp++;
2072 /** Given a string containing part of a configuration file or similar format,
2073 * advance past comments and whitespace and try to parse a single line. If we
2074 * parse a line successfully, set *<b>key_out</b> to a new string holding the
2075 * key portion and *<b>value_out</b> to a new string holding the value portion
2076 * of the line, and return a pointer to the start of the next line. If we run
2077 * out of data, return a pointer to the end of the string. If we encounter an
2078 * error, return NULL.
2080 const char *
2081 parse_config_line_from_str(const char *line, char **key_out, char **value_out)
2083 const char *key, *val, *cp;
2085 tor_assert(key_out);
2086 tor_assert(value_out);
2088 *key_out = *value_out = NULL;
2089 key = val = NULL;
2090 /* Skip until the first keyword. */
2091 while (1) {
2092 while (TOR_ISSPACE(*line))
2093 ++line;
2094 if (*line == '#') {
2095 while (*line && *line != '\n')
2096 ++line;
2097 } else {
2098 break;
2102 if (!*line) { /* End of string? */
2103 *key_out = *value_out = NULL;
2104 return line;
2107 /* Skip until the next space. */
2108 key = line;
2109 while (*line && !TOR_ISSPACE(*line) && *line != '#')
2110 ++line;
2111 *key_out = tor_strndup(key, line-key);
2113 /* Skip until the value. */
2114 while (*line == ' ' || *line == '\t')
2115 ++line;
2117 val = line;
2119 /* Find the end of the line. */
2120 if (*line == '\"') {
2121 if (!(line = unescape_string(line, value_out, NULL)))
2122 return NULL;
2123 while (*line == ' ' || *line == '\t')
2124 ++line;
2125 if (*line && *line != '#' && *line != '\n')
2126 return NULL;
2127 } else {
2128 while (*line && *line != '\n' && *line != '#')
2129 ++line;
2130 if (*line == '\n') {
2131 cp = line++;
2132 } else {
2133 cp = line;
2135 while (cp>val && TOR_ISSPACE(*(cp-1)))
2136 --cp;
2138 tor_assert(cp >= val);
2139 *value_out = tor_strndup(val, cp-val);
2142 if (*line == '#') {
2143 do {
2144 ++line;
2145 } while (*line && *line != '\n');
2147 while (TOR_ISSPACE(*line)) ++line;
2149 return line;
2152 /** Expand any homedir prefix on <b>filename</b>; return a newly allocated
2153 * string. */
2154 char *
2155 expand_filename(const char *filename)
2157 tor_assert(filename);
2158 if (*filename == '~') {
2159 size_t len;
2160 char *home, *result;
2161 const char *rest;
2163 if (filename[1] == '/' || filename[1] == '\0') {
2164 home = getenv("HOME");
2165 if (!home) {
2166 log_warn(LD_CONFIG, "Couldn't find $HOME environment variable while "
2167 "expanding \"%s\"", filename);
2168 return NULL;
2170 home = tor_strdup(home);
2171 rest = strlen(filename)>=2?(filename+2):"";
2172 } else {
2173 #ifdef HAVE_PWD_H
2174 char *username, *slash;
2175 slash = strchr(filename, '/');
2176 if (slash)
2177 username = tor_strndup(filename+1,slash-filename-1);
2178 else
2179 username = tor_strdup(filename+1);
2180 if (!(home = get_user_homedir(username))) {
2181 log_warn(LD_CONFIG,"Couldn't get homedir for \"%s\"",username);
2182 tor_free(username);
2183 return NULL;
2185 tor_free(username);
2186 rest = slash ? (slash+1) : "";
2187 #else
2188 log_warn(LD_CONFIG, "Couldn't expend homedir on system without pwd.h");
2189 return tor_strdup(filename);
2190 #endif
2192 tor_assert(home);
2193 /* Remove trailing slash. */
2194 if (strlen(home)>1 && !strcmpend(home,PATH_SEPARATOR)) {
2195 home[strlen(home)-1] = '\0';
2197 /* Plus one for /, plus one for NUL.
2198 * Round up to 16 in case we can't do math. */
2199 len = strlen(home)+strlen(rest)+16;
2200 result = tor_malloc(len);
2201 tor_snprintf(result,len,"%s"PATH_SEPARATOR"%s",home,rest);
2202 tor_free(home);
2203 return result;
2204 } else {
2205 return tor_strdup(filename);
2209 #define MAX_SCANF_WIDTH 9999
2211 /** DOCDOC */
2212 static int
2213 digit_to_num(char d)
2215 int num = ((int)d) - (int)'0';
2216 tor_assert(num <= 9 && num >= 0);
2217 return num;
2220 /** DOCDOC */
2221 static int
2222 scan_unsigned(const char **bufp, unsigned *out, int width)
2224 unsigned result = 0;
2225 int scanned_so_far = 0;
2226 if (!bufp || !*bufp || !out)
2227 return -1;
2228 if (width<0)
2229 width=MAX_SCANF_WIDTH;
2231 while (**bufp && TOR_ISDIGIT(**bufp) && scanned_so_far < width) {
2232 int digit = digit_to_num(*(*bufp)++);
2233 unsigned new_result = result * 10 + digit;
2234 if (new_result > UINT32_MAX || new_result < result)
2235 return -1; /* over/underflow. */
2236 result = new_result;
2237 ++scanned_so_far;
2240 if (!scanned_so_far) /* No actual digits scanned */
2241 return -1;
2243 *out = result;
2244 return 0;
2247 /** DOCDOC */
2248 static int
2249 scan_string(const char **bufp, char *out, int width)
2251 int scanned_so_far = 0;
2252 if (!bufp || !out || width < 0)
2253 return -1;
2254 while (**bufp && ! TOR_ISSPACE(**bufp) && scanned_so_far < width) {
2255 *out++ = *(*bufp)++;
2256 ++scanned_so_far;
2258 *out = '\0';
2259 return 0;
2262 /** Locale-independent, minimal, no-surprises scanf variant, accepting only a
2263 * restricted pattern format. For more info on what it supports, see
2264 * tor_sscanf() documentation. */
2266 tor_vsscanf(const char *buf, const char *pattern, va_list ap)
2268 int n_matched = 0;
2270 while (*pattern) {
2271 if (*pattern != '%') {
2272 if (*buf == *pattern) {
2273 ++buf;
2274 ++pattern;
2275 continue;
2276 } else {
2277 return n_matched;
2279 } else {
2280 int width = -1;
2281 ++pattern;
2282 if (TOR_ISDIGIT(*pattern)) {
2283 width = digit_to_num(*pattern++);
2284 while (TOR_ISDIGIT(*pattern)) {
2285 width *= 10;
2286 width += digit_to_num(*pattern++);
2287 if (width > MAX_SCANF_WIDTH)
2288 return -1;
2290 if (!width) /* No zero-width things. */
2291 return -1;
2293 if (*pattern == 'u') {
2294 unsigned *u = va_arg(ap, unsigned *);
2295 if (!*buf)
2296 return n_matched;
2297 if (scan_unsigned(&buf, u, width)<0)
2298 return n_matched;
2299 ++pattern;
2300 ++n_matched;
2301 } else if (*pattern == 's') {
2302 char *s = va_arg(ap, char *);
2303 if (width < 0)
2304 return -1;
2305 if (scan_string(&buf, s, width)<0)
2306 return n_matched;
2307 ++pattern;
2308 ++n_matched;
2309 } else if (*pattern == 'c') {
2310 char *ch = va_arg(ap, char *);
2311 if (width != -1)
2312 return -1;
2313 if (!*buf)
2314 return n_matched;
2315 *ch = *buf++;
2316 ++pattern;
2317 ++n_matched;
2318 } else if (*pattern == '%') {
2319 if (*buf != '%')
2320 return -1;
2321 ++buf;
2322 ++pattern;
2323 } else {
2324 return -1; /* Unrecognized pattern component. */
2329 return n_matched;
2332 /** Minimal sscanf replacement: parse <b>buf</b> according to <b>pattern</b>
2333 * and store the results in the corresponding argument fields. Differs from
2334 * sscanf in that it: Only handles %u and %Ns. Does not handle arbitrarily
2335 * long widths. %u does not consume any space. Is locale-independent.
2336 * Returns -1 on malformed patterns. */
2338 tor_sscanf(const char *buf, const char *pattern, ...)
2340 int r;
2341 va_list ap;
2342 va_start(ap, pattern);
2343 r = tor_vsscanf(buf, pattern, ap);
2344 va_end(ap);
2345 return r;
2348 /** Return a new list containing the filenames in the directory <b>dirname</b>.
2349 * Return NULL on error or if <b>dirname</b> is not a directory.
2351 smartlist_t *
2352 tor_listdir(const char *dirname)
2354 smartlist_t *result;
2355 #ifdef MS_WINDOWS
2356 char *pattern;
2357 HANDLE handle;
2358 WIN32_FIND_DATA findData;
2359 size_t pattern_len = strlen(dirname)+16;
2360 pattern = tor_malloc(pattern_len);
2361 tor_snprintf(pattern, pattern_len, "%s\\*", dirname);
2362 if (INVALID_HANDLE_VALUE == (handle = FindFirstFile(pattern, &findData))) {
2363 tor_free(pattern);
2364 return NULL;
2366 result = smartlist_create();
2367 while (1) {
2368 if (strcmp(findData.cFileName, ".") &&
2369 strcmp(findData.cFileName, "..")) {
2370 smartlist_add(result, tor_strdup(findData.cFileName));
2372 if (!FindNextFile(handle, &findData)) {
2373 DWORD err;
2374 if ((err = GetLastError()) != ERROR_NO_MORE_FILES) {
2375 char *errstr = format_win32_error(err);
2376 log_warn(LD_FS, "Error reading directory '%s': %s", dirname, errstr);
2377 tor_free(errstr);
2379 break;
2382 FindClose(handle);
2383 tor_free(pattern);
2384 #else
2385 DIR *d;
2386 struct dirent *de;
2387 if (!(d = opendir(dirname)))
2388 return NULL;
2390 result = smartlist_create();
2391 while ((de = readdir(d))) {
2392 if (!strcmp(de->d_name, ".") ||
2393 !strcmp(de->d_name, ".."))
2394 continue;
2395 smartlist_add(result, tor_strdup(de->d_name));
2397 closedir(d);
2398 #endif
2399 return result;
2402 /** Return true iff <b>filename</b> is a relative path. */
2404 path_is_relative(const char *filename)
2406 if (filename && filename[0] == '/')
2407 return 0;
2408 #ifdef MS_WINDOWS
2409 else if (filename && filename[0] == '\\')
2410 return 0;
2411 else if (filename && strlen(filename)>3 && TOR_ISALPHA(filename[0]) &&
2412 filename[1] == ':' && filename[2] == '\\')
2413 return 0;
2414 #endif
2415 else
2416 return 1;
2419 /* =====
2420 * Process helpers
2421 * ===== */
2423 #ifndef MS_WINDOWS
2424 /* Based on code contributed by christian grothoff */
2425 /** True iff we've called start_daemon(). */
2426 static int start_daemon_called = 0;
2427 /** True iff we've called finish_daemon(). */
2428 static int finish_daemon_called = 0;
2429 /** Socketpair used to communicate between parent and child process while
2430 * daemonizing. */
2431 static int daemon_filedes[2];
2432 /** Start putting the process into daemon mode: fork and drop all resources
2433 * except standard fds. The parent process never returns, but stays around
2434 * until finish_daemon is called. (Note: it's safe to call this more
2435 * than once: calls after the first are ignored.)
2437 void
2438 start_daemon(void)
2440 pid_t pid;
2442 if (start_daemon_called)
2443 return;
2444 start_daemon_called = 1;
2446 if (pipe(daemon_filedes)) {
2447 log_err(LD_GENERAL,"pipe failed; exiting. Error was %s", strerror(errno));
2448 exit(1);
2450 pid = fork();
2451 if (pid < 0) {
2452 log_err(LD_GENERAL,"fork failed. Exiting.");
2453 exit(1);
2455 if (pid) { /* Parent */
2456 int ok;
2457 char c;
2459 close(daemon_filedes[1]); /* we only read */
2460 ok = -1;
2461 while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
2462 if (c == '.')
2463 ok = 1;
2465 fflush(stdout);
2466 if (ok == 1)
2467 exit(0);
2468 else
2469 exit(1); /* child reported error */
2470 } else { /* Child */
2471 close(daemon_filedes[0]); /* we only write */
2473 pid = setsid(); /* Detach from controlling terminal */
2475 * Fork one more time, so the parent (the session group leader) can exit.
2476 * This means that we, as a non-session group leader, can never regain a
2477 * controlling terminal. This part is recommended by Stevens's
2478 * _Advanced Programming in the Unix Environment_.
2480 if (fork() != 0) {
2481 exit(0);
2483 return;
2487 /** Finish putting the process into daemon mode: drop standard fds, and tell
2488 * the parent process to exit. (Note: it's safe to call this more than once:
2489 * calls after the first are ignored. Calls start_daemon first if it hasn't
2490 * been called already.)
2492 void
2493 finish_daemon(const char *desired_cwd)
2495 int nullfd;
2496 char c = '.';
2497 if (finish_daemon_called)
2498 return;
2499 if (!start_daemon_called)
2500 start_daemon();
2501 finish_daemon_called = 1;
2503 if (!desired_cwd)
2504 desired_cwd = "/";
2505 /* Don't hold the wrong FS mounted */
2506 if (chdir(desired_cwd) < 0) {
2507 log_err(LD_GENERAL,"chdir to \"%s\" failed. Exiting.",desired_cwd);
2508 exit(1);
2511 nullfd = open("/dev/null", O_RDWR);
2512 if (nullfd < 0) {
2513 log_err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
2514 exit(1);
2516 /* close fds linking to invoking terminal, but
2517 * close usual incoming fds, but redirect them somewhere
2518 * useful so the fds don't get reallocated elsewhere.
2520 if (dup2(nullfd,0) < 0 ||
2521 dup2(nullfd,1) < 0 ||
2522 dup2(nullfd,2) < 0) {
2523 log_err(LD_GENERAL,"dup2 failed. Exiting.");
2524 exit(1);
2526 if (nullfd > 2)
2527 close(nullfd);
2528 /* signal success */
2529 if (write(daemon_filedes[1], &c, sizeof(char)) != sizeof(char)) {
2530 log_err(LD_GENERAL,"write failed. Exiting.");
2532 close(daemon_filedes[1]);
2534 #else
2535 /* defined(MS_WINDOWS) */
2536 void
2537 start_daemon(void)
2540 void
2541 finish_daemon(const char *cp)
2543 (void)cp;
2545 #endif
2547 /** Write the current process ID, followed by NL, into <b>filename</b>.
2549 void
2550 write_pidfile(char *filename)
2552 FILE *pidfile;
2554 if ((pidfile = fopen(filename, "w")) == NULL) {
2555 log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
2556 strerror(errno));
2557 } else {
2558 #ifdef MS_WINDOWS
2559 fprintf(pidfile, "%d\n", (int)_getpid());
2560 #else
2561 fprintf(pidfile, "%d\n", (int)getpid());
2562 #endif
2563 fclose(pidfile);