]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
posix: standardize -errno returns
authorTulio A M Mendes <[email protected]>
Sun, 8 Feb 2026 02:46:46 +0000 (23:46 -0300)
committerTulio A M Mendes <[email protected]>
Sun, 8 Feb 2026 02:46:46 +0000 (23:46 -0300)
Adopt negative errno return convention across waitpid/open/fd_close and TTY read/write, add missing errno constants, and return ENOSYS for unknown syscalls.

include/errno.h
src/kernel/scheduler.c
src/kernel/syscall.c
src/kernel/tty.c

index c4de0d5d4e9baa0892d3a815db5a4e7d6ae0502b..81a618d4b5a4ff056e8bc71d1f488bad691b0331 100644 (file)
@@ -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
index db0bea2b324ddf3eefad3dbef57a96a0013c0304..40ac6b496fbaf06c60bf2f2017522a8dad973efa 100644 (file)
@@ -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) {
index dd58b7aebc87857da5852df430e8c2d04005f08c..691a331e0e1300b2ad160fc0ed14aaccef7af4e0 100644 (file)
@@ -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) {
index 95055a174cd87aaff42bd3e967aa8e10615d6aab..c561964ec82b98c58861087d9831c736ed8a75a7 100644 (file)
@@ -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);