tor  0.4.2.0-alpha-dev
main.c
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
12 #include "core/or/or.h"
13 
14 #include "app/config/config.h"
15 #include "app/config/statefile.h"
16 #include "app/main/main.h"
17 #include "app/main/ntmain.h"
18 #include "app/main/shutdown.h"
19 #include "app/main/subsysmgr.h"
22 #include "core/mainloop/mainloop.h"
23 #include "core/mainloop/mainloop_pubsub.h"
24 #include "core/mainloop/netstatus.h"
25 #include "core/or/channel.h"
26 #include "core/or/channelpadding.h"
27 #include "core/or/circuitpadding.h"
28 #include "core/or/circuitlist.h"
29 #include "core/or/command.h"
30 #include "core/or/connection_or.h"
31 #include "core/or/relay.h"
32 #include "core/or/status.h"
33 #include "feature/api/tor_api.h"
34 #include "feature/api/tor_api_internal.h"
35 #include "feature/client/addressmap.h"
39 #include "feature/dirauth/keypin.h"
41 #include "feature/dircache/consdiffmgr.h"
47 #include "feature/relay/dns.h"
48 #include "feature/relay/ext_orport.h"
49 #include "feature/relay/routerkeys.h"
51 #include "feature/rend/rendcache.h"
53 #include "feature/stats/predict_ports.h"
54 #include "feature/stats/rephist.h"
55 #include "lib/compress/compress.h"
56 #include "lib/buf/buffers.h"
59 #include "lib/net/resolve.h"
60 
61 #include "lib/process/waitpid.h"
63 
64 #include "lib/meminfo/meminfo.h"
65 #include "lib/osinfo/uname.h"
66 #include "lib/sandbox/sandbox.h"
67 #include "lib/fs/lockfile.h"
68 #include "lib/net/resolve.h"
69 #include "lib/tls/tortls.h"
71 #include "lib/encoding/confline.h"
72 #include "lib/evloop/timers.h"
74 #include "lib/version/torversion.h"
75 
76 #include <event2/event.h>
77 
79 #include "feature/dirauth/shared_random.h"
80 
81 #include "core/or/or_connection_st.h"
82 #include "core/or/port_cfg_st.h"
83 
84 #ifdef HAVE_UNISTD_H
85 #include <unistd.h>
86 #endif
87 
88 #ifdef HAVE_SYSTEMD
89 # if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__)
90 /* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse
91  * Coverity. Here's a kludge to unconfuse it.
92  */
93 # define __INCLUDE_LEVEL__ 2
94 #endif /* defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__) */
95 #include <systemd/sd-daemon.h>
96 #endif /* defined(HAVE_SYSTEMD) */
97 
98 #ifdef HAVE_RUST
99 // helper function defined in Rust to output a log message indicating if tor is
100 // running with Rust enabled. See src/rust/tor_util
101 void rust_log_welcome_string(void);
102 #endif
103 
104 /********* PROTOTYPES **********/
105 
106 static void dumpmemusage(int severity);
107 static void dumpstats(int severity); /* log stats */
108 static void process_signal(int sig);
109 
110 /********* START VARIABLES **********/
111 
116 int quiet_level = 0;
117 
118 /********* END VARIABLES ************/
119 
122 static int
123 do_hup(void)
124 {
125  const or_options_t *options = get_options();
126 
127  log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config and "
128  "resetting internal state.");
129  if (accounting_is_enabled(options))
130  accounting_record_bandwidth_usage(time(NULL), get_or_state());
131 
134  /* first, reload config variables, in case they've changed */
135  if (options->ReloadTorrcOnSIGHUP) {
136  /* no need to provide argc/v, they've been cached in init_from_config */
137  int init_rv = options_init_from_torrc(0, NULL);
138  if (init_rv < 0) {
139  log_err(LD_CONFIG,"Reading config failed--see warnings above. "
140  "For usage, try -h.");
141  return -1;
142  } else if (BUG(init_rv > 0)) {
143  // LCOV_EXCL_START
144  /* This should be impossible: the only "return 1" cases in
145  * options_init_from_torrc are ones caused by command-line arguments;
146  * but they can't change while Tor is running. */
147  return -1;
148  // LCOV_EXCL_STOP
149  }
150  options = get_options(); /* they have changed now */
151  /* Logs are only truncated the first time they are opened, but were
152  probably intended to be cleaned up on signal. */
153  if (options->TruncateLogFile)
154  truncate_logs();
155  } else {
156  char *msg = NULL;
157  log_notice(LD_GENERAL, "Not reloading config file: the controller told "
158  "us not to.");
159  /* Make stuff get rescanned, reloaded, etc. */
160  if (set_options((or_options_t*)options, &msg) < 0) {
161  if (!msg)
162  msg = tor_strdup("Unknown error");
163  log_warn(LD_GENERAL, "Unable to re-set previous options: %s", msg);
164  tor_free(msg);
165  }
166  }
167  if (authdir_mode(options)) {
168  /* reload the approved-routers file */
169  if (dirserv_load_fingerprint_file() < 0) {
170  /* warnings are logged from dirserv_load_fingerprint_file() directly */
171  log_info(LD_GENERAL, "Error reloading fingerprints. "
172  "Continuing with old list.");
173  }
174  }
175 
176  /* Rotate away from the old dirty circuits. This has to be done
177  * after we've read the new options, but before we start using
178  * circuits for directory fetches. */
180 
181  /* retry appropriate downloads */
184  if (!net_is_disabled())
185  update_networkstatus_downloads(time(NULL));
186 
187  /* We'll retry routerstatus downloads in about 10 seconds; no need to
188  * force a retry there. */
189 
190  if (server_mode(options)) {
191  /* Maybe we've been given a new ed25519 key or certificate?
192  */
193  time_t now = approx_time();
194  int new_signing_key = load_ed_keys(options, now);
195  if (new_signing_key < 0 ||
196  generate_ed_link_cert(options, now, new_signing_key > 0)) {
197  log_warn(LD_OR, "Problem reloading Ed25519 keys; still using old keys.");
198  }
199 
200  /* Update cpuworker and dnsworker processes, so they get up-to-date
201  * configuration options. */
203  dns_reset();
204  }
205  return 0;
206 }
207 
210 static void
211 signal_callback(evutil_socket_t fd, short events, void *arg)
212 {
213  const int *sigptr = arg;
214  const int sig = *sigptr;
215  (void)fd;
216  (void)events;
217 
218  update_current_time(time(NULL));
219  process_signal(sig);
220 }
221 
223 static void
225 {
226  switch (sig)
227  {
228  case SIGTERM:
229  log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
231  break;
232  case SIGINT:
233  if (!server_mode(get_options())) { /* do it now */
234  log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
236  return;
237  }
238 #ifdef HAVE_SYSTEMD
239  sd_notify(0, "STOPPING=1");
240 #endif
242  break;
243 #ifdef SIGPIPE
244  case SIGPIPE:
245  log_debug(LD_GENERAL,"Caught SIGPIPE. Ignoring.");
246  break;
247 #endif
248  case SIGUSR1:
249  /* prefer to log it at INFO, but make sure we always see it */
252  break;
253  case SIGUSR2:
255  log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
256  "Send HUP to change back.");
258  break;
259  case SIGHUP:
260 #ifdef HAVE_SYSTEMD
261  sd_notify(0, "RELOADING=1");
262 #endif
263  if (do_hup() < 0) {
264  log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
266  return;
267  }
268 #ifdef HAVE_SYSTEMD
269  sd_notify(0, "READY=1");
270 #endif
272  break;
273 #ifdef SIGCHLD
274  case SIGCHLD:
276  break;
277 #endif
278  case SIGNEWNYM: {
279  do_signewnym(time(NULL));
280  break;
281  }
282  case SIGCLEARDNSCACHE:
285  break;
286  case SIGHEARTBEAT:
287  log_heartbeat(time(NULL));
289  break;
290  case SIGACTIVE:
291  /* "SIGACTIVE" counts as ersatz user activity. */
292  note_user_activity(approx_time());
294  break;
295  case SIGDORMANT:
296  /* "SIGDORMANT" means to ignore past user activity */
297  log_notice(LD_GENERAL, "Going dormant because of controller request.");
298  reset_user_activity(0);
299  set_network_participation(false);
300  schedule_rescan_periodic_events();
302  break;
303  }
304 }
305 
309 static void
310 dumpmemusage(int severity)
311 {
313  tor_log(severity, LD_GENERAL, "In rephist: %"PRIu64" used by %d Tors.",
315  dump_routerlist_mem_usage(severity);
316  dump_cell_pool_usage(severity);
317  dump_dns_mem_usage(severity);
318  tor_log_mallinfo(severity);
319 }
320 
323 static void
324 dumpstats(int severity)
325 {
326  time_t now = time(NULL);
327  time_t elapsed;
328  size_t rbuf_cap, wbuf_cap, rbuf_len, wbuf_len;
329 
330  tor_log(severity, LD_GENERAL, "Dumping stats:");
331 
332  SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
333  int i = conn_sl_idx;
334  tor_log(severity, LD_GENERAL,
335  "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
336  i, (int)conn->s, conn->type, conn_type_to_string(conn->type),
337  conn->state, conn_state_to_string(conn->type, conn->state),
338  (int)(now - conn->timestamp_created));
339  if (!connection_is_listener(conn)) {
340  tor_log(severity,LD_GENERAL,
341  "Conn %d is to %s:%d.", i,
342  safe_str_client(conn->address),
343  conn->port);
344  tor_log(severity,LD_GENERAL,
345  "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
346  i,
347  (int)connection_get_inbuf_len(conn),
348  (int)buf_allocation(conn->inbuf),
349  (int)(now - conn->timestamp_last_read_allowed));
350  tor_log(severity,LD_GENERAL,
351  "Conn %d: %d bytes waiting on outbuf "
352  "(len %d, last written %d secs ago)",i,
353  (int)connection_get_outbuf_len(conn),
354  (int)buf_allocation(conn->outbuf),
355  (int)(now - conn->timestamp_last_write_allowed));
356  if (conn->type == CONN_TYPE_OR) {
357  or_connection_t *or_conn = TO_OR_CONN(conn);
358  if (or_conn->tls) {
359  if (tor_tls_get_buffer_sizes(or_conn->tls, &rbuf_cap, &rbuf_len,
360  &wbuf_cap, &wbuf_len) == 0) {
361  tor_log(severity, LD_GENERAL,
362  "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
363  "%d/%d bytes used on write buffer.",
364  i, (int)rbuf_len, (int)rbuf_cap, (int)wbuf_len, (int)wbuf_cap);
365  }
366  }
367  }
368  }
369  circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
370  * using this conn */
371  } SMARTLIST_FOREACH_END(conn);
372 
373  channel_dumpstats(severity);
374  channel_listener_dumpstats(severity);
375 
376  tor_log(severity, LD_NET,
377  "Cells processed: %"PRIu64" padding\n"
378  " %"PRIu64" create\n"
379  " %"PRIu64" created\n"
380  " %"PRIu64" relay\n"
381  " (%"PRIu64" relayed)\n"
382  " (%"PRIu64" delivered)\n"
383  " %"PRIu64" destroy",
392  tor_log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
393  100*(((double)stats_n_data_bytes_packaged) /
396  tor_log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
397  100*(((double)stats_n_data_bytes_received) /
399 
400  cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_TAP, "TAP");
401  cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_NTOR,"ntor");
402 
403  if (now - time_of_process_start >= 0)
404  elapsed = now - time_of_process_start;
405  else
406  elapsed = 0;
407 
408  if (elapsed) {
409  tor_log(severity, LD_NET,
410  "Average bandwidth: %"PRIu64"/%d = %d bytes/sec reading",
411  (get_bytes_read()),
412  (int)elapsed,
413  (int) (get_bytes_read()/elapsed));
414  tor_log(severity, LD_NET,
415  "Average bandwidth: %"PRIu64"/%d = %d bytes/sec writing",
416  (get_bytes_written()),
417  (int)elapsed,
418  (int) (get_bytes_written()/elapsed));
419  }
420 
421  tor_log(severity, LD_NET, "--------------- Dumping memory information:");
422  dumpmemusage(severity);
423 
424  rep_hist_dump_stats(now,severity);
425  rend_service_dump_stats(severity);
426 }
427 
428 #ifdef _WIN32
429 #define UNIX_ONLY 0
430 #else
431 #define UNIX_ONLY 1
432 #endif
433 
434 static struct {
442  struct event *signal_event;
443 } signal_handlers[] = {
444 #ifdef SIGINT
445  { SIGINT, UNIX_ONLY, NULL }, /* do a controlled slow shutdown */
446 #endif
447 #ifdef SIGTERM
448  { SIGTERM, UNIX_ONLY, NULL }, /* to terminate now */
449 #endif
450 #ifdef SIGPIPE
451  { SIGPIPE, UNIX_ONLY, NULL }, /* otherwise SIGPIPE kills us */
452 #endif
453 #ifdef SIGUSR1
454  { SIGUSR1, UNIX_ONLY, NULL }, /* dump stats */
455 #endif
456 #ifdef SIGUSR2
457  { SIGUSR2, UNIX_ONLY, NULL }, /* go to loglevel debug */
458 #endif
459 #ifdef SIGHUP
460  { SIGHUP, UNIX_ONLY, NULL }, /* to reload config, retry conns, etc */
461 #endif
462 #ifdef SIGXFSZ
463  { SIGXFSZ, UNIX_ONLY, NULL }, /* handle file-too-big resource exhaustion */
464 #endif
465 #ifdef SIGCHLD
466  { SIGCHLD, UNIX_ONLY, NULL }, /* handle dns/cpu workers that exit */
467 #endif
468  /* These are controller-only */
469  { SIGNEWNYM, 0, NULL },
470  { SIGCLEARDNSCACHE, 0, NULL },
471  { SIGHEARTBEAT, 0, NULL },
472  { SIGACTIVE, 0, NULL },
473  { SIGDORMANT, 0, NULL },
474  { -1, -1, NULL }
475 };
476 
479 void
481 {
482  int i;
483  const int enabled = !get_options()->DisableSignalHandlers;
484 
485  for (i = 0; signal_handlers[i].signal_value >= 0; ++i) {
486  /* Signal handlers are only registered with libevent if they need to catch
487  * real POSIX signals. We construct these signal handler events in either
488  * case, though, so that controllers can activate them with the SIGNAL
489  * command.
490  */
491  if (enabled && signal_handlers[i].try_to_register) {
492  signal_handlers[i].signal_event =
493  tor_evsignal_new(tor_libevent_get_base(),
494  signal_handlers[i].signal_value,
496  &signal_handlers[i].signal_value);
497  if (event_add(signal_handlers[i].signal_event, NULL))
498  log_warn(LD_BUG, "Error from libevent when adding "
499  "event for signal %d",
500  signal_handlers[i].signal_value);
501  } else {
502  signal_handlers[i].signal_event =
503  tor_event_new(tor_libevent_get_base(), -1,
504  EV_SIGNAL, signal_callback,
505  &signal_handlers[i].signal_value);
506  }
507  }
508 }
509 
510 /* Cause the signal handler for signal_num to be called in the event loop. */
511 void
512 activate_signal(int signal_num)
513 {
514  int i;
515  for (i = 0; signal_handlers[i].signal_value >= 0; ++i) {
516  if (signal_handlers[i].signal_value == signal_num) {
517  event_active(signal_handlers[i].signal_event, EV_SIGNAL, 1);
518  return;
519  }
520  }
521 }
522 
526 int
527 tor_init(int argc, char *argv[])
528 {
529  char progname[256];
530  int quiet = 0;
531 
532  time_of_process_start = time(NULL);
534  /* Have the log set up with our application name. */
535  tor_snprintf(progname, sizeof(progname), "Tor %s", get_version());
536  log_set_application_name(progname);
537 
538  /* Initialize the history structures. */
539  rep_hist_init();
540  /* Initialize the service cache. */
541  rend_cache_init();
542  addressmap_init(); /* Init the client dns cache. Do it always, since it's
543  * cheap. */
544 
545  /* Initialize the HS subsystem. */
546  hs_init();
547 
548  {
549  /* We search for the "quiet" option first, since it decides whether we
550  * will log anything at all to the command line. */
551  config_line_t *opts = NULL, *cmdline_opts = NULL;
552  const config_line_t *cl;
553  (void) config_parse_commandline(argc, argv, 1, &opts, &cmdline_opts);
554  for (cl = cmdline_opts; cl; cl = cl->next) {
555  if (!strcmp(cl->key, "--hush"))
556  quiet = 1;
557  if (!strcmp(cl->key, "--quiet") ||
558  !strcmp(cl->key, "--dump-config"))
559  quiet = 2;
560  /* The following options imply --hush */
561  if (!strcmp(cl->key, "--version") || !strcmp(cl->key, "--digests") ||
562  !strcmp(cl->key, "--list-torrc-options") ||
563  !strcmp(cl->key, "--library-versions") ||
564  !strcmp(cl->key, "--list-modules") ||
565  !strcmp(cl->key, "--hash-password") ||
566  !strcmp(cl->key, "-h") || !strcmp(cl->key, "--help")) {
567  if (quiet < 1)
568  quiet = 1;
569  }
570  }
571  config_free_lines(opts);
572  config_free_lines(cmdline_opts);
573  }
574 
575  /* give it somewhere to log to initially */
576  switch (quiet) {
577  case 2:
578  /* no initial logging */
579  break;
580  case 1:
582  break;
583  default:
585  }
586  quiet_level = quiet;
587 
588  {
589  const char *version = get_version();
590 
591  log_notice(LD_GENERAL, "Tor %s running on %s with Libevent %s, "
592  "%s %s, Zlib %s, Liblzma %s, and Libzstd %s.", version,
593  get_uname(),
597  tor_compress_supports_method(ZLIB_METHOD) ?
598  tor_compress_version_str(ZLIB_METHOD) : "N/A",
599  tor_compress_supports_method(LZMA_METHOD) ?
600  tor_compress_version_str(LZMA_METHOD) : "N/A",
601  tor_compress_supports_method(ZSTD_METHOD) ?
602  tor_compress_version_str(ZSTD_METHOD) : "N/A");
603 
604  log_notice(LD_GENERAL, "Tor can't help you if you use it wrong! "
605  "Learn how to be safe at "
606  "https://www.torproject.org/download/download#warning");
607 
608  if (strstr(version, "alpha") || strstr(version, "beta"))
609  log_notice(LD_GENERAL, "This version is not a stable Tor release. "
610  "Expect more bugs than usual.");
611 
613  }
614 
615 #ifdef HAVE_RUST
616  rust_log_welcome_string();
617 #endif /* defined(HAVE_RUST) */
618 
619  int init_rv = options_init_from_torrc(argc,argv);
620  if (init_rv < 0) {
621  log_err(LD_CONFIG,"Reading config failed--see warnings above.");
622  return -1;
623  } else if (init_rv > 0) {
624  // We succeeded, and should exit anyway -- probably the user just said
625  // "--version" or something like that.
626  return 1;
627  }
628 
629  /* The options are now initialised */
630  const or_options_t *options = get_options();
631 
632  /* Initialize channelpadding and circpad parameters to defaults
633  * until we get a consensus */
634  channelpadding_new_consensus_params(NULL);
636 
637  /* Initialize circuit padding to defaults+torrc until we get a consensus */
639 
640  /* Initialize predicted ports list after loading options */
641  predicted_ports_init();
642 
643 #ifndef _WIN32
644  if (geteuid()==0)
645  log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
646  "and you probably shouldn't.");
647 #endif
648 
649  if (crypto_global_init(options->HardwareAccel,
650  options->AccelName,
651  options->AccelDir)) {
652  log_err(LD_BUG, "Unable to initialize OpenSSL. Exiting.");
653  return -1;
654  }
655 
656  /* Scan/clean unparseable descriptors; after reading config */
658 
659  return 0;
660 }
661 
665 static tor_lockfile_t *lockfile = NULL;
666 
672 int
673 try_locking(const or_options_t *options, int err_if_locked)
674 {
675  if (lockfile)
676  return 0;
677  else {
678  char *fname = options_get_datadir_fname(options, "lock");
679  int already_locked = 0;
680  tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
681  tor_free(fname);
682  if (!lf) {
683  if (err_if_locked && already_locked) {
684  int r;
685  log_warn(LD_GENERAL, "It looks like another Tor process is running "
686  "with the same data directory. Waiting 5 seconds to see "
687  "if it goes away.");
688 #ifndef _WIN32
689  sleep(5);
690 #else
691  Sleep(5000);
692 #endif
693  r = try_locking(options, 0);
694  if (r<0) {
695  log_err(LD_GENERAL, "No, it's still there. Exiting.");
696  return -1;
697  }
698  return r;
699  }
700  return -1;
701  }
702  lockfile = lf;
703  return 0;
704  }
705 }
706 
708 int
710 {
711  return lockfile != NULL;
712 }
713 
715 void
717 {
718  if (lockfile) {
720  lockfile = NULL;
721  }
722 }
723 
728 void
729 tor_remove_file(const char *filename)
730 {
731  if (filename && tor_unlink(filename) != 0 && errno != ENOENT) {
732  log_warn(LD_FS, "Couldn't unlink %s: %s",
733  filename, strerror(errno));
734  }
735 }
736 
738 static int
740 {
741  char buf[FINGERPRINT_LEN+1];
742  crypto_pk_t *k;
743  const char *nickname = get_options()->Nickname;
744  sandbox_disable_getaddrinfo_cache();
745  if (!server_mode(get_options())) {
746  log_err(LD_GENERAL,
747  "Clients don't have long-term identity keys. Exiting.");
748  return -1;
749  }
750  tor_assert(nickname);
751  if (init_keys() < 0) {
752  log_err(LD_GENERAL,"Error initializing keys; exiting.");
753  return -1;
754  }
755  if (!(k = get_server_identity_key())) {
756  log_err(LD_GENERAL,"Error: missing identity key.");
757  return -1;
758  }
759  if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
760  log_err(LD_BUG, "Error computing fingerprint");
761  return -1;
762  }
763  printf("%s %s\n", nickname, buf);
764  return 0;
765 }
766 
769 static void
771 {
772 
773  char output[256];
775 
776  crypto_rand(key, S2K_RFC2440_SPECIFIER_LEN-1);
777  key[S2K_RFC2440_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
779  get_options()->command_arg, strlen(get_options()->command_arg),
780  key);
781  base16_encode(output, sizeof(output), key, sizeof(key));
782  printf("16:%s\n",output);
783 }
784 
787 static int
789 {
790  const or_options_t *options = get_options();
791  const char *arg = options->command_arg;
792  int how;
793  char *opts;
794 
795  if (!strcmp(arg, "short")) {
796  how = OPTIONS_DUMP_MINIMAL;
797  } else if (!strcmp(arg, "non-builtin")) {
798  how = OPTIONS_DUMP_DEFAULTS;
799  } else if (!strcmp(arg, "full")) {
800  how = OPTIONS_DUMP_ALL;
801  } else {
802  fprintf(stderr, "No valid argument to --dump-config found!\n");
803  fprintf(stderr, "Please select 'short', 'non-builtin', or 'full'.\n");
804 
805  return -1;
806  }
807 
808  opts = options_dump(options, how);
809  printf("%s", opts);
810  tor_free(opts);
811 
812  return 0;
813 }
814 
815 static void
816 init_addrinfo(void)
817 {
818  if (! server_mode(get_options()) ||
819  (get_options()->Address && strlen(get_options()->Address) > 0)) {
820  /* We don't need to seed our own hostname, because we won't be calling
821  * resolve_my_address on it.
822  */
823  return;
824  }
825  char hname[256];
826 
827  // host name to sandbox
828  gethostname(hname, sizeof(hname));
829  tor_add_addrinfo(hname);
830 }
831 
832 static sandbox_cfg_t*
833 sandbox_init_filter(void)
834 {
835  const or_options_t *options = get_options();
837  int i;
838 
840  get_cachedir_fname("cached-status"));
841 
842 #define OPEN(name) \
843  sandbox_cfg_allow_open_filename(&cfg, tor_strdup(name))
844 
845 #define OPEN_DATADIR(name) \
846  sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname(name))
847 
848 #define OPEN_DATADIR2(name, name2) \
849  sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname2((name), (name2)))
850 
851 #define OPEN_DATADIR_SUFFIX(name, suffix) do { \
852  OPEN_DATADIR(name); \
853  OPEN_DATADIR(name suffix); \
854  } while (0)
855 
856 #define OPEN_DATADIR2_SUFFIX(name, name2, suffix) do { \
857  OPEN_DATADIR2(name, name2); \
858  OPEN_DATADIR2(name, name2 suffix); \
859  } while (0)
860 
861 #define OPEN_KEY_DIRECTORY() \
862  sandbox_cfg_allow_open_filename(&cfg, tor_strdup(options->KeyDirectory))
863 #define OPEN_CACHEDIR(name) \
864  sandbox_cfg_allow_open_filename(&cfg, get_cachedir_fname(name))
865 #define OPEN_CACHEDIR_SUFFIX(name, suffix) do { \
866  OPEN_CACHEDIR(name); \
867  OPEN_CACHEDIR(name suffix); \
868  } while (0)
869 #define OPEN_KEYDIR(name) \
870  sandbox_cfg_allow_open_filename(&cfg, get_keydir_fname(name))
871 #define OPEN_KEYDIR_SUFFIX(name, suffix) do { \
872  OPEN_KEYDIR(name); \
873  OPEN_KEYDIR(name suffix); \
874  } while (0)
875 
876  OPEN(options->DataDirectory);
877  OPEN_KEY_DIRECTORY();
878 
879  OPEN_CACHEDIR_SUFFIX("cached-certs", ".tmp");
880  OPEN_CACHEDIR_SUFFIX("cached-consensus", ".tmp");
881  OPEN_CACHEDIR_SUFFIX("unverified-consensus", ".tmp");
882  OPEN_CACHEDIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
883  OPEN_CACHEDIR_SUFFIX("cached-microdesc-consensus", ".tmp");
884  OPEN_CACHEDIR_SUFFIX("cached-microdescs", ".tmp");
885  OPEN_CACHEDIR_SUFFIX("cached-microdescs.new", ".tmp");
886  OPEN_CACHEDIR_SUFFIX("cached-descriptors", ".tmp");
887  OPEN_CACHEDIR_SUFFIX("cached-descriptors.new", ".tmp");
888  OPEN_CACHEDIR("cached-descriptors.tmp.tmp");
889  OPEN_CACHEDIR_SUFFIX("cached-extrainfo", ".tmp");
890  OPEN_CACHEDIR_SUFFIX("cached-extrainfo.new", ".tmp");
891  OPEN_CACHEDIR("cached-extrainfo.tmp.tmp");
892 
893  OPEN_DATADIR_SUFFIX("state", ".tmp");
894  OPEN_DATADIR_SUFFIX("sr-state", ".tmp");
895  OPEN_DATADIR_SUFFIX("unparseable-desc", ".tmp");
896  OPEN_DATADIR_SUFFIX("v3-status-votes", ".tmp");
897  OPEN_DATADIR("key-pinning-journal");
898  OPEN("/dev/srandom");
899  OPEN("/dev/urandom");
900  OPEN("/dev/random");
901  OPEN("/etc/hosts");
902  OPEN("/proc/meminfo");
903 
904  if (options->BridgeAuthoritativeDir)
905  OPEN_DATADIR_SUFFIX("networkstatus-bridges", ".tmp");
906 
907  if (authdir_mode(options))
908  OPEN_DATADIR("approved-routers");
909 
910  if (options->ServerDNSResolvConfFile)
912  tor_strdup(options->ServerDNSResolvConfFile));
913  else
914  sandbox_cfg_allow_open_filename(&cfg, tor_strdup("/etc/resolv.conf"));
915 
916  for (i = 0; i < 2; ++i) {
917  if (get_torrc_fname(i)) {
918  sandbox_cfg_allow_open_filename(&cfg, tor_strdup(get_torrc_fname(i)));
919  }
920  }
921 
922  SMARTLIST_FOREACH(options->FilesOpenedByIncludes, char *, f, {
923  OPEN(f);
924  });
925 
926 #define RENAME_SUFFIX(name, suffix) \
927  sandbox_cfg_allow_rename(&cfg, \
928  get_datadir_fname(name suffix), \
929  get_datadir_fname(name))
930 
931 #define RENAME_SUFFIX2(prefix, name, suffix) \
932  sandbox_cfg_allow_rename(&cfg, \
933  get_datadir_fname2(prefix, name suffix), \
934  get_datadir_fname2(prefix, name))
935 
936 #define RENAME_CACHEDIR_SUFFIX(name, suffix) \
937  sandbox_cfg_allow_rename(&cfg, \
938  get_cachedir_fname(name suffix), \
939  get_cachedir_fname(name))
940 
941 #define RENAME_KEYDIR_SUFFIX(name, suffix) \
942  sandbox_cfg_allow_rename(&cfg, \
943  get_keydir_fname(name suffix), \
944  get_keydir_fname(name))
945 
946  RENAME_CACHEDIR_SUFFIX("cached-certs", ".tmp");
947  RENAME_CACHEDIR_SUFFIX("cached-consensus", ".tmp");
948  RENAME_CACHEDIR_SUFFIX("unverified-consensus", ".tmp");
949  RENAME_CACHEDIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
950  RENAME_CACHEDIR_SUFFIX("cached-microdesc-consensus", ".tmp");
951  RENAME_CACHEDIR_SUFFIX("cached-microdescs", ".tmp");
952  RENAME_CACHEDIR_SUFFIX("cached-microdescs", ".new");
953  RENAME_CACHEDIR_SUFFIX("cached-microdescs.new", ".tmp");
954  RENAME_CACHEDIR_SUFFIX("cached-descriptors", ".tmp");
955  RENAME_CACHEDIR_SUFFIX("cached-descriptors", ".new");
956  RENAME_CACHEDIR_SUFFIX("cached-descriptors.new", ".tmp");
957  RENAME_CACHEDIR_SUFFIX("cached-extrainfo", ".tmp");
958  RENAME_CACHEDIR_SUFFIX("cached-extrainfo", ".new");
959  RENAME_CACHEDIR_SUFFIX("cached-extrainfo.new", ".tmp");
960 
961  RENAME_SUFFIX("state", ".tmp");
962  RENAME_SUFFIX("sr-state", ".tmp");
963  RENAME_SUFFIX("unparseable-desc", ".tmp");
964  RENAME_SUFFIX("v3-status-votes", ".tmp");
965 
966  if (options->BridgeAuthoritativeDir)
967  RENAME_SUFFIX("networkstatus-bridges", ".tmp");
968 
969 #define STAT_DATADIR(name) \
970  sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname(name))
971 
972 #define STAT_CACHEDIR(name) \
973  sandbox_cfg_allow_stat_filename(&cfg, get_cachedir_fname(name))
974 
975 #define STAT_DATADIR2(name, name2) \
976  sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname2((name), (name2)))
977 
978 #define STAT_KEY_DIRECTORY() \
979  sandbox_cfg_allow_stat_filename(&cfg, tor_strdup(options->KeyDirectory))
980 
981  STAT_DATADIR(NULL);
982  STAT_DATADIR("lock");
983  STAT_DATADIR("state");
984  STAT_DATADIR("router-stability");
985 
986  STAT_CACHEDIR("cached-extrainfo.new");
987 
988  {
989  smartlist_t *files = smartlist_new();
991  SMARTLIST_FOREACH(files, char *, file_name, {
992  /* steals reference */
993  sandbox_cfg_allow_open_filename(&cfg, file_name);
994  });
995  smartlist_free(files);
996  }
997 
998  {
999  smartlist_t *files = smartlist_new();
1000  smartlist_t *dirs = smartlist_new();
1001  hs_service_lists_fnames_for_sandbox(files, dirs);
1002  SMARTLIST_FOREACH(files, char *, file_name, {
1003  char *tmp_name = NULL;
1004  tor_asprintf(&tmp_name, "%s.tmp", file_name);
1005  sandbox_cfg_allow_rename(&cfg,
1006  tor_strdup(tmp_name), tor_strdup(file_name));
1007  /* steals references */
1008  sandbox_cfg_allow_open_filename(&cfg, file_name);
1009  sandbox_cfg_allow_open_filename(&cfg, tmp_name);
1010  });
1011  SMARTLIST_FOREACH(dirs, char *, dir, {
1012  /* steals reference */
1014  });
1015  smartlist_free(files);
1016  smartlist_free(dirs);
1017  }
1018 
1019  {
1020  char *fname;
1021  if ((fname = get_controller_cookie_file_name())) {
1022  sandbox_cfg_allow_open_filename(&cfg, fname);
1023  }
1024  if ((fname = get_ext_or_auth_cookie_file_name())) {
1025  sandbox_cfg_allow_open_filename(&cfg, fname);
1026  }
1027  }
1028 
1029  SMARTLIST_FOREACH_BEGIN(get_configured_ports(), port_cfg_t *, port) {
1030  if (!port->is_unix_addr)
1031  continue;
1032  /* When we open an AF_UNIX address, we want permission to open the
1033  * directory that holds it. */
1034  char *dirname = tor_strdup(port->unix_addr);
1035  if (get_parent_directory(dirname) == 0) {
1036  OPEN(dirname);
1037  }
1038  tor_free(dirname);
1039  sandbox_cfg_allow_chmod_filename(&cfg, tor_strdup(port->unix_addr));
1040  sandbox_cfg_allow_chown_filename(&cfg, tor_strdup(port->unix_addr));
1041  } SMARTLIST_FOREACH_END(port);
1042 
1043  if (options->DirPortFrontPage) {
1045  tor_strdup(options->DirPortFrontPage));
1046  }
1047 
1048  // orport
1049  if (server_mode(get_options())) {
1050 
1051  OPEN_KEYDIR_SUFFIX("secret_id_key", ".tmp");
1052  OPEN_KEYDIR_SUFFIX("secret_onion_key", ".tmp");
1053  OPEN_KEYDIR_SUFFIX("secret_onion_key_ntor", ".tmp");
1054  OPEN_KEYDIR("secret_id_key.old");
1055  OPEN_KEYDIR("secret_onion_key.old");
1056  OPEN_KEYDIR("secret_onion_key_ntor.old");
1057 
1058  OPEN_KEYDIR_SUFFIX("ed25519_master_id_secret_key", ".tmp");
1059  OPEN_KEYDIR_SUFFIX("ed25519_master_id_secret_key_encrypted", ".tmp");
1060  OPEN_KEYDIR_SUFFIX("ed25519_master_id_public_key", ".tmp");
1061  OPEN_KEYDIR_SUFFIX("ed25519_signing_secret_key", ".tmp");
1062  OPEN_KEYDIR_SUFFIX("ed25519_signing_secret_key_encrypted", ".tmp");
1063  OPEN_KEYDIR_SUFFIX("ed25519_signing_public_key", ".tmp");
1064  OPEN_KEYDIR_SUFFIX("ed25519_signing_cert", ".tmp");
1065 
1066  OPEN_DATADIR2_SUFFIX("stats", "bridge-stats", ".tmp");
1067  OPEN_DATADIR2_SUFFIX("stats", "dirreq-stats", ".tmp");
1068 
1069  OPEN_DATADIR2_SUFFIX("stats", "entry-stats", ".tmp");
1070  OPEN_DATADIR2_SUFFIX("stats", "exit-stats", ".tmp");
1071  OPEN_DATADIR2_SUFFIX("stats", "buffer-stats", ".tmp");
1072  OPEN_DATADIR2_SUFFIX("stats", "conn-stats", ".tmp");
1073  OPEN_DATADIR2_SUFFIX("stats", "hidserv-stats", ".tmp");
1074 
1075  OPEN_DATADIR("approved-routers");
1076  OPEN_DATADIR_SUFFIX("fingerprint", ".tmp");
1077  OPEN_DATADIR_SUFFIX("hashed-fingerprint", ".tmp");
1078  OPEN_DATADIR_SUFFIX("router-stability", ".tmp");
1079 
1080  OPEN("/etc/resolv.conf");
1081 
1082  RENAME_SUFFIX("fingerprint", ".tmp");
1083  RENAME_KEYDIR_SUFFIX("secret_onion_key_ntor", ".tmp");
1084 
1085  RENAME_KEYDIR_SUFFIX("secret_id_key", ".tmp");
1086  RENAME_KEYDIR_SUFFIX("secret_id_key.old", ".tmp");
1087  RENAME_KEYDIR_SUFFIX("secret_onion_key", ".tmp");
1088  RENAME_KEYDIR_SUFFIX("secret_onion_key.old", ".tmp");
1089 
1090  RENAME_SUFFIX2("stats", "bridge-stats", ".tmp");
1091  RENAME_SUFFIX2("stats", "dirreq-stats", ".tmp");
1092  RENAME_SUFFIX2("stats", "entry-stats", ".tmp");
1093  RENAME_SUFFIX2("stats", "exit-stats", ".tmp");
1094  RENAME_SUFFIX2("stats", "buffer-stats", ".tmp");
1095  RENAME_SUFFIX2("stats", "conn-stats", ".tmp");
1096  RENAME_SUFFIX2("stats", "hidserv-stats", ".tmp");
1097  RENAME_SUFFIX("hashed-fingerprint", ".tmp");
1098  RENAME_SUFFIX("router-stability", ".tmp");
1099 
1100  RENAME_KEYDIR_SUFFIX("ed25519_master_id_secret_key", ".tmp");
1101  RENAME_KEYDIR_SUFFIX("ed25519_master_id_secret_key_encrypted", ".tmp");
1102  RENAME_KEYDIR_SUFFIX("ed25519_master_id_public_key", ".tmp");
1103  RENAME_KEYDIR_SUFFIX("ed25519_signing_secret_key", ".tmp");
1104  RENAME_KEYDIR_SUFFIX("ed25519_signing_cert", ".tmp");
1105 
1106  sandbox_cfg_allow_rename(&cfg,
1107  get_keydir_fname("secret_onion_key"),
1108  get_keydir_fname("secret_onion_key.old"));
1109  sandbox_cfg_allow_rename(&cfg,
1110  get_keydir_fname("secret_onion_key_ntor"),
1111  get_keydir_fname("secret_onion_key_ntor.old"));
1112 
1113  STAT_KEY_DIRECTORY();
1114  OPEN_DATADIR("stats");
1115  STAT_DATADIR("stats");
1116  STAT_DATADIR2("stats", "dirreq-stats");
1117 
1118  consdiffmgr_register_with_sandbox(&cfg);
1119  }
1120 
1121  init_addrinfo();
1122 
1123  return cfg;
1124 }
1125 
1126 int
1127 run_tor_main_loop(void)
1128 {
1129  handle_signals();
1132 
1133  /* load the private keys, if we're supposed to have them, and set up the
1134  * TLS context. */
1135  if (! client_identity_key_is_set()) {
1136  if (init_keys() < 0) {
1137  log_err(LD_OR, "Error initializing keys; exiting");
1138  return -1;
1139  }
1140  }
1141 
1142  /* Set up our buckets */
1144 
1145  /* initialize the bootstrap status events to know we're starting up */
1146  control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
1147 
1148  /* Initialize the keypinning log. */
1149  if (authdir_mode_v3(get_options())) {
1150  char *fname = get_datadir_fname("key-pinning-journal");
1151  int r = 0;
1152  if (keypin_load_journal(fname)<0) {
1153  log_err(LD_DIR, "Error loading key-pinning journal: %s",strerror(errno));
1154  r = -1;
1155  }
1156  if (keypin_open_journal(fname)<0) {
1157  log_err(LD_DIR, "Error opening key-pinning journal: %s",strerror(errno));
1158  r = -1;
1159  }
1160  tor_free(fname);
1161  if (r)
1162  return r;
1163  }
1164  {
1165  /* This is the old name for key-pinning-journal. These got corrupted
1166  * in a couple of cases by #16530, so we started over. See #16580 for
1167  * the rationale and for other options we didn't take. We can remove
1168  * this code once all the authorities that ran 0.2.7.1-alpha-dev are
1169  * upgraded.
1170  */
1171  char *fname = get_datadir_fname("key-pinning-entries");
1172  unlink(fname);
1173  tor_free(fname);
1174  }
1175 
1176  if (trusted_dirs_reload_certs()) {
1177  log_warn(LD_DIR,
1178  "Couldn't load all cached v3 certificates. Starting anyway.");
1179  }
1181  return -1;
1182  }
1183  /* load the routers file, or assign the defaults. */
1184  if (router_reload_router_list()) {
1185  return -1;
1186  }
1187  /* load the networkstatuses. (This launches a download for new routers as
1188  * appropriate.)
1189  */
1190  const time_t now = time(NULL);
1191  directory_info_has_arrived(now, 1, 0);
1192 
1193  if (server_mode(get_options()) || dir_server_mode(get_options())) {
1194  /* launch cpuworkers. Need to do this *after* we've read the onion key. */
1195  cpu_init();
1196  }
1197  consdiffmgr_enable_background_compression();
1198 
1199  /* Setup shared random protocol subsystem. */
1200  if (authdir_mode_v3(get_options())) {
1201  if (sr_init(1) < 0) {
1202  return -1;
1203  }
1204  }
1205 
1206  /* initialize dns resolve map, spawn workers if needed */
1207  if (dns_init() < 0) {
1208  if (get_options()->ServerDNSAllowBrokenConfig)
1209  log_warn(LD_GENERAL, "Couldn't set up any working nameservers. "
1210  "Network not up yet? Will try again soon.");
1211  else {
1212  log_err(LD_GENERAL,"Error initializing dns subsystem; exiting. To "
1213  "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
1214  }
1215  }
1216 
1217 #ifdef HAVE_SYSTEMD
1218  {
1219  const int r = sd_notify(0, "READY=1");
1220  if (r < 0) {
1221  log_warn(LD_GENERAL, "Unable to send readiness to systemd: %s",
1222  strerror(r));
1223  } else if (r > 0) {
1224  log_notice(LD_GENERAL, "Signaled readiness to systemd");
1225  } else {
1226  log_info(LD_GENERAL, "Systemd NOTIFY_SOCKET not present.");
1227  }
1228  }
1229 #endif /* defined(HAVE_SYSTEMD) */
1230 
1231  return do_main_loop();
1232 }
1233 
1235 static void
1237 {
1238  pubsub_builder_t *builder = pubsub_builder_new();
1239  int r = subsystems_add_pubsub(builder);
1240  tor_assert(r == 0);
1241  r = tor_mainloop_connect_pubsub(builder); // consumes builder
1242  tor_assert(r == 0);
1243 }
1244 
1247 static void
1249 {
1250  if (get_options()->command == CMD_RUN_TOR) {
1251  tor_mainloop_connect_pubsub_events();
1252  /* XXXX For each pubsub channel, its delivery strategy should be set at
1253  * this XXXX point, using tor_mainloop_set_delivery_strategy().
1254  */
1255  tor_mainloop_set_delivery_strategy("orconn", DELIV_IMMEDIATE);
1256  tor_mainloop_set_delivery_strategy("ocirc", DELIV_IMMEDIATE);
1257  }
1258 }
1259 
1260 /* Main entry point for the Tor process. Called from tor_main(), and by
1261  * anybody embedding Tor. */
1262 int
1264 {
1265  int result = 0;
1266 
1267 #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
1268  event_set_mem_functions(tor_malloc_, tor_realloc_, tor_free_);
1269 #endif
1270 
1271  subsystems_init();
1272 
1274 
1275  int argc = tor_cfg->argc + tor_cfg->argc_owned;
1276  char **argv = tor_calloc(argc, sizeof(char*));
1277  memcpy(argv, tor_cfg->argv, tor_cfg->argc*sizeof(char*));
1278  if (tor_cfg->argc_owned)
1279  memcpy(argv + tor_cfg->argc, tor_cfg->argv_owned,
1280  tor_cfg->argc_owned*sizeof(char*));
1281 
1282 #ifdef NT_SERVICE
1283  {
1284  int done = 0;
1285  result = nt_service_parse_options(argc, argv, &done);
1286  if (done) {
1287  goto done;
1288  }
1289  }
1290 #endif /* defined(NT_SERVICE) */
1291 
1292  pubsub_install();
1293 
1294  {
1295  int init_rv = tor_init(argc, argv);
1296  if (init_rv) {
1297  tor_free_all(0);
1298  result = (init_rv < 0) ? -1 : 0;
1299  goto done;
1300  }
1301  }
1302 
1303  pubsub_connect();
1304 
1305  if (get_options()->Sandbox && get_options()->command == CMD_RUN_TOR) {
1306  sandbox_cfg_t* cfg = sandbox_init_filter();
1307 
1308  if (sandbox_init(cfg)) {
1309  tor_free(argv);
1310  log_err(LD_BUG,"Failed to create syscall sandbox filter");
1311  tor_free_all(0);
1312  return -1;
1313  }
1314  tor_make_getaddrinfo_cache_active();
1315 
1316  // registering libevent rng
1317 #ifdef HAVE_EVUTIL_SECURE_RNG_SET_URANDOM_DEVICE_FILE
1318  evutil_secure_rng_set_urandom_device_file(
1319  (char*) sandbox_intern_string("/dev/urandom"));
1320 #endif
1321  }
1322 
1323  switch (get_options()->command) {
1324  case CMD_RUN_TOR:
1325 #ifdef NT_SERVICE
1326  nt_service_set_state(SERVICE_RUNNING);
1327 #endif
1328  result = run_tor_main_loop();
1329  break;
1330  case CMD_KEYGEN:
1331  result = load_ed_keys(get_options(), time(NULL)) < 0;
1332  break;
1333  case CMD_KEY_EXPIRATION:
1334  init_keys();
1335  result = log_cert_expiration();
1336  break;
1337  case CMD_LIST_FINGERPRINT:
1338  result = do_list_fingerprint();
1339  break;
1340  case CMD_HASH_PASSWORD:
1341  do_hash_password();
1342  result = 0;
1343  break;
1344  case CMD_VERIFY_CONFIG:
1345  if (quiet_level == 0)
1346  printf("Configuration was valid\n");
1347  result = 0;
1348  break;
1349  case CMD_DUMP_CONFIG:
1350  result = do_dump_config();
1351  break;
1352  case CMD_RUN_UNITTESTS: /* only set by test.c */
1353  default:
1354  log_warn(LD_BUG,"Illegal command number %d: internal error.",
1355  get_options()->command);
1356  result = -1;
1357  }
1358  tor_cleanup();
1359  done:
1360  tor_free(argv);
1361  return result;
1362 }
void timers_initialize(void)
Definition: timers.c:203
#define RELAY_PAYLOAD_SIZE
Definition: or.h:605
uint64_t stats_n_data_cells_received
Definition: relay.c:2028
char * AccelName
void router_reset_descriptor_download_failures(void)
Definition: routerlist.c:2829
Header for statefile.c.
time_t time_of_process_start
Definition: mainloop.c:142
uint64_t rephist_total_alloc
Definition: rephist.c:104
Header for uname.c.
Header for confline.c.
struct sandbox_cfg_elem sandbox_cfg_t
Definition: sandbox.h:35
tor_lockfile_t * tor_lockfile_lock(const char *filename, int blocking, int *locked_out)
Definition: lockfile.c:63
Common functions for using (pseudo-)random number generators.
void init_protocol_warning_severity_level(void)
Definition: config.c:1154
int have_lockfile(void)
Definition: main.c:709
Header file for control_auth.c.
void notify_pending_waitpid_callbacks(void)
Definition: waitpid.c:141
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
uint64_t stats_n_relay_cells_relayed
Definition: relay.c:125
Header file for command.c.
Headers for waitpid.c.
void update_current_time(time_t now)
Definition: mainloop.c:2173
void tor_cleanup(void)
Definition: shutdown.c:59
int options_init_from_torrc(int argc, char **argv)
Definition: config.c:5157
struct pubsub_builder_t pubsub_builder_t
Definition: pubsub_build.h:28
int dir_server_mode(const or_options_t *options)
Definition: routermode.c:20
static void signal_callback(evutil_socket_t fd, short events, void *arg)
Definition: main.c:211
void dump_routerlist_mem_usage(int severity)
Definition: routerlist.c:976
Header file for connection.c.
char * DataDirectory
Definition: or_options_st.h:55
char * DirPortFrontPage
#define LD_GENERAL
Definition: log.h:59
int authdir_mode(const or_options_t *options)
Definition: authmode.c:25
Headers for compress.c.
#define LOG_INFO
Definition: log.h:42
void channel_dumpstats(int severity)
Definition: channel.c:2068
int tor_compress_supports_method(compress_method_t method)
Definition: compress.c:304
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:632
int dns_init(void)
Definition: dns.c:224
Header file for shutdown.c.
Header file for ntmain.c.
int connection_is_listener(connection_t *conn)
Definition: connection.c:4623
int tor_tls_get_buffer_sizes(tor_tls_t *tls, size_t *rbuf_capacity, size_t *rbuf_bytes, size_t *wbuf_capacity, size_t *wbuf_bytes)
Definition: tortls_nss.c:674
int try_to_register
Definition: main.c:440
int ReloadTorrcOnSIGHUP
int get_min_log_level(void)
Definition: log.c:1450
void tor_log_get_logfile_names(smartlist_t *out)
Definition: log.c:712
Header file for config.c.
Header file for authcert.c.
#define CONN_TYPE_OR
Definition: connection.h:24
const char * conn_type_to_string(int type)
Definition: connection.c:243
int sandbox_init(sandbox_cfg_t *cfg)
Definition: sandbox.c:1736
Header file for cpuworker.c.
int control_event_signal(uintptr_t signal_num)
static int do_list_fingerprint(void)
Definition: main.c:739
#define tor_free(p)
Definition: malloc.h:52
int do_main_loop(void)
Definition: mainloop.c:2318
void tor_shutdown_event_loop_and_exit(int exitcode)
Definition: mainloop.c:764
void initialize_mainloop_events(void)
Definition: mainloop.c:2304
void circpad_new_consensus_params(const networkstatus_t *ns)
struct event * signal_event
Definition: main.c:442
uint32_t rephist_total_num
Definition: rephist.c:106
#define LOG_NOTICE
Definition: log.h:47
const char * conn_state_to_string(int type, int state)
Definition: connection.c:275
Header file for mainloop.c.
void add_temp_log(int min_severity)
Definition: log.c:936
static int do_dump_config(void)
Definition: main.c:788
char * options_dump(const or_options_t *options, int how_to_dump)
Definition: config.c:2998
int sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file)
Definition: sandbox.c:1760
Header for meminfo.c.
static void do_hash_password(void)
Definition: main.c:770
pubsub_builder_t * pubsub_builder_new(void)
Definition: pubsub_build.c:56
#define sandbox_intern_string(s)
Definition: sandbox.h:112
char * get_controller_cookie_file_name(void)
Definition: control_auth.c:48
struct tor_tls_t * tls
static tor_lockfile_t * lockfile
Definition: main.c:665
Header file for main.c.
int keypin_load_journal(const char *fname)
Definition: keypin.c:452
void connection_dump_buffer_mem_stats(int severity)
Definition: connection.c:5202
or_connection_t * TO_OR_CONN(connection_t *c)
char * ServerDNSResolvConfFile
void tor_lockfile_unlock(tor_lockfile_t *lockfile)
Definition: lockfile.c:121
Header file for directory authority mode.
void circpad_machines_init(void)
int try_locking(const or_options_t *options, int err_if_locked)
Definition: main.c:673
void control_event_bootstrap(bootstrap_status_t status, int progress)
Header file for hibernate.c.
uint64_t stats_n_data_bytes_received
Definition: relay.c:2032
void handle_signals(void)
Definition: main.c:480
int dirserv_load_fingerprint_file(void)
void router_reset_warnings(void)
Definition: router.c:3336
uint64_t stats_n_relay_cells_processed
Definition: command.c:71
int client_identity_key_is_set(void)
Definition: router.c:419
Header file for channel.c.
const char * crypto_get_library_version_string(void)
Definition: crypto_init.c:188
void directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
Definition: mainloop.c:1106
uint64_t stats_n_created_cells_processed
Definition: command.c:69
tor_assert(buffer)
int init_keys(void)
Definition: router.c:926
void routerlist_reset_warnings(void)
Definition: routerlist.c:1443
int quiet_level
Definition: main.c:116
void log_set_application_name(const char *name)
Definition: log.c:247
int config_parse_commandline(int argc, char **argv, int ignore_errors, config_line_t **result, config_line_t **cmdline_result)
Definition: config.c:2470
int trusted_dirs_reload_certs(void)
Definition: authcert.c:324
void addressmap_init(void)
Definition: addressmap.c:91
void tor_compress_log_init_warnings(void)
Definition: compress.c:682
Header file for routermode.c.
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
uint64_t stats_n_data_bytes_packaged
Definition: relay.c:2026
void secret_to_key_rfc2440(char *key_out, size_t key_out_len, const char *secret, size_t secret_len, const char *s2k_specifier)
Definition: crypto_s2k.c:203
void cpu_init(void)
Definition: cpuworker.c:83
Header file for circuitpadding.c.
#define DIGEST_LEN
Definition: digest_sizes.h:20
int load_ed_keys(const or_options_t *options, time_t now)
Definition: routerkeys.c:55
Header file for sandbox.c.
Header file for rendcache.c.
int log_heartbeat(time_t now)
Definition: status.c:119
Public C API for the Tor network service.
Master header file for Tor-specific functionality.
int dns_reset(void)
Definition: dns.c:238
void rep_hist_dump_stats(time_t now, int severity)
Definition: rephist.c:554
uint64_t stats_n_padding_cells_processed
Definition: channeltls.c:78
uint64_t stats_n_relay_cells_delivered
Definition: relay.c:129
int keypin_open_journal(const char *fname)
Definition: keypin.c:305
void release_lockfile(void)
Definition: main.c:716
struct smartlist_t * FilesOpenedByIncludes
Header for crypto_s2k.c.
static void dumpmemusage(int severity)
Definition: main.c:310
void routerparse_init(void)
Definition: routerparse.c:1228
Header file for rephist.c.
#define LOG_WARN
Definition: log.h:50
const char * tor_libevent_get_version_str(void)
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
int router_reload_consensus_networkstatus(void)
void tor_free_(void *mem)
Definition: malloc.c:227
void rep_hist_init(void)
Definition: rephist.c:232
void do_signewnym(time_t now)
Definition: mainloop.c:1307
Header file for circuitlist.c.
int tor_run_main(const tor_main_configuration_t *tor_cfg)
Definition: main.c:1263
int BridgeAuthoritativeDir
Header file for rendservice.c.
Header file for process_descs.c.
void cpuworkers_rotate_keyinfo(void)
Definition: cpuworker.c:188
void truncate_logs(void)
Definition: log.c:1480
#define LD_OR
Definition: log.h:89
void hibernate_begin_shutdown(void)
Definition: hibernate.c:927
static void process_signal(int sig)
Definition: main.c:224
void circuit_dump_by_conn(connection_t *conn, int severity)
Definition: circuitlist.c:1378
static void dumpstats(int severity)
Definition: main.c:324
#define LD_FS
Definition: log.h:67
#define LD_DIR
Definition: log.h:85
static void pubsub_install(void)
Definition: main.c:1236
int generate_ed_link_cert(const or_options_t *options, time_t now, int force)
Definition: routerkeys.c:365
void router_reset_status_download_failures(void)
Definition: dirlist.c:103
int sandbox_cfg_allow_stat_filename(sandbox_cfg_t **cfg, char *file)
Definition: sandbox.c:1774
uint64_t stats_n_data_cells_packaged
Definition: relay.c:2022
int crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out, int add_space)
Definition: crypto_rsa.c:229
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
int tor_init(int argc, char *argv[])
Definition: main.c:527
size_t buf_allocation(const buf_t *buf)
Definition: buffers.c:399
void channel_listener_dumpstats(int severity)
Definition: channel.c:2099
#define S2K_RFC2440_SPECIFIER_LEN
Definition: crypto_s2k.h:21
void circuit_mark_all_dirty_circs_as_unusable(void)
Definition: circuitlist.c:2106
Header for timers.c.
Header file for relay.c.
#define SMARTLIST_FOREACH(sl, type, var, cmd)
char * command_arg
Definition: or_options_st.h:39
#define FINGERPRINT_LEN
Definition: crypto_rsa.h:34
uint64_t stats_n_create_cells_processed
Definition: command.c:67
void * tor_malloc_(size_t size)
Definition: malloc.c:32
uint64_t stats_n_destroy_cells_processed
Definition: command.c:73
void update_networkstatus_downloads(time_t now)
void switch_logs_debug(void)
Definition: log.c:1465
int set_options(or_options_t *new_val, char **msg)
Definition: config.c:925
void cpuworker_log_onionskin_overhead(int severity, int onionskin_type, const char *onionskin_type_name)
Definition: cpuworker.c:292
void dump_dns_mem_usage(int severity)
Definition: dns.c:2152
Headers for tortls.c.
Header file for control.c.
Header for resolve.c.
void rend_cache_init(void)
Definition: rendcache.c:68
time_t approx_time(void)
Definition: approx_time.c:32
int router_reload_router_list(void)
Definition: routerlist.c:457
void tor_log_mallinfo(int severity)
Definition: meminfo.c:43
static void pubsub_connect(void)
Definition: main.c:1248
char * get_ext_or_auth_cookie_file_name(void)
Definition: ext_orport.c:127
void * tor_realloc_(void *ptr, size_t size)
Definition: malloc.c:118
Header file for buffers.c.
void connection_bucket_init(void)
Definition: connection.c:3416
int accounting_record_bandwidth_usage(time_t now, or_state_t *state)
Definition: hibernate.c:705
Header file for connection_or.c.
void rend_service_dump_stats(int severity)
Definition: rendservice.c:4359
#define LD_NET
Definition: log.h:63
int signal_value
Definition: main.c:437
const char * get_torrc_fname(int defaults_fname)
Definition: config.c:5554
void addressmap_clear_transient(void)
Definition: addressmap.c:312
int log_cert_expiration(void)
Definition: routerkeys.c:593
Header for compat_libevent.c.
Header file for control_events.c.
void tor_init_connection_lists(void)
Definition: mainloop.c:412
Header used for constructing the OO publish-subscribe facility.
const char * tor_compress_version_str(compress_method_t method)
Definition: compress.c:417
void tor_remove_file(const char *filename)
Definition: main.c:729
int get_parent_directory(char *fname)
Definition: path.c:176
Header file for networkstatus.c.
int crypto_global_init(int useAccel, const char *accelName, const char *accelDir)
Definition: crypto_init.c:85
#define LD_BUG
Definition: log.h:83
Header file for dns.c.
Headers for crypto_init.c.
#define LD_CONFIG
Definition: log.h:65
void tor_free_all(int postfork)
Definition: shutdown.c:109
Header file for routerlist.c.
void dump_cell_pool_usage(int severity)
Definition: relay.c:2501
Header file for routerparse.c.
static int do_hup(void)
Definition: main.c:123
int sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file)
Definition: sandbox.c:1767
sandbox_cfg_t * sandbox_cfg_new(void)
Definition: sandbox.c:1730
const char * crypto_get_library_name(void)
Definition: crypto_init.c:175
Header for lockfile.c.