scclib
Stable Cloud Computing C++ Library
Files | Functions
Signal-safe C library wrapper

Provides signal safe wrapper for commonly used C library APIs. More...

Collaboration diagram for Signal-safe C library wrapper:

Files

file  safe_clib.h
 Signal-safe C library wrapper.
 
file  safe_clib.cc
 Signal-safe C library wrapper implementation
 
file  safe_clib.cc
 Tests for Signal-safe C library wrapper.
 

Functions

int scc::util::safe_close (int fd)
 Signal safe close.
 
int scc::util::safe_close_throw (int fd)
 Signal safe close, throws system_error on error.
 
ssize_t scc::util::safe_read (int fd, void *buf, size_t count)
 Signal safe read.
 
ssize_t scc::util::safe_read_throw (int fd, void *buf, size_t count)
 Signal safe read, throws system_error on error.
 
ssize_t scc::util::safe_write (int fd, const void *buf, size_t count)
 Signal safe write.
 
ssize_t scc::util::safe_write_throw (int fd, const void *buf, size_t count)
 Signal safe write, throws system_error on error.
 
int scc::util::safe_dup (int oldfd)
 Signal safe dup.
 
int scc::util::safe_dup_throw (int oldfd)
 Signal safe dup, throws system_error on error.
 
int scc::util::safe_dup2 (int oldfd, int newfd)
 Signal safe dup2.
 
int scc::util::safe_dup2_throw (int oldfd, int newfd)
 Signal safe dup2, throws system_error on error.
 
int scc::util::safe_open (const char *pathname, int flags)
 Signal safe open.
 
int scc::util::safe_open (const char *pathname, int flags, mode_t mode)
 Signal safe open.
 
int scc::util::safe_open_throw (const char *pathname, int flags)
 Signal safe open, throws system_error on error.
 
int scc::util::safe_open_throw (const char *pathname, int flags, mode_t mode)
 Signal safe open, throws system_error on error.
 
FILE * scc::util::safe_fopen (const char *pathname, const char *mode)
 Signal safe fopen.
 
FILE * scc::util::safe_fopen_throw (const char *pathname, const char *mode)
 Signal safe fopen, throws system_error on error.
 
int scc::util::safe_fclose (FILE *stream)
 Signal safe fclose.
 
int scc::util::safe_fclose_throw (FILE *stream)
 Signal safe fclose, throws system_error on error.
 
int scc::util::safe_fscanf (FILE *stream, const char *format,...)
 Signal safe fscanf.
 
int scc::util::safe_fscanf_throw (FILE *stream, const char *format,...)
 Signal safe fscanf, throw on error.
 
ssize_t scc::util::safe_getline (char **lineptr, size_t *n, FILE *stream)
 Signal safe getline.
 
ssize_t scc::util::safe_getline_throw (char **lineptr, size_t *n, FILE *stream)
 Signal safe getline, throw on error.
 
int scc::util::safe_truncate (const char *path, off_t length)
 Signal safe truncate.
 
int scc::util::safe_truncate_throw (const char *path, off_t length)
 Signal safe truncate, throw on error.
 
int scc::util::safe_ftruncate (int fd, off_t length)
 Signal safe ftruncate.
 
int scc::util::safe_ftruncate_throw (int fd, off_t length)
 Signal safe ftruncate, throw on error.
 
int scc::util::safe_wait (int *wstatus)
 Signal safe wait.
 
int scc::util::safe_waitpid (pid_t pid, int *wstatus, int options)
 Signal safe waitpid.
 
int scc::util::safe_wait_throw (int *wstatus)
 Signal safe wait, throws system_error on error.
 
int scc::util::safe_waitpid_throw (pid_t pid, int *wstatus, int options)
 Signal safe waitpid, throws system_error on error.
 

Detailed Description

Provides signal safe wrapper for commonly used C library APIs.

Generally, APIs which are capable of EINTR error when interrupted a signal are included.

See https://man7.org/linux/man-pages/man7/signal.7.html

An example which uses safe clib apis to write into a sparse file, from scclib/util/unittest/fs.cc

TEST(FsExampleTest, sparse_and_trunc)
{
system_error err;
fs::remove_all("sandbox", &err);
fs::create_dir("sandbox");
auto curdir = fs::get_current_dir();
cout << "curdir: " << curdir << endl;
fs::change_dir("sandbox");
string s("this is a test of the emergency sizing system\n");
cout << "sparse test with string len=" << s.size() << endl;
string fn("sparse_file");
auto write_sparse = [&fn, &s](off_t loc)
{
// use clib to write data into the middle of the file
// if we use fstream, the system may truncate the file
// use system wrapper FileDesc to ensure the file is always closed
lseek(fd, loc, SEEK_SET);
safe_write_throw(fd, &s[0], s.size());
cout << "wrote " << s.size() << " bytes at loc " << loc << endl;
};
fs::set_size(fn, 16384); // 16 K file, with a 0 at the end
auto st = fs::file_stat(fn);
cout << "12288 hole at start of file:\n" << st << endl;
ASSERT_EQ(st.size, 16384);
ASSERT_EQ(st.alloc_size, 4096); // the last block has data
auto sm = fs::sparse_map(fn);
cout << setw(6) << "start" << setw(6) << "data" << endl;
for (auto& x : sm)
{
cout << setw(6) << x.first << setw(6) << x.second << endl;
}
std::map<int64_t,int64_t> ver;
ver[0] = 12287; // 12288 sized hole at 0 (beginning)
ASSERT_EQ(sm, ver);
write_sparse(1024); // write the string at 1024, causing first block to be written
st = fs::file_stat(fn);
cout << "8192 hole in middle of file\n" << st << endl;
ASSERT_EQ(st.alloc_size, 8192);
sm = fs::sparse_map(fn);
cout << setw(6) << "start" << setw(6) << "data" << endl;
for (auto& x : sm)
{
cout << setw(6) << x.first << setw(6) << x.second << endl;
}
ver.clear();
ver[4096] = 12287; // 8192 sized hole at 4096 (middle)
ASSERT_EQ(sm, ver);
fs::set_size(fn, 12288);
st = fs::file_stat(fn);
cout << "8192 hole at end of file\n" << st << endl;
ASSERT_EQ(st.alloc_size, 4096);
sm = fs::sparse_map(fn);
cout << setw(6) << "start" << setw(6) << "data" << endl;
for (auto& x : sm)
{
cout << setw(6) << x.first << setw(6) << x.second << endl;
}
ver.clear();
ver[4096] = 12287; // 8192 sized hole at 4096 (end)
ASSERT_EQ(sm, ver);
ifstream f(fn);
f.seekg(1024);
string val;
val.resize(s.size(), '\x01');
f.read(&val[0], val.size());
ASSERT_EQ(val, s);
fs::change_dir(curdir);
fs::remove_all("sandbox");
}
File descriptor.
Definition: filedesc.h:66
static std::string get_current_dir(std::system_error *=nullptr)
Get working directory.
Definition: fs.cc:454
static void create_reg(const std::string &, std::system_error *=nullptr)
Create a regular file.
Definition: fs.cc:298
static FileStat file_stat(const std::string &, std::system_error *=nullptr)
Get the file stat.
Definition: fs.cc:400
static void set_size(const std::string &, off_t, std::system_error *=nullptr)
Set the file size.
Definition: fs.cc:610
static void change_dir(const std::string &, std::system_error *=nullptr)
Change working directory.
Definition: fs.cc:440
static std::map< off_t, off_t > sparse_map(const std::string &, std::system_error *=nullptr)
Map of file sparseness.
Definition: fs.cc:525
static void create_dir(const std::string &, std::system_error *=nullptr)
Create a directory.
Definition: fs.cc:284
ssize_t safe_write_throw(int fd, const void *buf, size_t count)
Signal safe write, throws system_error on error.
Definition: safe_clib.cc:152
int safe_open_throw(const char *pathname, int flags)
Signal safe open, throws system_error on error.
Definition: safe_clib.cc:221
TEST(inet_example, client_server_stream_test)
[Inet client server]
Definition: inet.cc:521