Skip to content

Commit

Permalink
More work towards getting bash to build.
Browse files Browse the repository at this point in the history
Implemented some syscalls: dup(), dup2(), getdtablesize().
FileHandle is now a retainable, since that's needed for dup()'ed fd's.
I didn't really test any of this beyond a basic smoke check.
  • Loading branch information
awesomekling committed Nov 5, 2018
1 parent 82f84ba commit 9f2b9c8
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 23 deletions.
33 changes: 32 additions & 1 deletion Kernel/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,6 @@ int Process::sys$open(const char* path, int options)
if (!m_file_descriptors[fd])
break;
}
handle->setFD(fd);
m_file_descriptors[fd] = move(handle);
return fd;
}
Expand Down Expand Up @@ -1435,3 +1434,35 @@ int Process::sys$tcsetpgrp(int fd, pid_t pgid)
tty.set_pgid(pgid);
return 0;
}

int Process::sys$getdtablesize()
{
return m_max_open_file_descriptors;
}

int Process::sys$dup(int old_fd)
{
auto* handle = fileHandleIfExists(old_fd);
if (!handle)
return -EBADF;
if (number_of_open_file_descriptors() == m_max_open_file_descriptors)
return -EMFILE;
int new_fd = 0;
for (; new_fd < m_max_open_file_descriptors; ++new_fd) {
if (!m_file_descriptors[new_fd])
break;
}
m_file_descriptors[new_fd] = handle;
return new_fd;
}

int Process::sys$dup2(int old_fd, int new_fd)
{
auto* handle = fileHandleIfExists(old_fd);
if (!handle)
return -EBADF;
if (number_of_open_file_descriptors() == m_max_open_file_descriptors)
return -EMFILE;
m_file_descriptors[new_fd] = handle;
return new_fd;
}
5 changes: 4 additions & 1 deletion Kernel/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ class Process : public InlineLinkedListNode<Process> {
int sys$execve(const char* filename, const char** argv, const char** envp);
Unix::sighandler_t sys$signal(int signum, Unix::sighandler_t);
int sys$isatty(int fd);
int sys$getdtablesize();
int sys$dup(int oldfd);
int sys$dup2(int oldfd, int newfd);

static void initialize();

Expand Down Expand Up @@ -193,7 +196,7 @@ class Process : public InlineLinkedListNode<Process> {
State m_state { Invalid };
DWORD m_wakeupTime { 0 };
TSS32 m_tss;
Vector<OwnPtr<FileHandle>> m_file_descriptors;
Vector<RetainPtr<FileHandle>> m_file_descriptors;
RingLevel m_ring { Ring0 };
int m_error { 0 };
void* m_kernelStack { nullptr };
Expand Down
6 changes: 6 additions & 0 deletions Kernel/Syscall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ static DWORD handle(RegisterDump& regs, DWORD function, DWORD arg1, DWORD arg2,
return (dword)current->sys$signal((int)arg1, (Unix::sighandler_t)arg2);
case Syscall::PosixIsatty:
return current->sys$isatty((int)arg1);
case Syscall::Getdtablesize:
return current->sys$getdtablesize();
case Syscall::Dup:
return current->sys$dup((int)arg1);
case Syscall::Dup2:
return current->sys$dup2((int)arg1, (int)arg2);
default:
kprintf("<%u> int0x80: Unknown function %x requested {%x, %x, %x}\n", current->pid(), function, arg1, arg2, arg3);
break;
Expand Down
3 changes: 3 additions & 0 deletions Kernel/Syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ enum Function {
PosixGetegid = 0x2021,
PosixSignal = 0x2022,
PosixIsatty = 0x2023,
Getdtablesize = 0x2024,
Dup = 0x2025,
Dup2 = 0x2026,
};

void initialize();
Expand Down
11 changes: 11 additions & 0 deletions LibC/dirent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <Kernel/Syscall.h>

extern "C" {
Expand All @@ -20,6 +21,16 @@ DIR* opendir(const char* name)
return dirp;
}

int closedir(DIR* dirp)
{
if (!dirp || dirp->fd == -1)
return -EBADF;
int rc = close(dirp->fd);
if (rc == 0)
dirp->fd = -1;
return rc;
}

struct sys_dirent {
ino_t ino;
byte file_type;
Expand Down
3 changes: 2 additions & 1 deletion LibC/dirent.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ struct __DIR {
typedef struct __DIR DIR;

DIR* opendir(const char* name);
struct dirent* readdir(DIR* dirp);
int closedir(DIR*);
struct dirent* readdir(DIR*);

__END_DECLS

11 changes: 11 additions & 0 deletions LibC/stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ FILE* fopen(const char* pathname, const char* mode)
return fp;
}

FILE* fdopen(int fd, const char* mode)
{
assert(!strcmp(mode, "r") || !strcmp(mode, "rb"));
if (fd < 0)
return nullptr;
auto* fp = (FILE*)malloc(sizeof(FILE));
fp->fd = fd;
fp->eof = false;
return fp;
}

int fclose(FILE* stream)
{
int rc = close(stream->fd);
Expand Down
1 change: 1 addition & 0 deletions LibC/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ int fileno(FILE*);
int fgetc(FILE*);
int getc(FILE*);
int getchar();
FILE* fdopen(int fd, const char* mode);
FILE* fopen(const char* pathname, const char* mode);
int fclose(FILE*);
void rewind(FILE*);
Expand Down
Empty file added LibC/sys/resource.h
Empty file.
17 changes: 17 additions & 0 deletions LibC/unistd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,22 @@ int isatty(int fd)
__RETURN_WITH_ERRNO(rc, 1, 0);
}

int getdtablesize()
{
int rc = Syscall::invoke(Syscall::Getdtablesize);
__RETURN_WITH_ERRNO(rc, 1, 0);
}

int dup(int old_fd)
{
int rc = Syscall::invoke(Syscall::Dup, (dword)old_fd);
__RETURN_WITH_ERRNO(rc, rc, -1);
}

int dup2(int old_fd, int new_fd)
{
int rc = Syscall::invoke(Syscall::Dup2, (dword)old_fd, (dword)new_fd);
__RETURN_WITH_ERRNO(rc, rc, -1);
}

}
3 changes: 3 additions & 0 deletions LibC/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ int ttyname_r(int fd, char* buffer, size_t);
off_t lseek(int fd, off_t, int whence);
int link(const char* oldpath, const char* newpath);
int unlink(const char* pathname);
int getdtablesize();
int dup(int old_fd);
int dup2(int old_fd, int new_fd);

#define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
#define WTERMSIG(status) ((status) & 0x7f)
Expand Down
2 changes: 1 addition & 1 deletion VirtualFileSystem/CharacterDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CharacterDevice::~CharacterDevice()
{
}

OwnPtr<FileHandle> CharacterDevice::open(int options)
RetainPtr<FileHandle> CharacterDevice::open(int options)
{
return VirtualFileSystem::the().open(*this, options);
}
2 changes: 1 addition & 1 deletion VirtualFileSystem/CharacterDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CharacterDevice {
public:
virtual ~CharacterDevice();

virtual OwnPtr<FileHandle> open(int options);
RetainPtr<FileHandle> open(int options);

virtual bool hasDataAvailableForRead() const = 0;

Expand Down
9 changes: 7 additions & 2 deletions VirtualFileSystem/FileHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#include "TTY.h"
#include <AK/BufferStream.h>

RetainPtr<FileHandle> FileHandle::create(RetainPtr<VirtualFileSystem::Node>&& vnode)
{
return adopt(*new FileHandle(move(vnode)));
}

FileHandle::FileHandle(RetainPtr<VirtualFileSystem::Node>&& vnode)
: m_vnode(move(vnode))
{
Expand All @@ -15,9 +20,9 @@ FileHandle::~FileHandle()
{
}

OwnPtr<FileHandle> FileHandle::clone()
RetainPtr<FileHandle> FileHandle::clone()
{
auto handle = make<FileHandle>(m_vnode.copyRef());
auto handle = FileHandle::create(m_vnode.copyRef());
if (!handle)
return nullptr;
handle->m_currentOffset = m_currentOffset;
Expand Down
11 changes: 5 additions & 6 deletions VirtualFileSystem/FileHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
#include "VirtualFileSystem.h"
#include "InodeMetadata.h"
#include <AK/ByteBuffer.h>
#include <AK/Retainable.h>

class TTY;

class FileHandle {
class FileHandle : public Retainable<FileHandle> {
public:
explicit FileHandle(RetainPtr<VirtualFileSystem::Node>&&);
static RetainPtr<FileHandle> create(RetainPtr<VirtualFileSystem::Node>&&);
~FileHandle();

OwnPtr<FileHandle> clone();
RetainPtr<FileHandle> clone();

int close();

Expand Down Expand Up @@ -39,9 +40,6 @@ class FileHandle {
VirtualFileSystem::Node* vnode() { return m_vnode.ptr(); }

#ifdef SERENITY
int fd() const { return m_fd; }
void setFD(int fd) { m_fd = fd; }

bool isBlocking() const { return m_isBlocking; }
void setBlocking(bool b) { m_isBlocking = b; }
#endif
Expand All @@ -50,6 +48,7 @@ class FileHandle {

private:
friend class VirtualFileSystem;
explicit FileHandle(RetainPtr<VirtualFileSystem::Node>&&);

RetainPtr<VirtualFileSystem::Node> m_vnode;

Expand Down
12 changes: 6 additions & 6 deletions VirtualFileSystem/VirtualFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,34 +398,34 @@ bool VirtualFileSystem::touch(const String& path)
return inode.fileSystem()->setModificationTime(inode, ktime(nullptr));
}

OwnPtr<FileHandle> VirtualFileSystem::open(CharacterDevice& device, int options)
RetainPtr<FileHandle> VirtualFileSystem::open(CharacterDevice& device, int options)
{
auto vnode = getOrCreateNode(device);
if (!vnode)
return nullptr;
return make<FileHandle>(move(vnode));
return FileHandle::create(move(vnode));
}

OwnPtr<FileHandle> VirtualFileSystem::open(const String& path, int& error, int options, InodeIdentifier base)
RetainPtr<FileHandle> VirtualFileSystem::open(const String& path, int& error, int options, InodeIdentifier base)
{
auto inode = resolvePath(path, error, base, options);
if (!inode.isValid())
return nullptr;
auto vnode = getOrCreateNode(inode);
if (!vnode)
return nullptr;
return make<FileHandle>(move(vnode));
return FileHandle::create(move(vnode));
}

OwnPtr<FileHandle> VirtualFileSystem::create(const String& path, InodeIdentifier base)
RetainPtr<FileHandle> VirtualFileSystem::create(const String& path, InodeIdentifier base)
{
// FIXME: Do the real thing, not just this fake thing!
(void) path;
m_rootNode->fileSystem()->createInode(m_rootNode->fileSystem()->rootInode(), "empty", 0100644, 0);
return nullptr;
}

OwnPtr<FileHandle> VirtualFileSystem::mkdir(const String& path, InodeIdentifier base)
RetainPtr<FileHandle> VirtualFileSystem::mkdir(const String& path, InodeIdentifier base)
{
// FIXME: Do the real thing, not just this fake thing!
(void) path;
Expand Down
8 changes: 4 additions & 4 deletions VirtualFileSystem/VirtualFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ class VirtualFileSystem {
bool mountRoot(RetainPtr<FileSystem>&&);
bool mount(RetainPtr<FileSystem>&&, const String& path);

OwnPtr<FileHandle> open(CharacterDevice&, int options);
OwnPtr<FileHandle> open(const String& path, int& error, int options = 0, InodeIdentifier base = InodeIdentifier());
OwnPtr<FileHandle> create(const String& path, InodeIdentifier base = InodeIdentifier());
OwnPtr<FileHandle> mkdir(const String& path, InodeIdentifier base = InodeIdentifier());
RetainPtr<FileHandle> open(CharacterDevice&, int options);
RetainPtr<FileHandle> open(const String& path, int& error, int options = 0, InodeIdentifier base = InodeIdentifier());
RetainPtr<FileHandle> create(const String& path, InodeIdentifier base = InodeIdentifier());
RetainPtr<FileHandle> mkdir(const String& path, InodeIdentifier base = InodeIdentifier());

bool isRoot(InodeIdentifier) const;

Expand Down

0 comments on commit 9f2b9c8

Please sign in to comment.