Kernel, libc: Add access()

This commit is contained in:
apio 2022-10-20 19:03:24 +02:00
parent 27448611b3
commit f7cf395f71
7 changed files with 89 additions and 60 deletions

View File

@ -21,6 +21,7 @@
#define SYS_mkdir 16
#define SYS_fork 17
#define SYS_waitpid 18
#define SYS_access 19
namespace Syscall
{
@ -46,3 +47,4 @@ void sys_clock(Context* context);
void sys_mkdir(Context* context, const char* filename);
void sys_fork(Context* context);
void sys_waitpid(Context* context, long pid, int* wstatus, int options);
void sys_access(Context* context, const char* path, int amode);

View File

@ -30,6 +30,7 @@ void Syscall::entry(Context* context)
case SYS_mkdir: sys_mkdir(context, (const char*)context->rdi); break;
case SYS_fork: sys_fork(context); break;
case SYS_waitpid: sys_waitpid(context, (long)context->rdi, (int*)context->rsi, (int)context->rdx); break;
case SYS_access: sys_access(context, (const char*)context->rdi, (int)context->rsi); break;
default: context->rax = -ENOSYS; break;
}
VMM::exit_syscall_context();

View File

@ -239,3 +239,12 @@ void sys_mkdir(Context* context, const char* filename)
context->rax = rc;
}
void sys_access(Context* context, const char* path, int) // FIXME: Use the amode argument.
{
char* kpath = strdup_from_user(path);
if (!VFS::exists(kpath)) { context->rax = -ENOENT; }
else
context->rax = 0;
kfree(kpath);
}

View File

@ -20,5 +20,6 @@
#define SYS_mkdir 16
#define SYS_fork 17
#define SYS_waitpid 18
#define SYS_access 19
#endif

View File

@ -6,9 +6,14 @@
#include <luna/os-limits.h>
#include <sys/types.h>
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
#define STDIN_FILENO 0 // The standard input stream.
#define STDOUT_FILENO 1 // The standard output stream.
#define STDERR_FILENO 2 // The standard error stream.
#define F_OK 0 // Check for a file's existence.
#define R_OK 1 // Check whether a file is readable.
#define W_OK 2 // Check whether a file is writable.
#define X_OK 4 // Check whether a file is executable.
#ifdef __cplusplus
extern "C"
@ -58,6 +63,9 @@ extern "C"
/* Returns a copy of the file descriptor fd. */
int dup(int fd);
/* Checks if the current program can access the file or directory at path. */
int access(const char* path, int amode);
#ifdef __cplusplus
}
#endif

59
libs/libc/src/syscall.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <bits/error.h>
#include <errno.h>
#include <luna/syscall.h>
#include <stdarg.h>
#include <sys/syscall.h>
extern "C" long syscall(long number, ...)
{
typedef unsigned long arg;
long result;
va_list ap;
va_start(ap, number);
switch (number)
{
case SYS_clock:
case SYS_yield:
case SYS_fork: result = __luna_syscall0(number); break;
case SYS_exit:
case SYS_getprocid:
case SYS_close:
case SYS_exec:
case SYS_mkdir:
case SYS_sleep: result = __luna_syscall1(number, va_arg(ap, arg)); break;
case SYS_munmap:
case SYS_access:
case SYS_open: {
arg arg0 = va_arg(ap, arg);
arg arg1 = va_arg(ap, arg);
result = __luna_syscall2(number, arg0, arg1);
break;
}
case SYS_fcntl:
case SYS_seek:
case SYS_write:
case SYS_read:
case SYS_mprotect:
case SYS_waitpid:
case SYS_mmap: {
arg arg0 = va_arg(ap, arg);
arg arg1 = va_arg(ap, arg);
arg arg2 = va_arg(ap, arg);
result = __luna_syscall3(number, arg0, arg1, arg2);
break;
}
case SYS_paint: {
arg arg0 = va_arg(ap, arg);
arg arg1 = va_arg(ap, arg);
arg arg2 = va_arg(ap, arg);
arg arg3 = va_arg(ap, arg);
arg arg4 = va_arg(ap, arg);
result = __luna_syscall5(number, arg0, arg1, arg2, arg3, arg4);
break;
}
default: result = -ENOSYS; break;
}
va_end(ap);
if (number == SYS_mmap) { _RETURN_WITH_MEMORY_ERRNO(result, long int); }
else { _RETURN_WITH_ERRNO(result, long); }
}

View File

@ -1,8 +1,5 @@
#include <bits/error.h>
#include <fcntl.h>
#include <luna.h>
#include <luna/syscall.h>
#include <stdarg.h>
#include <sys/syscall.h>
#include <unistd.h>
@ -37,59 +34,6 @@ extern "C"
return getprocid(ID_PPID);
}
long syscall(long number, ...)
{
typedef unsigned long int arg;
long result;
va_list ap;
va_start(ap, number);
switch (number)
{
case SYS_clock:
case SYS_yield:
case SYS_fork: result = __luna_syscall0(number); break;
case SYS_exit:
case SYS_getprocid:
case SYS_close:
case SYS_exec:
case SYS_mkdir:
case SYS_sleep: result = __luna_syscall1(number, va_arg(ap, arg)); break;
case SYS_munmap:
case SYS_open: {
arg arg0 = va_arg(ap, arg);
arg arg1 = va_arg(ap, arg);
result = __luna_syscall2(number, arg0, arg1);
break;
}
case SYS_fcntl:
case SYS_seek:
case SYS_write:
case SYS_read:
case SYS_mprotect:
case SYS_waitpid:
case SYS_mmap: {
arg arg0 = va_arg(ap, arg);
arg arg1 = va_arg(ap, arg);
arg arg2 = va_arg(ap, arg);
result = __luna_syscall3(number, arg0, arg1, arg2);
break;
}
case SYS_paint: {
arg arg0 = va_arg(ap, arg);
arg arg1 = va_arg(ap, arg);
arg arg2 = va_arg(ap, arg);
arg arg3 = va_arg(ap, arg);
arg arg4 = va_arg(ap, arg);
result = __luna_syscall5(number, arg0, arg1, arg2, arg3, arg4);
break;
}
default: result = -ENOSYS; break;
}
va_end(ap);
if (number == SYS_mmap) { _RETURN_WITH_MEMORY_ERRNO(result, long int); }
else { _RETURN_WITH_ERRNO(result, long); }
}
unsigned int sleep(unsigned int seconds)
{
return msleep(seconds * 1000);
@ -125,4 +69,9 @@ extern "C"
{
return fcntl(fd, F_DUPFD, 0);
}
int access(const char* path, int amode)
{
return (int)syscall(SYS_access, path, amode);
}
}