process.hpp 7.1 KB

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