forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logout.cpp
49 lines (40 loc) · 1.24 KB
/
logout.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* Copyright (c) 2021, Peter Elliott <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/ProcessStatisticsReader.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#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();
}
ErrorOr<int> serenity_main(Main::Arguments)
{
TRY(Core::System::pledge("stdio proc rpath"));
TRY(Core::System::unveil("/proc/all", "r"));
TRY(Core::System::unveil("/etc/passwd", "r"));
TRY(Core::System::unveil(nullptr, nullptr));
// 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;
}
TRY(Core::System::kill(-sid, SIGTERM));
return 0;
}