mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-28 07:12:07 +00:00 
			
		
		
		
	 5dcc58d54a
			
		
	
	
		5dcc58d54a
		
	
	
	
	
		
			
			Before this patch, Core::SessionManagement::parse_path_with_sid() would figure out the root session ID by sifting through /sys/kernel/processes. That file can take quite a while to generate (sometimes up to 40ms on my machine, which is a problem on its own!) and with no caching, many of our programs were effectively doing this multiple times on startup when unveiling something in /tmp/session/%sid/ While we should find ways to make generating /sys/kernel/processes fast again, this patch addresses the specific problem by introducing a new syscall: sys$get_root_session_id(). This extracts the root session ID by looking directly at the process table and takes <1ms instead of 40ms. This cuts WebContent process startup time by ~100ms on my machine. :^)
		
			
				
	
	
		
			55 lines
		
	
	
	
		
			1.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			55 lines
		
	
	
	
		
			1.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * 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>
 | |
| 
 | |
| #ifdef AK_OS_SERENITY
 | |
| #    include <LibSystem/syscall.h>
 | |
| #endif
 | |
| 
 | |
| namespace Core::SessionManagement {
 | |
| 
 | |
| ErrorOr<pid_t> root_session_id([[maybe_unused]] Optional<pid_t> force_sid)
 | |
| {
 | |
| #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);
 | |
|     }
 | |
|     return static_cast<pid_t>(rc);
 | |
| #else
 | |
|     return 0;
 | |
| #endif
 | |
| }
 | |
| 
 | |
| ErrorOr<void> logout(Optional<pid_t> force_sid)
 | |
| {
 | |
|     pid_t sid = TRY(root_session_id(force_sid));
 | |
|     TRY(System::kill(-sid, SIGTERM));
 | |
|     return {};
 | |
| }
 | |
| 
 | |
| ErrorOr<DeprecatedString> 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, DeprecatedString::number(sid), ReplaceMode::All);
 | |
|     }
 | |
|     return DeprecatedString(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 = DeprecatedString::formatted("/tmp/session/{}", sid);
 | |
|     auto directory = TRY(Core::Directory::create(temporary_directory, Core::Directory::CreateDirectories::Yes));
 | |
|     TRY(directory.chown(uid, gid));
 | |
|     return {};
 | |
| }
 | |
| 
 | |
| }
 |