Macros | Functions | Variables
statefile.c File Reference
#include "core/or/or.h"
#include "core/or/circuitstats.h"
#include "app/config/config.h"
#include "app/config/confparse.h"
#include "core/mainloop/mainloop.h"
#include "core/mainloop/netstatus.h"
#include "core/mainloop/connection.h"
#include "feature/control/control.h"
#include "feature/client/entrynodes.h"
#include "feature/hibernate/hibernate.h"
#include "feature/stats/rephist.h"
#include "feature/relay/router.h"
#include "feature/relay/routermode.h"
#include "lib/sandbox/sandbox.h"
#include "app/config/statefile.h"
#include "lib/encoding/confline.h"
#include "lib/net/resolve.h"
#include "lib/version/torversion.h"
#include "app/config/or_state_st.h"

Go to the source code of this file.


#define VAR(name, conftype, member, initvalue)
#define V(member, conftype, initvalue)   VAR(#member, conftype, member, initvalue)
#define OR_STATE_MAGIC   0x57A73f57


static int or_state_validate (or_state_t *state, char **msg)
static int or_state_validate_cb (void *old_options, void *options, void *default_options, int from_setconf, char **msg)
static void or_state_free_cb (void *state)
 MOCK_IMPL (or_state_t *, get_or_state,(void))
int or_state_loaded (void)
static int state_transport_line_is_valid (const char *line)
static int validate_transports_in_state (or_state_t *state)
static int or_state_set (or_state_t *new_state)
static void or_state_save_broken (char *fname)
STATIC or_state_tor_state_new (void)
int or_state_load (void)
int did_last_state_file_write_fail (void)
int or_state_save (time_t now)
STATIC config_line_tget_transport_in_state_by_name (const char *transport)
static const char * get_transport_bindaddr (const char *line, const char *transport)
char * get_stored_bindaddr_for_server_transport (const char *transport)
void save_transport_to_state (const char *transport, const tor_addr_t *addr, uint16_t port)
void or_state_mark_dirty (or_state_t *state, time_t when)
STATIC void or_state_free_ (or_state_t *state)
void or_state_free_all (void)


static config_abbrev_t state_abbrevs_ []
static config_var_t state_vars_ []
static config_var_t state_extra_var
static const config_format_t state_format
static or_state_tglobal_state = NULL
static int last_state_file_write_failed = 0

Detailed Description

Handles parsing and encoding the persistent 'state' file that carries miscellaneous persistent state between Tor invocations.

This 'state' file is a typed key-value store that allows multiple entries for the same key. It follows the same metaformat as described in confparse.c, and uses the same code to read and write itself.

The state file is most suitable for small values that don't change too frequently. For values that become very large, we typically use a separate file – for example, see how we handle microdescriptors, by storing them in a separate file with a journal.

The current state is accessed via get_or_state(), which returns a singleton or_state_t object. Functions that change it should call or_state_mark_dirty() to ensure that it will get written to disk.

The or_state_save() function additionally calls various functioens throughout Tor that might want to flush more state to the the disk, including some in rephist.c, entrynodes.c, circuitstats.c, hibernate.c.

Definition in file statefile.c.

Macro Definition Documentation


#define OR_STATE_MAGIC   0x57A73f57

Magic value for or_state_t.

Definition at line 154 of file statefile.c.



If we're a relay, how often should we checkpoint our state file even if nothing else dirties it? This will checkpoint ongoing stats like bandwidth used, per-country user stats, etc.

Definition at line 489 of file statefile.c.



If writing the state to disk fails, try again after this many seconds.

Definition at line 484 of file statefile.c.

◆ V

#define V (   member,
)    VAR(#member, conftype, member, initvalue)

As VAR, but the option name and member name are the same.

Definition at line 78 of file statefile.c.


#define VAR (   name,
{ name, CONFIG_TYPE_ ## conftype, offsetof(or_state_t, member), \
initvalue CONF_TEST_MEMBERS(or_state_t, conftype, member) }

Definition at line 74 of file statefile.c.

Function Documentation

◆ did_last_state_file_write_fail()

int did_last_state_file_write_fail ( void  )

Return whether the state file failed to write last time we tried.

Definition at line 478 of file statefile.c.

References last_state_file_write_failed.



dummy instance of or_state_t, used for type-checking its members with CONF_CHECK_VAR_TYPE.

◆ get_stored_bindaddr_for_server_transport()

char* get_stored_bindaddr_for_server_transport ( const char *  transport)

Return a string containing the address:port that a proxy transport should bind on. The string is stored on the heap and must be freed by the caller of this function.

If we didn't find references for this pluggable transport in the state file, we should instruct the pluggable transport proxy to listen on INADDR_ANY on a random ephemeral port.

Definition at line 620 of file statefile.c.

References get_transport_bindaddr(), get_transport_bindaddr_from_config(), and get_transport_in_state_by_name().

Referenced by get_bindaddr_for_server_proxy().

◆ get_transport_bindaddr()

static const char* get_transport_bindaddr ( const char *  line,
const char *  transport 

Return string containing the address:port part of the TransportProxy line for transport transport. If the line is corrupted, return NULL.

Definition at line 594 of file statefile.c.

References strcmpstart(), tor_asprintf(), and tor_free.

Referenced by get_stored_bindaddr_for_server_transport(), and save_transport_to_state().

◆ get_transport_in_state_by_name()

STATIC config_line_t* get_transport_in_state_by_name ( const char *  transport)

Return the config line for transport transport in the current state. Return NULL if there is no config line for transport.

Definition at line 556 of file statefile.c.

References smartlist_split_string(), and tor_assert().

Referenced by get_stored_bindaddr_for_server_transport(), and save_transport_to_state().


MOCK_IMPL ( or_state_t ,
get_or_state  ,

Return the persistent state struct for this Tor.

Definition at line 180 of file statefile.c.

References global_state, and tor_assert().

◆ or_state_load()

int or_state_load ( void  )

Reload the persistent state from disk, generating a new state as needed. Return 0 on success, less than 0 on failure.

Definition at line 374 of file statefile.c.

◆ or_state_loaded()

int or_state_loaded ( void  )

Return true iff we have loaded the global state for this Tor

Definition at line 189 of file statefile.c.

References global_state.

◆ or_state_mark_dirty()

void or_state_mark_dirty ( or_state_t state,
time_t  when 

Change the next_write time of state to when, unless the state is already scheduled to be written to disk earlier than when.

Definition at line 715 of file statefile.c.

References or_state_t::next_write, and reschedule_or_state_save().

Referenced by entry_guards_changed_for_guard_selection(), entry_guards_update_state(), and tor_cleanup().

◆ or_state_save()

int or_state_save ( time_t  now)

Write the persistent state to disk. Return 0 for success, <0 on failure.

Definition at line 493 of file statefile.c.

Referenced by save_state_callback(), and tor_cleanup().

◆ or_state_save_broken()

static void or_state_save_broken ( char *  fname)

Save a broken state file to a backup location.

Definition at line 325 of file statefile.c.

◆ or_state_set()

static int or_state_set ( or_state_t new_state)

Replace the current persistent state with new_state

Definition at line 295 of file statefile.c.

References tor_assert().

◆ or_state_validate()

static int or_state_validate ( or_state_t state,
char **  msg 

Return 0 if every setting in state is reasonable, and a permissible transition from old_state. Else warn and return -1. Should have no side effects, except for normalizing the contents of state.

Definition at line 282 of file statefile.c.

References entry_guards_parse_state(), and validate_transports_in_state().

◆ save_transport_to_state()

void save_transport_to_state ( const char *  transport,
const tor_addr_t addr,
uint16_t  port 

Save transport listening on addr:port to state

find where to write on the state

Definition at line 653 of file statefile.c.

References get_transport_bindaddr(), and get_transport_in_state_by_name().

Referenced by register_server_proxy().

◆ state_transport_line_is_valid()

static int state_transport_line_is_valid ( const char *  line)

Return true if line is a valid state TransportProxy line. Return false otherwise.

Definition at line 197 of file statefile.c.

References smartlist_split_string().

Referenced by validate_transports_in_state().

◆ validate_transports_in_state()

static int validate_transports_in_state ( or_state_t state)

Return 0 if all TransportProxy lines in state are well formed. Otherwise, return -1.

Definition at line 240 of file statefile.c.

References state_transport_line_is_valid(), and tor_assert().

Referenced by or_state_validate().

Variable Documentation

◆ global_state

or_state_t* global_state = NULL

Persistent serialized state.

Definition at line 177 of file statefile.c.

Referenced by MOCK_IMPL(), and or_state_loaded().

◆ last_state_file_write_failed

int last_state_file_write_failed = 0

Did the last time we tried to write the state file fail? If so, we should consider disabling such features as preemptive circuit generation to compute circuit-build-time.

Definition at line 474 of file statefile.c.

Referenced by did_last_state_file_write_fail().

◆ state_abbrevs_

config_abbrev_t state_abbrevs_[]
Initial value:
= {
{ "AccountingBytesReadInterval", "AccountingBytesReadInInterval", 0, 0 },
{ "HelperNode", "EntryGuard", 0, 0 },
{ "HelperNodeDownSince", "EntryGuardDownSince", 0, 0 },
{ "HelperNodeUnlistedSince", "EntryGuardUnlistedSince", 0, 0 },
{ "EntryNode", "EntryGuard", 0, 0 },
{ "EntryNodeDownSince", "EntryGuardDownSince", 0, 0 },
{ "EntryNodeUnlistedSince", "EntryGuardUnlistedSince", 0, 0 },
{ NULL, NULL, 0, 0},

A list of state-file "abbreviations," for compatibility.

Definition at line 58 of file statefile.c.

◆ state_extra_var

config_var_t state_extra_var
Initial value:
= {
"__extra", CONFIG_TYPE_LINELIST, offsetof(or_state_t, ExtraLines), NULL

"Extra" variable in the state that receives lines we can't parse. This lets us preserve options from versions of Tor newer than us.

Definition at line 158 of file statefile.c.

◆ state_format

const config_format_t state_format
Initial value:
= {
offsetof(or_state_t, magic_),
static config_var_t state_extra_var
Definition: statefile.c:158
static config_abbrev_t state_abbrevs_[]
Definition: statefile.c:58
Definition: statefile.c:154
static config_var_t state_vars_[]
Definition: statefile.c:82

Configuration format for or_state_t.

Definition at line 164 of file statefile.c.

◆ state_vars_

config_var_t state_vars_[]

Array of "state" variables saved to the ~/.tor/state file.

Definition at line 82 of file statefile.c.