Process.cpp 37 KB

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