2021-10-10 22:04:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2021-11-23 15:07:18 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-10-10 22:04:14 +00:00
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
static Core::ProcessStatistics const& get_proc(Core::AllProcessesStatistics const& stats, pid_t pid)
|
|
|
|
{
|
|
|
|
for (auto& proc : stats.processes) {
|
|
|
|
if (proc.pid == pid)
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2021-11-23 15:07:18 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments)
|
2021-10-10 22:04:14 +00:00
|
|
|
{
|
2021-11-23 15:07:18 +00:00
|
|
|
TRY(Core::System::pledge("stdio proc rpath", nullptr));
|
|
|
|
TRY(Core::System::unveil("/proc/all", "r"));
|
|
|
|
TRY(Core::System::unveil("/etc/passwd", "r"));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-10-10 22:04:14 +00:00
|
|
|
|
|
|
|
// logout finds the highest session up all nested sessions, and kills it.
|
|
|
|
auto stats = Core::ProcessStatisticsReader::get_all();
|
|
|
|
if (!stats.has_value()) {
|
|
|
|
warnln("couldn't get process statistics");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t sid = getsid(0);
|
|
|
|
while (true) {
|
|
|
|
pid_t parent = get_proc(stats.value(), sid).ppid;
|
|
|
|
pid_t parent_sid = get_proc(stats.value(), parent).sid;
|
|
|
|
|
|
|
|
if (parent_sid == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
sid = parent_sid;
|
|
|
|
}
|
|
|
|
|
2021-11-23 15:07:18 +00:00
|
|
|
TRY(Core::System::kill(-sid, SIGTERM));
|
2021-10-10 22:04:14 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|