Rewrite errno handling

This commit is contained in:
blankie 2023-01-17 22:35:08 +07:00
parent 398909328c
commit 6f5fa39b04
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
4 changed files with 47 additions and 42 deletions

View File

@ -13,17 +13,17 @@
static FILE* fopen_or_raise(const char* path, const char* mode, bool ignore_enoent) { static FILE* fopen_or_raise(const char* path, const char* mode, bool ignore_enoent) {
FILE* file = fopen(path, mode); FILE* file = fopen(path, mode);
if (file || (ignore_enoent && errno == ENOENT)) { if (!file && !(ignore_enoent && errno == ENOENT)) {
return file; throw_system_error(std::string("fopen(") + quote(path) + ')');
} }
throw make_system_error(std::string("fopen(") + quote(path) + ')'); return file;
} }
static void fclose_and_log(FILE* file) { static void fclose_and_log(FILE* file) {
if (!fclose(file)) { if (!fclose(file)) {
return; return;
} }
log(std::string("Failed to close a file: ") + make_system_error("fclose()").what()); log(std::string("Failed to close a file: fclose(): ") + strerror(errno));
} }
static bool write(const std::string& ptr, FILE* file) { static bool write(const std::string& ptr, FILE* file) {
@ -107,7 +107,7 @@ void create_config_folders_if_necessary() {
if (errno == EEXIST) { if (errno == EEXIST) {
continue; continue;
} }
throw make_system_error(std::string("mkdir(") + quote(path) + ')'); throw_system_error(std::string("mkdir(") + quote(path) + ')');
} }
} }
@ -141,15 +141,13 @@ static inline Config load_config(FILE* file) {
while (true) { while (true) {
errno = 0; errno = 0;
if (getline(&line, &line_capacity_size, file) < 0) { if (getline(&line, &line_capacity_size, file) < 0) {
// https://stackoverflow.com/questions/30569981/does-free-set-errno
int errsv = errno; int errsv = errno;
free(line); free(line);
if (errsv == ENOMEM) { if (errsv) {
throw std::bad_alloc(); throw_system_error(errsv, "getline()");
} else if (errsv != 0) {
throw make_system_error(errsv, "getline()");
} else {
break;
} }
break;
} }
if (line_capacity_size == 0 || line[0] == '\0' || line[0] == '#') { if (line_capacity_size == 0 || line[0] == '\0' || line[0] == '#') {
continue; continue;
@ -221,5 +219,5 @@ void write_config(const Config& config) {
if (!rename(tmp_config_file_path.c_str(), config_file_path.c_str())) { if (!rename(tmp_config_file_path.c_str(), config_file_path.c_str())) {
return; return;
} }
throw make_system_error(std::string("rename(") + quote(tmp_config_file_path) + ", " + quote(config_file_path) + ')'); throw_system_error(std::string("rename(") + quote(tmp_config_file_path) + ", " + quote(config_file_path) + ')');
} }

View File

@ -1,3 +1,4 @@
#include <cstring>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <sys/epoll.h> #include <sys/epoll.h>
@ -12,10 +13,10 @@
static void mark_nonblock(int fd) { static void mark_nonblock(int fd) {
int flags = fcntl(fd, F_GETFL); int flags = fcntl(fd, F_GETFL);
if (flags < 0) { if (flags < 0) {
throw make_system_error("fcntl(fd, F_GETFL)"); throw_system_error("fcntl(fd, F_GETFL)");
} }
if (!(flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { if (!(flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
throw make_system_error("fcntl(fd, F_SETFL)"); throw_system_error("fcntl(fd, F_SETFL)");
} }
} }
@ -25,9 +26,9 @@ LogcatThread::LogcatThread(bool* run_event_loop) {
struct epoll_event event = {.events = EPOLLIN | EPOLLET}; struct epoll_event event = {.events = EPOLLIN | EPOLLET};
if (pipe(fds)) { if (pipe(fds)) {
std::system_error e = make_system_error("pipe() for stdout"); int errsv = errno;
this->~LogcatThread(); this->~LogcatThread();
throw e; throw_system_error(errsv, "pipe() for stdout");
} }
this->_stdout_read_fd = fds[0]; this->_stdout_read_fd = fds[0];
this->_stdout_write_fd = fds[1]; this->_stdout_write_fd = fds[1];
@ -39,9 +40,9 @@ LogcatThread::LogcatThread(bool* run_event_loop) {
} }
if (pipe(fds)) { if (pipe(fds)) {
std::system_error e = make_system_error("pipe() for stderr"); int errsv = errno;
this->~LogcatThread(); this->~LogcatThread();
throw e; throw_system_error(errsv, "pipe() for stderr");
} }
this->_stderr_read_fd = fds[0]; this->_stderr_read_fd = fds[0];
this->_stderr_write_fd = fds[1]; this->_stderr_write_fd = fds[1];
@ -54,19 +55,21 @@ LogcatThread::LogcatThread(bool* run_event_loop) {
this->_epoll_fd = epoll_create1(EPOLL_CLOEXEC); this->_epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if (this->_epoll_fd == -1) { if (this->_epoll_fd == -1) {
throw make_system_error("epoll_create1()"); int errsv = errno;
this->~LogcatThread();
throw_system_error(errsv, "epoll_create1()");
} }
event.data.fd = this->_stdout_read_fd; event.data.fd = this->_stdout_read_fd;
if (epoll_ctl(this->_epoll_fd, EPOLL_CTL_ADD, this->_stdout_read_fd, &event)) { if (epoll_ctl(this->_epoll_fd, EPOLL_CTL_ADD, this->_stdout_read_fd, &event)) {
std::system_error e = make_system_error("epoll_ctl() for stdout"); int errsv = errno;
this->~LogcatThread(); this->~LogcatThread();
throw e; throw_system_error(errsv, "epoll_ctl() for stdout");
} }
event.data.fd = this->_stderr_read_fd; event.data.fd = this->_stderr_read_fd;
if (epoll_ctl(this->_epoll_fd, EPOLL_CTL_ADD, this->_stderr_read_fd, &event)) { if (epoll_ctl(this->_epoll_fd, EPOLL_CTL_ADD, this->_stderr_read_fd, &event)) {
std::system_error e = make_system_error("epoll_ctl() for stderr"); int errsv = errno;
this->~LogcatThread(); this->~LogcatThread();
throw e; throw_system_error(errsv, "epoll_ctl() for stderr");
} }
this->_thread = std::thread(&LogcatThread::_run, this, run_event_loop); this->_thread = std::thread(&LogcatThread::_run, this, run_event_loop);
@ -74,19 +77,19 @@ LogcatThread::LogcatThread(bool* run_event_loop) {
LogcatThread::~LogcatThread() { LogcatThread::~LogcatThread() {
if (this->_epoll_fd != -1 && close(this->_epoll_fd)) { if (this->_epoll_fd != -1 && close(this->_epoll_fd)) {
log(std::string("Failed to close epoll file descriptor: ") + make_system_error("close()").what()); log(std::string("Failed to close epoll file descriptor: close(): ") + strerror(errno));
} }
if (this->_stdout_read_fd != -1 && close(this->_stdout_read_fd)) { if (this->_stdout_read_fd != -1 && close(this->_stdout_read_fd)) {
log(std::string("Failed to close stdout read pipe: ") + make_system_error("close()").what()); log(std::string("Failed to close stdout read pipe: close(): ") + strerror(errno));
} }
if (this->_stdout_write_fd != -1 && close(this->_stdout_write_fd)) { if (this->_stdout_write_fd != -1 && close(this->_stdout_write_fd)) {
log(std::string("Failed to close stdout write pipe: ") + make_system_error("close()").what()); log(std::string("Failed to close stdout write pipe: close(): ") + strerror(errno));
} }
if (this->_stderr_read_fd != -1 && close(this->_stderr_read_fd)) { if (this->_stderr_read_fd != -1 && close(this->_stderr_read_fd)) {
log(std::string("Failed to close stderr read pipe: ") + make_system_error("close()").what()); log(std::string("Failed to close stderr read pipe: close(): ") + strerror(errno));
} }
if (this->_stderr_write_fd != -1 && close(this->_stderr_write_fd)) { if (this->_stderr_write_fd != -1 && close(this->_stderr_write_fd)) {
log(std::string("Failed to close stderr write pipe: ") + make_system_error("close()").what()); log(std::string("Failed to close stderr write pipe: close(): ") + strerror(errno));
} }
} }
@ -101,7 +104,7 @@ void LogcatThread::_run(bool* run_event_loop) {
printf("(boop)\n"); printf("(boop)\n");
int ready_fds = epoll_wait(this->_epoll_fd, events, EPOLL_MAX_EVENTS, 1000); int ready_fds = epoll_wait(this->_epoll_fd, events, EPOLL_MAX_EVENTS, 1000);
if (ready_fds == -1) { if (ready_fds == -1) {
printf("%s\n", format_log(make_system_error("epoll_wait()").what()).c_str()); printf("%s\n", format_log(std::string("epoll_wait(): ") + strerror(errno)).c_str());
break; break;
} }

View File

@ -10,18 +10,24 @@ std::string quote(const std::string& str) {
return ss.str(); return ss.str();
} }
std::system_error make_system_error(int err, const char* what) { void throw_system_error(int err, const char* what) {
return std::system_error(err, std::generic_category(), what); if (err == ENOMEM) {
throw std::bad_alloc();
}
throw std::system_error(err, std::generic_category(), what);
} }
std::system_error make_system_error(int err, std::string what) { void throw_system_error(int err, std::string what) {
return std::system_error(err, std::generic_category(), std::move(what)); if (err == ENOMEM) {
throw std::bad_alloc();
}
throw std::system_error(err, std::generic_category(), std::move(what));
} }
std::system_error make_system_error(const char* what) { void throw_system_error(const char* what) {
return make_system_error(errno, what); throw_system_error(errno, what);
} }
std::system_error make_system_error(std::string what) { void throw_system_error(std::string what) {
return make_system_error(errno, std::move(what)); throw_system_error(errno, std::move(what));
} }

8
misc.h
View File

@ -1,10 +1,8 @@
#pragma once #pragma once
#include <string> #include <string>
#include <system_error>
std::string quote(const std::string& str); std::string quote(const std::string& str);
std::system_error make_system_error(int err, const char* what); void throw_system_error(int err, const char* what);
std::system_error make_system_error(int err, std::string what); void throw_system_error(const char* what);
std::system_error make_system_error(const char* what); void throw_system_error(std::string what);
std::system_error make_system_error(std::string what);