DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
okssystem
src
Process.cpp
Go to the documentation of this file.
1
// DUNE DAQ modification notice:
2
// This file has been modified from the original ATLAS system source for the DUNE DAQ project.
3
// Fork baseline commit: system-00-00-20 (2020-09-25).
4
// Renamed since fork: yes (from src/Process.cxx to src/Process.cpp).
5
6
/*
7
* Process.cxx
8
* OksSystem
9
*
10
* Created by Matthias Wiesmann on 18.01.05.
11
* Copyright 2005 CERN. All rights reserved.
12
*
13
*/
14
15
#include <sys/types.h>
16
#include <sys/wait.h>
17
#include <sysexits.h>
18
#include <unistd.h>
19
20
#include <iostream>
21
#include <sstream>
22
23
#include "
ers/ers.hpp
"
24
25
#include "
okssystem/Process.hpp
"
26
#include "
okssystem/exceptions.hpp
"
27
28
const
int
OksSystem::Process::TEST_BASE_VALUE
= 182;
29
const
int
OksSystem::Process::TEST_MAX_VALUE
= 186;
30
31
const
int
OksSystem::Process::TERMINATION_WAIT
=
32
100000;
33
34
const
char
*
const
OksSystem::Process::SYS_EXITS_NAMES
[] = {
35
"command line usage error"
,
"data format error"
,
36
"cannot open input"
,
"addressee unknown"
,
37
"host name unknown"
,
"service unavailable"
,
38
"internal software error"
,
"okssystem error"
,
39
"critical OS file missing"
,
"can't create (user) output file"
,
40
"input/output error"
,
"temp failure; user is invited to retry"
,
41
"remote error in protocol"
,
"permission denied "
,
42
"configuration error"
43
};
44
const
char
*
const
OksSystem::Process::TEST_EXITS_NAMES
[] = {
"undefined test"
,
45
"test failed"
,
46
"test unresolved"
,
47
"test untested"
,
48
"unsupported test"
};
49
const
char
*
const
OksSystem::Process::OK_EXIT_NAME
=
"ok"
;
50
51
OksSystem::Process
*
OksSystem::Process::s_instance
= 0;
52
58
59
const
char
*
60
OksSystem::Process::exit_text
(
int
return_value)
61
{
62
if
(0 == return_value)
63
return
OK_EXIT_NAME
;
64
if
((return_value >= EX__BASE) && (return_value <= EX__MAX))
65
return
SYS_EXITS_NAMES
[return_value - EX__BASE];
66
if
((return_value >=
TEST_BASE_VALUE
) && (return_value <=
TEST_MAX_VALUE
))
67
return
TEST_EXITS_NAMES
[return_value -
TEST_BASE_VALUE
];
68
return
0;
69
}
// return_text
70
76
77
std::string
78
OksSystem::Process::exit_pretty
(
int
return_value)
79
{
80
std::ostringstream stream;
81
const
char
* text =
OksSystem::Process::exit_text
(return_value);
82
if
(text) {
83
stream << text <<
'('
<< return_value <<
')'
;
84
}
else
{
85
stream << return_value;
86
}
87
return
stream.str();
88
}
// exit_string
89
94
95
const
OksSystem::Process
*
96
OksSystem::Process::instance
() throw()
97
{
98
if
(0 ==
s_instance
) {
99
s_instance
=
new
OksSystem::Process
();
100
}
// if
101
return
s_instance
;
102
}
// instance
103
106
107
void
108
OksSystem::Process::set_name
(
const
std::string& name)
throw
()
109
{
110
(void)
instance
();
111
s_instance
->m_process_name = name;
112
}
// set_name
113
117
118
OksSystem::Process::Process
()
119
{
120
m_process_id
= ::getpid();
121
}
// Process
122
126
127
OksSystem::Process::Process
(
const
Process
& other)
128
{
129
m_process_id
= other.
m_process_id
;
130
m_process_name
= other.
m_process_name
;
131
}
// Process
132
136
137
OksSystem::Process::Process
(pid_t
id
)
138
{
139
m_process_id
= id;
140
}
// Process
141
146
147
OksSystem::Process::Process
(pid_t
id
,
const
std::string& name)
148
{
149
m_process_id
= id;
150
m_process_name
= name;
151
}
152
155
156
OksSystem::Process::~Process
() throw()
157
{
158
m_process_id
= 0;
159
}
// ~Process
160
163
164
OksSystem::Process::operator pid_t()
const
throw()
165
{
166
return
m_process_id
;
167
}
// operator pid_t
168
174
175
int
176
OksSystem::Process::join
(
bool
throw_non_zero)
const
177
{
178
ERS_PRECONDITION
(!
equals
(*
instance
()));
179
int
status;
180
errno = 0;
181
pid_t pid = ::waitpid(
m_process_id
, &status, 0);
182
if
(pid !=
m_process_id
) {
183
std::string message =
"on process "
+ this->
to_string
();
184
throw
OksSystem::OksSystemCallIssue(
ERS_HERE
, errno,
"waitpid"
, message.c_str());
185
}
// if
186
if
(WIFEXITED(status)) {
187
int
exit_status = WEXITSTATUS(status);
188
if
((throw_non_zero ==
false
) || (exit_status == 0))
189
return
exit_status;
190
throw
OksSystem::TerminationIssue(
ERS_HERE
, errno, exit_status);
191
}
// exit status
192
if
(WIFSIGNALED(status))
193
throw
OksSystem::SignalIssue(
ERS_HERE
, errno, WTERMSIG(status));
194
if
(WIFSTOPPED(status))
195
throw
OksSystem::SignalIssue(
ERS_HERE
, errno, WSTOPSIG(status));
196
return
WEXITSTATUS(status);
197
}
// join
198
202
203
void
204
OksSystem::Process::signal
(
int
signal_number)
const
205
{
206
const
int
status = ::kill(
m_process_id
, signal_number);
207
if
(status < 0) {
208
std::string message =
"on process "
+ this->
to_string
();
209
throw
OksSystem::OksSystemCallIssue(
ERS_HERE
, errno,
"kill"
, message.c_str());
210
}
211
}
// signal
212
218
219
bool
220
OksSystem::Process::exists
()
const
221
{
222
const
int
status = ::kill(
m_process_id
, 0);
223
return
(status >= 0);
224
}
// exists
225
230
231
bool
232
OksSystem::Process::equals
(
const
Process
& other)
const
throw
()
233
{
234
return
m_process_id
== other.m_process_id;
235
}
// equals
236
241
242
void
243
OksSystem::Process::terminate
()
const
244
{
245
/*
246
struct timespec wait_time;
247
struct timespec remain_time;
248
wait_time.tv_sec = 0;
249
wait_time.tv_nsec = TERMINATION_WAIT;
250
*/
251
if
(
exists
()) {
252
signal
(SIGTERM);
253
}
254
return
;
255
}
256
257
/* This is a piece of crap
258
nanosleep(&wait_time,&remain_time);
259
if (exists()) {
260
signal(SIGQUIT);
261
} else {
262
return;
263
} //
264
nanosleep(&wait_time,&remain_time);
265
if (exists()) {
266
signal(SIGKILL);
267
} // exists
268
} // terminate
269
*/
273
274
std::string
275
OksSystem::Process::to_string
()
const
throw()
276
{
277
std::ostringstream stream;
278
if
(!
m_process_name
.empty()) {
279
stream << m_process_name <<
' '
;
280
}
281
stream <<
"pid: "
<<
m_process_id
;
282
return
stream.str();
283
}
// to_string
284
287
288
pid_t
289
OksSystem::Process::process_id
()
const
throw()
290
{
291
return
m_process_id
;
292
}
293
299
300
std::ostream&
301
operator<<
(std::ostream& out,
const
OksSystem::Process
& proc)
302
{
303
out << proc.
to_string
();
304
return
out;
305
}
// operator<<
306
313
314
bool
315
operator==
(
const
OksSystem::Process
& a,
const
OksSystem::Process
& b)
throw
()
316
{
317
return
a.equals(b);
318
}
// operator ==
319
326
327
bool
328
operator!=
(
const
OksSystem::Process
& a,
const
OksSystem::Process
& b)
throw
()
329
{
330
return
!a.equals(b);
331
}
// operator !=
ERS_PRECONDITION
#define ERS_PRECONDITION(expression)
operator==
bool operator==(const OksSystem::File &a, const OksSystem::File &b)
Definition
File.cpp:956
operator!=
bool operator!=(const OksSystem::File &a, const OksSystem::File &b)
Definition
File.cpp:962
ERS_HERE
#define ERS_HERE
Definition
LocalContext.hpp:127
operator<<
void operator<<(TraceStreamer &x, const ers::Issue &r)
Definition
Logger.hxx:128
Process.hpp
OksSystem::Process
Wrapper for process manipulation.
Definition
Process.hpp:31
OksSystem::Process::TERMINATION_WAIT
static const int TERMINATION_WAIT
wait time before deciding a termination signal did not work
Definition
Process.hpp:41
OksSystem::Process::instance
static const Process * instance()
Definition
Process.cpp:96
OksSystem::Process::OK_EXIT_NAME
static const char *const OK_EXIT_NAME
string to describe 0 exit value
Definition
Process.hpp:37
OksSystem::Process::exit_text
static const char * exit_text(int return_value)
the textual description of standard exit codes
Definition
Process.cpp:60
OksSystem::Process::join
int join(bool throw_non_zero=false) const
Waits for the process to terminate.
Definition
Process.cpp:176
OksSystem::Process::equals
bool equals(const Process &other) const
Comparison method.
Definition
Process.cpp:232
OksSystem::Process::TEST_BASE_VALUE
static const int TEST_BASE_VALUE
first value for test manager exit codes
Definition
Process.hpp:39
OksSystem::Process::signal
void signal(int signal) const
Sends a signal to the process.
Definition
Process.cpp:204
OksSystem::Process::m_process_id
pid_t m_process_id
process identifier
Definition
Process.hpp:33
OksSystem::Process::terminate
void terminate() const
Terminate the process.
Definition
Process.cpp:243
OksSystem::Process::SYS_EXITS_NAMES
static const char *const SYS_EXITS_NAMES[]
names for standard system exit values
Definition
Process.hpp:34
OksSystem::Process::set_name
static void set_name(const std::string &name)
Definition
Process.cpp:108
OksSystem::Process::TEST_MAX_VALUE
static const int TEST_MAX_VALUE
last value for test manager exit codes
Definition
Process.hpp:40
OksSystem::Process::exit_pretty
static std::string exit_pretty(int return_value)
Definition
Process.cpp:78
OksSystem::Process::exists
bool exists() const
Does the process exist?
Definition
Process.cpp:220
OksSystem::Process::TEST_EXITS_NAMES
static const char *const TEST_EXITS_NAMES[]
names for testing exit values
Definition
Process.hpp:44
OksSystem::Process::process_id
pid_t process_id() const
the id of the process
Definition
Process.cpp:289
OksSystem::Process::s_instance
static Process * s_instance
singleton instance
Definition
Process.hpp:42
OksSystem::Process::~Process
~Process()
Destructor.
Definition
Process.cpp:156
OksSystem::Process::m_process_name
std::string m_process_name
process name - only to be used for display
Definition
Process.hpp:34
OksSystem::Process::Process
Process()
Builds a process representing the current process.
Definition
Process.cpp:118
OksSystem::Process::to_string
std::string to_string() const
String conversion method.
Definition
Process.cpp:275
ers.hpp
exceptions.hpp
Generated on
for DUNE-DAQ by
1.18.0