14using namespace MaxOS::processes;
15using namespace MaxOS::processes::resources;
16using namespace MaxOS::memory;
17using namespace MaxOS::common;
18using namespace MaxOS::KPI::processes;
20ProcessResource::ProcessResource(
string const& name,
size_t flags,
resource_type_t type)
49 if(!
process && flags != (
size_t)ProcessFlags::READ_STATS)
52 switch ((ProcessFlags)flags) {
54 case ProcessFlags::READ_STATS: {
57 if(size <
sizeof(process_stats_t))
61 process_stats_t stats = {};
67 stats.memory_usage = 0;
73 memcpy(buffer, &stats,
sizeof(process_stats_t));
74 return sizeof(process_stats_t);
78 case ProcessFlags::READ_ENVIRONMENT: {
81 if(size <
sizeof(process_environment_t))
85 process_environment_t info;
89 info.env_var_count = 0;
91 info.executable_path = 0;
100 memcpy(buffer, &info,
sizeof(process_environment_t));
101 return sizeof(process_environment_t);
106 case ProcessFlags::READ_HANDLES: {
110 if(size < size_needed)
116 handle_info_t handle_info;
117 handle_info.handle = resource.first;
118 handle_info.type = resource.second->type();
120 memcpy((
void*)handle_info.name, resource.second->name().c_str(), resource.second->name().length() + 1);
123 memcpy(buffer, &handle_info,
sizeof(handle_info_t));
124 buffer = (
void*)((uintptr_t)buffer +
sizeof(handle_info_t));
127 return (
int)size_needed;
151 switch ((ProcessFlags)flags) {
153 case ProcessFlags::WRITE_CHANGE_DIRECTORY: {
156 string new_directory = string((
const char*)buffer);
161 case ProcessFlags::WRITE_KILL: {
176ProcessResource::~ProcessResource() =
default;
179ThreadResource::ThreadResource(
string const& name,
size_t flags,
resource_type_t type)
207 if(!
thread && flags != (
size_t)ThreadFlags::READ_STATS)
210 switch ((ThreadFlags)flags) {
212 case ThreadFlags::READ_STATS: {
215 if(size <
sizeof(thread_stats_t))
219 thread_stats_t stats = {};
231 memcpy(buffer, &stats,
sizeof(thread_stats_t));
232 return sizeof(thread_stats_t);
258 switch ((ThreadFlags)flags) {
260 case ThreadFlags::WRITE_SLEEP_TIME: {
263 uint64_t sleep_time = *((uint64_t*)buffer);
268 case ThreadFlags::WRITE_KILL: {
277 case ThreadFlags::WRITE_YIELD: {
282 case ThreadFlags::WRITE_ENABLE_PORT_IO: {
283 uint64_t port = *((uint64_t*)buffer);
288 case ThreadFlags::WRITE_DISABLE_PORT_IO: {
289 uint64_t port = *((uint64_t*)buffer);
300ThreadResource::~ThreadResource() =
default;
302ThreadResourceRegistry::ThreadResourceRegistry()
325 if(resource !=
nullptr)
329 uint64_t tid = name.to_int();
334 if(resource !=
nullptr)
338 auto thread_resource =
new ThreadResource(
string(thread -> tid), 0, resource_type_t::PROCESS);
339 thread_resource->thread = thread;
342 return thread_resource;
345ThreadResourceRegistry::~ThreadResourceRegistry() =
default;
347ProcessResourceRegistry::ProcessResourceRegistry()
365 auto command = (execute_command_t*)data;
370 auto exec_buffer = buffer_t(command->file_data_size);
371 exec_buffer.copy_from(command->file_data, command->file_data_size);
374 uint64_t argc = command->args_count;
375 size_t arg_size = argc *
sizeof(
void*);
377 for (
int i = 0; i < argc; i++) {
380 size_t len = strlen(((
char**)command->args)[i]) + 1;
382 memcpy((
void*) ((
char**)argv)[i], (
void*) ((
char**)command->args)[i], len);
386 auto elf =
new ELF64((uintptr_t)exec_buffer.raw());
387 if (!elf->is_valid())
391 auto process =
new Process(name, argv, argc, elf);
395 auto resource =
new ProcessResource(name, flags, resource_type_t::PROCESS);
396 resource->process = process;
421 if(resource !=
nullptr)
425 uint64_t pid = name.to_int();
430 if(resource !=
nullptr)
434 auto process_resource =
new ProcessResource(
string(process -> pid()), 0, resource_type_t::PROCESS);
435 process_resource->process = process;
438 return process_resource;
441ProcessResourceRegistry::~ProcessResourceRegistry() =
default;
static void * malloc(size_t size)
Allocates a block of memory in the current USERSPACE heap.
static void * kmalloc(size_t size)
Allocates a block of memory in the KERNEL space.
Handles the loading and parsing of 64-bit ELF files.
static Process * get_process(uint64_t pid)
Gets a process on the currently executing core by its PID.
static uint64_t remove_process(Process *process)
Removes a process from the scheduler if the process has no threads, if it does then the threads are s...
static GlobalScheduler * system_scheduler()
Gets the system scheduler.
static Process * current_process()
Gets the process on the currently executing core.
static Thread * get_thread(uint64_t tid)
Gets a thread on the currently executing core by its TID.
static Thread * current_thread()
Gets the thread on the currently executing core.
A process that can be scheduled by the Scheduler, wraps & manages threads as well as its own address ...
uint64_t pid() const
Gets the pid of the process.
uint64_t total_ticks()
Gets the total ticks of the threads in the process (not this does not include any past killed threads...
resources::ResourceManager resource_manager
The manger for resources used by this process.
common::Vector< Thread * > threads()
Gets the threads of the process.
string working_directory
The working directory of the process.
void enable_port(uint64_t port)
Sets bits in the threads TSS to allow a port for I/O.
uint64_t parent_pid
The parent process ID.
uint64_t tid
The thread ID.
void yield()
Pass execution to the next ready thread.
volatile size_t ticks
The number of ticks the thread has run for.
void disable_port(uint64_t port)
Sets bits in the threads TSS to disallow a port for I/O.
volatile thread_state_t thread_state
The current state of the thread.
volatile size_t wakeup_time
The time at which the thread should wake up (if sleeping)
void sleep(size_t milliseconds)
Sleeps the thread for a certain amount of time.
virtual Resource * get_resource(const string &name)
Gets a resource from the registry by name.
bool register_resource(Resource *resource)
Registers a resource in the registry.
Resource * get_resource(const string &name) final
The.
Resource * create_resource(const string &name, size_t flags, uintptr_t data) final
Opens an executable.
A wrapper for a Process which exposes Process operations as Resource operations.
int write(const void *buffer, size_t size, size_t flags) final
Writes data to the process resource.
Process * process
The process that this resource handles & exposes.
uint64_t exit_code
The exit code of the process once it has exited.
void close(size_t flags) final
Closes the process resource, killing the process and removing it from the scheduler.
int read(void *buffer, size_t size, size_t flags) final
Reads data from the process resource.
common::Map< uint64_t, Resource * > resources()
Get the resources currently open.
A resource registry for a specific resource type.
Resource * create_resource(const string &name, size_t flags, uintptr_t data) override
Create a new resource of the specific type.
Resource * create_resource(const string &name, size_t flags, uintptr_t data) final
td
Resource * get_resource(const string &name) final
Gets a resource from the registry by name.
A wrapper for a Thread which exposes Thread operations as Resource operations.
void close(size_t flags) final
Closes the thread resource, marking the thread to be removed by the scheduler.
uint64_t exit_code
The exit code of the thread once it has exited.
int read(void *buffer, size_t size, size_t flags) final
Reads data from the thread resource.
int write(const void *buffer, size_t size, size_t flags) final
Writes data to the thread resource.
Thread * thread
The thread that this resource handles & exposes.
KPI::Resource Resource
Alias to make the libsyscore Resource accessible here.
KPI::ResourceType resource_type_t
Alias to make the libsyscore ResourceType accessible here.
Defines a GlobalScheduler and Scheduler for managing processes and threads.
Defines scheduler resource classes for wrapping Processe and Thread objects as Resources in the Globa...