Files
Astral/kernel-src/sys/syscalls/ioctl.c
T
2025-12-25 20:20:34 -03:00

35 lines
805 B
C

#include <kernel/syscalls.h>
#include <kernel/file.h>
#include <kernel/vfs.h>
#include <arch/cpu.h>
syscallret_t syscall_ioctl(context_t *, int fd, unsigned long request, void *arg) {
syscallret_t ret = {
.ret = -1
};
// arg could be null for, say, an ioctl that does not pass an argument
// this is safe as any accesses on ioctls are done through user copy, which
// returns EFAULT on a null access
if (arg != NULL && IS_USER_ADDRESS(arg) == false) {
ret.errno = EFAULT;
return ret;
}
file_t *file = fd_get(fd);
if (file == NULL) {
ret.errno = EBADF;
return ret;
}
int r = 0;
VOP_LOCK(file->vnode);
ret.errno = VOP_IOCTL(file->vnode, request, arg, &r, &current_thread()->proc->cred);
VOP_UNLOCK(file->vnode);
ret.ret = ret.errno ? -1 : r;
fd_release(file);
return ret;
}