process.hpp 7.2 KB

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