Bitcoin
Classes | Namespaces | Functions | Variables
system.cpp File Reference
#include <util/system.h>
#include <chainparamsbase.h>
#include <util/strencodings.h>
#include <stdarg.h>
#include <algorithm>
#include <fcntl.h>
#include <sched.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <thread>

Classes

class  ArgsManagerHelper
 

Namespaces

 util
 

Functions

bool LockDirectory (const fs::path &directory, const std::string lockfile_name, bool probe_only)
 
void UnlockDirectory (const fs::path &directory, const std::string &lockfile_name)
 
void ReleaseDirectoryLocks ()
 
bool DirIsWritable (const fs::path &directory)
 
bool CheckDiskSpace (const fs::path &dir, uint64_t additional_bytes)
 
static bool InterpretBool (const std::string &strValue)
 
static bool InterpretNegatedOption (std::string &key, std::string &val)
 
bool HelpRequested (const ArgsManager &args)
 
void SetupHelpOptions (ArgsManager &args)
 
std::string HelpMessageGroup (const std::string &message)
 
std::string HelpMessageOpt (const std::string &option, const std::string &message)
 
static std::string FormatException (const std::exception *pex, const char *pszThread)
 
void PrintExceptionContinue (const std::exception *pex, const char *pszThread)
 
fs::path GetDefaultDataDir ()
 
const fs::path & GetBlocksDir ()
 
const fs::path & GetDataDir (bool fNetSpecific)
 
void ClearDatadirCache ()
 
fs::path GetConfigFile (const std::string &confPath)
 
static std::string TrimString (const std::string &str, const std::string &pattern)
 
static bool GetConfigOptions (std::istream &stream, const std::string &filepath, std::string &error, std::vector< std::pair< std::string, std::string >> &options, std::list< SectionInfo > &sections)
 
bool RenameOver (fs::path src, fs::path dest)
 
bool TryCreateDirectories (const fs::path &p)
 
bool FileCommit (FILE *file)
 
bool TruncateFile (FILE *file, unsigned int length)
 
int RaiseFileDescriptorLimit (int nMinFD)
 
void AllocateFileRange (FILE *file, unsigned int offset, unsigned int length)
 
void SetupEnvironment ()
 
bool SetupNetworking ()
 
int GetNumCores ()
 
std::string CopyrightHolders (const std::string &strPrefix)
 
int64_t GetStartupTime ()
 
fs::path AbsPathForConfigVal (const fs::path &path, bool net_specific)
 
int ScheduleBatchPriority ()
 

Variables

const int64_t nStartupTime = GetTime()
 
const char *const BITCOIN_CONF_FILENAME = "bitcoin.conf"
 
ArgsManager gArgs
 
static std::map< std::string, std::unique_ptr< fsbridge::FileLock > > dir_locks
 
static std::mutex cs_dir_locks
 
static const int screenWidth = 79
 
static const int optIndent = 2
 
static const int msgIndent = 7
 
static fs::path g_blocks_path_cache_net_specific
 
static fs::path pathCached
 
static fs::path pathCachedNetSpecific
 
static RecursiveMutex csPathCached
 

Function Documentation

◆ AbsPathForConfigVal()

fs::path AbsPathForConfigVal ( const fs::path &  path,
bool  net_specific = true 
)

Most paths passed as configuration arguments are treated as relative to the datadir if they are not absolute.

Parameters
pathThe path to be conditionally prefixed with datadir.
net_specificForwarded to GetDataDir().
Returns
The normalized path.

◆ AllocateFileRange()

void AllocateFileRange ( FILE *  file,
unsigned int  offset,
unsigned int  length 
)

this function tries to make a particular range of a file allocated (corresponding to disk space) it is advisory, and the range specified in the arguments will never contain live data

◆ CheckDiskSpace()

bool CheckDiskSpace ( const fs::path &  dir,
uint64_t  additional_bytes 
)

◆ ClearDatadirCache()

void ClearDatadirCache ( )

Tests only

◆ CopyrightHolders()

std::string CopyrightHolders ( const std::string &  strPrefix)

◆ DirIsWritable()

bool DirIsWritable ( const fs::path &  directory)

◆ FileCommit()

bool FileCommit ( FILE *  file)

◆ FormatException()

static std::string FormatException ( const std::exception *  pex,
const char *  pszThread 
)
static

◆ GetBlocksDir()

const fs::path& GetBlocksDir ( )

◆ GetConfigFile()

fs::path GetConfigFile ( const std::string &  confPath)

◆ GetConfigOptions()

static bool GetConfigOptions ( std::istream &  stream,
const std::string &  filepath,
std::string &  error,
std::vector< std::pair< std::string, std::string >> &  options,
std::list< SectionInfo > &  sections 
)
static

◆ GetDataDir()

const fs::path& GetDataDir ( bool  fNetSpecific)

◆ GetDefaultDataDir()

fs::path GetDefaultDataDir ( )

◆ GetNumCores()

int GetNumCores ( )

Return the number of cores available on the current system.

Note
This does count virtual cores, such as those provided by HyperThreading.

◆ GetStartupTime()

int64_t GetStartupTime ( )

Server/client environment: argument handling, config file parsing, thread wrappers, startup time

◆ HelpMessageGroup()

std::string HelpMessageGroup ( const std::string &  message)

Format a string to be used as group of options in help messages

Parameters
messageGroup name (e.g. "RPC server options:")
Returns
the formatted string

◆ HelpMessageOpt()

std::string HelpMessageOpt ( const std::string &  option,
const std::string &  message 
)

Format a string to be used as option description in help messages

Parameters
optionOption message (e.g. "-rpcuser=<user>")
messageOption description (e.g. "Username for JSON-RPC connections")
Returns
the formatted string

◆ HelpRequested()

bool HelpRequested ( const ArgsManager args)
Returns
true if help has been requested via a command-line arg

◆ InterpretBool()

static bool InterpretBool ( const std::string &  strValue)
static

Interpret a string argument as a boolean.

The definition of atoi() requires that non-numeric string values like "foo", return 0. This means that if a user unintentionally supplies a non-integer argument here, the return value is always false. This means that -foo=false does what the user probably expects, but -foo=true is well defined but does not do what they probably expected.

The return value of atoi() is undefined when given input not representable as an int. On most systems this means string value between "-2147483648" and "2147483647" are well defined (this method will return true). Setting -txindex=2147483648 on most systems, however, is probably undefined.

For a more extensive discussion of this topic (and a wide range of opinions on the Right Way to change this code), see PR12713.

◆ InterpretNegatedOption()

static bool InterpretNegatedOption ( std::string &  key,
std::string &  val 
)
static

Interpret -nofoo as if the user supplied -foo=0.

This method also tracks when the -no form was supplied, and if so, checks whether there was a double-negative (-nofoo=0 -> -foo=1).

If there was not a double negative, it removes the "no" from the key, and returns true, indicating the caller should clear the args vector to indicate a negated option.

If there was a double negative, it removes "no" from the key, sets the value to "1" and returns false.

If there was no "no", it leaves key and value untouched and returns false.

Where an option was negated can be later checked using the IsArgNegated() method. One use case for this is to have a way to disable options that are not normally boolean (e.g. using -nodebuglogfile to request that debug log output is not sent to any file at all).

◆ LockDirectory()

bool LockDirectory ( const fs::path &  directory,
const std::string  lockfile_name,
bool  probe_only 
)

◆ PrintExceptionContinue()

void PrintExceptionContinue ( const std::exception *  pex,
const char *  pszThread 
)

◆ RaiseFileDescriptorLimit()

int RaiseFileDescriptorLimit ( int  nMinFD)

this function tries to raise the file descriptor limit to the requested number. It returns the actual file descriptor limit (which may be more or less than nMinFD)

◆ ReleaseDirectoryLocks()

void ReleaseDirectoryLocks ( )

Release all directory locks. This is used for unit testing only, at runtime the global destructor will take care of the locks.

◆ RenameOver()

bool RenameOver ( fs::path  src,
fs::path  dest 
)

◆ ScheduleBatchPriority()

int ScheduleBatchPriority ( )

On platforms that support it, tell the kernel the calling thread is CPU-intensive and non-interactive. See SCHED_BATCH in sched(7) for details.

Returns
The return value of sched_setschedule(), or 1 on systems without sched_setschedule().

◆ SetupEnvironment()

void SetupEnvironment ( )

◆ SetupHelpOptions()

void SetupHelpOptions ( ArgsManager args)

Add help options to the args manager

◆ SetupNetworking()

bool SetupNetworking ( )

◆ TrimString()

static std::string TrimString ( const std::string &  str,
const std::string &  pattern 
)
static

◆ TruncateFile()

bool TruncateFile ( FILE *  file,
unsigned int  length 
)

◆ TryCreateDirectories()

bool TryCreateDirectories ( const fs::path &  p)

Ignores exceptions thrown by Boost's create_directories if the requested directory exists. Specifically handles case where path p exists, but it wasn't possible for the user to write to the parent directory.

◆ UnlockDirectory()

void UnlockDirectory ( const fs::path &  directory,
const std::string &  lockfile_name 
)

Variable Documentation

◆ BITCOIN_CONF_FILENAME

const char* const BITCOIN_CONF_FILENAME = "bitcoin.conf"

◆ cs_dir_locks

std::mutex cs_dir_locks
static

Mutex to protect dir_locks.

◆ csPathCached

RecursiveMutex csPathCached
static

◆ dir_locks

std::map<std::string, std::unique_ptr<fsbridge::FileLock> > dir_locks
static

A map that contains all the currently held directory locks. After successful locking, these will be held here until the global destructor cleans them up and thus automatically unlocks them, or ReleaseDirectoryLocks is called.

◆ g_blocks_path_cache_net_specific

fs::path g_blocks_path_cache_net_specific
static

◆ gArgs

ArgsManager gArgs

◆ msgIndent

const int msgIndent = 7
static

◆ nStartupTime

const int64_t nStartupTime = GetTime()

◆ optIndent

const int optIndent = 2
static

◆ pathCached

fs::path pathCached
static

◆ pathCachedNetSpecific

fs::path pathCachedNetSpecific
static

◆ screenWidth

const int screenWidth = 79
static