From: Tulio A M Mendes Date: Sun, 8 Feb 2026 02:46:46 +0000 (-0300) Subject: posix: standardize -errno returns X-Git-Url: https://projects.tadryanom.me/?a=commitdiff_plain;h=7b3453170010007ae76f833f2e93d2d9e3eef8c9;p=AdrOS.git posix: standardize -errno returns Adopt negative errno return convention across waitpid/open/fd_close and TTY read/write, add missing errno constants, and return ENOSYS for unknown syscalls. --- diff --git a/include/errno.h b/include/errno.h index c4de0d5..81a618d 100644 --- a/include/errno.h +++ b/include/errno.h @@ -5,11 +5,13 @@ #define ENOENT 2 #define EIO 5 #define EBADF 9 +#define ECHILD 10 #define EFAULT 14 #define ENOMEM 12 #define EINVAL 22 #define EPIPE 32 #define EMFILE 24 #define ESPIPE 29 +#define ENOSYS 38 #endif diff --git a/src/kernel/scheduler.c b/src/kernel/scheduler.c index db0bea2..40ac6b4 100644 --- a/src/kernel/scheduler.c +++ b/src/kernel/scheduler.c @@ -6,6 +6,7 @@ #include "timer.h" // Need access to current tick usually, but we pass it in wake_check #include "spinlock.h" #include "utils.h" +#include "errno.h" #include "hal/cpu.h" #if defined(__i386__) #include "arch/x86/usermode.h" @@ -71,7 +72,7 @@ static void process_reap_locked(struct process* p) { } int process_waitpid(int pid, int* status_out, uint32_t options) { - if (!current_process) return -1; + if (!current_process) return -ECHILD; const uint32_t WNOHANG = 1U; @@ -103,7 +104,7 @@ int process_waitpid(int pid, int* status_out, uint32_t options) { if (!found_child) { spin_unlock_irqrestore(&sched_lock, flags); - return -1; + return -ECHILD; } if ((options & WNOHANG) != 0) { diff --git a/src/kernel/syscall.c b/src/kernel/syscall.c index dd58b7a..691a331 100644 --- a/src/kernel/syscall.c +++ b/src/kernel/syscall.c @@ -296,11 +296,11 @@ static struct file* fd_get(int fd) { } static int fd_close(int fd) { - if (!current_process) return -1; - if (fd < 0 || fd >= PROCESS_MAX_FILES) return -1; + if (!current_process) return -EBADF; + if (fd < 0 || fd >= PROCESS_MAX_FILES) return -EBADF; struct file* f = current_process->files[fd]; - if (!f) return -1; + if (!f) return -EBADF; current_process->files[fd] = NULL; if (f->refcount > 0) { @@ -534,12 +534,12 @@ static int syscall_lseek_impl(int fd, int32_t offset, int whence) { static int syscall_open_impl(const char* user_path, uint32_t flags) { (void)flags; - if (!user_path) return -1; + if (!user_path) return -EFAULT; char path[128]; for (size_t i = 0; i < sizeof(path); i++) { if (copy_from_user(&path[i], &user_path[i], 1) < 0) { - return -1; + return -EFAULT; } if (path[i] == 0) break; if (i + 1 == sizeof(path)) { @@ -549,11 +549,11 @@ static int syscall_open_impl(const char* user_path, uint32_t flags) { } fs_node_t* node = vfs_lookup(path); - if (!node) return -1; - if (node->flags != FS_FILE) return -1; + if (!node) return -ENOENT; + if (node->flags != FS_FILE && node->flags != FS_CHARDEVICE) return -EINVAL; struct file* f = (struct file*)kmalloc(sizeof(*f)); - if (!f) return -1; + if (!f) return -ENOMEM; f->node = node; f->offset = 0; f->flags = 0; @@ -562,23 +562,23 @@ static int syscall_open_impl(const char* user_path, uint32_t flags) { int fd = fd_alloc(f); if (fd < 0) { kfree(f); - return -1; + return -EMFILE; } return fd; } static int syscall_read_impl(int fd, void* user_buf, uint32_t len) { - if (len > 1024 * 1024) return -1; - if (user_range_ok(user_buf, (size_t)len) == 0) return -1; + if (len > 1024 * 1024) return -EINVAL; + if (user_range_ok(user_buf, (size_t)len) == 0) return -EFAULT; if (fd == 0 && (!current_process || !current_process->files[0])) { return tty_read(user_buf, len); } - if ((fd == 1 || fd == 2) && (!current_process || !current_process->files[fd])) return -1; + if ((fd == 1 || fd == 2) && (!current_process || !current_process->files[fd])) return -EBADF; struct file* f = fd_get(fd); - if (!f || !f->node) return -1; + if (!f || !f->node) return -EBADF; uint8_t kbuf[256]; uint32_t total = 0; @@ -590,7 +590,7 @@ static int syscall_read_impl(int fd, void* user_buf, uint32_t len) { if (rd == 0) break; if (copy_to_user((uint8_t*)user_buf + total, kbuf, rd) < 0) { - return -1; + return -EFAULT; } f->offset += rd; @@ -704,14 +704,14 @@ static void syscall_handler(struct registers* regs) { uint32_t options = regs->edx; if (user_status && user_range_ok(user_status, sizeof(int)) == 0) { - regs->eax = (uint32_t)-1; + regs->eax = (uint32_t)-EFAULT; return; } int status = 0; int retpid = process_waitpid(pid, &status, options); if (retpid < 0) { - regs->eax = (uint32_t)-1; + regs->eax = (uint32_t)retpid; return; } @@ -722,7 +722,7 @@ static void syscall_handler(struct registers* regs) { if (user_status) { if (copy_to_user(user_status, &status, sizeof(status)) < 0) { - regs->eax = (uint32_t)-1; + regs->eax = (uint32_t)-EFAULT; return; } } @@ -785,7 +785,7 @@ static void syscall_handler(struct registers* regs) { return; } - regs->eax = (uint32_t)-1; + regs->eax = (uint32_t)-ENOSYS; } void syscall_init(void) { diff --git a/src/kernel/tty.c b/src/kernel/tty.c index 95055a1..c561964 100644 --- a/src/kernel/tty.c +++ b/src/kernel/tty.c @@ -5,6 +5,7 @@ #include "spinlock.h" #include "uart_console.h" #include "uaccess.h" +#include "errno.h" #include "hal/cpu.h" @@ -121,9 +122,9 @@ void tty_init(void) { } int tty_write(const void* user_buf, uint32_t len) { - if (!user_buf) return -1; - if (len > 1024 * 1024) return -1; - if (user_range_ok(user_buf, (size_t)len) == 0) return -1; + if (!user_buf) return -EFAULT; + if (len > 1024 * 1024) return -EINVAL; + if (user_range_ok(user_buf, (size_t)len) == 0) return -EFAULT; char kbuf[256]; uint32_t remaining = len; @@ -133,7 +134,7 @@ int tty_write(const void* user_buf, uint32_t len) { uint32_t chunk = remaining; if (chunk > sizeof(kbuf)) chunk = (uint32_t)sizeof(kbuf); - if (copy_from_user(kbuf, (const void*)up, (size_t)chunk) < 0) return -1; + if (copy_from_user(kbuf, (const void*)up, (size_t)chunk) < 0) return -EFAULT; for (uint32_t i = 0; i < chunk; i++) { uart_put_char(kbuf[i]); @@ -147,10 +148,10 @@ int tty_write(const void* user_buf, uint32_t len) { } int tty_read(void* user_buf, uint32_t len) { - if (!user_buf) return -1; - if (len > 1024 * 1024) return -1; - if (user_range_ok(user_buf, (size_t)len) == 0) return -1; - if (!current_process) return -1; + if (!user_buf) return -EFAULT; + if (len > 1024 * 1024) return -EINVAL; + if (user_range_ok(user_buf, (size_t)len) == 0) return -EFAULT; + if (!current_process) return -ECHILD; while (1) { uintptr_t flags = spin_lock_irqsave(&tty_lock); @@ -173,7 +174,7 @@ int tty_read(void* user_buf, uint32_t len) { spin_unlock_irqrestore(&tty_lock, flags); - if (copy_to_user((uint8_t*)user_buf + total, kbuf, (size_t)chunk) < 0) return -1; + if (copy_to_user((uint8_t*)user_buf + total, kbuf, (size_t)chunk) < 0) return -EFAULT; total += chunk; flags = spin_lock_irqsave(&tty_lock);