2#include "memfd-syscalls.h"
3#include "pedigree/kernel/process/Process.h"
4#include "pedigree/kernel/process/TerminationDeferral.h"
5#include "pedigree/kernel/process/Thread.h"
6#include "pedigree/kernel/processor/Processor.h"
7#include "pedigree/kernel/processor/ProcessorInformation.h"
8#include "pedigree/kernel/syscallError.h"
12#include "FileDescriptor.h"
13#include "PosixSubsystem.h"
14#include "memfd-file.h"
15#include "modules/system/vfs/VFS.h"
17int posix_memfd_create(
const char* name,
unsigned int flags) {
18 if (flags & ~(LinuxMemFd::CloseOnExec | LinuxMemFd::AllowSealing)) {
19 SYSCALL_ERROR(InvalidArgument);
26 if (copied != PosixSubsystem::UserStringSuccess) {
27 syscallError(copied == PosixSubsystem::UserStringTooLong ? Error::InvalidArgument
32 auto* subsystem =
static_cast<PosixSubsystem*
>(process->getSubsystem());
34 SYSCALL_ERROR(InvalidArgument);
38 if (!Process::currentFilesystemCredentials(credentials)) {
39 SYSCALL_ERROR(PermissionDenied);
45 new MemFdFile(filename, flags & LinuxMemFd::AllowSealing, credentials.uid, credentials.gid);
47 SYSCALL_ERROR(OutOfMemory);
53 flags & LinuxMemFd::CloseOnExec ? FD_CLOEXEC : 0, O_RDWR);
54 if (!descriptor || !descriptor->acquireOpenFileDescription()) {
57 SYSCALL_ERROR(OutOfMemory);
61 const size_t fd = subsystem->installFileDescriptor(descriptor, published);
63 return static_cast<int>(fd);
66int posix_memfd_fcntl(
const DescriptorLease& descriptor,
int command, uintptr_t argument) {
69 SYSCALL_ERROR(BadFileDescriptor);
74 SYSCALL_ERROR(BadFileDescriptor);
77 MemFdFile* file = MemFdFile::fromFile(description->getFile());
78 if (!file || (command != F_GET_SEALS && command != F_ADD_SEALS)) {
79 SYSCALL_ERROR(InvalidArgument);
83 if (statusFlags & O_PATH) {
84 SYSCALL_ERROR(BadFileDescriptor);
87 if (command == F_GET_SEALS)
88 return file->getSeals();
89 const int access = statusFlags & O_ACCMODE;
90 if (access != O_WRONLY && access != O_RDWR) {
91 SYSCALL_ERROR(NotEnoughPermissions);
95 return file->addSeals(
static_cast<unsigned int>(argument));
OpenFileDescriptionLease acquireOpenFileDescription() const
int getStatusFlags() const
Get current status flags.
static UserStringResult copyUserString(const char *userString, String ©, size_t maxLength)
static ProcessorInformation & information()
bool untrackFile(File *pFile, bool destroy=true)
void trackFile(File *pFile)
Track a File object that exists. It is necessary to keep track of File objects, or at least those tha...