process.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 <errno.h>
  10. #include <fcntl.h>
  11. #include <stdint.h>
  12. #include <sys/types.h>
  13. #include <kernel/task/current.hpp>
  14. #include <kernel/task/thread.hpp>
  15. #include <types/allocator.hpp>
  16. #include <types/cplusplus.hpp>
  17. #include <types/path.hpp>
  18. #include <types/types.h>
  19. #include <kernel/async/waitlist.hpp>
  20. #include <kernel/interrupt.hpp>
  21. #include <kernel/mem/mm_list.hpp>
  22. #include <kernel/mem/paging.hpp>
  23. #include <kernel/signal.hpp>
  24. #include <kernel/tty.hpp>
  25. #include <kernel/user/thread_local.hpp>
  26. #include <kernel/vfs.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. struct thread_attr {
  36. uint32_t system : 1;
  37. uint32_t ready : 1;
  38. };
  39. class filearr {
  40. private:
  41. // TODO: change this
  42. struct fditem {
  43. int flags;
  44. std::shared_ptr<fs::file> file;
  45. };
  46. std::map<int, fditem> arr;
  47. int min_avail { };
  48. private:
  49. int allocate_fd(int from);
  50. void release_fd(int fd);
  51. inline int next_fd() { return allocate_fd(min_avail); }
  52. public:
  53. constexpr filearr() = default;
  54. constexpr filearr(const filearr& val) = default;
  55. constexpr filearr(filearr&& val) = default;
  56. constexpr filearr& operator=(const filearr&) = delete;
  57. constexpr filearr& operator=(filearr&&) = delete;
  58. // TODO: the third parameter should be int flags
  59. // determining whether the fd should be closed
  60. // after exec() (FD_CLOEXEC)
  61. int dup2(int old_fd, int new_fd);
  62. int dup(int old_fd);
  63. int dupfd(int fd, int minfd, int flags);
  64. int set_flags(int fd, int flags);
  65. int clear_flags(int fd, int flags);
  66. constexpr fs::file* operator[](int i) const
  67. {
  68. auto iter = arr.find(i);
  69. if (!iter)
  70. return nullptr;
  71. return iter->second.file.get();
  72. }
  73. int pipe(int pipefd[2])
  74. {
  75. std::shared_ptr<fs::pipe> ppipe { new fs::pipe };
  76. bool inserted = false;
  77. int fd = next_fd();
  78. std::tie(std::ignore, inserted) = arr.emplace(fd, fditem {
  79. 0, std::shared_ptr<fs::file> {
  80. new fs::fifo_file(nullptr, {
  81. .read = 1,
  82. .write = 0,
  83. .append = 0,
  84. }, ppipe),
  85. } } );
  86. assert(inserted);
  87. // TODO: use copy_to_user()
  88. pipefd[0] = fd;
  89. fd = next_fd();
  90. std::tie(std::ignore, inserted) = arr.emplace(fd, fditem {
  91. 0, std::shared_ptr<fs::file> {
  92. new fs::fifo_file(nullptr, {
  93. .read = 0,
  94. .write = 1,
  95. .append = 0,
  96. }, ppipe),
  97. } } );
  98. assert(inserted);
  99. // TODO: use copy_to_user()
  100. pipefd[1] = fd;
  101. return 0;
  102. }
  103. int open(const process& current, const types::path& filepath, int flags, mode_t mode);
  104. constexpr void close(int fd)
  105. {
  106. auto iter = arr.find(fd);
  107. if (!iter)
  108. return;
  109. release_fd(fd);
  110. arr.erase(iter);
  111. }
  112. constexpr void onexec()
  113. {
  114. for (auto iter = arr.begin(); iter != arr.end(); ) {
  115. if (!(iter->second.flags & FD_CLOEXEC)) {
  116. ++iter;
  117. continue;
  118. }
  119. release_fd(iter->first);
  120. iter = arr.erase(iter);
  121. }
  122. }
  123. constexpr void close_all(void)
  124. {
  125. for (const auto& item : arr)
  126. release_fd(item.first);
  127. arr.clear();
  128. }
  129. constexpr ~filearr()
  130. {
  131. close_all();
  132. }
  133. };
  134. class process {
  135. public:
  136. struct wait_obj {
  137. pid_t pid;
  138. int code;
  139. };
  140. public:
  141. kernel::mem::mm_list mms {};
  142. std::set<kernel::task::thread> thds;
  143. kernel::async::wait_list waitlist;
  144. kernel::async::mutex mtx_waitprocs;
  145. std::list<wait_obj> waitprocs;
  146. process_attr attr {};
  147. filearr files;
  148. types::path pwd;
  149. mode_t umask { 0022 };
  150. pid_t pid {};
  151. pid_t ppid {};
  152. pid_t pgid {};
  153. pid_t sid {};
  154. kernel::tty::tty* control_tty {};
  155. fs::dentry* root { fs::fs_root };
  156. std::set<pid_t> children;
  157. public:
  158. process(const process&) = delete;
  159. explicit process(const process& parent, pid_t pid);
  160. // this function is used for system initialization
  161. // DO NOT use this after the system is on
  162. explicit process(pid_t pid, pid_t ppid);
  163. constexpr bool is_system(void) const
  164. { return attr.system; }
  165. constexpr bool is_zombie(void) const
  166. { return attr.zombie; }
  167. void send_signal(kernel::signal_list::signo_type signal);
  168. };
  169. class proclist final {
  170. private:
  171. std::map<pid_t, process> m_procs;
  172. pid_t m_nextpid = 2;
  173. constexpr pid_t next_pid() { return m_nextpid++; }
  174. process& real_emplace(pid_t pid, pid_t ppid);
  175. public:
  176. proclist();
  177. constexpr process& copy_from(process& proc)
  178. {
  179. pid_t pid = next_pid();
  180. auto [ iter, inserted ] = m_procs.try_emplace(pid, proc, pid);
  181. assert(inserted);
  182. proc.children.insert(pid);
  183. return iter->second;
  184. }
  185. constexpr void remove(pid_t pid)
  186. {
  187. make_children_orphans(pid);
  188. auto proc_iter = m_procs.find(pid);
  189. auto ppid = proc_iter->second.ppid;
  190. find(ppid).children.erase(pid);
  191. m_procs.erase(proc_iter);
  192. }
  193. constexpr std::pair<process*, bool> try_find(pid_t pid) const
  194. {
  195. auto iter = m_procs.find(pid);
  196. if (iter)
  197. return { (process*)&iter->second, true };
  198. else
  199. return { nullptr, false };
  200. }
  201. // if process doesn't exist, the behavior is undefined
  202. constexpr process& find(pid_t pid)
  203. {
  204. auto [ ptr, found] = try_find(pid);
  205. assert(found);
  206. return *ptr;
  207. }
  208. constexpr void make_children_orphans(pid_t pid)
  209. {
  210. auto& children = find(pid).children;
  211. auto& init_children = find(1).children;
  212. for (auto item : children) {
  213. init_children.insert(item);
  214. find(item).ppid = 1;
  215. }
  216. children.clear();
  217. }
  218. // the process MUST exist, or the behavior is undefined
  219. void send_signal(pid_t pid, kernel::signal_list::signo_type signal)
  220. {
  221. auto& proc = find(pid);
  222. proc.send_signal(signal);
  223. }
  224. void send_signal_grp(pid_t pgid, kernel::signal_list::signo_type signal)
  225. {
  226. // TODO: find processes that are in the same session quickly
  227. for (auto& [ pid, proc ] : m_procs) {
  228. if (proc.pgid != pgid)
  229. continue;
  230. proc.send_signal(signal);
  231. }
  232. }
  233. void kill(pid_t pid, int exit_code);
  234. constexpr auto begin() const { return m_procs.begin(); }
  235. constexpr auto end() const { return m_procs.end(); }
  236. };
  237. void NORETURN init_scheduler(kernel::mem::paging::pfn_t kernel_stack_pfn);
  238. /// @return true if returned normally, false if being interrupted
  239. bool schedule(void);
  240. void NORETURN schedule_noreturn(void);
  241. void k_new_thread(void (*func)(void*), void* data);
  242. void NORETURN freeze(void);
  243. void NORETURN kill_current(int signo);
  244. void check_signal(void);