2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-01-11 08:52:18 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-01-18 22:31:29 +00:00
|
|
|
#include <AK/Demangle.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2019-02-16 23:13:47 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-05-18 00:00:01 +00:00
|
|
|
#include <AK/Time.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <AK/Types.h>
|
2020-07-04 23:37:36 +00:00
|
|
|
#include <Kernel/API/Syscall.h>
|
2021-06-21 15:34:09 +00:00
|
|
|
#include <Kernel/Arch/x86/InterruptDisabler.h>
|
2020-11-06 08:09:51 +00:00
|
|
|
#include <Kernel/CoreDump.h>
|
2021-01-25 15:07:10 +00:00
|
|
|
#include <Kernel/Debug.h>
|
2019-11-28 19:59:11 +00:00
|
|
|
#include <Kernel/Devices/NullDevice.h>
|
2019-05-30 16:58:59 +00:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2019-08-07 19:52:43 +00:00
|
|
|
#include <Kernel/KBufferBuilder.h>
|
2019-06-07 17:29:34 +00:00
|
|
|
#include <Kernel/KSyms.h>
|
2019-11-28 19:59:11 +00:00
|
|
|
#include <Kernel/Module.h>
|
2021-06-23 19:54:41 +00:00
|
|
|
#include <Kernel/Panic.h>
|
2020-02-16 01:01:42 +00:00
|
|
|
#include <Kernel/PerformanceEventBuffer.h>
|
2021-05-07 08:41:13 +00:00
|
|
|
#include <Kernel/PerformanceManager.h>
|
2019-06-07 17:29:34 +00:00
|
|
|
#include <Kernel/Process.h>
|
2020-11-06 08:09:51 +00:00
|
|
|
#include <Kernel/RTC.h>
|
2021-06-22 15:40:16 +00:00
|
|
|
#include <Kernel/Sections.h>
|
2020-05-15 22:32:14 +00:00
|
|
|
#include <Kernel/StdLib.h>
|
2020-02-16 01:01:42 +00:00
|
|
|
#include <Kernel/TTY/TTY.h>
|
2019-10-20 16:11:40 +00:00
|
|
|
#include <Kernel/Thread.h>
|
2021-01-17 07:51:41 +00:00
|
|
|
#include <Kernel/VM/AnonymousVMObject.h>
|
2020-02-16 00:33:41 +00:00
|
|
|
#include <Kernel/VM/PageDirectory.h>
|
2020-09-05 21:52:14 +00:00
|
|
|
#include <Kernel/VM/PrivateInodeVMObject.h>
|
2020-02-28 19:29:14 +00:00
|
|
|
#include <Kernel/VM/SharedInodeVMObject.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <LibC/errno_numbers.h>
|
2020-01-02 12:01:41 +00:00
|
|
|
#include <LibC/limits.h>
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-02-13 23:53:53 +00:00
|
|
|
static void create_signal_trampoline();
|
2019-07-19 15:01:16 +00:00
|
|
|
|
2020-06-28 21:34:31 +00:00
|
|
|
RecursiveSpinLock g_processes_lock;
|
|
|
|
static Atomic<pid_t> next_pid;
|
2021-06-06 21:40:03 +00:00
|
|
|
READONLY_AFTER_INIT Process::List* g_processes;
|
2021-02-14 16:39:25 +00:00
|
|
|
READONLY_AFTER_INIT String* g_hostname;
|
|
|
|
READONLY_AFTER_INIT Lock* g_hostname_lock;
|
|
|
|
READONLY_AFTER_INIT HashMap<String, OwnPtr<Module>>* g_modules;
|
|
|
|
READONLY_AFTER_INIT Region* g_signal_trampoline_region;
|
2018-10-26 12:56:21 +00:00
|
|
|
|
2020-08-08 15:32:34 +00:00
|
|
|
ProcessID Process::allocate_pid()
|
2019-12-22 10:51:24 +00:00
|
|
|
{
|
2020-08-08 15:32:34 +00:00
|
|
|
// Overflow is UB, and negative PIDs wreck havoc.
|
|
|
|
// TODO: Handle PID overflow
|
|
|
|
// For example: Use an Atomic<u32>, mask the most significant bit,
|
2020-08-08 20:04:20 +00:00
|
|
|
// retry if PID is already taken as a PID, taken as a TID,
|
|
|
|
// takes as a PGID, taken as a SID, or zero.
|
2020-06-28 21:34:31 +00:00
|
|
|
return next_pid.fetch_add(1, AK::MemoryOrder::memory_order_acq_rel);
|
2019-12-22 10:51:24 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 17:41:50 +00:00
|
|
|
UNMAP_AFTER_INIT void Process::initialize()
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
2019-11-28 19:59:11 +00:00
|
|
|
g_modules = new HashMap<String, OwnPtr<Module>>;
|
|
|
|
|
2020-06-28 21:34:31 +00:00
|
|
|
next_pid.store(0, AK::MemoryOrder::memory_order_release);
|
2021-06-06 21:40:03 +00:00
|
|
|
g_processes = new Process::List();
|
2021-06-03 10:21:04 +00:00
|
|
|
g_process_groups = new ProcessGroup::List();
|
2020-07-30 21:38:15 +00:00
|
|
|
g_hostname = new String("courage");
|
|
|
|
g_hostname_lock = new Lock;
|
2019-07-19 15:01:16 +00:00
|
|
|
|
2021-02-13 23:53:53 +00:00
|
|
|
create_signal_trampoline();
|
2018-11-02 13:06:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 15:32:34 +00:00
|
|
|
Vector<ProcessID> Process::all_pids()
|
2019-02-03 11:33:11 +00:00
|
|
|
{
|
2020-08-08 15:32:34 +00:00
|
|
|
Vector<ProcessID> pids;
|
2020-06-28 21:34:31 +00:00
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
2021-06-06 21:15:48 +00:00
|
|
|
pids.ensure_capacity(g_processes->size_slow());
|
2019-08-08 12:40:13 +00:00
|
|
|
for (auto& process : *g_processes)
|
|
|
|
pids.append(process.pid());
|
2019-02-03 11:33:11 +00:00
|
|
|
return pids;
|
|
|
|
}
|
|
|
|
|
2020-08-02 02:04:56 +00:00
|
|
|
NonnullRefPtrVector<Process> Process::all_processes()
|
2018-10-23 10:44:46 +00:00
|
|
|
{
|
2020-08-02 02:04:56 +00:00
|
|
|
NonnullRefPtrVector<Process> processes;
|
2020-06-28 21:34:31 +00:00
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
2021-06-06 21:15:48 +00:00
|
|
|
processes.ensure_capacity(g_processes->size_slow());
|
2019-08-08 12:40:13 +00:00
|
|
|
for (auto& process : *g_processes)
|
2020-08-02 02:04:56 +00:00
|
|
|
processes.append(NonnullRefPtr<Process>(process));
|
2018-11-01 12:15:46 +00:00
|
|
|
return processes;
|
2018-10-23 10:44:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:32:53 +00:00
|
|
|
bool Process::in_group(gid_t gid) const
|
|
|
|
{
|
2021-03-10 19:07:08 +00:00
|
|
|
return this->gid() == gid || extra_gids().contains_slow(gid);
|
2019-02-27 11:32:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
void Process::kill_threads_except_self()
|
2018-10-28 08:57:22 +00:00
|
|
|
{
|
2020-07-30 21:38:15 +00:00
|
|
|
InterruptDisabler disabler;
|
2018-10-28 08:57:22 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
if (thread_count() <= 1)
|
|
|
|
return;
|
2019-12-25 20:50:13 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
auto current_thread = Thread::current();
|
|
|
|
for_each_thread([&](Thread& thread) {
|
2021-05-16 09:36:52 +00:00
|
|
|
if (&thread == current_thread)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (auto state = thread.state(); state == Thread::State::Dead
|
|
|
|
|| state == Thread::State::Dying)
|
|
|
|
return;
|
2019-12-25 20:50:13 +00:00
|
|
|
|
2020-09-26 03:44:43 +00:00
|
|
|
// We need to detach this thread in case it hasn't been joined
|
|
|
|
thread.detach();
|
2020-07-30 21:38:15 +00:00
|
|
|
thread.set_should_die();
|
|
|
|
});
|
2020-02-28 19:47:27 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
big_lock().clear_waiters();
|
2020-01-07 18:29:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
void Process::kill_all_threads()
|
2019-12-30 20:11:25 +00:00
|
|
|
{
|
2020-07-30 21:38:15 +00:00
|
|
|
for_each_thread([&](Thread& thread) {
|
2020-09-26 03:44:43 +00:00
|
|
|
// We need to detach this thread in case it hasn't been joined
|
|
|
|
thread.detach();
|
2020-07-30 21:38:15 +00:00
|
|
|
thread.set_should_die();
|
|
|
|
});
|
2019-12-30 20:11:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 14:53:35 +00:00
|
|
|
RefPtr<Process> Process::create_user_process(RefPtr<Thread>& first_thread, const String& path, uid_t uid, gid_t gid, ProcessID parent_pid, int& error, Vector<String>&& arguments, Vector<String>&& environment, TTY* tty)
|
2018-10-24 07:48:24 +00:00
|
|
|
{
|
2020-07-30 21:38:15 +00:00
|
|
|
auto parts = path.split('/');
|
|
|
|
if (arguments.is_empty()) {
|
|
|
|
arguments.append(parts.last());
|
2020-01-11 11:47:47 +00:00
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
RefPtr<Custody> cwd;
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
2020-08-02 02:04:56 +00:00
|
|
|
if (auto parent = Process::from_pid(parent_pid)) {
|
2020-07-30 21:38:15 +00:00
|
|
|
cwd = parent->m_cwd;
|
2019-12-29 05:54:10 +00:00
|
|
|
}
|
2019-12-09 18:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!cwd)
|
|
|
|
cwd = VFS::the().root_custody();
|
2020-01-30 20:46:45 +00:00
|
|
|
|
2021-05-14 11:55:43 +00:00
|
|
|
auto process = Process::create(first_thread, parts.take_last(), uid, gid, parent_pid, false, move(cwd), nullptr, tty);
|
2020-09-05 21:52:14 +00:00
|
|
|
if (!first_thread)
|
|
|
|
return {};
|
2021-04-30 10:18:10 +00:00
|
|
|
if (!process->m_fds.try_resize(m_max_open_file_descriptors)) {
|
|
|
|
first_thread = nullptr;
|
|
|
|
return {};
|
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
auto& device_to_use_as_tty = tty ? (CharacterDevice&)*tty : NullDevice::the();
|
|
|
|
auto description = device_to_use_as_tty.open(O_RDWR).value();
|
|
|
|
process->m_fds[0].set(*description);
|
|
|
|
process->m_fds[1].set(*description);
|
|
|
|
process->m_fds[2].set(*description);
|
2020-01-30 20:46:45 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
error = process->exec(path, move(arguments), move(environment));
|
|
|
|
if (error != 0) {
|
2021-01-08 23:11:15 +00:00
|
|
|
dbgln("Failed to exec {}: {}", path, error);
|
2020-09-27 14:53:35 +00:00
|
|
|
first_thread = nullptr;
|
2020-08-02 02:04:56 +00:00
|
|
|
return {};
|
2019-08-29 18:57:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2020-08-02 02:04:56 +00:00
|
|
|
process->ref();
|
2021-06-06 21:40:03 +00:00
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
|
|
|
g_processes->prepend(*process);
|
2019-08-29 18:57:02 +00:00
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
error = 0;
|
|
|
|
return process;
|
2018-10-24 07:48:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 21:52:14 +00:00
|
|
|
RefPtr<Process> Process::create_kernel_process(RefPtr<Thread>& first_thread, String&& name, void (*entry)(void*), void* entry_data, u32 affinity)
|
2019-08-12 17:33:24 +00:00
|
|
|
{
|
2021-05-14 11:55:43 +00:00
|
|
|
auto process = Process::create(first_thread, move(name), (uid_t)0, (gid_t)0, ProcessID(0), true);
|
|
|
|
if (!first_thread || !process)
|
2020-09-05 21:52:14 +00:00
|
|
|
return {};
|
2021-06-23 19:54:41 +00:00
|
|
|
#if ARCH(I386)
|
2020-11-17 03:51:34 +00:00
|
|
|
first_thread->tss().eip = (FlatPtr)entry;
|
|
|
|
first_thread->tss().esp = FlatPtr(entry_data); // entry function argument is expected to be in tss.esp
|
2021-06-23 19:54:41 +00:00
|
|
|
#else
|
|
|
|
(void)entry;
|
|
|
|
(void)entry_data;
|
|
|
|
PANIC("Process::create_kernel_process() not implemented");
|
|
|
|
#endif
|
2019-12-30 20:11:25 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
if (process->pid() != 0) {
|
2020-08-02 02:04:56 +00:00
|
|
|
process->ref();
|
2021-06-06 21:40:03 +00:00
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
|
|
|
g_processes->prepend(*process);
|
2019-12-30 20:11:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 02:22:59 +00:00
|
|
|
ScopedSpinLock lock(g_scheduler_lock);
|
2020-07-30 21:38:15 +00:00
|
|
|
first_thread->set_affinity(affinity);
|
|
|
|
first_thread->set_state(Thread::State::Runnable);
|
|
|
|
return process;
|
|
|
|
}
|
2019-12-30 20:11:25 +00:00
|
|
|
|
2021-03-10 18:59:46 +00:00
|
|
|
void Process::protect_data()
|
|
|
|
{
|
2021-03-11 12:13:05 +00:00
|
|
|
MM.set_page_writable_direct(VirtualAddress { this }, false);
|
2021-03-10 18:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Process::unprotect_data()
|
|
|
|
{
|
2021-03-11 12:13:05 +00:00
|
|
|
MM.set_page_writable_direct(VirtualAddress { this }, true);
|
2021-03-10 18:59:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 11:55:43 +00:00
|
|
|
RefPtr<Process> Process::create(RefPtr<Thread>& first_thread, const String& name, uid_t uid, gid_t gid, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty, Process* fork_parent)
|
|
|
|
{
|
2021-06-20 08:21:16 +00:00
|
|
|
auto process = adopt_ref_if_nonnull(new (nothrow) Process(name, uid, gid, ppid, is_kernel_process, move(cwd), move(executable), tty));
|
2021-05-14 11:55:43 +00:00
|
|
|
if (!process)
|
|
|
|
return {};
|
|
|
|
auto result = process->attach_resources(first_thread, fork_parent);
|
|
|
|
if (result.is_error())
|
|
|
|
return {};
|
|
|
|
return process;
|
|
|
|
}
|
|
|
|
|
|
|
|
Process::Process(const String& name, uid_t uid, gid_t gid, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty)
|
2020-07-30 21:38:15 +00:00
|
|
|
: m_name(move(name))
|
2020-09-10 15:46:24 +00:00
|
|
|
, m_is_kernel_process(is_kernel_process)
|
2020-07-30 21:38:15 +00:00
|
|
|
, m_executable(move(executable))
|
|
|
|
, m_cwd(move(cwd))
|
|
|
|
, m_tty(tty)
|
2020-11-29 23:05:27 +00:00
|
|
|
, m_wait_block_condition(*this)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-03-11 13:46:27 +00:00
|
|
|
// Ensure that we protect the process data when exiting the constructor.
|
|
|
|
ProtectedDataMutationScope scope { *this };
|
|
|
|
|
2021-03-11 12:13:05 +00:00
|
|
|
m_pid = allocate_pid();
|
|
|
|
m_ppid = ppid;
|
|
|
|
m_uid = uid;
|
|
|
|
m_gid = gid;
|
|
|
|
m_euid = uid;
|
|
|
|
m_egid = gid;
|
|
|
|
m_suid = uid;
|
|
|
|
m_sgid = gid;
|
2021-03-10 18:59:46 +00:00
|
|
|
|
|
|
|
dbgln_if(PROCESS_DEBUG, "Created new process {}({})", m_name, this->pid().value());
|
2021-05-14 11:55:43 +00:00
|
|
|
}
|
2019-12-30 20:11:25 +00:00
|
|
|
|
2021-05-14 11:55:43 +00:00
|
|
|
KResult Process::attach_resources(RefPtr<Thread>& first_thread, Process* fork_parent)
|
|
|
|
{
|
2021-02-08 14:45:40 +00:00
|
|
|
m_space = Space::create(*this, fork_parent ? &fork_parent->space() : nullptr);
|
2021-05-14 11:55:43 +00:00
|
|
|
if (!m_space)
|
|
|
|
return ENOMEM;
|
2019-12-30 20:11:25 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
if (fork_parent) {
|
|
|
|
// NOTE: fork() doesn't clone all threads; the thread that called fork() becomes the only thread in the new process.
|
|
|
|
first_thread = Thread::current()->clone(*this);
|
2021-05-14 11:55:43 +00:00
|
|
|
if (!first_thread)
|
|
|
|
return ENOMEM;
|
2020-07-30 21:38:15 +00:00
|
|
|
} else {
|
|
|
|
// NOTE: This non-forked code path is only taken when the kernel creates a process "manually" (at boot.)
|
2021-02-07 17:13:51 +00:00
|
|
|
auto thread_or_error = Thread::try_create(*this);
|
2021-05-14 11:55:43 +00:00
|
|
|
if (thread_or_error.is_error())
|
|
|
|
return thread_or_error.error();
|
2021-02-07 17:13:51 +00:00
|
|
|
first_thread = thread_or_error.release_value();
|
2020-09-26 03:44:43 +00:00
|
|
|
first_thread->detach();
|
2019-12-30 20:11:25 +00:00
|
|
|
}
|
2021-05-14 11:55:43 +00:00
|
|
|
return KSuccess;
|
2019-08-12 17:33:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
Process::~Process()
|
2019-12-09 18:12:38 +00:00
|
|
|
{
|
2021-03-11 12:13:05 +00:00
|
|
|
unprotect_data();
|
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(thread_count() == 0); // all threads should have been finalized
|
|
|
|
VERIFY(!m_alarm_timer);
|
2020-11-29 23:05:27 +00:00
|
|
|
|
2021-05-07 08:41:13 +00:00
|
|
|
PerformanceManager::add_process_exit_event(*this);
|
2021-04-25 21:42:36 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-04-25 21:42:36 +00:00
|
|
|
ScopedSpinLock processes_lock(g_processes_lock);
|
2021-06-06 21:40:03 +00:00
|
|
|
if (m_list_node.is_in_list())
|
|
|
|
g_processes->remove(*this);
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2020-01-30 20:46:45 +00:00
|
|
|
|
Kernel: Mark compilation-unit-only functions as static
This enables a nice warning in case a function becomes dead code. Also, in case
of signal_trampoline_dummy, marking it external (non-static) prevents it from
being 'optimized away', which would lead to surprising and weird linker errors.
I found these places by using -Wmissing-declarations.
The Kernel still shows these issues, which I think are false-positives,
but don't want to touch:
- Kernel/Arch/i386/CPU.cpp:1081:17: void Kernel::enter_thread_context(Kernel::Thread*, Kernel::Thread*)
- Kernel/Arch/i386/CPU.cpp:1170:17: void Kernel::context_first_init(Kernel::Thread*, Kernel::Thread*, Kernel::TrapFrame*)
- Kernel/Arch/i386/CPU.cpp:1304:16: u32 Kernel::do_init_context(Kernel::Thread*, u32)
- Kernel/Arch/i386/CPU.cpp:1347:17: void Kernel::pre_init_finished()
- Kernel/Arch/i386/CPU.cpp:1360:17: void Kernel::post_init_finished()
No idea, not gonna touch it.
- Kernel/init.cpp:104:30: void Kernel::init()
- Kernel/init.cpp:167:30: void Kernel::init_ap(u32, Kernel::Processor*)
- Kernel/init.cpp:184:17: void Kernel::init_finished(u32)
Called by boot.S.
- Kernel/init.cpp:383:16: int Kernel::__cxa_atexit(void (*)(void*), void*, void*)
- Kernel/StdLib.cpp:285:19: void __cxa_pure_virtual()
- Kernel/StdLib.cpp:300:19: void __stack_chk_fail()
- Kernel/StdLib.cpp:305:19: void __stack_chk_fail_local()
Not sure how to tell the compiler that the compiler is already using them.
Also, maybe __cxa_atexit should go into StdLib.cpp?
- Kernel/Modules/TestModule.cpp:31:17: void module_init()
- Kernel/Modules/TestModule.cpp:40:17: void module_fini()
Could maybe go into a new header. This would also provide type-checking for new modules.
2020-08-10 19:12:13 +00:00
|
|
|
// Make sure the compiler doesn't "optimize away" this function:
|
2021-04-29 12:54:15 +00:00
|
|
|
extern void signal_trampoline_dummy() __attribute__((used));
|
2020-12-20 23:09:48 +00:00
|
|
|
void signal_trampoline_dummy()
|
2020-04-12 18:22:26 +00:00
|
|
|
{
|
2021-02-25 15:18:36 +00:00
|
|
|
#if ARCH(I386)
|
2020-07-30 21:38:15 +00:00
|
|
|
// The trampoline preserves the current eax, pushes the signal code and
|
|
|
|
// then calls the signal handler. We do this because, when interrupting a
|
|
|
|
// blocking syscall, that syscall may return some special error code in eax;
|
|
|
|
// This error code would likely be overwritten by the signal handler, so it's
|
2020-10-02 21:14:37 +00:00
|
|
|
// necessary to preserve it here.
|
2020-07-30 21:38:15 +00:00
|
|
|
asm(
|
|
|
|
".intel_syntax noprefix\n"
|
|
|
|
"asm_signal_trampoline:\n"
|
|
|
|
"push ebp\n"
|
|
|
|
"mov ebp, esp\n"
|
|
|
|
"push eax\n" // we have to store eax 'cause it might be the return value from a syscall
|
|
|
|
"sub esp, 4\n" // align the stack to 16 bytes
|
|
|
|
"mov eax, [ebp+12]\n" // push the signal code
|
|
|
|
"push eax\n"
|
|
|
|
"call [ebp+8]\n" // call the signal handler
|
|
|
|
"add esp, 8\n"
|
|
|
|
"mov eax, %P0\n"
|
|
|
|
"int 0x82\n" // sigreturn syscall
|
|
|
|
"asm_signal_trampoline_end:\n"
|
|
|
|
".att_syntax" ::"i"(Syscall::SC_sigreturn));
|
2021-03-04 16:50:05 +00:00
|
|
|
#elif ARCH(X86_64)
|
|
|
|
asm("asm_signal_trampoline:\n"
|
|
|
|
"cli;hlt\n"
|
|
|
|
"asm_signal_trampoline_end:\n");
|
2021-02-25 15:18:36 +00:00
|
|
|
#endif
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2020-04-12 18:22:26 +00:00
|
|
|
|
2021-04-29 12:54:15 +00:00
|
|
|
extern "C" void asm_signal_trampoline(void) __attribute__((used));
|
2020-12-24 15:41:54 +00:00
|
|
|
extern "C" void asm_signal_trampoline_end(void);
|
2020-04-12 18:22:26 +00:00
|
|
|
|
2021-02-13 23:53:53 +00:00
|
|
|
void create_signal_trampoline()
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
// NOTE: We leak this region.
|
2021-02-14 00:25:22 +00:00
|
|
|
g_signal_trampoline_region = MM.allocate_kernel_region(PAGE_SIZE, "Signal trampolines", Region::Access::Read | Region::Access::Write).leak_ptr();
|
2021-02-13 23:53:53 +00:00
|
|
|
g_signal_trampoline_region->set_syscall_region(true);
|
2020-04-12 18:22:26 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
u8* trampoline = (u8*)asm_signal_trampoline;
|
|
|
|
u8* trampoline_end = (u8*)asm_signal_trampoline_end;
|
|
|
|
size_t trampoline_size = trampoline_end - trampoline;
|
2020-04-12 18:22:26 +00:00
|
|
|
|
2021-02-13 23:53:53 +00:00
|
|
|
u8* code_ptr = (u8*)g_signal_trampoline_region->vaddr().as_ptr();
|
|
|
|
memcpy(code_ptr, trampoline, trampoline_size);
|
2020-04-12 18:22:26 +00:00
|
|
|
|
2021-02-13 23:53:53 +00:00
|
|
|
g_signal_trampoline_region->set_writable(false);
|
|
|
|
g_signal_trampoline_region->remap();
|
2020-04-12 18:22:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
void Process::crash(int signal, u32 eip, bool out_of_memory)
|
2019-12-09 18:12:38 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!is_dead());
|
|
|
|
VERIFY(Process::current() == this);
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
if (out_of_memory) {
|
2021-01-08 23:11:15 +00:00
|
|
|
dbgln("\033[31;1mOut of memory\033[m, killing: {}", *this);
|
2020-07-30 21:38:15 +00:00
|
|
|
} else {
|
|
|
|
if (eip >= 0xc0000000 && g_kernel_symbols_available) {
|
|
|
|
auto* symbol = symbolicate_kernel_address(eip);
|
2021-01-08 23:11:15 +00:00
|
|
|
dbgln("\033[31;1m{:p} {} +{}\033[0m\n", eip, (symbol ? demangle(symbol->name) : "(k?)"), (symbol ? eip - symbol->address : 0));
|
2020-07-30 21:38:15 +00:00
|
|
|
} else {
|
2021-01-08 23:11:15 +00:00
|
|
|
dbgln("\033[31;1m{:p} (?)\033[0m\n", eip);
|
2019-12-29 12:16:53 +00:00
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
dump_backtrace();
|
2019-12-09 18:12:38 +00:00
|
|
|
}
|
2021-03-11 13:24:08 +00:00
|
|
|
{
|
|
|
|
ProtectedDataMutationScope scope { *this };
|
|
|
|
m_termination_signal = signal;
|
|
|
|
}
|
2020-12-18 10:22:21 +00:00
|
|
|
set_dump_core(!out_of_memory);
|
2021-02-08 14:45:40 +00:00
|
|
|
space().dump_regions();
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(is_user_process());
|
2020-07-30 21:38:15 +00:00
|
|
|
die();
|
|
|
|
// We can not return from here, as there is nowhere
|
|
|
|
// to unwind to, so die right away.
|
|
|
|
Thread::current()->die_if_needed();
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-12-09 18:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 15:32:34 +00:00
|
|
|
RefPtr<Process> Process::from_pid(ProcessID pid)
|
2018-10-26 07:54:29 +00:00
|
|
|
{
|
2020-07-30 21:38:15 +00:00
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
|
|
|
for (auto& process : *g_processes) {
|
2020-08-08 15:32:34 +00:00
|
|
|
process.pid();
|
2020-07-30 21:38:15 +00:00
|
|
|
if (process.pid() == pid)
|
|
|
|
return &process;
|
|
|
|
}
|
2020-08-02 02:04:56 +00:00
|
|
|
return {};
|
2018-10-26 07:54:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
RefPtr<FileDescription> Process::file_description(int fd) const
|
2020-04-26 05:59:47 +00:00
|
|
|
{
|
2020-07-30 21:38:15 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return nullptr;
|
|
|
|
if (static_cast<size_t>(fd) < m_fds.size())
|
2020-07-30 21:50:31 +00:00
|
|
|
return m_fds[fd].description();
|
2020-07-30 21:38:15 +00:00
|
|
|
return nullptr;
|
2020-04-26 05:59:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
int Process::fd_flags(int fd) const
|
2018-11-02 19:41:58 +00:00
|
|
|
{
|
2020-07-30 21:38:15 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
if (static_cast<size_t>(fd) < m_fds.size())
|
2020-07-30 21:50:31 +00:00
|
|
|
return m_fds[fd].flags();
|
2020-07-30 21:38:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-11-10 22:29:07 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
int Process::number_of_open_file_descriptors() const
|
2019-11-14 19:58:23 +00:00
|
|
|
{
|
2020-07-30 21:38:15 +00:00
|
|
|
int count = 0;
|
|
|
|
for (auto& description : m_fds) {
|
|
|
|
if (description)
|
|
|
|
++count;
|
2020-01-10 18:15:01 +00:00
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
return count;
|
2019-04-08 23:10:00 +00:00
|
|
|
}
|
2019-04-22 16:44:45 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
int Process::alloc_fd(int first_candidate_fd)
|
2019-07-22 18:01:11 +00:00
|
|
|
{
|
2020-07-30 21:38:15 +00:00
|
|
|
for (int i = first_candidate_fd; i < (int)m_max_open_file_descriptors; ++i) {
|
|
|
|
if (!m_fds[i])
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -EMFILE;
|
2019-07-22 18:01:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:18:48 +00:00
|
|
|
Time kgettimeofday()
|
2019-07-19 11:08:26 +00:00
|
|
|
{
|
2021-02-28 01:18:48 +00:00
|
|
|
return TimeManagement::now();
|
2019-07-19 11:08:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
siginfo_t Process::wait_info()
|
2019-08-02 13:18:47 +00:00
|
|
|
{
|
2021-02-21 10:16:16 +00:00
|
|
|
siginfo_t siginfo {};
|
2020-07-30 21:38:15 +00:00
|
|
|
siginfo.si_signo = SIGCHLD;
|
2020-11-29 23:05:27 +00:00
|
|
|
siginfo.si_pid = pid().value();
|
|
|
|
siginfo.si_uid = uid();
|
2020-05-28 18:12:13 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
if (m_termination_signal) {
|
|
|
|
siginfo.si_status = m_termination_signal;
|
2020-07-30 21:38:15 +00:00
|
|
|
siginfo.si_code = CLD_KILLED;
|
|
|
|
} else {
|
2020-11-29 23:05:27 +00:00
|
|
|
siginfo.si_status = m_termination_status;
|
2020-07-30 21:38:15 +00:00
|
|
|
siginfo.si_code = CLD_EXITED;
|
2020-01-11 16:08:35 +00:00
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
return siginfo;
|
2019-07-25 19:02:19 +00:00
|
|
|
}
|
2019-07-29 05:26:01 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
Custody& Process::current_directory()
|
2019-10-13 14:41:55 +00:00
|
|
|
{
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!m_cwd)
|
|
|
|
m_cwd = VFS::the().root_custody();
|
|
|
|
return *m_cwd;
|
2019-10-13 14:41:55 +00:00
|
|
|
}
|
2019-11-02 18:34:06 +00:00
|
|
|
|
2021-05-29 14:59:40 +00:00
|
|
|
KResultOr<NonnullOwnPtr<KString>> Process::get_syscall_path_argument(char const* user_path, size_t path_length) const
|
2019-11-23 07:48:07 +00:00
|
|
|
{
|
2020-07-30 21:38:15 +00:00
|
|
|
if (path_length == 0)
|
2021-01-20 22:11:17 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
if (path_length > PATH_MAX)
|
2021-01-20 22:11:17 +00:00
|
|
|
return ENAMETOOLONG;
|
2021-05-29 14:59:40 +00:00
|
|
|
auto string_or_error = try_copy_kstring_from_user(user_path, path_length);
|
|
|
|
if (string_or_error.is_error())
|
|
|
|
return string_or_error.error();
|
|
|
|
return string_or_error.release_value();
|
2019-11-23 07:48:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-29 14:59:40 +00:00
|
|
|
KResultOr<NonnullOwnPtr<KString>> Process::get_syscall_path_argument(Syscall::StringArgument const& path) const
|
2019-11-02 18:34:06 +00:00
|
|
|
{
|
2020-07-30 21:38:15 +00:00
|
|
|
return get_syscall_path_argument(path.characters, path.length);
|
2019-11-02 18:34:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 17:53:45 +00:00
|
|
|
bool Process::dump_core()
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(is_dumpable());
|
|
|
|
VERIFY(should_core_dump());
|
2021-03-10 18:59:46 +00:00
|
|
|
dbgln("Generating coredump for pid: {}", pid().value());
|
|
|
|
auto coredump_path = String::formatted("/tmp/coredump/{}_{}_{}", name(), pid().value(), RTC::now());
|
2021-01-11 17:53:45 +00:00
|
|
|
auto coredump = CoreDump::create(*this, coredump_path);
|
|
|
|
if (!coredump)
|
|
|
|
return false;
|
|
|
|
return !coredump->write().is_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Process::dump_perfcore()
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(is_dumpable());
|
|
|
|
VERIFY(m_perf_event_buffer);
|
2021-03-10 18:59:46 +00:00
|
|
|
dbgln("Generating perfcore for pid: {}", pid().value());
|
|
|
|
auto description_or_error = VFS::the().open(String::formatted("perfcore.{}", pid().value()), O_CREAT | O_EXCL, 0400, current_directory(), UidAndGid { uid(), gid() });
|
2021-01-11 17:53:45 +00:00
|
|
|
if (description_or_error.is_error())
|
|
|
|
return false;
|
|
|
|
auto& description = description_or_error.value();
|
2021-03-02 18:01:02 +00:00
|
|
|
KBufferBuilder builder;
|
|
|
|
if (!m_perf_event_buffer->to_json(builder))
|
2021-01-11 17:53:45 +00:00
|
|
|
return false;
|
|
|
|
|
2021-03-02 18:01:02 +00:00
|
|
|
auto json = builder.build();
|
|
|
|
if (!json)
|
|
|
|
return false;
|
2021-01-11 17:53:45 +00:00
|
|
|
auto json_buffer = UserOrKernelBuffer::for_kernel_buffer(json->data());
|
2021-05-22 20:07:40 +00:00
|
|
|
if (description->write(json_buffer, json->size()).is_error())
|
|
|
|
return false;
|
|
|
|
dbgln("Wrote perfcore to {}", description->absolute_path());
|
|
|
|
return true;
|
2021-01-11 17:53:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 04:18:45 +00:00
|
|
|
void Process::finalize()
|
2020-03-12 23:17:14 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(Thread::current() == g_finalizer);
|
2021-01-12 21:30:52 +00:00
|
|
|
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(PROCESS_DEBUG, "Finalizing process {}", *this);
|
2020-03-12 23:17:14 +00:00
|
|
|
|
2021-01-11 17:53:45 +00:00
|
|
|
if (is_dumpable()) {
|
|
|
|
if (m_should_dump_core)
|
|
|
|
dump_core();
|
2021-05-17 13:28:46 +00:00
|
|
|
if (m_perf_event_buffer) {
|
2021-01-11 17:53:45 +00:00
|
|
|
dump_perfcore();
|
2021-05-17 13:28:46 +00:00
|
|
|
TimeManagement::the().disable_profile_timer();
|
|
|
|
}
|
2020-03-12 23:17:14 +00:00
|
|
|
}
|
2019-11-02 18:34:06 +00:00
|
|
|
|
2021-01-15 19:29:13 +00:00
|
|
|
m_threads_for_coredump.clear();
|
|
|
|
|
2020-12-01 22:44:52 +00:00
|
|
|
if (m_alarm_timer)
|
|
|
|
TimerQueue::the().cancel_timer(m_alarm_timer.release_nonnull());
|
2020-07-30 21:38:15 +00:00
|
|
|
m_fds.clear();
|
|
|
|
m_tty = nullptr;
|
|
|
|
m_executable = nullptr;
|
|
|
|
m_cwd = nullptr;
|
|
|
|
m_root_directory = nullptr;
|
|
|
|
m_root_directory_relative_to_global_root = nullptr;
|
2021-01-15 19:21:03 +00:00
|
|
|
m_arguments.clear();
|
|
|
|
m_environment.clear();
|
2020-03-03 04:12:39 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
m_dead = true;
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2020-08-08 15:32:34 +00:00
|
|
|
// FIXME: PID/TID BUG
|
2021-03-10 19:09:49 +00:00
|
|
|
if (auto parent_thread = Thread::from_tid(ppid().value())) {
|
2020-11-29 23:05:27 +00:00
|
|
|
if (!(parent_thread->m_signal_action_data[SIGCHLD].flags & SA_NOCLDWAIT))
|
2020-07-30 21:38:15 +00:00
|
|
|
parent_thread->send_signal(SIGCHLD, this);
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ScopedSpinLock processses_lock(g_processes_lock);
|
|
|
|
if (!!ppid()) {
|
|
|
|
if (auto parent = Process::from_pid(ppid())) {
|
|
|
|
parent->m_ticks_in_user_for_dead_children += m_ticks_in_user + m_ticks_in_user_for_dead_children;
|
|
|
|
parent->m_ticks_in_kernel_for_dead_children += m_ticks_in_kernel + m_ticks_in_kernel_for_dead_children;
|
2019-11-02 18:34:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-12-09 04:18:45 +00:00
|
|
|
unblock_waiters(Thread::WaitBlocker::UnblockFlags::Terminated);
|
2020-11-29 23:05:27 +00:00
|
|
|
|
2021-02-08 14:45:40 +00:00
|
|
|
m_space->remove_all_regions({});
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(ref_count() > 0);
|
2020-11-29 23:05:27 +00:00
|
|
|
// WaitBlockCondition::finalize will be in charge of dropping the last
|
|
|
|
// reference if there are still waiters around, or whenever the last
|
|
|
|
// waitable states are consumed. Unless there is no parent around
|
|
|
|
// anymore, in which case we'll just drop it right away.
|
|
|
|
m_wait_block_condition.finalize();
|
|
|
|
}
|
|
|
|
|
2020-12-09 02:04:05 +00:00
|
|
|
void Process::disowned_by_waiter(Process& process)
|
|
|
|
{
|
|
|
|
m_wait_block_condition.disowned_by_waiter(process);
|
|
|
|
}
|
|
|
|
|
2020-12-09 04:18:45 +00:00
|
|
|
void Process::unblock_waiters(Thread::WaitBlocker::UnblockFlags flags, u8 signal)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
|
|
|
if (auto parent = Process::from_pid(ppid()))
|
2020-12-09 04:18:45 +00:00
|
|
|
parent->m_wait_block_condition.unblock(*this, flags, signal);
|
2019-11-02 18:34:06 +00:00
|
|
|
}
|
2019-11-09 21:18:16 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
void Process::die()
|
2019-11-09 21:18:16 +00:00
|
|
|
{
|
2020-07-30 21:38:15 +00:00
|
|
|
// Let go of the TTY, otherwise a slave PTY may keep the master PTY from
|
|
|
|
// getting an EOF when the last process using the slave PTY dies.
|
|
|
|
// If the master PTY owner relies on an EOF to know when to wait() on a
|
|
|
|
// slave owner, we have to allow the PTY pair to be torn down.
|
|
|
|
m_tty = nullptr;
|
|
|
|
|
2021-06-14 13:47:19 +00:00
|
|
|
VERIFY(m_threads_for_coredump.is_empty());
|
2021-01-15 19:29:13 +00:00
|
|
|
for_each_thread([&](auto& thread) {
|
2021-01-28 07:41:18 +00:00
|
|
|
m_threads_for_coredump.append(thread);
|
2021-01-15 19:29:13 +00:00
|
|
|
});
|
|
|
|
|
2021-02-25 15:19:31 +00:00
|
|
|
{
|
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
2021-06-06 21:40:03 +00:00
|
|
|
for (auto it = g_processes->begin(); it != g_processes->end();) {
|
|
|
|
auto& process = *it;
|
|
|
|
++it;
|
|
|
|
if (process.has_tracee_thread(pid())) {
|
|
|
|
dbgln_if(PROCESS_DEBUG, "Process {} ({}) is attached by {} ({}) which will exit", process.name(), process.pid(), name(), pid());
|
|
|
|
process.stop_tracing();
|
|
|
|
auto err = process.send_signal(SIGSTOP, this);
|
2021-02-25 15:19:31 +00:00
|
|
|
if (err.is_error())
|
2021-06-06 21:40:03 +00:00
|
|
|
dbgln("Failed to send the SIGSTOP signal to {} ({})", process.name(), process.pid());
|
2021-02-25 15:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
kill_all_threads();
|
2019-11-09 21:18:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
void Process::terminate_due_to_signal(u8 signal)
|
2019-12-22 20:29:47 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_INTERRUPTS_DISABLED();
|
|
|
|
VERIFY(signal < 32);
|
|
|
|
VERIFY(Process::current() == this);
|
2021-01-08 23:11:15 +00:00
|
|
|
dbgln("Terminating {} due to signal {}", *this, signal);
|
2021-03-11 13:24:08 +00:00
|
|
|
{
|
|
|
|
ProtectedDataMutationScope scope { *this };
|
|
|
|
m_termination_status = 0;
|
|
|
|
m_termination_signal = signal;
|
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
die();
|
2019-12-22 20:29:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
KResult Process::send_signal(u8 signal, Process* sender)
|
2019-12-22 20:29:47 +00:00
|
|
|
{
|
2020-08-09 13:23:31 +00:00
|
|
|
// Try to send it to the "obvious" main thread:
|
2021-03-10 18:59:46 +00:00
|
|
|
auto receiver_thread = Thread::from_tid(pid().value());
|
2020-08-09 13:23:31 +00:00
|
|
|
// If the main thread has died, there may still be other threads:
|
|
|
|
if (!receiver_thread) {
|
|
|
|
// The first one should be good enough.
|
|
|
|
// Neither kill(2) nor kill(3) specify any selection precedure.
|
|
|
|
for_each_thread([&receiver_thread](Thread& thread) -> IterationDecision {
|
|
|
|
receiver_thread = &thread;
|
|
|
|
return IterationDecision::Break;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (receiver_thread) {
|
|
|
|
receiver_thread->send_signal(signal, sender);
|
2020-07-30 21:38:15 +00:00
|
|
|
return KSuccess;
|
|
|
|
}
|
2021-01-20 22:11:17 +00:00
|
|
|
return ESRCH;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2019-12-22 20:29:47 +00:00
|
|
|
|
2020-11-17 03:51:34 +00:00
|
|
|
RefPtr<Thread> Process::create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, const String& name, u32 affinity, bool joinable)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY((priority >= THREAD_PRIORITY_MIN) && (priority <= THREAD_PRIORITY_MAX));
|
2020-04-26 09:40:24 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
// FIXME: Do something with guard pages?
|
2020-04-26 09:40:24 +00:00
|
|
|
|
2021-02-07 17:13:51 +00:00
|
|
|
auto thread_or_error = Thread::try_create(*this);
|
|
|
|
if (thread_or_error.is_error())
|
2020-09-05 21:52:14 +00:00
|
|
|
return {};
|
2020-04-26 09:40:24 +00:00
|
|
|
|
2021-02-07 21:13:30 +00:00
|
|
|
auto thread = thread_or_error.release_value();
|
2020-07-30 21:38:15 +00:00
|
|
|
thread->set_name(name);
|
|
|
|
thread->set_affinity(affinity);
|
|
|
|
thread->set_priority(priority);
|
2020-09-26 03:44:43 +00:00
|
|
|
if (!joinable)
|
|
|
|
thread->detach();
|
2020-04-26 09:40:24 +00:00
|
|
|
|
2021-06-23 19:54:41 +00:00
|
|
|
#if ARCH(I386)
|
2020-07-30 21:38:15 +00:00
|
|
|
auto& tss = thread->tss();
|
|
|
|
tss.eip = (FlatPtr)entry;
|
2020-11-17 03:51:34 +00:00
|
|
|
tss.esp = FlatPtr(entry_data); // entry function argument is expected to be in tss.esp
|
2021-06-23 19:54:41 +00:00
|
|
|
#else
|
|
|
|
(void)entry;
|
|
|
|
(void)entry_data;
|
|
|
|
PANIC("Process::create_kernel_thread() not implemented");
|
|
|
|
#endif
|
2019-12-22 20:29:47 +00:00
|
|
|
|
2020-10-26 02:22:59 +00:00
|
|
|
ScopedSpinLock lock(g_scheduler_lock);
|
2020-07-30 21:38:15 +00:00
|
|
|
thread->set_state(Thread::State::Runnable);
|
|
|
|
return thread;
|
2019-12-22 20:29:47 +00:00
|
|
|
}
|
2019-12-30 18:23:13 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
void Process::FileDescriptionAndFlags::clear()
|
2019-12-30 18:23:13 +00:00
|
|
|
{
|
2020-07-30 21:50:31 +00:00
|
|
|
m_description = nullptr;
|
|
|
|
m_flags = 0;
|
2019-12-30 18:23:13 +00:00
|
|
|
}
|
2019-12-30 19:10:00 +00:00
|
|
|
|
2020-07-30 21:50:31 +00:00
|
|
|
void Process::FileDescriptionAndFlags::set(NonnullRefPtr<FileDescription>&& description, u32 flags)
|
2019-12-30 19:10:00 +00:00
|
|
|
{
|
2020-07-30 21:50:31 +00:00
|
|
|
m_description = move(description);
|
|
|
|
m_flags = flags;
|
2019-12-30 19:10:00 +00:00
|
|
|
}
|
2020-01-10 22:14:04 +00:00
|
|
|
|
|
|
|
Custody& Process::root_directory()
|
|
|
|
{
|
|
|
|
if (!m_root_directory)
|
|
|
|
m_root_directory = VFS::the().root_custody();
|
|
|
|
return *m_root_directory;
|
|
|
|
}
|
|
|
|
|
2020-01-12 18:42:01 +00:00
|
|
|
Custody& Process::root_directory_relative_to_global_root()
|
2020-01-10 22:48:44 +00:00
|
|
|
{
|
2020-01-12 18:42:01 +00:00
|
|
|
if (!m_root_directory_relative_to_global_root)
|
|
|
|
m_root_directory_relative_to_global_root = root_directory();
|
|
|
|
return *m_root_directory_relative_to_global_root;
|
2020-01-10 22:48:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 22:14:04 +00:00
|
|
|
void Process::set_root_directory(const Custody& root)
|
|
|
|
{
|
|
|
|
m_root_directory = root;
|
|
|
|
}
|
2020-01-11 19:48:43 +00:00
|
|
|
|
2020-02-16 01:01:42 +00:00
|
|
|
void Process::set_tty(TTY* tty)
|
|
|
|
{
|
|
|
|
m_tty = tty;
|
|
|
|
}
|
|
|
|
|
2021-05-13 04:44:07 +00:00
|
|
|
KResult Process::start_tracing_from(ProcessID tracer)
|
2020-12-09 04:18:45 +00:00
|
|
|
{
|
2021-05-13 04:44:07 +00:00
|
|
|
auto thread_tracer = ThreadTracer::create(tracer);
|
|
|
|
if (!thread_tracer)
|
|
|
|
return ENOMEM;
|
|
|
|
m_tracer = move(thread_tracer);
|
|
|
|
return KSuccess;
|
2020-12-09 04:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Process::stop_tracing()
|
|
|
|
{
|
|
|
|
m_tracer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Process::tracer_trap(Thread& thread, const RegisterState& regs)
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_tracer.ptr());
|
2020-12-09 04:18:45 +00:00
|
|
|
m_tracer->set_regs(regs);
|
|
|
|
thread.send_urgent_signal_to_self(SIGTRAP);
|
|
|
|
}
|
|
|
|
|
2021-03-02 15:55:54 +00:00
|
|
|
bool Process::create_perf_events_buffer_if_needed()
|
2021-01-11 08:52:18 +00:00
|
|
|
{
|
2021-03-02 15:55:54 +00:00
|
|
|
if (!m_perf_event_buffer) {
|
|
|
|
m_perf_event_buffer = PerformanceEventBuffer::try_create_with_size(4 * MiB);
|
2021-04-25 21:42:36 +00:00
|
|
|
m_perf_event_buffer->add_process(*this, ProcessEventType::Create);
|
2021-03-02 15:55:54 +00:00
|
|
|
}
|
|
|
|
return !!m_perf_event_buffer;
|
2021-01-11 08:52:18 +00:00
|
|
|
}
|
2021-01-23 06:24:33 +00:00
|
|
|
|
2021-04-19 04:10:05 +00:00
|
|
|
void Process::delete_perf_events_buffer()
|
|
|
|
{
|
|
|
|
if (m_perf_event_buffer)
|
|
|
|
m_perf_event_buffer = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-01-23 06:24:33 +00:00
|
|
|
bool Process::remove_thread(Thread& thread)
|
|
|
|
{
|
2021-03-11 13:12:55 +00:00
|
|
|
ProtectedDataMutationScope scope { *this };
|
2021-01-23 06:24:33 +00:00
|
|
|
auto thread_cnt_before = m_thread_count.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(thread_cnt_before != 0);
|
2021-01-23 06:24:33 +00:00
|
|
|
ScopedSpinLock thread_list_lock(m_thread_list_lock);
|
|
|
|
m_thread_list.remove(thread);
|
|
|
|
return thread_cnt_before == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Process::add_thread(Thread& thread)
|
|
|
|
{
|
2021-03-11 13:12:55 +00:00
|
|
|
ProtectedDataMutationScope scope { *this };
|
2021-01-23 06:24:33 +00:00
|
|
|
bool is_first = m_thread_count.fetch_add(1, AK::MemoryOrder::memory_order_relaxed) == 0;
|
|
|
|
ScopedSpinLock thread_list_lock(m_thread_list_lock);
|
|
|
|
m_thread_list.append(thread);
|
|
|
|
return is_first;
|
|
|
|
}
|
|
|
|
|
2021-03-10 21:42:07 +00:00
|
|
|
void Process::set_dumpable(bool dumpable)
|
|
|
|
{
|
2021-03-11 12:13:05 +00:00
|
|
|
if (dumpable == m_dumpable)
|
2021-03-10 21:42:07 +00:00
|
|
|
return;
|
2021-03-11 12:13:05 +00:00
|
|
|
ProtectedDataMutationScope scope { *this };
|
|
|
|
m_dumpable = dumpable;
|
2021-03-10 21:42:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 18:11:54 +00:00
|
|
|
void Process::set_coredump_metadata(const String& key, String value)
|
|
|
|
{
|
|
|
|
m_coredump_metadata.set(key, move(value));
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|