FreeNOS
ProcessClient.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <FreeNOS/System.h>
19#include <Types.h>
20#include <Macros.h>
21#include "ProcessClient.h"
22
24
26
28{
29 return m_pid;
30}
31
36
38 ProcessClient::Info &info) const
39{
40#ifndef __HOST__
41 const char * textStates[] = {
42 "Ready",
43 "Sleeping",
44 "Waiting",
45 "Stopped"
46 };
47 const Arch::MemoryMap map;
48 const Memory::Range range = map.range(MemoryMap::UserArgs);
49 char cmd[128];
50
51 const API::Result result = ProcessCtl(pid, InfoPID, (Address) &info.kernelState);
52 switch (result)
53 {
54 case API::Success:
55 break;
56 case API::NotFound:
57 return NotFound;
58 default:
59 return IOError;
60 }
61
62 // Read the full command
63 if (VMCopy(pid, API::Read, (Address) cmd, range.virt, sizeof(cmd)) != API::Success)
64 {
65 return IOError;
66 }
67
68 // Fill output
69 info.command = cmd;
70 info.textState = (pid == m_pid ? "Running" : textStates[info.kernelState.state]);
71#endif /* __HOST__ */
72
73 return Success;
74}
75
77 ProcessClient::Info &info) const
78{
79 // Loop processes
80 for (ProcessID i = 0; i < MaximumProcesses; i++)
81 {
82 const Result result = processInfo(i, info);
83 if (result == Success && info.command.equals(program))
84 {
85 return result;
86 }
87 }
88
89 return NotFound;
90}
91
93{
95
96 const Result result = processInfo(program, info);
97 if (result == Success)
98 {
99 return info.kernelState.id;
100 }
101 else
102 {
103 return ANY;
104 }
105}
Result
Enumeration of generic kernel API result codes.
Definition API.h:69
@ NotFound
Definition API.h:73
@ Success
Definition API.h:70
@ Read
Definition API.h:98
Memory mapping for the kernel and user processes on the ARM architecture.
Definition ARMMap.h:38
@ UserArgs
< Used for copying program arguments and file descriptors
Definition MemoryMap.h:61
Memory::Range range(Region region) const
Get memory range for the given region.
Definition MemoryMap.cpp:36
static const Size MaximumProcesses
Maximum number of processes.
static const ProcessID m_parent
Our parent process identifier.
Result
Result codes.
ProcessID findProcess(const String program) const
Find a process by its program name.
ProcessID getProcessID() const
Get current process identifier.
static const ProcessID m_pid
Our own process identifier.
ProcessID getParentID() const
Get parent process identifier.
Result processInfo(const ProcessID pid, Info &info) const
Get process information by its ID.
Abstraction of strings.
Definition String.h:42
virtual bool equals(const String &str) const
Alias for compareTo().
Definition String.cpp:262
API::Result VMCopy(const ProcessID proc, const API::Operation how, const Address ours, const Address theirs, const Size sz)
Prototype for user applications.
Definition VMCopy.h:42
#define SELF
Definition ProcessID.h:35
#define ANY
Definition ProcessID.h:34
API::Result ProcessCtl(const ProcessID proc, const ProcessOperation op, const Address addr=0, const Address output=0)
Prototype for user applications.
Definition ProcessCtl.h:93
@ GetParent
Definition ProcessCtl.h:42
@ InfoPID
Definition ProcessCtl.h:47
@ GetPID
Definition ProcessCtl.h:41
u32 ProcessID
Process Identification Number.
Definition Types.h:140
unsigned long Address
A memory address.
Definition Types.h:131
Memory range.
Definition Memory.h:56
Address virt
Virtual address.
Definition Memory.h:57
Process information.
String command
Full command including program path.
ProcessInfo kernelState
Process state retrieved from the kernel.
String textState
Textual state of the process.
Process::State state
Defines the current state of the Process.
Definition ProcessCtl.h:72
ProcessID id
Process Identity number.
Definition ProcessCtl.h:66