Tor 0.4.9.1-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-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"
17#include "app/main/main.h"
18#include "app/main/shutdown.h"
19#include "app/main/subsysmgr.h"
23#include "core/or/channeltls.h"
24#include "core/or/circuitlist.h"
29#include "core/or/dos.h"
30#include "core/or/scheduler.h"
54#include "lib/geoip/geoip.h"
55
56void evdns_shutdown(int);
57
58/** Do whatever cleanup is necessary before shutting Tor down. */
59void
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). */
110void
111tor_free_all(int postfork)
112{
113 if (!postfork) {
114 evdns_shutdown(1);
115 }
122 dirserv_free_all();
124 bwhist_free_all();
130 pt_free_all();
142 hs_free_all();
143 dos_free_all();
147
148 if (!postfork) {
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
161
162 if (!postfork) {
164 }
165
167
168 /* Stuff in util.c and address.c*/
169 if (!postfork) {
170 esc_router_info(NULL);
171 }
172}
void addressmap_free_all(void)
Definition: addressmap.c:328
Header for addressmap.c.
int authdir_mode(const or_options_t *options)
Definition: authmode.c:25
int authdir_mode_tests_reachability(const or_options_t *options)
Definition: authmode.c:68
Header file for directory authority mode.
void bridges_free_all(void)
Definition: bridges.c:1085
Header file for circuitbuild.c.
Header for feature/stats/bwhist.c.
void channel_free_all(void)
Definition: channel.c:2253
void channel_tls_free_all(void)
Definition: channeltls.c:300
Header file for channeltls.c.
void circuit_free_all(void)
Definition: circuitlist.c:1337
Header file for circuitlist.c.
void circuitmux_ewma_free_all(void)
Header file for circuitmux_ewma.c.
void circpad_free_all(void)
void circpad_machines_free(void)
Header file for circuitpadding.c.
Header for compat_libevent.c.
const or_options_t * get_options(void)
Definition: config.c:944
void config_free_all(void)
Definition: config.c:1065
Header file for config.c.
void conflux_notify_shutdown(void)
void conflux_pool_free_all(void)
Header file for conflux_pool.c.
void connection_free_all(void)
Definition: connection.c:5920
Header file for connection.c.
void connection_edge_free_all(void)
Header file for connection_edge.c.
void consdiffmgr_free_all(void)
Definition: consdiffmgr.c:1267
Header for consdiffmgr.c.
void control_free_all(void)
Definition: control.c:620
Header file for control.c.
char * get_controller_cookie_file_name(void)
Definition: control_auth.c:48
Header file for control_auth.c.
void cpuworker_free_all(void)
Definition: cpuworker.c:144
Header file for cpuworker.c.
Header file for dirserv.c.
void entry_guards_free_all(void)
Definition: entrynodes.c:4324
Header file for circuitbuild.c.
char * get_ext_or_auth_cookie_file_name(void)
Definition: ext_orport.c:127
Header for ext_orport.c.
void geoip_free_all(void)
Definition: geoip.c:510
Header file for geoip.c.
Header file for geoip_stats.c.
void geoip_stats_free_all(void)
Definition: geoip_stats.c:1403
int accounting_record_bandwidth_usage(time_t now, or_state_t *state)
Definition: hibernate.c:705
int accounting_is_enabled(const or_options_t *options)
Definition: hibernate.c:305
void accounting_free_all(void)
Definition: hibernate.c:1254
Header file for hibernate.c.
void hs_free_all(void)
Definition: hs_common.c:1710
Header file containing common data for the whole HS subsystem.
void release_lockfile(void)
Definition: main.c:704
void tor_remove_file(const char *filename)
Definition: main.c:717
Header file for main.c.
void tor_mainloop_disconnect_pubsub(void)
Header for mainloop_pubsub.c.
#define tor_free(p)
Definition: malloc.h:56
void microdesc_free_all(void)
Definition: microdesc.c:926
Header file for microdesc.c.
void networkstatus_free_all(void)
Header file for networkstatus.c.
void nodelist_free_all(void)
Definition: nodelist.c:923
Header file for nodelist.c.
Master header file for Tor-specific functionality.
void relay_config_free_all(void)
Definition: relay_config.c:86
Header for feature/relay/relay_config.c.
void rep_hist_free_all(void)
Definition: rephist.c:2986
int rep_hist_record_mtbf_data(time_t now, int missing_means_down)
Definition: rephist.c:1018
Header file for rephist.c.
void routerlist_free_all(void)
Definition: routerlist.c:1518
const char * esc_router_info(const routerinfo_t *router)
Definition: routerlist.c:3302
Header file for routerlist.c.
void routerparse_free_all(void)
Definition: routerparse.c:1272
Header file for routerparse.c.
void scheduler_free_all(void)
Definition: scheduler.c:484
Header file for scheduler*.c.
void sr_save_and_cleanup(void)
This file contains ABI/API of the shared random protocol defined in proposal #250....
void tor_free_all(int postfork)
Definition: shutdown.c:111
void tor_cleanup(void)
Definition: shutdown.c:60
Header file for shutdown.c.
void or_state_mark_dirty(or_state_t *state, time_t when)
Definition: statefile.c:784
or_state_t * get_or_state(void)
Definition: statefile.c:220
int or_state_save(time_t now)
Definition: statefile.c:562
Header for statefile.c.
char * ControlPortWriteToFile
tor_cmdline_mode_t command
Definition: or_options_st.h:68
void subsystems_shutdown(void)
Definition: subsysmgr.c:204
Header for subsysmgr.c.
void timers_shutdown(void)
Definition: timers.c:234
@ CMD_RUN_TOR
void pt_free_all(void)
Definition: transports.c:1905
Headers for transports.c.
const struct passwd * tor_getpwnam(const char *username)
Definition: userdb.c:70