Process.h 14 KB

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