tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

shutdown.c (5142B)


      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-2024, The Tor Project, Inc. */
      5 /* See LICENSE for licensing information */
      6 
      7 /**
      8 * @file shutdown.c
      9 * @brief Code to free global resources used by Tor.
     10 *
     11 * In the future, this should all be handled by the subsystem manager. */
     12 
     13 #include "core/or/or.h"
     14 
     15 #include "app/config/config.h"
     16 #include "app/config/statefile.h"
     17 #include "app/main/main.h"
     18 #include "app/main/shutdown.h"
     19 #include "app/main/subsysmgr.h"
     20 #include "core/mainloop/connection.h"
     21 #include "core/mainloop/mainloop_pubsub.h"
     22 #include "core/mainloop/cpuworker.h"
     23 #include "core/or/channeltls.h"
     24 #include "core/or/circuitlist.h"
     25 #include "core/or/circuitmux_ewma.h"
     26 #include "core/or/circuitpadding.h"
     27 #include "core/or/conflux_pool.h"
     28 #include "core/or/connection_edge.h"
     29 #include "core/or/dos.h"
     30 #include "core/or/scheduler.h"
     31 #include "feature/client/addressmap.h"
     32 #include "feature/client/bridges.h"
     33 #include "feature/client/entrynodes.h"
     34 #include "feature/client/transports.h"
     35 #include "feature/control/control.h"
     36 #include "feature/control/control_auth.h"
     37 #include "feature/dirauth/authmode.h"
     38 #include "feature/dirauth/shared_random.h"
     39 #include "feature/dircache/consdiffmgr.h"
     40 #include "feature/dircache/dirserv.h"
     41 #include "feature/dirparse/routerparse.h"
     42 #include "feature/hibernate/hibernate.h"
     43 #include "feature/hs/hs_common.h"
     44 #include "feature/nodelist/microdesc.h"
     45 #include "feature/nodelist/networkstatus.h"
     46 #include "feature/nodelist/nodelist.h"
     47 #include "feature/nodelist/routerlist.h"
     48 #include "feature/relay/ext_orport.h"
     49 #include "feature/relay/relay_config.h"
     50 #include "feature/stats/bwhist.h"
     51 #include "feature/stats/geoip_stats.h"
     52 #include "feature/stats/rephist.h"
     53 #include "lib/evloop/compat_libevent.h"
     54 #include "lib/geoip/geoip.h"
     55 
     56 void evdns_shutdown(int);
     57 
     58 /** Do whatever cleanup is necessary before shutting Tor down. */
     59 void
     60 tor_cleanup(void)
     61 {
     62  const or_options_t *options = get_options();
     63  if (options->command == CMD_RUN_TOR) {
     64    time_t now = time(NULL);
     65    /* Remove our pid file. We don't care if there was an error when we
     66     * unlink, nothing we could do about it anyways. */
     67    tor_remove_file(options->PidFile);
     68    /* Remove control port file */
     69    tor_remove_file(options->ControlPortWriteToFile);
     70    /* Remove cookie authentication file */
     71    {
     72      char *cookie_fname = get_controller_cookie_file_name();
     73      tor_remove_file(cookie_fname);
     74      tor_free(cookie_fname);
     75    }
     76    /* Remove Extended ORPort cookie authentication file */
     77    {
     78      char *cookie_fname = get_ext_or_auth_cookie_file_name();
     79      if (cookie_fname)
     80        tor_remove_file(cookie_fname);
     81      tor_free(cookie_fname);
     82    }
     83    if (accounting_is_enabled(options))
     84      accounting_record_bandwidth_usage(now, get_or_state());
     85    or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
     86    or_state_save(now);
     87    if (authdir_mode(options)) {
     88      sr_save_and_cleanup();
     89    }
     90    if (authdir_mode_tests_reachability(options))
     91      rep_hist_record_mtbf_data(now, 0);
     92  }
     93 
     94  timers_shutdown();
     95 
     96  tor_free_all(0); /* We could move tor_free_all back into the ifdef below
     97                      later, if it makes shutdown unacceptably slow.  But for
     98                      now, leave it here: it's helped us catch bugs in the
     99                      past. */
    100 }
    101 
    102 /** Free all memory that we might have allocated somewhere.
    103 * If <b>postfork</b>, we are a worker process and we want to free
    104 * only the parts of memory that we won't touch. If !<b>postfork</b>,
    105 * Tor is shutting down and we should free everything.
    106 *
    107 * Helps us find the real leaks with sanitizers and the like. Also valgrind
    108 * should then report 0 reachable in its leak report (in an ideal world --
    109 * in practice libevent, SSL, libc etc never quite free everything). */
    110 void
    111 tor_free_all(int postfork)
    112 {
    113  if (!postfork) {
    114    evdns_shutdown(1);
    115  }
    116  cpuworker_free_all();
    117  geoip_free_all();
    118  geoip_stats_free_all();
    119  routerlist_free_all();
    120  networkstatus_free_all();
    121  addressmap_free_all();
    122  dirserv_free_all();
    123  rep_hist_free_all();
    124  bwhist_free_all();
    125  conflux_notify_shutdown();
    126  circuit_free_all();
    127  conflux_pool_free_all();
    128  circpad_machines_free();
    129  entry_guards_free_all();
    130  pt_free_all();
    131  channel_tls_free_all();
    132  channel_free_all();
    133  connection_free_all();
    134  connection_edge_free_all();
    135  scheduler_free_all();
    136  nodelist_free_all();
    137  microdesc_free_all();
    138  routerparse_free_all();
    139  control_free_all();
    140  bridges_free_all();
    141  consdiffmgr_free_all();
    142  hs_free_all();
    143  dos_free_all();
    144  circuitmux_ewma_free_all();
    145  accounting_free_all();
    146  circpad_free_all();
    147 
    148  if (!postfork) {
    149    config_free_all();
    150    relay_config_free_all();
    151    or_state_free_all();
    152  }
    153  if (!postfork) {
    154 #ifndef _WIN32
    155    tor_getpwnam(NULL);
    156 #endif
    157  }
    158  /* stuff in main.c */
    159 
    160  tor_mainloop_disconnect_pubsub();
    161 
    162  if (!postfork) {
    163    release_lockfile();
    164  }
    165 
    166  subsystems_shutdown();
    167 
    168  /* Stuff in util.c and address.c*/
    169  if (!postfork) {
    170    esc_router_info(NULL);
    171  }
    172 }