Process.h 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Concepts.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/IntrusiveList.h>
  10. #include <AK/IntrusiveListRelaxedConst.h>
  11. #include <AK/OwnPtr.h>
  12. #include <AK/RefPtr.h>
  13. #include <AK/Userspace.h>
  14. #include <AK/Variant.h>
  15. #include <Kernel/API/POSIX/select.h>
  16. #include <Kernel/API/POSIX/sys/resource.h>
  17. #include <Kernel/API/Syscall.h>
  18. #include <Kernel/Assertions.h>
  19. #include <Kernel/AtomicEdgeAction.h>
  20. #include <Kernel/Credentials.h>
  21. #include <Kernel/FileSystem/InodeMetadata.h>
  22. #include <Kernel/FileSystem/OpenFileDescription.h>
  23. #include <Kernel/FileSystem/UnveilNode.h>
  24. #include <Kernel/Forward.h>
  25. #include <Kernel/FutexQueue.h>
  26. #include <Kernel/Jail.h>
  27. #include <Kernel/Library/LockWeakPtr.h>
  28. #include <Kernel/Library/LockWeakable.h>
  29. #include <Kernel/Locking/Mutex.h>
  30. #include <Kernel/Locking/MutexProtected.h>
  31. #include <Kernel/Memory/AddressSpace.h>
  32. #include <Kernel/PerformanceEventBuffer.h>
  33. #include <Kernel/ProcessGroup.h>
  34. #include <Kernel/StdLib.h>
  35. #include <Kernel/Thread.h>
  36. #include <Kernel/UnixTypes.h>
  37. #include <LibC/elf.h>
  38. namespace Kernel {
  39. MutexProtected<OwnPtr<KString>>& hostname();
  40. Time kgettimeofday();
  41. #define ENUMERATE_PLEDGE_PROMISES \
  42. __ENUMERATE_PLEDGE_PROMISE(stdio) \
  43. __ENUMERATE_PLEDGE_PROMISE(rpath) \
  44. __ENUMERATE_PLEDGE_PROMISE(wpath) \
  45. __ENUMERATE_PLEDGE_PROMISE(cpath) \
  46. __ENUMERATE_PLEDGE_PROMISE(dpath) \
  47. __ENUMERATE_PLEDGE_PROMISE(inet) \
  48. __ENUMERATE_PLEDGE_PROMISE(id) \
  49. __ENUMERATE_PLEDGE_PROMISE(proc) \
  50. __ENUMERATE_PLEDGE_PROMISE(ptrace) \
  51. __ENUMERATE_PLEDGE_PROMISE(exec) \
  52. __ENUMERATE_PLEDGE_PROMISE(unix) \
  53. __ENUMERATE_PLEDGE_PROMISE(recvfd) \
  54. __ENUMERATE_PLEDGE_PROMISE(sendfd) \
  55. __ENUMERATE_PLEDGE_PROMISE(fattr) \
  56. __ENUMERATE_PLEDGE_PROMISE(tty) \
  57. __ENUMERATE_PLEDGE_PROMISE(chown) \
  58. __ENUMERATE_PLEDGE_PROMISE(thread) \
  59. __ENUMERATE_PLEDGE_PROMISE(video) \
  60. __ENUMERATE_PLEDGE_PROMISE(accept) \
  61. __ENUMERATE_PLEDGE_PROMISE(settime) \
  62. __ENUMERATE_PLEDGE_PROMISE(sigaction) \
  63. __ENUMERATE_PLEDGE_PROMISE(setkeymap) \
  64. __ENUMERATE_PLEDGE_PROMISE(prot_exec) \
  65. __ENUMERATE_PLEDGE_PROMISE(map_fixed) \
  66. __ENUMERATE_PLEDGE_PROMISE(getkeymap) \
  67. __ENUMERATE_PLEDGE_PROMISE(jail) \
  68. __ENUMERATE_PLEDGE_PROMISE(no_error)
  69. enum class Pledge : u32 {
  70. #define __ENUMERATE_PLEDGE_PROMISE(x) x,
  71. ENUMERATE_PLEDGE_PROMISES
  72. #undef __ENUMERATE_PLEDGE_PROMISE
  73. };
  74. enum class VeilState {
  75. None,
  76. Dropped,
  77. Locked,
  78. LockedInherited,
  79. };
  80. static constexpr FlatPtr futex_key_private_flag = 0b1;
  81. union GlobalFutexKey {
  82. struct {
  83. Memory::VMObject const* vmobject;
  84. FlatPtr offset;
  85. } shared;
  86. struct {
  87. Memory::AddressSpace const* address_space;
  88. FlatPtr user_address;
  89. } private_;
  90. struct {
  91. FlatPtr parent;
  92. FlatPtr offset;
  93. } raw;
  94. };
  95. static_assert(sizeof(GlobalFutexKey) == (sizeof(FlatPtr) * 2));
  96. struct LoadResult;
  97. class ProcessList;
  98. class Process final
  99. : public ListedRefCounted<Process, LockType::Spinlock>
  100. , public LockWeakable<Process> {
  101. class ProtectedValues {
  102. public:
  103. ProcessID pid { 0 };
  104. ProcessID ppid { 0 };
  105. // FIXME: This should be a NonnullRefPtr
  106. RefPtr<Credentials> credentials;
  107. RefPtr<ProcessGroup> process_group;
  108. RefPtr<TTY> tty;
  109. bool dumpable { false };
  110. bool executable_is_setid { false };
  111. Atomic<bool> has_promises { false };
  112. Atomic<u32> promises { 0 };
  113. Atomic<bool> has_execpromises { false };
  114. Atomic<u32> execpromises { 0 };
  115. mode_t umask { 022 };
  116. VirtualAddress signal_trampoline;
  117. Atomic<u32> thread_count { 0 };
  118. u8 termination_status { 0 };
  119. u8 termination_signal { 0 };
  120. };
  121. public:
  122. AK_MAKE_NONCOPYABLE(Process);
  123. AK_MAKE_NONMOVABLE(Process);
  124. MAKE_ALIGNED_ALLOCATED(Process, PAGE_SIZE);
  125. friend class Thread;
  126. friend class Coredump;
  127. auto with_protected_data(auto&& callback) const
  128. {
  129. SpinlockLocker locker(m_protected_data_lock);
  130. return callback(m_protected_values_do_not_access_directly);
  131. }
  132. auto with_mutable_protected_data(auto&& callback)
  133. {
  134. SpinlockLocker locker(m_protected_data_lock);
  135. unprotect_data();
  136. auto guard = ScopeGuard([&] { protect_data(); });
  137. return callback(m_protected_values_do_not_access_directly);
  138. }
  139. enum class State : u8 {
  140. Running = 0,
  141. Dying,
  142. Dead
  143. };
  144. public:
  145. static Process& current()
  146. {
  147. auto* current_thread = Processor::current_thread();
  148. VERIFY(current_thread);
  149. return current_thread->process();
  150. }
  151. static bool has_current()
  152. {
  153. return Processor::current_thread() != nullptr;
  154. }
  155. template<typename EntryFunction>
  156. static void kernel_process_trampoline(void* data)
  157. {
  158. EntryFunction* func = reinterpret_cast<EntryFunction*>(data);
  159. (*func)();
  160. delete func;
  161. }
  162. enum class RegisterProcess {
  163. No,
  164. Yes
  165. };
  166. struct ProcessAndFirstThread {
  167. NonnullRefPtr<Process> process;
  168. NonnullRefPtr<Thread> first_thread;
  169. };
  170. template<typename EntryFunction>
  171. static ErrorOr<ProcessAndFirstThread> create_kernel_process(NonnullOwnPtr<KString> name, EntryFunction entry, u32 affinity = THREAD_AFFINITY_DEFAULT, RegisterProcess do_register = RegisterProcess::Yes)
  172. {
  173. auto* entry_func = new EntryFunction(move(entry));
  174. return create_kernel_process(move(name), &Process::kernel_process_trampoline<EntryFunction>, entry_func, affinity, do_register);
  175. }
  176. static ErrorOr<ProcessAndFirstThread> create_kernel_process(NonnullOwnPtr<KString> name, void (*entry)(void*), void* entry_data = nullptr, u32 affinity = THREAD_AFFINITY_DEFAULT, RegisterProcess do_register = RegisterProcess::Yes);
  177. static ErrorOr<ProcessAndFirstThread> create_user_process(StringView path, UserID, GroupID, Vector<NonnullOwnPtr<KString>> arguments, Vector<NonnullOwnPtr<KString>> environment, RefPtr<TTY>);
  178. static void register_new(Process&);
  179. ~Process();
  180. virtual void remove_from_secondary_lists();
  181. ErrorOr<NonnullRefPtr<Thread>> create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, NonnullOwnPtr<KString> name, u32 affinity = THREAD_AFFINITY_DEFAULT, bool joinable = true);
  182. bool is_profiling() const { return m_profiling; }
  183. void set_profiling(bool profiling) { m_profiling = profiling; }
  184. bool should_generate_coredump() const { return m_should_generate_coredump; }
  185. void set_should_generate_coredump(bool b) { m_should_generate_coredump = b; }
  186. bool is_dying() const { return m_state.load(AK::MemoryOrder::memory_order_acquire) != State::Running; }
  187. bool is_dead() const { return m_state.load(AK::MemoryOrder::memory_order_acquire) == State::Dead; }
  188. bool is_stopped() const { return m_is_stopped; }
  189. bool set_stopped(bool stopped) { return m_is_stopped.exchange(stopped); }
  190. bool is_kernel_process() const { return m_is_kernel_process; }
  191. bool is_user_process() const { return !m_is_kernel_process; }
  192. static RefPtr<Process> from_pid_in_same_jail(ProcessID);
  193. static RefPtr<Process> from_pid_ignoring_jails(ProcessID);
  194. static SessionID get_sid_from_pgid(ProcessGroupID pgid);
  195. SpinlockProtected<NonnullOwnPtr<KString>, LockRank::None> const& name() const;
  196. void set_name(NonnullOwnPtr<KString>);
  197. ProcessID pid() const
  198. {
  199. return with_protected_data([](auto& protected_data) { return protected_data.pid; });
  200. }
  201. SessionID sid() const { return credentials()->sid(); }
  202. bool is_session_leader() const { return sid().value() == pid().value(); }
  203. ProcessGroupID pgid() const
  204. {
  205. return with_protected_data([](auto& protected_data) { return protected_data.process_group ? protected_data.process_group->pgid() : 0; });
  206. }
  207. bool is_group_leader() const { return pgid().value() == pid().value(); }
  208. ProcessID ppid() const
  209. {
  210. return with_protected_data([](auto& protected_data) { return protected_data.ppid; });
  211. }
  212. SpinlockProtected<RefPtr<Jail>, LockRank::Process> const& jail() { return m_attached_jail; }
  213. bool is_currently_in_jail() const
  214. {
  215. return m_attached_jail.with([&](auto& jail) -> bool { return !jail.is_null(); });
  216. }
  217. NonnullRefPtr<Credentials> credentials() const;
  218. bool is_dumpable() const
  219. {
  220. return with_protected_data([](auto& protected_data) { return protected_data.dumpable; });
  221. }
  222. mode_t umask() const
  223. {
  224. return with_protected_data([](auto& protected_data) { return protected_data.umask; });
  225. }
  226. // Breakable iteration functions
  227. template<IteratorFunction<Process&> Callback>
  228. static void for_each_ignoring_jails(Callback);
  229. static ErrorOr<void> for_each_in_same_jail(Function<ErrorOr<void>(Process&)>);
  230. ErrorOr<void> for_each_in_pgrp_in_same_jail(ProcessGroupID, Function<ErrorOr<void>(Process&)>);
  231. ErrorOr<void> for_each_child_in_same_jail(Function<ErrorOr<void>(Process&)>);
  232. template<IteratorFunction<Thread&> Callback>
  233. IterationDecision for_each_thread(Callback);
  234. template<IteratorFunction<Thread&> Callback>
  235. IterationDecision for_each_thread(Callback callback) const;
  236. ErrorOr<void> try_for_each_thread(Function<ErrorOr<void>(Thread const&)>) const;
  237. // Non-breakable iteration functions
  238. template<VoidFunction<Process&> Callback>
  239. static void for_each_ignoring_jails(Callback);
  240. template<VoidFunction<Thread&> Callback>
  241. IterationDecision for_each_thread(Callback);
  242. template<VoidFunction<Thread&> Callback>
  243. IterationDecision for_each_thread(Callback callback) const;
  244. void die();
  245. void finalize();
  246. ThreadTracer* tracer() { return m_tracer.ptr(); }
  247. bool is_traced() const { return !!m_tracer; }
  248. ErrorOr<void> start_tracing_from(ProcessID tracer);
  249. void stop_tracing();
  250. void tracer_trap(Thread&, RegisterState const&);
  251. ErrorOr<FlatPtr> sys$emuctl();
  252. ErrorOr<FlatPtr> sys$yield();
  253. ErrorOr<FlatPtr> sys$sync();
  254. ErrorOr<FlatPtr> sys$beep(int tone);
  255. ErrorOr<FlatPtr> sys$create_inode_watcher(u32 flags);
  256. ErrorOr<FlatPtr> sys$inode_watcher_add_watch(Userspace<Syscall::SC_inode_watcher_add_watch_params const*> user_params);
  257. ErrorOr<FlatPtr> sys$inode_watcher_remove_watch(int fd, int wd);
  258. ErrorOr<FlatPtr> sys$dbgputstr(Userspace<char const*>, size_t);
  259. ErrorOr<FlatPtr> sys$dump_backtrace();
  260. ErrorOr<FlatPtr> sys$gettid();
  261. ErrorOr<FlatPtr> sys$setsid();
  262. ErrorOr<FlatPtr> sys$getsid(pid_t);
  263. ErrorOr<FlatPtr> sys$setpgid(pid_t pid, pid_t pgid);
  264. ErrorOr<FlatPtr> sys$getpgrp();
  265. ErrorOr<FlatPtr> sys$getpgid(pid_t);
  266. ErrorOr<FlatPtr> sys$getuid();
  267. ErrorOr<FlatPtr> sys$getgid();
  268. ErrorOr<FlatPtr> sys$geteuid();
  269. ErrorOr<FlatPtr> sys$getegid();
  270. ErrorOr<FlatPtr> sys$getpid();
  271. ErrorOr<FlatPtr> sys$getppid();
  272. ErrorOr<FlatPtr> sys$getresuid(Userspace<UserID*>, Userspace<UserID*>, Userspace<UserID*>);
  273. ErrorOr<FlatPtr> sys$getresgid(Userspace<GroupID*>, Userspace<GroupID*>, Userspace<GroupID*>);
  274. ErrorOr<FlatPtr> sys$getrusage(int, Userspace<rusage*>);
  275. ErrorOr<FlatPtr> sys$umask(mode_t);
  276. ErrorOr<FlatPtr> sys$open(Userspace<Syscall::SC_open_params const*>);
  277. ErrorOr<FlatPtr> sys$close(int fd);
  278. ErrorOr<FlatPtr> sys$read(int fd, Userspace<u8*>, size_t);
  279. ErrorOr<FlatPtr> sys$pread(int fd, Userspace<u8*>, size_t, Userspace<off_t const*>);
  280. ErrorOr<FlatPtr> sys$readv(int fd, Userspace<const struct iovec*> iov, int iov_count);
  281. ErrorOr<FlatPtr> sys$write(int fd, Userspace<u8 const*>, size_t);
  282. ErrorOr<FlatPtr> sys$pwritev(int fd, Userspace<const struct iovec*> iov, int iov_count, Userspace<off_t const*>);
  283. ErrorOr<FlatPtr> sys$fstat(int fd, Userspace<stat*>);
  284. ErrorOr<FlatPtr> sys$stat(Userspace<Syscall::SC_stat_params const*>);
  285. ErrorOr<FlatPtr> sys$annotate_mapping(Userspace<void*>, int flags);
  286. ErrorOr<FlatPtr> sys$lseek(int fd, Userspace<off_t*>, int whence);
  287. ErrorOr<FlatPtr> sys$ftruncate(int fd, Userspace<off_t const*>);
  288. ErrorOr<FlatPtr> sys$posix_fallocate(int fd, Userspace<off_t const*>, Userspace<off_t const*>);
  289. ErrorOr<FlatPtr> sys$kill(pid_t pid_or_pgid, int sig);
  290. [[noreturn]] void sys$exit(int status);
  291. ErrorOr<FlatPtr> sys$sigreturn(RegisterState& registers);
  292. ErrorOr<FlatPtr> sys$waitid(Userspace<Syscall::SC_waitid_params const*>);
  293. ErrorOr<FlatPtr> sys$mmap(Userspace<Syscall::SC_mmap_params const*>);
  294. ErrorOr<FlatPtr> sys$mremap(Userspace<Syscall::SC_mremap_params const*>);
  295. ErrorOr<FlatPtr> sys$munmap(Userspace<void*>, size_t);
  296. ErrorOr<FlatPtr> sys$set_mmap_name(Userspace<Syscall::SC_set_mmap_name_params const*>);
  297. ErrorOr<FlatPtr> sys$mprotect(Userspace<void*>, size_t, int prot);
  298. ErrorOr<FlatPtr> sys$madvise(Userspace<void*>, size_t, int advice);
  299. ErrorOr<FlatPtr> sys$msync(Userspace<void*>, size_t, int flags);
  300. ErrorOr<FlatPtr> sys$purge(int mode);
  301. ErrorOr<FlatPtr> sys$poll(Userspace<Syscall::SC_poll_params const*>);
  302. ErrorOr<FlatPtr> sys$get_dir_entries(int fd, Userspace<void*>, size_t);
  303. ErrorOr<FlatPtr> sys$getcwd(Userspace<char*>, size_t);
  304. ErrorOr<FlatPtr> sys$chdir(Userspace<char const*>, size_t);
  305. ErrorOr<FlatPtr> sys$fchdir(int fd);
  306. ErrorOr<FlatPtr> sys$adjtime(Userspace<timeval const*>, Userspace<timeval*>);
  307. ErrorOr<FlatPtr> sys$clock_gettime(clockid_t, Userspace<timespec*>);
  308. ErrorOr<FlatPtr> sys$clock_settime(clockid_t, Userspace<timespec const*>);
  309. ErrorOr<FlatPtr> sys$clock_nanosleep(Userspace<Syscall::SC_clock_nanosleep_params const*>);
  310. ErrorOr<FlatPtr> sys$clock_getres(Userspace<Syscall::SC_clock_getres_params const*>);
  311. ErrorOr<FlatPtr> sys$gethostname(Userspace<char*>, size_t);
  312. ErrorOr<FlatPtr> sys$sethostname(Userspace<char const*>, size_t);
  313. ErrorOr<FlatPtr> sys$uname(Userspace<utsname*>);
  314. ErrorOr<FlatPtr> sys$readlink(Userspace<Syscall::SC_readlink_params const*>);
  315. ErrorOr<FlatPtr> sys$fork(RegisterState&);
  316. ErrorOr<FlatPtr> sys$execve(Userspace<Syscall::SC_execve_params const*>);
  317. ErrorOr<FlatPtr> sys$dup2(int old_fd, int new_fd);
  318. ErrorOr<FlatPtr> sys$sigaction(int signum, Userspace<sigaction const*> act, Userspace<sigaction*> old_act);
  319. ErrorOr<FlatPtr> sys$sigaltstack(Userspace<stack_t const*> ss, Userspace<stack_t*> old_ss);
  320. ErrorOr<FlatPtr> sys$sigprocmask(int how, Userspace<sigset_t const*> set, Userspace<sigset_t*> old_set);
  321. ErrorOr<FlatPtr> sys$sigpending(Userspace<sigset_t*>);
  322. ErrorOr<FlatPtr> sys$sigsuspend(Userspace<sigset_t const*>);
  323. ErrorOr<FlatPtr> sys$sigtimedwait(Userspace<sigset_t const*>, Userspace<siginfo_t*>, Userspace<timespec const*>);
  324. ErrorOr<FlatPtr> sys$getgroups(size_t, Userspace<GroupID*>);
  325. ErrorOr<FlatPtr> sys$setgroups(size_t, Userspace<GroupID const*>);
  326. ErrorOr<FlatPtr> sys$pipe(Userspace<int*>, int flags);
  327. ErrorOr<FlatPtr> sys$killpg(pid_t pgrp, int sig);
  328. ErrorOr<FlatPtr> sys$seteuid(UserID);
  329. ErrorOr<FlatPtr> sys$setegid(GroupID);
  330. ErrorOr<FlatPtr> sys$setuid(UserID);
  331. ErrorOr<FlatPtr> sys$setgid(GroupID);
  332. ErrorOr<FlatPtr> sys$setreuid(UserID, UserID);
  333. ErrorOr<FlatPtr> sys$setresuid(UserID, UserID, UserID);
  334. ErrorOr<FlatPtr> sys$setregid(GroupID, GroupID);
  335. ErrorOr<FlatPtr> sys$setresgid(GroupID, GroupID, GroupID);
  336. ErrorOr<FlatPtr> sys$alarm(unsigned seconds);
  337. ErrorOr<FlatPtr> sys$faccessat(Userspace<Syscall::SC_faccessat_params const*>);
  338. ErrorOr<FlatPtr> sys$fcntl(int fd, int cmd, uintptr_t extra_arg);
  339. ErrorOr<FlatPtr> sys$ioctl(int fd, unsigned request, FlatPtr arg);
  340. ErrorOr<FlatPtr> sys$mkdir(int dirfd, Userspace<char const*> pathname, size_t path_length, mode_t mode);
  341. ErrorOr<FlatPtr> sys$times(Userspace<tms*>);
  342. ErrorOr<FlatPtr> sys$utime(Userspace<char const*> pathname, size_t path_length, Userspace<const struct utimbuf*>);
  343. ErrorOr<FlatPtr> sys$utimensat(Userspace<Syscall::SC_utimensat_params const*>);
  344. ErrorOr<FlatPtr> sys$link(Userspace<Syscall::SC_link_params const*>);
  345. ErrorOr<FlatPtr> sys$unlink(int dirfd, Userspace<char const*> pathname, size_t path_length, int flags);
  346. ErrorOr<FlatPtr> sys$symlink(Userspace<Syscall::SC_symlink_params const*>);
  347. ErrorOr<FlatPtr> sys$rmdir(Userspace<char const*> pathname, size_t path_length);
  348. ErrorOr<FlatPtr> sys$mount(Userspace<Syscall::SC_mount_params const*>);
  349. ErrorOr<FlatPtr> sys$umount(Userspace<char const*> mountpoint, size_t mountpoint_length);
  350. ErrorOr<FlatPtr> sys$chmod(Userspace<Syscall::SC_chmod_params const*>);
  351. ErrorOr<FlatPtr> sys$fchmod(int fd, mode_t);
  352. ErrorOr<FlatPtr> sys$chown(Userspace<Syscall::SC_chown_params const*>);
  353. ErrorOr<FlatPtr> sys$fchown(int fd, UserID, GroupID);
  354. ErrorOr<FlatPtr> sys$fsync(int fd);
  355. ErrorOr<FlatPtr> sys$socket(int domain, int type, int protocol);
  356. ErrorOr<FlatPtr> sys$bind(int sockfd, Userspace<sockaddr const*> addr, socklen_t);
  357. ErrorOr<FlatPtr> sys$listen(int sockfd, int backlog);
  358. ErrorOr<FlatPtr> sys$accept4(Userspace<Syscall::SC_accept4_params const*>);
  359. ErrorOr<FlatPtr> sys$connect(int sockfd, Userspace<sockaddr const*>, socklen_t);
  360. ErrorOr<FlatPtr> sys$shutdown(int sockfd, int how);
  361. ErrorOr<FlatPtr> sys$sendmsg(int sockfd, Userspace<const struct msghdr*>, int flags);
  362. ErrorOr<FlatPtr> sys$recvmsg(int sockfd, Userspace<struct msghdr*>, int flags);
  363. ErrorOr<FlatPtr> sys$getsockopt(Userspace<Syscall::SC_getsockopt_params const*>);
  364. ErrorOr<FlatPtr> sys$setsockopt(Userspace<Syscall::SC_setsockopt_params const*>);
  365. ErrorOr<FlatPtr> sys$getsockname(Userspace<Syscall::SC_getsockname_params const*>);
  366. ErrorOr<FlatPtr> sys$getpeername(Userspace<Syscall::SC_getpeername_params const*>);
  367. ErrorOr<FlatPtr> sys$socketpair(Userspace<Syscall::SC_socketpair_params const*>);
  368. ErrorOr<FlatPtr> sys$scheduler_set_parameters(Userspace<Syscall::SC_scheduler_parameters_params const*>);
  369. ErrorOr<FlatPtr> sys$scheduler_get_parameters(Userspace<Syscall::SC_scheduler_parameters_params*>);
  370. ErrorOr<FlatPtr> sys$create_thread(void* (*)(void*), Userspace<Syscall::SC_create_thread_params const*>);
  371. [[noreturn]] void sys$exit_thread(Userspace<void*>, Userspace<void*>, size_t);
  372. ErrorOr<FlatPtr> sys$join_thread(pid_t tid, Userspace<void**> exit_value);
  373. ErrorOr<FlatPtr> sys$detach_thread(pid_t tid);
  374. ErrorOr<FlatPtr> sys$set_thread_name(pid_t tid, Userspace<char const*> buffer, size_t buffer_size);
  375. ErrorOr<FlatPtr> sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size);
  376. ErrorOr<FlatPtr> sys$kill_thread(pid_t tid, int signal);
  377. ErrorOr<FlatPtr> sys$rename(Userspace<Syscall::SC_rename_params const*>);
  378. ErrorOr<FlatPtr> sys$mknod(Userspace<Syscall::SC_mknod_params const*>);
  379. ErrorOr<FlatPtr> sys$realpath(Userspace<Syscall::SC_realpath_params const*>);
  380. ErrorOr<FlatPtr> sys$getrandom(Userspace<void*>, size_t, unsigned int);
  381. ErrorOr<FlatPtr> sys$getkeymap(Userspace<Syscall::SC_getkeymap_params const*>);
  382. ErrorOr<FlatPtr> sys$setkeymap(Userspace<Syscall::SC_setkeymap_params const*>);
  383. ErrorOr<FlatPtr> sys$profiling_enable(pid_t, Userspace<u64 const*>);
  384. ErrorOr<FlatPtr> profiling_enable(pid_t, u64 event_mask);
  385. ErrorOr<FlatPtr> sys$profiling_disable(pid_t);
  386. ErrorOr<FlatPtr> sys$profiling_free_buffer(pid_t);
  387. ErrorOr<FlatPtr> sys$futex(Userspace<Syscall::SC_futex_params const*>);
  388. ErrorOr<FlatPtr> sys$pledge(Userspace<Syscall::SC_pledge_params const*>);
  389. ErrorOr<FlatPtr> sys$unveil(Userspace<Syscall::SC_unveil_params const*>);
  390. ErrorOr<FlatPtr> sys$perf_event(int type, FlatPtr arg1, FlatPtr arg2);
  391. ErrorOr<FlatPtr> sys$perf_register_string(Userspace<char const*>, size_t);
  392. ErrorOr<FlatPtr> sys$get_stack_bounds(Userspace<FlatPtr*> stack_base, Userspace<size_t*> stack_size);
  393. ErrorOr<FlatPtr> sys$ptrace(Userspace<Syscall::SC_ptrace_params const*>);
  394. ErrorOr<FlatPtr> sys$sendfd(int sockfd, int fd);
  395. ErrorOr<FlatPtr> sys$recvfd(int sockfd, int options);
  396. ErrorOr<FlatPtr> sys$sysconf(int name);
  397. ErrorOr<FlatPtr> sys$disown(ProcessID);
  398. ErrorOr<FlatPtr> sys$allocate_tls(Userspace<char const*> initial_data, size_t);
  399. ErrorOr<FlatPtr> sys$prctl(int option, FlatPtr arg1, FlatPtr arg2);
  400. ErrorOr<FlatPtr> sys$anon_create(size_t, int options);
  401. ErrorOr<FlatPtr> sys$statvfs(Userspace<Syscall::SC_statvfs_params const*> user_params);
  402. ErrorOr<FlatPtr> sys$fstatvfs(int fd, statvfs* buf);
  403. ErrorOr<FlatPtr> sys$map_time_page();
  404. ErrorOr<FlatPtr> sys$jail_create(Userspace<Syscall::SC_jail_create_params*> user_params);
  405. ErrorOr<FlatPtr> sys$jail_attach(Userspace<Syscall::SC_jail_attach_params const*> user_params);
  406. ErrorOr<FlatPtr> sys$get_root_session_id(pid_t force_sid);
  407. enum SockOrPeerName {
  408. SockName,
  409. PeerName,
  410. };
  411. template<SockOrPeerName, typename Params>
  412. ErrorOr<void> get_sock_or_peer_name(Params const&);
  413. static void initialize();
  414. [[noreturn]] void crash(int signal, Optional<RegisterState const&> regs, bool out_of_memory = false);
  415. [[nodiscard]] siginfo_t wait_info() const;
  416. RefPtr<TTY> tty();
  417. RefPtr<TTY const> tty() const;
  418. void set_tty(RefPtr<TTY>);
  419. clock_t m_ticks_in_user { 0 };
  420. clock_t m_ticks_in_kernel { 0 };
  421. clock_t m_ticks_in_user_for_dead_children { 0 };
  422. clock_t m_ticks_in_kernel_for_dead_children { 0 };
  423. NonnullRefPtr<Custody> current_directory();
  424. RefPtr<Custody> executable();
  425. RefPtr<Custody const> executable() const;
  426. static constexpr size_t max_arguments_size = Thread::default_userspace_stack_size / 8;
  427. static constexpr size_t max_environment_size = Thread::default_userspace_stack_size / 8;
  428. static constexpr size_t max_auxiliary_size = Thread::default_userspace_stack_size / 8;
  429. Vector<NonnullOwnPtr<KString>> const& arguments() const { return m_arguments; };
  430. Vector<NonnullOwnPtr<KString>> const& environment() const { return m_environment; };
  431. ErrorOr<void> exec(NonnullOwnPtr<KString> path, Vector<NonnullOwnPtr<KString>> arguments, Vector<NonnullOwnPtr<KString>> environment, Thread*& new_main_thread, InterruptsState& previous_interrupts_state, int recursion_depth = 0);
  432. ErrorOr<LoadResult> load(Memory::AddressSpace& new_space, NonnullRefPtr<OpenFileDescription> main_program_description, RefPtr<OpenFileDescription> interpreter_description, const ElfW(Ehdr) & main_program_header);
  433. void terminate_due_to_signal(u8 signal);
  434. ErrorOr<void> send_signal(u8 signal, Process* sender);
  435. u8 termination_signal() const
  436. {
  437. return with_protected_data([](auto& protected_data) -> u8 {
  438. return protected_data.termination_signal;
  439. });
  440. }
  441. u8 termination_status() const
  442. {
  443. return with_protected_data([](auto& protected_data) { return protected_data.termination_status; });
  444. }
  445. u16 thread_count() const
  446. {
  447. return with_protected_data([](auto& protected_data) {
  448. return protected_data.thread_count.load(AK::MemoryOrder::memory_order_relaxed);
  449. });
  450. }
  451. Mutex& big_lock() { return m_big_lock; }
  452. Mutex& ptrace_lock() { return m_ptrace_lock; }
  453. bool has_promises() const
  454. {
  455. return with_protected_data([](auto& protected_data) { return protected_data.has_promises.load(); });
  456. }
  457. bool has_promised(Pledge pledge) const
  458. {
  459. return with_protected_data([&](auto& protected_data) {
  460. return (protected_data.promises & (1U << (u32)pledge)) != 0;
  461. });
  462. }
  463. VeilState veil_state() const
  464. {
  465. return m_unveil_data.with([&](auto const& unveil_data) { return unveil_data.state; });
  466. }
  467. struct UnveilData {
  468. explicit UnveilData(UnveilNode&& p)
  469. : paths(move(p))
  470. {
  471. }
  472. VeilState state { VeilState::None };
  473. UnveilNode paths;
  474. };
  475. auto& unveil_data() { return m_unveil_data; }
  476. auto const& unveil_data() const { return m_unveil_data; }
  477. auto& exec_unveil_data() { return m_exec_unveil_data; }
  478. auto const& exec_unveil_data() const { return m_exec_unveil_data; }
  479. bool wait_for_tracer_at_next_execve() const
  480. {
  481. return m_wait_for_tracer_at_next_execve;
  482. }
  483. void set_wait_for_tracer_at_next_execve(bool val)
  484. {
  485. m_wait_for_tracer_at_next_execve = val;
  486. }
  487. ErrorOr<void> peek_user_data(Span<u8> destination, Userspace<u8 const*> address);
  488. ErrorOr<FlatPtr> peek_user_data(Userspace<FlatPtr const*> address);
  489. ErrorOr<void> poke_user_data(Userspace<FlatPtr*> address, FlatPtr data);
  490. void disowned_by_waiter(Process& process);
  491. void unblock_waiters(Thread::WaitBlocker::UnblockFlags, u8 signal = 0);
  492. Thread::WaitBlockerSet& wait_blocker_set() { return m_wait_blocker_set; }
  493. template<typename Callback>
  494. ErrorOr<void> for_each_coredump_property(Callback callback) const
  495. {
  496. return m_coredump_properties.with([&](auto const& coredump_properties) -> ErrorOr<void> {
  497. for (auto const& property : coredump_properties) {
  498. if (property.key && property.value)
  499. TRY(callback(*property.key, *property.value));
  500. }
  501. return {};
  502. });
  503. }
  504. ErrorOr<void> set_coredump_property(NonnullOwnPtr<KString> key, NonnullOwnPtr<KString> value);
  505. ErrorOr<void> try_set_coredump_property(StringView key, StringView value);
  506. Vector<NonnullRefPtr<Thread>> const& threads_for_coredump(Badge<Coredump>) const { return m_threads_for_coredump; }
  507. PerformanceEventBuffer* perf_events() { return m_perf_event_buffer; }
  508. PerformanceEventBuffer const* perf_events() const { return m_perf_event_buffer; }
  509. SpinlockProtected<OwnPtr<Memory::AddressSpace>, LockRank::None>& address_space() { return m_space; }
  510. SpinlockProtected<OwnPtr<Memory::AddressSpace>, LockRank::None> const& address_space() const { return m_space; }
  511. VirtualAddress signal_trampoline() const
  512. {
  513. return with_protected_data([](auto& protected_data) { return protected_data.signal_trampoline; });
  514. }
  515. ErrorOr<void> require_promise(Pledge);
  516. ErrorOr<void> require_no_promises() const;
  517. ErrorOr<void> validate_mmap_prot(int prot, bool map_stack, bool map_anonymous, Memory::Region const* region = nullptr) const;
  518. ErrorOr<void> validate_inode_mmap_prot(int prot, bool description_readable, bool description_writable, bool map_shared) const;
  519. private:
  520. friend class MemoryManager;
  521. friend class Scheduler;
  522. friend class Region;
  523. friend class PerformanceManager;
  524. bool add_thread(Thread&);
  525. bool remove_thread(Thread&);
  526. Process(NonnullOwnPtr<KString> name, NonnullRefPtr<Credentials>, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> current_directory, RefPtr<Custody> executable, RefPtr<TTY> tty, UnveilNode unveil_tree, UnveilNode exec_unveil_tree);
  527. static ErrorOr<ProcessAndFirstThread> create(NonnullOwnPtr<KString> name, UserID, GroupID, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> current_directory = nullptr, RefPtr<Custody> executable = nullptr, RefPtr<TTY> = nullptr, Process* fork_parent = nullptr);
  528. ErrorOr<NonnullRefPtr<Thread>> attach_resources(NonnullOwnPtr<Memory::AddressSpace>&&, Process* fork_parent);
  529. static ProcessID allocate_pid();
  530. void kill_threads_except_self();
  531. void kill_all_threads();
  532. ErrorOr<void> dump_core();
  533. ErrorOr<void> dump_perfcore();
  534. bool create_perf_events_buffer_if_needed();
  535. void delete_perf_events_buffer();
  536. ErrorOr<void> do_exec(NonnullRefPtr<OpenFileDescription> main_program_description, Vector<NonnullOwnPtr<KString>> arguments, Vector<NonnullOwnPtr<KString>> environment, RefPtr<OpenFileDescription> interpreter_description, Thread*& new_main_thread, InterruptsState& previous_interrupts_state, const ElfW(Ehdr) & main_program_header);
  537. ErrorOr<FlatPtr> do_write(OpenFileDescription&, UserOrKernelBuffer const&, size_t, Optional<off_t> = {});
  538. ErrorOr<FlatPtr> do_statvfs(FileSystem const& path, Custody const*, statvfs* buf);
  539. ErrorOr<RefPtr<OpenFileDescription>> find_elf_interpreter_for_executable(StringView path, ElfW(Ehdr) const& main_executable_header, size_t main_executable_header_size, size_t file_size);
  540. ErrorOr<void> do_kill(Process&, int signal);
  541. ErrorOr<void> do_killpg(ProcessGroupID pgrp, int signal);
  542. ErrorOr<void> do_killall(int signal);
  543. ErrorOr<void> do_killself(int signal);
  544. ErrorOr<siginfo_t> do_waitid(Variant<Empty, NonnullRefPtr<Process>, NonnullRefPtr<ProcessGroup>> waitee, int options);
  545. static ErrorOr<NonnullOwnPtr<KString>> get_syscall_path_argument(Userspace<char const*> user_path, size_t path_length);
  546. static ErrorOr<NonnullOwnPtr<KString>> get_syscall_path_argument(Syscall::StringArgument const&);
  547. bool has_tracee_thread(ProcessID tracer_pid);
  548. void clear_signal_handlers_for_exec();
  549. void clear_futex_queues_on_exec();
  550. ErrorOr<GlobalFutexKey> get_futex_key(FlatPtr user_address, bool shared);
  551. ErrorOr<void> remap_range_as_stack(FlatPtr address, size_t size);
  552. ErrorOr<FlatPtr> read_impl(int fd, Userspace<u8*> buffer, size_t size);
  553. public:
  554. ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const;
  555. ErrorOr<NonnullRefPtr<Inode>> lookup_as_directory(ProcFS&, StringView name) const;
  556. ErrorOr<void> procfs_get_fds_stats(KBufferBuilder& builder) const;
  557. ErrorOr<void> procfs_get_perf_events(KBufferBuilder& builder) const;
  558. ErrorOr<void> procfs_get_unveil_stats(KBufferBuilder& builder) const;
  559. ErrorOr<void> procfs_get_pledge_stats(KBufferBuilder& builder) const;
  560. ErrorOr<void> procfs_get_virtual_memory_stats(KBufferBuilder& builder) const;
  561. ErrorOr<void> procfs_get_binary_link(KBufferBuilder& builder) const;
  562. ErrorOr<void> procfs_get_current_work_directory_link(KBufferBuilder& builder) const;
  563. ErrorOr<void> procfs_get_command_line(KBufferBuilder& builder) const;
  564. mode_t binary_link_required_mode() const;
  565. ErrorOr<void> procfs_get_thread_stack(ThreadID thread_id, KBufferBuilder& builder) const;
  566. ErrorOr<void> traverse_stacks_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const;
  567. ErrorOr<NonnullRefPtr<Inode>> lookup_stacks_directory(ProcFS&, StringView name) const;
  568. ErrorOr<size_t> procfs_get_file_description_link(unsigned fd, KBufferBuilder& builder) const;
  569. ErrorOr<void> traverse_file_descriptions_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const;
  570. ErrorOr<NonnullRefPtr<Inode>> lookup_file_descriptions_directory(ProcFS&, StringView name) const;
  571. ErrorOr<NonnullRefPtr<Inode>> lookup_children_directory(ProcFS&, StringView name) const;
  572. ErrorOr<void> traverse_children_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const;
  573. ErrorOr<size_t> procfs_get_child_process_link(ProcessID child_pid, KBufferBuilder& builder) const;
  574. private:
  575. inline PerformanceEventBuffer* current_perf_events_buffer()
  576. {
  577. if (g_profiling_all_threads)
  578. return g_global_perf_events;
  579. if (m_profiling)
  580. return m_perf_event_buffer.ptr();
  581. return nullptr;
  582. }
  583. SpinlockProtected<NonnullOwnPtr<KString>, LockRank::None> m_name;
  584. SpinlockProtected<OwnPtr<Memory::AddressSpace>, LockRank::None> m_space;
  585. RecursiveSpinlock<LockRank::None> mutable m_protected_data_lock;
  586. AtomicEdgeAction<u32> m_protected_data_refs;
  587. void protect_data();
  588. void unprotect_data();
  589. OwnPtr<ThreadTracer> m_tracer;
  590. public:
  591. class OpenFileDescriptionAndFlags {
  592. public:
  593. bool is_valid() const { return !m_description.is_null(); }
  594. bool is_allocated() const { return m_is_allocated; }
  595. void allocate()
  596. {
  597. VERIFY(!m_is_allocated);
  598. VERIFY(!is_valid());
  599. m_is_allocated = true;
  600. }
  601. void deallocate()
  602. {
  603. VERIFY(m_is_allocated);
  604. VERIFY(!is_valid());
  605. m_is_allocated = false;
  606. }
  607. OpenFileDescription* description() { return m_description; }
  608. OpenFileDescription const* description() const { return m_description; }
  609. u32 flags() const { return m_flags; }
  610. void set_flags(u32 flags) { m_flags = flags; }
  611. void clear();
  612. void set(NonnullRefPtr<OpenFileDescription>, u32 flags = 0);
  613. private:
  614. RefPtr<OpenFileDescription> m_description;
  615. bool m_is_allocated { false };
  616. u32 m_flags { 0 };
  617. };
  618. class ScopedDescriptionAllocation;
  619. class OpenFileDescriptions {
  620. AK_MAKE_NONCOPYABLE(OpenFileDescriptions);
  621. AK_MAKE_NONMOVABLE(OpenFileDescriptions);
  622. friend class Process;
  623. public:
  624. OpenFileDescriptions() { }
  625. ALWAYS_INLINE OpenFileDescriptionAndFlags const& operator[](size_t i) const { return at(i); }
  626. ALWAYS_INLINE OpenFileDescriptionAndFlags& operator[](size_t i) { return at(i); }
  627. ErrorOr<void> try_clone(Kernel::Process::OpenFileDescriptions const& other)
  628. {
  629. TRY(try_resize(other.m_fds_metadatas.size()));
  630. for (size_t i = 0; i < other.m_fds_metadatas.size(); ++i) {
  631. m_fds_metadatas[i] = other.m_fds_metadatas[i];
  632. }
  633. return {};
  634. }
  635. OpenFileDescriptionAndFlags const& at(size_t i) const;
  636. OpenFileDescriptionAndFlags& at(size_t i);
  637. OpenFileDescriptionAndFlags const* get_if_valid(size_t i) const;
  638. OpenFileDescriptionAndFlags* get_if_valid(size_t i);
  639. void enumerate(Function<void(OpenFileDescriptionAndFlags const&)>) const;
  640. ErrorOr<void> try_enumerate(Function<ErrorOr<void>(OpenFileDescriptionAndFlags const&)>) const;
  641. void change_each(Function<void(OpenFileDescriptionAndFlags&)>);
  642. ErrorOr<ScopedDescriptionAllocation> allocate(int first_candidate_fd = 0);
  643. size_t open_count() const;
  644. ErrorOr<void> try_resize(size_t size) { return m_fds_metadatas.try_resize(size); }
  645. static constexpr size_t max_open()
  646. {
  647. return s_max_open_file_descriptors;
  648. }
  649. void clear()
  650. {
  651. m_fds_metadatas.clear();
  652. }
  653. ErrorOr<NonnullRefPtr<OpenFileDescription>> open_file_description(int fd) const;
  654. private:
  655. static constexpr size_t s_max_open_file_descriptors { FD_SETSIZE };
  656. Vector<OpenFileDescriptionAndFlags> m_fds_metadatas;
  657. };
  658. class ScopedDescriptionAllocation {
  659. AK_MAKE_NONCOPYABLE(ScopedDescriptionAllocation);
  660. public:
  661. ScopedDescriptionAllocation() = default;
  662. ScopedDescriptionAllocation(int tracked_fd, OpenFileDescriptionAndFlags* description)
  663. : fd(tracked_fd)
  664. , m_description(description)
  665. {
  666. }
  667. ScopedDescriptionAllocation(ScopedDescriptionAllocation&& other)
  668. : fd(other.fd)
  669. {
  670. // Take over the responsibility of tracking to deallocation.
  671. swap(m_description, other.m_description);
  672. }
  673. ScopedDescriptionAllocation& operator=(ScopedDescriptionAllocation&& other)
  674. {
  675. if (this != &other) {
  676. m_description = exchange(other.m_description, nullptr);
  677. fd = exchange(other.fd, -1);
  678. }
  679. return *this;
  680. }
  681. ~ScopedDescriptionAllocation()
  682. {
  683. if (m_description && m_description->is_allocated() && !m_description->is_valid()) {
  684. m_description->deallocate();
  685. }
  686. }
  687. int fd { -1 };
  688. private:
  689. OpenFileDescriptionAndFlags* m_description { nullptr };
  690. };
  691. MutexProtected<OpenFileDescriptions>& fds() { return m_fds; }
  692. MutexProtected<OpenFileDescriptions> const& fds() const { return m_fds; }
  693. ErrorOr<NonnullRefPtr<OpenFileDescription>> open_file_description(int fd)
  694. {
  695. return m_fds.with_shared([fd](auto& fds) { return fds.open_file_description(fd); });
  696. }
  697. ErrorOr<NonnullRefPtr<OpenFileDescription>> open_file_description(int fd) const
  698. {
  699. return m_fds.with_shared([fd](auto& fds) { return fds.open_file_description(fd); });
  700. }
  701. ErrorOr<ScopedDescriptionAllocation> allocate_fd()
  702. {
  703. return m_fds.with_exclusive([](auto& fds) { return fds.allocate(); });
  704. }
  705. private:
  706. ErrorOr<NonnullRefPtr<Custody>> custody_for_dirfd(int dirfd);
  707. SpinlockProtected<Thread::ListInProcess, LockRank::None>& thread_list() { return m_thread_list; }
  708. SpinlockProtected<Thread::ListInProcess, LockRank::None> const& thread_list() const { return m_thread_list; }
  709. ErrorOr<NonnullRefPtr<Thread>> get_thread_from_pid_or_tid(pid_t pid_or_tid, Syscall::SchedulerParametersMode mode);
  710. SpinlockProtected<Thread::ListInProcess, LockRank::None> m_thread_list {};
  711. MutexProtected<OpenFileDescriptions> m_fds;
  712. bool const m_is_kernel_process;
  713. Atomic<State> m_state { State::Running };
  714. bool m_profiling { false };
  715. Atomic<bool, AK::MemoryOrder::memory_order_relaxed> m_is_stopped { false };
  716. bool m_should_generate_coredump { false };
  717. SpinlockProtected<RefPtr<Custody>, LockRank::None> m_executable;
  718. SpinlockProtected<RefPtr<Custody>, LockRank::None> m_current_directory;
  719. Vector<NonnullOwnPtr<KString>> m_arguments;
  720. Vector<NonnullOwnPtr<KString>> m_environment;
  721. LockWeakPtr<Memory::Region> m_master_tls_region;
  722. IntrusiveListNode<Process> m_jail_process_list_node;
  723. IntrusiveListNode<Process> m_all_processes_list_node;
  724. public:
  725. using AllProcessesList = IntrusiveListRelaxedConst<&Process::m_all_processes_list_node>;
  726. using JailProcessList = IntrusiveListRelaxedConst<&Process::m_jail_process_list_node>;
  727. private:
  728. SpinlockProtected<RefPtr<ProcessList>, LockRank::None> m_jail_process_list;
  729. SpinlockProtected<RefPtr<Jail>, LockRank::Process> m_attached_jail {};
  730. size_t m_master_tls_size { 0 };
  731. size_t m_master_tls_alignment { 0 };
  732. Mutex m_big_lock { "Process"sv, Mutex::MutexBehavior::BigLock };
  733. Mutex m_ptrace_lock { "ptrace"sv };
  734. SpinlockProtected<RefPtr<Timer>, LockRank::None> m_alarm_timer;
  735. SpinlockProtected<UnveilData, LockRank::None> m_unveil_data;
  736. SpinlockProtected<UnveilData, LockRank::None> m_exec_unveil_data;
  737. OwnPtr<PerformanceEventBuffer> m_perf_event_buffer;
  738. // This member is used in the implementation of ptrace's PT_TRACEME flag.
  739. // If it is set to true, the process will stop at the next execve syscall
  740. // and wait for a tracer to attach.
  741. bool m_wait_for_tracer_at_next_execve { false };
  742. Thread::WaitBlockerSet m_wait_blocker_set;
  743. struct CoredumpProperty {
  744. OwnPtr<KString> key;
  745. OwnPtr<KString> value;
  746. };
  747. SpinlockProtected<Array<CoredumpProperty, 4>, LockRank::None> m_coredump_properties {};
  748. Vector<NonnullRefPtr<Thread>> m_threads_for_coredump;
  749. struct SignalActionData {
  750. VirtualAddress handler_or_sigaction;
  751. int flags { 0 };
  752. u32 mask { 0 };
  753. };
  754. Array<SignalActionData, NSIG> m_signal_action_data;
  755. static_assert(sizeof(ProtectedValues) < (PAGE_SIZE));
  756. alignas(4096) ProtectedValues m_protected_values_do_not_access_directly;
  757. u8 m_protected_values_padding[PAGE_SIZE - sizeof(ProtectedValues)];
  758. public:
  759. static SpinlockProtected<Process::AllProcessesList, LockRank::None>& all_instances();
  760. };
  761. class ProcessList : public RefCounted<ProcessList> {
  762. public:
  763. static ErrorOr<NonnullRefPtr<ProcessList>> create();
  764. SpinlockProtected<Process::JailProcessList, LockRank::None>& attached_processes() { return m_attached_processes; }
  765. SpinlockProtected<Process::JailProcessList, LockRank::None> const& attached_processes() const { return m_attached_processes; }
  766. private:
  767. ProcessList() = default;
  768. SpinlockProtected<Process::JailProcessList, LockRank::None> m_attached_processes;
  769. };
  770. // Note: Process object should be 2 pages of 4096 bytes each.
  771. // It's not expected that the Process object will expand further because the first
  772. // page is used for all unprotected values (which should be plenty of space for them).
  773. // The second page is being used exclusively for write-protected values.
  774. static_assert(AssertSize<Process, (PAGE_SIZE * 2)>());
  775. extern RecursiveSpinlock<LockRank::None> g_profiling_lock;
  776. template<IteratorFunction<Thread&> Callback>
  777. inline IterationDecision Process::for_each_thread(Callback callback)
  778. {
  779. return thread_list().with([&](auto& thread_list) -> IterationDecision {
  780. for (auto& thread : thread_list) {
  781. IterationDecision decision = callback(thread);
  782. if (decision != IterationDecision::Continue)
  783. return decision;
  784. }
  785. return IterationDecision::Continue;
  786. });
  787. }
  788. template<IteratorFunction<Process&> Callback>
  789. inline void Process::for_each_ignoring_jails(Callback callback)
  790. {
  791. Process::all_instances().with([&](auto const& list) {
  792. for (auto it = list.begin(); it != list.end();) {
  793. auto& process = *it;
  794. ++it;
  795. if (callback(process) == IterationDecision::Break)
  796. break;
  797. }
  798. });
  799. }
  800. template<IteratorFunction<Thread&> Callback>
  801. inline IterationDecision Process::for_each_thread(Callback callback) const
  802. {
  803. return thread_list().with([&](auto& thread_list) -> IterationDecision {
  804. for (auto& thread : thread_list) {
  805. IterationDecision decision = callback(thread);
  806. if (decision != IterationDecision::Continue)
  807. return decision;
  808. }
  809. return IterationDecision::Continue;
  810. });
  811. }
  812. template<VoidFunction<Thread&> Callback>
  813. inline IterationDecision Process::for_each_thread(Callback callback) const
  814. {
  815. thread_list().with([&](auto& thread_list) {
  816. for (auto& thread : thread_list)
  817. callback(thread);
  818. });
  819. return IterationDecision::Continue;
  820. }
  821. inline ErrorOr<void> Process::try_for_each_thread(Function<ErrorOr<void>(Thread const&)> callback) const
  822. {
  823. return thread_list().with([&](auto& thread_list) -> ErrorOr<void> {
  824. for (auto& thread : thread_list)
  825. TRY(callback(thread));
  826. return {};
  827. });
  828. }
  829. template<VoidFunction<Thread&> Callback>
  830. inline IterationDecision Process::for_each_thread(Callback callback)
  831. {
  832. thread_list().with([&](auto& thread_list) {
  833. for (auto& thread : thread_list)
  834. callback(thread);
  835. });
  836. return IterationDecision::Continue;
  837. }
  838. inline ProcessID Thread::pid() const
  839. {
  840. return m_process->pid();
  841. }
  842. }
  843. #define VERIFY_PROCESS_BIG_LOCK_ACQUIRED(process) \
  844. VERIFY(process->big_lock().is_exclusively_locked_by_current_thread())
  845. #define VERIFY_NO_PROCESS_BIG_LOCK(process) \
  846. VERIFY(!process->big_lock().is_exclusively_locked_by_current_thread())
  847. inline ErrorOr<NonnullOwnPtr<KString>> try_copy_kstring_from_user(Kernel::Syscall::StringArgument const& string)
  848. {
  849. Userspace<char const*> characters((FlatPtr)string.characters);
  850. return try_copy_kstring_from_user(characters, string.length);
  851. }
  852. template<>
  853. struct AK::Formatter<Kernel::Process> : AK::Formatter<FormatString> {
  854. ErrorOr<void> format(FormatBuilder& builder, Kernel::Process const& value)
  855. {
  856. return value.name().with([&](auto& process_name) {
  857. return AK::Formatter<FormatString>::format(builder, "{}({})"sv, process_name->view(), value.pid().value());
  858. });
  859. }
  860. };
  861. namespace AK {
  862. template<>
  863. struct Traits<Kernel::GlobalFutexKey> : public GenericTraits<Kernel::GlobalFutexKey> {
  864. static unsigned hash(Kernel::GlobalFutexKey const& futex_key) { return pair_int_hash(ptr_hash(futex_key.raw.parent), ptr_hash(futex_key.raw.offset)); }
  865. static bool equals(Kernel::GlobalFutexKey const& a, Kernel::GlobalFutexKey const& b) { return a.raw.parent == b.raw.parent && a.raw.offset == b.raw.offset; }
  866. };
  867. };