shutdown.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * @file shutdown.c
  8. * @brief Code to free global resources used by Tor.
  9. *
  10. * In the future, this should all be handled by the subsystem manager. */
  11. #include "core/or/or.h"
  12. #include "app/config/config.h"
  13. #include "app/config/statefile.h"
  14. #include "app/main/main.h"
  15. #include "app/main/shutdown.h"
  16. #include "app/main/subsysmgr.h"
  17. #include "core/mainloop/connection.h"
  18. #include "core/mainloop/mainloop_pubsub.h"
  19. #include "core/or/channeltls.h"
  20. #include "core/or/circuitlist.h"
  21. #include "core/or/circuitmux_ewma.h"
  22. #include "core/or/circuitpadding.h"
  23. #include "core/or/connection_edge.h"
  24. #include "core/or/dos.h"
  25. #include "core/or/policies.h"
  26. #include "core/or/protover.h"
  27. #include "core/or/scheduler.h"
  28. #include "core/or/versions.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/bwauth.h"
  37. #include "feature/dirauth/dirvote.h"
  38. #include "feature/dirauth/keypin.h"
  39. #include "feature/dirauth/shared_random.h"
  40. #include "feature/dircache/consdiffmgr.h"
  41. #include "feature/dircache/dirserv.h"
  42. #include "feature/dirparse/routerparse.h"
  43. #include "feature/hibernate/hibernate.h"
  44. #include "feature/hs/hs_common.h"
  45. #include "feature/nodelist/microdesc.h"
  46. #include "feature/nodelist/networkstatus.h"
  47. #include "feature/nodelist/nodelist.h"
  48. #include "feature/nodelist/routerlist.h"
  49. #include "feature/nodelist/routerlist.h"
  50. #include "feature/relay/dns.h"
  51. #include "feature/relay/ext_orport.h"
  52. #include "feature/relay/onion_queue.h"
  53. #include "feature/relay/routerkeys.h"
  54. #include "feature/rend/rendcache.h"
  55. #include "feature/rend/rendclient.h"
  56. #include "feature/stats/geoip_stats.h"
  57. #include "feature/stats/rephist.h"
  58. #include "lib/evloop/compat_libevent.h"
  59. #include "lib/geoip/geoip.h"
  60. #include "src/feature/relay/router.h"
  61. void evdns_shutdown(int);
  62. /** Do whatever cleanup is necessary before shutting Tor down. */
  63. void
  64. tor_cleanup(void)
  65. {
  66. const or_options_t *options = get_options();
  67. if (options->command == CMD_RUN_TOR) {
  68. time_t now = time(NULL);
  69. /* Remove our pid file. We don't care if there was an error when we
  70. * unlink, nothing we could do about it anyways. */
  71. tor_remove_file(options->PidFile);
  72. /* Remove control port file */
  73. tor_remove_file(options->ControlPortWriteToFile);
  74. /* Remove cookie authentication file */
  75. {
  76. char *cookie_fname = get_controller_cookie_file_name();
  77. tor_remove_file(cookie_fname);
  78. tor_free(cookie_fname);
  79. }
  80. /* Remove Extended ORPort cookie authentication file */
  81. {
  82. char *cookie_fname = get_ext_or_auth_cookie_file_name();
  83. tor_remove_file(cookie_fname);
  84. tor_free(cookie_fname);
  85. }
  86. if (accounting_is_enabled(options))
  87. accounting_record_bandwidth_usage(now, get_or_state());
  88. or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
  89. or_state_save(now);
  90. if (authdir_mode(options)) {
  91. sr_save_and_cleanup();
  92. }
  93. if (authdir_mode_tests_reachability(options))
  94. rep_hist_record_mtbf_data(now, 0);
  95. }
  96. timers_shutdown();
  97. tor_free_all(0); /* We could move tor_free_all back into the ifdef below
  98. later, if it makes shutdown unacceptably slow. But for
  99. now, leave it here: it's helped us catch bugs in the
  100. past. */
  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();
  117. geoip_stats_free_all();
  118. routerlist_free_all();
  119. networkstatus_free_all();
  120. addressmap_free_all();
  121. dirserv_free_all();
  122. rend_cache_free_all();
  123. rend_service_authorization_free_all();
  124. rep_hist_free_all();
  125. dns_free_all();
  126. clear_pending_onions();
  127. circuit_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. ext_orport_free_all();
  140. control_free_all();
  141. protover_free_all();
  142. bridges_free_all();
  143. consdiffmgr_free_all();
  144. hs_free_all();
  145. dos_free_all();
  146. circuitmux_ewma_free_all();
  147. accounting_free_all();
  148. protover_summary_cache_free_all();
  149. if (!postfork) {
  150. config_free_all();
  151. or_state_free_all();
  152. router_free_all();
  153. routerkeys_free_all();
  154. policies_free_all();
  155. }
  156. if (!postfork) {
  157. #ifndef _WIN32
  158. tor_getpwnam(NULL);
  159. #endif
  160. }
  161. /* stuff in main.c */
  162. tor_mainloop_disconnect_pubsub();
  163. if (!postfork) {
  164. release_lockfile();
  165. }
  166. tor_libevent_free_all();
  167. subsystems_shutdown();
  168. /* Stuff in util.c and address.c*/
  169. if (!postfork) {
  170. esc_router_info(NULL);
  171. }
  172. }