Process.cpp 32 KB

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