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