mainloop_sys.c (2038B)
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 mainloop_sys.c 9 * @brief Declare the "mainloop" subsystem. 10 **/ 11 12 #include "core/or/or.h" 13 #include "core/mainloop/mainloop_sys.h" 14 #include "core/mainloop/mainloop.h" 15 #include "core/mainloop/mainloop_state_st.h" 16 #include "core/mainloop/netstatus.h" 17 #include "lib/conf/conftypes.h" 18 #include "lib/conf/confdecl.h" 19 20 #include "lib/subsys/subsys.h" 21 22 static int 23 subsys_mainloop_initialize(void) 24 { 25 initialize_periodic_events(); 26 return 0; 27 } 28 29 static void 30 subsys_mainloop_shutdown(void) 31 { 32 tor_mainloop_free_all(); 33 } 34 35 /** Declare a list of state variables for mainloop state. */ 36 #define CONF_CONTEXT TABLE 37 #include "core/mainloop/mainloop_state.inc" 38 #undef CONF_CONTEXT 39 40 /** Magic number for mainloop state objects */ 41 #define MAINLOOP_STATE_MAGIC 0x59455449 42 43 /** 44 * Format object for mainloop state. 45 **/ 46 static config_format_t mainloop_state_fmt = { 47 .size = sizeof(mainloop_state_t), 48 .magic = { "mainloop_state", 49 MAINLOOP_STATE_MAGIC, 50 offsetof(mainloop_state_t, magic) 51 }, 52 .vars = mainloop_state_t_vars, 53 }; 54 55 /** 56 */ 57 static int 58 mainloop_set_state(void *arg) 59 { 60 const mainloop_state_t *state = arg; 61 tor_assert(state->magic == MAINLOOP_STATE_MAGIC); 62 63 netstatus_load_from_state(state, approx_time()); 64 65 return 0; 66 } 67 68 static int 69 mainloop_flush_state(void *arg) 70 { 71 mainloop_state_t *state = arg; 72 tor_assert(state->magic == MAINLOOP_STATE_MAGIC); 73 74 netstatus_flush_to_state(state, approx_time()); 75 76 return 0; 77 } 78 79 const struct subsys_fns_t sys_mainloop = { 80 .name = "mainloop", 81 SUBSYS_DECLARE_LOCATION(), 82 .supported = true, 83 .level = 5, 84 .initialize = subsys_mainloop_initialize, 85 .shutdown = subsys_mainloop_shutdown, 86 87 .state_format = &mainloop_state_fmt, 88 .set_state = mainloop_set_state, 89 .flush_state = mainloop_flush_state, 90 };