2022-09-05 22:10:46 -06:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Peter Elliott <pelliott@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/Directory.h>
|
|
|
|
#include <LibCore/SessionManagement.h>
|
|
|
|
#include <LibCore/System.h>
|
|
|
|
|
2023-01-10 17:25:01 +01:00
|
|
|
#ifdef AK_OS_SERENITY
|
|
|
|
# include <LibSystem/syscall.h>
|
|
|
|
#endif
|
|
|
|
|
2022-09-05 22:10:46 -06:00
|
|
|
namespace Core::SessionManagement {
|
|
|
|
|
2023-01-10 17:25:01 +01:00
|
|
|
ErrorOr<pid_t> root_session_id([[maybe_unused]] Optional<pid_t> force_sid)
|
2022-09-05 22:10:46 -06:00
|
|
|
{
|
2023-01-10 17:25:01 +01:00
|
|
|
#ifdef AK_OS_SERENITY
|
|
|
|
int rc = syscall(SC_get_root_session_id, force_sid.value_or(-1));
|
|
|
|
if (rc < 0) {
|
|
|
|
return Error::from_syscall("get_root_session_id"sv, rc);
|
2022-09-05 22:10:46 -06:00
|
|
|
}
|
2023-01-10 17:25:01 +01:00
|
|
|
return static_cast<pid_t>(rc);
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2022-09-05 22:10:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<void> logout(Optional<pid_t> force_sid)
|
|
|
|
{
|
|
|
|
pid_t sid = TRY(root_session_id(force_sid));
|
|
|
|
TRY(System::kill(-sid, SIGTERM));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
ErrorOr<DeprecatedString> parse_path_with_sid(StringView general_path, Optional<pid_t> force_sid)
|
2022-09-05 22:10:46 -06:00
|
|
|
{
|
|
|
|
if (general_path.contains("%sid"sv)) {
|
|
|
|
pid_t sid = TRY(root_session_id(force_sid));
|
2022-12-04 18:02:33 +00:00
|
|
|
return general_path.replace("%sid"sv, DeprecatedString::number(sid), ReplaceMode::All);
|
2022-09-05 22:10:46 -06:00
|
|
|
}
|
2022-12-04 18:02:33 +00:00
|
|
|
return DeprecatedString(general_path);
|
2022-09-05 22:10:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2022-12-04 18:02:33 +00:00
|
|
|
auto const temporary_directory = DeprecatedString::formatted("/tmp/session/{}", sid);
|
2022-09-05 22:10:46 -06:00
|
|
|
auto directory = TRY(Core::Directory::create(temporary_directory, Core::Directory::CreateDirectories::Yes));
|
|
|
|
TRY(directory.chown(uid, gid));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|