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