Process.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Timon Kruiper <timonkruiper@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Singleton.h>
  8. #include <AK/StdLibExtras.h>
  9. #include <AK/Time.h>
  10. #include <AK/Types.h>
  11. #include <Kernel/API/Syscall.h>
  12. #include <Kernel/Debug.h>
  13. #include <Kernel/Devices/DeviceManagement.h>
  14. #include <Kernel/Interrupts/InterruptDisabler.h>
  15. #include <Kernel/Security/Credentials.h>
  16. #include <Kernel/Tasks/Coredump.h>
  17. #ifdef ENABLE_KERNEL_COVERAGE_COLLECTION
  18. # include <Kernel/Devices/KCOVDevice.h>
  19. #endif
  20. #include <Kernel/API/POSIX/errno.h>
  21. #include <Kernel/API/POSIX/sys/limits.h>
  22. #include <Kernel/Arch/PageDirectory.h>
  23. #include <Kernel/Devices/Generic/NullDevice.h>
  24. #include <Kernel/FileSystem/Custody.h>
  25. #include <Kernel/FileSystem/OpenFileDescription.h>
  26. #include <Kernel/FileSystem/VirtualFileSystem.h>
  27. #include <Kernel/KSyms.h>
  28. #include <Kernel/Library/KBufferBuilder.h>
  29. #include <Kernel/Library/Panic.h>
  30. #include <Kernel/Library/StdLib.h>
  31. #include <Kernel/Memory/AnonymousVMObject.h>
  32. #include <Kernel/Memory/SharedInodeVMObject.h>
  33. #include <Kernel/Sections.h>
  34. #include <Kernel/TTY/TTY.h>
  35. #include <Kernel/Tasks/PerformanceEventBuffer.h>
  36. #include <Kernel/Tasks/PerformanceManager.h>
  37. #include <Kernel/Tasks/Process.h>
  38. #include <Kernel/Tasks/Scheduler.h>
  39. #include <Kernel/Tasks/Thread.h>
  40. #include <Kernel/Tasks/ThreadTracer.h>
  41. #include <Kernel/Time/TimerQueue.h>
  42. namespace Kernel {
  43. static void create_signal_trampoline();
  44. extern ProcessID g_init_pid;
  45. RecursiveSpinlock<LockRank::None> g_profiling_lock {};
  46. static Atomic<pid_t> next_pid;
  47. static Singleton<SpinlockProtected<Process::AllProcessesList, LockRank::None>> s_all_instances;
  48. READONLY_AFTER_INIT Memory::Region* g_signal_trampoline_region;
  49. static Singleton<MutexProtected<OwnPtr<KString>>> s_hostname;
  50. MutexProtected<OwnPtr<KString>>& hostname()
  51. {
  52. return *s_hostname;
  53. }
  54. SpinlockProtected<Process::AllProcessesList, LockRank::None>& Process::all_instances()
  55. {
  56. return *s_all_instances;
  57. }
  58. ErrorOr<void> Process::for_each_in_same_jail(Function<ErrorOr<void>(Process&)> callback)
  59. {
  60. return Process::current().m_jail_process_list.with([&](auto const& list_ptr) -> ErrorOr<void> {
  61. ErrorOr<void> result {};
  62. if (list_ptr) {
  63. list_ptr->attached_processes().with([&](auto const& list) {
  64. for (auto& process : list) {
  65. result = callback(process);
  66. if (result.is_error())
  67. break;
  68. }
  69. });
  70. return result;
  71. }
  72. all_instances().with([&](auto const& list) {
  73. for (auto& process : list) {
  74. result = callback(process);
  75. if (result.is_error())
  76. break;
  77. }
  78. });
  79. return result;
  80. });
  81. }
  82. ErrorOr<void> Process::for_each_child_in_same_jail(Function<ErrorOr<void>(Process&)> callback)
  83. {
  84. ProcessID my_pid = pid();
  85. return m_jail_process_list.with([&](auto const& list_ptr) -> ErrorOr<void> {
  86. ErrorOr<void> result {};
  87. if (list_ptr) {
  88. list_ptr->attached_processes().with([&](auto const& list) {
  89. for (auto& process : list) {
  90. if (process.ppid() == my_pid || process.has_tracee_thread(pid()))
  91. result = callback(process);
  92. if (result.is_error())
  93. break;
  94. }
  95. });
  96. return result;
  97. }
  98. all_instances().with([&](auto const& list) {
  99. for (auto& process : list) {
  100. if (process.ppid() == my_pid || process.has_tracee_thread(pid()))
  101. result = callback(process);
  102. if (result.is_error())
  103. break;
  104. }
  105. });
  106. return result;
  107. });
  108. }
  109. ErrorOr<void> Process::for_each_in_pgrp_in_same_jail(ProcessGroupID pgid, Function<ErrorOr<void>(Process&)> callback)
  110. {
  111. return m_jail_process_list.with([&](auto const& list_ptr) -> ErrorOr<void> {
  112. ErrorOr<void> result {};
  113. if (list_ptr) {
  114. list_ptr->attached_processes().with([&](auto const& list) {
  115. for (auto& process : list) {
  116. if (!process.is_dead() && process.pgid() == pgid)
  117. result = callback(process);
  118. if (result.is_error())
  119. break;
  120. }
  121. });
  122. return result;
  123. }
  124. all_instances().with([&](auto const& list) {
  125. for (auto& process : list) {
  126. if (!process.is_dead() && process.pgid() == pgid)
  127. result = callback(process);
  128. if (result.is_error())
  129. break;
  130. }
  131. });
  132. return result;
  133. });
  134. }
  135. ProcessID Process::allocate_pid()
  136. {
  137. // Overflow is UB, and negative PIDs wreck havoc.
  138. // TODO: Handle PID overflow
  139. // For example: Use an Atomic<u32>, mask the most significant bit,
  140. // retry if PID is already taken as a PID, taken as a TID,
  141. // takes as a PGID, taken as a SID, or zero.
  142. return next_pid.fetch_add(1, AK::MemoryOrder::memory_order_acq_rel);
  143. }
  144. UNMAP_AFTER_INIT void Process::initialize()
  145. {
  146. next_pid.store(0, AK::MemoryOrder::memory_order_release);
  147. // Note: This is called before scheduling is initialized, and before APs are booted.
  148. // So we can "safely" bypass the lock here.
  149. reinterpret_cast<OwnPtr<KString>&>(hostname()) = KString::must_create("courage"sv);
  150. create_signal_trampoline();
  151. }
  152. void Process::kill_threads_except_self()
  153. {
  154. InterruptDisabler disabler;
  155. if (thread_count() <= 1)
  156. return;
  157. auto* current_thread = Thread::current();
  158. for_each_thread([&](Thread& thread) {
  159. if (&thread == current_thread)
  160. return;
  161. if (auto state = thread.state(); state == Thread::State::Dead
  162. || state == Thread::State::Dying)
  163. return;
  164. // We need to detach this thread in case it hasn't been joined
  165. thread.detach();
  166. thread.set_should_die();
  167. });
  168. u32 dropped_lock_count = 0;
  169. if (big_lock().force_unlock_exclusive_if_locked(dropped_lock_count) != LockMode::Unlocked)
  170. dbgln("Process {} big lock had {} locks", *this, dropped_lock_count);
  171. }
  172. void Process::kill_all_threads()
  173. {
  174. for_each_thread([&](Thread& thread) {
  175. // We need to detach this thread in case it hasn't been joined
  176. thread.detach();
  177. thread.set_should_die();
  178. });
  179. }
  180. void Process::register_new(Process& process)
  181. {
  182. // Note: this is essentially the same like process->ref()
  183. NonnullRefPtr<Process> const new_process = process;
  184. all_instances().with([&](auto& list) {
  185. list.prepend(process);
  186. });
  187. }
  188. ErrorOr<Process::ProcessAndFirstThread> Process::create_user_process(StringView path, UserID uid, GroupID gid, Vector<NonnullOwnPtr<KString>> arguments, Vector<NonnullOwnPtr<KString>> environment, RefPtr<TTY> tty)
  189. {
  190. auto parts = path.split_view('/');
  191. if (arguments.is_empty()) {
  192. auto last_part = TRY(KString::try_create(parts.last()));
  193. TRY(arguments.try_append(move(last_part)));
  194. }
  195. auto path_string = TRY(KString::try_create(path));
  196. auto name = TRY(KString::try_create(parts.last()));
  197. auto [process, first_thread] = TRY(Process::create(move(name), uid, gid, ProcessID(0), false, VirtualFileSystem::the().root_custody(), nullptr, tty));
  198. TRY(process->m_fds.with_exclusive([&](auto& fds) -> ErrorOr<void> {
  199. TRY(fds.try_resize(Process::OpenFileDescriptions::max_open()));
  200. auto& device_to_use_as_tty = tty ? (CharacterDevice&)*tty : DeviceManagement::the().null_device();
  201. auto description = TRY(device_to_use_as_tty.open(O_RDWR));
  202. auto setup_description = [&](int fd) {
  203. fds.m_fds_metadatas[fd].allocate();
  204. fds[fd].set(*description);
  205. };
  206. setup_description(0);
  207. setup_description(1);
  208. setup_description(2);
  209. return {};
  210. }));
  211. Thread* new_main_thread = nullptr;
  212. InterruptsState previous_interrupts_state = InterruptsState::Enabled;
  213. TRY(process->exec(move(path_string), move(arguments), move(environment), new_main_thread, previous_interrupts_state));
  214. register_new(*process);
  215. // NOTE: All user processes have a leaked ref on them. It's balanced by Thread::WaitBlockerSet::finalize().
  216. process->ref();
  217. {
  218. SpinlockLocker lock(g_scheduler_lock);
  219. new_main_thread->set_state(Thread::State::Runnable);
  220. }
  221. return ProcessAndFirstThread { move(process), move(first_thread) };
  222. }
  223. ErrorOr<Process::ProcessAndFirstThread> Process::create_kernel_process(NonnullOwnPtr<KString> name, void (*entry)(void*), void* entry_data, u32 affinity, RegisterProcess do_register)
  224. {
  225. auto process_and_first_thread = TRY(Process::create(move(name), UserID(0), GroupID(0), ProcessID(0), true));
  226. auto& process = *process_and_first_thread.process;
  227. auto& thread = *process_and_first_thread.first_thread;
  228. thread.regs().set_entry_function((FlatPtr)entry, (FlatPtr)entry_data);
  229. if (do_register == RegisterProcess::Yes)
  230. register_new(process);
  231. SpinlockLocker lock(g_scheduler_lock);
  232. thread.set_affinity(affinity);
  233. thread.set_state(Thread::State::Runnable);
  234. return process_and_first_thread;
  235. }
  236. void Process::protect_data()
  237. {
  238. m_protected_data_refs.unref([&]() {
  239. MM.set_page_writable_direct(VirtualAddress { &this->m_protected_values_do_not_access_directly }, false);
  240. });
  241. }
  242. void Process::unprotect_data()
  243. {
  244. m_protected_data_refs.ref([&]() {
  245. MM.set_page_writable_direct(VirtualAddress { &this->m_protected_values_do_not_access_directly }, true);
  246. });
  247. }
  248. ErrorOr<Process::ProcessAndFirstThread> Process::create(NonnullOwnPtr<KString> name, UserID uid, GroupID gid, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> current_directory, RefPtr<Custody> executable, RefPtr<TTY> tty, Process* fork_parent)
  249. {
  250. auto unveil_tree = UnveilNode { TRY(KString::try_create("/"sv)), UnveilMetadata(TRY(KString::try_create("/"sv))) };
  251. auto exec_unveil_tree = UnveilNode { TRY(KString::try_create("/"sv)), UnveilMetadata(TRY(KString::try_create("/"sv))) };
  252. auto credentials = TRY(Credentials::create(uid, gid, uid, gid, uid, gid, {}, fork_parent ? fork_parent->sid() : 0, fork_parent ? fork_parent->pgid() : 0));
  253. auto process = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Process(move(name), move(credentials), ppid, is_kernel_process, move(current_directory), move(executable), tty, move(unveil_tree), move(exec_unveil_tree), kgettimeofday())));
  254. OwnPtr<Memory::AddressSpace> new_address_space;
  255. if (fork_parent) {
  256. TRY(fork_parent->address_space().with([&](auto& parent_address_space) -> ErrorOr<void> {
  257. new_address_space = TRY(Memory::AddressSpace::try_create(*process, parent_address_space.ptr()));
  258. return {};
  259. }));
  260. } else {
  261. new_address_space = TRY(Memory::AddressSpace::try_create(*process, nullptr));
  262. }
  263. auto first_thread = TRY(process->attach_resources(new_address_space.release_nonnull(), fork_parent));
  264. return ProcessAndFirstThread { move(process), move(first_thread) };
  265. }
  266. Process::Process(NonnullOwnPtr<KString> name, NonnullRefPtr<Credentials> credentials, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> current_directory, RefPtr<Custody> executable, RefPtr<TTY> tty, UnveilNode unveil_tree, UnveilNode exec_unveil_tree, UnixDateTime creation_time)
  267. : m_name(move(name))
  268. , m_is_kernel_process(is_kernel_process)
  269. , m_executable(move(executable))
  270. , m_current_directory(move(current_directory))
  271. , m_creation_time(creation_time)
  272. , m_unveil_data(move(unveil_tree))
  273. , m_exec_unveil_data(move(exec_unveil_tree))
  274. , m_wait_blocker_set(*this)
  275. {
  276. // Ensure that we protect the process data when exiting the constructor.
  277. with_mutable_protected_data([&](auto& protected_data) {
  278. protected_data.pid = allocate_pid();
  279. protected_data.ppid = ppid;
  280. protected_data.credentials = move(credentials);
  281. protected_data.tty = move(tty);
  282. });
  283. if constexpr (PROCESS_DEBUG) {
  284. this->name().with([&](auto& process_name) {
  285. dbgln("Created new process {}({})", process_name->view(), this->pid().value());
  286. });
  287. }
  288. }
  289. ErrorOr<NonnullRefPtr<Thread>> Process::attach_resources(NonnullOwnPtr<Memory::AddressSpace>&& preallocated_space, Process* fork_parent)
  290. {
  291. m_space.with([&](auto& space) {
  292. space = move(preallocated_space);
  293. });
  294. auto create_first_thread = [&] {
  295. if (fork_parent) {
  296. // NOTE: fork() doesn't clone all threads; the thread that called fork() becomes the only thread in the new process.
  297. return Thread::current()->clone(*this);
  298. }
  299. // NOTE: This non-forked code path is only taken when the kernel creates a process "manually" (at boot.)
  300. return Thread::create(*this);
  301. };
  302. auto first_thread = TRY(create_first_thread());
  303. if (!fork_parent) {
  304. // FIXME: Figure out if this is really necessary.
  305. first_thread->detach();
  306. }
  307. // This is not actually explicitly verified by any official documentation,
  308. // but it's not listed anywhere as being cleared, and rsync expects it to work like this.
  309. if (fork_parent)
  310. m_signal_action_data = fork_parent->m_signal_action_data;
  311. return first_thread;
  312. }
  313. Process::~Process()
  314. {
  315. unprotect_data();
  316. VERIFY(thread_count() == 0); // all threads should have been finalized
  317. PerformanceManager::add_process_exit_event(*this);
  318. }
  319. // Make sure the compiler doesn't "optimize away" this function:
  320. extern void signal_trampoline_dummy() __attribute__((used));
  321. void signal_trampoline_dummy()
  322. {
  323. #if ARCH(X86_64)
  324. // The trampoline preserves the current rax, pushes the signal code and
  325. // then calls the signal handler. We do this because, when interrupting a
  326. // blocking syscall, that syscall may return some special error code in eax;
  327. // This error code would likely be overwritten by the signal handler, so it's
  328. // necessary to preserve it here.
  329. constexpr static auto offset_to_first_register_slot = sizeof(__ucontext) + sizeof(siginfo) + sizeof(FPUState) + 3 * sizeof(FlatPtr);
  330. asm(
  331. ".intel_syntax noprefix\n"
  332. ".globl asm_signal_trampoline\n"
  333. "asm_signal_trampoline:\n"
  334. // stack state: 0, ucontext, signal_info (alignment = 16), fpu_state (alignment = 16), ucontext*, siginfo*, signal, handler
  335. // Pop the handler into rcx
  336. "pop rcx\n" // save handler
  337. // we have to save rax 'cause it might be the return value from a syscall
  338. "mov [rsp+%P1], rax\n"
  339. // pop signal number into rdi (first param)
  340. "pop rdi\n"
  341. // pop siginfo* into rsi (second param)
  342. "pop rsi\n"
  343. // pop ucontext* into rdx (third param)
  344. "pop rdx\n"
  345. // Note that the stack is currently aligned to 16 bytes as we popped the extra entries above.
  346. // call the signal handler
  347. "call rcx\n"
  348. // Current stack state is just saved_rax, ucontext, signal_info, fpu_state.
  349. // syscall SC_sigreturn
  350. "mov rax, %P0\n"
  351. "syscall\n"
  352. ".globl asm_signal_trampoline_end\n"
  353. "asm_signal_trampoline_end:\n"
  354. ".att_syntax"
  355. :
  356. : "i"(Syscall::SC_sigreturn),
  357. "i"(offset_to_first_register_slot));
  358. #elif ARCH(AARCH64)
  359. constexpr static auto offset_to_first_register_slot = align_up_to(sizeof(__ucontext) + sizeof(siginfo) + sizeof(FPUState) + 3 * sizeof(FlatPtr), 16);
  360. asm(
  361. ".global asm_signal_trampoline\n"
  362. "asm_signal_trampoline:\n"
  363. // stack state: 0, ucontext, signal_info (alignment = 16), fpu_state (alignment = 16), ucontext*, siginfo*, signal, handler
  364. // Load the handler address into x3.
  365. "ldr x3, [sp, #0]\n"
  366. // Store x0 (return value from a syscall) into the register slot, such that we can return the correct value in sys$sigreturn.
  367. "str x0, [sp, %[offset_to_first_register_slot]]\n"
  368. // Load the signal number into the first argument.
  369. "ldr x0, [sp, #8]\n"
  370. // Load a pointer to the signal_info structure into the second argument.
  371. "ldr x1, [sp, #16]\n"
  372. // Load a pointer to the ucontext into the third argument.
  373. "ldr x2, [sp, #24]\n"
  374. // Pop the values off the stack.
  375. "add sp, sp, 32\n"
  376. // Call the signal handler.
  377. "blr x3\n"
  378. // Call sys$sigreturn.
  379. "mov x8, %[sigreturn_syscall_number]\n"
  380. "svc #0\n"
  381. // We should never return, so trap if we do return.
  382. "brk #0\n"
  383. "\n"
  384. ".global asm_signal_trampoline_end\n"
  385. "asm_signal_trampoline_end: \n" ::[sigreturn_syscall_number] "i"(Syscall::SC_sigreturn),
  386. [offset_to_first_register_slot] "i"(offset_to_first_register_slot));
  387. #else
  388. # error Unknown architecture
  389. #endif
  390. }
  391. extern "C" char const asm_signal_trampoline[];
  392. extern "C" char const asm_signal_trampoline_end[];
  393. void create_signal_trampoline()
  394. {
  395. // NOTE: We leak this region.
  396. g_signal_trampoline_region = MM.allocate_kernel_region(PAGE_SIZE, "Signal trampolines"sv, Memory::Region::Access::ReadWrite).release_value().leak_ptr();
  397. g_signal_trampoline_region->set_syscall_region(true);
  398. size_t trampoline_size = asm_signal_trampoline_end - asm_signal_trampoline;
  399. u8* code_ptr = (u8*)g_signal_trampoline_region->vaddr().as_ptr();
  400. memcpy(code_ptr, asm_signal_trampoline, trampoline_size);
  401. g_signal_trampoline_region->set_writable(false);
  402. g_signal_trampoline_region->remap();
  403. }
  404. void Process::crash(int signal, Optional<RegisterState const&> regs, bool out_of_memory)
  405. {
  406. VERIFY(!is_dead());
  407. VERIFY(&Process::current() == this);
  408. auto ip = regs.has_value() ? regs->ip() : 0;
  409. if (out_of_memory) {
  410. dbgln("\033[31;1mOut of memory\033[m, killing: {}", *this);
  411. } else {
  412. if (ip >= kernel_load_base && g_kernel_symbols_available) {
  413. auto const* symbol = symbolicate_kernel_address(ip);
  414. dbgln("\033[31;1m{:p} {} +{}\033[0m\n", ip, (symbol ? symbol->name : "(k?)"), (symbol ? ip - symbol->address : 0));
  415. } else {
  416. dbgln("\033[31;1m{:p} (?)\033[0m\n", ip);
  417. }
  418. #if ARCH(X86_64)
  419. constexpr bool userspace_backtrace = false;
  420. #elif ARCH(AARCH64)
  421. constexpr bool userspace_backtrace = true;
  422. #else
  423. # error "Unknown architecture"
  424. #endif
  425. if constexpr (userspace_backtrace) {
  426. dbgln("Userspace backtrace:");
  427. auto bp = regs.has_value() ? regs->bp() : 0;
  428. dump_backtrace_from_base_pointer(bp);
  429. }
  430. dbgln("Kernel backtrace:");
  431. dump_backtrace();
  432. }
  433. with_mutable_protected_data([&](auto& protected_data) {
  434. protected_data.termination_signal = signal;
  435. });
  436. set_should_generate_coredump(!out_of_memory);
  437. if constexpr (DUMP_REGIONS_ON_CRASH) {
  438. address_space().with([](auto& space) { space->dump_regions(); });
  439. }
  440. VERIFY(is_user_process());
  441. die();
  442. // We can not return from here, as there is nowhere
  443. // to unwind to, so die right away.
  444. Thread::current()->die_if_needed();
  445. VERIFY_NOT_REACHED();
  446. }
  447. RefPtr<Process> Process::from_pid_in_same_jail(ProcessID pid)
  448. {
  449. return Process::current().m_jail_process_list.with([&](auto const& list_ptr) -> RefPtr<Process> {
  450. if (list_ptr) {
  451. return list_ptr->attached_processes().with([&](auto const& list) -> RefPtr<Process> {
  452. for (auto& process : list) {
  453. if (process.pid() == pid) {
  454. return process;
  455. }
  456. }
  457. return {};
  458. });
  459. }
  460. return all_instances().with([&](auto const& list) -> RefPtr<Process> {
  461. for (auto& process : list) {
  462. if (process.pid() == pid) {
  463. return process;
  464. }
  465. }
  466. return {};
  467. });
  468. });
  469. }
  470. RefPtr<Process> Process::from_pid_ignoring_jails(ProcessID pid)
  471. {
  472. return all_instances().with([&](auto const& list) -> RefPtr<Process> {
  473. for (auto const& process : list) {
  474. if (process.pid() == pid)
  475. return &process;
  476. }
  477. return {};
  478. });
  479. }
  480. Process::OpenFileDescriptionAndFlags const* Process::OpenFileDescriptions::get_if_valid(size_t i) const
  481. {
  482. if (m_fds_metadatas.size() <= i)
  483. return nullptr;
  484. if (auto const& metadata = m_fds_metadatas[i]; metadata.is_valid())
  485. return &metadata;
  486. return nullptr;
  487. }
  488. Process::OpenFileDescriptionAndFlags* Process::OpenFileDescriptions::get_if_valid(size_t i)
  489. {
  490. if (m_fds_metadatas.size() <= i)
  491. return nullptr;
  492. if (auto& metadata = m_fds_metadatas[i]; metadata.is_valid())
  493. return &metadata;
  494. return nullptr;
  495. }
  496. Process::OpenFileDescriptionAndFlags const& Process::OpenFileDescriptions::at(size_t i) const
  497. {
  498. VERIFY(m_fds_metadatas[i].is_allocated());
  499. return m_fds_metadatas[i];
  500. }
  501. Process::OpenFileDescriptionAndFlags& Process::OpenFileDescriptions::at(size_t i)
  502. {
  503. VERIFY(m_fds_metadatas[i].is_allocated());
  504. return m_fds_metadatas[i];
  505. }
  506. ErrorOr<NonnullRefPtr<OpenFileDescription>> Process::OpenFileDescriptions::open_file_description(int fd) const
  507. {
  508. if (fd < 0)
  509. return EBADF;
  510. if (static_cast<size_t>(fd) >= m_fds_metadatas.size())
  511. return EBADF;
  512. RefPtr description = m_fds_metadatas[fd].description();
  513. if (!description)
  514. return EBADF;
  515. return description.release_nonnull();
  516. }
  517. void Process::OpenFileDescriptions::enumerate(Function<void(OpenFileDescriptionAndFlags const&)> callback) const
  518. {
  519. for (auto const& file_description_metadata : m_fds_metadatas) {
  520. callback(file_description_metadata);
  521. }
  522. }
  523. ErrorOr<void> Process::OpenFileDescriptions::try_enumerate(Function<ErrorOr<void>(OpenFileDescriptionAndFlags const&)> callback) const
  524. {
  525. for (auto const& file_description_metadata : m_fds_metadatas) {
  526. TRY(callback(file_description_metadata));
  527. }
  528. return {};
  529. }
  530. void Process::OpenFileDescriptions::change_each(Function<void(OpenFileDescriptionAndFlags&)> callback)
  531. {
  532. for (auto& file_description_metadata : m_fds_metadatas) {
  533. callback(file_description_metadata);
  534. }
  535. }
  536. size_t Process::OpenFileDescriptions::open_count() const
  537. {
  538. size_t count = 0;
  539. enumerate([&](auto& file_description_metadata) {
  540. if (file_description_metadata.is_valid())
  541. ++count;
  542. });
  543. return count;
  544. }
  545. ErrorOr<Process::ScopedDescriptionAllocation> Process::OpenFileDescriptions::allocate(int first_candidate_fd)
  546. {
  547. for (size_t i = first_candidate_fd; i < max_open(); ++i) {
  548. if (!m_fds_metadatas[i].is_allocated()) {
  549. m_fds_metadatas[i].allocate();
  550. return Process::ScopedDescriptionAllocation { static_cast<int>(i), &m_fds_metadatas[i] };
  551. }
  552. }
  553. return EMFILE;
  554. }
  555. UnixDateTime kgettimeofday()
  556. {
  557. return TimeManagement::now();
  558. }
  559. siginfo_t Process::wait_info() const
  560. {
  561. auto credentials = this->credentials();
  562. siginfo_t siginfo {};
  563. siginfo.si_signo = SIGCHLD;
  564. siginfo.si_pid = pid().value();
  565. siginfo.si_uid = credentials->uid().value();
  566. with_protected_data([&](auto& protected_data) {
  567. if (protected_data.termination_signal != 0) {
  568. siginfo.si_status = protected_data.termination_signal;
  569. siginfo.si_code = CLD_KILLED;
  570. } else {
  571. siginfo.si_status = protected_data.termination_status;
  572. siginfo.si_code = CLD_EXITED;
  573. }
  574. });
  575. return siginfo;
  576. }
  577. NonnullRefPtr<Custody> Process::current_directory()
  578. {
  579. return m_current_directory.with([&](auto& current_directory) -> NonnullRefPtr<Custody> {
  580. if (!current_directory)
  581. current_directory = VirtualFileSystem::the().root_custody();
  582. return *current_directory;
  583. });
  584. }
  585. ErrorOr<NonnullOwnPtr<KString>> Process::get_syscall_path_argument(Userspace<char const*> user_path, size_t path_length)
  586. {
  587. if (path_length == 0)
  588. return EINVAL;
  589. if (path_length > PATH_MAX)
  590. return ENAMETOOLONG;
  591. return try_copy_kstring_from_user(user_path, path_length);
  592. }
  593. ErrorOr<NonnullOwnPtr<KString>> Process::get_syscall_path_argument(Syscall::StringArgument const& path)
  594. {
  595. Userspace<char const*> path_characters((FlatPtr)path.characters);
  596. return get_syscall_path_argument(path_characters, path.length);
  597. }
  598. ErrorOr<void> Process::dump_core()
  599. {
  600. VERIFY(is_dumpable());
  601. VERIFY(should_generate_coredump());
  602. dbgln("Generating coredump for pid: {}", pid().value());
  603. auto coredump_directory_path = TRY(Coredump::directory_path().with([&](auto& coredump_directory_path) -> ErrorOr<NonnullOwnPtr<KString>> {
  604. if (coredump_directory_path)
  605. return KString::try_create(coredump_directory_path->view());
  606. return KString::try_create(""sv);
  607. }));
  608. if (coredump_directory_path->view() == ""sv) {
  609. dbgln("Generating coredump for pid {} failed because coredump directory was not set.", pid().value());
  610. return {};
  611. }
  612. auto coredump_path = TRY(name().with([&](auto& process_name) {
  613. return KString::formatted("{}/{}_{}_{}", coredump_directory_path->view(), process_name->view(), pid().value(), kgettimeofday().seconds_since_epoch());
  614. }));
  615. auto coredump = TRY(Coredump::try_create(*this, coredump_path->view()));
  616. return coredump->write();
  617. }
  618. ErrorOr<void> Process::dump_perfcore()
  619. {
  620. VERIFY(is_dumpable());
  621. VERIFY(m_perf_event_buffer);
  622. dbgln("Generating perfcore for pid: {}", pid().value());
  623. // Try to generate a filename which isn't already used.
  624. auto base_filename = TRY(name().with([&](auto& process_name) {
  625. return KString::formatted("{}_{}", process_name->view(), pid().value());
  626. }));
  627. auto perfcore_filename = TRY(KString::formatted("{}.profile", base_filename));
  628. RefPtr<OpenFileDescription> description;
  629. auto credentials = this->credentials();
  630. for (size_t attempt = 1; attempt <= 10; ++attempt) {
  631. auto description_or_error = VirtualFileSystem::the().open(*this, credentials, perfcore_filename->view(), O_CREAT | O_EXCL, 0400, current_directory(), UidAndGid { 0, 0 });
  632. if (!description_or_error.is_error()) {
  633. description = description_or_error.release_value();
  634. break;
  635. }
  636. perfcore_filename = TRY(KString::formatted("{}.{}.profile", base_filename, attempt));
  637. }
  638. if (!description) {
  639. dbgln("Failed to generate perfcore for pid {}: Could not generate filename for the perfcore file.", pid().value());
  640. return EEXIST;
  641. }
  642. auto builder = TRY(KBufferBuilder::try_create());
  643. TRY(m_perf_event_buffer->to_json(builder));
  644. auto json = builder.build();
  645. if (!json) {
  646. dbgln("Failed to generate perfcore for pid {}: Could not allocate buffer.", pid().value());
  647. return ENOMEM;
  648. }
  649. auto json_buffer = UserOrKernelBuffer::for_kernel_buffer(json->data());
  650. TRY(description->write(json_buffer, json->size()));
  651. dbgln("Wrote perfcore for pid {} to {}", pid().value(), perfcore_filename);
  652. return {};
  653. }
  654. void Process::finalize()
  655. {
  656. VERIFY(Thread::current() == g_finalizer);
  657. dbgln_if(PROCESS_DEBUG, "Finalizing process {}", *this);
  658. if (veil_state() == VeilState::Dropped) {
  659. name().with([&](auto& process_name) {
  660. dbgln("\x1b[01;31mProcess '{}' exited with the veil left open\x1b[0m", process_name->view());
  661. });
  662. }
  663. if (g_init_pid != 0 && pid() == g_init_pid)
  664. PANIC("Init process quit unexpectedly. Exit code: {}", termination_status());
  665. if (is_dumpable()) {
  666. if (m_should_generate_coredump) {
  667. auto result = dump_core();
  668. if (result.is_error()) {
  669. dmesgln("Failed to write coredump for pid {}: {}", pid(), result.error());
  670. }
  671. }
  672. if (m_perf_event_buffer) {
  673. auto result = dump_perfcore();
  674. if (result.is_error())
  675. dmesgln("Failed to write perfcore for pid {}: {}", pid(), result.error());
  676. TimeManagement::the().disable_profile_timer();
  677. }
  678. }
  679. m_threads_for_coredump.clear();
  680. m_alarm_timer.with([&](auto& timer) {
  681. if (timer)
  682. TimerQueue::the().cancel_timer(timer.release_nonnull());
  683. });
  684. m_fds.with_exclusive([](auto& fds) { fds.clear(); });
  685. with_mutable_protected_data([&](auto& protected_data) { protected_data.tty = nullptr; });
  686. m_executable.with([](auto& executable) { executable = nullptr; });
  687. m_attached_jail.with([](auto& jail) {
  688. if (jail)
  689. jail->detach({});
  690. jail = nullptr;
  691. });
  692. m_arguments.clear();
  693. m_environment.clear();
  694. m_state.store(State::Dead, AK::MemoryOrder::memory_order_release);
  695. {
  696. if (auto parent_process = Process::from_pid_ignoring_jails(ppid())) {
  697. if (parent_process->is_user_process() && (parent_process->m_signal_action_data[SIGCHLD].flags & SA_NOCLDWAIT) != SA_NOCLDWAIT)
  698. (void)parent_process->send_signal(SIGCHLD, this);
  699. }
  700. }
  701. if (!!ppid()) {
  702. if (auto parent = Process::from_pid_ignoring_jails(ppid())) {
  703. parent->m_ticks_in_user_for_dead_children += m_ticks_in_user + m_ticks_in_user_for_dead_children;
  704. parent->m_ticks_in_kernel_for_dead_children += m_ticks_in_kernel + m_ticks_in_kernel_for_dead_children;
  705. }
  706. }
  707. unblock_waiters(Thread::WaitBlocker::UnblockFlags::Terminated);
  708. m_space.with([](auto& space) { space->remove_all_regions({}); });
  709. VERIFY(ref_count() > 0);
  710. // WaitBlockerSet::finalize will be in charge of dropping the last
  711. // reference if there are still waiters around, or whenever the last
  712. // waitable states are consumed. Unless there is no parent around
  713. // anymore, in which case we'll just drop it right away.
  714. m_wait_blocker_set.finalize();
  715. }
  716. void Process::disowned_by_waiter(Process& process)
  717. {
  718. m_wait_blocker_set.disowned_by_waiter(process);
  719. }
  720. void Process::unblock_waiters(Thread::WaitBlocker::UnblockFlags flags, u8 signal)
  721. {
  722. RefPtr<Process> waiter_process;
  723. if (auto* my_tracer = tracer())
  724. waiter_process = Process::from_pid_ignoring_jails(my_tracer->tracer_pid());
  725. else
  726. waiter_process = Process::from_pid_ignoring_jails(ppid());
  727. if (waiter_process)
  728. waiter_process->m_wait_blocker_set.unblock(*this, flags, signal);
  729. }
  730. void Process::remove_from_secondary_lists()
  731. {
  732. m_jail_process_list.with([this](auto& list_ptr) {
  733. if (list_ptr) {
  734. list_ptr->attached_processes().with([&](auto& list) {
  735. list.remove(*this);
  736. });
  737. }
  738. });
  739. }
  740. void Process::die()
  741. {
  742. auto expected = State::Running;
  743. if (!m_state.compare_exchange_strong(expected, State::Dying, AK::memory_order_acquire)) {
  744. // It's possible that another thread calls this at almost the same time
  745. // as we can't always instantly kill other threads (they may be blocked)
  746. // So if we already were called then other threads should stop running
  747. // momentarily and we only really need to service the first thread
  748. return;
  749. }
  750. // Let go of the TTY, otherwise a slave PTY may keep the master PTY from
  751. // getting an EOF when the last process using the slave PTY dies.
  752. // If the master PTY owner relies on an EOF to know when to wait() on a
  753. // slave owner, we have to allow the PTY pair to be torn down.
  754. with_mutable_protected_data([&](auto& protected_data) { protected_data.tty = nullptr; });
  755. VERIFY(m_threads_for_coredump.is_empty());
  756. for_each_thread([&](auto& thread) {
  757. auto result = m_threads_for_coredump.try_append(thread);
  758. if (result.is_error())
  759. dbgln("Failed to add thread {} to coredump due to OOM", thread.tid());
  760. });
  761. all_instances().with([&](auto const& list) {
  762. for (auto it = list.begin(); it != list.end();) {
  763. auto& process = *it;
  764. ++it;
  765. if (process.has_tracee_thread(pid())) {
  766. if constexpr (PROCESS_DEBUG) {
  767. process.name().with([&](auto& process_name) {
  768. name().with([&](auto& name) {
  769. dbgln("Process {} ({}) is attached by {} ({}) which will exit", process_name->view(), process.pid(), name->view(), pid());
  770. });
  771. });
  772. }
  773. process.stop_tracing();
  774. auto err = process.send_signal(SIGSTOP, this);
  775. if (err.is_error()) {
  776. process.name().with([&](auto& process_name) {
  777. dbgln("Failed to send the SIGSTOP signal to {} ({})", process_name->view(), process.pid());
  778. });
  779. }
  780. }
  781. }
  782. });
  783. kill_all_threads();
  784. #ifdef ENABLE_KERNEL_COVERAGE_COLLECTION
  785. KCOVDevice::free_process();
  786. #endif
  787. }
  788. void Process::terminate_due_to_signal(u8 signal)
  789. {
  790. VERIFY_INTERRUPTS_DISABLED();
  791. VERIFY(signal < NSIG);
  792. VERIFY(&Process::current() == this);
  793. dbgln("Terminating {} due to signal {}", *this, signal);
  794. with_mutable_protected_data([&](auto& protected_data) {
  795. protected_data.termination_status = 0;
  796. protected_data.termination_signal = signal;
  797. });
  798. die();
  799. }
  800. ErrorOr<void> Process::send_signal(u8 signal, Process* sender)
  801. {
  802. VERIFY(is_user_process());
  803. // Try to send it to the "obvious" main thread:
  804. auto receiver_thread = Thread::from_tid(pid().value());
  805. // If the main thread has died, there may still be other threads:
  806. if (!receiver_thread) {
  807. // The first one should be good enough.
  808. // Neither kill(2) nor kill(3) specify any selection procedure.
  809. for_each_thread([&receiver_thread](Thread& thread) -> IterationDecision {
  810. receiver_thread = &thread;
  811. return IterationDecision::Break;
  812. });
  813. }
  814. if (receiver_thread) {
  815. receiver_thread->send_signal(signal, sender);
  816. return {};
  817. }
  818. return ESRCH;
  819. }
  820. ErrorOr<NonnullRefPtr<Thread>> Process::create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, NonnullOwnPtr<KString> name, u32 affinity, bool joinable)
  821. {
  822. VERIFY((priority >= THREAD_PRIORITY_MIN) && (priority <= THREAD_PRIORITY_MAX));
  823. // FIXME: Do something with guard pages?
  824. auto thread = TRY(Thread::create(*this));
  825. thread->set_name(move(name));
  826. thread->set_affinity(affinity);
  827. thread->set_priority(priority);
  828. if (!joinable)
  829. thread->detach();
  830. auto& regs = thread->regs();
  831. regs.set_ip((FlatPtr)entry);
  832. regs.set_sp((FlatPtr)entry_data); // entry function argument is expected to be in the SP register
  833. SpinlockLocker lock(g_scheduler_lock);
  834. thread->set_state(Thread::State::Runnable);
  835. return thread;
  836. }
  837. void Process::OpenFileDescriptionAndFlags::clear()
  838. {
  839. m_description = nullptr;
  840. m_flags = 0;
  841. }
  842. void Process::OpenFileDescriptionAndFlags::set(NonnullRefPtr<OpenFileDescription> description, u32 flags)
  843. {
  844. m_description = move(description);
  845. m_flags = flags;
  846. }
  847. RefPtr<TTY> Process::tty()
  848. {
  849. return with_protected_data([&](auto& protected_data) { return protected_data.tty; });
  850. }
  851. RefPtr<TTY const> Process::tty() const
  852. {
  853. return with_protected_data([&](auto& protected_data) { return protected_data.tty; });
  854. }
  855. void Process::set_tty(RefPtr<TTY> new_tty)
  856. {
  857. with_mutable_protected_data([&](auto& protected_data) { protected_data.tty = move(new_tty); });
  858. }
  859. ErrorOr<void> Process::start_tracing_from(ProcessID tracer)
  860. {
  861. m_tracer = TRY(ThreadTracer::try_create(tracer));
  862. return {};
  863. }
  864. void Process::stop_tracing()
  865. {
  866. m_tracer = nullptr;
  867. }
  868. void Process::tracer_trap(Thread& thread, RegisterState const& regs)
  869. {
  870. VERIFY(m_tracer.ptr());
  871. m_tracer->set_regs(regs);
  872. thread.send_urgent_signal_to_self(SIGTRAP);
  873. }
  874. bool Process::create_perf_events_buffer_if_needed()
  875. {
  876. if (m_perf_event_buffer)
  877. return true;
  878. m_perf_event_buffer = PerformanceEventBuffer::try_create_with_size(4 * MiB);
  879. if (!m_perf_event_buffer)
  880. return false;
  881. return !m_perf_event_buffer->add_process(*this, ProcessEventType::Create).is_error();
  882. }
  883. void Process::delete_perf_events_buffer()
  884. {
  885. if (m_perf_event_buffer)
  886. m_perf_event_buffer = nullptr;
  887. }
  888. bool Process::remove_thread(Thread& thread)
  889. {
  890. u32 thread_count_before = 0;
  891. thread_list().with([&](auto& thread_list) {
  892. thread_list.remove(thread);
  893. with_mutable_protected_data([&](auto& protected_data) {
  894. thread_count_before = protected_data.thread_count.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel);
  895. VERIFY(thread_count_before != 0);
  896. });
  897. });
  898. return thread_count_before == 1;
  899. }
  900. bool Process::add_thread(Thread& thread)
  901. {
  902. bool is_first = false;
  903. thread_list().with([&](auto& thread_list) {
  904. thread_list.append(thread);
  905. with_mutable_protected_data([&](auto& protected_data) {
  906. is_first = protected_data.thread_count.fetch_add(1, AK::MemoryOrder::memory_order_relaxed) == 0;
  907. });
  908. });
  909. return is_first;
  910. }
  911. ErrorOr<void> Process::set_coredump_property(NonnullOwnPtr<KString> key, NonnullOwnPtr<KString> value)
  912. {
  913. return m_coredump_properties.with([&](auto& coredump_properties) -> ErrorOr<void> {
  914. // Write it into the first available property slot.
  915. for (auto& slot : coredump_properties) {
  916. if (slot.key)
  917. continue;
  918. slot.key = move(key);
  919. slot.value = move(value);
  920. return {};
  921. }
  922. return ENOBUFS;
  923. });
  924. }
  925. ErrorOr<void> Process::try_set_coredump_property(StringView key, StringView value)
  926. {
  927. auto key_kstring = TRY(KString::try_create(key));
  928. auto value_kstring = TRY(KString::try_create(value));
  929. return set_coredump_property(move(key_kstring), move(value_kstring));
  930. }
  931. static constexpr StringView to_string(Pledge promise)
  932. {
  933. #define __ENUMERATE_PLEDGE_PROMISE(x) \
  934. case Pledge::x: \
  935. return #x##sv;
  936. switch (promise) {
  937. ENUMERATE_PLEDGE_PROMISES
  938. }
  939. #undef __ENUMERATE_PLEDGE_PROMISE
  940. VERIFY_NOT_REACHED();
  941. }
  942. ErrorOr<void> Process::require_no_promises() const
  943. {
  944. if (!has_promises())
  945. return {};
  946. dbgln("Has made a promise");
  947. Thread::current()->set_promise_violation_pending(true);
  948. return EPROMISEVIOLATION;
  949. }
  950. ErrorOr<void> Process::require_promise(Pledge promise)
  951. {
  952. if (!has_promises())
  953. return {};
  954. if (has_promised(promise))
  955. return {};
  956. dbgln("Has not pledged {}", to_string(promise));
  957. Thread::current()->set_promise_violation_pending(true);
  958. (void)try_set_coredump_property("pledge_violation"sv, to_string(promise));
  959. return EPROMISEVIOLATION;
  960. }
  961. NonnullRefPtr<Credentials> Process::credentials() const
  962. {
  963. return with_protected_data([&](auto& protected_data) -> NonnullRefPtr<Credentials> {
  964. return *protected_data.credentials;
  965. });
  966. }
  967. RefPtr<Custody> Process::executable()
  968. {
  969. return m_executable.with([](auto& executable) { return executable; });
  970. }
  971. RefPtr<Custody const> Process::executable() const
  972. {
  973. return m_executable.with([](auto& executable) { return executable; });
  974. }
  975. ErrorOr<NonnullRefPtr<Custody>> Process::custody_for_dirfd(int dirfd)
  976. {
  977. if (dirfd == AT_FDCWD)
  978. return current_directory();
  979. auto description = TRY(open_file_description(dirfd));
  980. if (!description->custody())
  981. return EINVAL;
  982. if (!description->is_directory())
  983. return ENOTDIR;
  984. return *description->custody();
  985. }
  986. SpinlockProtected<NonnullOwnPtr<KString>, LockRank::None> const& Process::name() const
  987. {
  988. return m_name;
  989. }
  990. void Process::set_name(NonnullOwnPtr<KString> name)
  991. {
  992. m_name.with([&](auto& this_name) {
  993. this_name = move(name);
  994. });
  995. }
  996. }