mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibCore: Create Core::SessionManagement for session management
This commit is contained in:
parent
76c67b7ae9
commit
2c3caa7e17
Notes:
sideshowbarker
2024-07-17 06:24:28 +09:00
Author: https://github.com/petelliott Commit: https://github.com/SerenityOS/serenity/commit/2c3caa7e17 Pull-request: https://github.com/SerenityOS/serenity/pull/15142 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/bgianfo
3 changed files with 89 additions and 0 deletions
|
@ -26,6 +26,7 @@ set(SOURCES
|
|||
ProcessStatisticsReader.cpp
|
||||
Property.cpp
|
||||
SecretString.cpp
|
||||
SessionManagement.cpp
|
||||
SOCKSProxyClient.cpp
|
||||
Stream.cpp
|
||||
StandardPaths.cpp
|
||||
|
|
68
Userland/Libraries/LibCore/SessionManagement.cpp
Normal file
68
Userland/Libraries/LibCore/SessionManagement.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Peter Elliott <pelliott@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibCore/Directory.h>
|
||||
#include <LibCore/ProcessStatisticsReader.h>
|
||||
#include <LibCore/SessionManagement.h>
|
||||
#include <LibCore/System.h>
|
||||
|
||||
namespace Core::SessionManagement {
|
||||
|
||||
static ErrorOr<Core::ProcessStatistics const*> get_proc(Core::AllProcessesStatistics const& stats, pid_t pid)
|
||||
{
|
||||
for (auto& proc : stats.processes) {
|
||||
if (proc.pid == pid)
|
||||
return &proc;
|
||||
}
|
||||
return Error::from_string_literal("Could not find pid in process statistics.");
|
||||
}
|
||||
|
||||
ErrorOr<pid_t> root_session_id(Optional<pid_t> force_sid)
|
||||
{
|
||||
auto stats = Core::ProcessStatisticsReader::get_all(false);
|
||||
if (!stats.has_value())
|
||||
return Error::from_string_literal("Failed to get all process statistics");
|
||||
|
||||
pid_t sid = (force_sid.has_value()) ? force_sid.value() : TRY(System::getsid());
|
||||
while (true) {
|
||||
pid_t parent = TRY(get_proc(stats.value(), sid))->ppid;
|
||||
pid_t parent_sid = TRY(get_proc(stats.value(), parent))->sid;
|
||||
|
||||
if (parent_sid == 0)
|
||||
break;
|
||||
|
||||
sid = parent_sid;
|
||||
}
|
||||
|
||||
return sid;
|
||||
}
|
||||
|
||||
ErrorOr<void> logout(Optional<pid_t> force_sid)
|
||||
{
|
||||
pid_t sid = TRY(root_session_id(force_sid));
|
||||
TRY(System::kill(-sid, SIGTERM));
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<String> parse_path_with_sid(StringView general_path, Optional<pid_t> force_sid)
|
||||
{
|
||||
if (general_path.contains("%sid"sv)) {
|
||||
pid_t sid = TRY(root_session_id(force_sid));
|
||||
return general_path.replace("%sid"sv, String::number(sid), ReplaceMode::All);
|
||||
}
|
||||
return String(general_path);
|
||||
}
|
||||
|
||||
ErrorOr<void> create_session_temporary_directory_if_needed(uid_t uid, gid_t gid, Optional<pid_t> force_sid)
|
||||
{
|
||||
pid_t sid = TRY(root_session_id(force_sid));
|
||||
auto const temporary_directory = String::formatted("/tmp/session/{}", sid);
|
||||
auto directory = TRY(Core::Directory::create(temporary_directory, Core::Directory::CreateDirectories::Yes));
|
||||
TRY(directory.chown(uid, gid));
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
20
Userland/Libraries/LibCore/SessionManagement.h
Normal file
20
Userland/Libraries/LibCore/SessionManagement.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Peter Elliott <pelliott@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Error.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
namespace Core::SessionManagement {
|
||||
|
||||
ErrorOr<pid_t> root_session_id(Optional<pid_t> force_sid = {});
|
||||
ErrorOr<void> logout(Optional<pid_t> force_sid = {});
|
||||
|
||||
ErrorOr<String> parse_path_with_sid(StringView general_path, Optional<pid_t> force_sid = {});
|
||||
ErrorOr<void> create_session_temporary_directory_if_needed(uid_t uid, gid_t gid, Optional<pid_t> force_sid = {});
|
||||
|
||||
}
|
Loading…
Reference in a new issue