Process.h 29 KB

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