Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::processes::resources::ProcessResource Class Referencefinal

A wrapper for a Process which exposes Process operations as Resource operations. More...

#include <scheduler_resources.h>

Inheritance diagram for MaxOS::processes::resources::ProcessResource:

Public Member Functions

 ProcessResource (const string &name, size_t flags, resource_type_t type)
 
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.
 
int write (const void *buffer, size_t size, size_t flags) final
 Writes data to the process resource.
 

Public Attributes

Process * process
 The process that this resource handles & exposes.
 
uint64_t exit_code
 The exit code of the process once it has exited.
 

Detailed Description

A wrapper for a Process which exposes Process operations as Resource operations.

See also
Process
Resource
Note
The process pointer will be freed by scheduler during close()

Definition at line 34 of file scheduler_resources.h.

Constructor & Destructor Documentation

◆ ProcessResource()

ProcessResource::ProcessResource ( const string &  name,
size_t  flags,
resource_type_t  type 
)

Definition at line 20 of file scheduler_resources.cpp.

21: Resource(name, flags, type)
22{
23
24}
KPI::Resource Resource
Alias to make the libsyscore Resource accessible here.
Definition resource.h:26

Member Function Documentation

◆ close()

void ProcessResource::close ( size_t  flags)
final

Closes the process resource, killing the process and removing it from the scheduler.

Parameters
flagsN/A

Definition at line 31 of file scheduler_resources.cpp.

31 {
32
33 if(process)
35}
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...
Process * process
The process that this resource handles & exposes.

References process, and MaxOS::processes::GlobalScheduler::remove_process().

◆ read()

int ProcessResource::read ( void *  buffer,
size_t  size,
size_t  flags 
)
final

Reads data from the process resource.

Parameters
bufferThe buffer to read into
sizeThe number of bytes to read
flagsThe flags to pass to the reading
Returns
The number of bytes successfully read or -1 on error

Definition at line 46 of file scheduler_resources.cpp.

46 {
47
48 // Process not found (allow reading stats only to get exit code)
49 if(!process && flags != (size_t)ProcessFlags::READ_STATS)
50 return -1;
51
52 switch ((ProcessFlags)flags) {
53
54 case ProcessFlags::READ_STATS: {
55
56 // Check bounds
57 if(size < sizeof(process_stats_t))
58 return -1;
59
60 // Get the info
61 process_stats_t stats = {};
62 if(process) {
63 stats.pid = process->pid();
64 stats.parent_pid = 0; //TODO: store parent pid
65 stats.thread_count = process->threads().size();
66 stats.total_ticks = process->total_ticks();
67 stats.memory_usage = 0; //process->memory_manager->memory_used(); TODO: boken?
68 stats.handle_count = process->resource_manager.resources().size();
69 }
70 stats.exit_code = exit_code;
71
72 // Copy across
73 memcpy(buffer, &stats, sizeof(process_stats_t));
74 return sizeof(process_stats_t);
75
76 }
77
78 case ProcessFlags::READ_ENVIRONMENT: {
79
80 // Check bounds
81 if(size < sizeof(process_environment_t))
82 return -1;
83
84 // Get the info
85 process_environment_t info;
86 info.args = 0;
87 info.arg_count = 0; //TODO: store
88 info.env_vars = 0; //TODO: implement
89 info.env_var_count = 0;
90 info.working_directory = (const char*)MemoryManager::malloc(process->working_directory.length() + 1);
91 info.executable_path = 0;// TODO: store
92
93 // Copy the info
94 // .. args
95 // .. env vars
96 memcpy((void*)info.working_directory, process->working_directory.c_str(), process->working_directory.length() + 1);
97 // .. executable path
98
99 // Copy across
100 memcpy(buffer, &info, sizeof(process_environment_t));
101 return sizeof(process_environment_t);
102
103 }
104
105
106 case ProcessFlags::READ_HANDLES: {
107
108 // Check bounds
109 size_t size_needed = sizeof(handle_info_t) * process->resource_manager.resources().size();
110 if(size < size_needed)
111 return -1;
112
113 for(const auto& resource : process->resource_manager.resources()) {
114
115 // Get the info
116 handle_info_t handle_info;
117 handle_info.handle = resource.first;
118 handle_info.type = resource.second->type();
119 handle_info.name = (const char*)MemoryManager::malloc(resource.second->name().length() + 1);
120 memcpy((void*)handle_info.name, resource.second->name().c_str(), resource.second->name().length() + 1);
121
122 // Copy across
123 memcpy(buffer, &handle_info, sizeof(handle_info_t));
124 buffer = (void*)((uintptr_t)buffer + sizeof(handle_info_t));
125 }
126
127 return (int)size_needed;
128
129 }
130
131 default:
132 return -1;
133 }
134}
static void * malloc(size_t size)
Allocates a block of memory in the current USERSPACE heap.
uint64_t pid() const
Gets the pid of the process.
Definition process.cpp:429
uint64_t total_ticks()
Gets the total ticks of the threads in the process (not this does not include any past killed threads...
Definition process.cpp:439
resources::ResourceManager resource_manager
The manger for resources used by this process.
Definition process.h:122
common::Vector< Thread * > threads()
Gets the threads of the process.
Definition process.cpp:419
string working_directory
The working directory of the process.
Definition process.h:119
uint64_t exit_code
The exit code of the process once it has exited.
common::Map< uint64_t, Resource * > resources()
Get the resources currently open.
Definition resource.cpp:223

References exit_code, MaxOS::memory::MemoryManager::malloc(), MaxOS::processes::Process::pid(), process, MaxOS::processes::Process::resource_manager, MaxOS::processes::resources::ResourceManager::resources(), MaxOS::processes::Process::threads(), MaxOS::processes::Process::total_ticks(), and MaxOS::processes::Process::working_directory.

◆ write()

int ProcessResource::write ( const void *  buffer,
size_t  size,
size_t  flags 
)
final

Writes data to the process resource.

Parameters
bufferThe buffer to write from
sizeThe number of bytes to write
flagsThe flags to pass to the writing
Returns
The number of bytes successfully written or -1 on error

Definition at line 145 of file scheduler_resources.cpp.

145 {
146
147 // Process not found
148 if(!process)
149 return -1;
150
151 switch ((ProcessFlags)flags) {
152
153 case ProcessFlags::WRITE_CHANGE_DIRECTORY: {
154
155 // Change the working directory
156 string new_directory = string((const char*)buffer);
157 process->working_directory = new_directory;
158 return size;
159 }
160
161 case ProcessFlags::WRITE_KILL: {
162
163 // Kill the process
164 exit_code = *((uint64_t*)buffer);
166 process->threads()[0]->yield();
167
168 return size;
169 }
170
171 default:
172 return -1;
173 }
174}

References exit_code, process, MaxOS::processes::GlobalScheduler::remove_process(), MaxOS::processes::Process::threads(), and MaxOS::processes::Process::working_directory.

Member Data Documentation

◆ exit_code

uint64_t MaxOS::processes::resources::ProcessResource::exit_code

The exit code of the process once it has exited.

Definition at line 41 of file scheduler_resources.h.

Referenced by read(), and write().

◆ process

Process* MaxOS::processes::resources::ProcessResource::process

The process that this resource handles & exposes.

Definition at line 40 of file scheduler_resources.h.

Referenced by close(), read(), and write().


The documentation for this class was generated from the following files: