Merge branch 'master' into vfs-dev
[lunaix-os.git] / lunaix-os / kernel / sched.c
index 12deb174e7c90cc3fe5b6bd07055075d21f6e3ad..be9952519fe282993a29154123c89a90f8d7b249 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <lunaix/mm/kalloc.h>
 #include <lunaix/mm/pmm.h>
 
 #include <lunaix/mm/kalloc.h>
 #include <lunaix/mm/pmm.h>
+#include <lunaix/mm/valloc.h>
 #include <lunaix/mm/vmm.h>
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
 #include <lunaix/mm/vmm.h>
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
@@ -59,7 +60,8 @@ run(struct proc_info* proc)
     apic_done_servicing();
 
     asm volatile("pushl %0\n"
     apic_done_servicing();
 
     asm volatile("pushl %0\n"
-                 "jmp switch_to\n" ::"r"(proc)); // kernel/asm/x86/interrupt.S
+                 "jmp switch_to\n" ::"r"(proc)
+                 : "memory"); // kernel/asm/x86/interrupt.S
 }
 
 int
 }
 
 int
@@ -201,6 +203,11 @@ __DEFINE_LXSYSCALL3(pid_t, waitpid, pid_t, pid, int*, status, int, options)
     return _wait(pid, status, options);
 }
 
     return _wait(pid, status, options);
 }
 
+__DEFINE_LXSYSCALL(int, geterrno)
+{
+    return __current->k_status;
+}
+
 pid_t
 _wait(pid_t wpid, int* status, int options)
 {
 pid_t
 _wait(pid_t wpid, int* status, int options)
 {
@@ -237,7 +244,9 @@ repeat:
 done:
     cpu_disable_interrupt();
     status_flags |= PEXITSIG * (proc->sig_inprogress != 0);
 done:
     cpu_disable_interrupt();
     status_flags |= PEXITSIG * (proc->sig_inprogress != 0);
-    *status = proc->exit_code | status_flags;
+    if (status) {
+        *status = proc->exit_code | status_flags;
+    }
     return destroy_process(proc->pid);
 }
 
     return destroy_process(proc->pid);
 }
 
@@ -264,6 +273,7 @@ alloc_process()
     proc->pid = i;
     proc->created = clock_systime();
     proc->pgid = proc->pid;
     proc->pid = i;
     proc->created = clock_systime();
     proc->pgid = proc->pid;
+    proc->fdtable = vzalloc(sizeof(struct v_fdtable));
 
     llist_init_head(&proc->mm.regions);
     llist_init_head(&proc->children);
 
     llist_init_head(&proc->mm.regions);
     llist_init_head(&proc->children);
@@ -279,7 +289,7 @@ commit_process(struct proc_info* process)
     assert(process == &sched_ctx._procs[process->pid]);
 
     if (process->state != PS_CREATED) {
     assert(process == &sched_ctx._procs[process->pid]);
 
     if (process->state != PS_CREATED) {
-        __current->k_status = LXINVL;
+        __current->k_status = EINVAL;
         return;
     }
 
         return;
     }
 
@@ -302,13 +312,21 @@ destroy_process(pid_t pid)
 {
     int index = pid;
     if (index <= 0 || index > sched_ctx.ptable_len) {
 {
     int index = pid;
     if (index <= 0 || index > sched_ctx.ptable_len) {
-        __current->k_status = LXINVLDPID;
+        __current->k_status = EINVAL;
         return;
     }
     struct proc_info* proc = &sched_ctx._procs[index];
     proc->state = PS_DESTROY;
     llist_delete(&proc->siblings);
 
         return;
     }
     struct proc_info* proc = &sched_ctx._procs[index];
     proc->state = PS_DESTROY;
     llist_delete(&proc->siblings);
 
+    for (size_t i = 0; i < VFS_MAX_FD; i++) {
+        struct v_fd* fd = proc->fdtable->fds[i];
+        if (fd)
+            vfs_close(fd->file);
+    }
+
+    vfree(proc->fdtable);
+
     struct mm_region *pos, *n;
     llist_for_each(pos, n, &proc->mm.regions.head, head)
     {
     struct mm_region *pos, *n;
     llist_for_each(pos, n, &proc->mm.regions.head, head)
     {