Process.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #pragma once
  2. #include <AK/Types.h>
  3. #include <AK/InlineLinkedList.h>
  4. #include <AK/AKString.h>
  5. #include <AK/Vector.h>
  6. #include <AK/WeakPtr.h>
  7. #include <AK/Weakable.h>
  8. #include <Kernel/FileSystem/VirtualFileSystem.h>
  9. #include <Kernel/TTY/TTY.h>
  10. #include <Kernel/Syscall.h>
  11. #include <Kernel/UnixTypes.h>
  12. #include <Kernel/Thread.h>
  13. #include <Kernel/Lock.h>
  14. class FileDescriptor;
  15. class PageDirectory;
  16. class Region;
  17. class VMObject;
  18. void kgettimeofday(timeval&);
  19. class Process : public InlineLinkedListNode<Process>, public Weakable<Process> {
  20. friend class InlineLinkedListNode<Process>;
  21. friend class Thread;
  22. public:
  23. static Process* create_kernel_process(String&& name, void (*entry)());
  24. static Process* create_user_process(const String& path, uid_t, gid_t, pid_t ppid, int& error, Vector<String>&& arguments = Vector<String>(), Vector<String>&& environment = Vector<String>(), TTY* = nullptr);
  25. ~Process();
  26. static Vector<pid_t> all_pids();
  27. static Vector<Process*> all_processes();
  28. enum Priority {
  29. IdlePriority,
  30. LowPriority,
  31. NormalPriority,
  32. HighPriority,
  33. };
  34. enum RingLevel {
  35. Ring0 = 0,
  36. Ring3 = 3,
  37. };
  38. bool is_dead() const { return m_dead; }
  39. Thread::State state() const { return main_thread().state(); }
  40. Thread& main_thread() { return *m_main_thread; }
  41. const Thread& main_thread() const { return *m_main_thread; }
  42. bool is_ring0() const { return m_ring == Ring0; }
  43. bool is_ring3() const { return m_ring == Ring3; }
  44. PageDirectory& page_directory() { return *m_page_directory; }
  45. const PageDirectory& page_directory() const { return *m_page_directory; }
  46. static Process* from_pid(pid_t);
  47. void set_priority(Priority p) { m_priority = p; }
  48. Priority priority() const { return m_priority; }
  49. const String& name() const { return m_name; }
  50. pid_t pid() const { return m_pid; }
  51. pid_t sid() const { return m_sid; }
  52. pid_t pgid() const { return m_pgid; }
  53. uid_t uid() const { return m_uid; }
  54. gid_t gid() const { return m_gid; }
  55. const HashTable<gid_t>& gids() const { return m_gids; }
  56. uid_t euid() const { return m_euid; }
  57. gid_t egid() const { return m_egid; }
  58. pid_t ppid() const { return m_ppid; }
  59. mode_t umask() const { return m_umask; }
  60. bool in_group(gid_t) const;
  61. FileDescriptor* file_descriptor(int fd);
  62. const FileDescriptor* file_descriptor(int fd) const;
  63. template<typename Callback> static void for_each(Callback);
  64. template<typename Callback> static void for_each_in_pgrp(pid_t, Callback);
  65. template<typename Callback> void for_each_child(Callback);
  66. template<typename Callback> void for_each_thread(Callback) const;
  67. void die();
  68. void finalize();
  69. int sys$gettid();
  70. int sys$donate(int tid);
  71. int sys$shm_open(const char* name, int flags, mode_t);
  72. int sys$shm_unlink(const char* name);
  73. int sys$ftruncate(int fd, off_t);
  74. pid_t sys$setsid();
  75. pid_t sys$getsid(pid_t);
  76. int sys$setpgid(pid_t pid, pid_t pgid);
  77. pid_t sys$getpgrp();
  78. pid_t sys$getpgid(pid_t);
  79. uid_t sys$getuid();
  80. gid_t sys$getgid();
  81. uid_t sys$geteuid();
  82. gid_t sys$getegid();
  83. pid_t sys$getpid();
  84. pid_t sys$getppid();
  85. mode_t sys$umask(mode_t);
  86. int sys$open(const char* path, int options, mode_t mode = 0);
  87. int sys$close(int fd);
  88. ssize_t sys$read(int fd, byte*, ssize_t);
  89. ssize_t sys$write(int fd, const byte*, ssize_t);
  90. int sys$fstat(int fd, stat*);
  91. int sys$lstat(const char*, stat*);
  92. int sys$stat(const char*, stat*);
  93. int sys$lseek(int fd, off_t, int whence);
  94. int sys$kill(pid_t pid, int sig);
  95. [[noreturn]] void sys$exit(int status);
  96. [[noreturn]] void sys$sigreturn();
  97. pid_t sys$waitpid(pid_t, int* wstatus, int options);
  98. void* sys$mmap(const Syscall::SC_mmap_params*);
  99. int sys$munmap(void*, size_t size);
  100. int sys$set_mmap_name(void*, size_t, const char*);
  101. int sys$select(const Syscall::SC_select_params*);
  102. int sys$poll(pollfd*, int nfds, int timeout);
  103. ssize_t sys$get_dir_entries(int fd, void*, ssize_t);
  104. int sys$getcwd(char*, ssize_t);
  105. int sys$chdir(const char*);
  106. int sys$sleep(unsigned seconds);
  107. int sys$usleep(useconds_t usec);
  108. int sys$gettimeofday(timeval*);
  109. int sys$gethostname(char*, ssize_t);
  110. int sys$uname(utsname*);
  111. int sys$readlink(const char*, char*, ssize_t);
  112. int sys$ttyname_r(int fd, char*, ssize_t);
  113. int sys$ptsname_r(int fd, char*, ssize_t);
  114. pid_t sys$fork(RegisterDump&);
  115. int sys$execve(const char* filename, const char** argv, const char** envp);
  116. int sys$isatty(int fd);
  117. int sys$getdtablesize();
  118. int sys$dup(int oldfd);
  119. int sys$dup2(int oldfd, int newfd);
  120. int sys$sigaction(int signum, const sigaction* act, sigaction* old_act);
  121. int sys$sigprocmask(int how, const sigset_t* set, sigset_t* old_set);
  122. int sys$sigpending(sigset_t*);
  123. int sys$getgroups(ssize_t, gid_t*);
  124. int sys$setgroups(ssize_t, const gid_t*);
  125. int sys$pipe(int* pipefd);
  126. int sys$killpg(int pgrp, int sig);
  127. int sys$setgid(gid_t);
  128. int sys$setuid(uid_t);
  129. unsigned sys$alarm(unsigned seconds);
  130. int sys$access(const char* pathname, int mode);
  131. int sys$fcntl(int fd, int cmd, dword extra_arg);
  132. int sys$ioctl(int fd, unsigned request, unsigned arg);
  133. int sys$mkdir(const char* pathname, mode_t mode);
  134. clock_t sys$times(tms*);
  135. int sys$utime(const char* pathname, const struct utimbuf*);
  136. int sys$link(const char* old_path, const char* new_path);
  137. int sys$unlink(const char* pathname);
  138. int sys$symlink(const char* target, const char* linkpath);
  139. int sys$rmdir(const char* pathname);
  140. int sys$read_tsc(dword* lsw, dword* msw);
  141. int sys$chmod(const char* pathname, mode_t);
  142. int sys$fchmod(int fd, mode_t);
  143. int sys$chown(const char* pathname, uid_t, gid_t);
  144. int sys$socket(int domain, int type, int protocol);
  145. int sys$bind(int sockfd, const sockaddr* addr, socklen_t);
  146. int sys$listen(int sockfd, int backlog);
  147. int sys$accept(int sockfd, sockaddr*, socklen_t*);
  148. int sys$connect(int sockfd, const sockaddr*, socklen_t);
  149. ssize_t sys$sendto(const Syscall::SC_sendto_params*);
  150. ssize_t sys$recvfrom(const Syscall::SC_recvfrom_params*);
  151. int sys$getsockopt(const Syscall::SC_getsockopt_params*);
  152. int sys$setsockopt(const Syscall::SC_setsockopt_params*);
  153. int sys$restore_signal_mask(dword mask);
  154. int sys$create_thread(int(*)(void*), void*);
  155. int sys$rename(const char* oldpath, const char* newpath);
  156. int sys$create_shared_buffer(pid_t peer_pid, int, void** buffer);
  157. void* sys$get_shared_buffer(int shared_buffer_id);
  158. int sys$release_shared_buffer(int shared_buffer_id);
  159. int sys$seal_shared_buffer(int shared_buffer_id);
  160. int sys$get_shared_buffer_size(int shared_buffer_id);
  161. static void initialize();
  162. [[noreturn]] void crash();
  163. [[nodiscard]] static int reap(Process&);
  164. const TTY* tty() const { return m_tty; }
  165. void set_tty(TTY* tty) { m_tty = tty; }
  166. size_t region_count() const { return m_regions.size(); }
  167. const Vector<Retained<Region>>& regions() const { return m_regions; }
  168. void dump_regions();
  169. dword m_ticks_in_user { 0 };
  170. dword m_ticks_in_kernel { 0 };
  171. dword m_ticks_in_user_for_dead_children { 0 };
  172. dword m_ticks_in_kernel_for_dead_children { 0 };
  173. bool validate_read_from_kernel(LinearAddress) const;
  174. bool validate_read(const void*, ssize_t) const;
  175. bool validate_write(void*, ssize_t) const;
  176. bool validate_read_str(const char* str);
  177. template<typename T> bool validate_read_typed(T* value, size_t count = 1) { return validate_read(value, sizeof(T) * count); }
  178. template<typename T> bool validate_write_typed(T* value, size_t count = 1) { return validate_write(value, sizeof(T) * count); }
  179. Inode& cwd_inode();
  180. Inode* executable_inode() { return m_executable.ptr(); }
  181. int number_of_open_file_descriptors() const;
  182. int max_open_file_descriptors() const { return m_max_open_file_descriptors; }
  183. size_t amount_virtual() const;
  184. size_t amount_resident() const;
  185. size_t amount_shared() const;
  186. Process* fork(RegisterDump&);
  187. int exec(String path, Vector<String> arguments, Vector<String> environment);
  188. bool is_superuser() const { return m_euid == 0; }
  189. Region* allocate_region_with_vmo(LinearAddress, size_t, Retained<VMObject>&&, size_t offset_in_vmo, String&& name, bool is_readable, bool is_writable);
  190. Region* allocate_file_backed_region(LinearAddress, size_t, RetainPtr<Inode>&&, String&& name, bool is_readable, bool is_writable);
  191. Region* allocate_region(LinearAddress, size_t, String&& name, bool is_readable = true, bool is_writable = true, bool commit = true);
  192. bool deallocate_region(Region& region);
  193. void set_being_inspected(bool b) { m_being_inspected = b; }
  194. bool is_being_inspected() const { return m_being_inspected; }
  195. void terminate_due_to_signal(byte signal);
  196. void send_signal(byte, Process* sender);
  197. int thread_count() const;
  198. Lock& big_lock() { return m_big_lock; }
  199. unsigned syscall_count() const { return m_syscall_count; }
  200. void did_syscall() { ++m_syscall_count; }
  201. private:
  202. friend class MemoryManager;
  203. friend class Scheduler;
  204. friend class Region;
  205. Process(String&& name, uid_t, gid_t, pid_t ppid, RingLevel, RetainPtr<Inode>&& cwd = nullptr, RetainPtr<Inode>&& executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr);
  206. int do_exec(String path, Vector<String> arguments, Vector<String> environment);
  207. int alloc_fd(int first_candidate_fd = 0);
  208. void disown_all_shared_buffers();
  209. void create_signal_trampolines_if_needed();
  210. Thread* m_main_thread { nullptr };
  211. RetainPtr<PageDirectory> m_page_directory;
  212. Process* m_prev { nullptr };
  213. Process* m_next { nullptr };
  214. String m_name;
  215. pid_t m_pid { 0 };
  216. uid_t m_uid { 0 };
  217. gid_t m_gid { 0 };
  218. uid_t m_euid { 0 };
  219. gid_t m_egid { 0 };
  220. pid_t m_sid { 0 };
  221. pid_t m_pgid { 0 };
  222. Priority m_priority { NormalPriority };
  223. struct FileDescriptorAndFlags {
  224. operator bool() const { return !!descriptor; }
  225. void clear() { descriptor = nullptr; flags = 0; }
  226. void set(Retained<FileDescriptor>&& d, dword f = 0) { descriptor = move(d); flags = f; }
  227. RetainPtr<FileDescriptor> descriptor;
  228. dword flags { 0 };
  229. };
  230. Vector<FileDescriptorAndFlags> m_fds;
  231. RingLevel m_ring { Ring0 };
  232. int m_max_open_file_descriptors { 128 };
  233. byte m_termination_status { 0 };
  234. byte m_termination_signal { 0 };
  235. RetainPtr<Inode> m_cwd;
  236. RetainPtr<Inode> m_executable;
  237. TTY* m_tty { nullptr };
  238. Region* region_from_range(LinearAddress, size_t);
  239. Vector<Retained<Region>> m_regions;
  240. // FIXME: Implement some kind of ASLR?
  241. LinearAddress m_next_region;
  242. LinearAddress m_return_to_ring3_from_signal_trampoline;
  243. LinearAddress m_return_to_ring0_from_signal_trampoline;
  244. pid_t m_ppid { 0 };
  245. mode_t m_umask { 022 };
  246. static void notify_waiters(pid_t waitee, int exit_status, int signal);
  247. HashTable<gid_t> m_gids;
  248. bool m_being_inspected { false };
  249. bool m_dead { false };
  250. int m_next_tid { 0 };
  251. unsigned m_syscall_count { 0 };
  252. Lock m_big_lock;
  253. };
  254. class ProcessInspectionHandle {
  255. public:
  256. ProcessInspectionHandle(Process& process)
  257. : m_process(process)
  258. {
  259. if (&process != &current->process()) {
  260. ASSERT(!m_process.is_being_inspected());
  261. m_process.set_being_inspected(true);
  262. }
  263. }
  264. ~ProcessInspectionHandle()
  265. {
  266. m_process.set_being_inspected(false);
  267. }
  268. Process& process() { return m_process; }
  269. static OwnPtr<ProcessInspectionHandle> from_pid(pid_t pid)
  270. {
  271. InterruptDisabler disabler;
  272. auto* process = Process::from_pid(pid);
  273. if (process)
  274. return make<ProcessInspectionHandle>(*process);
  275. return nullptr;
  276. }
  277. Process* operator->() { return &m_process; }
  278. Process& operator*() { return m_process; }
  279. private:
  280. Process& m_process;
  281. };
  282. extern const char* to_string(Process::Priority);
  283. extern InlineLinkedList<Process>* g_processes;
  284. template<typename Callback>
  285. inline void Process::for_each(Callback callback)
  286. {
  287. ASSERT_INTERRUPTS_DISABLED();
  288. for (auto* process = g_processes->head(); process;) {
  289. auto* next_process = process->next();
  290. if (!callback(*process))
  291. break;
  292. process = next_process;
  293. }
  294. }
  295. template<typename Callback>
  296. inline void Process::for_each_child(Callback callback)
  297. {
  298. ASSERT_INTERRUPTS_DISABLED();
  299. pid_t my_pid = pid();
  300. for (auto* process = g_processes->head(); process;) {
  301. auto* next_process = process->next();
  302. if (process->ppid() == my_pid) {
  303. if (!callback(*process))
  304. break;
  305. }
  306. process = next_process;
  307. }
  308. }
  309. template<typename Callback>
  310. inline void Process::for_each_thread(Callback callback) const
  311. {
  312. InterruptDisabler disabler;
  313. pid_t my_pid = pid();
  314. for (auto* thread = g_threads->head(); thread;) {
  315. auto* next_thread = thread->next();
  316. if (thread->pid() == my_pid) {
  317. if (callback(*thread) == IterationDecision::Abort)
  318. break;
  319. }
  320. thread = next_thread;
  321. }
  322. }
  323. template<typename Callback>
  324. inline void Process::for_each_in_pgrp(pid_t pgid, Callback callback)
  325. {
  326. ASSERT_INTERRUPTS_DISABLED();
  327. for (auto* process = g_processes->head(); process;) {
  328. auto* next_process = process->next();
  329. if (process->pgid() == pgid) {
  330. if (!callback(*process))
  331. break;
  332. }
  333. process = next_process;
  334. }
  335. }
  336. inline bool InodeMetadata::may_read(Process& process) const
  337. {
  338. return may_read(process.euid(), process.gids());
  339. }
  340. inline bool InodeMetadata::may_write(Process& process) const
  341. {
  342. return may_write(process.euid(), process.gids());
  343. }
  344. inline bool InodeMetadata::may_execute(Process& process) const
  345. {
  346. return may_execute(process.euid(), process.gids());
  347. }
  348. inline int Thread::pid() const
  349. {
  350. return m_process.pid();
  351. }