Process.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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/Checked.h>
  28. #include <AK/FixedArray.h>
  29. #include <AK/HashMap.h>
  30. #include <AK/InlineLinkedList.h>
  31. #include <AK/NonnullOwnPtrVector.h>
  32. #include <AK/NonnullRefPtrVector.h>
  33. #include <AK/String.h>
  34. #include <AK/Userspace.h>
  35. #include <AK/WeakPtr.h>
  36. #include <Kernel/API/Syscall.h>
  37. #include <Kernel/FileSystem/InodeMetadata.h>
  38. #include <Kernel/Forward.h>
  39. #include <Kernel/Lock.h>
  40. #include <Kernel/StdLib.h>
  41. #include <Kernel/Thread.h>
  42. #include <Kernel/UnixTypes.h>
  43. #include <Kernel/VM/RangeAllocator.h>
  44. #include <LibC/signal_numbers.h>
  45. #include <LibELF/AuxiliaryVector.h>
  46. namespace ELF {
  47. class Loader;
  48. }
  49. namespace Kernel {
  50. timeval kgettimeofday();
  51. void kgettimeofday(timeval&);
  52. extern VirtualAddress g_return_to_ring3_from_signal_trampoline;
  53. #define ENUMERATE_PLEDGE_PROMISES \
  54. __ENUMERATE_PLEDGE_PROMISE(stdio) \
  55. __ENUMERATE_PLEDGE_PROMISE(rpath) \
  56. __ENUMERATE_PLEDGE_PROMISE(wpath) \
  57. __ENUMERATE_PLEDGE_PROMISE(cpath) \
  58. __ENUMERATE_PLEDGE_PROMISE(dpath) \
  59. __ENUMERATE_PLEDGE_PROMISE(inet) \
  60. __ENUMERATE_PLEDGE_PROMISE(id) \
  61. __ENUMERATE_PLEDGE_PROMISE(proc) \
  62. __ENUMERATE_PLEDGE_PROMISE(exec) \
  63. __ENUMERATE_PLEDGE_PROMISE(unix) \
  64. __ENUMERATE_PLEDGE_PROMISE(recvfd) \
  65. __ENUMERATE_PLEDGE_PROMISE(sendfd) \
  66. __ENUMERATE_PLEDGE_PROMISE(fattr) \
  67. __ENUMERATE_PLEDGE_PROMISE(tty) \
  68. __ENUMERATE_PLEDGE_PROMISE(chown) \
  69. __ENUMERATE_PLEDGE_PROMISE(chroot) \
  70. __ENUMERATE_PLEDGE_PROMISE(thread) \
  71. __ENUMERATE_PLEDGE_PROMISE(video) \
  72. __ENUMERATE_PLEDGE_PROMISE(accept) \
  73. __ENUMERATE_PLEDGE_PROMISE(settime) \
  74. __ENUMERATE_PLEDGE_PROMISE(sigaction) \
  75. __ENUMERATE_PLEDGE_PROMISE(setkeymap) \
  76. __ENUMERATE_PLEDGE_PROMISE(shared_buffer)
  77. enum class Pledge : u32 {
  78. #define __ENUMERATE_PLEDGE_PROMISE(x) x,
  79. ENUMERATE_PLEDGE_PROMISES
  80. #undef __ENUMERATE_PLEDGE_PROMISE
  81. };
  82. enum class VeilState {
  83. None,
  84. Dropped,
  85. Locked,
  86. };
  87. struct UnveiledPath {
  88. enum Access {
  89. Read = 1,
  90. Write = 2,
  91. Execute = 4,
  92. CreateOrRemove = 8,
  93. };
  94. String path;
  95. unsigned permissions { 0 };
  96. };
  97. class Process : public RefCounted<Process>, public InlineLinkedListNode<Process> {
  98. AK_MAKE_NONCOPYABLE(Process);
  99. AK_MAKE_NONMOVABLE(Process);
  100. friend class InlineLinkedListNode<Process>;
  101. friend class Thread;
  102. public:
  103. inline static Process* current()
  104. {
  105. auto current_thread = Processor::current().current_thread();
  106. return current_thread ? &current_thread->process() : nullptr;
  107. }
  108. static NonnullRefPtr<Process> create_kernel_process(Thread*& first_thread, String&& name, void (*entry)(), u32 affinity = THREAD_AFFINITY_DEFAULT);
  109. static RefPtr<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);
  110. ~Process();
  111. static Vector<pid_t> all_pids();
  112. static AK::NonnullRefPtrVector<Process> all_processes();
  113. Thread* create_kernel_thread(void (*entry)(), u32 priority, const String& name, u32 affinity = THREAD_AFFINITY_DEFAULT, bool joinable = true);
  114. bool is_profiling() const { return m_profiling; }
  115. void set_profiling(bool profiling) { m_profiling = profiling; }
  116. enum RingLevel : u8 {
  117. Ring0 = 0,
  118. Ring3 = 3,
  119. };
  120. KBuffer backtrace() const;
  121. bool is_dead() const { return m_dead; }
  122. bool is_ring0() const { return m_ring == Ring0; }
  123. bool is_ring3() const { return m_ring == Ring3; }
  124. PageDirectory& page_directory() { return *m_page_directory; }
  125. const PageDirectory& page_directory() const { return *m_page_directory; }
  126. static RefPtr<Process> from_pid(pid_t);
  127. const String& name() const { return m_name; }
  128. pid_t pid() const { return m_pid; }
  129. pid_t sid() const { return m_sid; }
  130. pid_t pgid() const { return m_pgid; }
  131. const FixedArray<gid_t>& extra_gids() const { return m_extra_gids; }
  132. uid_t euid() const { return m_euid; }
  133. gid_t egid() const { return m_egid; }
  134. uid_t uid() const { return m_uid; }
  135. gid_t gid() const { return m_gid; }
  136. uid_t suid() const { return m_suid; }
  137. gid_t sgid() const { return m_sgid; }
  138. pid_t ppid() const { return m_ppid; }
  139. pid_t exec_tid() const { return m_exec_tid; }
  140. mode_t umask() const { return m_umask; }
  141. bool in_group(gid_t) const;
  142. RefPtr<FileDescription> file_description(int fd) const;
  143. int fd_flags(int fd) const;
  144. template<typename Callback>
  145. static void for_each(Callback);
  146. template<typename Callback>
  147. static void for_each_in_pgrp(pid_t, Callback);
  148. template<typename Callback>
  149. void for_each_child(Callback);
  150. template<typename Callback>
  151. void for_each_thread(Callback) const;
  152. void die();
  153. void finalize();
  154. ALWAYS_INLINE SpinLock<u32>& get_lock() const { return m_lock; }
  155. int sys$yield();
  156. int sys$sync();
  157. int sys$beep();
  158. int sys$get_process_name(Userspace<char*> buffer, size_t buffer_size);
  159. int sys$set_process_name(Userspace<const char*> user_name, size_t user_name_length);
  160. int sys$watch_file(Userspace<const char*> path, size_t path_length);
  161. int sys$dbgputch(u8);
  162. int sys$dbgputstr(Userspace<const u8*>, int length);
  163. int sys$dump_backtrace();
  164. int sys$gettid();
  165. int sys$donate(int tid);
  166. int sys$ftruncate(int fd, off_t);
  167. pid_t sys$setsid();
  168. pid_t sys$getsid(pid_t);
  169. int sys$setpgid(pid_t pid, pid_t pgid);
  170. pid_t sys$getpgrp();
  171. pid_t sys$getpgid(pid_t);
  172. uid_t sys$getuid();
  173. gid_t sys$getgid();
  174. uid_t sys$geteuid();
  175. gid_t sys$getegid();
  176. pid_t sys$getpid();
  177. pid_t sys$getppid();
  178. int sys$getresuid(uid_t*, uid_t*, uid_t*);
  179. int sys$getresgid(gid_t*, gid_t*, gid_t*);
  180. mode_t sys$umask(mode_t);
  181. int sys$open(Userspace<const Syscall::SC_open_params*>);
  182. int sys$close(int fd);
  183. ssize_t sys$read(int fd, Userspace<u8*>, ssize_t);
  184. ssize_t sys$write(int fd, const u8*, ssize_t);
  185. ssize_t sys$writev(int fd, const struct iovec* iov, int iov_count);
  186. int sys$fstat(int fd, stat*);
  187. int sys$stat(Userspace<const Syscall::SC_stat_params*>);
  188. int sys$lseek(int fd, off_t, int whence);
  189. int sys$kill(pid_t pid, int sig);
  190. [[noreturn]] void sys$exit(int status);
  191. int sys$sigreturn(RegisterState& registers);
  192. pid_t sys$waitid(const Syscall::SC_waitid_params*);
  193. void* sys$mmap(const Syscall::SC_mmap_params*);
  194. int sys$munmap(void*, size_t size);
  195. int sys$set_mmap_name(const Syscall::SC_set_mmap_name_params*);
  196. int sys$mprotect(void*, size_t, int prot);
  197. int sys$madvise(void*, size_t, int advice);
  198. int sys$minherit(void*, size_t, int inherit);
  199. int sys$purge(int mode);
  200. int sys$select(const Syscall::SC_select_params*);
  201. int sys$poll(const Syscall::SC_poll_params*);
  202. ssize_t sys$get_dir_entries(int fd, void*, ssize_t);
  203. int sys$getcwd(Userspace<char*>, ssize_t);
  204. int sys$chdir(Userspace<const char*>, size_t);
  205. int sys$fchdir(int fd);
  206. int sys$sleep(unsigned seconds);
  207. int sys$usleep(useconds_t usec);
  208. int sys$gettimeofday(timeval*);
  209. int sys$clock_gettime(clockid_t, timespec*);
  210. int sys$clock_settime(clockid_t, timespec*);
  211. int sys$clock_nanosleep(const Syscall::SC_clock_nanosleep_params*);
  212. int sys$gethostname(char*, ssize_t);
  213. int sys$sethostname(const char*, ssize_t);
  214. int sys$uname(utsname*);
  215. int sys$readlink(const Syscall::SC_readlink_params*);
  216. int sys$ttyname(int fd, Userspace<char*>, size_t);
  217. int sys$ptsname(int fd, Userspace<char*>, size_t);
  218. pid_t sys$fork(RegisterState&);
  219. int sys$execve(const Syscall::SC_execve_params*);
  220. int sys$dup(int oldfd);
  221. int sys$dup2(int oldfd, int newfd);
  222. int sys$sigaction(int signum, const sigaction* act, sigaction* old_act);
  223. int sys$sigprocmask(int how, const sigset_t* set, sigset_t* old_set);
  224. int sys$sigpending(sigset_t*);
  225. int sys$getgroups(ssize_t, gid_t*);
  226. int sys$setgroups(ssize_t, const gid_t*);
  227. int sys$pipe(int pipefd[2], int flags);
  228. int sys$killpg(int pgrp, int sig);
  229. int sys$seteuid(uid_t);
  230. int sys$setegid(gid_t);
  231. int sys$setuid(uid_t);
  232. int sys$setgid(gid_t);
  233. int sys$setresuid(uid_t, uid_t, uid_t);
  234. int sys$setresgid(gid_t, gid_t, gid_t);
  235. unsigned sys$alarm(unsigned seconds);
  236. int sys$access(Userspace<const char*> pathname, size_t path_length, int mode);
  237. int sys$fcntl(int fd, int cmd, u32 extra_arg);
  238. int sys$ioctl(int fd, unsigned request, FlatPtr arg);
  239. int sys$mkdir(Userspace<const char*> pathname, size_t path_length, mode_t mode);
  240. clock_t sys$times(tms*);
  241. int sys$utime(Userspace<const char*> pathname, size_t path_length, Userspace<const struct utimbuf*>);
  242. int sys$link(Userspace<const Syscall::SC_link_params*>);
  243. int sys$unlink(const char* pathname, size_t path_length);
  244. int sys$symlink(Userspace<const Syscall::SC_symlink_params*>);
  245. int sys$rmdir(Userspace<const char*> pathname, size_t path_length);
  246. int sys$mount(const Syscall::SC_mount_params*);
  247. int sys$umount(const char* mountpoint, size_t mountpoint_length);
  248. int sys$chmod(const char* pathname, size_t path_length, mode_t);
  249. int sys$fchmod(int fd, mode_t);
  250. int sys$chown(const Syscall::SC_chown_params*);
  251. int sys$fchown(int fd, uid_t, gid_t);
  252. int sys$socket(int domain, int type, int protocol);
  253. int sys$bind(int sockfd, const sockaddr* addr, socklen_t);
  254. int sys$listen(int sockfd, int backlog);
  255. int sys$accept(int sockfd, sockaddr*, socklen_t*);
  256. int sys$connect(int sockfd, const sockaddr*, socklen_t);
  257. int sys$shutdown(int sockfd, int how);
  258. ssize_t sys$sendto(const Syscall::SC_sendto_params*);
  259. ssize_t sys$recvfrom(const Syscall::SC_recvfrom_params*);
  260. int sys$getsockopt(const Syscall::SC_getsockopt_params*);
  261. int sys$setsockopt(const Syscall::SC_setsockopt_params*);
  262. int sys$getsockname(const Syscall::SC_getsockname_params*);
  263. int sys$getpeername(const Syscall::SC_getpeername_params*);
  264. int sys$sched_setparam(pid_t pid, Userspace<const struct sched_param*>);
  265. int sys$sched_getparam(pid_t pid, Userspace<struct sched_param*>);
  266. int sys$create_thread(void* (*)(void*), const Syscall::SC_create_thread_params*);
  267. void sys$exit_thread(void*);
  268. int sys$join_thread(int tid, void** exit_value);
  269. int sys$detach_thread(int tid);
  270. int sys$set_thread_name(int tid, const char* buffer, size_t buffer_size);
  271. int sys$get_thread_name(int tid, char* buffer, size_t buffer_size);
  272. int sys$rename(const Syscall::SC_rename_params*);
  273. int sys$mknod(const Syscall::SC_mknod_params*);
  274. int sys$shbuf_create(int, void** buffer);
  275. int sys$shbuf_allow_pid(int, pid_t peer_pid);
  276. int sys$shbuf_allow_all(int);
  277. void* sys$shbuf_get(int shbuf_id, size_t* size);
  278. int sys$shbuf_release(int shbuf_id);
  279. int sys$shbuf_seal(int shbuf_id);
  280. int sys$shbuf_set_volatile(int shbuf_id, bool);
  281. int sys$halt();
  282. int sys$reboot();
  283. int sys$set_process_icon(int icon_id);
  284. int sys$realpath(const Syscall::SC_realpath_params*);
  285. ssize_t sys$getrandom(void*, size_t, unsigned int);
  286. int sys$setkeymap(Userspace<const Syscall::SC_setkeymap_params*>);
  287. int sys$module_load(const char* path, size_t path_length);
  288. int sys$module_unload(const char* name, size_t name_length);
  289. int sys$profiling_enable(pid_t);
  290. int sys$profiling_disable(pid_t);
  291. int sys$futex(const Syscall::SC_futex_params*);
  292. int sys$set_thread_boost(int tid, int amount);
  293. int sys$set_process_boost(pid_t, int amount);
  294. int sys$chroot(const char* path, size_t path_length, int mount_flags);
  295. int sys$pledge(Userspace<const Syscall::SC_pledge_params*>);
  296. int sys$unveil(Userspace<const Syscall::SC_unveil_params*>);
  297. int sys$perf_event(int type, FlatPtr arg1, FlatPtr arg2);
  298. int sys$get_stack_bounds(FlatPtr* stack_base, size_t* stack_size);
  299. int sys$ptrace(Userspace<const Syscall::SC_ptrace_params*>);
  300. int sys$sendfd(int sockfd, int fd);
  301. int sys$recvfd(int sockfd);
  302. long sys$sysconf(int name);
  303. int sys$disown(pid_t);
  304. template<bool sockname, typename Params>
  305. int get_sock_or_peer_name(const Params&);
  306. static void initialize();
  307. [[noreturn]] void crash(int signal, u32 eip, bool out_of_memory = false);
  308. [[nodiscard]] static siginfo_t reap(Process&);
  309. const TTY* tty() const { return m_tty; }
  310. void set_tty(TTY*);
  311. size_t region_count() const { return m_regions.size(); }
  312. const NonnullOwnPtrVector<Region>& regions() const
  313. {
  314. ASSERT(m_lock.is_locked());
  315. return m_regions;
  316. }
  317. void dump_regions();
  318. u32 m_ticks_in_user { 0 };
  319. u32 m_ticks_in_kernel { 0 };
  320. u32 m_ticks_in_user_for_dead_children { 0 };
  321. u32 m_ticks_in_kernel_for_dead_children { 0 };
  322. [[nodiscard]] bool validate_read_from_kernel(VirtualAddress, size_t) const;
  323. [[nodiscard]] bool validate_read(const void*, size_t) const;
  324. [[nodiscard]] bool validate_write(void*, size_t) const;
  325. template<typename T>
  326. [[nodiscard]] bool validate_read(Userspace<T*> ptr, size_t size) const
  327. {
  328. return validate_read(ptr.unsafe_userspace_ptr(), size);
  329. }
  330. template<typename T>
  331. [[nodiscard]] bool validate_write(Userspace<T*> ptr, size_t size) const
  332. {
  333. return validate_write(ptr.unsafe_userspace_ptr(), size);
  334. }
  335. template<typename T>
  336. [[nodiscard]] bool validate_read_typed(T* value, size_t count = 1)
  337. {
  338. Checked size = sizeof(T);
  339. size *= count;
  340. if (size.has_overflow())
  341. return false;
  342. return validate_read(value, size.value());
  343. }
  344. template<typename T>
  345. [[nodiscard]] bool validate_read_typed(Userspace<T*> value, size_t count = 1)
  346. {
  347. Checked size = sizeof(T);
  348. size *= count;
  349. if (size.has_overflow())
  350. return false;
  351. return validate_read(value, size.value());
  352. }
  353. template<typename T>
  354. [[nodiscard]] bool validate_read_and_copy_typed(T* dest, const T* src)
  355. {
  356. bool validated = validate_read_typed(src);
  357. if (validated) {
  358. copy_from_user(dest, src);
  359. }
  360. return validated;
  361. }
  362. template<typename T>
  363. [[nodiscard]] bool validate_read_and_copy_typed(T* dest, Userspace<const T*> src)
  364. {
  365. bool validated = validate_read_typed(src);
  366. if (validated) {
  367. copy_from_user(dest, src);
  368. }
  369. return validated;
  370. }
  371. template<typename T>
  372. [[nodiscard]] bool validate_write_typed(T* value, size_t count = 1)
  373. {
  374. Checked size = sizeof(T);
  375. size *= count;
  376. if (size.has_overflow())
  377. return false;
  378. return validate_write(value, size.value());
  379. }
  380. template<typename T>
  381. [[nodiscard]] bool validate_write_typed(Userspace<T*> value, size_t count = 1)
  382. {
  383. Checked size = sizeof(T);
  384. size *= count;
  385. if (size.has_overflow())
  386. return false;
  387. return validate_write(value, size.value());
  388. }
  389. template<typename DataType, typename SizeType>
  390. [[nodiscard]] bool validate(const Syscall::MutableBufferArgument<DataType, SizeType>& buffer)
  391. {
  392. return validate_write(buffer.data, buffer.size);
  393. }
  394. template<typename DataType, typename SizeType>
  395. [[nodiscard]] bool validate(const Syscall::ImmutableBufferArgument<DataType, SizeType>& buffer)
  396. {
  397. return validate_read(buffer.data, buffer.size);
  398. }
  399. [[nodiscard]] String validate_and_copy_string_from_user(const char*, size_t) const;
  400. [[nodiscard]] String validate_and_copy_string_from_user(Userspace<const char*> user_characters, size_t size) const
  401. {
  402. return validate_and_copy_string_from_user(user_characters.unsafe_userspace_ptr(), size); }
  403. [[nodiscard]] String validate_and_copy_string_from_user(const Syscall::StringArgument&) const;
  404. Custody& current_directory();
  405. Custody* executable()
  406. {
  407. return m_executable.ptr();
  408. }
  409. int number_of_open_file_descriptors() const;
  410. int max_open_file_descriptors() const
  411. {
  412. return m_max_open_file_descriptors;
  413. }
  414. size_t amount_clean_inode() const;
  415. size_t amount_dirty_private() const;
  416. size_t amount_virtual() const;
  417. size_t amount_resident() const;
  418. size_t amount_shared() const;
  419. size_t amount_purgeable_volatile() const;
  420. size_t amount_purgeable_nonvolatile() const;
  421. int exec(String path, Vector<String> arguments, Vector<String> environment, int recusion_depth = 0);
  422. bool is_superuser() const
  423. {
  424. return m_euid == 0;
  425. }
  426. Region* allocate_region_with_vmobject(VirtualAddress, size_t, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const String& name, int prot);
  427. Region* allocate_region(VirtualAddress, size_t, const String& name, int prot = PROT_READ | PROT_WRITE, bool should_commit = true);
  428. Region* allocate_region_with_vmobject(const Range&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const String& name, int prot);
  429. Region* allocate_region(const Range&, const String& name, int prot = PROT_READ | PROT_WRITE, bool should_commit = true);
  430. bool deallocate_region(Region& region);
  431. Region& allocate_split_region(const Region& source_region, const Range&, size_t offset_in_vmobject);
  432. Vector<Region*, 2> split_region_around_range(const Region& source_region, const Range&);
  433. void terminate_due_to_signal(u8 signal);
  434. KResult send_signal(u8 signal, Process* sender);
  435. u16 thread_count() const
  436. {
  437. return m_thread_count.load(AK::MemoryOrder::memory_order_consume);
  438. }
  439. Lock& big_lock()
  440. {
  441. return m_big_lock;
  442. }
  443. struct ELFBundle {
  444. OwnPtr<Region> region;
  445. RefPtr<ELF::Loader> elf_loader;
  446. };
  447. OwnPtr<ELFBundle> elf_bundle() const;
  448. int icon_id() const
  449. {
  450. return m_icon_id;
  451. }
  452. u32 priority_boost() const
  453. {
  454. return m_priority_boost;
  455. }
  456. Custody& root_directory();
  457. Custody& root_directory_relative_to_global_root();
  458. void set_root_directory(const Custody&);
  459. bool has_promises() const
  460. {
  461. return m_promises;
  462. }
  463. bool has_promised(Pledge pledge) const
  464. {
  465. return m_promises & (1u << (u32)pledge);
  466. }
  467. VeilState veil_state() const
  468. {
  469. return m_veil_state;
  470. }
  471. const Vector<UnveiledPath>& unveiled_paths() const
  472. {
  473. return m_unveiled_paths;
  474. }
  475. bool wait_for_tracer_at_next_execve() const
  476. {
  477. return m_wait_for_tracer_at_next_execve;
  478. }
  479. void set_wait_for_tracer_at_next_execve(bool val)
  480. {
  481. m_wait_for_tracer_at_next_execve = val;
  482. }
  483. KResultOr<u32> peek_user_data(Userspace<const u32*> address);
  484. KResult poke_user_data(Userspace<u32*> address, u32 data);
  485. private:
  486. friend class MemoryManager;
  487. friend class Scheduler;
  488. friend class Region;
  489. 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);
  490. static pid_t allocate_pid();
  491. Range allocate_range(VirtualAddress, size_t, size_t alignment = PAGE_SIZE);
  492. Region& add_region(NonnullOwnPtr<Region>);
  493. void kill_threads_except_self();
  494. void kill_all_threads();
  495. int do_exec(NonnullRefPtr<FileDescription> main_program_description, Vector<String> arguments, Vector<String> environment, RefPtr<FileDescription> interpreter_description, Thread*& new_main_thread, u32& prev_flags);
  496. ssize_t do_write(FileDescription&, const u8*, int data_size);
  497. KResultOr<NonnullRefPtr<FileDescription>> find_elf_interpreter_for_executable(const String& path, char (&first_page)[PAGE_SIZE], int nread, size_t file_size);
  498. Vector<AuxiliaryValue> generate_auxiliary_vector() const;
  499. int alloc_fd(int first_candidate_fd = 0);
  500. void disown_all_shared_buffers();
  501. KResult do_kill(Process&, int signal);
  502. KResult do_killpg(pid_t pgrp, int signal);
  503. KResult do_killall(int signal);
  504. KResult do_killself(int signal);
  505. KResultOr<siginfo_t> do_waitid(idtype_t idtype, int id, int options);
  506. KResultOr<String> get_syscall_path_argument(const char* user_path, size_t path_length) const;
  507. KResultOr<String> get_syscall_path_argument(Userspace<const char*> user_path, size_t path_length) const
  508. {
  509. return get_syscall_path_argument(user_path.unsafe_userspace_ptr(), path_length);
  510. }
  511. KResultOr<String> get_syscall_path_argument(const Syscall::StringArgument&) const;
  512. bool has_tracee_thread(int tracer_pid) const;
  513. RefPtr<PageDirectory> m_page_directory;
  514. Process* m_prev { nullptr };
  515. Process* m_next { nullptr };
  516. String m_name;
  517. pid_t m_pid { 0 };
  518. pid_t m_sid { 0 };
  519. pid_t m_pgid { 0 };
  520. uid_t m_euid { 0 };
  521. gid_t m_egid { 0 };
  522. uid_t m_uid { 0 };
  523. gid_t m_gid { 0 };
  524. uid_t m_suid { 0 };
  525. gid_t m_sgid { 0 };
  526. pid_t m_exec_tid { 0 };
  527. FlatPtr m_load_offset { 0U };
  528. FlatPtr m_entry_eip { 0U };
  529. static const int m_max_open_file_descriptors { FD_SETSIZE };
  530. class FileDescriptionAndFlags {
  531. public:
  532. operator bool() const { return !!m_description; }
  533. FileDescription* description() { return m_description; }
  534. const FileDescription* description() const { return m_description; }
  535. u32 flags() const { return m_flags; }
  536. void set_flags(u32 flags) { m_flags = flags; }
  537. void clear();
  538. void set(NonnullRefPtr<FileDescription>&&, u32 flags = 0);
  539. private:
  540. RefPtr<FileDescription> m_description;
  541. u32 m_flags { 0 };
  542. };
  543. Vector<FileDescriptionAndFlags> m_fds;
  544. RingLevel m_ring { Ring0 };
  545. u8 m_termination_status { 0 };
  546. u8 m_termination_signal { 0 };
  547. Atomic<u16> m_thread_count { 0 };
  548. bool m_dead { false };
  549. bool m_profiling { false };
  550. RefPtr<Custody> m_executable;
  551. RefPtr<Custody> m_cwd;
  552. RefPtr<Custody> m_root_directory;
  553. RefPtr<Custody> m_root_directory_relative_to_global_root;
  554. RefPtr<TTY> m_tty;
  555. Region* find_region_from_range(const Range&);
  556. Region* find_region_containing(const Range&);
  557. NonnullOwnPtrVector<Region> m_regions;
  558. struct RegionLookupCache {
  559. Range range;
  560. WeakPtr<Region> region;
  561. };
  562. RegionLookupCache m_region_lookup_cache;
  563. pid_t m_ppid { 0 };
  564. mode_t m_umask { 022 };
  565. FixedArray<gid_t> m_extra_gids;
  566. WeakPtr<Region> m_master_tls_region;
  567. size_t m_master_tls_size { 0 };
  568. size_t m_master_tls_alignment { 0 };
  569. Lock m_big_lock { "Process" };
  570. mutable SpinLock<u32> m_lock;
  571. u64 m_alarm_deadline { 0 };
  572. int m_icon_id { -1 };
  573. u32 m_priority_boost { 0 };
  574. u32 m_promises { 0 };
  575. u32 m_execpromises { 0 };
  576. VeilState m_veil_state { VeilState::None };
  577. Vector<UnveiledPath> m_unveiled_paths;
  578. WaitQueue& futex_queue(i32*);
  579. HashMap<u32, OwnPtr<WaitQueue>> m_futex_queues;
  580. OwnPtr<PerformanceEventBuffer> m_perf_event_buffer;
  581. // This member is used in the implementation of ptrace's PT_TRACEME flag.
  582. // If it is set to true, the process will stop at the next execve syscall
  583. // and wait for a tracer to attach.
  584. bool m_wait_for_tracer_at_next_execve { false };
  585. };
  586. extern InlineLinkedList<Process>* g_processes;
  587. extern RecursiveSpinLock g_processes_lock;
  588. template<typename Callback>
  589. inline void Process::for_each(Callback callback)
  590. {
  591. ASSERT_INTERRUPTS_DISABLED();
  592. ScopedSpinLock lock(g_processes_lock);
  593. for (auto* process = g_processes->head(); process;) {
  594. auto* next_process = process->next();
  595. if (callback(*process) == IterationDecision::Break)
  596. break;
  597. process = next_process;
  598. }
  599. }
  600. template<typename Callback>
  601. inline void Process::for_each_child(Callback callback)
  602. {
  603. ASSERT_INTERRUPTS_DISABLED();
  604. pid_t my_pid = pid();
  605. ScopedSpinLock lock(g_processes_lock);
  606. for (auto* process = g_processes->head(); process;) {
  607. auto* next_process = process->next();
  608. if (process->ppid() == my_pid || process->has_tracee_thread(m_pid)) {
  609. if (callback(*process) == IterationDecision::Break)
  610. break;
  611. }
  612. process = next_process;
  613. }
  614. }
  615. template<typename Callback>
  616. inline void Process::for_each_thread(Callback callback) const
  617. {
  618. InterruptDisabler disabler;
  619. pid_t my_pid = pid();
  620. if (my_pid == 0) {
  621. // NOTE: Special case the colonel process, since its main thread is not in the global thread table.
  622. Processor::for_each(
  623. [&](Processor& proc) -> IterationDecision {
  624. auto idle_thread = proc.idle_thread();
  625. if (idle_thread != nullptr)
  626. return callback(*idle_thread);
  627. return IterationDecision::Continue;
  628. });
  629. return;
  630. }
  631. Thread::for_each([callback, my_pid](Thread& thread) -> IterationDecision {
  632. if (thread.pid() == my_pid)
  633. return callback(thread);
  634. return IterationDecision::Continue;
  635. });
  636. }
  637. template<typename Callback>
  638. inline void Process::for_each_in_pgrp(pid_t pgid, Callback callback)
  639. {
  640. ASSERT_INTERRUPTS_DISABLED();
  641. ScopedSpinLock lock(g_processes_lock);
  642. for (auto* process = g_processes->head(); process;) {
  643. auto* next_process = process->next();
  644. if (!process->is_dead() && process->pgid() == pgid) {
  645. if (callback(*process) == IterationDecision::Break)
  646. break;
  647. }
  648. process = next_process;
  649. }
  650. }
  651. inline bool InodeMetadata::may_read(const Process& process) const
  652. {
  653. return may_read(process.euid(), process.egid(), process.extra_gids());
  654. }
  655. inline bool InodeMetadata::may_write(const Process& process) const
  656. {
  657. return may_write(process.euid(), process.egid(), process.extra_gids());
  658. }
  659. inline bool InodeMetadata::may_execute(const Process& process) const
  660. {
  661. return may_execute(process.euid(), process.egid(), process.extra_gids());
  662. }
  663. inline int Thread::pid() const
  664. {
  665. return m_process->pid();
  666. }
  667. inline const LogStream& operator<<(const LogStream& stream, const Process& process)
  668. {
  669. return stream << process.name() << '(' << process.pid() << ')';
  670. }
  671. inline u32 Thread::effective_priority() const
  672. {
  673. return m_priority + m_process->priority_boost() + m_priority_boost + m_extra_priority;
  674. }
  675. #define REQUIRE_NO_PROMISES \
  676. do { \
  677. if (Process::current()->has_promises()) { \
  678. dbg() << "Has made a promise"; \
  679. cli(); \
  680. Process::current()->crash(SIGABRT, 0); \
  681. ASSERT_NOT_REACHED(); \
  682. } \
  683. } while (0)
  684. #define REQUIRE_PROMISE(promise) \
  685. do { \
  686. if (Process::current()->has_promises() \
  687. && !Process::current()->has_promised(Pledge::promise)) { \
  688. dbg() << "Has not pledged " << #promise; \
  689. cli(); \
  690. Process::current()->crash(SIGABRT, 0); \
  691. ASSERT_NOT_REACHED(); \
  692. } \
  693. } while (0)
  694. }