FreeNOS
ProcessList.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 <Types.h>
19 #include <Macros.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <ProcessClient.h>
23 #include "ProcessList.h"
24 
25 ProcessList::ProcessList(int argc, char **argv)
26  : POSIXApplication(argc, argv)
27 {
28  parser().setDescription("Output system process list");
29 }
30 
32 {
33  const ProcessClient process;
34  String out;
35 
36  // Print header
37  out << "ID PARENT USER GROUP STATUS CMD\r\n";
38 
39  // Loop processes
40  for (ProcessID pid = 0; pid < ProcessClient::MaximumProcesses; pid++)
41  {
43 
44  const ProcessClient::Result result = process.processInfo(pid, info);
45  if (result == ProcessClient::Success)
46  {
47  DEBUG("PID " << pid << " state = " << *info.textState);
48 
49  // Output a line
50  char line[128];
51  snprintf(line, sizeof(line),
52  "%3d %7d %4d %5d %10s %32s\r\n",
53  pid, info.kernelState.parent,
54  0, 0, *info.textState, *info.command);
55  out << line;
56  }
57  }
58 
59  // Output the table
60  write(1, *out, out.length());
61  return Success;
62 }
ProcessList::ProcessList
ProcessList(int argc, char **argv)
Constructor.
Definition: ProcessList.cpp:25
Macros.h
ProcessList.h
write
ssize_t write(int fildes, const void *buf, size_t nbyte)
Write on a file.
Definition: write.cpp:22
Types.h
String::length
Size length() const
Same as count().
Definition: String.cpp:105
ProcessInfo::parent
ProcessID parent
Parent process id.
Definition: ProcessCtl.h:69
ProcessClient::processInfo
Result processInfo(const ProcessID pid, Info &info) const
Get process information by its ID.
Definition: ProcessClient.cpp:37
ProcessClient::Success
@ Success
Definition: ProcessClient.h:51
String
Abstraction of strings.
Definition: String.h:41
ProcessClient
ProcessClient provides information about all processes on the local core.
Definition: ProcessClient.h:39
ProcessID
u32 ProcessID
Process Identification Number.
Definition: Types.h:140
POSIXApplication
POSIX-compatible application.
Definition: POSIXApplication.h:35
Application::Success
@ Success
Definition: Application.h:55
ArgumentParser::setDescription
void setDescription(const String &desc)
Set program description.
Definition: ArgumentParser.cpp:95
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
DEBUG
#define DEBUG(msg)
Output a debug message to standard output.
Definition: Log.h:89
ProcessClient::Info::textState
String textState
Textual state of the process.
Definition: ProcessClient.h:68
stdio.h
Application::Result
Result
Result codes.
Definition: Application.h:53
ProcessList::exec
virtual Result exec()
Execute the application.
Definition: ProcessList.cpp:31
ProcessClient::Info::kernelState
ProcessInfo kernelState
Process state retrieved from the kernel.
Definition: ProcessClient.h:62
ProcessClient::Result
Result
Result codes.
Definition: ProcessClient.h:49
ProcessClient.h
ProcessClient::Info
Process information.
Definition: ProcessClient.h:59
unistd.h
Application::parser
ArgumentParser & parser()
Get program arguments parser.
Definition: Application.cpp:102
snprintf
int snprintf(char *buffer, unsigned int size, const char *fmt,...)
Write a formatted string into a buffer.
Definition: snprintf.cpp:22