Serenity Operating System
at master 46 lines 1.2 kB view raw
1/* 2 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <Kernel/FileSystem/AnonymousFile.h> 8#include <Kernel/FileSystem/OpenFileDescription.h> 9#include <Kernel/Memory/AnonymousVMObject.h> 10#include <Kernel/Process.h> 11 12namespace Kernel { 13 14ErrorOr<FlatPtr> Process::sys$anon_create(size_t size, int options) 15{ 16 VERIFY_NO_PROCESS_BIG_LOCK(this); 17 TRY(require_promise(Pledge::stdio)); 18 19 if (!size) 20 return EINVAL; 21 22 if (size % PAGE_SIZE) 23 return EINVAL; 24 25 if (size > NumericLimits<ssize_t>::max()) 26 return EINVAL; 27 28 auto vmobject = TRY(Memory::AnonymousVMObject::try_create_purgeable_with_size(size, AllocationStrategy::AllocateNow)); 29 auto anon_file = TRY(AnonymousFile::try_create(move(vmobject))); 30 auto description = TRY(OpenFileDescription::try_create(move(anon_file))); 31 32 description->set_writable(true); 33 description->set_readable(true); 34 35 u32 fd_flags = 0; 36 if (options & O_CLOEXEC) 37 fd_flags |= FD_CLOEXEC; 38 39 return m_fds.with_exclusive([&](auto& fds) -> ErrorOr<FlatPtr> { 40 auto new_fd = TRY(fds.allocate()); 41 fds[new_fd.fd].set(description, fd_flags); 42 return new_fd.fd; 43 }); 44} 45 46}