Process.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. #pragma once
  2. #include <AK/InlineLinkedList.h>
  3. #include <AK/NonnullRefPtrVector.h>
  4. #include <AK/String.h>
  5. #include <AK/Types.h>
  6. #include <AK/Vector.h>
  7. #include <AK/WeakPtr.h>
  8. #include <AK/Weakable.h>
  9. #include <Kernel/FileSystem/VirtualFileSystem.h>
  10. #include <Kernel/Lock.h>
  11. #include <Kernel/Syscall.h>
  12. #include <Kernel/TTY/TTY.h>
  13. #include <Kernel/Thread.h>
  14. #include <Kernel/UnixTypes.h>
  15. #include <Kernel/VM/RangeAllocator.h>
  16. #include <LibC/signal_numbers.h>
  17. class ELFLoader;
  18. class FileDescription;
  19. class KBuffer;
  20. class PageDirectory;
  21. class Region;
  22. class VMObject;
  23. class ProcessTracer;
  24. class SharedBuffer;
  25. timeval kgettimeofday();
  26. void kgettimeofday(timeval&);
  27. extern VirtualAddress g_return_to_ring3_from_signal_trampoline;
  28. extern VirtualAddress g_return_to_ring0_from_signal_trampoline;
  29. class Process : public InlineLinkedListNode<Process>
  30. , public Weakable<Process> {
  31. friend class InlineLinkedListNode<Process>;
  32. friend class Thread;
  33. public:
  34. static Process* create_kernel_process(Thread*& first_thread, String&& name, void (*entry)());
  35. static Process* create_user_process(Thread*& first_thread, const String& path, uid_t, gid_t, pid_t ppid, int& error, Vector<String>&& arguments = Vector<String>(), Vector<String>&& environment = Vector<String>(), TTY* = nullptr);
  36. ~Process();
  37. static Vector<pid_t> all_pids();
  38. static Vector<Process*> all_processes();
  39. bool is_profiling() const { return m_profiling; }
  40. void set_profiling(bool profiling) { m_profiling = profiling; }
  41. enum RingLevel : u8 {
  42. Ring0 = 0,
  43. Ring3 = 3,
  44. };
  45. KBuffer backtrace(ProcessInspectionHandle&) const;
  46. bool is_dead() const { return m_dead; }
  47. bool is_ring0() const { return m_ring == Ring0; }
  48. bool is_ring3() const { return m_ring == Ring3; }
  49. PageDirectory& page_directory() { return *m_page_directory; }
  50. const PageDirectory& page_directory() const { return *m_page_directory; }
  51. static Process* from_pid(pid_t);
  52. static void update_info_page_timestamp(const timeval&);
  53. const String& name() const { return m_name; }
  54. pid_t pid() const { return m_pid; }
  55. pid_t sid() const { return m_sid; }
  56. pid_t pgid() const { return m_pgid; }
  57. uid_t uid() const { return m_uid; }
  58. gid_t gid() const { return m_gid; }
  59. const HashTable<gid_t>& extra_gids() const { return m_extra_gids; }
  60. uid_t euid() const { return m_euid; }
  61. gid_t egid() const { return m_egid; }
  62. pid_t ppid() const { return m_ppid; }
  63. mode_t umask() const { return m_umask; }
  64. bool in_group(gid_t) const;
  65. FileDescription* file_description(int fd);
  66. const FileDescription* file_description(int fd) const;
  67. int fd_flags(int fd) const;
  68. template<typename Callback>
  69. static void for_each(Callback);
  70. template<typename Callback>
  71. static void for_each_in_pgrp(pid_t, Callback);
  72. template<typename Callback>
  73. void for_each_child(Callback);
  74. template<typename Callback>
  75. void for_each_thread(Callback) const;
  76. void die();
  77. void finalize();
  78. int sys$yield();
  79. int sys$sync();
  80. int sys$beep();
  81. int sys$get_process_name(char* buffer, int buffer_size);
  82. int sys$watch_file(const char* path, size_t path_length);
  83. int sys$dbgputch(u8);
  84. int sys$dbgputstr(const u8*, int length);
  85. int sys$dump_backtrace();
  86. int sys$gettid();
  87. int sys$donate(int tid);
  88. int sys$ftruncate(int fd, off_t);
  89. pid_t sys$setsid();
  90. pid_t sys$getsid(pid_t);
  91. int sys$setpgid(pid_t pid, pid_t pgid);
  92. pid_t sys$getpgrp();
  93. pid_t sys$getpgid(pid_t);
  94. uid_t sys$getuid();
  95. gid_t sys$getgid();
  96. uid_t sys$geteuid();
  97. gid_t sys$getegid();
  98. pid_t sys$getpid();
  99. pid_t sys$getppid();
  100. mode_t sys$umask(mode_t);
  101. int sys$open(const Syscall::SC_open_params*);
  102. int sys$openat(const Syscall::SC_openat_params*);
  103. int sys$close(int fd);
  104. ssize_t sys$read(int fd, u8*, ssize_t);
  105. ssize_t sys$write(int fd, const u8*, ssize_t);
  106. ssize_t sys$writev(int fd, const struct iovec* iov, int iov_count);
  107. int sys$fstat(int fd, stat*);
  108. int sys$lstat(const char*, size_t, stat*);
  109. int sys$stat(const char*, size_t, stat*);
  110. int sys$lseek(int fd, off_t, int whence);
  111. int sys$kill(pid_t pid, int sig);
  112. [[noreturn]] void sys$exit(int status);
  113. int sys$sigreturn(RegisterDump& registers);
  114. pid_t sys$waitpid(pid_t, int* wstatus, int options);
  115. void* sys$mmap(const Syscall::SC_mmap_params*);
  116. int sys$munmap(void*, size_t size);
  117. int sys$set_mmap_name(const Syscall::SC_set_mmap_name_params*);
  118. int sys$mprotect(void*, size_t, int prot);
  119. int sys$madvise(void*, size_t, int advice);
  120. int sys$purge(int mode);
  121. int sys$select(const Syscall::SC_select_params*);
  122. int sys$poll(pollfd*, int nfds, int timeout);
  123. ssize_t sys$get_dir_entries(int fd, void*, ssize_t);
  124. int sys$getcwd(char*, ssize_t);
  125. int sys$chdir(const char*, size_t);
  126. int sys$fchdir(int fd);
  127. int sys$sleep(unsigned seconds);
  128. int sys$usleep(useconds_t usec);
  129. int sys$gettimeofday(timeval*);
  130. int sys$clock_gettime(clockid_t, timespec*);
  131. int sys$clock_nanosleep(const Syscall::SC_clock_nanosleep_params*);
  132. int sys$gethostname(char*, ssize_t);
  133. int sys$uname(utsname*);
  134. int sys$readlink(const char*, char*, ssize_t);
  135. int sys$ttyname_r(int fd, char*, ssize_t);
  136. int sys$ptsname_r(int fd, char*, ssize_t);
  137. pid_t sys$fork(RegisterDump&);
  138. int sys$execve(const char* filename, const char** argv, const char** envp);
  139. int sys$getdtablesize();
  140. int sys$dup(int oldfd);
  141. int sys$dup2(int oldfd, int newfd);
  142. int sys$sigaction(int signum, const sigaction* act, sigaction* old_act);
  143. int sys$sigprocmask(int how, const sigset_t* set, sigset_t* old_set);
  144. int sys$sigpending(sigset_t*);
  145. int sys$getgroups(ssize_t, gid_t*);
  146. int sys$setgroups(ssize_t, const gid_t*);
  147. int sys$pipe(int pipefd[2], int flags);
  148. int sys$killpg(int pgrp, int sig);
  149. int sys$setgid(gid_t);
  150. int sys$setuid(uid_t);
  151. unsigned sys$alarm(unsigned seconds);
  152. int sys$access(const char* pathname, size_t path_length, int mode);
  153. int sys$fcntl(int fd, int cmd, u32 extra_arg);
  154. int sys$ioctl(int fd, unsigned request, unsigned arg);
  155. int sys$mkdir(const char* pathname, size_t path_length, mode_t mode);
  156. clock_t sys$times(tms*);
  157. int sys$utime(const char* pathname, const struct utimbuf*);
  158. int sys$link(const char* old_path, const char* new_path);
  159. int sys$unlink(const char* pathname);
  160. int sys$symlink(const char* target, const char* linkpath);
  161. int sys$rmdir(const char* pathname, size_t path_length);
  162. int sys$mount(const char* device, const char* mountpoint, const char* fstype);
  163. int sys$umount(const char* mountpoint);
  164. int sys$chmod(const char* pathname, size_t path_length, mode_t);
  165. int sys$fchmod(int fd, mode_t);
  166. int sys$chown(const char* pathname, uid_t, gid_t);
  167. int sys$fchown(int fd, uid_t, gid_t);
  168. int sys$socket(int domain, int type, int protocol);
  169. int sys$bind(int sockfd, const sockaddr* addr, socklen_t);
  170. int sys$listen(int sockfd, int backlog);
  171. int sys$accept(int sockfd, sockaddr*, socklen_t*);
  172. int sys$connect(int sockfd, const sockaddr*, socklen_t);
  173. ssize_t sys$sendto(const Syscall::SC_sendto_params*);
  174. ssize_t sys$recvfrom(const Syscall::SC_recvfrom_params*);
  175. int sys$getsockopt(const Syscall::SC_getsockopt_params*);
  176. int sys$setsockopt(const Syscall::SC_setsockopt_params*);
  177. int sys$getsockname(int sockfd, sockaddr* addr, socklen_t* addrlen);
  178. int sys$getpeername(int sockfd, sockaddr* addr, socklen_t* addrlen);
  179. int sys$sched_setparam(pid_t pid, const struct sched_param* param);
  180. int sys$sched_getparam(pid_t pid, struct sched_param* param);
  181. int sys$restore_signal_mask(u32 mask);
  182. int sys$create_thread(void* (*)(void*), void* argument, const Syscall::SC_create_thread_params*);
  183. void sys$exit_thread(void*);
  184. int sys$join_thread(int tid, void** exit_value);
  185. int sys$detach_thread(int tid);
  186. int sys$set_thread_name(int tid, const char* buffer, int buffer_size);
  187. int sys$get_thread_name(int tid, char* buffer, int buffer_size);
  188. int sys$rename(const char* oldpath, const char* newpath);
  189. int sys$systrace(pid_t);
  190. int sys$mknod(const char* pathname, mode_t, dev_t);
  191. int sys$create_shared_buffer(int, void** buffer);
  192. int sys$share_buffer_with(int, pid_t peer_pid);
  193. int sys$share_buffer_globally(int);
  194. void* sys$get_shared_buffer(int shared_buffer_id);
  195. int sys$release_shared_buffer(int shared_buffer_id);
  196. int sys$seal_shared_buffer(int shared_buffer_id);
  197. int sys$get_shared_buffer_size(int shared_buffer_id);
  198. int sys$set_shared_buffer_volatile(int shared_buffer_id, bool);
  199. int sys$halt();
  200. int sys$reboot();
  201. int sys$set_process_icon(int icon_id);
  202. int sys$realpath(const Syscall::SC_realpath_params*);
  203. ssize_t sys$getrandom(void*, size_t, unsigned int);
  204. int sys$setkeymap(const Syscall::SC_setkeymap_params*);
  205. int sys$module_load(const char* path, size_t path_length);
  206. int sys$module_unload(const char* name, size_t name_length);
  207. int sys$profiling_enable(pid_t);
  208. int sys$profiling_disable(pid_t);
  209. void* sys$get_kernel_info_page();
  210. int sys$futex(const Syscall::SC_futex_params*);
  211. int sys$set_thread_boost(int tid, int amount);
  212. int sys$set_process_boost(pid_t, int amount);
  213. static void initialize();
  214. [[noreturn]] void crash(int signal, u32 eip);
  215. [[nodiscard]] static int reap(Process&);
  216. const TTY* tty() const { return m_tty; }
  217. void set_tty(TTY* tty) { m_tty = tty; }
  218. size_t region_count() const { return m_regions.size(); }
  219. const NonnullOwnPtrVector<Region>& regions() const { return m_regions; }
  220. void dump_regions();
  221. ProcessTracer* tracer() { return m_tracer.ptr(); }
  222. ProcessTracer& ensure_tracer();
  223. u32 m_ticks_in_user { 0 };
  224. u32 m_ticks_in_kernel { 0 };
  225. u32 m_ticks_in_user_for_dead_children { 0 };
  226. u32 m_ticks_in_kernel_for_dead_children { 0 };
  227. bool validate_read_from_kernel(VirtualAddress, ssize_t) const;
  228. bool validate_read(const void*, ssize_t) const;
  229. bool validate_write(void*, ssize_t) const;
  230. bool validate_read_str(const char* str);
  231. template<typename T>
  232. bool validate_read_typed(T* value, size_t count = 1) { return validate_read(value, sizeof(T) * count); }
  233. template<typename T>
  234. bool validate_write_typed(T* value, size_t count = 1) { return validate_write(value, sizeof(T) * count); }
  235. Custody& current_directory();
  236. Custody* executable() { return m_executable.ptr(); }
  237. int number_of_open_file_descriptors() const;
  238. int max_open_file_descriptors() const { return m_max_open_file_descriptors; }
  239. size_t amount_clean_inode() const;
  240. size_t amount_dirty_private() const;
  241. size_t amount_virtual() const;
  242. size_t amount_resident() const;
  243. size_t amount_shared() const;
  244. size_t amount_purgeable_volatile() const;
  245. size_t amount_purgeable_nonvolatile() const;
  246. int exec(String path, Vector<String> arguments, Vector<String> environment);
  247. bool is_superuser() const { return m_euid == 0; }
  248. Region* allocate_region_with_vmobject(VirtualAddress, size_t, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const String& name, int prot);
  249. Region* allocate_file_backed_region(VirtualAddress, size_t, NonnullRefPtr<Inode>, const String& name, int prot);
  250. Region* allocate_region(VirtualAddress, size_t, const String& name, int prot = PROT_READ | PROT_WRITE, bool commit = true);
  251. bool deallocate_region(Region& region);
  252. Region& allocate_split_region(const Region& source_region, const Range&, size_t offset_in_vmobject);
  253. Vector<Region*, 2> split_region_around_range(const Region& source_region, const Range&);
  254. void set_being_inspected(bool b) { m_being_inspected = b; }
  255. bool is_being_inspected() const { return m_being_inspected; }
  256. void terminate_due_to_signal(u8 signal);
  257. void send_signal(u8, Process* sender);
  258. u16 thread_count() const { return m_thread_count; }
  259. Thread& any_thread();
  260. Lock& big_lock() { return m_big_lock; }
  261. const ELFLoader* elf_loader() const { return m_elf_loader.ptr(); }
  262. int icon_id() const { return m_icon_id; }
  263. u32 priority_boost() const { return m_priority_boost; }
  264. private:
  265. friend class MemoryManager;
  266. friend class Scheduler;
  267. friend class Region;
  268. Process(Thread*& first_thread, const String& name, uid_t, gid_t, pid_t ppid, RingLevel, RefPtr<Custody> cwd = nullptr, RefPtr<Custody> executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr);
  269. static pid_t allocate_pid();
  270. Range allocate_range(VirtualAddress, size_t);
  271. int do_exec(String path, Vector<String> arguments, Vector<String> environment);
  272. ssize_t do_write(FileDescription&, const u8*, int data_size);
  273. int alloc_fd(int first_candidate_fd = 0);
  274. void disown_all_shared_buffers();
  275. KResultOr<Vector<String>> find_shebang_interpreter_for_executable(const String& executable_path);
  276. KResult do_kill(Process&, int signal);
  277. KResult do_killpg(pid_t pgrp, int signal);
  278. KResultOr<String> get_syscall_path_argument(const char* user_path, size_t path_length);
  279. RefPtr<PageDirectory> m_page_directory;
  280. Process* m_prev { nullptr };
  281. Process* m_next { nullptr };
  282. String m_name;
  283. pid_t m_pid { 0 };
  284. uid_t m_uid { 0 };
  285. gid_t m_gid { 0 };
  286. uid_t m_euid { 0 };
  287. gid_t m_egid { 0 };
  288. pid_t m_sid { 0 };
  289. pid_t m_pgid { 0 };
  290. static const int m_max_open_file_descriptors { FD_SETSIZE };
  291. struct FileDescriptionAndFlags {
  292. operator bool() const { return !!description; }
  293. void clear();
  294. void set(NonnullRefPtr<FileDescription>&& d, u32 f = 0);
  295. RefPtr<FileDescription> description;
  296. u32 flags { 0 };
  297. };
  298. Vector<FileDescriptionAndFlags> m_fds;
  299. RingLevel m_ring { Ring0 };
  300. u8 m_termination_status { 0 };
  301. u8 m_termination_signal { 0 };
  302. u16 m_thread_count { 0 };
  303. bool m_being_inspected { false };
  304. bool m_dead { false };
  305. bool m_profiling { false };
  306. RefPtr<Custody> m_executable;
  307. RefPtr<Custody> m_cwd;
  308. RefPtr<TTY> m_tty;
  309. Region* region_from_range(const Range&);
  310. Region* region_containing(const Range&);
  311. NonnullOwnPtrVector<Region> m_regions;
  312. pid_t m_ppid { 0 };
  313. mode_t m_umask { 022 };
  314. HashTable<gid_t> m_extra_gids;
  315. RefPtr<ProcessTracer> m_tracer;
  316. OwnPtr<ELFLoader> m_elf_loader;
  317. Region* m_master_tls_region { nullptr };
  318. size_t m_master_tls_size { 0 };
  319. size_t m_master_tls_alignment { 0 };
  320. Lock m_big_lock { "Process" };
  321. u64 m_alarm_deadline { 0 };
  322. int m_icon_id { -1 };
  323. u32 m_priority_boost { 0 };
  324. WaitQueue& futex_queue(i32*);
  325. HashMap<u32, OwnPtr<WaitQueue>> m_futex_queues;
  326. };
  327. class ProcessInspectionHandle {
  328. public:
  329. ProcessInspectionHandle(Process& process)
  330. : m_process(process)
  331. {
  332. if (&process != &current->process()) {
  333. ASSERT(!m_process.is_being_inspected());
  334. m_process.set_being_inspected(true);
  335. }
  336. }
  337. ~ProcessInspectionHandle()
  338. {
  339. m_process.set_being_inspected(false);
  340. }
  341. Process& process() { return m_process; }
  342. static OwnPtr<ProcessInspectionHandle> from_pid(pid_t pid)
  343. {
  344. InterruptDisabler disabler;
  345. auto* process = Process::from_pid(pid);
  346. if (process)
  347. return make<ProcessInspectionHandle>(*process);
  348. return nullptr;
  349. }
  350. Process* operator->() { return &m_process; }
  351. Process& operator*() { return m_process; }
  352. private:
  353. Process& m_process;
  354. };
  355. extern InlineLinkedList<Process>* g_processes;
  356. template<typename Callback>
  357. inline void Process::for_each(Callback callback)
  358. {
  359. ASSERT_INTERRUPTS_DISABLED();
  360. for (auto* process = g_processes->head(); process;) {
  361. auto* next_process = process->next();
  362. if (callback(*process) == IterationDecision::Break)
  363. break;
  364. process = next_process;
  365. }
  366. }
  367. template<typename Callback>
  368. inline void Process::for_each_child(Callback callback)
  369. {
  370. ASSERT_INTERRUPTS_DISABLED();
  371. pid_t my_pid = pid();
  372. for (auto* process = g_processes->head(); process;) {
  373. auto* next_process = process->next();
  374. if (process->ppid() == my_pid) {
  375. if (callback(*process) == IterationDecision::Break)
  376. break;
  377. }
  378. process = next_process;
  379. }
  380. }
  381. template<typename Callback>
  382. inline void Process::for_each_thread(Callback callback) const
  383. {
  384. InterruptDisabler disabler;
  385. pid_t my_pid = pid();
  386. if (my_pid == 0) {
  387. // NOTE: Special case the colonel process, since its main thread is not in the global thread table.
  388. callback(*g_colonel);
  389. return;
  390. }
  391. Thread::for_each([callback, my_pid](Thread& thread) -> IterationDecision {
  392. if (thread.pid() == my_pid)
  393. return callback(thread);
  394. return IterationDecision::Continue;
  395. });
  396. }
  397. template<typename Callback>
  398. inline void Process::for_each_in_pgrp(pid_t pgid, Callback callback)
  399. {
  400. ASSERT_INTERRUPTS_DISABLED();
  401. for (auto* process = g_processes->head(); process;) {
  402. auto* next_process = process->next();
  403. if (!process->is_dead() && process->pgid() == pgid) {
  404. if (callback(*process) == IterationDecision::Break)
  405. break;
  406. }
  407. process = next_process;
  408. }
  409. }
  410. inline bool InodeMetadata::may_read(Process& process) const
  411. {
  412. return may_read(process.euid(), process.egid(), process.extra_gids());
  413. }
  414. inline bool InodeMetadata::may_write(Process& process) const
  415. {
  416. return may_write(process.euid(), process.egid(), process.extra_gids());
  417. }
  418. inline bool InodeMetadata::may_execute(Process& process) const
  419. {
  420. return may_execute(process.euid(), process.egid(), process.extra_gids());
  421. }
  422. inline int Thread::pid() const
  423. {
  424. return m_process.pid();
  425. }
  426. inline const LogStream& operator<<(const LogStream& stream, const Process& process)
  427. {
  428. return stream << process.name() << '(' << process.pid() << ')';
  429. }
  430. inline u32 Thread::effective_priority() const
  431. {
  432. return m_priority + m_process.priority_boost() + m_priority_boost + m_extra_priority;
  433. }