shutdown.c 5.0 KB

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