FreeNOS
SysControl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 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/User.h>
19 #include <Log.h>
20 #include <RecoveryClient.h>
21 #include "SysControl.h"
22 
23 SysControl::SysControl(int argc, char **argv)
24  : POSIXApplication(argc, argv)
25 {
26  parser().setDescription("Control program for various system services");
27  parser().registerPositional("PID", "List of processes to target", 0);
28  parser().registerFlag('s', "stop", "Stop the given process(es)");
29  parser().registerFlag('c', "continue", "Continue executing the given process(es)");
30  parser().registerFlag('r', "restart", "Restart the given process(es) via recovery server");
31 }
32 
34 {
35 }
36 
38 {
39  const Vector<Argument *> & positionals = arguments().getPositionals();
40 
41  // Loop positional arguments (list of process identifiers)
42  for (Size i = 0; i < positionals.count(); i++)
43  {
44  const ProcessID pid = positionals[i]->getValue().toLong();
45 
46  if (arguments().get("stop") != ZERO)
47  {
48  const Result r = stopProcess(pid);
49  if (r != Success)
50  {
51  return r;
52  }
53  }
54  else if (arguments().get("continue") != ZERO)
55  {
56  const Result r = resumeProcess(pid);
57  if (r != Success)
58  {
59  return r;
60  }
61  }
62  else if (arguments().get("restart") != ZERO)
63  {
64  const Result r = restartProcess(pid);
65  if (r != Success)
66  {
67  return r;
68  }
69  }
70  else
71  {
72  ERROR("no operation specified for PID: " << pid);
73  return InvalidArgument;
74  }
75  }
76 
77  return Success;
78 }
79 
81 {
82  DEBUG("pid = " << pid);
83 
84  const API::Result result = ProcessCtl(pid, Stop);
85  if (result != API::Success)
86  {
87  ERROR("failed to stop PID " << pid << ": result = " << (int) result);
88  return IOError;
89  }
90 
91  return Success;
92 }
93 
95 {
96  DEBUG("pid = " << pid);
97 
98  const API::Result result = ProcessCtl(pid, Resume);
99  if (result != API::Success)
100  {
101  ERROR("failed to resume PID " << pid << ": result = " << (int) result);
102  return IOError;
103  }
104 
105  return Success;
106 }
107 
109 {
110  DEBUG("pid = " << pid);
111 
112  const RecoveryClient recovery;
113  const Recovery::Result result = recovery.restartProcess(pid);
114  if (result != Recovery::Success)
115  {
116  ERROR("failed to restart PID " << pid << ": result = " << (int) result);
117  return IOError;
118  }
119 
120  return Success;
121 }
SysControl::restartProcess
Result restartProcess(const ProcessID pid) const
Restart the given process by its ID.
Definition: SysControl.cpp:108
API::Result
Result
Enumeration of generic kernel API result codes.
Definition: API.h:68
RecoveryClient::restartProcess
Recovery::Result restartProcess(const ProcessID pid) const
Restart a process.
Definition: RecoveryClient.cpp:39
SysControl::SysControl
SysControl(int argc, char **argv)
Constructor.
Definition: SysControl.cpp:23
Recovery::Success
@ Success
Definition: Recovery.h:44
Resume
@ Resume
Definition: ProcessCtl.h:55
ProcessID
u32 ProcessID
Process Identification Number.
Definition: Types.h:140
POSIXApplication
POSIX-compatible application.
Definition: POSIXApplication.h:35
SysControl::resumeProcess
Result resumeProcess(const ProcessID pid) const
Resume the given process by its ID.
Definition: SysControl.cpp:94
Application::Success
@ Success
Definition: Application.h:55
Application::arguments
const ArgumentContainer & arguments() const
Get program arguments.
Definition: Application.cpp:112
ArgumentParser::setDescription
void setDescription(const String &desc)
Set program description.
Definition: ArgumentParser.cpp:95
RecoveryClient
RecoveryClient provides a simple interface to the local core's RecoveryServer.
Definition: RecoveryClient.h:40
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
Log.h
Application::IOError
@ IOError
Definition: Application.h:57
DEBUG
#define DEBUG(msg)
Output a debug message to standard output.
Definition: Log.h:89
SysControl::~SysControl
virtual ~SysControl()
Destructor.
Definition: SysControl.cpp:33
Application::InvalidArgument
@ InvalidArgument
Definition: Application.h:58
Vector::count
virtual Size count() const
Returns the number of items inside the Vector.
Definition: Vector.h:204
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
Application::Result
Result
Result codes.
Definition: Application.h:53
ArgumentParser::registerFlag
Result registerFlag(char arg, const char *name, const char *description)
Register a flag Argument.
Definition: ArgumentParser.cpp:100
ArgumentParser::registerPositional
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
Definition: ArgumentParser.cpp:119
Recovery::Result
Result
Result codes.
Definition: Recovery.h:42
Application::parser
ArgumentParser & parser()
Get program arguments parser.
Definition: Application.cpp:102
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
RecoveryClient.h
SysControl.h
API::Success
@ Success
Definition: API.h:70
SysControl::stopProcess
Result stopProcess(const ProcessID pid) const
Stop the given process by its ID.
Definition: SysControl.cpp:80
SysControl::exec
virtual Result exec()
Execute the application.
Definition: SysControl.cpp:37
Vector< Argument * >
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
Stop
@ Stop
Definition: ProcessCtl.h:54
ArgumentContainer::getPositionals
const Vector< Argument * > & getPositionals() const
Get positional arguments.
Definition: ArgumentContainer.cpp:39