Tor  0.4.6.0-alpha-dev
shutdown.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 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"
22 #include "core/or/channeltls.h"
23 #include "core/or/circuitlist.h"
25 #include "core/or/circuitpadding.h"
27 #include "core/or/dos.h"
28 #include "core/or/scheduler.h"
30 #include "feature/client/bridges.h"
41 #include "feature/hs/hs_common.h"
48 #include "feature/rend/rendcache.h"
50 #include "feature/stats/bwhist.h"
52 #include "feature/stats/rephist.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
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 */
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))
85  or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
86  or_state_save(now);
87  if (authdir_mode(options)) {
89  }
92  }
93 
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  geoip_free_all();
121  dirserv_free_all();
125  bwhist_free_all();
129  pt_free_all();
141  hs_free_all();
142  dos_free_all();
146 
147  if (!postfork) {
148  config_free_all();
150  or_state_free_all();
151  }
152  if (!postfork) {
153 #ifndef _WIN32
154  tor_getpwnam(NULL);
155 #endif
156  }
157  /* stuff in main.c */
158 
160 
161  if (!postfork) {
163  }
164 
166 
167  /* Stuff in util.c and address.c*/
168  if (!postfork) {
169  esc_router_info(NULL);
170  }
171 }
connection_edge_free_all
void connection_edge_free_all(void)
Definition: connection_edge.c:4718
tor_free
#define tor_free(p)
Definition: malloc.h:52
geoip_free_all
void geoip_free_all(void)
Definition: geoip.c:510
connection_edge.h
Header file for connection_edge.c.
or_state_mark_dirty
void or_state_mark_dirty(or_state_t *state, time_t when)
Definition: statefile.c:784
circuitmux_ewma.h
Header file for circuitmux_ewma.c.
CMD_RUN_TOR
@ CMD_RUN_TOR
Definition: tor_cmdline_mode.h:20
control.h
Header file for control.c.
microdesc.h
Header file for microdesc.c.
rend_cache_free_all
void rend_cache_free_all(void)
Definition: rendcache.c:244
bwhist.h
Header for feature/stats/bwhist.c.
accounting_free_all
void accounting_free_all(void)
Definition: hibernate.c:1254
rep_hist_free_all
void rep_hist_free_all(void)
Definition: rephist.c:2276
circuitmux_ewma_free_all
void circuitmux_ewma_free_all(void)
Definition: circuitmux_ewma.c:719
or_options_t::command
tor_cmdline_mode_t command
Definition: or_options_st.h:68
timers_shutdown
void timers_shutdown(void)
Definition: timers.c:234
routerparse_free_all
void routerparse_free_all(void)
Definition: routerparse.c:1260
circuitpadding.h
Header file for circuitpadding.c.
statefile.h
Header for statefile.c.
get_or_state
or_state_t * get_or_state(void)
Definition: statefile.c:220
networkstatus.h
Header file for networkstatus.c.
tor_cleanup
void tor_cleanup(void)
Definition: shutdown.c:60
scheduler.h
Header file for scheduler*.c.
geoip_stats.h
Header file for geoip_stats.c.
hibernate.h
Header file for hibernate.c.
shutdown.h
Header file for shutdown.c.
tor_remove_file
void tor_remove_file(const char *filename)
Definition: main.c:727
connection_free_all
void connection_free_all(void)
Definition: connection.c:5806
circuitlist.h
Header file for circuitlist.c.
relay_config_free_all
void relay_config_free_all(void)
Definition: relay_config.c:84
accounting_is_enabled
int accounting_is_enabled(const or_options_t *options)
Definition: hibernate.c:305
tor_getpwnam
const struct passwd * tor_getpwnam(const char *username)
Definition: userdb.c:70
tor_free_all
void tor_free_all(int postfork)
Definition: shutdown.c:111
bridges.h
Header file for circuitbuild.c.
geoip.h
Header file for geoip.c.
get_controller_cookie_file_name
char * get_controller_cookie_file_name(void)
Definition: control_auth.c:48
control_free_all
void control_free_all(void)
Definition: control.c:622
entrynodes.h
Header file for circuitbuild.c.
relay_config.h
Header for feature/relay/relay_config.c.
control_auth.h
Header file for control_auth.c.
or_options_t::PidFile
char * PidFile
Definition: or_options_st.h:106
subsystems_shutdown
void subsystems_shutdown(void)
Definition: subsysmgr.c:204
shared_random.h
This file contains ABI/API of the shared random protocol defined in proposal #250....
bridges_free_all
void bridges_free_all(void)
Definition: bridges.c:1036
authmode.h
Header file for directory authority mode.
nodelist.h
Header file for nodelist.c.
tor_mainloop_disconnect_pubsub
void tor_mainloop_disconnect_pubsub(void)
Definition: mainloop_pubsub.c:167
rend_service_authorization_free_all
void rend_service_authorization_free_all(void)
Definition: rendclient.c:1176
routerlist.h
Header file for routerlist.c.
microdesc_free_all
void microdesc_free_all(void)
Definition: microdesc.c:928
circpad_free_all
void circpad_free_all(void)
Definition: circuitpadding.c:3114
entry_guards_free_all
void entry_guards_free_all(void)
Definition: entrynodes.c:3939
pt_free_all
void pt_free_all(void)
Definition: transports.c:1824
transports.h
Headers for transports.c.
rendcache.h
Header file for rendcache.c.
ext_orport.h
Header for ext_orport.c.
channel_free_all
void channel_free_all(void)
Definition: channel.c:2246
get_options
const or_options_t * get_options(void)
Definition: config.c:932
addressmap.h
Header for addressmap.c.
hs_common.h
Header file containing common data for the whole HS subsystem.
geoip_stats_free_all
void geoip_stats_free_all(void)
Definition: geoip_stats.c:1402
or_state_save
int or_state_save(time_t now)
Definition: statefile.c:562
or_options_t::ControlPortWriteToFile
char * ControlPortWriteToFile
Definition: or_options_st.h:865
connection.h
Header file for connection.c.
channeltls.h
Header file for channeltls.c.
circpad_machines_free
void circpad_machines_free(void)
Definition: circuitpadding.c:2800
dirserv.h
Header file for dirserv.c.
main.h
Header file for main.c.
get_ext_or_auth_cookie_file_name
char * get_ext_or_auth_cookie_file_name(void)
Definition: ext_orport.c:127
sr_save_and_cleanup
void sr_save_and_cleanup(void)
Definition: shared_random.c:1277
subsysmgr.h
Header for subsysmgr.c.
release_lockfile
void release_lockfile(void)
Definition: main.c:714
consdiffmgr_free_all
void consdiffmgr_free_all(void)
Definition: consdiffmgr.c:1267
config.h
Header file for config.c.
channel_tls_free_all
void channel_tls_free_all(void)
Definition: channeltls.c:297
addressmap_free_all
void addressmap_free_all(void)
Definition: addressmap.c:328
routerlist_free_all
void routerlist_free_all(void)
Definition: routerlist.c:1515
authdir_mode_tests_reachability
int authdir_mode_tests_reachability(const or_options_t *options)
Definition: authmode.c:68
circuit_free_all
void circuit_free_all(void)
Definition: circuitlist.c:1307
networkstatus_free_all
void networkstatus_free_all(void)
Definition: networkstatus.c:2763
or_options_t
Definition: or_options_st.h:64
scheduler_free_all
void scheduler_free_all(void)
Definition: scheduler.c:484
nodelist_free_all
void nodelist_free_all(void)
Definition: nodelist.c:845
accounting_record_bandwidth_usage
int accounting_record_bandwidth_usage(time_t now, or_state_t *state)
Definition: hibernate.c:705
routerparse.h
Header file for routerparse.c.
config_free_all
void config_free_all(void)
Definition: config.c:1052
esc_router_info
const char * esc_router_info(const routerinfo_t *router)
Definition: routerlist.c:3243
compat_libevent.h
Header for compat_libevent.c.
rendclient.h
Header file for rendclient.c.
rephist.h
Header file for rephist.c.
authdir_mode
int authdir_mode(const or_options_t *options)
Definition: authmode.c:25
mainloop_pubsub.h
Header for mainloop_pubsub.c.
hs_free_all
void hs_free_all(void)
Definition: hs_common.c:1836
or.h
Master header file for Tor-specific functionality.
consdiffmgr.h
Header for consdiffmgr.c.
rep_hist_record_mtbf_data
int rep_hist_record_mtbf_data(time_t now, int missing_means_down)
Definition: rephist.c:613