process.hpp 4.3 KB

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