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-06-28 15:59:35 -03:00
|
|
|
KResultOr<FlatPtr> Process::sys$anon_create(size_t size, int options)
|
2021-01-15 07:28:07 -03:00
|
|
|
{
|
2021-07-18 15:20:12 -03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-01-15 12:42:09 -03:00
|
|
|
REQUIRE_PROMISE(stdio);
|
|
|
|
|
|
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;
|
|
|
|
|
|
2021-09-05 09:36:40 -03:00
|
|
|
auto new_fd = TRY(m_fds.allocate());
|
|
|
|
|
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 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;
|
|
|
|
|
|
2021-07-28 03:59:24 -03:00
|
|
|
m_fds[new_fd.fd].set(move(description), fd_flags);
|
|
|
|
|
return new_fd.fd;
|
2021-01-15 07:28:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|