Browse Source

feat: move codes to kill proc into proclist kill()

greatbridf 2 years ago
parent
commit
bc87e8c44f
3 changed files with 39 additions and 33 deletions
  1. 2 0
      include/kernel/process.hpp
  2. 36 0
      src/kernel/process.cpp
  3. 1 33
      src/kernel/syscall.cpp

+ 2 - 0
include/kernel/process.hpp

@@ -364,6 +364,8 @@ public:
             m_child_idx.remove(children);
         }
     }
+
+    void kill(pid_t pid, int exit_code);
 };
 
 class readyqueue final {

+ 36 - 0
src/kernel/process.cpp

@@ -84,6 +84,42 @@ process::process(pid_t _ppid, bool _system)
 {
 }
 
+void proclist::kill(pid_t pid, int exit_code)
+{
+    process* proc = this->find(pid);
+
+    // remove threads from ready list
+    for (auto& thd : proc->thds.underlying_list()) {
+        thd.attr.ready = 0;
+        readythds->remove_all(&thd);
+    }
+
+    // write back mmap'ped files and close them
+    proc->files.close_all();
+
+    // unmap all user memory areas
+    proc->mms.clear_user();
+
+    // init should never exit
+    if (proc->ppid == 0) {
+        console->print("kernel panic: init exited!\n");
+        crash();
+    }
+
+    // make child processes orphans (children of init)
+    this->make_children_orphans(pid);
+
+    proc->attr.zombie = 1;
+
+    // notify parent process and init
+    auto* parent = this->find(proc->ppid);
+    auto* init = this->find(1);
+    while (!proc->wait_lst.empty()) {
+        init->wait_lst.push(proc->wait_lst.front());
+    }
+    parent->wait_lst.push({ nullptr, (void*)pid, (void*)exit_code, nullptr });
+}
+
 inline void NORETURN _noreturn_crash(void)
 {
     for (;;)

+ 1 - 33
src/kernel/syscall.cpp

@@ -159,8 +159,6 @@ void _syscall_exec(interrupt_stack* data)
 void _syscall_exit(interrupt_stack* data)
 {
     uint32_t exit_code = data->s_regs.edi;
-    pid_t pid = current_process->pid;
-    pid_t ppid = current_process->ppid;
 
     // TODO: terminating a thread only
     if (current_thread->owner->thds.size() != 1) {
@@ -168,37 +166,7 @@ void _syscall_exit(interrupt_stack* data)
     }
 
     // terminating a whole process:
-
-    // remove threads from ready list
-    for (auto& thd : current_process->thds.underlying_list()) {
-        thd.attr.ready = 0;
-        readythds->remove_all(&thd);
-    }
-
-    // TODO: write back mmap'ped files and close them
-
-    // unmap all user memory areas
-    current_process->mms.clear_user();
-
-    // init should never exit
-    if (current_process->ppid == 0) {
-        console->print("kernel panic: init exited!\n");
-        crash();
-    }
-
-    // make child processes orphans (children of init)
-    procs->make_children_orphans(pid);
-
-    current_process->attr.zombie = 1;
-
-    // notify parent process and init
-    auto* proc = procs->find(pid);
-    auto* parent = procs->find(ppid);
-    auto* init = procs->find(1);
-    while (!proc->wait_lst.empty()) {
-        init->wait_lst.push(proc->wait_lst.front());
-    }
-    parent->wait_lst.push({ current_thread, (void*)pid, (void*)exit_code, nullptr });
+    procs->kill(current_process->pid, exit_code);
 
     // switch to new process and continue
     schedule();