2021-05-31 04:47:02 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/sysfunc.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <syscall_nums.h>
|
|
|
|
#include <kernel/printf.h>
|
2018-03-19 05:38:11 +03:00
|
|
|
#include <kernel/process.h>
|
2021-05-31 04:47:02 +03:00
|
|
|
#include <kernel/string.h>
|
2018-03-19 05:38:11 +03:00
|
|
|
#include <kernel/version.h>
|
2021-05-31 04:47:02 +03:00
|
|
|
#include <kernel/pipe.h>
|
2018-03-19 05:38:11 +03:00
|
|
|
#include <kernel/shm.h>
|
2021-05-31 04:47:02 +03:00
|
|
|
#include <kernel/mmu.h>
|
|
|
|
#include <kernel/pty.h>
|
|
|
|
#include <kernel/spinlock.h>
|
|
|
|
#include <kernel/signal.h>
|
|
|
|
#include <kernel/time.h>
|
|
|
|
#include <kernel/syscall.h>
|
|
|
|
#include <kernel/misc.h>
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
static char hostname[256];
|
|
|
|
static size_t hostname_len = 0;
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
void ptr_validate(void * ptr, const char * syscall) {
|
2018-03-16 15:56:19 +03:00
|
|
|
if (ptr && !PTR_INRANGE(ptr)) {
|
2021-05-31 04:47:02 +03:00
|
|
|
printf("invalid pointer passed to %s (%p < %p)\n",
|
|
|
|
syscall, ptr, (void*)this_core->current_process->image.entry);
|
|
|
|
while (1) {}
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_sbrk(ssize_t size) {
|
|
|
|
if (size & 0xFFF) return -EINVAL;
|
|
|
|
volatile process_t * volatile proc = this_core->current_process;
|
|
|
|
if (proc->group != 0) {
|
|
|
|
proc = process_from_pid(proc->group);
|
|
|
|
}
|
|
|
|
spin_lock(proc->image.lock);
|
|
|
|
uintptr_t out = proc->image.heap;
|
|
|
|
for (uintptr_t i = out; i < out + size; i += 0x1000) {
|
|
|
|
union PML * page = mmu_get_page(i, MMU_GET_MAKE);
|
|
|
|
if (page->bits.page != 0) {
|
|
|
|
printf("odd, %#zx is already allocated?\n", i);
|
|
|
|
}
|
|
|
|
mmu_frame_allocate(page, MMU_FLAG_WRITABLE);
|
|
|
|
mmu_invalidate(i);
|
|
|
|
}
|
|
|
|
proc->image.heap += size;
|
|
|
|
spin_unlock(proc->image.lock);
|
|
|
|
return (long)out;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-06-03 15:01:57 +03:00
|
|
|
extern int elf_module(const char * path);
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_sysfunc(long fn, char ** args) {
|
|
|
|
/* FIXME: Most of these should be top-level, many are hacks/broken in Misaka */
|
|
|
|
switch (fn) {
|
|
|
|
case TOARU_SYS_FUNC_SYNC:
|
2021-06-04 14:38:27 +03:00
|
|
|
/* FIXME: There is no sync ability in the VFS at the moment.
|
|
|
|
* XXX: Should this just be an ioctl on individual devices?
|
|
|
|
* Or possibly even an ioctl we can send to arbitrary files? */
|
2021-05-31 04:47:02 +03:00
|
|
|
printf("sync: not implemented\n");
|
|
|
|
return -EINVAL;
|
2021-06-04 14:38:27 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
case TOARU_SYS_FUNC_LOGHERE:
|
2021-06-04 14:38:27 +03:00
|
|
|
/* FIXME: The entire kernel logging system needs to be revamped as
|
|
|
|
* Misaka switched everything to raw printfs, and then also
|
|
|
|
* removed most of them for cleanliness... first task would
|
|
|
|
* be to reintroduce kernel fprintf() to printf to fs_nodes. */
|
2021-05-31 04:47:02 +03:00
|
|
|
printf("loghere: not implemented\n");
|
|
|
|
return -EINVAL;
|
2021-06-04 14:38:27 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
case TOARU_SYS_FUNC_KDEBUG:
|
2021-06-04 14:38:27 +03:00
|
|
|
/* FIXME: The kernel debugger is completely deprecated and fully removed
|
|
|
|
* in Misaka, and I'm not sure I want to add it back... */
|
2021-05-31 04:47:02 +03:00
|
|
|
printf("kdebug: not implemented\n");
|
|
|
|
return -EINVAL;
|
2021-06-04 14:38:27 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
case TOARU_SYS_FUNC_INSMOD:
|
2021-06-04 14:38:27 +03:00
|
|
|
/* Linux has init_module as a system call? */
|
2021-06-03 15:01:57 +03:00
|
|
|
return elf_module(args[0]);
|
2021-06-04 14:38:27 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
case TOARU_SYS_FUNC_SETHEAP: {
|
2021-06-04 14:38:27 +03:00
|
|
|
/* I'm not really sure how this should be done...
|
|
|
|
* traditional brk() would be expected to map everything in-between,
|
|
|
|
* but we use this to move the heap in ld.so, and we don't want
|
|
|
|
* the stuff in the middle to be mapped necessarily... */
|
2021-05-31 04:47:02 +03:00
|
|
|
volatile process_t * volatile proc = this_core->current_process;
|
|
|
|
if (proc->group != 0) proc = process_from_pid(proc->group);
|
|
|
|
spin_lock(proc->image.lock);
|
|
|
|
proc->image.heap = (uintptr_t)args[0];
|
|
|
|
spin_unlock(proc->image.lock);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-04 14:38:27 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
case TOARU_SYS_FUNC_MMAP: {
|
2021-06-04 14:38:27 +03:00
|
|
|
/* FIXME: This whole thing should be removed; we need a proper mmap interface,
|
|
|
|
* preferrably with all of the file mapping options, too. And it should
|
|
|
|
* probably also interact with the SHM subsystem... */
|
2021-05-31 04:47:02 +03:00
|
|
|
volatile process_t * volatile proc = this_core->current_process;
|
|
|
|
if (proc->group != 0) proc = process_from_pid(proc->group);
|
|
|
|
spin_lock(proc->image.lock);
|
|
|
|
/* Align inputs */
|
|
|
|
uintptr_t start = ((uintptr_t)args[0]) & 0xFFFFffffFFFFf000UL;
|
|
|
|
uintptr_t end = ((uintptr_t)args[0] + (size_t)args[1] + 0xFFF) & 0xFFFFffffFFFFf000UL;
|
|
|
|
for (uintptr_t i = start; i < end; i += 0x1000) {
|
|
|
|
union PML * page = mmu_get_page(i, MMU_GET_MAKE);
|
|
|
|
mmu_frame_allocate(page, MMU_FLAG_WRITABLE);
|
|
|
|
mmu_invalidate(i);
|
|
|
|
}
|
|
|
|
spin_unlock(proc->image.lock);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-04 14:38:27 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
case TOARU_SYS_FUNC_THREADNAME: {
|
|
|
|
/* This should probably be moved to a new system call. */
|
|
|
|
int count = 0;
|
|
|
|
char **arg = args;
|
|
|
|
PTR_VALIDATE(args);
|
|
|
|
while (*arg) {
|
|
|
|
PTR_VALIDATE(*args);
|
|
|
|
count++;
|
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
this_core->current_process->cmdline = malloc(sizeof(char*)*(count+1));
|
|
|
|
int i = 0;
|
|
|
|
while (i < count) {
|
|
|
|
this_core->current_process->cmdline[i] = strdup(args[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
this_core->current_process->cmdline[i] = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-04 14:38:27 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
case TOARU_SYS_FUNC_SETGSBASE:
|
2021-06-04 14:38:27 +03:00
|
|
|
/* This should be a new system call; see what Linux, et al., call it. */
|
2021-05-31 04:47:02 +03:00
|
|
|
PTR_VALIDATE(args);
|
|
|
|
this_core->current_process->thread.context.tls_base = (uintptr_t)args[0];
|
|
|
|
arch_set_tls_base(this_core->current_process->thread.context.tls_base);
|
|
|
|
return 0;
|
2021-06-01 13:36:46 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
default:
|
|
|
|
printf("Bad system function: %ld\n", fn);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
__attribute__((noreturn))
|
|
|
|
static long sys_exit(long exitcode) {
|
|
|
|
/* TODO remove print */
|
|
|
|
//printf("(process %d [%s] exited with %ld)\n", current_process->id, current_process->name, exitcode);
|
|
|
|
|
|
|
|
task_exit((exitcode & 0xFF) << 8);
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
static long sys_write(int fd, char * ptr, unsigned long len) {
|
2018-03-16 15:56:19 +03:00
|
|
|
if (FD_CHECK(fd)) {
|
|
|
|
PTR_VALIDATE(ptr);
|
|
|
|
fs_node_t * node = FD_ENTRY(fd);
|
2021-05-31 04:47:02 +03:00
|
|
|
if (!(FD_MODE(fd) & 2)) return -EACCES;
|
|
|
|
int64_t out = write_fs(node, FD_OFFSET(fd), len, (uint8_t*)ptr);
|
|
|
|
if (out > 0) {
|
|
|
|
FD_OFFSET(fd) += out;
|
2018-10-29 14:57:17 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
return out;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2018-07-18 04:45:42 +03:00
|
|
|
return -EBADF;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long stat_node(fs_node_t * fn, uintptr_t st) {
|
|
|
|
struct stat * f = (struct stat *)st;
|
|
|
|
|
|
|
|
PTR_VALIDATE(f);
|
|
|
|
|
|
|
|
if (!fn) {
|
|
|
|
/* XXX: Does this need to zero the stat struct when returning -ENOENT? */
|
|
|
|
memset(f, 0x00, sizeof(struct stat));
|
|
|
|
return -ENOENT;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
|
|
|
|
f->st_dev = (uint16_t)(((uint64_t)fn->device & 0xFFFF0) >> 8);
|
|
|
|
f->st_ino = fn->inode;
|
|
|
|
|
|
|
|
uint32_t flags = 0;
|
|
|
|
if (fn->flags & FS_FILE) { flags |= _IFREG; }
|
|
|
|
if (fn->flags & FS_DIRECTORY) { flags |= _IFDIR; }
|
|
|
|
if (fn->flags & FS_CHARDEVICE) { flags |= _IFCHR; }
|
|
|
|
if (fn->flags & FS_BLOCKDEVICE) { flags |= _IFBLK; }
|
|
|
|
if (fn->flags & FS_PIPE) { flags |= _IFIFO; }
|
|
|
|
if (fn->flags & FS_SYMLINK) { flags |= _IFLNK; }
|
|
|
|
|
|
|
|
f->st_mode = fn->mask | flags;
|
|
|
|
f->st_nlink = fn->nlink;
|
|
|
|
f->st_uid = fn->uid;
|
|
|
|
f->st_gid = fn->gid;
|
|
|
|
f->st_rdev = 0;
|
|
|
|
f->st_size = fn->length;
|
|
|
|
|
|
|
|
f->st_atime = fn->atime;
|
|
|
|
f->st_mtime = fn->mtime;
|
|
|
|
f->st_ctime = fn->ctime;
|
|
|
|
f->st_blksize = 512; /* whatever */
|
|
|
|
|
|
|
|
if (fn->get_size) {
|
|
|
|
f->st_size = fn->get_size(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_stat(int fd, uintptr_t st) {
|
|
|
|
PTR_VALIDATE(st);
|
2018-03-16 15:56:19 +03:00
|
|
|
if (FD_CHECK(fd)) {
|
2021-05-31 04:47:02 +03:00
|
|
|
return stat_node(FD_ENTRY(fd), st);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2018-07-18 04:45:42 +03:00
|
|
|
return -EBADF;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_statf(char * file, uintptr_t st) {
|
|
|
|
int result;
|
|
|
|
PTR_VALIDATE(file);
|
|
|
|
PTR_VALIDATE(st);
|
|
|
|
fs_node_t * fn = kopen(file, 0);
|
|
|
|
result = stat_node(fn, st);
|
|
|
|
if (fn) {
|
|
|
|
close_fs(fn);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
return result;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_symlink(char * target, char * name) {
|
|
|
|
PTR_VALIDATE(target);
|
|
|
|
PTR_VALIDATE(name);
|
|
|
|
return symlink_fs(target, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static long sys_readlink(const char * file, char * ptr, long len) {
|
|
|
|
PTR_VALIDATE(file);
|
|
|
|
fs_node_t * node = kopen((char *) file, O_PATH | O_NOFOLLOW);
|
|
|
|
if (!node) {
|
|
|
|
return -ENOENT;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
long rv = readlink_fs(node, ptr, len);
|
|
|
|
close_fs(node);
|
|
|
|
return rv;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_lstat(char * file, uintptr_t st) {
|
|
|
|
PTR_VALIDATE(file);
|
|
|
|
PTR_VALIDATE(st);
|
|
|
|
fs_node_t * fn = kopen(file, O_PATH | O_NOFOLLOW);
|
|
|
|
long result = stat_node(fn, st);
|
|
|
|
if (fn) {
|
|
|
|
close_fs(fn);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long sys_open(const char * file, long flags, long mode) {
|
2018-03-16 15:56:19 +03:00
|
|
|
PTR_VALIDATE(file);
|
|
|
|
fs_node_t * node = kopen((char *)file, flags);
|
|
|
|
|
2018-10-29 14:57:17 +03:00
|
|
|
int access_bits = 0;
|
|
|
|
|
2018-11-22 10:50:21 +03:00
|
|
|
if (node && (flags & O_CREAT) && (flags & O_EXCL)) {
|
|
|
|
close_fs(node);
|
|
|
|
return -EEXIST;
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:57:17 +03:00
|
|
|
if (!(flags & O_WRONLY) || (flags & O_RDWR)) {
|
|
|
|
if (node && !has_permission(node, 04)) {
|
|
|
|
close_fs(node);
|
|
|
|
return -EACCES;
|
|
|
|
} else {
|
|
|
|
access_bits |= 01;
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2018-11-22 10:27:20 +03:00
|
|
|
|
|
|
|
if ((flags & O_RDWR) || (flags & O_WRONLY)) {
|
2018-10-29 14:57:17 +03:00
|
|
|
if (node && !has_permission(node, 02)) {
|
2018-07-18 04:45:42 +03:00
|
|
|
close_fs(node);
|
2018-03-16 15:56:19 +03:00
|
|
|
return -EACCES;
|
|
|
|
}
|
2018-11-22 10:11:05 +03:00
|
|
|
if (node && (node->flags & FS_DIRECTORY)) {
|
|
|
|
return -EISDIR;
|
|
|
|
}
|
2018-11-22 10:27:20 +03:00
|
|
|
if ((flags & O_RDWR) || (flags & O_WRONLY)) {
|
|
|
|
/* truncate doesn't grant write permissions */
|
|
|
|
access_bits |= 02;
|
2018-11-21 15:54:31 +03:00
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!node && (flags & O_CREAT)) {
|
|
|
|
/* TODO check directory permissions */
|
|
|
|
int result = create_file_fs((char *)file, mode);
|
|
|
|
if (!result) {
|
|
|
|
node = kopen((char *)file, flags);
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2018-11-22 10:27:20 +03:00
|
|
|
|
2018-11-28 08:51:35 +03:00
|
|
|
if (node && (flags & O_DIRECTORY)) {
|
|
|
|
if (!(node->flags & FS_DIRECTORY)) {
|
|
|
|
return -ENOTDIR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-22 10:27:20 +03:00
|
|
|
if (node && (flags & O_TRUNC)) {
|
|
|
|
if (!(access_bits & 02)) {
|
|
|
|
close_fs(node);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
truncate_fs(node);
|
|
|
|
}
|
|
|
|
|
2018-03-16 15:56:19 +03:00
|
|
|
if (!node) {
|
2018-07-18 04:45:42 +03:00
|
|
|
return -ENOENT;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2018-11-22 10:11:05 +03:00
|
|
|
if (node && (flags & O_CREAT) && (node->flags & FS_DIRECTORY)) {
|
2018-11-22 10:27:20 +03:00
|
|
|
close_fs(node);
|
2018-11-22 10:11:05 +03:00
|
|
|
return -EISDIR;
|
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
int fd = process_append_fd((process_t *)this_core->current_process, node);
|
2018-10-29 14:57:17 +03:00
|
|
|
FD_MODE(fd) = access_bits;
|
2018-07-17 10:52:00 +03:00
|
|
|
if (flags & O_APPEND) {
|
2018-10-29 14:57:17 +03:00
|
|
|
FD_OFFSET(fd) = node->length;
|
2018-07-17 10:52:00 +03:00
|
|
|
} else {
|
2018-10-29 14:57:17 +03:00
|
|
|
FD_OFFSET(fd) = 0;
|
2018-07-17 10:52:00 +03:00
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_close(int fd) {
|
2018-03-16 15:56:19 +03:00
|
|
|
if (FD_CHECK(fd)) {
|
|
|
|
close_fs(FD_ENTRY(fd));
|
|
|
|
FD_ENTRY(fd) = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-07-18 04:45:42 +03:00
|
|
|
return -EBADF;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_seek(int fd, long offset, long whence) {
|
2018-03-16 15:56:19 +03:00
|
|
|
if (FD_CHECK(fd)) {
|
2019-12-15 11:01:18 +03:00
|
|
|
if ((FD_ENTRY(fd)->flags & FS_PIPE) || (FD_ENTRY(fd)->flags & FS_CHARDEVICE)) return -ESPIPE;
|
2018-03-16 15:56:19 +03:00
|
|
|
switch (whence) {
|
|
|
|
case 0:
|
2018-10-29 14:57:17 +03:00
|
|
|
FD_OFFSET(fd) = offset;
|
2018-03-16 15:56:19 +03:00
|
|
|
break;
|
|
|
|
case 1:
|
2018-10-29 14:57:17 +03:00
|
|
|
FD_OFFSET(fd) += offset;
|
2018-03-16 15:56:19 +03:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-10-29 14:57:17 +03:00
|
|
|
FD_OFFSET(fd) = FD_ENTRY(fd)->length + offset;
|
2018-03-16 15:56:19 +03:00
|
|
|
break;
|
2019-12-15 11:08:21 +03:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2018-10-29 14:57:17 +03:00
|
|
|
return FD_OFFSET(fd);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2018-07-18 04:45:42 +03:00
|
|
|
return -EBADF;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_read(int fd, char * ptr, unsigned long len) {
|
|
|
|
if (FD_CHECK(fd)) {
|
|
|
|
PTR_VALIDATE(ptr);
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
fs_node_t * node = FD_ENTRY(fd);
|
|
|
|
if (!(FD_MODE(fd) & 01)) {
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
uint64_t out = read_fs(node, FD_OFFSET(fd), len, (uint8_t *)ptr);
|
|
|
|
FD_OFFSET(fd) += out;
|
|
|
|
return out;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
return -EBADF;
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_ioctl(int fd, int request, void * argp) {
|
|
|
|
if (FD_CHECK(fd)) {
|
|
|
|
PTR_VALIDATE(argp);
|
|
|
|
return ioctl_fs(FD_ENTRY(fd), request, argp);
|
|
|
|
}
|
|
|
|
return -EBADF;
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_readdir(int fd, long index, struct dirent * entry) {
|
|
|
|
if (FD_CHECK(fd)) {
|
|
|
|
PTR_VALIDATE(entry);
|
|
|
|
struct dirent * kentry = readdir_fs(FD_ENTRY(fd), (uint64_t)index);
|
|
|
|
if (kentry) {
|
|
|
|
memcpy(entry, kentry, sizeof *entry);
|
|
|
|
free(kentry);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
return -EBADF;
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_mkdir(char * path, uint64_t mode) {
|
|
|
|
return mkdir_fs(path, mode);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_access(const char * file, long flags) {
|
2018-03-16 15:56:19 +03:00
|
|
|
PTR_VALIDATE(file);
|
2021-05-31 04:47:02 +03:00
|
|
|
fs_node_t * node = kopen((char *)file, 0);
|
|
|
|
if (!node) return -ENOENT;
|
|
|
|
close_fs(node);
|
|
|
|
return 0;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_chmod(char * file, long mode) {
|
2018-03-16 15:56:19 +03:00
|
|
|
PTR_VALIDATE(file);
|
|
|
|
fs_node_t * fn = kopen(file, 0);
|
|
|
|
if (fn) {
|
2018-07-18 04:45:42 +03:00
|
|
|
/* Can group members change bits? I think it's only owners. */
|
2021-05-31 04:47:02 +03:00
|
|
|
if (this_core->current_process->user != 0 && this_core->current_process->user != fn->uid) {
|
2018-07-18 04:45:42 +03:00
|
|
|
close_fs(fn);
|
2018-07-18 09:35:52 +03:00
|
|
|
return -EACCES;
|
2018-07-18 04:45:42 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
long result = chmod_fs(fn, mode);
|
2018-03-16 15:56:19 +03:00
|
|
|
close_fs(fn);
|
|
|
|
return result;
|
|
|
|
} else {
|
2018-07-18 04:45:42 +03:00
|
|
|
return -ENOENT;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_chown(char * file, uid_t uid, uid_t gid) {
|
2018-03-16 15:56:19 +03:00
|
|
|
PTR_VALIDATE(file);
|
|
|
|
fs_node_t * fn = kopen(file, 0);
|
|
|
|
if (fn) {
|
2018-07-18 04:45:42 +03:00
|
|
|
/* TODO: Owners can change groups... */
|
2021-05-31 04:47:02 +03:00
|
|
|
if (this_core->current_process->user != 0) {
|
2018-07-18 04:45:42 +03:00
|
|
|
close_fs(fn);
|
2018-07-18 09:35:52 +03:00
|
|
|
return -EACCES;
|
2018-07-18 04:45:42 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
long result = chown_fs(fn, uid, gid);
|
2018-03-16 15:56:19 +03:00
|
|
|
close_fs(fn);
|
|
|
|
return result;
|
|
|
|
} else {
|
2018-07-18 04:45:42 +03:00
|
|
|
return -ENOENT;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_gettimeofday(struct timeval * tv, void * tz) {
|
|
|
|
PTR_VALIDATE(tv);
|
|
|
|
PTR_VALIDATE(tz);
|
|
|
|
return gettimeofday(tv, tz);
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_getuid(void) {
|
|
|
|
return (long)this_core->current_process->real_user;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long sys_geteuid(void) {
|
|
|
|
return (long)this_core->current_process->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long sys_setuid(uid_t new_uid) {
|
|
|
|
if (this_core->current_process->user == USER_ROOT_UID) {
|
|
|
|
this_core->current_process->user = new_uid;
|
|
|
|
this_core->current_process->real_user = new_uid;
|
|
|
|
return 0;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
return -EPERM;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_getpid(void) {
|
|
|
|
/* The user actually wants the pid of the originating thread (which can be us). */
|
|
|
|
return this_core->current_process->group ? (long)this_core->current_process->group : (long)this_core->current_process->id;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_gettid(void) {
|
|
|
|
return (long)this_core->current_process->id;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_setsid(void) {
|
|
|
|
if (this_core->current_process->job == this_core->current_process->group) {
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
this_core->current_process->session = this_core->current_process->group;
|
|
|
|
this_core->current_process->job = this_core->current_process->group;
|
|
|
|
return this_core->current_process->session;
|
2018-10-31 04:28:41 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_setpgid(pid_t pid, pid_t pgid) {
|
|
|
|
if (pgid < 0) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
process_t * proc = NULL;
|
|
|
|
if (pid == 0) {
|
|
|
|
proc = (process_t*)this_core->current_process;
|
|
|
|
} else {
|
|
|
|
proc = process_from_pid(pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!proc) {
|
|
|
|
return -ESRCH;
|
|
|
|
}
|
|
|
|
if (proc->session != this_core->current_process->session || proc->session == proc->group) {
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pgid == 0) {
|
|
|
|
proc->job = proc->group;
|
|
|
|
} else {
|
|
|
|
process_t * pgroup = process_from_pid(pgid);
|
|
|
|
|
|
|
|
if (!pgroup || pgroup->session != proc->session) {
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
proc->job = pgid;
|
|
|
|
}
|
|
|
|
return 0;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_getpgid(pid_t pid) {
|
|
|
|
process_t * proc;
|
|
|
|
if (pid == 0) {
|
|
|
|
proc = (process_t*)this_core->current_process;
|
|
|
|
} else {
|
|
|
|
proc = NULL; process_from_pid(pid);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
|
|
|
|
if (!proc) {
|
|
|
|
return -ESRCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
return proc->job;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_uname(struct utsname * name) {
|
2018-03-16 15:56:19 +03:00
|
|
|
PTR_VALIDATE(name);
|
|
|
|
char version_number[256];
|
2021-05-31 04:47:02 +03:00
|
|
|
snprintf(version_number, 255, __kernel_version_format,
|
2018-03-16 15:56:19 +03:00
|
|
|
__kernel_version_major,
|
|
|
|
__kernel_version_minor,
|
|
|
|
__kernel_version_lower,
|
|
|
|
__kernel_version_suffix);
|
|
|
|
char version_string[256];
|
2021-05-31 04:47:02 +03:00
|
|
|
snprintf(version_string, 255, "%s %s %s",
|
2018-03-16 15:56:19 +03:00
|
|
|
__kernel_version_codename,
|
|
|
|
__kernel_build_date,
|
|
|
|
__kernel_build_time);
|
|
|
|
strcpy(name->sysname, __kernel_name);
|
|
|
|
strcpy(name->nodename, hostname);
|
|
|
|
strcpy(name->release, version_number);
|
|
|
|
strcpy(name->version, version_string);
|
|
|
|
strcpy(name->machine, __kernel_arch);
|
2021-05-31 04:47:02 +03:00
|
|
|
strcpy(name->domainname, ""); /* TODO */
|
2018-03-16 15:56:19 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_chdir(char * newdir) {
|
2018-03-16 15:56:19 +03:00
|
|
|
PTR_VALIDATE(newdir);
|
2021-05-31 04:47:02 +03:00
|
|
|
char * path = canonicalize_path(this_core->current_process->wd_name, newdir);
|
2018-03-16 15:56:19 +03:00
|
|
|
fs_node_t * chd = kopen(path, 0);
|
|
|
|
if (chd) {
|
|
|
|
if ((chd->flags & FS_DIRECTORY) == 0) {
|
|
|
|
close_fs(chd);
|
2018-07-18 04:45:42 +03:00
|
|
|
return -ENOTDIR;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2018-11-01 07:46:59 +03:00
|
|
|
if (!has_permission(chd, 01)) {
|
|
|
|
close_fs(chd);
|
|
|
|
return -EACCES;
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
close_fs(chd);
|
2021-05-31 04:47:02 +03:00
|
|
|
free(this_core->current_process->wd_name);
|
|
|
|
this_core->current_process->wd_name = malloc(strlen(path) + 1);
|
|
|
|
memcpy(this_core->current_process->wd_name, path, strlen(path) + 1);
|
2018-03-16 15:56:19 +03:00
|
|
|
return 0;
|
|
|
|
} else {
|
2018-07-18 04:45:42 +03:00
|
|
|
return -ENOENT;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_getcwd(char * buf, size_t size) {
|
2018-03-16 15:56:19 +03:00
|
|
|
if (buf) {
|
|
|
|
PTR_VALIDATE(buf);
|
2021-05-31 04:47:02 +03:00
|
|
|
size_t len = strlen(this_core->current_process->wd_name) + 1;
|
|
|
|
return (long)memcpy(buf, this_core->current_process->wd_name, size < len ? size : len);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_dup2(int old, int new) {
|
|
|
|
return process_move_fd((process_t *)this_core->current_process, old, new);
|
|
|
|
}
|
|
|
|
|
|
|
|
static long sys_sethostname(char * new_hostname) {
|
|
|
|
if (this_core->current_process->user == USER_ROOT_UID) {
|
2018-03-16 15:56:19 +03:00
|
|
|
PTR_VALIDATE(new_hostname);
|
|
|
|
size_t len = strlen(new_hostname) + 1;
|
|
|
|
if (len > 256) {
|
2018-07-18 04:45:42 +03:00
|
|
|
return -ENAMETOOLONG;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
hostname_len = len;
|
|
|
|
memcpy(hostname, new_hostname, hostname_len);
|
|
|
|
return 0;
|
|
|
|
} else {
|
2018-07-18 04:45:42 +03:00
|
|
|
return -EPERM;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_gethostname(char * buffer) {
|
|
|
|
PTR_VALIDATE(buffer);
|
|
|
|
memcpy(buffer, hostname, hostname_len);
|
|
|
|
return hostname_len;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_mount(char * arg, char * mountpoint, char * type, unsigned long flags, void * data) {
|
|
|
|
/* TODO: Make use of flags and data from mount command. */
|
|
|
|
(void)flags;
|
|
|
|
(void)data;
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
if (this_core->current_process->user != USER_ROOT_UID) {
|
|
|
|
return -EPERM;
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
if (PTR_INRANGE(arg) && PTR_INRANGE(mountpoint) && PTR_INRANGE(type)) {
|
|
|
|
return vfs_mount_type(type, arg, mountpoint);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
return -EFAULT;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_umask(long mode) {
|
|
|
|
this_core->current_process->mask = mode & 0777;
|
2018-03-16 15:56:19 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_unlink(char * file) {
|
2018-03-16 15:56:19 +03:00
|
|
|
PTR_VALIDATE(file);
|
|
|
|
return unlink_fs(file);
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_execve(const char * filename, char *const argv[], char *const envp[]) {
|
|
|
|
PTR_VALIDATE(filename);
|
|
|
|
PTR_VALIDATE(argv);
|
|
|
|
PTR_VALIDATE(envp);
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
int argc = 0;
|
|
|
|
int envc = 0;
|
|
|
|
while (argv[argc]) {
|
|
|
|
PTR_VALIDATE(argv[argc]);
|
|
|
|
++argc;
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
if (envp) {
|
|
|
|
while (envp[envc]) {
|
|
|
|
PTR_VALIDATE(envp[envc]);
|
|
|
|
++envc;
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
char **argv_ = malloc(sizeof(char*) * (argc + 1));
|
|
|
|
for (int j = 0; j < argc; ++j) {
|
|
|
|
argv_[j] = malloc(strlen(argv[j]) + 1);
|
|
|
|
memcpy(argv_[j], argv[j], strlen(argv[j]) + 1);
|
|
|
|
}
|
|
|
|
argv_[argc] = 0;
|
|
|
|
char ** envp_;
|
|
|
|
if (envp && envc) {
|
|
|
|
envp_ = malloc(sizeof(char*) * (envc + 1));
|
|
|
|
for (int j = 0; j < envc; ++j) {
|
|
|
|
envp_[j] = malloc(strlen(envp[j]) + 1);
|
|
|
|
memcpy(envp_[j], envp[j], strlen(envp[j]) + 1);
|
|
|
|
}
|
|
|
|
envp_[envc] = 0;
|
2018-10-26 17:19:46 +03:00
|
|
|
} else {
|
2021-05-31 04:47:02 +03:00
|
|
|
envp_ = malloc(sizeof(char*));
|
|
|
|
envp_[0] = NULL;
|
2018-10-26 17:19:46 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
|
2021-06-04 15:11:01 +03:00
|
|
|
/**
|
|
|
|
* FIXME: For legacy reasons, we're just going to close everything >2 for now,
|
|
|
|
* but we should really implement proper CLOEXEC semantics...
|
|
|
|
*/
|
|
|
|
for (unsigned int i = 3; i < this_core->current_process->fds->length; ++i) {
|
|
|
|
if (this_core->current_process->fds->entries[i]) {
|
|
|
|
close_fs(this_core->current_process->fds->entries[i]);
|
|
|
|
this_core->current_process->fds->entries[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 15:13:42 +03:00
|
|
|
shm_release_all((process_t *)this_core->current_process);
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
this_core->current_process->cmdline = argv_;
|
|
|
|
return exec(filename, argc, argv_, envp_, 0);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_fork(void) {
|
|
|
|
return fork();
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_clone(uintptr_t new_stack, uintptr_t thread_func, uintptr_t arg) {
|
|
|
|
if (!new_stack || !PTR_INRANGE(new_stack)) return -EINVAL;
|
|
|
|
if (!thread_func || !PTR_INRANGE(thread_func)) return -EINVAL;
|
|
|
|
return (int)clone(new_stack, thread_func, arg);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_waitpid(int pid, int * status, int options) {
|
|
|
|
if (status && !PTR_INRANGE(status)) return -EINVAL;
|
|
|
|
return waitpid(pid, status, options);
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_yield(void) {
|
|
|
|
switch_task(1);
|
|
|
|
return 1;
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_sleepabs(unsigned long seconds, unsigned long subseconds) {
|
|
|
|
/* Mark us as asleep until <some time period> */
|
|
|
|
sleep_until((process_t *)this_core->current_process, seconds, subseconds);
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
/* Switch without adding us to the queue */
|
|
|
|
//printf("process %p (pid=%d) entering sleep until %ld.%06ld\n", current_process, current_process->id, seconds, subseconds);
|
|
|
|
switch_task(0);
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
unsigned long timer_ticks = 0, timer_subticks = 0;
|
|
|
|
relative_time(0,0,&timer_ticks,&timer_subticks);
|
|
|
|
//printf("process %p (pid=%d) resumed from sleep at %ld.%06ld\n", current_process, current_process->id, timer_ticks, timer_subticks);
|
2018-10-29 14:57:17 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
if (seconds > timer_ticks || (seconds == timer_ticks && subseconds >= timer_subticks)) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_sleep(unsigned long seconds, unsigned long subseconds) {
|
|
|
|
unsigned long s, ss;
|
|
|
|
relative_time(seconds, subseconds * 10000, &s, &ss);
|
|
|
|
return sys_sleepabs(s, ss);
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_pipe(int pipes[2]) {
|
2018-03-16 15:56:19 +03:00
|
|
|
if (pipes && !PTR_INRANGE(pipes)) {
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs_node_t * outpipes[2];
|
|
|
|
|
|
|
|
make_unix_pipe(outpipes);
|
|
|
|
|
|
|
|
open_fs(outpipes[0], 0);
|
|
|
|
open_fs(outpipes[1], 0);
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
pipes[0] = process_append_fd((process_t *)this_core->current_process, outpipes[0]);
|
|
|
|
pipes[1] = process_append_fd((process_t *)this_core->current_process, outpipes[1]);
|
2018-10-29 14:57:17 +03:00
|
|
|
FD_MODE(pipes[0]) = 03;
|
|
|
|
FD_MODE(pipes[1]) = 03;
|
2018-03-16 15:56:19 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_signal(long signum, uintptr_t handler) {
|
|
|
|
if (signum > NUMSIGNALS) {
|
|
|
|
return -EINVAL;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
uintptr_t old = this_core->current_process->signals[signum];
|
|
|
|
this_core->current_process->signals[signum] = handler;
|
|
|
|
return old;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_fswait(int c, int fds[]) {
|
2018-03-16 15:56:19 +03:00
|
|
|
PTR_VALIDATE(fds);
|
|
|
|
for (int i = 0; i < c; ++i) {
|
2018-07-18 04:45:42 +03:00
|
|
|
if (!FD_CHECK(fds[i])) return -EBADF;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
fs_node_t ** nodes = malloc(sizeof(fs_node_t *)*(c+1));
|
|
|
|
for (int i = 0; i < c; ++i) {
|
|
|
|
nodes[i] = FD_ENTRY(fds[i]);
|
|
|
|
}
|
|
|
|
nodes[c] = NULL;
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
int result = process_wait_nodes((process_t *)this_core->current_process, nodes, -1);
|
2018-03-16 15:56:19 +03:00
|
|
|
free(nodes);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_fswait_timeout(int c, int fds[], int timeout) {
|
2018-03-16 15:56:19 +03:00
|
|
|
PTR_VALIDATE(fds);
|
|
|
|
for (int i = 0; i < c; ++i) {
|
2018-07-18 04:45:42 +03:00
|
|
|
if (!FD_CHECK(fds[i])) return -EBADF;
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|
|
|
|
fs_node_t ** nodes = malloc(sizeof(fs_node_t *)*(c+1));
|
|
|
|
for (int i = 0; i < c; ++i) {
|
|
|
|
nodes[i] = FD_ENTRY(fds[i]);
|
|
|
|
}
|
|
|
|
nodes[c] = NULL;
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
int result = process_wait_nodes((process_t *)this_core->current_process, nodes, timeout);
|
2018-03-16 15:56:19 +03:00
|
|
|
free(nodes);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_fswait_multi(int c, int fds[], int timeout, int out[]) {
|
2019-12-05 11:42:46 +03:00
|
|
|
PTR_VALIDATE(fds);
|
|
|
|
PTR_VALIDATE(out);
|
|
|
|
int has_match = -1;
|
|
|
|
for (int i = 0; i < c; ++i) {
|
|
|
|
if (!FD_CHECK(fds[i])) {
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
if (selectcheck_fs(FD_ENTRY(fds[i])) == 0) {
|
|
|
|
out[i] = 1;
|
|
|
|
has_match = (has_match == -1) ? i : has_match;
|
|
|
|
} else {
|
|
|
|
out[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Already found a match, return immediately with the first match */
|
|
|
|
if (has_match != -1) return has_match;
|
|
|
|
|
|
|
|
int result = sys_fswait_timeout(c, fds, timeout);
|
2021-05-31 04:47:02 +03:00
|
|
|
if (result >= 0) out[result] = 1;
|
2019-12-05 11:42:46 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_shm_obtain(char * path, size_t * size) {
|
|
|
|
PTR_VALIDATE(path);
|
|
|
|
PTR_VALIDATE(size);
|
|
|
|
return (long)shm_obtain(path, size);
|
2018-10-26 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_shm_release(char * path) {
|
|
|
|
PTR_VALIDATE(path);
|
|
|
|
return shm_release(path);
|
|
|
|
}
|
2018-10-26 14:34:15 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_openpty(int * master, int * slave, char * name, void * _ign0, void * size) {
|
|
|
|
/* We require a place to put these when we are done. */
|
|
|
|
if (!master || !slave) return -EINVAL;
|
|
|
|
if (master && !PTR_INRANGE(master)) return -EINVAL;
|
|
|
|
if (slave && !PTR_INRANGE(slave)) return -EINVAL;
|
|
|
|
if (size && !PTR_INRANGE(size)) return -EINVAL;
|
2018-10-26 17:15:08 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
/* Create a new pseudo terminal */
|
|
|
|
fs_node_t * fs_master;
|
|
|
|
fs_node_t * fs_slave;
|
2018-10-26 17:15:08 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
pty_create(size, &fs_master, &fs_slave);
|
2018-10-26 17:15:08 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
/* Append the master and slave to the calling process */
|
|
|
|
*master = process_append_fd((process_t *)this_core->current_process, fs_master);
|
|
|
|
*slave = process_append_fd((process_t *)this_core->current_process, fs_slave);
|
|
|
|
|
|
|
|
FD_MODE(*master) = 03;
|
|
|
|
FD_MODE(*slave) = 03;
|
|
|
|
|
|
|
|
open_fs(fs_master, 0);
|
|
|
|
open_fs(fs_slave, 0);
|
|
|
|
|
|
|
|
/* Return success */
|
2018-10-26 14:34:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_kill(pid_t process, int signal) {
|
|
|
|
if (process < -1) {
|
|
|
|
return group_send_signal(-process, signal, 0);
|
|
|
|
} else if (process == 0) {
|
|
|
|
return group_send_signal(this_core->current_process->job, signal, 0);
|
2018-10-30 07:28:11 +03:00
|
|
|
} else {
|
2021-05-31 04:47:02 +03:00
|
|
|
return send_signal(process, signal, 0);
|
2018-10-30 07:28:11 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
}
|
2018-10-30 07:28:11 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long sys_reboot(void) {
|
|
|
|
if (this_core->current_process->user != USER_ROOT_UID) {
|
|
|
|
return -EPERM;
|
2018-10-30 07:28:11 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
return arch_reboot();
|
2018-10-30 07:28:11 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
extern long net_socket();
|
|
|
|
extern long net_setsockopt();
|
|
|
|
extern long net_bind();
|
|
|
|
extern long net_accept();
|
|
|
|
extern long net_listen();
|
|
|
|
extern long net_connect();
|
|
|
|
extern long net_getsockopt();
|
|
|
|
extern long net_recv();
|
|
|
|
extern long net_send();
|
|
|
|
extern long net_shutdown();
|
|
|
|
|
|
|
|
static long (*syscalls[])() = {
|
2018-03-16 15:56:19 +03:00
|
|
|
/* System Call Table */
|
|
|
|
[SYS_EXT] = sys_exit,
|
2018-10-31 04:28:41 +03:00
|
|
|
[SYS_GETEUID] = sys_geteuid,
|
2018-03-16 15:56:19 +03:00
|
|
|
[SYS_OPEN] = sys_open,
|
|
|
|
[SYS_READ] = sys_read,
|
|
|
|
[SYS_WRITE] = sys_write,
|
|
|
|
[SYS_CLOSE] = sys_close,
|
|
|
|
[SYS_GETTIMEOFDAY] = sys_gettimeofday,
|
|
|
|
[SYS_GETPID] = sys_getpid,
|
|
|
|
[SYS_SBRK] = sys_sbrk,
|
|
|
|
[SYS_UNAME] = sys_uname,
|
|
|
|
[SYS_SEEK] = sys_seek,
|
|
|
|
[SYS_STAT] = sys_stat,
|
|
|
|
[SYS_GETUID] = sys_getuid,
|
|
|
|
[SYS_SETUID] = sys_setuid,
|
|
|
|
[SYS_READDIR] = sys_readdir,
|
|
|
|
[SYS_CHDIR] = sys_chdir,
|
|
|
|
[SYS_GETCWD] = sys_getcwd,
|
|
|
|
[SYS_SETHOSTNAME] = sys_sethostname,
|
|
|
|
[SYS_GETHOSTNAME] = sys_gethostname,
|
|
|
|
[SYS_MKDIR] = sys_mkdir,
|
|
|
|
[SYS_GETTID] = sys_gettid,
|
|
|
|
[SYS_SYSFUNC] = sys_sysfunc,
|
|
|
|
[SYS_IOCTL] = sys_ioctl,
|
|
|
|
[SYS_ACCESS] = sys_access,
|
|
|
|
[SYS_STATF] = sys_statf,
|
|
|
|
[SYS_CHMOD] = sys_chmod,
|
|
|
|
[SYS_UMASK] = sys_umask,
|
|
|
|
[SYS_UNLINK] = sys_unlink,
|
|
|
|
[SYS_MOUNT] = sys_mount,
|
|
|
|
[SYS_SYMLINK] = sys_symlink,
|
|
|
|
[SYS_READLINK] = sys_readlink,
|
|
|
|
[SYS_LSTAT] = sys_lstat,
|
|
|
|
[SYS_CHOWN] = sys_chown,
|
2018-10-26 14:34:15 +03:00
|
|
|
[SYS_SETSID] = sys_setsid,
|
|
|
|
[SYS_SETPGID] = sys_setpgid,
|
2018-10-30 07:28:11 +03:00
|
|
|
[SYS_GETPGID] = sys_getpgid,
|
2021-05-31 04:47:02 +03:00
|
|
|
[SYS_DUP2] = sys_dup2,
|
|
|
|
[SYS_EXECVE] = sys_execve,
|
|
|
|
[SYS_FORK] = sys_fork,
|
|
|
|
[SYS_WAITPID] = sys_waitpid,
|
|
|
|
[SYS_YIELD] = sys_yield,
|
|
|
|
[SYS_SLEEPABS] = sys_sleepabs,
|
|
|
|
[SYS_SLEEP] = sys_sleep,
|
|
|
|
[SYS_PIPE] = sys_pipe,
|
|
|
|
[SYS_FSWAIT] = sys_fswait,
|
|
|
|
[SYS_FSWAIT2] = sys_fswait_timeout,
|
|
|
|
[SYS_FSWAIT3] = sys_fswait_multi,
|
|
|
|
[SYS_CLONE] = sys_clone,
|
|
|
|
[SYS_OPENPTY] = sys_openpty,
|
|
|
|
[SYS_SHM_OBTAIN] = sys_shm_obtain,
|
|
|
|
[SYS_SHM_RELEASE] = sys_shm_release,
|
|
|
|
[SYS_SIGNAL] = sys_signal,
|
|
|
|
[SYS_KILL] = sys_kill,
|
|
|
|
[SYS_REBOOT] = sys_reboot,
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
[SYS_SOCKET] = net_socket,
|
|
|
|
[SYS_SETSOCKOPT] = net_setsockopt,
|
|
|
|
[SYS_BIND] = net_bind,
|
|
|
|
[SYS_ACCEPT] = net_accept,
|
|
|
|
[SYS_LISTEN] = net_listen,
|
|
|
|
[SYS_CONNECT] = net_connect,
|
|
|
|
[SYS_GETSOCKOPT] = net_getsockopt,
|
|
|
|
[SYS_RECV] = net_recv,
|
|
|
|
[SYS_SEND] = net_send,
|
|
|
|
[SYS_SHUTDOWN] = net_shutdown,
|
|
|
|
};
|
2018-03-16 15:56:19 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
static long num_syscalls = sizeof(syscalls) / sizeof(*syscalls);
|
|
|
|
typedef long (*scall_func)();
|
2018-03-16 15:56:19 +03:00
|
|
|
|
|
|
|
void syscall_handler(struct regs * r) {
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
if (arch_syscall_number(r) >= num_syscalls) {
|
|
|
|
arch_syscall_return(r, -EINVAL);
|
2018-03-16 15:56:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
scall_func func = syscalls[arch_syscall_number(r)];
|
|
|
|
this_core->current_process->syscall_registers = r;
|
|
|
|
arch_syscall_return(r, func(
|
|
|
|
arch_syscall_arg0(r), arch_syscall_arg1(r), arch_syscall_arg2(r),
|
|
|
|
arch_syscall_arg3(r), arch_syscall_arg4(r)));
|
2018-03-16 15:56:19 +03:00
|
|
|
}
|