logmeow/logcat_thread.cpp

116 lines
3.5 KiB
C++
Raw Normal View History

2023-01-17 15:35:08 +00:00
#include <cstring>
2023-01-17 14:44:17 +00:00
#include <fcntl.h>
2023-01-16 09:10:53 +00:00
#include <unistd.h>
#include <sys/epoll.h>
2023-01-17 14:44:17 +00:00
#include <system_error>
2023-01-16 09:10:53 +00:00
#include "log.h"
#include "misc.h"
#include "logcat_thread.h"
#define EPOLL_MAX_EVENTS 10
2023-01-17 14:44:17 +00:00
static void mark_nonblock(int fd) {
int flags = fcntl(fd, F_GETFL);
if (flags < 0) {
2023-01-17 15:35:08 +00:00
throw_system_error("fcntl(fd, F_GETFL)");
2023-01-17 14:44:17 +00:00
}
if (!(flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
2023-01-17 15:35:08 +00:00
throw_system_error("fcntl(fd, F_SETFL)");
2023-01-17 14:44:17 +00:00
}
}
LogcatThread::LogcatThread(bool* run_event_loop) {
2023-01-17 14:44:17 +00:00
int fds[2];
struct epoll_event event = {.events = EPOLLIN | EPOLLET};
if (pipe(fds)) {
2023-01-17 15:35:08 +00:00
int errsv = errno;
2023-01-17 14:44:17 +00:00
this->~LogcatThread();
2023-01-17 15:35:08 +00:00
throw_system_error(errsv, "pipe() for stdout");
2023-01-17 14:44:17 +00:00
}
this->_stdout_read_fd = fds[0];
this->_stdout_write_fd = fds[1];
try {
mark_nonblock(this->_stdout_read_fd);
} catch (const std::exception& e) {
this->~LogcatThread();
throw;
}
if (pipe(fds)) {
2023-01-17 15:35:08 +00:00
int errsv = errno;
2023-01-17 14:44:17 +00:00
this->~LogcatThread();
2023-01-17 15:35:08 +00:00
throw_system_error(errsv, "pipe() for stderr");
2023-01-17 14:44:17 +00:00
}
this->_stderr_read_fd = fds[0];
this->_stderr_write_fd = fds[1];
try {
mark_nonblock(this->_stderr_read_fd);
} catch (const std::exception& e) {
this->~LogcatThread();
throw;
}
2023-01-17 13:56:56 +00:00
this->_epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if (this->_epoll_fd == -1) {
2023-01-17 15:35:08 +00:00
int errsv = errno;
this->~LogcatThread();
throw_system_error(errsv, "epoll_create1()");
2023-01-16 09:10:53 +00:00
}
2023-01-17 14:44:17 +00:00
event.data.fd = this->_stdout_read_fd;
if (epoll_ctl(this->_epoll_fd, EPOLL_CTL_ADD, this->_stdout_read_fd, &event)) {
2023-01-17 15:35:08 +00:00
int errsv = errno;
2023-01-17 14:44:17 +00:00
this->~LogcatThread();
2023-01-17 15:35:08 +00:00
throw_system_error(errsv, "epoll_ctl() for stdout");
2023-01-17 14:44:17 +00:00
}
event.data.fd = this->_stderr_read_fd;
if (epoll_ctl(this->_epoll_fd, EPOLL_CTL_ADD, this->_stderr_read_fd, &event)) {
2023-01-17 15:35:08 +00:00
int errsv = errno;
2023-01-17 14:44:17 +00:00
this->~LogcatThread();
2023-01-17 15:35:08 +00:00
throw_system_error(errsv, "epoll_ctl() for stderr");
2023-01-17 14:44:17 +00:00
}
this->_thread = std::thread(&LogcatThread::_run, this, run_event_loop);
2023-01-16 09:10:53 +00:00
}
2023-01-17 13:56:56 +00:00
LogcatThread::~LogcatThread() {
if (this->_epoll_fd != -1 && close(this->_epoll_fd)) {
2023-01-17 15:35:08 +00:00
log(std::string("Failed to close epoll file descriptor: close(): ") + strerror(errno));
2023-01-16 09:10:53 +00:00
}
2023-01-17 14:44:17 +00:00
if (this->_stdout_read_fd != -1 && close(this->_stdout_read_fd)) {
2023-01-17 15:35:08 +00:00
log(std::string("Failed to close stdout read pipe: close(): ") + strerror(errno));
2023-01-17 14:44:17 +00:00
}
if (this->_stdout_write_fd != -1 && close(this->_stdout_write_fd)) {
2023-01-17 15:35:08 +00:00
log(std::string("Failed to close stdout write pipe: close(): ") + strerror(errno));
2023-01-17 14:44:17 +00:00
}
if (this->_stderr_read_fd != -1 && close(this->_stderr_read_fd)) {
2023-01-17 15:35:08 +00:00
log(std::string("Failed to close stderr read pipe: close(): ") + strerror(errno));
2023-01-17 14:44:17 +00:00
}
if (this->_stderr_write_fd != -1 && close(this->_stderr_write_fd)) {
2023-01-17 15:35:08 +00:00
log(std::string("Failed to close stderr write pipe: close(): ") + strerror(errno));
2023-01-17 14:44:17 +00:00
}
2023-01-16 09:10:53 +00:00
}
2023-01-17 13:56:56 +00:00
void LogcatThread::join() {
this->_thread.join();
}
void LogcatThread::_run(bool* run_event_loop) {
2023-01-16 09:10:53 +00:00
struct epoll_event events[EPOLL_MAX_EVENTS];
while (*run_event_loop) {
2023-01-16 09:10:53 +00:00
printf("(boop)\n");
2023-01-17 13:56:56 +00:00
int ready_fds = epoll_wait(this->_epoll_fd, events, EPOLL_MAX_EVENTS, 1000);
2023-01-16 09:10:53 +00:00
if (ready_fds == -1) {
2023-01-17 15:35:08 +00:00
printf("%s\n", format_log(std::string("epoll_wait(): ") + strerror(errno)).c_str());
2023-01-16 09:10:53 +00:00
break;
}
for (int i=0; i < ready_fds; i++) {
printf("ain't no way in hell do we get a ready fd (#%d) when we haven't set up anything\n", i);
}
}
}