process.hpp 4.3 KB

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