2021-06-23 00:24:25 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/JsonArraySerializer.h>
|
|
|
|
#include <AK/JsonObjectSerializer.h>
|
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
#include <Kernel/Arch/x86/InterruptDisabler.h>
|
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
|
|
|
#include <Kernel/KBufferBuilder.h>
|
|
|
|
#include <Kernel/ProcessExposed.h>
|
|
|
|
#include <Kernel/VM/AnonymousVMObject.h>
|
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
class ProcFSProcessStacks;
|
|
|
|
class ProcFSThreadStack final : public ProcFSProcessInformation {
|
|
|
|
public:
|
|
|
|
// Note: We pass const ProcFSProcessStacks& to enforce creation with this type of folder
|
2021-07-11 02:33:40 +03:00
|
|
|
static NonnullRefPtr<ProcFSThreadStack> create(const ProcFSProcessDirectory& process_folder, const ProcFSProcessStacks&, const Thread& thread)
|
2021-06-23 00:24:25 +03:00
|
|
|
{
|
|
|
|
return adopt_ref(*new (nothrow) ProcFSThreadStack(process_folder, thread));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-11 02:33:40 +03:00
|
|
|
explicit ProcFSThreadStack(const ProcFSProcessDirectory& process_folder, const Thread& thread)
|
2021-06-23 00:24:25 +03:00
|
|
|
: ProcFSProcessInformation(String::formatted("{}", thread.tid()), process_folder)
|
|
|
|
, m_associated_thread(thread)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual bool output(KBufferBuilder& builder) override
|
|
|
|
{
|
|
|
|
JsonArraySerializer array { builder };
|
|
|
|
bool show_kernel_addresses = Process::current()->is_superuser();
|
|
|
|
bool kernel_address_added = false;
|
|
|
|
for (auto address : Processor::capture_stack_trace(*m_associated_thread, 1024)) {
|
|
|
|
if (!show_kernel_addresses && !is_user_address(VirtualAddress { address })) {
|
|
|
|
if (kernel_address_added)
|
|
|
|
continue;
|
|
|
|
address = 0xdeadc0de;
|
|
|
|
kernel_address_added = true;
|
|
|
|
}
|
2021-06-30 12:31:12 +03:00
|
|
|
array.add(address);
|
2021-06-23 00:24:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
array.finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtr<Thread> m_associated_thread;
|
|
|
|
};
|
|
|
|
|
2021-07-11 02:33:40 +03:00
|
|
|
class ProcFSProcessStacks final : public ProcFSExposedDirectory {
|
2021-06-23 00:24:25 +03:00
|
|
|
// Note: This folder is special, because everything that is created here is dynamic!
|
|
|
|
// This means we don't register anything in the m_components Vector, and every inode
|
|
|
|
// is created in runtime when called to get it
|
|
|
|
// Every ProcFSThreadStack (that represents a thread stack) is created only as a temporary object
|
|
|
|
// therefore, we don't use m_components so when we are done with the ProcFSThreadStack object,
|
|
|
|
// It should be deleted (as soon as possible)
|
|
|
|
public:
|
|
|
|
virtual KResultOr<size_t> entries_count() const override;
|
2021-07-11 01:20:38 +03:00
|
|
|
virtual KResult traverse_as_directory(unsigned, Function<bool(FileSystem::DirectoryEntryView const&)>) const override;
|
2021-06-23 00:24:25 +03:00
|
|
|
virtual RefPtr<ProcFSExposedComponent> lookup(StringView name) override;
|
|
|
|
|
2021-07-11 02:33:40 +03:00
|
|
|
static NonnullRefPtr<ProcFSProcessStacks> create(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
{
|
|
|
|
auto folder = adopt_ref(*new (nothrow) ProcFSProcessStacks(parent_folder));
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void prepare_for_deletion() override
|
|
|
|
{
|
2021-07-11 02:33:40 +03:00
|
|
|
ProcFSExposedDirectory::prepare_for_deletion();
|
2021-06-23 00:24:25 +03:00
|
|
|
m_process_folder.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-11 02:33:40 +03:00
|
|
|
ProcFSProcessStacks(const ProcFSProcessDirectory& parent_folder)
|
|
|
|
: ProcFSExposedDirectory("stacks"sv, parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
, m_process_folder(parent_folder)
|
|
|
|
{
|
|
|
|
}
|
2021-07-11 02:33:40 +03:00
|
|
|
WeakPtr<ProcFSProcessDirectory> m_process_folder;
|
2021-06-23 00:24:25 +03:00
|
|
|
mutable Lock m_lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
KResultOr<size_t> ProcFSProcessStacks::entries_count() const
|
|
|
|
{
|
|
|
|
Locker locker(m_lock);
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_process_folder.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return KResult(EINVAL);
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return KResult(ESRCH);
|
|
|
|
return process->thread_count();
|
2021-06-23 00:24:25 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:20:38 +03:00
|
|
|
KResult ProcFSProcessStacks::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
|
2021-06-23 00:24:25 +03:00
|
|
|
{
|
|
|
|
Locker locker(m_lock);
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_process_folder.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return KResult(EINVAL);
|
2021-06-23 00:24:25 +03:00
|
|
|
callback({ ".", { fsid, component_index() }, 0 });
|
2021-07-01 19:18:38 +03:00
|
|
|
callback({ "..", { fsid, parent_folder->component_index() }, 0 });
|
2021-06-23 00:24:25 +03:00
|
|
|
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return KResult(ESRCH);
|
2021-06-23 00:24:25 +03:00
|
|
|
process->for_each_thread([&](const Thread& thread) {
|
|
|
|
int tid = thread.tid().value();
|
|
|
|
InodeIdentifier identifier = { fsid, thread.global_procfs_inode_index() };
|
|
|
|
callback({ String::number(tid), identifier, 0 });
|
|
|
|
});
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ProcFSExposedComponent> ProcFSProcessStacks::lookup(StringView name)
|
|
|
|
{
|
|
|
|
Locker locker(m_lock);
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_process_folder.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return nullptr;
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return nullptr;
|
2021-06-23 00:24:25 +03:00
|
|
|
RefPtr<ProcFSThreadStack> procfd_stack;
|
|
|
|
// FIXME: Try to exit the loop earlier
|
|
|
|
process->for_each_thread([&](const Thread& thread) {
|
|
|
|
int tid = thread.tid().value();
|
|
|
|
if (name == String::number(tid)) {
|
2021-07-01 19:18:38 +03:00
|
|
|
procfd_stack = ProcFSThreadStack::create(*parent_folder, *this, thread);
|
2021-06-23 00:24:25 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return procfd_stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ProcFSProcessFileDescriptions;
|
|
|
|
class ProcFSProcessFileDescription final : public ProcFSExposedLink {
|
|
|
|
public:
|
|
|
|
// Note: we pass const ProcFSProcessFileDescriptions& just to enforce creation of this in the correct folder.
|
|
|
|
static NonnullRefPtr<ProcFSProcessFileDescription> create(unsigned fd_number, const FileDescription& fd, InodeIndex preallocated_index, const ProcFSProcessFileDescriptions&)
|
|
|
|
{
|
|
|
|
return adopt_ref(*new (nothrow) ProcFSProcessFileDescription(fd_number, fd, preallocated_index));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit ProcFSProcessFileDescription(unsigned fd_number, const FileDescription& fd, InodeIndex preallocated_index)
|
|
|
|
: ProcFSExposedLink(String::formatted("{}", fd_number), preallocated_index)
|
|
|
|
, m_associated_file_description(fd)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual bool acquire_link(KBufferBuilder& builder) override
|
|
|
|
{
|
|
|
|
builder.append_bytes(m_associated_file_description->absolute_path().bytes());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtr<FileDescription> m_associated_file_description;
|
|
|
|
};
|
|
|
|
|
2021-07-11 02:33:40 +03:00
|
|
|
class ProcFSProcessFileDescriptions final : public ProcFSExposedDirectory {
|
2021-06-23 00:24:25 +03:00
|
|
|
// Note: This folder is special, because everything that is created here is dynamic!
|
|
|
|
// This means we don't register anything in the m_components Vector, and every inode
|
|
|
|
// is created in runtime when called to get it
|
|
|
|
// Every ProcFSProcessFileDescription (that represents a file descriptor) is created only as a temporary object
|
|
|
|
// therefore, we don't use m_components so when we are done with the ProcFSProcessFileDescription object,
|
|
|
|
// It should be deleted (as soon as possible)
|
|
|
|
public:
|
|
|
|
virtual KResultOr<size_t> entries_count() const override;
|
2021-07-11 01:20:38 +03:00
|
|
|
virtual KResult traverse_as_directory(unsigned, Function<bool(FileSystem::DirectoryEntryView const&)>) const override;
|
2021-06-23 00:24:25 +03:00
|
|
|
virtual RefPtr<ProcFSExposedComponent> lookup(StringView name) override;
|
|
|
|
|
2021-07-11 02:33:40 +03:00
|
|
|
static NonnullRefPtr<ProcFSProcessFileDescriptions> create(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
{
|
|
|
|
return adopt_ref(*new (nothrow) ProcFSProcessFileDescriptions(parent_folder));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void prepare_for_deletion() override
|
|
|
|
{
|
2021-07-11 02:33:40 +03:00
|
|
|
ProcFSExposedDirectory::prepare_for_deletion();
|
2021-06-23 00:24:25 +03:00
|
|
|
m_process_folder.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-11 02:33:40 +03:00
|
|
|
explicit ProcFSProcessFileDescriptions(const ProcFSProcessDirectory& parent_folder)
|
|
|
|
: ProcFSExposedDirectory("fd"sv, parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
, m_process_folder(parent_folder)
|
|
|
|
{
|
|
|
|
}
|
2021-07-11 02:33:40 +03:00
|
|
|
WeakPtr<ProcFSProcessDirectory> m_process_folder;
|
2021-06-23 00:24:25 +03:00
|
|
|
mutable Lock m_lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
KResultOr<size_t> ProcFSProcessFileDescriptions::entries_count() const
|
|
|
|
{
|
|
|
|
Locker locker(m_lock);
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_process_folder.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return KResult(EINVAL);
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return KResult(ESRCH);
|
|
|
|
return process->fds().open_count();
|
2021-06-23 00:24:25 +03:00
|
|
|
}
|
2021-07-11 01:20:38 +03:00
|
|
|
KResult ProcFSProcessFileDescriptions::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
|
2021-06-23 00:24:25 +03:00
|
|
|
{
|
|
|
|
Locker locker(m_lock);
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_process_folder.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return KResult(EINVAL);
|
2021-06-23 00:24:25 +03:00
|
|
|
callback({ ".", { fsid, component_index() }, 0 });
|
2021-07-01 19:18:38 +03:00
|
|
|
callback({ "..", { fsid, parent_folder->component_index() }, 0 });
|
2021-06-23 00:24:25 +03:00
|
|
|
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return KResult(ESRCH);
|
2021-06-23 00:24:25 +03:00
|
|
|
size_t count = 0;
|
2021-07-01 19:18:38 +03:00
|
|
|
process->fds().enumerate([&](auto& file_description_metadata) {
|
2021-06-23 00:24:25 +03:00
|
|
|
if (!file_description_metadata.is_valid()) {
|
|
|
|
count++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
InodeIdentifier identifier = { fsid, file_description_metadata.global_procfs_inode_index() };
|
|
|
|
callback({ String::number(count), identifier, 0 });
|
|
|
|
count++;
|
|
|
|
});
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
RefPtr<ProcFSExposedComponent> ProcFSProcessFileDescriptions::lookup(StringView name)
|
|
|
|
{
|
|
|
|
Locker locker(m_lock);
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_process_folder.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return nullptr;
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return nullptr;
|
2021-06-23 00:24:25 +03:00
|
|
|
RefPtr<ProcFSProcessFileDescription> procfd_fd;
|
|
|
|
// FIXME: Try to exit the loop earlier
|
|
|
|
size_t count = 0;
|
2021-07-01 19:18:38 +03:00
|
|
|
process->fds().enumerate([&](auto& file_description_metadata) {
|
2021-06-23 00:24:25 +03:00
|
|
|
if (!file_description_metadata.is_valid()) {
|
|
|
|
count++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (name == String::number(count)) {
|
|
|
|
procfd_fd = ProcFSProcessFileDescription::create(count, *file_description_metadata.description(), file_description_metadata.global_procfs_inode_index(), *this);
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
});
|
|
|
|
return procfd_fd;
|
|
|
|
}
|
|
|
|
|
2021-07-07 23:31:40 +03:00
|
|
|
class ProcFSProcessPledge final : public ProcFSProcessInformation {
|
|
|
|
public:
|
2021-07-11 02:33:40 +03:00
|
|
|
static NonnullRefPtr<ProcFSProcessPledge> create(const ProcFSProcessDirectory& parent_folder)
|
2021-07-07 23:31:40 +03:00
|
|
|
{
|
|
|
|
return adopt_ref(*new (nothrow) ProcFSProcessPledge(parent_folder));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-11 02:33:40 +03:00
|
|
|
explicit ProcFSProcessPledge(const ProcFSProcessDirectory& parent_folder)
|
2021-07-07 23:31:40 +03:00
|
|
|
: ProcFSProcessInformation("pledge"sv, parent_folder)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual bool output(KBufferBuilder& builder) override
|
|
|
|
{
|
|
|
|
auto parent_folder = m_parent_folder.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return false;
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return false;
|
2021-07-07 23:31:40 +03:00
|
|
|
JsonObjectSerializer obj { builder };
|
|
|
|
#define __ENUMERATE_PLEDGE_PROMISE(x) \
|
|
|
|
if (process->has_promised(Pledge::x)) { \
|
|
|
|
if (!builder.is_empty()) \
|
|
|
|
builder.append(' '); \
|
|
|
|
builder.append(#x); \
|
|
|
|
}
|
|
|
|
if (process->has_promises()) {
|
|
|
|
StringBuilder builder;
|
|
|
|
ENUMERATE_PLEDGE_PROMISES
|
|
|
|
obj.add("promises", builder.build());
|
|
|
|
}
|
|
|
|
#undef __ENUMERATE_PLEDGE_PROMISE
|
|
|
|
obj.finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-23 00:24:25 +03:00
|
|
|
class ProcFSProcessUnveil final : public ProcFSProcessInformation {
|
|
|
|
public:
|
2021-07-11 02:33:40 +03:00
|
|
|
static NonnullRefPtr<ProcFSProcessUnveil> create(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
{
|
|
|
|
return adopt_ref(*new (nothrow) ProcFSProcessUnveil(parent_folder));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-11 02:33:40 +03:00
|
|
|
explicit ProcFSProcessUnveil(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
: ProcFSProcessInformation("unveil"sv, parent_folder)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual bool output(KBufferBuilder& builder) override
|
|
|
|
{
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_parent_folder.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return false;
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return false;
|
2021-06-23 00:24:25 +03:00
|
|
|
JsonArraySerializer array { builder };
|
2021-07-10 00:24:47 +03:00
|
|
|
for (auto& unveiled_path : process->unveiled_paths()) {
|
2021-06-23 00:24:25 +03:00
|
|
|
if (!unveiled_path.was_explicitly_unveiled())
|
|
|
|
continue;
|
|
|
|
auto obj = array.add_object();
|
|
|
|
obj.add("path", unveiled_path.path());
|
|
|
|
StringBuilder permissions_builder;
|
|
|
|
if (unveiled_path.permissions() & UnveilAccess::Read)
|
|
|
|
permissions_builder.append('r');
|
|
|
|
if (unveiled_path.permissions() & UnveilAccess::Write)
|
|
|
|
permissions_builder.append('w');
|
|
|
|
if (unveiled_path.permissions() & UnveilAccess::Execute)
|
|
|
|
permissions_builder.append('x');
|
|
|
|
if (unveiled_path.permissions() & UnveilAccess::CreateOrRemove)
|
|
|
|
permissions_builder.append('c');
|
|
|
|
if (unveiled_path.permissions() & UnveilAccess::Browse)
|
|
|
|
permissions_builder.append('b');
|
|
|
|
obj.add("permissions", permissions_builder.to_string());
|
|
|
|
}
|
|
|
|
array.finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProcFSProcessPerformanceEvents final : public ProcFSProcessInformation {
|
|
|
|
public:
|
2021-07-11 02:33:40 +03:00
|
|
|
static NonnullRefPtr<ProcFSProcessPerformanceEvents> create(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
{
|
|
|
|
return adopt_ref(*new (nothrow) ProcFSProcessPerformanceEvents(parent_folder));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-11 02:33:40 +03:00
|
|
|
explicit ProcFSProcessPerformanceEvents(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
: ProcFSProcessInformation("perf_events"sv, parent_folder)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual bool output(KBufferBuilder& builder) override
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_parent_folder.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return false;
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return false;
|
2021-07-01 19:18:38 +03:00
|
|
|
if (!process->perf_events()) {
|
|
|
|
dbgln("ProcFS: No perf events for {}", process->pid());
|
2021-06-23 00:24:25 +03:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-01 19:18:38 +03:00
|
|
|
return process->perf_events()->to_json(builder);
|
2021-06-23 00:24:25 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProcFSProcessOverallFileDescriptions final : public ProcFSProcessInformation {
|
|
|
|
public:
|
2021-07-11 02:33:40 +03:00
|
|
|
static NonnullRefPtr<ProcFSProcessOverallFileDescriptions> create(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
{
|
|
|
|
return adopt_ref(*new (nothrow) ProcFSProcessOverallFileDescriptions(parent_folder));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-11 02:33:40 +03:00
|
|
|
explicit ProcFSProcessOverallFileDescriptions(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
: ProcFSProcessInformation("fds"sv, parent_folder)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual bool output(KBufferBuilder& builder) override
|
|
|
|
{
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_parent_folder.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return false;
|
2021-06-23 00:24:25 +03:00
|
|
|
JsonArraySerializer array { builder };
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return false;
|
2021-06-23 00:24:25 +03:00
|
|
|
if (process->fds().open_count() == 0) {
|
|
|
|
array.finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t count = 0;
|
|
|
|
process->fds().enumerate([&](auto& file_description_metadata) {
|
|
|
|
if (!file_description_metadata.is_valid()) {
|
|
|
|
count++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool cloexec = file_description_metadata.flags() & FD_CLOEXEC;
|
|
|
|
RefPtr<FileDescription> description = file_description_metadata.description();
|
|
|
|
auto description_object = array.add_object();
|
|
|
|
description_object.add("fd", count);
|
|
|
|
description_object.add("absolute_path", description->absolute_path());
|
|
|
|
description_object.add("seekable", description->file().is_seekable());
|
|
|
|
description_object.add("class", description->file().class_name());
|
|
|
|
description_object.add("offset", description->offset());
|
|
|
|
description_object.add("cloexec", cloexec);
|
|
|
|
description_object.add("blocking", description->is_blocking());
|
|
|
|
description_object.add("can_read", description->can_read());
|
|
|
|
description_object.add("can_write", description->can_write());
|
|
|
|
count++;
|
|
|
|
});
|
|
|
|
|
|
|
|
array.finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProcFSProcessRoot final : public ProcFSExposedLink {
|
|
|
|
public:
|
2021-07-11 02:33:40 +03:00
|
|
|
static NonnullRefPtr<ProcFSProcessRoot> create(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
{
|
|
|
|
return adopt_ref(*new (nothrow) ProcFSProcessRoot(parent_folder));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-11 02:33:40 +03:00
|
|
|
explicit ProcFSProcessRoot(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
: ProcFSExposedLink("root"sv)
|
|
|
|
, m_parent_process_directory(parent_folder)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual bool acquire_link(KBufferBuilder& builder) override
|
|
|
|
{
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_parent_process_directory.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return false;
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return false;
|
|
|
|
builder.append_bytes(process->root_directory_relative_to_global_root().absolute_path().to_byte_buffer());
|
2021-06-23 00:24:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
2021-07-11 02:33:40 +03:00
|
|
|
WeakPtr<ProcFSProcessDirectory> m_parent_process_directory;
|
2021-06-23 00:24:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class ProcFSProcessVirtualMemory final : public ProcFSProcessInformation {
|
|
|
|
public:
|
2021-07-11 02:33:40 +03:00
|
|
|
static NonnullRefPtr<ProcFSProcessRoot> create(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
{
|
|
|
|
return adopt_ref(*new (nothrow) ProcFSProcessVirtualMemory(parent_folder));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-11 02:33:40 +03:00
|
|
|
explicit ProcFSProcessVirtualMemory(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
: ProcFSProcessInformation("vm"sv, parent_folder)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual bool output(KBufferBuilder& builder) override
|
|
|
|
{
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_parent_folder.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return false;
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return false;
|
2021-06-23 00:24:25 +03:00
|
|
|
JsonArraySerializer array { builder };
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(process->space().get_lock());
|
|
|
|
for (auto& region : process->space().regions()) {
|
|
|
|
if (!region->is_user() && !Process::current()->is_superuser())
|
|
|
|
continue;
|
|
|
|
auto region_object = array.add_object();
|
|
|
|
region_object.add("readable", region->is_readable());
|
|
|
|
region_object.add("writable", region->is_writable());
|
|
|
|
region_object.add("executable", region->is_executable());
|
|
|
|
region_object.add("stack", region->is_stack());
|
|
|
|
region_object.add("shared", region->is_shared());
|
|
|
|
region_object.add("syscall", region->is_syscall_region());
|
|
|
|
region_object.add("purgeable", region->vmobject().is_anonymous());
|
|
|
|
if (region->vmobject().is_anonymous()) {
|
|
|
|
region_object.add("volatile", static_cast<const AnonymousVMObject&>(region->vmobject()).is_any_volatile());
|
|
|
|
}
|
|
|
|
region_object.add("cacheable", region->is_cacheable());
|
|
|
|
region_object.add("address", region->vaddr().get());
|
|
|
|
region_object.add("size", region->size());
|
|
|
|
region_object.add("amount_resident", region->amount_resident());
|
|
|
|
region_object.add("amount_dirty", region->amount_dirty());
|
|
|
|
region_object.add("cow_pages", region->cow_pages());
|
|
|
|
region_object.add("name", region->name());
|
|
|
|
region_object.add("vmobject", region->vmobject().class_name());
|
|
|
|
|
|
|
|
StringBuilder pagemap_builder;
|
|
|
|
for (size_t i = 0; i < region->page_count(); ++i) {
|
|
|
|
auto* page = region->physical_page(i);
|
|
|
|
if (!page)
|
|
|
|
pagemap_builder.append('N');
|
|
|
|
else if (page->is_shared_zero_page() || page->is_lazy_committed_page())
|
|
|
|
pagemap_builder.append('Z');
|
|
|
|
else
|
|
|
|
pagemap_builder.append('P');
|
|
|
|
}
|
|
|
|
region_object.add("pagemap", pagemap_builder.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
array.finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProcFSProcessCurrentWorkDirectory final : public ProcFSExposedLink {
|
|
|
|
public:
|
2021-07-11 02:33:40 +03:00
|
|
|
static NonnullRefPtr<ProcFSProcessCurrentWorkDirectory> create(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
{
|
|
|
|
return adopt_ref(*new (nothrow) ProcFSProcessCurrentWorkDirectory(parent_folder));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-11 02:33:40 +03:00
|
|
|
explicit ProcFSProcessCurrentWorkDirectory(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
: ProcFSExposedLink("cwd"sv)
|
|
|
|
, m_parent_process_directory(parent_folder)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual bool acquire_link(KBufferBuilder& builder) override
|
|
|
|
{
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_parent_process_directory.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return false;
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return false;
|
|
|
|
builder.append_bytes(process->current_directory().absolute_path().bytes());
|
2021-06-23 00:24:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-11 02:33:40 +03:00
|
|
|
WeakPtr<ProcFSProcessDirectory> m_parent_process_directory;
|
2021-06-23 00:24:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class ProcFSProcessBinary final : public ProcFSExposedLink {
|
|
|
|
public:
|
2021-07-11 02:33:40 +03:00
|
|
|
static NonnullRefPtr<ProcFSProcessBinary> create(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
{
|
|
|
|
return adopt_ref(*new (nothrow) ProcFSProcessBinary(parent_folder));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual mode_t required_mode() const override
|
|
|
|
{
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_parent_process_directory.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return false;
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return false;
|
|
|
|
if (!process->executable())
|
2021-06-23 00:24:25 +03:00
|
|
|
return 0;
|
|
|
|
return ProcFSExposedComponent::required_mode();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-11 02:33:40 +03:00
|
|
|
explicit ProcFSProcessBinary(const ProcFSProcessDirectory& parent_folder)
|
2021-06-23 00:24:25 +03:00
|
|
|
: ProcFSExposedLink("exe"sv)
|
|
|
|
, m_parent_process_directory(parent_folder)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual bool acquire_link(KBufferBuilder& builder) override
|
|
|
|
{
|
2021-07-01 19:18:38 +03:00
|
|
|
auto parent_folder = m_parent_process_directory.strong_ref();
|
|
|
|
if (parent_folder.is_null())
|
|
|
|
return false;
|
2021-07-10 00:24:47 +03:00
|
|
|
auto process = parent_folder->associated_process();
|
|
|
|
if (process.is_null())
|
|
|
|
return false;
|
|
|
|
auto* custody = process->executable();
|
2021-06-23 00:24:25 +03:00
|
|
|
if (!custody)
|
|
|
|
return false;
|
|
|
|
builder.append(custody->absolute_path().bytes());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-11 02:33:40 +03:00
|
|
|
WeakPtr<ProcFSProcessDirectory> m_parent_process_directory;
|
2021-06-23 00:24:25 +03:00
|
|
|
};
|
|
|
|
|
2021-07-11 02:33:40 +03:00
|
|
|
void ProcFSProcessDirectory::on_attach()
|
2021-07-02 11:03:40 +03:00
|
|
|
{
|
|
|
|
VERIFY(m_components.size() == 0);
|
2021-07-07 23:31:40 +03:00
|
|
|
m_components.append(ProcFSProcessPledge::create(*this));
|
2021-07-02 11:03:40 +03:00
|
|
|
m_components.append(ProcFSProcessUnveil::create(*this));
|
|
|
|
m_components.append(ProcFSProcessPerformanceEvents::create(*this));
|
|
|
|
m_components.append(ProcFSProcessFileDescriptions::create(*this));
|
|
|
|
m_components.append(ProcFSProcessOverallFileDescriptions::create(*this));
|
|
|
|
m_components.append(ProcFSProcessRoot::create(*this));
|
|
|
|
m_components.append(ProcFSProcessVirtualMemory::create(*this));
|
|
|
|
m_components.append(ProcFSProcessCurrentWorkDirectory::create(*this));
|
|
|
|
m_components.append(ProcFSProcessBinary::create(*this));
|
|
|
|
m_components.append(ProcFSProcessStacks::create(*this));
|
|
|
|
}
|
|
|
|
|
2021-07-11 02:33:40 +03:00
|
|
|
RefPtr<ProcFSExposedComponent> ProcFSProcessDirectory::lookup(StringView name)
|
2021-07-02 11:03:40 +03:00
|
|
|
{
|
|
|
|
// Note: we need to allocate all sub components when doing a lookup, because
|
|
|
|
// for some reason, the caller may not call ProcFSInode::attach method before calling this.
|
|
|
|
if (m_components.size() == 0)
|
|
|
|
on_attach();
|
2021-07-11 02:33:40 +03:00
|
|
|
return ProcFSExposedDirectory::lookup(name);
|
2021-07-02 11:03:40 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 02:33:40 +03:00
|
|
|
KResult ProcFSProcessDirectory::refresh_data(FileDescription&) const
|
2021-07-02 11:03:40 +03:00
|
|
|
{
|
|
|
|
if (m_components.size() != 0)
|
|
|
|
return KSuccess;
|
2021-07-11 02:33:40 +03:00
|
|
|
const_cast<ProcFSProcessDirectory&>(*this).on_attach();
|
2021-07-02 11:03:40 +03:00
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2021-07-11 02:33:40 +03:00
|
|
|
NonnullRefPtr<ProcFSProcessDirectory> ProcFSProcessDirectory::create(const Process& process)
|
2021-06-23 00:24:25 +03:00
|
|
|
{
|
2021-07-11 02:33:40 +03:00
|
|
|
return adopt_ref_if_nonnull(new (nothrow) ProcFSProcessDirectory(process)).release_nonnull();
|
2021-06-23 00:24:25 +03:00
|
|
|
}
|
|
|
|
|
2021-07-10 00:24:47 +03:00
|
|
|
void ProcFSProcessDirectory::prepare_for_deletion()
|
|
|
|
{
|
|
|
|
ProcFSExposedDirectory::prepare_for_deletion();
|
|
|
|
m_associated_process.clear();
|
|
|
|
}
|
|
|
|
|
2021-07-11 02:33:40 +03:00
|
|
|
ProcFSProcessDirectory::ProcFSProcessDirectory(const Process& process)
|
2021-07-11 02:40:26 +03:00
|
|
|
: ProcFSExposedDirectory(String::formatted("{:d}", process.pid().value()), ProcFSComponentRegistry::the().root_folder())
|
2021-06-23 00:24:25 +03:00
|
|
|
, m_associated_process(process)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|