Bitcoin Core  22.99.0
P2P Digital Currency
Namespaces | Functions | Variables
system.cpp File Reference
#include <util/system.h>
#include <boost/process.hpp>
#include <chainparamsbase.h>
#include <sync.h>
#include <util/check.h>
#include <util/getuniquepath.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/translation.h>
#include <algorithm>
#include <cassert>
#include <fcntl.h>
#include <sched.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <boost/algorithm/string/replace.hpp>
#include <thread>
#include <typeinfo>
#include <univalue.h>
Include dependency graph for system.cpp:

Go to the source code of this file.

Namespaces

 util
 

Functions

static std::map< std::string, std::unique_ptr< fsbridge::FileLock > > dir_locks GUARDED_BY (cs_dir_locks)
 A map that contains all the currently held directory locks. More...
 
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 ()
 Release all directory locks. More...
 
bool DirIsWritable (const fs::path &directory)
 
bool CheckDiskSpace (const fs::path &dir, uint64_t additional_bytes)
 
std::streampos GetFileSize (const char *path, std::streamsize max)
 Get the size of a file by scanning it. More...
 
static bool InterpretBool (const std::string &strValue)
 Interpret a string argument as a boolean. More...
 
static std::string SettingName (const std::string &arg)
 
static util::SettingsValue InterpretOption (std::string &section, std::string &key, const std::string &value)
 Interpret -nofoo as if the user supplied -foo=0. More...
 
static bool CheckValid (const std::string &key, const util::SettingsValue &val, unsigned int flags, std::string &error)
 Check settings value validity according to flags. More...
 
static void SaveErrors (const std::vector< std::string > errors, std::vector< std::string > *error_out)
 
bool HelpRequested (const ArgsManager &args)
 
void SetupHelpOptions (ArgsManager &args)
 Add help options to the args manager. More...
 
std::string HelpMessageGroup (const std::string &message)
 Format a string to be used as group of options in help messages. More...
 
std::string HelpMessageOpt (const std::string &option, const std::string &message)
 Format a string to be used as option description in help messages. More...
 
static std::string FormatException (const std::exception *pex, const char *pszThread)
 
void PrintExceptionContinue (const std::exception *pex, const char *pszThread)
 
fs::path GetDefaultDataDir ()
 
bool CheckDataDirOption ()
 
fs::path GetConfigFile (const std::string &confPath)
 
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)
 Ignores exceptions thrown by Boost's create_directories if the requested directory exists. More...
 
bool FileCommit (FILE *file)
 Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsync(). More...
 
void DirectoryCommit (const fs::path &dirname)
 Sync directory contents. More...
 
bool TruncateFile (FILE *file, unsigned int length)
 
int RaiseFileDescriptorLimit (int nMinFD)
 this function tries to raise the file descriptor limit to the requested number. More...
 
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 More...
 
std::string ShellEscape (const std::string &arg)
 
UniValue RunCommandParseJSON (const std::string &str_command, const std::string &str_std_in)
 Execute a command which returns JSON, and parse the result. More...
 
void SetupEnvironment ()
 
bool SetupNetworking ()
 
int GetNumCores ()
 Return the number of cores available on the current system. More...
 
std::string CopyrightHolders (const std::string &strPrefix)
 
int64_t GetStartupTime ()
 
fs::path AbsPathForConfigVal (const fs::path &path, bool net_specific)
 Most paths passed as configuration arguments are treated as relative to the datadir if they are not absolute. More...
 
void ScheduleBatchPriority ()
 On platforms that support it, tell the kernel the calling thread is CPU-intensive and non-interactive. More...
 

Variables

const int64_t nStartupTime = GetTime()
 
const char *const BITCOIN_CONF_FILENAME = "bitcoin.conf"
 
const char *const BITCOIN_SETTINGS_FILENAME = "settings.json"
 
ArgsManager gArgs
 
static Mutex cs_dir_locks
 Mutex to protect dir_locks. More...
 
static const int screenWidth = 79
 
static const int optIndent = 2
 
static const int msgIndent = 7
 

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_specificUse network specific datadir variant
Returns
The normalized path.

Definition at line 1362 of file system.cpp.

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

◆ 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

Definition at line 1166 of file system.cpp.

Here is the caller graph for this function:

◆ CheckDataDirOption()

bool CheckDataDirOption ( )

Definition at line 811 of file system.cpp.

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

◆ CheckDiskSpace()

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

Definition at line 144 of file system.cpp.

Here is the caller graph for this function:

◆ CheckValid()

static bool CheckValid ( const std::string &  key,
const util::SettingsValue val,
unsigned int  flags,
std::string &  error 
)
static

Check settings value validity according to flags.

TODO: Add more meaningful error checks here in the future See "here's how the flags are meant to behave" in https://github.com/bitcoin/bitcoin/pull/16097#issuecomment-514627823

Definition at line 234 of file system.cpp.

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

◆ CopyrightHolders()

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

Definition at line 1344 of file system.cpp.

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

◆ DirectoryCommit()

void DirectoryCommit ( const fs::path &  dirname)

Sync directory contents.

This is required on some environments to ensure that newly created files are committed to disk.

Definition at line 1120 of file system.cpp.

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

◆ DirIsWritable()

bool DirIsWritable ( const fs::path &  directory)

Definition at line 131 of file system.cpp.

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

◆ FileCommit()

bool FileCommit ( FILE *  file)

Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsync().

Definition at line 1089 of file system.cpp.

Here is the caller graph for this function:

◆ FormatException()

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

Definition at line 763 of file system.cpp.

Here is the caller graph for this function:

◆ GetConfigFile()

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

Definition at line 817 of file system.cpp.

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

◆ 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

Definition at line 822 of file system.cpp.

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

◆ GetDefaultDataDir()

fs::path GetDefaultDataDir ( )

Definition at line 786 of file system.cpp.

Here is the caller graph for this function:

◆ GetFileSize()

std::streampos GetFileSize ( const char *  path,
std::streamsize  max = std::numeric_limits< std::streamsize >::max() 
)

Get the size of a file by scanning it.

Parameters
[in]pathThe file path
[in]maxStop seeking beyond this limit
Returns
The file size or max

Definition at line 152 of file system.cpp.

Here is the caller graph for this function:

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

Definition at line 1339 of file system.cpp.

Here is the caller graph for this function:

◆ GetStartupTime()

int64_t GetStartupTime ( )

Definition at line 1357 of file system.cpp.

Here is the caller graph for this function:

◆ GUARDED_BY()

static std::map<std::string, std::unique_ptr<fsbridge::FileLock> > dir_locks GUARDED_BY ( cs_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.

◆ 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

Definition at line 752 of file system.cpp.

Here is the caller graph for this function:

◆ 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

Definition at line 756 of file system.cpp.

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

◆ HelpRequested()

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

Definition at line 737 of file system.cpp.

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

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

Definition at line 175 of file system.cpp.

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

◆ InterpretOption()

static util::SettingsValue InterpretOption ( std::string &  section,
std::string &  key,
const std::string &  value 
)
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 false.

If there was a double negative, it removes "no" from the key, and returns true.

If there was no "no", it returns the string value untouched.

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

Definition at line 207 of file system.cpp.

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

◆ LockDirectory()

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

Definition at line 95 of file system.cpp.

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

◆ PrintExceptionContinue()

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

Definition at line 779 of file system.cpp.

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

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

Definition at line 1143 of file system.cpp.

Here is the caller graph for this function:

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

Definition at line 125 of file system.cpp.

◆ RenameOver()

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

Definition at line 1059 of file system.cpp.

Here is the caller graph for this function:

◆ RunCommandParseJSON()

UniValue RunCommandParseJSON ( const std::string &  str_command,
const std::string &  str_std_in = "" 
)

Execute a command which returns JSON, and parse the result.

Parameters
str_commandThe command to execute, including any arguments
str_std_instring to pass to stdin
Returns
parsed JSON

Definition at line 1251 of file system.cpp.

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

◆ SaveErrors()

static void SaveErrors ( const std::vector< std::string >  errors,
std::vector< std::string > *  error_out 
)
static

Definition at line 527 of file system.cpp.

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

◆ ScheduleBatchPriority()

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

Definition at line 1370 of file system.cpp.

Here is the caller graph for this function:

◆ SettingName()

static std::string SettingName ( const std::string &  arg)
static

Definition at line 182 of file system.cpp.

Here is the caller graph for this function:

◆ SetupEnvironment()

void SetupEnvironment ( )

Definition at line 1290 of file system.cpp.

Here is the caller graph for this function:

◆ SetupHelpOptions()

void SetupHelpOptions ( ArgsManager args)

Add help options to the args manager.

Definition at line 742 of file system.cpp.

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

◆ SetupNetworking()

bool SetupNetworking ( )

Definition at line 1327 of file system.cpp.

Here is the caller graph for this function:

◆ ShellEscape()

std::string ShellEscape ( const std::string &  arg)

Definition at line 1229 of file system.cpp.

Here is the caller graph for this function:

◆ TruncateFile()

bool TruncateFile ( FILE *  file,
unsigned int  length 
)

Definition at line 1131 of file system.cpp.

Here is the caller graph for this function:

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

Definition at line 1075 of file system.cpp.

Here is the caller graph for this function:

◆ UnlockDirectory()

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

Definition at line 119 of file system.cpp.

Here is the caller graph for this function:

Variable Documentation

◆ BITCOIN_CONF_FILENAME

const char* const BITCOIN_CONF_FILENAME = "bitcoin.conf"

Definition at line 81 of file system.cpp.

◆ BITCOIN_SETTINGS_FILENAME

const char* const BITCOIN_SETTINGS_FILENAME = "settings.json"

Definition at line 82 of file system.cpp.

◆ cs_dir_locks

Mutex cs_dir_locks
static

Mutex to protect dir_locks.

Definition at line 87 of file system.cpp.

◆ gArgs

ArgsManager gArgs

Definition at line 84 of file system.cpp.

◆ msgIndent

const int msgIndent = 7
static

Definition at line 750 of file system.cpp.

◆ nStartupTime

const int64_t nStartupTime = GetTime()

Definition at line 79 of file system.cpp.

◆ optIndent

const int optIndent = 2
static

Definition at line 749 of file system.cpp.

◆ screenWidth

const int screenWidth = 79
static

Definition at line 748 of file system.cpp.