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