2023-04-12 17:53:02 +03:00
|
|
|
#include <BAN/Assert.h>
|
2023-05-26 22:21:07 +03:00
|
|
|
#include <kernel/Syscall.h>
|
2023-05-07 01:51:39 +03:00
|
|
|
#include <errno.h>
|
2023-04-12 17:53:02 +03:00
|
|
|
#include <stdarg.h>
|
2023-04-25 12:38:08 +03:00
|
|
|
#include <stdio.h>
|
2023-05-31 20:57:33 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-06-05 14:36:17 +03:00
|
|
|
#include <sys/stat.h>
|
2023-04-12 17:53:02 +03:00
|
|
|
#include <sys/syscall.h>
|
2023-04-05 23:58:40 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-06-05 20:34:08 +03:00
|
|
|
char** environ;
|
|
|
|
|
|
|
|
extern "C" void _init_libc(char** _environ)
|
2023-05-29 20:21:19 +03:00
|
|
|
{
|
2023-06-05 20:34:08 +03:00
|
|
|
environ = _environ;
|
2023-05-29 20:21:19 +03:00
|
|
|
}
|
|
|
|
|
2023-04-12 17:53:02 +03:00
|
|
|
void _exit(int status)
|
|
|
|
{
|
|
|
|
syscall(SYS_EXIT, status);
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
long syscall(long syscall, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, syscall);
|
|
|
|
|
2023-04-25 12:38:08 +03:00
|
|
|
long ret = -1;
|
2023-04-12 17:53:02 +03:00
|
|
|
|
|
|
|
switch (syscall)
|
|
|
|
{
|
2023-04-22 15:29:15 +03:00
|
|
|
case SYS_EXIT:
|
|
|
|
{
|
|
|
|
int exit_code = va_arg(args, int);
|
|
|
|
ret = Kernel::syscall(SYS_EXIT, exit_code);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_READ:
|
|
|
|
{
|
|
|
|
int fd = va_arg(args, int);
|
|
|
|
void* buffer = va_arg(args, void*);
|
2023-05-09 20:31:22 +03:00
|
|
|
size_t offset = va_arg(args, size_t);
|
2023-04-22 15:29:15 +03:00
|
|
|
size_t bytes = va_arg(args, size_t);
|
2023-05-09 20:31:22 +03:00
|
|
|
ret = Kernel::syscall(SYS_READ, fd, (uintptr_t)buffer, offset, bytes);
|
2023-04-22 15:29:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_WRITE:
|
|
|
|
{
|
|
|
|
int fd = va_arg(args, int);
|
|
|
|
const char* string = va_arg(args, const char*);
|
2023-05-09 20:31:22 +03:00
|
|
|
size_t offset = va_arg(args, size_t);
|
2023-04-22 15:29:15 +03:00
|
|
|
size_t bytes = va_arg(args, size_t);
|
2023-05-09 20:31:22 +03:00
|
|
|
ret = Kernel::syscall(SYS_WRITE, fd, (uintptr_t)string, offset, bytes);
|
2023-04-22 15:29:15 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-04-25 12:38:08 +03:00
|
|
|
case SYS_TERMID:
|
|
|
|
{
|
|
|
|
char* buffer = va_arg(args, char*);
|
2023-05-09 20:31:22 +03:00
|
|
|
ret = Kernel::syscall(SYS_TERMID, (uintptr_t)buffer);
|
2023-04-25 12:38:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_CLOSE:
|
|
|
|
{
|
|
|
|
int fd = va_arg(args, int);
|
|
|
|
ret = Kernel::syscall(SYS_CLOSE, fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_OPEN:
|
|
|
|
{
|
|
|
|
const char* path = va_arg(args, const char*);
|
|
|
|
int oflags = va_arg(args, int);
|
2023-05-09 20:31:22 +03:00
|
|
|
ret = Kernel::syscall(SYS_OPEN, (uintptr_t)path, oflags);
|
2023-04-25 12:38:08 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-06-11 03:28:43 +03:00
|
|
|
case SYS_OPENAT:
|
|
|
|
{
|
|
|
|
int fd = va_arg(args, int);
|
|
|
|
const char* path = va_arg(args, const char*);
|
|
|
|
int oflags = va_arg(args, int);
|
|
|
|
ret = Kernel::syscall(SYS_OPENAT, fd, (uintptr_t)path, oflags);
|
|
|
|
break;
|
|
|
|
}
|
2023-05-06 18:10:38 +03:00
|
|
|
case SYS_ALLOC:
|
|
|
|
{
|
|
|
|
size_t bytes = va_arg(args, size_t);
|
|
|
|
ret = Kernel::syscall(SYS_ALLOC, bytes);
|
|
|
|
break;
|
|
|
|
}
|
2023-06-11 03:28:43 +03:00
|
|
|
case SYS_REALLOC:
|
|
|
|
{
|
|
|
|
void* ptr = va_arg(args, void*);
|
|
|
|
size_t size = va_arg(args, size_t);
|
|
|
|
ret = Kernel::syscall(SYS_REALLOC, (uintptr_t)ptr, size);
|
|
|
|
break;
|
|
|
|
}
|
2023-05-07 01:21:50 +03:00
|
|
|
case SYS_FREE:
|
|
|
|
{
|
|
|
|
void* ptr = va_arg(args, void*);
|
2023-05-09 20:31:22 +03:00
|
|
|
ret = Kernel::syscall(SYS_FREE, (uintptr_t)ptr);
|
2023-05-07 01:21:50 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-05-16 14:14:47 +03:00
|
|
|
case SYS_SEEK:
|
|
|
|
{
|
|
|
|
int fd = va_arg(args, int);
|
|
|
|
off_t offset = va_arg(args, off_t);
|
|
|
|
int whence = va_arg(args, int);
|
|
|
|
ret = Kernel::syscall(SYS_SEEK, fd, offset, whence);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_TELL:
|
|
|
|
{
|
|
|
|
int fd = va_arg(args, int);
|
|
|
|
ret = Kernel::syscall(SYS_TELL, fd);
|
|
|
|
break;
|
|
|
|
}
|
2023-05-16 19:22:10 +03:00
|
|
|
case SYS_GET_TERMIOS:
|
|
|
|
{
|
|
|
|
struct termios* termios = va_arg(args, struct termios*);
|
|
|
|
ret = Kernel::syscall(SYS_GET_TERMIOS, (uintptr_t)termios);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_SET_TERMIOS:
|
|
|
|
{
|
|
|
|
const struct termios* termios = va_arg(args, const struct termios*);
|
|
|
|
ret = Kernel::syscall(SYS_SET_TERMIOS, (uintptr_t)termios);
|
|
|
|
break;
|
|
|
|
}
|
2023-05-28 18:08:26 +03:00
|
|
|
case SYS_FORK:
|
|
|
|
{
|
|
|
|
ret = Kernel::syscall(SYS_FORK);
|
|
|
|
break;
|
|
|
|
}
|
2023-05-31 20:57:33 +03:00
|
|
|
case SYS_EXEC:
|
|
|
|
{
|
|
|
|
const char* pathname = va_arg(args, const char*);
|
|
|
|
const char* const* argv = va_arg(args, const char* const*);
|
|
|
|
const char* const* envp = va_arg(args, const char* const*);
|
|
|
|
ret = Kernel::syscall(SYS_EXEC, (uintptr_t)pathname, (uintptr_t)argv, (uintptr_t)envp);
|
|
|
|
break;
|
|
|
|
}
|
2023-06-11 03:28:43 +03:00
|
|
|
case SYS_SLEEP:
|
2023-06-02 17:56:13 +03:00
|
|
|
{
|
2023-06-11 03:28:43 +03:00
|
|
|
unsigned int seconds = va_arg(args, unsigned int);
|
|
|
|
ret = Kernel::syscall(SYS_SLEEP, seconds);
|
2023-06-02 17:56:13 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-06-04 17:59:13 +03:00
|
|
|
case SYS_WAIT:
|
|
|
|
{
|
|
|
|
pid_t pid = va_arg(args, pid_t);
|
|
|
|
int* stat_loc = va_arg(args, int*);
|
|
|
|
int options = va_arg(args, int);
|
|
|
|
ret = Kernel::syscall(SYS_WAIT, pid, (uintptr_t)stat_loc, options);
|
|
|
|
break;
|
|
|
|
}
|
2023-06-11 00:54:04 +03:00
|
|
|
case SYS_FSTAT:
|
2023-06-05 14:36:17 +03:00
|
|
|
{
|
2023-06-11 00:54:04 +03:00
|
|
|
int fd = va_arg(args, int);
|
2023-06-05 14:36:17 +03:00
|
|
|
struct stat* buf = va_arg(args, struct stat*);
|
2023-06-11 00:54:04 +03:00
|
|
|
ret = Kernel::syscall(SYS_FSTAT, (uintptr_t)fd, (uintptr_t)buf);
|
2023-06-05 14:36:17 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-06-05 22:43:26 +03:00
|
|
|
case SYS_SETENVP:
|
|
|
|
{
|
|
|
|
char** envp = va_arg(args, char**);
|
|
|
|
ret = Kernel::syscall(SYS_SETENVP, (uintptr_t)envp);
|
|
|
|
break;
|
|
|
|
}
|
2023-06-11 00:18:08 +03:00
|
|
|
case SYS_READ_DIR_ENTRIES:
|
|
|
|
{
|
|
|
|
int fd = va_arg(args, int);
|
|
|
|
void* buffer = va_arg(args, void*);
|
|
|
|
size_t buffer_size = va_arg(args, size_t);
|
|
|
|
ret = Kernel::syscall(SYS_READ_DIR_ENTRIES, fd, (uintptr_t)buffer, buffer_size);
|
|
|
|
break;
|
|
|
|
}
|
2023-06-11 22:15:35 +03:00
|
|
|
case SYS_SET_UID:
|
|
|
|
{
|
|
|
|
uid_t uid = va_arg(args, uid_t);
|
|
|
|
ret = Kernel::syscall(SYS_SET_UID, uid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_SET_GID:
|
|
|
|
{
|
|
|
|
gid_t gid = va_arg(args, gid_t);
|
|
|
|
ret = Kernel::syscall(SYS_SET_GID, gid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_SET_EUID:
|
|
|
|
{
|
|
|
|
uid_t uid = va_arg(args, uid_t);
|
|
|
|
ret = Kernel::syscall(SYS_SET_EUID, uid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_SET_EGID:
|
|
|
|
{
|
|
|
|
gid_t gid = va_arg(args, gid_t);
|
|
|
|
ret = Kernel::syscall(SYS_SET_EGID, gid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_SET_REUID:
|
|
|
|
{
|
|
|
|
uid_t ruid = va_arg(args, uid_t);
|
|
|
|
uid_t euid = va_arg(args, uid_t);
|
|
|
|
ret = Kernel::syscall(SYS_SET_REUID, ruid, euid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_SET_REGID:
|
|
|
|
{
|
|
|
|
gid_t rgid = va_arg(args, gid_t);
|
|
|
|
gid_t egid = va_arg(args, gid_t);
|
|
|
|
ret = Kernel::syscall(SYS_SET_REGID, rgid, egid);
|
|
|
|
break;
|
|
|
|
}
|
2023-06-11 22:27:53 +03:00
|
|
|
case SYS_GET_UID:
|
|
|
|
{
|
|
|
|
ret = Kernel::syscall(SYS_GET_UID);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_GET_GID:
|
|
|
|
{
|
|
|
|
ret = Kernel::syscall(SYS_GET_GID);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_GET_EUID:
|
|
|
|
{
|
|
|
|
ret = Kernel::syscall(SYS_GET_EUID);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYS_GET_EGID:
|
|
|
|
{
|
|
|
|
ret = Kernel::syscall(SYS_GET_EGID);
|
|
|
|
break;
|
|
|
|
}
|
2023-04-25 12:38:08 +03:00
|
|
|
default:
|
|
|
|
puts("LibC: Unhandeled syscall");
|
2023-05-07 01:51:39 +03:00
|
|
|
ret = -ENOSYS;
|
|
|
|
break;
|
2023-04-12 17:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
2023-05-07 01:51:39 +03:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errno = -ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-04-12 17:53:02 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-06-11 01:10:28 +03:00
|
|
|
int close(int fd)
|
|
|
|
{
|
|
|
|
return syscall(SYS_CLOSE, fd);
|
|
|
|
}
|
|
|
|
|
2023-05-31 22:36:26 +03:00
|
|
|
int execl(const char* pathname, const char* arg0, ...)
|
|
|
|
{
|
|
|
|
if (arg0 == nullptr)
|
|
|
|
{
|
|
|
|
char* temp = nullptr;
|
|
|
|
return execv(pathname, &temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, arg0);
|
|
|
|
int argc = 1;
|
|
|
|
while (va_arg(ap, const char*))
|
|
|
|
argc++;
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
char** argv = (char**)malloc(sizeof(char*) * (argc + 1));
|
|
|
|
if (argv == nullptr)
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(ap, arg0);
|
|
|
|
argv[0] = (char*)arg0;
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
argv[i] = va_arg(ap, char*);
|
|
|
|
argv[argc] = nullptr;
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return execv(pathname, argv);
|
|
|
|
}
|
|
|
|
|
2023-06-05 21:53:37 +03:00
|
|
|
int execle(const char* pathname, const char* arg0, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
int argc = 0;
|
|
|
|
|
|
|
|
if (arg0)
|
|
|
|
{
|
|
|
|
va_start(ap, arg0);
|
|
|
|
argc = 1;
|
|
|
|
while (va_arg(ap, const char*))
|
|
|
|
argc++;
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
char** argv = (char**)malloc(sizeof(char*) * (argc + 1));
|
|
|
|
if (argv == nullptr)
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char** envp = nullptr;
|
|
|
|
|
|
|
|
va_start(ap, arg0);
|
|
|
|
argv[0] = (char*)arg0;
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
argv[i] = va_arg(ap, char*);
|
|
|
|
argv[argc] = nullptr;
|
|
|
|
envp = va_arg(ap, char**);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return execve(pathname, argv, envp);
|
|
|
|
}
|
|
|
|
|
2023-05-31 20:57:33 +03:00
|
|
|
int execv(const char* pathname, char* const argv[])
|
|
|
|
{
|
2023-06-05 21:53:37 +03:00
|
|
|
return execve(pathname, argv, environ);
|
|
|
|
}
|
|
|
|
|
|
|
|
int execve(const char* pathname, char* const argv[], char* const envp[])
|
|
|
|
{
|
|
|
|
return syscall(SYS_EXEC, pathname, argv, envp);
|
2023-05-31 20:57:33 +03:00
|
|
|
}
|
|
|
|
|
2023-04-05 23:58:40 +03:00
|
|
|
pid_t fork(void)
|
|
|
|
{
|
2023-05-28 18:08:26 +03:00
|
|
|
return syscall(SYS_FORK);
|
2023-04-05 23:58:40 +03:00
|
|
|
}
|
2023-05-28 22:34:48 +03:00
|
|
|
|
|
|
|
unsigned int sleep(unsigned int seconds)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SLEEP, seconds);
|
2023-05-29 20:21:19 +03:00
|
|
|
}
|
2023-06-11 22:15:35 +03:00
|
|
|
|
2023-06-11 22:27:53 +03:00
|
|
|
uid_t getuid(void)
|
|
|
|
{
|
|
|
|
return syscall(SYS_GET_UID);
|
|
|
|
}
|
|
|
|
|
|
|
|
gid_t getgid(void)
|
|
|
|
{
|
|
|
|
return syscall(SYS_GET_GID);
|
|
|
|
}
|
|
|
|
|
|
|
|
uid_t geteuid(void)
|
|
|
|
{
|
|
|
|
return syscall(SYS_GET_EUID);
|
|
|
|
}
|
|
|
|
|
|
|
|
gid_t getegid(void)
|
|
|
|
{
|
|
|
|
return syscall(SYS_GET_EGID);
|
|
|
|
}
|
|
|
|
|
2023-06-11 22:15:35 +03:00
|
|
|
int seteuid(uid_t uid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_EUID, uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int setegid(gid_t gid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_EGID, gid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int setuid(uid_t uid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_UID, uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int setgid(gid_t gid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_GID, gid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int setreuid(uid_t ruid, uid_t euid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_REUID, ruid, euid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int setregid(gid_t rgid, gid_t egid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_REGID, rgid, egid);
|
|
|
|
}
|