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