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
23SysControl::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
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}
Result
Enumeration of generic kernel API result codes.
Definition API.h:69
@ Success
Definition API.h:70
Result
Result codes.
Definition Application.h:54
const ArgumentContainer & arguments() const
Get program arguments.
ArgumentParser & parser()
Get program arguments parser.
const Vector< Argument * > & getPositionals() const
Get positional arguments.
void setDescription(const String &desc)
Set program description.
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
Result registerFlag(char arg, const char *name, const char *description)
Register a flag Argument.
POSIX-compatible application.
RecoveryClient provides a simple interface to the local core's RecoveryServer.
Recovery::Result restartProcess(const ProcessID pid) const
Restart a process.
virtual Result exec()
Execute the application.
virtual ~SysControl()
Destructor.
Result restartProcess(const ProcessID pid) const
Restart the given process by its ID.
SysControl(int argc, char **argv)
Constructor.
Result resumeProcess(const ProcessID pid) const
Resume the given process by its ID.
Result stopProcess(const ProcessID pid) const
Stop the given process by its ID.
Vectors are dynamically resizeable Arrays.
Definition Vector.h:42
virtual Size count() const
Returns the number of items inside the Vector.
Definition Vector.h:204
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
@ Resume
Definition ProcessCtl.h:55
@ Stop
Definition ProcessCtl.h:54
u32 ProcessID
Process Identification Number.
Definition Types.h:140
#define ERROR(msg)
Output an error message.
Definition Log.h:61
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
#define DEBUG(msg)
Output a debug message to standard output.
Definition Log.h:89
Result
Result codes.
Definition Recovery.h:43
@ Success
Definition Recovery.h:44