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 
33 {
34  return m_parent;
35 }
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 }
Memory::Range
Memory range.
Definition: Memory.h:55
API::Result
Result
Enumeration of generic kernel API result codes.
Definition: API.h:68
Macros.h
Types.h
ProcessClient::processInfo
Result processInfo(const ProcessID pid, Info &info) const
Get process information by its ID.
Definition: ProcessClient.cpp:37
ProcessInfo::id
ProcessID id
Process Identity number.
Definition: ProcessCtl.h:66
String
Abstraction of strings.
Definition: String.h:41
ProcessClient::Success
@ Success
Definition: ProcessClient.h:51
ProcessClient::getParentID
ProcessID getParentID() const
Get parent process identifier.
Definition: ProcessClient.cpp:32
ProcessID
u32 ProcessID
Process Identification Number.
Definition: Types.h:140
Address
unsigned long Address
A memory address.
Definition: Types.h:131
ProcessClient::getProcessID
ProcessID getProcessID() const
Get current process identifier.
Definition: ProcessClient.cpp:27
ProcessCtl
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
ProcessClient::MaximumProcesses
static const Size MaximumProcesses
Maximum number of processes.
Definition: ProcessClient.h:44
ProcessClient::Info::command
String command
Full command including program path.
Definition: ProcessClient.h:65
API::Read
@ Read
Definition: API.h:98
ARMMap
Memory mapping for the kernel and user processes on the ARM architecture.
Definition: ARMMap.h:37
SELF
#define SELF
Definition: ProcessID.h:35
InfoPID
@ InfoPID
Definition: ProcessCtl.h:47
ProcessInfo::state
Process::State state
Defines the current state of the Process.
Definition: ProcessCtl.h:72
GetPID
@ GetPID
Definition: ProcessCtl.h:41
ProcessClient::Info::textState
String textState
Textual state of the process.
Definition: ProcessClient.h:68
MemoryMap::range
Memory::Range range(Region region) const
Get memory range for the given region.
Definition: MemoryMap.cpp:36
ProcessClient::Info::kernelState
ProcessInfo kernelState
Process state retrieved from the kernel.
Definition: ProcessClient.h:62
ProcessClient::m_pid
static const ProcessID m_pid
Our own process identifier.
Definition: ProcessClient.h:127
ProcessClient::findProcess
ProcessID findProcess(const String program) const
Find a process by its program name.
Definition: ProcessClient.cpp:92
ProcessClient::Result
Result
Result codes.
Definition: ProcessClient.h:49
ProcessClient.h
ProcessClient::NotFound
@ NotFound
Definition: ProcessClient.h:52
ProcessClient::Info
Process information.
Definition: ProcessClient.h:59
ProcessClient::m_parent
static const ProcessID m_parent
Our parent process identifier.
Definition: ProcessClient.h:130
API::NotFound
@ NotFound
Definition: API.h:73
API::Success
@ Success
Definition: API.h:70
ANY
#define ANY
Definition: ProcessID.h:34
Memory::Range::virt
Address virt
Virtual address.
Definition: Memory.h:57
MemoryMap::UserArgs
@ UserArgs
< Used for copying program arguments and file descriptors
Definition: MemoryMap.h:61
VMCopy
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
String::equals
virtual bool equals(const String &str) const
Alias for compareTo().
Definition: String.cpp:262
ProcessClient::IOError
@ IOError
Definition: ProcessClient.h:53
GetParent
@ GetParent
Definition: ProcessCtl.h:42