More work towards getting bash to build.

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.
This commit is contained in:
Andreas Kling 2018-11-05 19:01:22 +01:00
parent 82f84bab11
commit 9f2b9c82bf
Notes: sideshowbarker 2024-07-19 18:33:20 +09:00
17 changed files with 114 additions and 23 deletions

View File

@ -1158,7 +1158,6 @@ int Process::sys$open(const char* path, int options)
if (!m_file_descriptors[fd]) if (!m_file_descriptors[fd])
break; break;
} }
handle->setFD(fd);
m_file_descriptors[fd] = move(handle); m_file_descriptors[fd] = move(handle);
return fd; return fd;
} }
@ -1435,3 +1434,35 @@ int Process::sys$tcsetpgrp(int fd, pid_t pgid)
tty.set_pgid(pgid); tty.set_pgid(pgid);
return 0; 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;
}

View File

@ -126,6 +126,9 @@ public:
int sys$execve(const char* filename, const char** argv, const char** envp); int sys$execve(const char* filename, const char** argv, const char** envp);
Unix::sighandler_t sys$signal(int signum, Unix::sighandler_t); Unix::sighandler_t sys$signal(int signum, Unix::sighandler_t);
int sys$isatty(int fd); int sys$isatty(int fd);
int sys$getdtablesize();
int sys$dup(int oldfd);
int sys$dup2(int oldfd, int newfd);
static void initialize(); static void initialize();
@ -193,7 +196,7 @@ private:
State m_state { Invalid }; State m_state { Invalid };
DWORD m_wakeupTime { 0 }; DWORD m_wakeupTime { 0 };
TSS32 m_tss; TSS32 m_tss;
Vector<OwnPtr<FileHandle>> m_file_descriptors; Vector<RetainPtr<FileHandle>> m_file_descriptors;
RingLevel m_ring { Ring0 }; RingLevel m_ring { Ring0 };
int m_error { 0 }; int m_error { 0 };
void* m_kernelStack { nullptr }; void* m_kernelStack { nullptr };

View File

@ -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); return (dword)current->sys$signal((int)arg1, (Unix::sighandler_t)arg2);
case Syscall::PosixIsatty: case Syscall::PosixIsatty:
return current->sys$isatty((int)arg1); 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: default:
kprintf("<%u> int0x80: Unknown function %x requested {%x, %x, %x}\n", current->pid(), function, arg1, arg2, arg3); kprintf("<%u> int0x80: Unknown function %x requested {%x, %x, %x}\n", current->pid(), function, arg1, arg2, arg3);
break; break;

View File

@ -53,6 +53,9 @@ enum Function {
PosixGetegid = 0x2021, PosixGetegid = 0x2021,
PosixSignal = 0x2022, PosixSignal = 0x2022,
PosixIsatty = 0x2023, PosixIsatty = 0x2023,
Getdtablesize = 0x2024,
Dup = 0x2025,
Dup2 = 0x2026,
}; };
void initialize(); void initialize();

View File

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include <Kernel/Syscall.h> #include <Kernel/Syscall.h>
extern "C" { extern "C" {
@ -20,6 +21,16 @@ DIR* opendir(const char* name)
return dirp; 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 { struct sys_dirent {
ino_t ino; ino_t ino;
byte file_type; byte file_type;

View File

@ -23,7 +23,8 @@ struct __DIR {
typedef struct __DIR DIR; typedef struct __DIR DIR;
DIR* opendir(const char* name); DIR* opendir(const char* name);
struct dirent* readdir(DIR* dirp); int closedir(DIR*);
struct dirent* readdir(DIR*);
__END_DECLS __END_DECLS

View File

@ -193,6 +193,17 @@ FILE* fopen(const char* pathname, const char* mode)
return fp; 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 fclose(FILE* stream)
{ {
int rc = close(stream->fd); int rc = close(stream->fd);

View File

@ -29,6 +29,7 @@ int fileno(FILE*);
int fgetc(FILE*); int fgetc(FILE*);
int getc(FILE*); int getc(FILE*);
int getchar(); int getchar();
FILE* fdopen(int fd, const char* mode);
FILE* fopen(const char* pathname, const char* mode); FILE* fopen(const char* pathname, const char* mode);
int fclose(FILE*); int fclose(FILE*);
void rewind(FILE*); void rewind(FILE*);

0
LibC/sys/resource.h Normal file
View File

View File

@ -190,5 +190,22 @@ int isatty(int fd)
__RETURN_WITH_ERRNO(rc, 1, 0); __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);
}
}

View File

@ -39,6 +39,9 @@ int ttyname_r(int fd, char* buffer, size_t);
off_t lseek(int fd, off_t, int whence); off_t lseek(int fd, off_t, int whence);
int link(const char* oldpath, const char* newpath); int link(const char* oldpath, const char* newpath);
int unlink(const char* pathname); 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 WEXITSTATUS(status) (((status) & 0xff00) >> 8)
#define WTERMSIG(status) ((status) & 0x7f) #define WTERMSIG(status) ((status) & 0x7f)

View File

@ -4,7 +4,7 @@ CharacterDevice::~CharacterDevice()
{ {
} }
OwnPtr<FileHandle> CharacterDevice::open(int options) RetainPtr<FileHandle> CharacterDevice::open(int options)
{ {
return VirtualFileSystem::the().open(*this, options); return VirtualFileSystem::the().open(*this, options);
} }

View File

@ -8,7 +8,7 @@ class CharacterDevice {
public: public:
virtual ~CharacterDevice(); virtual ~CharacterDevice();
virtual OwnPtr<FileHandle> open(int options); RetainPtr<FileHandle> open(int options);
virtual bool hasDataAvailableForRead() const = 0; virtual bool hasDataAvailableForRead() const = 0;

View File

@ -6,6 +6,11 @@
#include "TTY.h" #include "TTY.h"
#include <AK/BufferStream.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) FileHandle::FileHandle(RetainPtr<VirtualFileSystem::Node>&& vnode)
: m_vnode(move(vnode)) : m_vnode(move(vnode))
{ {
@ -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) if (!handle)
return nullptr; return nullptr;
handle->m_currentOffset = m_currentOffset; handle->m_currentOffset = m_currentOffset;

View File

@ -3,15 +3,16 @@
#include "VirtualFileSystem.h" #include "VirtualFileSystem.h"
#include "InodeMetadata.h" #include "InodeMetadata.h"
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/Retainable.h>
class TTY; class TTY;
class FileHandle { class FileHandle : public Retainable<FileHandle> {
public: public:
explicit FileHandle(RetainPtr<VirtualFileSystem::Node>&&); static RetainPtr<FileHandle> create(RetainPtr<VirtualFileSystem::Node>&&);
~FileHandle(); ~FileHandle();
OwnPtr<FileHandle> clone(); RetainPtr<FileHandle> clone();
int close(); int close();
@ -39,9 +40,6 @@ public:
VirtualFileSystem::Node* vnode() { return m_vnode.ptr(); } VirtualFileSystem::Node* vnode() { return m_vnode.ptr(); }
#ifdef SERENITY #ifdef SERENITY
int fd() const { return m_fd; }
void setFD(int fd) { m_fd = fd; }
bool isBlocking() const { return m_isBlocking; } bool isBlocking() const { return m_isBlocking; }
void setBlocking(bool b) { m_isBlocking = b; } void setBlocking(bool b) { m_isBlocking = b; }
#endif #endif
@ -50,6 +48,7 @@ public:
private: private:
friend class VirtualFileSystem; friend class VirtualFileSystem;
explicit FileHandle(RetainPtr<VirtualFileSystem::Node>&&);
RetainPtr<VirtualFileSystem::Node> m_vnode; RetainPtr<VirtualFileSystem::Node> m_vnode;

View File

@ -398,15 +398,15 @@ bool VirtualFileSystem::touch(const String& path)
return inode.fileSystem()->setModificationTime(inode, ktime(nullptr)); 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); auto vnode = getOrCreateNode(device);
if (!vnode) if (!vnode)
return nullptr; 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); auto inode = resolvePath(path, error, base, options);
if (!inode.isValid()) if (!inode.isValid())
@ -414,10 +414,10 @@ OwnPtr<FileHandle> VirtualFileSystem::open(const String& path, int& error, int o
auto vnode = getOrCreateNode(inode); auto vnode = getOrCreateNode(inode);
if (!vnode) if (!vnode)
return nullptr; 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! // FIXME: Do the real thing, not just this fake thing!
(void) path; (void) path;
@ -425,7 +425,7 @@ OwnPtr<FileHandle> VirtualFileSystem::create(const String& path, InodeIdentifier
return nullptr; 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! // FIXME: Do the real thing, not just this fake thing!
(void) path; (void) path;

View File

@ -93,10 +93,10 @@ public:
bool mountRoot(RetainPtr<FileSystem>&&); bool mountRoot(RetainPtr<FileSystem>&&);
bool mount(RetainPtr<FileSystem>&&, const String& path); bool mount(RetainPtr<FileSystem>&&, const String& path);
OwnPtr<FileHandle> open(CharacterDevice&, int options); RetainPtr<FileHandle> open(CharacterDevice&, int options);
OwnPtr<FileHandle> open(const String& path, int& error, int options = 0, InodeIdentifier base = InodeIdentifier()); RetainPtr<FileHandle> open(const String& path, int& error, int options = 0, InodeIdentifier base = InodeIdentifier());
OwnPtr<FileHandle> create(const String& path, InodeIdentifier base = InodeIdentifier()); RetainPtr<FileHandle> create(const String& path, InodeIdentifier base = InodeIdentifier());
OwnPtr<FileHandle> mkdir(const String& path, InodeIdentifier base = InodeIdentifier()); RetainPtr<FileHandle> mkdir(const String& path, InodeIdentifier base = InodeIdentifier());
bool isRoot(InodeIdentifier) const; bool isRoot(InodeIdentifier) const;