tor  master
Macros | Functions
statefile.c File Reference

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

#include "or.h"
#include "circuitstats.h"
#include "config.h"
#include "confparse.h"
#include "connection.h"
#include "control.h"
#include "entrynodes.h"
#include "hibernate.h"
#include "main.h"
#include "rephist.h"
#include "router.h"
#include "sandbox.h"
#include "statefile.h"
Include dependency graph for statefile.c:

Macros

#define STATEFILE_PRIVATE
 
#define VAR(name, conftype, member, initvalue)
 
#define V(member, conftype, initvalue)   VAR(#member, conftype, member, initvalue)
 
#define OR_STATE_MAGIC   0x57A73f57
 
#define STATE_WRITE_RETRY_INTERVAL   3600
 
#define STATE_RELAY_CHECKPOINT_INTERVAL   (12*60*60)
 

Functions

 DUMMY_TYPECHECK_INSTANCE (or_state_t)
 
 MOCK_IMPL (or_state_t *, get_or_state,(void))
 
int or_state_loaded (void)
 
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)
 
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)
 

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.

Macro Definition Documentation

◆ OR_STATE_MAGIC

#define OR_STATE_MAGIC   0x57A73f57

Magic value for or_state_t.

◆ STATE_RELAY_CHECKPOINT_INTERVAL

#define STATE_RELAY_CHECKPOINT_INTERVAL   (12*60*60)

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.

◆ STATE_WRITE_RETRY_INTERVAL

#define STATE_WRITE_RETRY_INTERVAL   3600

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

◆ V

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

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

◆ VAR

#define VAR (   name,
  conftype,
  member,
  initvalue 
)
Value:
{ name, CONFIG_TYPE_ ## conftype, offsetof(or_state_t, member), \
initvalue CONF_TEST_MEMBERS(or_state_t, conftype, member) }
Definition: or.h:4708

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.

◆ DUMMY_TYPECHECK_INSTANCE()

DUMMY_TYPECHECK_INSTANCE ( or_state_t  )

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.

Here is the call graph for this function:

◆ 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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MOCK_IMPL()

MOCK_IMPL ( or_state_t ,
get_or_state  ,
(void)   
)

Return the persistent state struct for this Tor.

◆ 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.

◆ or_state_loaded()

int or_state_loaded ( void  )

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

◆ 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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ or_state_save()

int or_state_save ( time_t  now)

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

Here is the caller graph for this function:

◆ 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

Here is the call graph for this function: