summaryrefslogtreecommitdiff
path: root/src/sys/os
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-09-26 12:24:11 -0400
committerIan Moffett <ian@osmora.org>2025-09-26 12:24:11 -0400
commitbb3dea72205df8496e3b811940265edfff876318 (patch)
treeddfea17ca799ad640c3a492bb8fc09f3c49f7646 /src/sys/os
parenta004f511d523ef194fa664f3a5708e0b2c131383 (diff)
kernel: filedesc: Allocate standard streams
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src/sys/os')
-rw-r--r--src/sys/os/os_filedes.c71
-rw-r--r--src/sys/os/os_proc.c6
2 files changed, 77 insertions, 0 deletions
diff --git a/src/sys/os/os_filedes.c b/src/sys/os/os_filedes.c
index ecb94ef..e7cc83a 100644
--- a/src/sys/os/os_filedes.c
+++ b/src/sys/os/os_filedes.c
@@ -30,7 +30,9 @@
#include <sys/syscall.h>
#include <sys/syslog.h>
#include <sys/errno.h>
+#include <sys/limits.h>
#include <os/filedesc.h>
+#include <os/kalloc.h>
#include <io/cons/cons.h>
#include <sys/proc.h>
#include <string.h>
@@ -38,6 +40,75 @@
#define STDOUT_FILENO 1
/*
+ * Allocate a file descriptor from a specific process's
+ * file descriptor table
+ *
+ * @procp: Process to allocate fd from
+ * @fd_res: Result pointer is written here
+ *
+ * Returns zero on success, otherwise a less than
+ * zero value upon failure
+ */
+static int
+fd_alloc(struct proc *procp, struct filedesc **fd_res)
+{
+ struct filedesc *fd;
+
+ if (procp == NULL) {
+ return -EINVAL;
+ }
+
+ /*
+ * Find an entry that is free and allocate a new
+ * file descriptor for it.
+ */
+ for (int i = 0; i < FD_MAX; ++i) {
+ if (procp->fdtab[i] != NULL) {
+ continue;
+ }
+
+ fd = kalloc(sizeof(*fd));
+ if (fd == NULL) {
+ return -EINVAL;
+ }
+
+ /* Zero and assign */
+ memset(fd, 0, sizeof(*fd));
+ procp->fdtab[i] = fd;
+ fd->fdno = i;
+ if (fd_res != NULL) {
+ *fd_res = fd;
+ }
+
+ return 0;
+ }
+
+ return -EMFILE;
+}
+
+/*
+ * Initialize file descriptor table
+ */
+int
+fdtab_init(struct proc *procp)
+{
+ if (procp == NULL) {
+ return -EINVAL;
+ }
+
+ /* Don't do it twice */
+ if (procp->fdtab[0] != NULL) {
+ printf("fdtab: fd table already initialized\n");
+ return -1;
+ }
+
+ fd_alloc(procp, NULL); /* stdin */
+ fd_alloc(procp, NULL); /* stdout */
+ fd_alloc(procp, NULL); /* stderr */
+ return 0;
+}
+
+/*
* XXX: STUB
*/
ssize_t
diff --git a/src/sys/os/os_proc.c b/src/sys/os/os_proc.c
index f21fc69..448402f 100644
--- a/src/sys/os/os_proc.c
+++ b/src/sys/os/os_proc.c
@@ -36,6 +36,7 @@
#include <vm/vm.h>
#include <vm/physseg.h>
#include <os/kalloc.h>
+#include <os/filedesc.h>
#include <string.h>
static pid_t next_pid = 0;
@@ -97,6 +98,11 @@ proc_init(struct proc *procp, int flags)
procp->pid = atomic_inc_int(&next_pid);
error = md_proc_init(procp, flags);
+ if (error < 0) {
+ return error;
+ }
+
+ error = fdtab_init(procp);
return error;
}