Process.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/FixedArray.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/InlineLinkedList.h>
  30. #include <AK/NonnullOwnPtrVector.h>
  31. #include <AK/String.h>
  32. #include <AK/WeakPtr.h>
  33. #include <Kernel/FileSystem/InodeMetadata.h>
  34. #include <Kernel/Forward.h>
  35. #include <Kernel/Lock.h>
  36. #include <Kernel/Syscall.h>
  37. #include <Kernel/Thread.h>
  38. #include <Kernel/UnixTypes.h>
  39. #include <Kernel/VM/RangeAllocator.h>
  40. #include <LibC/signal_numbers.h>
  41. class ELFLoader;
  42. namespace Kernel {
  43. timeval kgettimeofday();
  44. void kgettimeofday(timeval&);
  45. extern VirtualAddress g_return_to_ring3_from_signal_trampoline;
  46. #define ENUMERATE_PLEDGE_PROMISES \
  47. __ENUMERATE_PLEDGE_PROMISE(stdio) \
  48. __ENUMERATE_PLEDGE_PROMISE(rpath) \
  49. __ENUMERATE_PLEDGE_PROMISE(wpath) \
  50. __ENUMERATE_PLEDGE_PROMISE(cpath) \
  51. __ENUMERATE_PLEDGE_PROMISE(dpath) \
  52. __ENUMERATE_PLEDGE_PROMISE(inet) \
  53. __ENUMERATE_PLEDGE_PROMISE(id) \
  54. __ENUMERATE_PLEDGE_PROMISE(proc) \
  55. __ENUMERATE_PLEDGE_PROMISE(exec) \
  56. __ENUMERATE_PLEDGE_PROMISE(unix) \
  57. __ENUMERATE_PLEDGE_PROMISE(fattr) \
  58. __ENUMERATE_PLEDGE_PROMISE(tty) \
  59. __ENUMERATE_PLEDGE_PROMISE(chown) \
  60. __ENUMERATE_PLEDGE_PROMISE(chroot) \
  61. __ENUMERATE_PLEDGE_PROMISE(thread) \
  62. __ENUMERATE_PLEDGE_PROMISE(video) \
  63. __ENUMERATE_PLEDGE_PROMISE(accept) \
  64. __ENUMERATE_PLEDGE_PROMISE(shared_buffer)
  65. enum class Pledge : u32 {
  66. #define __ENUMERATE_PLEDGE_PROMISE(x) x,
  67. ENUMERATE_PLEDGE_PROMISES
  68. #undef __ENUMERATE_PLEDGE_PROMISE
  69. };
  70. enum class VeilState {
  71. None,
  72. Dropped,
  73. Locked,
  74. };
  75. struct UnveiledPath {
  76. enum Access {
  77. Read = 1,
  78. Write = 2,
  79. Execute = 4,
  80. CreateOrRemove = 8,
  81. };
  82. String path;
  83. unsigned permissions { 0 };
  84. };
  85. class Process : public InlineLinkedListNode<Process> {
  86. friend class InlineLinkedListNode<Process>;
  87. friend class Thread;
  88. public:
  89. static Process* current;
  90. static Process* create_kernel_process(Thread*& first_thread, String&& name, void (*entry)());
  91. 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);
  92. ~Process();
  93. static Vector<pid_t> all_pids();
  94. static Vector<Process*> all_processes();
  95. bool is_profiling() const { return m_profiling; }
  96. void set_profiling(bool profiling) { m_profiling = profiling; }
  97. enum RingLevel : u8 {
  98. Ring0 = 0,
  99. Ring3 = 3,
  100. };
  101. KBuffer backtrace(ProcessInspectionHandle&) const;
  102. bool is_dead() const { return m_dead; }
  103. bool is_ring0() const { return m_ring == Ring0; }
  104. bool is_ring3() const { return m_ring == Ring3; }
  105. PageDirectory& page_directory() { return *m_page_directory; }
  106. const PageDirectory& page_directory() const { return *m_page_directory; }
  107. static Process* from_pid(pid_t);
  108. static void update_info_page_timestamp(const timeval&);
  109. const String& name() const { return m_name; }
  110. pid_t pid() const { return m_pid; }
  111. pid_t sid() const { return m_sid; }
  112. pid_t pgid() const { return m_pgid; }
  113. uid_t uid() const { return m_uid; }
  114. gid_t gid() const { return m_gid; }
  115. const FixedArray<gid_t>& extra_gids() const { return m_extra_gids; }
  116. uid_t euid() const { return m_euid; }
  117. gid_t egid() const { return m_egid; }
  118. pid_t ppid() const { return m_ppid; }
  119. pid_t exec_tid() const { return m_exec_tid; }
  120. mode_t umask() const { return m_umask; }
  121. bool in_group(gid_t) const;
  122. RefPtr<FileDescription> file_description(int fd) const;
  123. int fd_flags(int fd) const;
  124. template<typename Callback>
  125. static void for_each(Callback);
  126. template<typename Callback>
  127. static void for_each_in_pgrp(pid_t, Callback);
  128. template<typename Callback>
  129. void for_each_child(Callback);
  130. template<typename Callback>
  131. void for_each_thread(Callback) const;
  132. void die();
  133. void finalize();
  134. int sys$yield();
  135. int sys$sync();
  136. int sys$beep();
  137. int sys$get_process_name(char* buffer, int buffer_size);
  138. int sys$watch_file(const char* path, size_t path_length);
  139. int sys$dbgputch(u8);
  140. int sys$dbgputstr(const u8*, int length);
  141. int sys$dump_backtrace();
  142. int sys$gettid();
  143. int sys$donate(int tid);
  144. int sys$ftruncate(int fd, off_t);
  145. pid_t sys$setsid();
  146. pid_t sys$getsid(pid_t);
  147. int sys$setpgid(pid_t pid, pid_t pgid);
  148. pid_t sys$getpgrp();
  149. pid_t sys$getpgid(pid_t);
  150. uid_t sys$getuid();
  151. gid_t sys$getgid();
  152. uid_t sys$geteuid();
  153. gid_t sys$getegid();
  154. pid_t sys$getpid();
  155. pid_t sys$getppid();
  156. mode_t sys$umask(mode_t);
  157. int sys$open(const Syscall::SC_open_params*);
  158. int sys$close(int fd);
  159. ssize_t sys$read(int fd, u8*, ssize_t);
  160. ssize_t sys$write(int fd, const u8*, ssize_t);
  161. ssize_t sys$writev(int fd, const struct iovec* iov, int iov_count);
  162. int sys$fstat(int fd, stat*);
  163. int sys$stat(const Syscall::SC_stat_params*);
  164. int sys$lseek(int fd, off_t, int whence);
  165. int sys$kill(pid_t pid, int sig);
  166. [[noreturn]] void sys$exit(int status);
  167. int sys$sigreturn(RegisterState& registers);
  168. pid_t sys$waitid(const Syscall::SC_waitid_params*);
  169. void* sys$mmap(const Syscall::SC_mmap_params*);
  170. int sys$munmap(void*, size_t size);
  171. int sys$set_mmap_name(const Syscall::SC_set_mmap_name_params*);
  172. int sys$mprotect(void*, size_t, int prot);
  173. int sys$madvise(void*, size_t, int advice);
  174. int sys$purge(int mode);
  175. int sys$select(const Syscall::SC_select_params*);
  176. int sys$poll(pollfd*, int nfds, int timeout);
  177. ssize_t sys$get_dir_entries(int fd, void*, ssize_t);
  178. int sys$getcwd(char*, ssize_t);
  179. int sys$chdir(const char*, size_t);
  180. int sys$fchdir(int fd);
  181. int sys$sleep(unsigned seconds);
  182. int sys$usleep(useconds_t usec);
  183. int sys$gettimeofday(timeval*);
  184. int sys$clock_gettime(clockid_t, timespec*);
  185. int sys$clock_nanosleep(const Syscall::SC_clock_nanosleep_params*);
  186. int sys$gethostname(char*, ssize_t);
  187. int sys$uname(utsname*);
  188. int sys$readlink(const Syscall::SC_readlink_params*);
  189. int sys$ttyname_r(int fd, char*, ssize_t);
  190. int sys$ptsname_r(int fd, char*, ssize_t);
  191. pid_t sys$fork(RegisterState&);
  192. int sys$execve(const Syscall::SC_execve_params*);
  193. int sys$getdtablesize();
  194. int sys$dup(int oldfd);
  195. int sys$dup2(int oldfd, int newfd);
  196. int sys$sigaction(int signum, const sigaction* act, sigaction* old_act);
  197. int sys$sigprocmask(int how, const sigset_t* set, sigset_t* old_set);
  198. int sys$sigpending(sigset_t*);
  199. int sys$getgroups(ssize_t, gid_t*);
  200. int sys$setgroups(ssize_t, const gid_t*);
  201. int sys$pipe(int pipefd[2], int flags);
  202. int sys$killpg(int pgrp, int sig);
  203. int sys$setgid(gid_t);
  204. int sys$setuid(uid_t);
  205. unsigned sys$alarm(unsigned seconds);
  206. int sys$access(const char* pathname, size_t path_length, int mode);
  207. int sys$fcntl(int fd, int cmd, u32 extra_arg);
  208. int sys$ioctl(int fd, unsigned request, unsigned arg);
  209. int sys$mkdir(const char* pathname, size_t path_length, mode_t mode);
  210. clock_t sys$times(tms*);
  211. int sys$utime(const char* pathname, size_t path_length, const struct utimbuf*);
  212. int sys$link(const Syscall::SC_link_params*);
  213. int sys$unlink(const char* pathname, size_t path_length);
  214. int sys$symlink(const Syscall::SC_symlink_params*);
  215. int sys$rmdir(const char* pathname, size_t path_length);
  216. int sys$mount(const Syscall::SC_mount_params*);
  217. int sys$umount(const char* mountpoint, size_t mountpoint_length);
  218. int sys$chmod(const char* pathname, size_t path_length, mode_t);
  219. int sys$fchmod(int fd, mode_t);
  220. int sys$chown(const Syscall::SC_chown_params*);
  221. int sys$fchown(int fd, uid_t, gid_t);
  222. int sys$socket(int domain, int type, int protocol);
  223. int sys$bind(int sockfd, const sockaddr* addr, socklen_t);
  224. int sys$listen(int sockfd, int backlog);
  225. int sys$accept(int sockfd, sockaddr*, socklen_t*);
  226. int sys$connect(int sockfd, const sockaddr*, socklen_t);
  227. int sys$shutdown(int sockfd, int how);
  228. ssize_t sys$sendto(const Syscall::SC_sendto_params*);
  229. ssize_t sys$recvfrom(const Syscall::SC_recvfrom_params*);
  230. int sys$getsockopt(const Syscall::SC_getsockopt_params*);
  231. int sys$setsockopt(const Syscall::SC_setsockopt_params*);
  232. int sys$getsockname(const Syscall::SC_getsockname_params*);
  233. int sys$getpeername(const Syscall::SC_getpeername_params*);
  234. int sys$sched_setparam(pid_t pid, const struct sched_param* param);
  235. int sys$sched_getparam(pid_t pid, struct sched_param* param);
  236. int sys$create_thread(void* (*)(void*), void* argument, const Syscall::SC_create_thread_params*);
  237. void sys$exit_thread(void*);
  238. int sys$join_thread(int tid, void** exit_value);
  239. int sys$detach_thread(int tid);
  240. int sys$set_thread_name(int tid, const char* buffer, size_t buffer_size);
  241. int sys$get_thread_name(int tid, char* buffer, size_t buffer_size);
  242. int sys$rename(const Syscall::SC_rename_params*);
  243. int sys$systrace(pid_t);
  244. int sys$mknod(const Syscall::SC_mknod_params*);
  245. int sys$shbuf_create(int, void** buffer);
  246. int sys$shbuf_allow_pid(int, pid_t peer_pid);
  247. int sys$shbuf_allow_all(int);
  248. void* sys$shbuf_get(int shbuf_id);
  249. int sys$shbuf_release(int shbuf_id);
  250. int sys$shbuf_seal(int shbuf_id);
  251. int sys$shbuf_get_size(int shbuf_id);
  252. int sys$shbuf_set_volatile(int shbuf_id, bool);
  253. int sys$halt();
  254. int sys$reboot();
  255. int sys$set_process_icon(int icon_id);
  256. int sys$realpath(const Syscall::SC_realpath_params*);
  257. ssize_t sys$getrandom(void*, size_t, unsigned int);
  258. int sys$setkeymap(const Syscall::SC_setkeymap_params*);
  259. int sys$module_load(const char* path, size_t path_length);
  260. int sys$module_unload(const char* name, size_t name_length);
  261. int sys$profiling_enable(pid_t);
  262. int sys$profiling_disable(pid_t);
  263. void* sys$get_kernel_info_page();
  264. int sys$futex(const Syscall::SC_futex_params*);
  265. int sys$set_thread_boost(int tid, int amount);
  266. int sys$set_process_boost(pid_t, int amount);
  267. int sys$chroot(const char* path, size_t path_length, int mount_flags);
  268. int sys$pledge(const Syscall::SC_pledge_params*);
  269. int sys$unveil(const Syscall::SC_unveil_params*);
  270. int sys$perf_event(int type, uintptr_t arg1, uintptr_t arg2);
  271. template<bool sockname, typename Params>
  272. int get_sock_or_peer_name(const Params&);
  273. static void initialize();
  274. [[noreturn]] void crash(int signal, u32 eip);
  275. [[nodiscard]] static siginfo_t reap(Process&);
  276. const TTY* tty() const { return m_tty; }
  277. void set_tty(TTY*);
  278. size_t region_count() const { return m_regions.size(); }
  279. const NonnullOwnPtrVector<Region>& regions() const { return m_regions; }
  280. void dump_regions();
  281. ProcessTracer* tracer() { return m_tracer.ptr(); }
  282. ProcessTracer& ensure_tracer();
  283. u32 m_ticks_in_user { 0 };
  284. u32 m_ticks_in_kernel { 0 };
  285. u32 m_ticks_in_user_for_dead_children { 0 };
  286. u32 m_ticks_in_kernel_for_dead_children { 0 };
  287. bool validate_read_from_kernel(VirtualAddress, size_t) const;
  288. bool validate_read(const void*, size_t) const;
  289. bool validate_write(void*, size_t) const;
  290. template<typename T>
  291. bool validate_read_typed(T* value, size_t count = 1) { return validate_read(value, sizeof(T) * count); }
  292. template<typename T>
  293. bool validate_read_and_copy_typed(T* dest, const T* src)
  294. {
  295. bool validated = validate_read_typed(src);
  296. if (validated) {
  297. copy_from_user(dest, src);
  298. }
  299. return validated;
  300. }
  301. template<typename T>
  302. bool validate_write_typed(T* value, size_t count = 1) { return validate_write(value, sizeof(T) * count); }
  303. template<typename DataType, typename SizeType>
  304. bool validate(const Syscall::MutableBufferArgument<DataType, SizeType>&);
  305. template<typename DataType, typename SizeType>
  306. bool validate(const Syscall::ImmutableBufferArgument<DataType, SizeType>&);
  307. String validate_and_copy_string_from_user(const char*, size_t) const;
  308. String validate_and_copy_string_from_user(const Syscall::StringArgument&) const;
  309. Custody& current_directory();
  310. Custody* executable() { return m_executable.ptr(); }
  311. int number_of_open_file_descriptors() const;
  312. int max_open_file_descriptors() const { return m_max_open_file_descriptors; }
  313. size_t amount_clean_inode() const;
  314. size_t amount_dirty_private() const;
  315. size_t amount_virtual() const;
  316. size_t amount_resident() const;
  317. size_t amount_shared() const;
  318. size_t amount_purgeable_volatile() const;
  319. size_t amount_purgeable_nonvolatile() const;
  320. int exec(String path, Vector<String> arguments, Vector<String> environment, int recusion_depth = 0);
  321. bool is_superuser() const { return m_euid == 0; }
  322. Region* allocate_region_with_vmobject(VirtualAddress, size_t, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const String& name, int prot, bool user_accessible = true);
  323. Region* allocate_file_backed_region(VirtualAddress, size_t, NonnullRefPtr<Inode>, const String& name, int prot);
  324. Region* allocate_region(VirtualAddress, size_t, const String& name, int prot = PROT_READ | PROT_WRITE, bool commit = true);
  325. Region* allocate_region_with_vmobject(const Range&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const String& name, int prot, bool user_accessible = true);
  326. Region* allocate_region(const Range&, const String& name, int prot = PROT_READ | PROT_WRITE, bool commit = true);
  327. bool deallocate_region(Region& region);
  328. Region& allocate_split_region(const Region& source_region, const Range&, size_t offset_in_vmobject);
  329. Vector<Region*, 2> split_region_around_range(const Region& source_region, const Range&);
  330. bool is_being_inspected() const { return m_inspector_count; }
  331. void terminate_due_to_signal(u8 signal);
  332. void send_signal(u8, Process* sender);
  333. u16 thread_count() const { return m_thread_count; }
  334. Thread& any_thread();
  335. Lock& big_lock() { return m_big_lock; }
  336. const ELFLoader* elf_loader() const { return m_elf_loader.ptr(); }
  337. int icon_id() const { return m_icon_id; }
  338. u32 priority_boost() const { return m_priority_boost; }
  339. Custody& root_directory();
  340. Custody& root_directory_relative_to_global_root();
  341. void set_root_directory(const Custody&);
  342. bool has_promises() const { return m_promises; }
  343. bool has_promised(Pledge pledge) const { return m_promises & (1u << (u32)pledge); }
  344. VeilState veil_state() const { return m_veil_state; }
  345. const Vector<UnveiledPath>& unveiled_paths() const { return m_unveiled_paths; }
  346. void increment_inspector_count(Badge<ProcessInspectionHandle>) { ++m_inspector_count; }
  347. void decrement_inspector_count(Badge<ProcessInspectionHandle>) { --m_inspector_count; }
  348. private:
  349. friend class MemoryManager;
  350. friend class Scheduler;
  351. friend class Region;
  352. 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);
  353. static pid_t allocate_pid();
  354. Range allocate_range(VirtualAddress, size_t, size_t alignment = PAGE_SIZE);
  355. Region& add_region(NonnullOwnPtr<Region>);
  356. void kill_threads_except_self();
  357. void kill_all_threads();
  358. int do_exec(NonnullRefPtr<FileDescription> main_program_description, Vector<String> arguments, Vector<String> environment, RefPtr<FileDescription> interpreter_description);
  359. ssize_t do_write(FileDescription&, const u8*, int data_size);
  360. KResultOr<NonnullRefPtr<FileDescription>> find_elf_interpreter_for_executable(const String& path, char (&first_page)[PAGE_SIZE], int nread, size_t file_size);
  361. int alloc_fd(int first_candidate_fd = 0);
  362. void disown_all_shared_buffers();
  363. KResult do_kill(Process&, int signal);
  364. KResult do_killpg(pid_t pgrp, int signal);
  365. KResultOr<siginfo_t> do_waitid(idtype_t idtype, int id, int options);
  366. KResultOr<String> get_syscall_path_argument(const char* user_path, size_t path_length) const;
  367. KResultOr<String> get_syscall_path_argument(const Syscall::StringArgument&) const;
  368. RefPtr<PageDirectory> m_page_directory;
  369. Process* m_prev { nullptr };
  370. Process* m_next { nullptr };
  371. String m_name;
  372. pid_t m_pid { 0 };
  373. uid_t m_uid { 0 };
  374. gid_t m_gid { 0 };
  375. uid_t m_euid { 0 };
  376. gid_t m_egid { 0 };
  377. pid_t m_sid { 0 };
  378. pid_t m_pgid { 0 };
  379. pid_t m_exec_tid { 0 };
  380. static const int m_max_open_file_descriptors { FD_SETSIZE };
  381. struct FileDescriptionAndFlags {
  382. operator bool() const { return !!description; }
  383. void clear();
  384. void set(NonnullRefPtr<FileDescription>&& d, u32 f = 0);
  385. RefPtr<FileDescription> description;
  386. u32 flags { 0 };
  387. };
  388. Vector<FileDescriptionAndFlags> m_fds;
  389. RingLevel m_ring { Ring0 };
  390. u8 m_termination_status { 0 };
  391. u8 m_termination_signal { 0 };
  392. u16 m_thread_count { 0 };
  393. bool m_dead { false };
  394. bool m_profiling { false };
  395. RefPtr<Custody> m_executable;
  396. RefPtr<Custody> m_cwd;
  397. RefPtr<Custody> m_root_directory;
  398. RefPtr<Custody> m_root_directory_relative_to_global_root;
  399. RefPtr<TTY> m_tty;
  400. Region* region_from_range(const Range&);
  401. Region* region_containing(const Range&);
  402. NonnullOwnPtrVector<Region> m_regions;
  403. struct RegionLookupCache {
  404. Range range;
  405. WeakPtr<Region> region;
  406. };
  407. RegionLookupCache m_region_lookup_cache;
  408. pid_t m_ppid { 0 };
  409. mode_t m_umask { 022 };
  410. FixedArray<gid_t> m_extra_gids;
  411. RefPtr<ProcessTracer> m_tracer;
  412. OwnPtr<ELFLoader> m_elf_loader;
  413. Region* m_master_tls_region { nullptr };
  414. size_t m_master_tls_size { 0 };
  415. size_t m_master_tls_alignment { 0 };
  416. Lock m_big_lock { "Process" };
  417. u64 m_alarm_deadline { 0 };
  418. int m_icon_id { -1 };
  419. u32 m_priority_boost { 0 };
  420. u32 m_promises { 0 };
  421. u32 m_execpromises { 0 };
  422. VeilState m_veil_state { VeilState::None };
  423. Vector<UnveiledPath> m_unveiled_paths;
  424. WaitQueue& futex_queue(i32*);
  425. HashMap<u32, OwnPtr<WaitQueue>> m_futex_queues;
  426. OwnPtr<PerformanceEventBuffer> m_perf_event_buffer;
  427. u32 m_inspector_count { 0 };
  428. };
  429. class ProcessInspectionHandle {
  430. public:
  431. ProcessInspectionHandle(Process& process)
  432. : m_process(process)
  433. {
  434. if (&process != Process::current) {
  435. InterruptDisabler disabler;
  436. m_process.increment_inspector_count({});
  437. }
  438. }
  439. ~ProcessInspectionHandle()
  440. {
  441. if (&m_process != Process::current) {
  442. InterruptDisabler disabler;
  443. m_process.decrement_inspector_count({});
  444. }
  445. }
  446. Process& process() { return m_process; }
  447. static OwnPtr<ProcessInspectionHandle> from_pid(pid_t pid)
  448. {
  449. InterruptDisabler disabler;
  450. auto* process = Process::from_pid(pid);
  451. if (process)
  452. return make<ProcessInspectionHandle>(*process);
  453. return nullptr;
  454. }
  455. Process* operator->() { return &m_process; }
  456. Process& operator*() { return m_process; }
  457. private:
  458. Process& m_process;
  459. };
  460. extern InlineLinkedList<Process>* g_processes;
  461. template<typename Callback>
  462. inline void Process::for_each(Callback callback)
  463. {
  464. ASSERT_INTERRUPTS_DISABLED();
  465. for (auto* process = g_processes->head(); process;) {
  466. auto* next_process = process->next();
  467. if (callback(*process) == IterationDecision::Break)
  468. break;
  469. process = next_process;
  470. }
  471. }
  472. template<typename Callback>
  473. inline void Process::for_each_child(Callback callback)
  474. {
  475. ASSERT_INTERRUPTS_DISABLED();
  476. pid_t my_pid = pid();
  477. for (auto* process = g_processes->head(); process;) {
  478. auto* next_process = process->next();
  479. if (process->ppid() == my_pid) {
  480. if (callback(*process) == IterationDecision::Break)
  481. break;
  482. }
  483. process = next_process;
  484. }
  485. }
  486. template<typename Callback>
  487. inline void Process::for_each_thread(Callback callback) const
  488. {
  489. InterruptDisabler disabler;
  490. pid_t my_pid = pid();
  491. if (my_pid == 0) {
  492. // NOTE: Special case the colonel process, since its main thread is not in the global thread table.
  493. callback(*g_colonel);
  494. return;
  495. }
  496. Thread::for_each([callback, my_pid](Thread& thread) -> IterationDecision {
  497. if (thread.pid() == my_pid)
  498. return callback(thread);
  499. return IterationDecision::Continue;
  500. });
  501. }
  502. template<typename Callback>
  503. inline void Process::for_each_in_pgrp(pid_t pgid, Callback callback)
  504. {
  505. ASSERT_INTERRUPTS_DISABLED();
  506. for (auto* process = g_processes->head(); process;) {
  507. auto* next_process = process->next();
  508. if (!process->is_dead() && process->pgid() == pgid) {
  509. if (callback(*process) == IterationDecision::Break)
  510. break;
  511. }
  512. process = next_process;
  513. }
  514. }
  515. inline bool InodeMetadata::may_read(const Process& process) const
  516. {
  517. return may_read(process.euid(), process.egid(), process.extra_gids());
  518. }
  519. inline bool InodeMetadata::may_write(const Process& process) const
  520. {
  521. return may_write(process.euid(), process.egid(), process.extra_gids());
  522. }
  523. inline bool InodeMetadata::may_execute(const Process& process) const
  524. {
  525. return may_execute(process.euid(), process.egid(), process.extra_gids());
  526. }
  527. inline int Thread::pid() const
  528. {
  529. return m_process.pid();
  530. }
  531. inline const LogStream& operator<<(const LogStream& stream, const Process& process)
  532. {
  533. return stream << process.name() << '(' << process.pid() << ')';
  534. }
  535. inline u32 Thread::effective_priority() const
  536. {
  537. return m_priority + m_process.priority_boost() + m_priority_boost + m_extra_priority;
  538. }
  539. #define REQUIRE_NO_PROMISES \
  540. do { \
  541. if (Process::current->has_promises()) { \
  542. dbg() << "Has made a promise"; \
  543. cli(); \
  544. Process::current->crash(SIGABRT, 0); \
  545. ASSERT_NOT_REACHED(); \
  546. } \
  547. } while (0)
  548. #define REQUIRE_PROMISE(promise) \
  549. do { \
  550. if (Process::current->has_promises() \
  551. && !Process::current->has_promised(Pledge::promise)) { \
  552. dbg() << "Has not pledged " << #promise; \
  553. cli(); \
  554. Process::current->crash(SIGABRT, 0); \
  555. ASSERT_NOT_REACHED(); \
  556. } \
  557. } while (0)
  558. }