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>
|
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/VM/AnonymousVMObject.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<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-01-15 18:42:09 +03:00
|
|
|
REQUIRE_PROMISE(stdio);
|
|
|
|
|
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;
|
|
|
|
|
2021-07-27 12:12:51 +03:00
|
|
|
auto new_fd_or_error = m_fds.allocate();
|
|
|
|
if (new_fd_or_error.is_error())
|
|
|
|
return new_fd_or_error.error();
|
|
|
|
auto new_fd = new_fd_or_error.value();
|
2021-07-25 02:46:44 +03:00
|
|
|
auto vmobject = AnonymousVMObject::try_create_purgeable_with_size(size, AllocationStrategy::Reserve);
|
2021-01-15 13:28:07 +03:00
|
|
|
if (!vmobject)
|
2021-03-01 15:49:16 +03:00
|
|
|
return ENOMEM;
|
2021-01-15 13:28:07 +03:00
|
|
|
|
|
|
|
auto anon_file = AnonymousFile::create(vmobject.release_nonnull());
|
2021-05-28 15:39:40 +03:00
|
|
|
if (!anon_file)
|
|
|
|
return ENOMEM;
|
2021-01-15 13:28:07 +03:00
|
|
|
auto description_or_error = FileDescription::create(*anon_file);
|
|
|
|
if (description_or_error.is_error())
|
|
|
|
return description_or_error.error();
|
|
|
|
|
|
|
|
auto description = description_or_error.release_value();
|
|
|
|
description->set_writable(true);
|
|
|
|
description->set_readable(true);
|
|
|
|
|
|
|
|
u32 fd_flags = 0;
|
|
|
|
if (options & O_CLOEXEC)
|
|
|
|
fd_flags |= FD_CLOEXEC;
|
|
|
|
|
|
|
|
m_fds[new_fd].set(move(description), fd_flags);
|
|
|
|
return new_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|