]> git.corax.cc Git - corax/commitdiff
Add infrastructure to allocate file descriptors to processes
authorMatthias Kruk <m@m10k.eu>
Thu, 3 Oct 2019 09:39:51 +0000 (18:39 +0900)
committerMatthias Kruk <m@m10k.eu>
Thu, 3 Oct 2019 09:40:38 +0000 (18:40 +0900)
kernel/core/fd.h [new file with mode: 0644]
kernel/core/process.c
kernel/include/process.h

diff --git a/kernel/core/fd.h b/kernel/core/fd.h
new file mode 100644 (file)
index 0000000..1d87f6f
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef __FD_H
+#define __FD_H
+
+#define FD_VALID (1 << 0)
+
+struct fd {
+       int fd_flags;
+       int fd_socknum;
+};
+
+#endif /* __FD_H */
index bbad28dff58dc5ec1e161bdfea35e2351706e5cf..06b00c81536ab43960f75b29018fd5c5ececb8c8 100644 (file)
@@ -6,11 +6,14 @@
 #include <debug.h>
 #include <process.h>
 #include "sched.h"
+#include "fd.h"
 
 struct process {
        struct pagedir *p_pgdir;
        struct task    *p_tasks;
        u32_t          p_privl;
+
+       struct fd      p_fds[16];
 };
 
 extern int sched_enqueue(task_t*);
@@ -54,6 +57,7 @@ int process_create(process_t **dst, u32_t ppl, void *entry)
                proc->p_tasks->t_kstack = kstack;
                proc->p_tasks->t_tslice = 50;
                proc->p_tasks->t_rslice = 50;
+               proc->p_tasks->t_proc = proc;
 
                if(sched_enqueue(proc->p_tasks) != 0) {
                        PANIC("sched_enqueue() failed for a new task\n");
@@ -93,3 +97,41 @@ int process_switch(process_t *proc)
 
        return(ret_val);
 }
+
+int process_falloc(process_t *proc, int sock)
+{
+       int ret_val;
+       int i;
+
+       ret_val = -EMFILE;
+
+       for(i = 0; i < (sizeof(proc->p_fds) / sizeof(proc->p_fds[0])); i++) {
+               if(!(proc->p_fds[i].fd_flags & FD_VALID)) {
+                       ret_val = i;
+                       proc->p_fds[i].fd_socknum = sock;
+                       proc->p_fds[i].fd_flags |= FD_VALID;
+                       break;
+               }
+       }
+
+       return(ret_val);
+}
+
+int process_ffree(process_t *proc, int fd)
+{
+       int ret_val;
+
+       ret_val = -EINVAL;
+
+       if(fd >= 0 && fd < (sizeof(proc->p_fds) / sizeof(proc->p_fds[0]))) {
+               ret_val = -EBADF;
+
+               if(proc->p_fds[fd].fd_flags & FD_VALID) {
+                       proc->p_fds[fd].fd_flags = 0;
+                       proc->p_fds[fd].fd_socknum = -1;
+                       ret_val = 0;
+               }
+       }
+
+       return(ret_val);
+}
index c20118033f85a1be739551690246ff15b21e1e00..3b1e566a776b7fb38dd422116561be147dcbf2e3 100644 (file)
@@ -26,4 +26,7 @@ typedef struct process process_t;
 int process_create(process_t**, u32_t, void*);
 int process_switch(process_t*);
 
+int process_falloc(process_t*, int);
+int process_ffree(process_t*, int);
+
 #endif /* __PROCESS_H */