anon_create.cpp 1.2 KB

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