Skip to content

Commit

Permalink
Kernel: More work towards POSIX SHM, also add ftruncate().
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Apr 8, 2019
1 parent 99f3cc2 commit 26a06f3
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Kernel/FileSystem/FileDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <Kernel/Process.h>
#include <Kernel/Devices/BlockDevice.h>
#include <Kernel/VM/MemoryManager.h>
#include <Kernel/SharedMemory.h>

Retained<FileDescriptor> FileDescriptor::create(RetainPtr<Inode>&& inode)
{
Expand Down Expand Up @@ -357,6 +358,11 @@ const char* to_string(SocketRole role)
}
}

bool FileDescriptor::is_file() const
{
return !is_tty() && !is_fifo() && !is_device() && !is_socket() && !is_shared_memory();
}

KResultOr<String> FileDescriptor::absolute_path()
{
Stopwatch sw("absolute_path");
Expand Down Expand Up @@ -439,3 +445,12 @@ const CharacterDevice* FileDescriptor::character_device() const
{
return is_character_device() ? static_cast<const CharacterDevice*>(device()) : nullptr;
}

KResult FileDescriptor::truncate(off_t length)
{
if (is_file()) {
return m_inode->truncate(length);
}
ASSERT(is_shared_memory());
return shared_memory()->truncate(length);
}
10 changes: 10 additions & 0 deletions Kernel/FileSystem/FileDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MasterPTY;
class Process;
class Region;
class CharacterDevice;
class SharedMemory;

class FileDescriptor : public Retainable<FileDescriptor> {
public:
Expand Down Expand Up @@ -85,13 +86,20 @@ class FileDescriptor : public Retainable<FileDescriptor> {
bool is_fifo() const { return m_fifo; }
FIFO::Direction fifo_direction() { return m_fifo_direction; }

bool is_file() const;
bool is_shared_memory() const { return m_shared_memory; }
SharedMemory* shared_memory() { return m_shared_memory.ptr(); }
const SharedMemory* shared_memory() const { return m_shared_memory.ptr(); }

ByteBuffer& generator_cache() { return m_generator_cache; }

void set_original_inode(Badge<VFS>, Retained<Inode>&& inode) { m_inode = move(inode); }

SocketRole socket_role() const { return m_socket_role; }
void set_socket_role(SocketRole);

KResult truncate(off_t);

private:
friend class VFS;
FileDescriptor(RetainPtr<Socket>&&, SocketRole);
Expand All @@ -115,6 +123,8 @@ class FileDescriptor : public Retainable<FileDescriptor> {
RetainPtr<FIFO> m_fifo;
FIFO::Direction m_fifo_direction { FIFO::Neither };

RetainPtr<SharedMemory> m_shared_memory;

bool m_closed { false };
};

1 change: 1 addition & 0 deletions Kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ KERNEL_OBJS = \
ELF/ELFImage.o \
ELF/ELFLoader.o \
KSyms.o \
SharedMemory.o \
FileSystem/DevPtsFS.o \
Devices/BXVGADevice.o \
PCI.o \
Expand Down
11 changes: 11 additions & 0 deletions Kernel/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2468,3 +2468,14 @@ int Process::sys$shm_unlink(const char* name)
return -EFAULT;
return -ENOTIMPL;
}

int Process::sys$ftruncate(int fd, off_t length)
{
auto* descriptor = file_descriptor(fd);
if (!descriptor)
return -EBADF;
// FIXME: Check that fd is writable, otherwise EINVAL.
if (!descriptor->is_file() && !descriptor->is_shared_memory())
return -EINVAL;
return descriptor->truncate(length);
}
1 change: 1 addition & 0 deletions Kernel/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class Process : public InlineLinkedListNode<Process>, public Weakable<Process> {
int sys$donate(int tid);
int sys$shm_open(const char* name, int flags, mode_t);
int sys$shm_unlink(const char* name);
int sys$ftruncate(int fd, off_t);
pid_t sys$setsid();
pid_t sys$getsid(pid_t);
int sys$setpgid(pid_t pid, pid_t pgid);
Expand Down
27 changes: 27 additions & 0 deletions Kernel/SharedMemory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <Kernel/SharedMemory.h>
#include <Kernel/VM/VMObject.h>

SharedMemory::SharedMemory()
{
}

SharedMemory::~SharedMemory()
{
}

KResult SharedMemory::truncate(int length)
{
if (!length) {
m_vmo = nullptr;
return KSuccess;
}

if (!m_vmo) {
m_vmo = VMObject::create_anonymous(length);
return KSuccess;
}

// FIXME: Support truncation.
ASSERT_NOT_REACHED();
return KResult(-ENOTIMPL);
}
22 changes: 22 additions & 0 deletions Kernel/SharedMemory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <AK/Retainable.h>
#include <AK/RetainPtr.h>
#include <Kernel/KResult.h>

class VMObject;

class SharedMemory : public Retainable<SharedMemory> {
public:
static Retained<SharedMemory> create();
~SharedMemory();

KResult truncate(int);

private:
SharedMemory();

int m_uid { 0 };
int m_gid { 0 };
RetainPtr<VMObject> m_vmo;
};
2 changes: 2 additions & 0 deletions Kernel/Syscall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ static dword handle(RegisterDump& regs, dword function, dword arg1, dword arg2,
return current->process().sys$shm_open((const char*)arg1, (int)arg2, (mode_t)arg3);
case Syscall::SC_shm_close:
return current->process().sys$shm_unlink((const char*)arg1);
case Syscall::SC_ftruncate:
return current->process().sys$ftruncate((int)arg1, (off_t)arg2);
default:
kprintf("<%u> int0x82: Unknown function %u requested {%x, %x, %x}\n", current->process().pid(), function, arg1, arg2, arg3);
break;
Expand Down
1 change: 1 addition & 0 deletions Kernel/Syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
__ENUMERATE_SYSCALL(rename) \
__ENUMERATE_SYSCALL(shm_open) \
__ENUMERATE_SYSCALL(shm_close) \
__ENUMERATE_SYSCALL(ftruncate) \


namespace Syscall {
Expand Down
3 changes: 2 additions & 1 deletion LibC/unistd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ int create_thread(int(*entry)(void*), void* argument)

int ftruncate(int fd, off_t length)
{
ASSERT_NOT_REACHED();
int rc = syscall(SC_ftruncate, fd, length);
__RETURN_WITH_ERRNO(rc, rc, -1);
}

int gettid()
Expand Down

0 comments on commit 26a06f3

Please sign in to comment.