diff --git a/config.cpp b/config.cpp index 5c124c1..fb9446d 100644 --- a/config.cpp +++ b/config.cpp @@ -13,17 +13,17 @@ static FILE* fopen_or_raise(const char* path, const char* mode, bool ignore_enoent) { FILE* file = fopen(path, mode); - if (file || (ignore_enoent && errno == ENOENT)) { - return file; + if (!file && !(ignore_enoent && errno == ENOENT)) { + 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) { if (!fclose(file)) { 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) { @@ -107,7 +107,7 @@ void create_config_folders_if_necessary() { if (errno == EEXIST) { 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) { errno = 0; if (getline(&line, &line_capacity_size, file) < 0) { + // https://stackoverflow.com/questions/30569981/does-free-set-errno int errsv = errno; free(line); - if (errsv == ENOMEM) { - throw std::bad_alloc(); - } else if (errsv != 0) { - throw make_system_error(errsv, "getline()"); - } else { - break; + if (errsv) { + throw_system_error(errsv, "getline()"); } + break; } if (line_capacity_size == 0 || line[0] == '\0' || line[0] == '#') { continue; @@ -221,5 +219,5 @@ void write_config(const Config& config) { if (!rename(tmp_config_file_path.c_str(), config_file_path.c_str())) { 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) + ')'); } diff --git a/logcat_thread.cpp b/logcat_thread.cpp index 531c94c..f8e906b 100644 --- a/logcat_thread.cpp +++ b/logcat_thread.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -12,10 +13,10 @@ static void mark_nonblock(int fd) { int flags = fcntl(fd, F_GETFL); 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) { - 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}; if (pipe(fds)) { - std::system_error e = make_system_error("pipe() for stdout"); + int errsv = errno; this->~LogcatThread(); - throw e; + throw_system_error(errsv, "pipe() for stdout"); } this->_stdout_read_fd = fds[0]; this->_stdout_write_fd = fds[1]; @@ -39,9 +40,9 @@ LogcatThread::LogcatThread(bool* run_event_loop) { } if (pipe(fds)) { - std::system_error e = make_system_error("pipe() for stderr"); + int errsv = errno; this->~LogcatThread(); - throw e; + throw_system_error(errsv, "pipe() for stderr"); } this->_stderr_read_fd = fds[0]; this->_stderr_write_fd = fds[1]; @@ -54,19 +55,21 @@ LogcatThread::LogcatThread(bool* run_event_loop) { this->_epoll_fd = epoll_create1(EPOLL_CLOEXEC); 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; 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(); - throw e; + throw_system_error(errsv, "epoll_ctl() for stdout"); } event.data.fd = this->_stderr_read_fd; 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(); - throw e; + throw_system_error(errsv, "epoll_ctl() for stderr"); } this->_thread = std::thread(&LogcatThread::_run, this, run_event_loop); @@ -74,19 +77,19 @@ LogcatThread::LogcatThread(bool* run_event_loop) { LogcatThread::~LogcatThread() { 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)) { - 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)) { - 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)) { - 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)) { - 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"); int ready_fds = epoll_wait(this->_epoll_fd, events, EPOLL_MAX_EVENTS, 1000); 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; } diff --git a/misc.cpp b/misc.cpp index ed4a702..6da5a7a 100644 --- a/misc.cpp +++ b/misc.cpp @@ -10,18 +10,24 @@ std::string quote(const std::string& str) { return ss.str(); } -std::system_error make_system_error(int err, const char* what) { - return std::system_error(err, std::generic_category(), what); +void throw_system_error(int err, const char* 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) { - return std::system_error(err, std::generic_category(), std::move(what)); +void throw_system_error(int err, std::string 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) { - return make_system_error(errno, what); +void throw_system_error(const char* what) { + throw_system_error(errno, what); } -std::system_error make_system_error(std::string what) { - return make_system_error(errno, std::move(what)); +void throw_system_error(std::string what) { + throw_system_error(errno, std::move(what)); } diff --git a/misc.h b/misc.h index b94f0e3..e97db12 100644 --- a/misc.h +++ b/misc.h @@ -1,10 +1,8 @@ #pragma once #include -#include std::string quote(const std::string& str); -std::system_error make_system_error(int err, const char* what); -std::system_error make_system_error(int err, std::string what); -std::system_error make_system_error(const char* what); -std::system_error make_system_error(std::string what); +void throw_system_error(int err, const char* what); +void throw_system_error(const char* what); +void throw_system_error(std::string what);