/* * Copyright (c) 2026, Liav A. * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Kernel { ErrorOr> UnsharedResourceFile::create(UnshareType type) { return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) UnsharedResourceFile(type))); } UnsharedResourceFile::UnsharedResourceFile(UnshareType type) : m_type(type) { } ErrorOr UnsharedResourceFile::initialize_resource() { switch (m_type) { case UnshareType::ScopedProcessList: { auto scoped_process_list = TRY(ScopedProcessList::create()); return scoped_process_list->id().value(); } case UnshareType::VFSRootContext: { auto vfs_root_context = TRY(VFSRootContext::create_with_empty_ramfs(VFSRootContext::AddToGlobalContextList::Yes)); return vfs_root_context->id().value(); } case UnshareType::HostnameContext: { // NOTE: Technically, we create a new context, based on the // contents of "previous" attached one. // However, the process can change the hostname context during // this call as we don't really hold a lock on it, but that's // really a non-issue because it's not destructive or really harmful // and if a process is so **eager** to blindly change hostname contexts // with multiple fds, we should not be the ones to stop it from doing that. auto current_context = Process::current().hostname_context(); FixedStringBuffer hostname; current_context->buffer().with([&hostname](auto& buffer) { hostname.store_characters(buffer.representable_view()); }); auto hostname_context = TRY(HostnameContext::create_with_name(hostname.representable_view())); return hostname_context->id().value(); } } return ENOTSUP; } UnsharedResourceFile::~UnsharedResourceFile() = default; ErrorOr UnsharedResourceFile::ioctl(OpenFileDescription&, unsigned, Userspace) { return ENOTSUP; } ErrorOr> UnsharedResourceFile::pseudo_path(OpenFileDescription const&) const { return KString::try_create(":unshared-resource-file:"sv); } }