2023-03-30 19:28:39 +00:00
|
|
|
#include <luna/Vector.h>
|
|
|
|
|
2023-01-22 14:00:20 +00:00
|
|
|
#include <bits/errno-return.h>
|
2023-03-24 20:33:20 +00:00
|
|
|
#include <fcntl.h>
|
2023-01-06 12:31:14 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdint.h>
|
2023-01-22 14:00:20 +00:00
|
|
|
#include <sys/syscall.h>
|
2023-01-06 12:31:14 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
extern "C" long arch_invoke_syscall(long, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
2023-03-18 22:58:56 +00:00
|
|
|
pid_t fork(void)
|
|
|
|
{
|
|
|
|
long rc = syscall(SYS_fork);
|
|
|
|
__errno_return(rc, int);
|
|
|
|
}
|
|
|
|
|
2023-03-11 21:19:58 +00:00
|
|
|
pid_t getpid(void)
|
|
|
|
{
|
|
|
|
return (pid_t)syscall(SYS_getpid);
|
|
|
|
}
|
|
|
|
|
2023-03-24 16:39:55 +00:00
|
|
|
pid_t getppid(void)
|
|
|
|
{
|
|
|
|
return (pid_t)syscall(SYS_getppid);
|
|
|
|
}
|
|
|
|
|
2023-03-18 21:31:16 +00:00
|
|
|
int execv(const char* path, char* const* argv)
|
|
|
|
{
|
|
|
|
long rc = syscall(SYS_exec, path, argv);
|
|
|
|
__errno_return(rc, int);
|
|
|
|
}
|
|
|
|
|
2023-03-30 19:28:39 +00:00
|
|
|
int execl(const char* path, const char* arg, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, arg);
|
|
|
|
|
|
|
|
Vector<char*> args;
|
|
|
|
if (args.try_append(const_cast<char*>(arg)).has_error()) return -1;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
char* str = va_arg(ap, char*);
|
|
|
|
if (args.try_append(str).has_error()) return -1;
|
|
|
|
if (!str) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return execv(path, args.data());
|
|
|
|
}
|
|
|
|
|
2023-01-06 12:31:14 +00:00
|
|
|
long syscall(long num, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, num);
|
|
|
|
|
|
|
|
uintptr_t arg0 = va_arg(ap, uintptr_t);
|
|
|
|
uintptr_t arg1 = va_arg(ap, uintptr_t);
|
|
|
|
uintptr_t arg2 = va_arg(ap, uintptr_t);
|
|
|
|
uintptr_t arg3 = va_arg(ap, uintptr_t);
|
|
|
|
uintptr_t arg4 = va_arg(ap, uintptr_t);
|
|
|
|
|
|
|
|
long rc = arch_invoke_syscall(num, arg0, arg1, arg2, arg3, arg4);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2023-01-22 14:00:20 +00:00
|
|
|
|
|
|
|
int usleep(useconds_t us)
|
|
|
|
{
|
|
|
|
long rc = syscall(SYS_usleep, us);
|
|
|
|
__errno_return(rc, int);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long sleep(unsigned long seconds)
|
|
|
|
{
|
|
|
|
syscall(SYS_usleep, seconds * 1000000);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-03-11 16:45:20 +00:00
|
|
|
|
|
|
|
int close(int fd)
|
|
|
|
{
|
|
|
|
long rc = syscall(SYS_close, fd);
|
|
|
|
__errno_return(rc, int);
|
|
|
|
}
|
2023-03-11 17:02:50 +00:00
|
|
|
|
|
|
|
ssize_t read(int fd, void* buf, size_t size)
|
|
|
|
{
|
|
|
|
long rc = syscall(SYS_read, fd, buf, size);
|
|
|
|
__errno_return(rc, ssize_t);
|
|
|
|
}
|
2023-03-12 10:37:41 +00:00
|
|
|
|
|
|
|
ssize_t write(int fd, const void* buf, size_t size)
|
|
|
|
{
|
|
|
|
long rc = syscall(SYS_write, fd, buf, size);
|
|
|
|
__errno_return(rc, ssize_t);
|
|
|
|
}
|
2023-03-12 12:15:24 +00:00
|
|
|
|
|
|
|
off_t lseek(int fd, off_t offset, int whence)
|
|
|
|
{
|
|
|
|
long rc = syscall(SYS_lseek, fd, offset, whence);
|
|
|
|
__errno_return(rc, off_t);
|
|
|
|
}
|
2023-03-24 20:33:20 +00:00
|
|
|
|
|
|
|
int dup(int fd)
|
|
|
|
{
|
|
|
|
return fcntl(fd, F_DUPFD, 0);
|
|
|
|
}
|
2023-01-06 12:31:14 +00:00
|
|
|
}
|