process.hpp 4.2 KB

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