diff options
author | Ian Moffett <ian@osmora.org> | 2025-09-26 12:24:11 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-09-26 12:24:11 -0400 |
commit | bb3dea72205df8496e3b811940265edfff876318 (patch) | |
tree | ddfea17ca799ad640c3a492bb8fc09f3c49f7646 /src/sys | |
parent | a004f511d523ef194fa664f3a5708e0b2c131383 (diff) |
kernel: filedesc: Allocate standard streams
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src/sys')
-rw-r--r-- | src/sys/include/os/filedesc.h | 27 | ||||
-rw-r--r-- | src/sys/include/sys/limits.h | 1 | ||||
-rw-r--r-- | src/sys/include/sys/proc.h | 2 | ||||
-rw-r--r-- | src/sys/os/os_filedes.c | 71 | ||||
-rw-r--r-- | src/sys/os/os_proc.c | 6 |
5 files changed, 107 insertions, 0 deletions
diff --git a/src/sys/include/os/filedesc.h b/src/sys/include/os/filedesc.h index 9d6f229..87bf242 100644 --- a/src/sys/include/os/filedesc.h +++ b/src/sys/include/os/filedesc.h @@ -31,6 +31,33 @@ #define _OS_FILEDESC_H_ 1 #include <sys/types.h> +#include <os/vnode.h> + +struct proc; + +/* + * Represents a file descriptor + * + * @fdno: File descriptor index + * @vp: Vnode this fd is linked with + * @mode: File attributes + */ +struct filedesc { + int fdno; + struct vnode *vp; + mode_t mode; +}; + +/* + * Initialize a process file descriptor table + * and set up standard streams + * + * @procp: Process of fd table to init + * + * Returns zero on success, less than zero values + * indicate failure. + */ +int fdtab_init(struct proc *procp); /* * Write to a file descriptor diff --git a/src/sys/include/sys/limits.h b/src/sys/include/sys/limits.h index d2cefb6..5e32233 100644 --- a/src/sys/include/sys/limits.h +++ b/src/sys/include/sys/limits.h @@ -36,5 +36,6 @@ #define CPU_MAX 256 #define SC_MAX 64 /* Max syscalls */ #define SCWIN_MAX 2 /* Max syscall windows */ +#define FD_MAX 256 /* Max file descriptors */ #endif /* !_SYS_LIMITS_H_ */ diff --git a/src/sys/include/sys/proc.h b/src/sys/include/sys/proc.h index 43772be..c31cc4c 100644 --- a/src/sys/include/sys/proc.h +++ b/src/sys/include/sys/proc.h @@ -36,6 +36,7 @@ #include <sys/param.h> #include <sys/queue.h> #include <os/mac.h> +#include <os/filedesc.h> #include <vm/vm.h> #include <machine/pcb.h> /* standard */ @@ -63,6 +64,7 @@ struct proc { uint32_t flags; struct md_pcb pcb; struct syscall_domain scdom; + struct filedesc *fdtab[FD_MAX]; mac_level_t level; TAILQ_HEAD(, vm_range) maplist; TAILQ_ENTRY(proc) link; 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; } |