process.hpp 7.1 KB

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