process.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/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
  68. { return attr.system; }
  69. constexpr bool is_zombie(void) const
  70. { return attr.zombie; }
  71. void send_signal(kernel::signal_list::signo_type signal);
  72. };
  73. class proclist final {
  74. private:
  75. std::map<pid_t, process> m_procs;
  76. pid_t m_nextpid = 2;
  77. constexpr pid_t next_pid() { return m_nextpid++; }
  78. process& real_emplace(pid_t pid, pid_t ppid);
  79. public:
  80. proclist();
  81. constexpr process& copy_from(process& proc)
  82. {
  83. pid_t pid = next_pid();
  84. auto [ iter, inserted ] = m_procs.try_emplace(pid, proc, pid);
  85. assert(inserted);
  86. proc.children.insert(pid);
  87. return iter->second;
  88. }
  89. constexpr void remove(pid_t pid)
  90. {
  91. make_children_orphans(pid);
  92. auto proc_iter = m_procs.find(pid);
  93. auto ppid = proc_iter->second.ppid;
  94. find(ppid).children.erase(pid);
  95. m_procs.erase(proc_iter);
  96. }
  97. constexpr std::pair<process*, bool> try_find(pid_t pid) const
  98. {
  99. auto iter = m_procs.find(pid);
  100. if (iter)
  101. return { (process*)&iter->second, true };
  102. else
  103. return { nullptr, false };
  104. }
  105. // if process doesn't exist, the behavior is undefined
  106. constexpr process& find(pid_t pid)
  107. {
  108. auto [ ptr, found] = try_find(pid);
  109. assert(found);
  110. return *ptr;
  111. }
  112. constexpr void make_children_orphans(pid_t pid)
  113. {
  114. auto& children = find(pid).children;
  115. auto& init_children = find(1).children;
  116. for (auto item : children) {
  117. init_children.insert(item);
  118. find(item).ppid = 1;
  119. }
  120. children.clear();
  121. }
  122. // the process MUST exist, or the behavior is undefined
  123. void send_signal(pid_t pid, kernel::signal_list::signo_type signal)
  124. {
  125. auto& proc = find(pid);
  126. proc.send_signal(signal);
  127. }
  128. void send_signal_grp(pid_t pgid, kernel::signal_list::signo_type signal)
  129. {
  130. // TODO: find processes that are in the same session quickly
  131. for (auto& [ pid, proc ] : m_procs) {
  132. if (proc.pgid != pgid)
  133. continue;
  134. proc.send_signal(signal);
  135. }
  136. }
  137. void kill(pid_t pid, int exit_code);
  138. constexpr auto begin() const { return m_procs.begin(); }
  139. constexpr auto end() const { return m_procs.end(); }
  140. };
  141. void NORETURN init_scheduler(kernel::mem::paging::pfn_t kernel_stack_pfn);
  142. /// @return true if returned normally, false if being interrupted
  143. bool schedule(void);
  144. void NORETURN schedule_noreturn(void);
  145. void NORETURN freeze(void);
  146. void NORETURN kill_current(int signo);
  147. void check_signal(void);