process.hpp 7.1 KB

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