Process.cpp 37 KB

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