#include "mologram/Daemon.hpp" #include #include #include #include #include #include #include #include #include #include "mologram/SocketUtil.hpp" namespace mologram { namespace fs = std::filesystem; Daemon::Daemon(ITelegramService& service, std::string socketPath) : service_(service), path_(std::move(socketPath)), bus_(stats_) {} Daemon::~Daemon() { stop(); conns_.clear(); service_.removeSink(&bus_); if (listener_.valid()) { std::error_code ec; fs::remove(path_, ec); fs::remove(path_ + ".lock", ec); } // removed while we still hold the flock } std::string Daemon::start() { std::error_code ec; fs::path dir = fs::path(path_).parent_path(); if (!dir.empty() && fs::create_directories(dir, ec)) ::chmod(dir.c_str(), 0700); // only a directory we created; never a shared one like /tmp lock_.reset(::open((path_ + ".lock").c_str(), O_CREAT | O_RDWR | O_CLOEXEC, 0600)); if (!lock_.valid()) return std::string("cannot open lockfile: ") + std::strerror(errno); if (::flock(lock_.get(), LOCK_EX | LOCK_NB) != 0) return "another daemon holds the lock for " + path_; if (fs::exists(path_, ec)) { int e = 0; Fd probe = SocketUtil::connectUnix(path_, e); if (probe.valid()) return "socket " + path_ + " is served by a live process"; fs::remove(path_, ec); stats_.staleSocketsRemoved++; } std::string err; listener_ = SocketUtil::listenUnix(path_, err); if (!listener_.valid()) return err; SocketUtil::setNonBlocking(listener_.get()); int p[2]; if (::pipe2(p, O_CLOEXEC | O_NONBLOCK) != 0) return std::string("pipe: ") + std::strerror(errno); wakeR_.reset(p[0]); wakeW_.reset(p[1]); service_.addSink(&bus_); return ""; } void Daemon::wake() { char c = 1; if (wakeW_.valid()) { ssize_t r = ::write(wakeW_.get(), &c, 1); (void)r; } } void Daemon::stop() { stop_ = true; wake(); } void Daemon::run() { while (!stop_.load()) { std::vector fds; fds.push_back({wakeR_.get(), POLLIN, 0}); fds.push_back({listener_.get(), POLLIN, 0}); for (auto& c : conns_) fds.push_back({c->fd(), (short)(POLLIN | (c->wantsWrite() ? POLLOUT : 0)), 0}); int rc = ::poll(fds.data(), fds.size(), -1); if (rc < 0) { if (errno == EINTR) continue; break; } const size_t polled = conns_.size(); if (fds[0].revents & POLLIN) { char buf[256]; while (::read(wakeR_.get(), buf, sizeof(buf)) > 0) {} } if (fds[1].revents & POLLIN) { for (;;) { int fd = ::accept4(listener_.get(), nullptr, nullptr, SOCK_CLOEXEC); if (fd < 0) break; auto c = std::make_shared(Fd(fd), service_, stats_, [this]() { wake(); }); conns_.push_back(c); bus_.add(c); } } std::vector> dead; for (size_t i = 0; i < polled; i++) { short re = fds[i + 2].revents; bool keep = true; if (re & POLLIN) keep = conns_[i]->onReadable(); if (keep && (re & POLLOUT)) keep = conns_[i]->onWritable(); if (keep && (re & (POLLERR | POLLNVAL))) keep = false; if (keep && (re & POLLHUP) && !(re & POLLIN)) keep = false; if (keep && conns_[i]->wantsWrite()) keep = conns_[i]->onWritable(); if (!keep) dead.push_back(conns_[i]); } for (auto& d : dead) { d->cancelAll(); bus_.remove(d.get()); conns_.erase(std::remove(conns_.begin(), conns_.end(), d), conns_.end()); } for (auto& c : conns_) if (c->hasRequested()) everAttached_ = true; // a connect-and-leave probe is not an attached UI if (everAttached_ && conns_.empty()) becameEmpty_ = true; else if (!conns_.empty()) becameEmpty_ = false; if (stopWhenEmpty_.load() && becameEmpty_.load()) break; } } } // namespace mologram