2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-01-18 23:31:29 +01:00
|
|
|
#include <AK/Demangle.h>
|
2020-08-22 16:48:27 +02:00
|
|
|
#include <AK/QuickSort.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <AK/StdLibExtras.h>
|
2019-02-17 00:13:47 +01:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-05-18 02:00:01 +02:00
|
|
|
#include <AK/Time.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <AK/Types.h>
|
2020-07-04 17:37:36 -06:00
|
|
|
#include <Kernel/API/Syscall.h>
|
2019-06-07 20:02:01 +02:00
|
|
|
#include <Kernel/Arch/i386/CPU.h>
|
2019-11-28 20:59:11 +01:00
|
|
|
#include <Kernel/Devices/NullDevice.h>
|
2019-05-30 18:58:59 +02:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2019-10-20 08:24:42 -06:00
|
|
|
#include <Kernel/Heap/kmalloc.h>
|
2019-08-07 21:52:43 +02:00
|
|
|
#include <Kernel/KBufferBuilder.h>
|
2019-06-07 19:29:34 +02:00
|
|
|
#include <Kernel/KSyms.h>
|
2019-11-28 20:59:11 +01:00
|
|
|
#include <Kernel/Module.h>
|
2020-02-16 02:01:42 +01:00
|
|
|
#include <Kernel/PerformanceEventBuffer.h>
|
2019-06-07 19:29:34 +02:00
|
|
|
#include <Kernel/Process.h>
|
2019-07-16 15:03:39 +02:00
|
|
|
#include <Kernel/SharedBuffer.h>
|
2020-05-16 03:02:14 +04:30
|
|
|
#include <Kernel/StdLib.h>
|
2020-02-16 02:01:42 +01:00
|
|
|
#include <Kernel/TTY/TTY.h>
|
2019-10-20 10:11:40 -06:00
|
|
|
#include <Kernel/Thread.h>
|
2020-02-16 01:33:41 +01:00
|
|
|
#include <Kernel/VM/PageDirectory.h>
|
2020-02-28 20:29:14 +01:00
|
|
|
#include <Kernel/VM/SharedInodeVMObject.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <LibC/errno_numbers.h>
|
2020-01-02 13:01:41 +01:00
|
|
|
#include <LibC/limits.h>
|
2020-04-11 12:24:07 -06:00
|
|
|
#include <LibELF/Loader.h>
|
2018-10-16 11:01:38 +02:00
|
|
|
|
2018-10-23 10:12:50 +02:00
|
|
|
//#define DEBUG_IO
|
2020-08-27 00:58:09 +02:00
|
|
|
//#define DEBUG_POLL_SELECT
|
|
|
|
//#define MM_DEBUG
|
|
|
|
//#define PROCESS_DEBUG
|
2019-07-15 20:38:41 +02:00
|
|
|
//#define SIGNAL_DEBUG
|
2018-11-05 13:48:07 +01:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-07-19 17:01:16 +02:00
|
|
|
static void create_signal_trampolines();
|
|
|
|
|
2020-06-28 15:34:31 -06:00
|
|
|
RecursiveSpinLock g_processes_lock;
|
|
|
|
static Atomic<pid_t> next_pid;
|
2018-11-07 22:15:02 +01:00
|
|
|
InlineLinkedList<Process>* g_processes;
|
2020-07-30 23:38:15 +02:00
|
|
|
String* g_hostname;
|
|
|
|
Lock* g_hostname_lock;
|
2019-07-19 17:01:16 +02:00
|
|
|
VirtualAddress g_return_to_ring3_from_signal_trampoline;
|
2019-11-28 20:59:11 +01:00
|
|
|
HashMap<String, OwnPtr<Module>>* g_modules;
|
2018-10-26 14:56:21 +02:00
|
|
|
|
2020-08-08 17:32:34 +02:00
|
|
|
ProcessID Process::allocate_pid()
|
2019-12-22 11:51:24 +01:00
|
|
|
{
|
2020-08-08 17:32:34 +02: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 22:04:20 +02: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 15:34:31 -06:00
|
|
|
return next_pid.fetch_add(1, AK::MemoryOrder::memory_order_acq_rel);
|
2019-12-22 11:51:24 +01:00
|
|
|
}
|
|
|
|
|
2018-11-01 13:15:46 +01:00
|
|
|
void Process::initialize()
|
2018-10-16 11:01:38 +02:00
|
|
|
{
|
2019-11-28 20:59:11 +01:00
|
|
|
g_modules = new HashMap<String, OwnPtr<Module>>;
|
|
|
|
|
2020-06-28 15:34:31 -06:00
|
|
|
next_pid.store(0, AK::MemoryOrder::memory_order_release);
|
2018-11-07 22:15:02 +01:00
|
|
|
g_processes = new InlineLinkedList<Process>;
|
2020-08-15 23:43:19 +04:30
|
|
|
g_process_groups = new InlineLinkedList<ProcessGroup>;
|
2020-07-30 23:38:15 +02:00
|
|
|
g_hostname = new String("courage");
|
|
|
|
g_hostname_lock = new Lock;
|
2019-07-19 17:01:16 +02:00
|
|
|
|
|
|
|
create_signal_trampolines();
|
2018-11-02 14:06:48 +01:00
|
|
|
}
|
|
|
|
|
2020-08-08 17:32:34 +02:00
|
|
|
Vector<ProcessID> Process::all_pids()
|
2019-02-03 12:33:11 +01:00
|
|
|
{
|
2020-08-08 17:32:34 +02:00
|
|
|
Vector<ProcessID> pids;
|
2020-06-28 15:34:31 -06:00
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
2019-12-09 17:48:58 +01:00
|
|
|
pids.ensure_capacity((int)g_processes->size_slow());
|
2019-08-08 14:40:13 +02:00
|
|
|
for (auto& process : *g_processes)
|
|
|
|
pids.append(process.pid());
|
2019-02-03 12:33:11 +01:00
|
|
|
return pids;
|
|
|
|
}
|
|
|
|
|
2020-08-01 20:04:56 -06:00
|
|
|
NonnullRefPtrVector<Process> Process::all_processes()
|
2018-10-23 12:44:46 +02:00
|
|
|
{
|
2020-08-01 20:04:56 -06:00
|
|
|
NonnullRefPtrVector<Process> processes;
|
2020-06-28 15:34:31 -06:00
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
2019-12-09 17:48:58 +01:00
|
|
|
processes.ensure_capacity((int)g_processes->size_slow());
|
2019-08-08 14:40:13 +02:00
|
|
|
for (auto& process : *g_processes)
|
2020-08-01 20:04:56 -06:00
|
|
|
processes.append(NonnullRefPtr<Process>(process));
|
2018-11-01 13:15:46 +01:00
|
|
|
return processes;
|
2018-10-23 12:44:46 +02:00
|
|
|
}
|
|
|
|
|
2019-02-27 12:32:53 +01:00
|
|
|
bool Process::in_group(gid_t gid) const
|
|
|
|
{
|
2020-09-07 11:53:54 +02:00
|
|
|
return m_gid == gid || m_extra_gids.contains_slow(gid);
|
2019-02-27 12:32:53 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 12:55:56 +01:00
|
|
|
Range Process::allocate_range(VirtualAddress vaddr, size_t size, size_t alignment)
|
2018-10-18 13:05:00 +02:00
|
|
|
{
|
2019-06-07 12:56:50 +02:00
|
|
|
vaddr.mask(PAGE_MASK);
|
2019-01-13 00:27:25 +01:00
|
|
|
size = PAGE_ROUND_UP(size);
|
2019-06-07 12:56:50 +02:00
|
|
|
if (vaddr.is_null())
|
2020-02-16 12:55:56 +01:00
|
|
|
return page_directory().range_allocator().allocate_anywhere(size, alignment);
|
2019-06-07 12:56:50 +02:00
|
|
|
return page_directory().range_allocator().allocate_specific(vaddr, size);
|
2019-05-17 04:39:22 +02:00
|
|
|
}
|
2019-05-17 03:40:15 +02:00
|
|
|
|
2019-12-19 19:13:44 +01:00
|
|
|
Region& Process::allocate_split_region(const Region& source_region, const Range& range, size_t offset_in_vmobject)
|
2019-08-29 20:57:02 +02:00
|
|
|
{
|
2020-01-25 17:57:10 +01:00
|
|
|
auto& region = add_region(Region::create_user_accessible(range, source_region.vmobject(), offset_in_vmobject, source_region.name(), source_region.access()));
|
2020-01-29 19:24:42 +01:00
|
|
|
region.set_mmap(source_region.is_mmap());
|
|
|
|
region.set_stack(source_region.is_stack());
|
2020-01-25 17:57:10 +01:00
|
|
|
size_t page_offset_in_source_region = (offset_in_vmobject - source_region.offset_in_vmobject()) / PAGE_SIZE;
|
|
|
|
for (size_t i = 0; i < region.page_count(); ++i) {
|
|
|
|
if (source_region.should_cow(page_offset_in_source_region + i))
|
|
|
|
region.set_should_cow(i, true);
|
|
|
|
}
|
|
|
|
return region;
|
2019-08-29 20:57:02 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 21:47:08 +02:00
|
|
|
Region* Process::allocate_region(const Range& range, const String& name, int prot, bool should_commit)
|
2019-05-17 04:39:22 +02:00
|
|
|
{
|
2020-02-16 12:55:56 +01:00
|
|
|
ASSERT(range.is_valid());
|
2020-03-01 11:02:22 +01:00
|
|
|
auto vmobject = AnonymousVMObject::create_with_size(range.size());
|
2020-05-08 21:47:08 +02:00
|
|
|
auto region = Region::create_user_accessible(range, vmobject, 0, name, prot_to_region_access_flags(prot));
|
|
|
|
region->map(page_directory());
|
|
|
|
if (should_commit && !region->commit())
|
|
|
|
return nullptr;
|
|
|
|
return &add_region(move(region));
|
2018-10-18 13:05:00 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 21:47:08 +02:00
|
|
|
Region* Process::allocate_region(VirtualAddress vaddr, size_t size, const String& name, int prot, bool should_commit)
|
2020-02-16 12:55:56 +01:00
|
|
|
{
|
|
|
|
auto range = allocate_range(vaddr, size);
|
|
|
|
if (!range.is_valid())
|
|
|
|
return nullptr;
|
2020-05-08 21:47:08 +02:00
|
|
|
return allocate_region(range, name, prot, should_commit);
|
2020-02-16 12:55:56 +01:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:46:24 +01:00
|
|
|
Region* Process::allocate_region_with_vmobject(const Range& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const String& name, int prot)
|
2018-11-08 21:20:09 +01:00
|
|
|
{
|
2020-02-16 12:55:56 +01:00
|
|
|
ASSERT(range.is_valid());
|
|
|
|
size_t end_in_vmobject = offset_in_vmobject + range.size();
|
2020-02-18 10:19:32 +01:00
|
|
|
if (end_in_vmobject <= offset_in_vmobject) {
|
2020-02-25 00:13:33 +02:00
|
|
|
dbg() << "allocate_region_with_vmobject: Overflow (offset + size)";
|
2020-01-18 14:37:22 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (offset_in_vmobject >= vmobject->size()) {
|
2020-02-25 00:13:33 +02:00
|
|
|
dbg() << "allocate_region_with_vmobject: Attempt to allocate a region with an offset past the end of its VMObject.";
|
2020-01-18 14:37:22 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (end_in_vmobject > vmobject->size()) {
|
2020-02-25 00:13:33 +02:00
|
|
|
dbg() << "allocate_region_with_vmobject: Attempt to allocate a region with an end past the end of its VMObject.";
|
2020-01-18 14:37:22 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-12-19 19:13:44 +01:00
|
|
|
offset_in_vmobject &= PAGE_MASK;
|
2020-03-02 10:46:24 +01:00
|
|
|
auto& region = add_region(Region::create_user_accessible(range, move(vmobject), offset_in_vmobject, name, prot_to_region_access_flags(prot)));
|
|
|
|
region.map(page_directory());
|
|
|
|
return ®ion;
|
2018-11-08 21:20:09 +01:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:46:24 +01:00
|
|
|
Region* Process::allocate_region_with_vmobject(VirtualAddress vaddr, size_t size, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const String& name, int prot)
|
2020-02-16 12:55:56 +01:00
|
|
|
{
|
|
|
|
auto range = allocate_range(vaddr, size);
|
|
|
|
if (!range.is_valid())
|
|
|
|
return nullptr;
|
2020-03-02 10:46:24 +01:00
|
|
|
return allocate_region_with_vmobject(range, move(vmobject), offset_in_vmobject, name, prot);
|
2020-02-16 12:55:56 +01:00
|
|
|
}
|
|
|
|
|
2018-11-03 11:28:23 +01:00
|
|
|
bool Process::deallocate_region(Region& region)
|
2018-10-24 09:48:24 +02:00
|
|
|
{
|
2020-07-02 11:53:19 +03:00
|
|
|
OwnPtr<Region> region_protector;
|
2020-06-28 15:34:31 -06:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-07-02 11:53:19 +03:00
|
|
|
|
2020-01-19 16:44:37 +01:00
|
|
|
if (m_region_lookup_cache.region == ®ion)
|
|
|
|
m_region_lookup_cache.region = nullptr;
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = 0; i < m_regions.size(); ++i) {
|
2019-06-27 13:34:28 +02:00
|
|
|
if (&m_regions[i] == ®ion) {
|
2020-07-02 11:53:19 +03:00
|
|
|
region_protector = m_regions.unstable_take(i);
|
2018-10-24 09:48:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-30 23:52:28 +02:00
|
|
|
Region* Process::find_region_from_range(const Range& range)
|
2019-08-29 20:57:02 +02:00
|
|
|
{
|
2020-06-28 15:34:31 -06:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-01-19 16:44:37 +01:00
|
|
|
if (m_region_lookup_cache.range == range && m_region_lookup_cache.region)
|
|
|
|
return m_region_lookup_cache.region;
|
|
|
|
|
2019-08-29 20:57:02 +02:00
|
|
|
size_t size = PAGE_ROUND_UP(range.size());
|
|
|
|
for (auto& region : m_regions) {
|
2020-01-19 16:44:37 +01:00
|
|
|
if (region.vaddr() == range.base() && region.size() == size) {
|
|
|
|
m_region_lookup_cache.range = range;
|
2020-02-24 13:24:30 +01:00
|
|
|
m_region_lookup_cache.region = region.make_weak_ptr();
|
2019-08-29 20:57:02 +02:00
|
|
|
return ®ion;
|
2020-01-19 16:44:37 +01:00
|
|
|
}
|
2019-08-29 20:57:02 +02:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-07-30 23:52:28 +02:00
|
|
|
Region* Process::find_region_containing(const Range& range)
|
2018-10-24 09:48:24 +02:00
|
|
|
{
|
2020-06-28 15:34:31 -06:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2018-10-24 09:48:24 +02:00
|
|
|
for (auto& region : m_regions) {
|
2019-08-29 20:57:02 +02:00
|
|
|
if (region.contains(range))
|
2019-06-27 13:34:28 +02:00
|
|
|
return ®ion;
|
2018-10-24 09:48:24 +02:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
void Process::kill_threads_except_self()
|
2018-10-28 09:57:22 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
InterruptDisabler disabler;
|
2018-10-28 09:57:22 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
if (thread_count() <= 1)
|
|
|
|
return;
|
2019-12-25 21:50:13 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
auto current_thread = Thread::current();
|
|
|
|
for_each_thread([&](Thread& thread) {
|
|
|
|
if (&thread == current_thread
|
|
|
|
|| thread.state() == Thread::State::Dead
|
|
|
|
|| thread.state() == Thread::State::Dying)
|
|
|
|
return IterationDecision::Continue;
|
2019-12-25 21:50:13 +01:00
|
|
|
|
2020-09-25 21:44:43 -06:00
|
|
|
// We need to detach this thread in case it hasn't been joined
|
|
|
|
thread.detach();
|
2020-07-30 23:38:15 +02:00
|
|
|
thread.set_should_die();
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2020-02-28 20:47:27 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
big_lock().clear_waiters();
|
2020-01-07 19:29:18 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
void Process::kill_all_threads()
|
2019-12-30 15:11:25 -05:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
for_each_thread([&](Thread& thread) {
|
2020-09-25 21:44:43 -06:00
|
|
|
// We need to detach this thread in case it hasn't been joined
|
|
|
|
thread.detach();
|
2020-07-30 23:38:15 +02:00
|
|
|
thread.set_should_die();
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2019-12-30 15:11:25 -05:00
|
|
|
}
|
|
|
|
|
2020-09-27 08:53:35 -06: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 09:48:24 +02:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
auto parts = path.split('/');
|
|
|
|
if (arguments.is_empty()) {
|
|
|
|
arguments.append(parts.last());
|
2020-01-11 12:47:47 +01:00
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
RefPtr<Custody> cwd;
|
|
|
|
RefPtr<Custody> root;
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
2020-08-01 20:04:56 -06:00
|
|
|
if (auto parent = Process::from_pid(parent_pid)) {
|
2020-07-30 23:38:15 +02:00
|
|
|
cwd = parent->m_cwd;
|
|
|
|
root = parent->m_root_directory;
|
2019-12-29 00:54:10 -05:00
|
|
|
}
|
2019-12-09 19:12:38 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!cwd)
|
|
|
|
cwd = VFS::the().root_custody();
|
2020-01-30 21:46:45 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!root)
|
|
|
|
root = VFS::the().root_custody();
|
2020-01-30 21:55:49 +01:00
|
|
|
|
2020-09-10 09:46:24 -06:00
|
|
|
auto process = adopt(*new Process(first_thread, parts.take_last(), uid, gid, parent_pid, false, move(cwd), nullptr, tty));
|
2020-07-30 23:38:15 +02:00
|
|
|
process->m_fds.resize(m_max_open_file_descriptors);
|
|
|
|
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 21:46:45 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
error = process->exec(path, move(arguments), move(environment));
|
|
|
|
if (error != 0) {
|
|
|
|
dbg() << "Failed to exec " << path << ": " << error;
|
2020-09-27 08:53:35 -06:00
|
|
|
first_thread = nullptr;
|
2020-08-01 20:04:56 -06:00
|
|
|
return {};
|
2019-08-29 20:57:02 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
|
|
|
g_processes->prepend(process);
|
2020-08-01 20:04:56 -06:00
|
|
|
process->ref();
|
2019-08-29 20:57:02 +02:00
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
error = 0;
|
|
|
|
return process;
|
2018-10-24 09:48:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-27 08:53:35 -06:00
|
|
|
NonnullRefPtr<Process> Process::create_kernel_process(RefPtr<Thread>& first_thread, String&& name, void (*e)(), u32 affinity)
|
2019-08-12 19:33:24 +02:00
|
|
|
{
|
2020-09-10 09:46:24 -06:00
|
|
|
auto process = adopt(*new Process(first_thread, move(name), (uid_t)0, (gid_t)0, ProcessID(0), true));
|
2020-07-30 23:38:15 +02:00
|
|
|
first_thread->tss().eip = (FlatPtr)e;
|
2019-12-30 15:11:25 -05:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
if (process->pid() != 0) {
|
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
|
|
|
g_processes->prepend(process);
|
2020-08-01 20:04:56 -06:00
|
|
|
process->ref();
|
2019-12-30 15:11:25 -05:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
first_thread->set_affinity(affinity);
|
|
|
|
first_thread->set_state(Thread::State::Runnable);
|
|
|
|
return process;
|
|
|
|
}
|
2019-12-30 15:11:25 -05:00
|
|
|
|
2020-09-27 08:53:35 -06:00
|
|
|
Process::Process(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)
|
2020-07-30 23:38:15 +02:00
|
|
|
: m_name(move(name))
|
|
|
|
, m_pid(allocate_pid())
|
|
|
|
, m_euid(uid)
|
|
|
|
, m_egid(gid)
|
|
|
|
, m_uid(uid)
|
|
|
|
, m_gid(gid)
|
|
|
|
, m_suid(uid)
|
|
|
|
, m_sgid(gid)
|
2020-09-10 09:46:24 -06:00
|
|
|
, m_is_kernel_process(is_kernel_process)
|
2020-07-30 23:38:15 +02:00
|
|
|
, m_executable(move(executable))
|
|
|
|
, m_cwd(move(cwd))
|
|
|
|
, m_tty(tty)
|
|
|
|
, m_ppid(ppid)
|
|
|
|
{
|
|
|
|
#ifdef PROCESS_DEBUG
|
2020-08-27 00:58:09 +02:00
|
|
|
dbg() << "Created new process " << m_name << "(" << m_pid.value() << ")";
|
2020-07-30 23:38:15 +02:00
|
|
|
#endif
|
2019-12-30 15:11:25 -05:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
m_page_directory = PageDirectory::create_for_userspace(*this, fork_parent ? &fork_parent->page_directory().range_allocator() : nullptr);
|
|
|
|
#ifdef MM_DEBUG
|
2020-08-27 00:58:09 +02:00
|
|
|
dbg() << "Process " << pid().value() << " ctor: PD=" << m_page_directory.ptr() << " created";
|
2020-07-30 23:38:15 +02:00
|
|
|
#endif
|
2019-12-30 15:11:25 -05:00
|
|
|
|
2020-07-30 23:38:15 +02: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);
|
|
|
|
} else {
|
|
|
|
// NOTE: This non-forked code path is only taken when the kernel creates a process "manually" (at boot.)
|
2020-09-27 08:53:35 -06:00
|
|
|
first_thread = adopt(*new Thread(*this));
|
2020-09-25 21:44:43 -06:00
|
|
|
first_thread->detach();
|
2019-12-30 15:11:25 -05:00
|
|
|
}
|
2019-08-12 19:33:24 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
Process::~Process()
|
2019-12-09 19:12:38 +01:00
|
|
|
{
|
2020-09-27 08:53:35 -06:00
|
|
|
ASSERT(!m_next && !m_prev); // should have been reaped
|
|
|
|
ASSERT(thread_count() == 0); // all threads should have been finalized
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
2020-01-30 21:46:45 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
void Process::dump_regions()
|
|
|
|
{
|
|
|
|
klog() << "Process regions:";
|
|
|
|
klog() << "BEGIN END SIZE ACCESS NAME";
|
2020-08-22 16:48:27 +02:00
|
|
|
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
|
|
|
|
Vector<Region*> sorted_regions;
|
|
|
|
sorted_regions.ensure_capacity(m_regions.size());
|
|
|
|
for (auto& region : m_regions)
|
|
|
|
sorted_regions.append(®ion);
|
|
|
|
quick_sort(sorted_regions, [](auto& a, auto& b) {
|
|
|
|
return a->vaddr() < b->vaddr();
|
|
|
|
});
|
|
|
|
|
|
|
|
for (auto& sorted_region : sorted_regions) {
|
|
|
|
auto& region = *sorted_region;
|
2020-07-30 23:38:15 +02:00
|
|
|
klog() << String::format("%08x", region.vaddr().get()) << " -- " << String::format("%08x", region.vaddr().offset(region.size() - 1).get()) << " " << String::format("%08x", region.size()) << " " << (region.is_readable() ? 'R' : ' ') << (region.is_writable() ? 'W' : ' ') << (region.is_executable() ? 'X' : ' ') << (region.is_shared() ? 'S' : ' ') << (region.is_stack() ? 'T' : ' ') << (region.vmobject().is_purgeable() ? 'P' : ' ') << " " << region.name().characters();
|
2019-12-18 20:48:24 +01:00
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
MM.dump_kernel_regions();
|
2019-12-09 19:12:38 +01: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 21:12:13 +02:00
|
|
|
// Make sure the compiler doesn't "optimize away" this function:
|
|
|
|
extern void signal_trampoline_dummy(void);
|
2020-07-30 23:38:15 +02:00
|
|
|
void signal_trampoline_dummy(void)
|
2020-04-12 20:22:26 +02:00
|
|
|
{
|
2020-07-30 23:38:15 +02: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 22:14:37 +01:00
|
|
|
// necessary to preserve it here.
|
2020-07-30 23:38:15 +02: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));
|
|
|
|
}
|
2020-04-12 20:22:26 +02:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
extern "C" void asm_signal_trampoline(void);
|
|
|
|
extern "C" void asm_signal_trampoline_end(void);
|
2020-04-12 20:22:26 +02:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
void create_signal_trampolines()
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
// NOTE: We leak this region.
|
|
|
|
auto* trampoline_region = MM.allocate_user_accessible_kernel_region(PAGE_SIZE, "Signal trampolines", Region::Access::Read | Region::Access::Write | Region::Access::Execute, false).leak_ptr();
|
|
|
|
g_return_to_ring3_from_signal_trampoline = trampoline_region->vaddr();
|
2020-04-12 20:22:26 +02:00
|
|
|
|
2020-07-30 23:38:15 +02: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 20:22:26 +02:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
|
|
|
SmapDisabler disabler;
|
|
|
|
u8* code_ptr = (u8*)trampoline_region->vaddr().as_ptr();
|
|
|
|
memcpy(code_ptr, trampoline, trampoline_size);
|
2020-04-12 20:22:26 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
trampoline_region->set_writable(false);
|
|
|
|
trampoline_region->remap();
|
2020-04-12 20:22:26 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
void Process::crash(int signal, u32 eip, bool out_of_memory)
|
2019-12-09 19:12:38 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
ASSERT(!is_dead());
|
|
|
|
ASSERT(Process::current() == this);
|
|
|
|
|
|
|
|
if (out_of_memory) {
|
|
|
|
dbg() << "\033[31;1mOut of memory\033[m, killing: " << *this;
|
|
|
|
} else {
|
|
|
|
if (eip >= 0xc0000000 && g_kernel_symbols_available) {
|
|
|
|
auto* symbol = symbolicate_kernel_address(eip);
|
|
|
|
dbg() << "\033[31;1m" << String::format("%p", eip) << " " << (symbol ? demangle(symbol->name) : "(k?)") << " +" << (symbol ? eip - symbol->address : 0) << "\033[0m\n";
|
|
|
|
} else if (auto elf_bundle = this->elf_bundle()) {
|
|
|
|
dbg() << "\033[31;1m" << String::format("%p", eip) << " " << elf_bundle->elf_loader->symbolicate(eip) << "\033[0m\n";
|
|
|
|
} else {
|
|
|
|
dbg() << "\033[31;1m" << String::format("%p", eip) << " (?)\033[0m\n";
|
2019-12-29 13:16:53 +01:00
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
dump_backtrace();
|
2019-12-09 19:12:38 +01:00
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
m_termination_signal = signal;
|
|
|
|
dump_regions();
|
2020-09-10 09:46:24 -06:00
|
|
|
ASSERT(is_user_process());
|
2020-07-30 23:38:15 +02:00
|
|
|
die();
|
|
|
|
// We can not return from here, as there is nowhere
|
|
|
|
// to unwind to, so die right away.
|
|
|
|
Thread::current()->die_if_needed();
|
|
|
|
ASSERT_NOT_REACHED();
|
2019-12-09 19:12:38 +01:00
|
|
|
}
|
|
|
|
|
2020-08-08 17:32:34 +02:00
|
|
|
RefPtr<Process> Process::from_pid(ProcessID pid)
|
2018-10-26 09:54:29 +02:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
|
|
|
for (auto& process : *g_processes) {
|
2020-08-08 17:32:34 +02:00
|
|
|
process.pid();
|
2020-07-30 23:38:15 +02:00
|
|
|
if (process.pid() == pid)
|
|
|
|
return &process;
|
|
|
|
}
|
2020-08-01 20:04:56 -06:00
|
|
|
return {};
|
2018-10-26 09:54:29 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
RefPtr<FileDescription> Process::file_description(int fd) const
|
2020-04-26 15:59:47 +10:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
if (fd < 0)
|
|
|
|
return nullptr;
|
|
|
|
if (static_cast<size_t>(fd) < m_fds.size())
|
2020-07-30 23:50:31 +02:00
|
|
|
return m_fds[fd].description();
|
2020-07-30 23:38:15 +02:00
|
|
|
return nullptr;
|
2020-04-26 15:59:47 +10:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
int Process::fd_flags(int fd) const
|
2018-11-02 20:41:58 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
if (static_cast<size_t>(fd) < m_fds.size())
|
2020-07-30 23:50:31 +02:00
|
|
|
return m_fds[fd].flags();
|
2020-07-30 23:38:15 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2018-11-10 23:29:07 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
int Process::number_of_open_file_descriptors() const
|
2019-11-14 20:58:23 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
int count = 0;
|
|
|
|
for (auto& description : m_fds) {
|
|
|
|
if (description)
|
|
|
|
++count;
|
2020-01-10 19:15:01 +01:00
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
return count;
|
2019-04-09 01:10:00 +02:00
|
|
|
}
|
2019-04-22 18:44:45 +02:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
int Process::alloc_fd(int first_candidate_fd)
|
2019-07-22 20:01:11 +02:00
|
|
|
{
|
2020-07-30 23:38:15 +02: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 20:01:11 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
timeval kgettimeofday()
|
2019-07-19 21:08:26 +10:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
return g_timeofday;
|
2019-07-19 21:08:26 +10:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
void kgettimeofday(timeval& tv)
|
2019-07-19 17:58:12 +10:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
tv = kgettimeofday();
|
2019-07-19 17:58:12 +10:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
siginfo_t Process::reap(Process& process)
|
2019-08-02 23:18:47 +10:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
siginfo_t siginfo;
|
|
|
|
memset(&siginfo, 0, sizeof(siginfo));
|
|
|
|
siginfo.si_signo = SIGCHLD;
|
2020-08-08 17:32:34 +02:00
|
|
|
siginfo.si_pid = process.pid().value();
|
2020-07-30 23:38:15 +02:00
|
|
|
siginfo.si_uid = process.uid();
|
2020-05-28 21:12:13 +03:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
if (process.m_termination_signal) {
|
|
|
|
siginfo.si_status = process.m_termination_signal;
|
|
|
|
siginfo.si_code = CLD_KILLED;
|
|
|
|
} else {
|
|
|
|
siginfo.si_status = process.m_termination_status;
|
|
|
|
siginfo.si_code = CLD_EXITED;
|
2020-01-11 19:08:35 +03:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
ASSERT(g_processes_lock.is_locked());
|
2020-05-28 18:46:16 +03:00
|
|
|
|
2020-08-08 17:32:34 +02:00
|
|
|
if (!!process.ppid()) {
|
2020-08-01 20:04:56 -06:00
|
|
|
auto parent = Process::from_pid(process.ppid());
|
2020-07-30 23:38:15 +02:00
|
|
|
if (parent) {
|
|
|
|
parent->m_ticks_in_user_for_dead_children += process.m_ticks_in_user + process.m_ticks_in_user_for_dead_children;
|
|
|
|
parent->m_ticks_in_kernel_for_dead_children += process.m_ticks_in_kernel + process.m_ticks_in_kernel_for_dead_children;
|
2019-08-15 19:13:56 +03:00
|
|
|
}
|
2019-08-02 23:18:47 +10:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
#ifdef PROCESS_DEBUG
|
|
|
|
dbg() << "Reaping process " << process;
|
|
|
|
#endif
|
|
|
|
ASSERT(process.is_dead());
|
|
|
|
g_processes->remove(&process);
|
2020-08-01 20:04:56 -06:00
|
|
|
process.unref();
|
2020-07-30 23:38:15 +02:00
|
|
|
return siginfo;
|
2019-07-25 21:02:19 +02:00
|
|
|
}
|
2019-07-29 07:26:01 +02:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
Custody& Process::current_directory()
|
2019-10-13 11:41:55 -03:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!m_cwd)
|
|
|
|
m_cwd = VFS::the().root_custody();
|
|
|
|
return *m_cwd;
|
2019-10-13 11:41:55 -03:00
|
|
|
}
|
2019-11-02 19:34:06 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
KResultOr<String> Process::get_syscall_path_argument(const char* user_path, size_t path_length) const
|
2019-11-23 10:48:07 +03:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
if (path_length == 0)
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
if (path_length > PATH_MAX)
|
|
|
|
return KResult(-ENAMETOOLONG);
|
2020-09-11 21:11:07 -06:00
|
|
|
auto copied_string = copy_string_from_user(user_path, path_length);
|
|
|
|
if (copied_string.is_null())
|
2020-07-30 23:38:15 +02:00
|
|
|
return KResult(-EFAULT);
|
2020-09-11 21:11:07 -06:00
|
|
|
return copied_string;
|
2019-11-23 10:48:07 +03:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
KResultOr<String> Process::get_syscall_path_argument(const Syscall::StringArgument& path) const
|
2019-11-02 19:34:06 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
return get_syscall_path_argument(path.characters, path.length);
|
2019-11-02 19:34:06 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
void Process::finalize()
|
2020-03-13 01:17:14 +02:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
ASSERT(Thread::current() == g_finalizer);
|
|
|
|
#ifdef PROCESS_DEBUG
|
|
|
|
dbg() << "Finalizing process " << *this;
|
|
|
|
#endif
|
2020-03-13 01:17:14 +02:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
if (m_perf_event_buffer) {
|
|
|
|
auto description_or_error = VFS::the().open(String::format("perfcore.%d", m_pid), O_CREAT | O_EXCL, 0400, current_directory(), UidAndGid { m_uid, m_gid });
|
|
|
|
if (!description_or_error.is_error()) {
|
|
|
|
auto& description = description_or_error.value();
|
|
|
|
auto json = m_perf_event_buffer->to_json(m_pid, m_executable ? m_executable->absolute_path() : "");
|
2020-09-11 21:11:07 -06:00
|
|
|
auto json_buffer = UserOrKernelBuffer::for_kernel_buffer(json.data());
|
2020-10-10 23:46:40 +02:00
|
|
|
auto result = description->write(json_buffer, json.size());
|
|
|
|
if (result.is_error()) {
|
|
|
|
dbgln("Error while writing perfcore file: {}", result.error().error());
|
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
2020-03-13 01:17:14 +02:00
|
|
|
}
|
2019-11-02 19:34:06 +01:00
|
|
|
|
2020-07-30 23:38:15 +02: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;
|
2020-03-03 05:12:39 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
disown_all_shared_buffers();
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2020-08-08 17:32:34 +02:00
|
|
|
// FIXME: PID/TID BUG
|
2020-09-27 08:53:35 -06:00
|
|
|
if (auto parent_thread = Thread::from_tid(m_ppid.value())) {
|
2020-07-30 23:38:15 +02:00
|
|
|
if (parent_thread->m_signal_action_data[SIGCHLD].flags & SA_NOCLDWAIT) {
|
|
|
|
// NOTE: If the parent doesn't care about this process, let it go.
|
|
|
|
m_ppid = 0;
|
|
|
|
} else {
|
|
|
|
parent_thread->send_signal(SIGCHLD, this);
|
2019-11-02 19:34:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2020-08-01 14:41:54 -06:00
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
m_regions.clear();
|
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
m_dead = true;
|
2019-11-02 19:34:06 +01:00
|
|
|
}
|
2019-11-09 22:18:16 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
void Process::die()
|
2019-11-09 22:18:16 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02: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;
|
|
|
|
|
|
|
|
kill_all_threads();
|
2019-11-09 22:18:16 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
size_t Process::amount_dirty_private() const
|
2019-11-09 22:18:16 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
// FIXME: This gets a bit more complicated for Regions sharing the same underlying VMObject.
|
|
|
|
// The main issue I'm thinking of is when the VMObject has physical pages that none of the Regions are mapping.
|
|
|
|
// That's probably a situation that needs to be looked at in general.
|
|
|
|
size_t amount = 0;
|
2020-08-01 14:41:54 -06:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-07-30 23:38:15 +02:00
|
|
|
for (auto& region : m_regions) {
|
|
|
|
if (!region.is_shared())
|
|
|
|
amount += region.amount_dirty();
|
|
|
|
}
|
|
|
|
return amount;
|
2019-11-09 22:18:16 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
size_t Process::amount_clean_inode() const
|
2019-11-09 22:18:16 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
HashTable<const InodeVMObject*> vmobjects;
|
2020-08-01 14:41:54 -06:00
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
for (auto& region : m_regions) {
|
|
|
|
if (region.vmobject().is_inode())
|
|
|
|
vmobjects.set(&static_cast<const InodeVMObject&>(region.vmobject()));
|
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
size_t amount = 0;
|
|
|
|
for (auto& vmobject : vmobjects)
|
|
|
|
amount += vmobject->amount_clean();
|
|
|
|
return amount;
|
2019-11-09 22:18:16 +01:00
|
|
|
}
|
2019-11-28 20:59:11 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
size_t Process::amount_virtual() const
|
2019-11-28 20:59:11 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
size_t amount = 0;
|
2020-08-01 14:41:54 -06:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-07-30 23:38:15 +02:00
|
|
|
for (auto& region : m_regions) {
|
|
|
|
amount += region.size();
|
2019-11-28 20:59:11 +01:00
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
return amount;
|
|
|
|
}
|
2019-11-28 20:59:11 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
size_t Process::amount_resident() const
|
|
|
|
{
|
|
|
|
// FIXME: This will double count if multiple regions use the same physical page.
|
|
|
|
size_t amount = 0;
|
2020-08-01 14:41:54 -06:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-07-30 23:38:15 +02:00
|
|
|
for (auto& region : m_regions) {
|
|
|
|
amount += region.amount_resident();
|
2019-12-24 11:40:26 +11:00
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
return amount;
|
2019-11-28 20:59:11 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
size_t Process::amount_shared() const
|
2019-11-28 20:59:11 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
// FIXME: This will double count if multiple regions use the same physical page.
|
|
|
|
// FIXME: It doesn't work at the moment, since it relies on PhysicalPage ref counts,
|
|
|
|
// and each PhysicalPage is only reffed by its VMObject. This needs to be refactored
|
|
|
|
// so that every Region contributes +1 ref to each of its PhysicalPages.
|
|
|
|
size_t amount = 0;
|
2020-08-01 14:41:54 -06:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-07-30 23:38:15 +02:00
|
|
|
for (auto& region : m_regions) {
|
|
|
|
amount += region.amount_shared();
|
|
|
|
}
|
|
|
|
return amount;
|
2019-11-28 20:59:11 +01:00
|
|
|
}
|
2019-12-11 20:36:56 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
size_t Process::amount_purgeable_volatile() const
|
2019-12-11 20:36:56 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
size_t amount = 0;
|
2020-08-01 14:41:54 -06:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-07-30 23:38:15 +02:00
|
|
|
for (auto& region : m_regions) {
|
|
|
|
if (region.vmobject().is_purgeable() && static_cast<const PurgeableVMObject&>(region.vmobject()).is_volatile())
|
|
|
|
amount += region.amount_resident();
|
|
|
|
}
|
|
|
|
return amount;
|
2019-12-11 20:36:56 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
size_t Process::amount_purgeable_nonvolatile() const
|
2019-12-11 20:36:56 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
size_t amount = 0;
|
2020-08-01 14:41:54 -06:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-07-30 23:38:15 +02:00
|
|
|
for (auto& region : m_regions) {
|
|
|
|
if (region.vmobject().is_purgeable() && !static_cast<const PurgeableVMObject&>(region.vmobject()).is_volatile())
|
|
|
|
amount += region.amount_resident();
|
|
|
|
}
|
|
|
|
return amount;
|
2019-12-11 20:36:56 +01:00
|
|
|
}
|
2019-12-15 21:29:26 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
void Process::terminate_due_to_signal(u8 signal)
|
2019-12-22 21:29:47 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
ASSERT(signal < 32);
|
2020-08-02 12:08:22 -06:00
|
|
|
dbg() << "Terminating " << *this << " due to signal " << signal;
|
2020-07-30 23:38:15 +02:00
|
|
|
m_termination_status = 0;
|
|
|
|
m_termination_signal = signal;
|
|
|
|
die();
|
2019-12-22 21:29:47 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
KResult Process::send_signal(u8 signal, Process* sender)
|
2019-12-22 21:29:47 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
InterruptDisabler disabler;
|
2020-08-09 15:23:31 +02:00
|
|
|
Thread* receiver_thread;
|
|
|
|
// Try to send it to the "obvious" main thread:
|
|
|
|
receiver_thread = Thread::from_tid(m_pid.value());
|
|
|
|
// 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 23:38:15 +02:00
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
return KResult(-ESRCH);
|
|
|
|
}
|
2019-12-22 21:29:47 +01:00
|
|
|
|
2020-09-27 08:53:35 -06:00
|
|
|
RefPtr<Thread> Process::create_kernel_thread(void (*entry)(), u32 priority, const String& name, u32 affinity, bool joinable)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
|
|
|
ASSERT((priority >= THREAD_PRIORITY_MIN) && (priority <= THREAD_PRIORITY_MAX));
|
2020-04-26 02:40:24 -07:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
// FIXME: Do something with guard pages?
|
2020-04-26 02:40:24 -07:00
|
|
|
|
2020-09-27 08:53:35 -06:00
|
|
|
auto thread = adopt(*new Thread(*this));
|
2020-04-26 02:40:24 -07:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
thread->set_name(name);
|
|
|
|
thread->set_affinity(affinity);
|
|
|
|
thread->set_priority(priority);
|
2020-09-25 21:44:43 -06:00
|
|
|
if (!joinable)
|
|
|
|
thread->detach();
|
2020-04-26 02:40:24 -07:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
auto& tss = thread->tss();
|
|
|
|
tss.eip = (FlatPtr)entry;
|
2019-12-22 21:29:47 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
thread->set_state(Thread::State::Runnable);
|
|
|
|
return thread;
|
2019-12-22 21:29:47 +01:00
|
|
|
}
|
2019-12-30 19:23:13 +01:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
void Process::FileDescriptionAndFlags::clear()
|
2019-12-30 19:23:13 +01:00
|
|
|
{
|
2020-07-30 23:50:31 +02:00
|
|
|
m_description = nullptr;
|
|
|
|
m_flags = 0;
|
2019-12-30 19:23:13 +01:00
|
|
|
}
|
2019-12-30 20:10:00 +01:00
|
|
|
|
2020-07-30 23:50:31 +02:00
|
|
|
void Process::FileDescriptionAndFlags::set(NonnullRefPtr<FileDescription>&& description, u32 flags)
|
2019-12-30 20:10:00 +01:00
|
|
|
{
|
2020-07-30 23:50:31 +02:00
|
|
|
m_description = move(description);
|
|
|
|
m_flags = flags;
|
2019-12-30 20:10:00 +01:00
|
|
|
}
|
2020-01-10 23:14:04 +01:00
|
|
|
|
2020-08-01 20:04:56 -06:00
|
|
|
KBuffer Process::backtrace() const
|
2020-01-10 23:14:04 +01:00
|
|
|
{
|
2020-07-30 23:38:15 +02:00
|
|
|
KBufferBuilder builder;
|
|
|
|
for_each_thread([&](Thread& thread) {
|
2020-08-09 01:08:24 +02:00
|
|
|
builder.appendf("Thread %d (%s):\n", thread.tid().value(), thread.name().characters());
|
2020-08-01 20:04:56 -06:00
|
|
|
builder.append(thread.backtrace());
|
2020-07-30 23:38:15 +02:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
return builder.build();
|
2020-01-10 23:14:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Custody& Process::root_directory()
|
|
|
|
{
|
|
|
|
if (!m_root_directory)
|
|
|
|
m_root_directory = VFS::the().root_custody();
|
|
|
|
return *m_root_directory;
|
|
|
|
}
|
|
|
|
|
2020-01-12 21:42:01 +03:00
|
|
|
Custody& Process::root_directory_relative_to_global_root()
|
2020-01-10 23:48:44 +01:00
|
|
|
{
|
2020-01-12 21:42:01 +03: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 23:48:44 +01:00
|
|
|
}
|
|
|
|
|
2020-01-10 23:14:04 +01:00
|
|
|
void Process::set_root_directory(const Custody& root)
|
|
|
|
{
|
|
|
|
m_root_directory = root;
|
|
|
|
}
|
2020-01-11 20:48:43 +01:00
|
|
|
|
2020-01-19 16:25:38 +01:00
|
|
|
Region& Process::add_region(NonnullOwnPtr<Region> region)
|
|
|
|
{
|
|
|
|
auto* ptr = region.ptr();
|
2020-06-28 15:34:31 -06:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-01-19 16:25:38 +01:00
|
|
|
m_regions.append(move(region));
|
|
|
|
return *ptr;
|
|
|
|
}
|
Kernel: Add a basic implementation of unveil()
This syscall is a complement to pledge() and adds the same sort of
incremental relinquishing of capabilities for filesystem access.
The first call to unveil() will "drop a veil" on the process, and from
now on, only unveiled parts of the filesystem are visible to it.
Each call to unveil() specifies a path to either a directory or a file
along with permissions for that path. The permissions are a combination
of the following:
- r: Read access (like the "rpath" promise)
- w: Write access (like the "wpath" promise)
- x: Execute access
- c: Create/remove access (like the "cpath" promise)
Attempts to open a path that has not been unveiled with fail with
ENOENT. If the unveiled path lacks sufficient permissions, it will fail
with EACCES.
Like pledge(), subsequent calls to unveil() with the same path can only
remove permissions, not add them.
Once you call unveil(nullptr, nullptr), the veil is locked, and it's no
longer possible to unveil any more paths for the process, ever.
This concept comes from OpenBSD, and their implementation does various
things differently, I'm sure. This is just a first implementation for
SerenityOS, and we'll keep improving on it as we go. :^)
2020-01-20 22:12:04 +01:00
|
|
|
|
2020-02-16 02:01:42 +01:00
|
|
|
void Process::set_tty(TTY* tty)
|
|
|
|
{
|
|
|
|
m_tty = tty;
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:40:40 +01:00
|
|
|
OwnPtr<Process::ELFBundle> Process::elf_bundle() const
|
|
|
|
{
|
|
|
|
if (!m_executable)
|
|
|
|
return nullptr;
|
|
|
|
auto bundle = make<ELFBundle>();
|
2020-04-07 18:23:37 +03:00
|
|
|
if (!m_executable->inode().shared_vmobject()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-03-02 10:40:40 +01:00
|
|
|
ASSERT(m_executable->inode().shared_vmobject());
|
|
|
|
auto& vmobject = *m_executable->inode().shared_vmobject();
|
|
|
|
bundle->region = MM.allocate_kernel_region_with_vmobject(const_cast<SharedInodeVMObject&>(vmobject), vmobject.size(), "ELF bundle", Region::Access::Read);
|
|
|
|
if (!bundle->region)
|
|
|
|
return nullptr;
|
2020-04-17 14:40:38 +03:00
|
|
|
bundle->elf_loader = ELF::Loader::create(bundle->region->vaddr().as_ptr(), bundle->region->size());
|
2020-03-02 10:40:40 +01:00
|
|
|
return bundle;
|
|
|
|
}
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|