From bbe1eca71119a3c816b0964aba27746234de8e55 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 7 Sep 2024 17:40:39 +0200 Subject: [PATCH] utils: Add a hackish "logout" command --- utils/CMakeLists.txt | 1 + utils/logout.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 utils/logout.cpp diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 5d3ee319..d892fe94 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -34,3 +34,4 @@ luna_utility(touch.cpp touch) luna_utility(free.cpp free) luna_utility(sha256sum.cpp sha256sum) luna_utility(arch.cpp arch) +luna_utility(logout.cpp logout) diff --git a/utils/logout.cpp b/utils/logout.cpp new file mode 100644 index 00000000..9e15144e --- /dev/null +++ b/utils/logout.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +// Very hackish. Will do for now. +Result luna_main(int argc, char** argv) +{ + os::ArgumentParser parser; + parser.add_description("Log out of a GUI session."); + parser.add_system_program_info("logout"_sv); + parser.parse(argc, argv); + + pid_t last = pstat(-1, nullptr); + for (pid_t pid = 1; pid <= last; pid++) + { + static process ps; + if (pstat(pid, &ps) < 0) + { + if (errno == ESRCH) continue; + return err(errno); + } + + if (strstr(ps.ps_name, "/usr/bin/launch")) { kill(ps.ps_pid, SIGTERM); } + } + + return 0; +}