Compare commits

...

5 Commits

Author SHA1 Message Date
770286a19d
kernel+libc: Implement fcntl() for F_DUPFD and F_DUPFD_CLOEXEC
All checks were successful
continuous-integration/drone/push Build is passing
2023-03-24 21:33:20 +01:00
0de41410c6
kernel: Use path as the new process name instead of argv[0] 2023-03-24 21:26:45 +01:00
8b712b04c2
kernel: Build with optimizations only in debug mode 2023-03-24 21:25:26 +01:00
36e48b2ad2
kernel: Do not attempt to close non-existent FDs on exec() 2023-03-24 21:21:13 +01:00
374a9ff7b8
kernel+libc: Implement O_CLOEXEC 2023-03-24 21:19:24 +01:00
11 changed files with 80 additions and 7 deletions

View File

@ -74,8 +74,6 @@ target_link_libraries(moon luna-freestanding)
target_compile_definitions(moon PRIVATE IN_MOON)
target_compile_options(moon PRIVATE -Os)
target_compile_options(moon PRIVATE -Wall -Wextra -Werror -Wvla -Wsign-conversion)
target_compile_options(moon PRIVATE -Wdisabled-optimization -Wformat=2 -Winit-self)
target_compile_options(moon PRIVATE -Wmissing-include-dirs -Wswitch-default -Wcast-qual -Wundef)
@ -94,6 +92,8 @@ if(MOON_DEBUG_SYMBOLS)
message(STATUS "Building Moon with debug symbols")
target_compile_options(moon PRIVATE -ggdb)
include(debug.cmake)
else()
target_compile_options(moon PRIVATE -Os)
endif()
target_link_options(moon PRIVATE -lgcc -Wl,--build-id=none -z max-page-size=0x1000 -mcmodel=kernel)

View File

@ -6,6 +6,7 @@
#include "thread/Scheduler.h"
#include "thread/ThreadImage.h"
#include <bits/modes.h>
#include <bits/open-flags.h>
#include <luna/CString.h>
#include <luna/ScopeGuard.h>
#include <luna/Vector.h>
@ -76,12 +77,16 @@ Result<u64> sys_exec(Registers* regs, SyscallArgs args)
guard.deactivate();
// FIXME: Close O_CLOEXEC file descriptors.
// for (int i = 0; i < FD_MAX; i++) { current->fd_table[i] = {}; }
for (int i = 0; i < FD_MAX; i++)
{
auto& descriptor = current->fd_table[i];
if (!descriptor.has_value()) continue;
if (descriptor->flags & O_CLOEXEC) descriptor = {};
}
MMU::delete_userspace_page_directory(current->directory);
current->name = argv[0].chars();
current->name = path.chars();
image->apply(current);

View File

@ -3,6 +3,8 @@
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <bits/fcntl.h>
#include <bits/open-flags.h>
#include <bits/seek.h>
#include <luna/SafeArithmetic.h>
#include <sys/types.h>
@ -84,3 +86,33 @@ Result<u64> sys_lseek(Registers*, SyscallArgs args)
return (u64)new_offset;
}
Result<u64> sys_fcntl(Registers*, SyscallArgs args)
{
int fd = (int)args[0];
int cmd = (int)args[1];
Thread* current = Scheduler::current();
auto& descriptor = *TRY(current->resolve_fd(fd));
bool is_cloexec = true;
switch (cmd)
{
case F_DUPFD: is_cloexec = false; [[fallthrough]];
case F_DUPFD_CLOEXEC: {
int arg = (int)args[2];
int new_fd = TRY(current->allocate_fd(arg));
current->fd_table[new_fd] = descriptor;
if (is_cloexec) current->fd_table[new_fd]->flags |= O_CLOEXEC;
else
current->fd_table[new_fd]->flags &= ~O_CLOEXEC;
return (u64)new_fd;
}
default: return err(EINVAL);
}
}

View File

@ -7,7 +7,7 @@
#include <bits/open-flags.h>
// These flags are needed after open(), the rest only affect open().
constexpr int FLAGS_TO_KEEP = O_RDWR | O_APPEND | O_NONBLOCK;
constexpr int FLAGS_TO_KEEP = O_RDWR | O_APPEND | O_NONBLOCK | O_CLOEXEC;
Result<u64> sys_open(Registers*, SyscallArgs args)
{

View File

@ -0,0 +1,9 @@
/* bits/fcntl.h: File control flags. */
#ifndef _BITS_FCNTL_H
#define _BITS_FCNTL_H
#define F_DUPFD 0
#define F_DUPFD_CLOEXEC 1
#endif

View File

@ -11,5 +11,6 @@
#define O_EXCL 16
#define O_TRUNC 32
#define O_NONBLOCK 64
#define O_CLOEXEC 128
#endif

View File

@ -3,6 +3,7 @@
#ifndef _FCNTL_H
#define _FCNTL_H
#include <bits/fcntl.h>
#include <bits/open-flags.h>
#include <sys/types.h>
@ -17,6 +18,9 @@ extern "C"
/* Create a file and return a file descriptor to it. */
int creat(const char* path, mode_t mode);
/* Perform a file control operation. */
int fcntl(int fd, int cmd, ...);
#ifdef __cplusplus
}
#endif

View File

@ -55,6 +55,9 @@ extern "C"
/* Modify a file descriptor's offset. */
off_t lseek(int fd, off_t offset, int whence);
/* Duplicate a file descriptor. */
int dup(int fd);
#ifdef __cplusplus
}
#endif

View File

@ -23,4 +23,17 @@ extern "C"
{
return open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
}
int fcntl(int fd, int cmd, ...)
{
va_list ap;
va_start(ap, cmd);
uintptr_t arg = (uintptr_t)va_arg(ap, uintptr_t);
long rc = syscall(SYS_fcntl, fd, cmd, arg);
va_end(ap);
__errno_return(rc, int);
}
}

View File

@ -1,4 +1,5 @@
#include <bits/errno-return.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdint.h>
#include <sys/syscall.h>
@ -83,4 +84,9 @@ extern "C"
long rc = syscall(SYS_lseek, fd, offset, whence);
__errno_return(rc, off_t);
}
int dup(int fd)
{
return fcntl(fd, F_DUPFD, 0);
}
}

View File

@ -2,7 +2,7 @@
#define enumerate_syscalls(_e) \
_e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(open) _e(close) _e(read) _e(getpid) _e(write) \
_e(lseek) _e(mkdir) _e(exec) _e(mknod) _e(fork) _e(waitpid) _e(getppid)
_e(lseek) _e(mkdir) _e(exec) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl)
enum Syscalls
{