process.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #pragma once
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <tuple>
  6. #include <utility>
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include <stdint.h>
  11. #include <sys/types.h>
  12. #include <types/allocator.hpp>
  13. #include <types/cplusplus.hpp>
  14. #include <types/path.hpp>
  15. #include <types/types.h>
  16. #include <kernel/async/waitlist.hpp>
  17. #include <kernel/interrupt.hpp>
  18. #include <kernel/mem/mm_list.hpp>
  19. #include <kernel/mem/paging.hpp>
  20. #include <kernel/signal.hpp>
  21. #include <kernel/task/current.hpp>
  22. #include <kernel/task/thread.hpp>
  23. #include <kernel/tty.hpp>
  24. #include <kernel/user/thread_local.hpp>
  25. #include <kernel/vfs.hpp>
  26. #include <kernel/vfs/dentry.hpp>
  27. class process;
  28. class proclist;
  29. inline process* volatile current_process;
  30. inline proclist* procs;
  31. struct process_attr {
  32. uint16_t system : 1;
  33. uint16_t zombie : 1 = 0;
  34. };
  35. class process {
  36. public:
  37. struct wait_obj {
  38. pid_t pid;
  39. int code;
  40. };
  41. public:
  42. kernel::mem::mm_list mms{};
  43. std::set<kernel::task::thread> thds;
  44. kernel::async::wait_list waitlist;
  45. kernel::async::mutex mtx_waitprocs;
  46. std::list<wait_obj> waitprocs;
  47. process_attr attr{};
  48. fs::rust_file_array files;
  49. fs::rust_fs_context fs_context;
  50. pid_t pid{};
  51. pid_t ppid{};
  52. pid_t pgid{};
  53. pid_t sid{};
  54. kernel::tty::tty* control_tty{};
  55. std::set<pid_t> children;
  56. public:
  57. process(const process&) = delete;
  58. explicit process(const process& parent, pid_t pid);
  59. // this function is used for system initialization
  60. // DO NOT use this after the system is on
  61. explicit process(pid_t pid, pid_t ppid);
  62. constexpr bool is_system(void) const { return attr.system; }
  63. constexpr bool is_zombie(void) const { return attr.zombie; }
  64. void send_signal(kernel::signal_list::signo_type signal);
  65. };
  66. class proclist final {
  67. private:
  68. std::map<pid_t, process> m_procs;
  69. pid_t m_nextpid = 2;
  70. constexpr pid_t next_pid() { return m_nextpid++; }
  71. process& real_emplace(pid_t pid, pid_t ppid);
  72. public:
  73. proclist();
  74. constexpr process& copy_from(process& proc) {
  75. pid_t pid = next_pid();
  76. auto [iter, inserted] = m_procs.try_emplace(pid, proc, pid);
  77. assert(inserted);
  78. proc.children.insert(pid);
  79. return iter->second;
  80. }
  81. constexpr void remove(pid_t pid) {
  82. make_children_orphans(pid);
  83. auto proc_iter = m_procs.find(pid);
  84. auto ppid = proc_iter->second.ppid;
  85. find(ppid).children.erase(pid);
  86. m_procs.erase(proc_iter);
  87. }
  88. constexpr std::pair<process*, bool> try_find(pid_t pid) const {
  89. auto iter = m_procs.find(pid);
  90. if (iter)
  91. return {(process*)&iter->second, true};
  92. else
  93. return {nullptr, false};
  94. }
  95. // if process doesn't exist, the behavior is undefined
  96. constexpr process& find(pid_t pid) {
  97. auto [ptr, found] = try_find(pid);
  98. assert(found);
  99. return *ptr;
  100. }
  101. constexpr void make_children_orphans(pid_t pid) {
  102. auto& children = find(pid).children;
  103. auto& init_children = find(1).children;
  104. for (auto item : children) {
  105. init_children.insert(item);
  106. find(item).ppid = 1;
  107. }
  108. children.clear();
  109. }
  110. // the process MUST exist, or the behavior is undefined
  111. void send_signal(pid_t pid, kernel::signal_list::signo_type signal) {
  112. auto& proc = find(pid);
  113. proc.send_signal(signal);
  114. }
  115. void send_signal_grp(pid_t pgid, kernel::signal_list::signo_type signal) {
  116. // TODO: find processes that are in the same session quickly
  117. for (auto& [pid, proc] : m_procs) {
  118. if (proc.pgid != pgid)
  119. continue;
  120. proc.send_signal(signal);
  121. }
  122. }
  123. void kill(pid_t pid, int exit_code);
  124. constexpr auto begin() const { return m_procs.begin(); }
  125. constexpr auto end() const { return m_procs.end(); }
  126. };
  127. void NORETURN init_scheduler(kernel::mem::paging::pfn_t kernel_stack_pfn);
  128. /// @return true if returned normally, false if being interrupted
  129. bool schedule_now(void);
  130. bool schedule_now_preempt_disabled();
  131. void NORETURN schedule_noreturn(void);
  132. void NORETURN freeze(void);
  133. void NORETURN kill_current(int signo);
  134. void check_signal(void);