Add --list-processes to list running -r/--run sessions
Lists every session pid file found under
$XDG_STATE_HOME/slocker-lite/run/, showing pid, container name, and
status (running or exited). Status is determined the same way any
external tool could check it: a non-blocking exclusive flock() on the
file that succeeds means it's actually stale (nothing holds it), so
the session is reported as exited in that case; the lock is always
released again immediately, never left held by the check itself.
list_sessions() (pid_file.{h,cpp}) reads the real pid from each file's
own contents rather than parsing it out of the filename, which would
be ambiguous for container names that themselves contain '-'.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gv3s5jckJKzh6JkMoi2Akz
This commit is contained in:
@@ -103,3 +103,53 @@ void release_session_lock(const SessionLock& lock) {
|
||||
spdlog::warn("failed to remove session pid file {}: {}", lock.path.string(), ec.message());
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<SessionInfo> list_sessions() {
|
||||
std::vector<SessionInfo> sessions;
|
||||
|
||||
auto dir = session_run_dir();
|
||||
std::error_code dir_ec;
|
||||
if (!std::filesystem::is_directory(dir, dir_ec)) {
|
||||
return sessions;
|
||||
}
|
||||
|
||||
std::error_code it_ec;
|
||||
for (auto it = std::filesystem::directory_iterator(dir, it_ec);
|
||||
!it_ec && it != std::filesystem::directory_iterator(); it.increment(it_ec)) {
|
||||
const auto& path = it->path();
|
||||
|
||||
int fd = open(path.c_str(), O_RDWR);
|
||||
if (fd < 0) {
|
||||
spdlog::debug("failed to open session file {}: {}", path.string(), strerror(errno));
|
||||
continue;
|
||||
}
|
||||
|
||||
char buf[32] = {};
|
||||
ssize_t n = read(fd, buf, sizeof(buf) - 1);
|
||||
pid_t pid = n > 0 ? static_cast<pid_t>(std::atoi(buf)) : 0;
|
||||
|
||||
bool running = true;
|
||||
if (flock(fd, LOCK_EX | LOCK_NB) == 0) {
|
||||
running = false;
|
||||
flock(fd, LOCK_UN);
|
||||
}
|
||||
close(fd);
|
||||
|
||||
if (pid <= 0) {
|
||||
spdlog::debug("skipping malformed session pid file {}", path.string());
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string filename = path.filename().string();
|
||||
std::string suffix = fmt::format("-{}", pid);
|
||||
std::string container_name =
|
||||
filename.size() > suffix.size() &&
|
||||
filename.compare(filename.size() - suffix.size(), suffix.size(), suffix) == 0
|
||||
? filename.substr(0, filename.size() - suffix.size())
|
||||
: filename;
|
||||
|
||||
sessions.push_back(SessionInfo{pid, container_name, running});
|
||||
}
|
||||
|
||||
return sessions;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user