Process.cpp 37 KB

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