LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
line::platform Namespace Reference

Classes

struct  FileStat
 What stat_file reports. More...

Typedefs

typedef std::intptr_t socket_t
 A TCP socket: a POSIX descriptor or a Winsock SOCKET.

Enumerations

enum class  ConnectFailure { None , Resolve , Connect }
 Why tcp_connect failed, so the caller keeps its own wording. More...

Functions

char path_list_separator ()
const char * null_device ()
const char * exe_suffix ()
std::string temp_root ()
 TMPDIR, else /tmp.
std::string home_dir ()
 HOME, or empty.
bool is_regular_file (const std::string &p)
bool is_directory (const std::string &p)
bool is_executable (const std::string &p)
 Exists and the caller may execute it (a directory with +x included, as access() says).
bool is_writable (const std::string &p)
bool stat_file (const std::string &p, FileStat &out)
std::string find_on_path (const std::string &name)
 The first dir/name on PATH that is a regular executable file, or empty.
std::string self_exe_path ()
 This executable's path, or empty when it cannot be read.
std::string current_dir ()
std::string host_sysname ()
 uname -s, or empty.
std::string host_machine ()
 uname -m, or empty.
bool make_dir_or_exists (const std::string &path)
 mkdir 0700; an existing entry is success.
std::string make_temp_dir (const std::string &tmpl)
 mkdtemp on a template ending in XXXXXX; empty on failure.
int make_temp_file (std::string &tmpl)
 mkstemp: replaces the XXXXXX in tmpl and returns an open descriptor, or -1.
int dup_fd (int fd)
int dup2_fd (int from, int to)
 POSIX dup2 returns the new descriptor; the CRT's _dup2 returns 0.
int close_fd (int fd)
bool remove_file (const std::string &p)
bool rename_replace (const std::string &from, const std::string &to)
void remove_tree (const std::string &dir)
 Remove a directory and everything under it, not following symlinks.
long process_id ()
long long free_disk_bytes (const std::string &path)
 Bytes available to this user on the filesystem holding path; -1 if unknown.
double available_memory_bytes ()
 Available physical memory in bytes; negative when the host cannot say.
long long monotonic_millis ()
 Milliseconds on the monotonic clock, for deadlines that survive a clock step.
void sleep_millis (long millis)
bool docker_user_spec (std::string &out)
 "uid:gid" for docker run --user; false where the notion does not apply.
bool spawn_detached (const std::vector< std::string > &argv)
 Start argv detached (own session, std streams on /dev/null, double fork so no zombie).
socket_t tcp_connect (const std::string &host, int port, int connectMillis, ConnectFailure &why, std::string &lastError)
 Connect with a deadline on the connect itself: each resolved address is tried non-blocking, then polled for writability, and SO_ERROR read back.
void set_io_timeout (socket_t s, int millis)
 Set the receive and send timeouts; not positive leaves them as they are.
socket_t tcp_connect_blocking (const std::string &host, const std::string &port, double ioSeconds, ConnectFailure &why)
 Connect with the I/O timeouts set BEFORE a blocking connect, so they bound the connect as well (Linux honours SO_SNDTIMEO there).
socket_t tcp_listen_any (int port, int backlog, std::string &stage)
 An IPv4 listener on every interface with SO_REUSEADDR.
socket_t tcp_accept (socket_t listener)
int free_loopback_port (std::string &stage)
 A loopback port the kernel reports free (bind port 0, read it back), or -1 with the failing step in stage ("socket", "bind" or "getsockname").
long send_some (socket_t s, const char *p, std::size_t n)
 send() without SIGPIPE; bytes sent or -1.
long recv_some (socket_t s, char *p, std::size_t n)
void close_socket (socket_t s)
int last_socket_error ()
bool err_interrupted (int e)
bool err_timed_out (int e)
 A Winsock receive timeout reports WSAETIMEDOUT where POSIX reports EAGAIN.
std::string socket_error_text (int e)

Variables

static const socket_t kInvalidSocket = -1

Typedef Documentation

◆ socket_t

typedef std::intptr_t line::platform::socket_t

A TCP socket: a POSIX descriptor or a Winsock SOCKET.

Definition at line 61 of file platform.h.

Enumeration Type Documentation

◆ ConnectFailure

enum class line::platform::ConnectFailure
strong

Why tcp_connect failed, so the caller keeps its own wording.

Enumerator
None 
Resolve 
Connect 

Definition at line 73 of file platform.h.

Function Documentation

◆ available_memory_bytes()

double line::platform::available_memory_bytes ( )
inline

Available physical memory in bytes; negative when the host cannot say.

Definition at line 277 of file platform.h.

References available_memory_bytes().

Referenced by available_memory_bytes(), and line::mc::ctmc_available_memory_bytes().

◆ close_fd()

int line::platform::close_fd ( int fd)
inline

Definition at line 239 of file platform.h.

References close_fd().

Referenced by close_fd(), line::io::jsim_stage_stdin(), run_invocation(), and run_server().

◆ close_socket()

void line::platform::close_socket ( socket_t s)
inline

Definition at line 522 of file platform.h.

References close_socket(), and kInvalidSocket.

Referenced by close_socket(), line::ws::Server::serve_one(), and line::ws::Server::~Server().

◆ current_dir()

std::string line::platform::current_dir ( )
inline

Definition at line 198 of file platform.h.

References current_dir().

Referenced by current_dir().

◆ docker_user_spec()

bool line::platform::docker_user_spec ( std::string & out)
inline

"uid:gid" for docker run --user; false where the notion does not apply.

Docker Desktop maps the caller itself; there is no uid:gid to pass.

Definition at line 302 of file platform.h.

References docker_user_spec().

Referenced by docker_user_spec(), and line::jmt::jmt_run_docker().

◆ dup2_fd()

int line::platform::dup2_fd ( int from,
int to )
inline

POSIX dup2 returns the new descriptor; the CRT's _dup2 returns 0.

Definition at line 238 of file platform.h.

References dup2_fd().

Referenced by dup2_fd(), and FdCapture::FdCapture().

◆ dup_fd()

int line::platform::dup_fd ( int fd)
inline

Definition at line 237 of file platform.h.

References dup_fd().

Referenced by dup_fd(), and FdCapture::FdCapture().

◆ err_interrupted()

bool line::platform::err_interrupted ( int e)
inline

Definition at line 527 of file platform.h.

References err_interrupted().

Referenced by err_interrupted().

◆ err_timed_out()

bool line::platform::err_timed_out ( int e)
inline

A Winsock receive timeout reports WSAETIMEDOUT where POSIX reports EAGAIN.

Definition at line 528 of file platform.h.

References err_timed_out().

Referenced by err_timed_out().

◆ exe_suffix()

const char * line::platform::exe_suffix ( )
inline

Definition at line 129 of file platform.h.

References exe_suffix().

Referenced by exe_suffix().

◆ find_on_path()

std::string line::platform::find_on_path ( const std::string & name)
inline

The first dir/name on PATH that is a regular executable file, or empty.

The first dir/name on PATH that is a regular file, trying name.exe too when name carries no extension, as cmd.exe resolves a bare command.

Definition at line 174 of file platform.h.

References find_on_path(), and is_regular_file().

Referenced by line::io::tikz_detail::find_on_path(), find_on_path(), and line::io::wfcommons_detail::which_on_path().

◆ free_disk_bytes()

long long line::platform::free_disk_bytes ( const std::string & path)
inline

Bytes available to this user on the filesystem holding path; -1 if unknown.

Definition at line 268 of file platform.h.

References free_disk_bytes().

Referenced by free_disk_bytes().

◆ free_loopback_port()

int line::platform::free_loopback_port ( std::string & stage)
inline

A loopback port the kernel reports free (bind port 0, read it back), or -1 with the failing step in stage ("socket", "bind" or "getsockname").

Definition at line 481 of file platform.h.

References free_loopback_port().

Referenced by free_loopback_port().

◆ home_dir()

std::string line::platform::home_dir ( )
inline

HOME, or empty.

Definition at line 138 of file platform.h.

References home_dir().

Referenced by home_dir(), and line::util::make_temp_dir().

◆ host_machine()

std::string line::platform::host_machine ( )
inline

uname -m, or empty.

Definition at line 210 of file platform.h.

References host_machine().

Referenced by host_machine(), and line::mc::gate_detail::machine_signature().

◆ host_sysname()

std::string line::platform::host_sysname ( )
inline

uname -s, or empty.

Definition at line 204 of file platform.h.

References host_sysname().

Referenced by host_sysname(), and line::mc::gate_detail::machine_signature().

◆ is_directory()

bool line::platform::is_directory ( const std::string & p)
inline

Definition at line 148 of file platform.h.

References is_directory().

Referenced by is_directory().

◆ is_executable()

bool line::platform::is_executable ( const std::string & p)
inline

Exists and the caller may execute it (a directory with +x included, as access() says).

Windows has no execute bit: anything that exists may be run (or entered).

Definition at line 154 of file platform.h.

References is_executable().

Referenced by is_executable().

◆ is_regular_file()

bool line::platform::is_regular_file ( const std::string & p)
inline

Definition at line 143 of file platform.h.

References is_regular_file().

Referenced by line::io::tikz_detail::file_exists(), find_on_path(), and is_regular_file().

◆ is_writable()

bool line::platform::is_writable ( const std::string & p)
inline

Definition at line 159 of file platform.h.

References is_writable().

Referenced by is_writable().

◆ last_socket_error()

int line::platform::last_socket_error ( )
inline

Definition at line 526 of file platform.h.

References last_socket_error().

Referenced by last_socket_error().

◆ make_dir_or_exists()

bool line::platform::make_dir_or_exists ( const std::string & path)
inline

mkdir 0700; an existing entry is success.

Definition at line 216 of file platform.h.

References make_dir_or_exists().

Referenced by make_dir_or_exists().

◆ make_temp_dir()

std::string line::platform::make_temp_dir ( const std::string & tmpl)
inline

mkdtemp on a template ending in XXXXXX; empty on failure.

Definition at line 221 of file platform.h.

References make_temp_dir().

Referenced by make_temp_dir(), and line::util::make_temp_dir().

◆ make_temp_file()

int line::platform::make_temp_file ( std::string & tmpl)
inline

mkstemp: replaces the XXXXXX in tmpl and returns an open descriptor, or -1.

Definition at line 229 of file platform.h.

References make_temp_file().

Referenced by FdCapture::FdCapture(), line::io::jsim_stage_stdin(), make_temp_file(), run_invocation(), and run_server().

◆ monotonic_millis()

long long line::platform::monotonic_millis ( )
inline

Milliseconds on the monotonic clock, for deadlines that survive a clock step.

Definition at line 288 of file platform.h.

References monotonic_millis().

Referenced by monotonic_millis().

◆ null_device()

const char * line::platform::null_device ( )
inline

Definition at line 128 of file platform.h.

References null_device().

Referenced by null_device().

◆ path_list_separator()

char line::platform::path_list_separator ( )
inline

Definition at line 127 of file platform.h.

References path_list_separator().

Referenced by path_list_separator().

◆ process_id()

long line::platform::process_id ( )
inline

Definition at line 265 of file platform.h.

References process_id().

Referenced by line::jmt::jmt_run_docker(), and process_id().

◆ recv_some()

long line::platform::recv_some ( socket_t s,
char * p,
std::size_t n )
inline

Definition at line 518 of file platform.h.

References recv_some().

Referenced by recv_some().

◆ remove_file()

bool line::platform::remove_file ( const std::string & p)
inline

Definition at line 240 of file platform.h.

References remove_file().

Referenced by remove_file().

◆ remove_tree()

void line::platform::remove_tree ( const std::string & dir)
inline

Remove a directory and everything under it, not following symlinks.

Remove a directory and everything under it.

A directory junction or symlink (a reparse point) is removed as an entry and NOT descended into, the Windows form of lstat's "do not follow". A read-only file is made writable first, since DeleteFile refuses it and unlink on POSIX would not care.

Definition at line 246 of file platform.h.

References remove_tree().

Referenced by remove_tree().

◆ rename_replace()

bool line::platform::rename_replace ( const std::string & from,
const std::string & to )
inline

Definition at line 241 of file platform.h.

References rename_replace().

Referenced by rename_replace().

◆ self_exe_path()

std::string line::platform::self_exe_path ( )
inline

This executable's path, or empty when it cannot be read.

Definition at line 191 of file platform.h.

References self_exe_path().

Referenced by self_exe_path().

◆ send_some()

long line::platform::send_some ( socket_t s,
const char * p,
std::size_t n )
inline

send() without SIGPIPE; bytes sent or -1.

Winsock raises no SIGPIPE, so a plain send() is the MSG_NOSIGNAL send.

Definition at line 509 of file platform.h.

References send_some().

Referenced by send_some().

◆ set_io_timeout()

void line::platform::set_io_timeout ( socket_t s,
int millis )
inline

Set the receive and send timeouts; not positive leaves them as they are.

Definition at line 397 of file platform.h.

References set_io_timeout().

Referenced by set_io_timeout().

◆ sleep_millis()

void line::platform::sleep_millis ( long millis)
inline

Definition at line 294 of file platform.h.

References sleep_millis().

Referenced by sleep_millis().

◆ socket_error_text()

std::string line::platform::socket_error_text ( int e)
inline

Definition at line 529 of file platform.h.

References socket_error_text().

Referenced by socket_error_text().

◆ spawn_detached()

bool line::platform::spawn_detached ( const std::vector< std::string > & argv)
inline

Start argv detached (own session, std streams on /dev/null, double fork so no zombie).

Start argv detached from this console with no standard streams, the Windows form of the POSIX double fork.

Like the POSIX version, success means the process was started, not that the program it names did what was asked.

Definition at line 309 of file platform.h.

References spawn_detached().

Referenced by line::io::tikz_detail::spawn_detached(), and spawn_detached().

◆ stat_file()

bool line::platform::stat_file ( const std::string & p,
FileStat & out )
inline

◆ tcp_accept()

socket_t line::platform::tcp_accept ( socket_t listener)
inline

Definition at line 472 of file platform.h.

References tcp_accept().

Referenced by line::ws::Server::serve_one(), and tcp_accept().

◆ tcp_connect()

socket_t line::platform::tcp_connect ( const std::string & host,
int port,
int connectMillis,
ConnectFailure & why,
std::string & lastError )
inline

Connect with a deadline on the connect itself: each resolved address is tried non-blocking, then polled for writability, and SO_ERROR read back.

lastError is the reason the last address failed.

Definition at line 340 of file platform.h.

References Connect, kInvalidSocket, None, Resolve, and tcp_connect().

Referenced by tcp_connect().

◆ tcp_connect_blocking()

socket_t line::platform::tcp_connect_blocking ( const std::string & host,
const std::string & port,
double ioSeconds,
ConnectFailure & why )
inline

Connect with the I/O timeouts set BEFORE a blocking connect, so they bound the connect as well (Linux honours SO_SNDTIMEO there).

The I/O timeouts are set first, as on POSIX.

port may be a service name; ioSeconds of zero leaves the socket unbounded.

Winsock does NOT bound a blocking connect by SO_SNDTIMEO the way Linux does, so the connect is made non-blocking under the same bound instead; zero leaves both unbounded.

Definition at line 412 of file platform.h.

References Connect, kInvalidSocket, None, Resolve, and tcp_connect_blocking().

Referenced by tcp_connect_blocking().

◆ tcp_listen_any()

socket_t line::platform::tcp_listen_any ( int port,
int backlog,
std::string & stage )
inline

An IPv4 listener on every interface with SO_REUSEADDR.

No SO_REUSEADDR here, on purpose: on Windows it lets a second socket STEAL a port that is in active use, whereas the POSIX meaning (rebind past TIME_WAIT) is already Winsock's default.

On failure returns kInvalidSocket and names the step in stage ("socket", "bind" or "listen").

Definition at line 446 of file platform.h.

References kInvalidSocket, and tcp_listen_any().

Referenced by line::ws::Server::Server(), and tcp_listen_any().

◆ temp_root()

std::string line::platform::temp_root ( )
inline

Variable Documentation

◆ kInvalidSocket

const socket_t line::platform::kInvalidSocket = -1
static

Definition at line 62 of file platform.h.

Referenced by close_socket(), tcp_connect(), tcp_connect_blocking(), and tcp_listen_any().