PidStopperLinux.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::core
19 * @author Nicola Miskowiec
20 * @date 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25
26#include "PidStopperLinux.h"
27
28#include <errno.h>
29#include <signal.h>
30#include <string.h>
31
32#include <sys/types.h>
33
35
36using namespace ScenarioManager;
37using namespace Exec;
38using namespace Data_Structure;
39
40namespace
41{
42 /**
43 * Sends `signalNumber` to `pid` and reports a failure to do so.
44 *
45 * kill() failing is not a detail: ESRCH means the process was already gone, and EPERM means
46 * the pid belongs to a process this ScenarioManager is not allowed to signal - which almost
47 * always means it is not the application at all, but an unrelated process that was handed the
48 * recycled pid. Both used to be discarded silently, leaving a stop that did nothing and then
49 * timed out with no indication of why.
50 */
51 void
52 Signal(int pid, int signalNumber, const std::string& signalName, const std::string& name)
53 {
54 if (::kill(pid, signalNumber) == 0)
55 {
56 return;
57 }
58
59 const int errorNumber = errno;
60 ARMARX_WARNING_S << "Could not send " << signalName << " to application " << name
61 << " (pid " << pid << "): " << ::strerror(errorNumber)
62 << (errorNumber == EPERM
63 ? ". The pid belongs to a process this ScenarioManager may not "
64 "signal, so it is most likely not this application."
65 : "");
66 }
67} // namespace
68
69void
71{
72 const int pid = application->getPid();
73 if (pid > 0)
74 {
75 ARMARX_INFO_S << "Stopping application " << application->getName() << " (pid " << pid
76 << ")";
77 Signal(pid, SIGTERM, "SIGTERM", application->getName());
78 }
79 else
80 {
81 ARMARX_WARNING_S << "Cannot stop application " << application->getName()
82 << ": no pid known for it (it is not running, or it was not started by "
83 "this ScenarioManager).";
84 }
85}
86
87void
89{
90 const int pid = application->getPid();
91 if (pid > 0)
92 {
93 ARMARX_INFO_S << "Killing application " << application->getName() << " (pid " << pid << ")";
94 Signal(pid, SIGKILL, "SIGKILL", application->getName());
95 }
96 else
97 {
98 ARMARX_WARNING_S << "Cannot kill application " << application->getName()
99 << ": no pid known for it (it is not running, or it was not started by "
100 "this ScenarioManager).";
101 }
102}
void kill(Data_Structure::ApplicationInstancePtr application) override
Kills an application based on its pid.
void stop(Data_Structure::ApplicationInstancePtr application) override
Stops an application based on its pid.
#define ARMARX_INFO_S
Definition Logging.h:200
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:211
std::shared_ptr< ApplicationInstance > ApplicationInstancePtr