process.hpp 6.9 KB

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