2021-01-15 13:28:07 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-01-15 13:28:07 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Kernel/FileSystem/AnonymousFile.h>
|
2021-09-07 14:39:11 +03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/AnonymousVMObject.h>
|
2021-01-15 13:28:07 +03:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$anon_create(size_t size, int options)
|
2021-01-15 13:28:07 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2021-01-15 18:42:09 +03:00
|
|
|
|
2021-01-25 11:35:25 +03:00
|
|
|
if (!size)
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINVAL;
|
2021-01-25 11:35:25 +03:00
|
|
|
|
2021-01-15 13:28:07 +03:00
|
|
|
if (size % PAGE_SIZE)
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINVAL;
|
2021-01-15 13:28:07 +03:00
|
|
|
|
2021-06-16 17:44:15 +03:00
|
|
|
if (size > NumericLimits<ssize_t>::max())
|
|
|
|
return EINVAL;
|
|
|
|
|
2022-01-29 03:22:28 +03:00
|
|
|
auto new_fd = TRY(allocate_fd());
|
2021-09-05 15:36:40 +03:00
|
|
|
auto vmobject = TRY(Memory::AnonymousVMObject::try_create_purgeable_with_size(size, AllocationStrategy::Reserve));
|
|
|
|
auto anon_file = TRY(AnonymousFile::try_create(move(vmobject)));
|
2021-09-07 14:39:11 +03:00
|
|
|
auto description = TRY(OpenFileDescription::try_create(move(anon_file)));
|
2021-09-05 15:36:40 +03:00
|
|
|
|
2021-01-15 13:28:07 +03:00
|
|
|
description->set_writable(true);
|
|
|
|
description->set_readable(true);
|
|
|
|
|
|
|
|
u32 fd_flags = 0;
|
|
|
|
if (options & O_CLOEXEC)
|
|
|
|
fd_flags |= FD_CLOEXEC;
|
|
|
|
|
2022-01-29 03:29:07 +03:00
|
|
|
m_fds.with_exclusive([&](auto& fds) { fds[new_fd.fd].set(move(description), fd_flags); });
|
2021-07-28 09:59:24 +03:00
|
|
|
return new_fd.fd;
|
2021-01-15 13:28:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|