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/wait.h>
16
#include <sys/types.h>
17
#include <unistd.h>
18
#include <sysexits.h>
19
20
#include <iostream>
21
#include <sstream>
22
23
#include "
ers/ers.hpp
"
24
25
#include "
okssystem/exceptions.hpp
"
26
#include "
okssystem/Process.hpp
"
27
28
29
const
int
OksSystem::Process::TEST_BASE_VALUE
= 182;
30
const
int
OksSystem::Process::TEST_MAX_VALUE
= 186;
31
32
const
int
OksSystem::Process::TERMINATION_WAIT
= 100000;
33
34
const
char
*
const
OksSystem::Process::SYS_EXITS_NAMES
[] = {
"command line usage error"
,
"data format error"
,
"cannot open input"
,
"addressee unknown"
,
"host name unknown"
,
"service unavailable"
,
"internal software error"
,
"okssystem error"
,
35
"critical OS file missing"
,
"can't create (user) output file"
,
"input/output error"
,
"temp failure; user is invited to retry"
,
"remote error in protocol"
,
"permission denied "
,
"configuration error"
};
36
const
char
*
const
OksSystem::Process::TEST_EXITS_NAMES
[] = {
"undefined test"
,
"test failed"
,
"test unresolved"
,
"test untested"
,
"unsupported test"
};
37
const
char
*
const
OksSystem::Process::OK_EXIT_NAME
=
"ok"
;
38
39
OksSystem::Process
*
OksSystem::Process::s_instance
= 0;
40
46
47
const
char
*
OksSystem::Process::exit_text
(
int
return_value) {
48
if
( 0==return_value)
return
OK_EXIT_NAME
;
49
if
((return_value >= EX__BASE) && (return_value <= EX__MAX))
return
SYS_EXITS_NAMES
[return_value-EX__BASE];
50
if
((return_value >=
TEST_BASE_VALUE
) && (return_value <=
TEST_MAX_VALUE
))
return
TEST_EXITS_NAMES
[return_value-
TEST_BASE_VALUE
];
51
return
0;
52
}
// return_text
53
59
60
std::string
OksSystem::Process::exit_pretty
(
int
return_value) {
61
std::ostringstream stream;
62
const
char
* text =
OksSystem::Process::exit_text
(return_value);
63
if
(text) {
64
stream << text <<
'('
<< return_value <<
')'
;
65
}
else
{
66
stream << return_value ;
67
}
68
return
stream.str();
69
}
// exit_string
70
71
76
77
const
OksSystem::Process
*
OksSystem::Process::instance
() throw() {
78
if
(0==
s_instance
) {
79
s_instance
=
new
OksSystem::Process
();
80
}
// if
81
return
s_instance
;
82
}
// instance
83
84
87
88
void
OksSystem::Process::set_name
(
const
std::string &name)
throw
() {
89
(void)
instance
();
90
s_instance
->m_process_name = name;
91
}
// set_name
92
93
97
98
OksSystem::Process::Process
() {
99
m_process_id
= ::getpid();
100
}
// Process
101
105
106
OksSystem::Process::Process
(
const
Process
&other) {
107
m_process_id
= other.
m_process_id
;
108
m_process_name
= other.
m_process_name
;
109
}
// Process
110
114
115
OksSystem::Process::Process
(pid_t
id
){
116
m_process_id
= id;
117
}
// Process
118
123
124
OksSystem::Process::Process
(pid_t
id
,
const
std::string &name) {
125
m_process_id
= id;
126
m_process_name
= name;
127
}
128
129
132
133
OksSystem::Process::~Process
() throw() {
134
m_process_id
= 0;
135
}
// ~Process
136
139
140
OksSystem::Process::operator pid_t()
const
throw() {
141
return
m_process_id
;
142
}
// operator pid_t
143
144
150
151
int
OksSystem::Process::join
(
bool
throw_non_zero)
const
{
152
ERS_PRECONDITION
(!
equals
(*
instance
()));
153
int
status;
154
errno = 0;
155
pid_t pid = ::waitpid(
m_process_id
,&status,0);
156
if
(pid!=
m_process_id
) {
157
std::string message =
"on process "
+ this->
to_string
();
158
throw
OksSystem::OksSystemCallIssue(
ERS_HERE
, errno,
"waitpid"
, message.c_str() );
159
}
// if
160
if
(WIFEXITED(status)) {
161
int
exit_status = WEXITSTATUS(status);
162
if
((throw_non_zero==
false
) || (exit_status==0))
return
exit_status;
163
throw
OksSystem::TerminationIssue(
ERS_HERE
, errno, exit_status);
164
}
// exit status
165
if
(WIFSIGNALED(status))
throw
OksSystem::SignalIssue(
ERS_HERE
,errno,WTERMSIG(status));
166
if
(WIFSTOPPED(status))
throw
OksSystem::SignalIssue(
ERS_HERE
,errno,WSTOPSIG(status));
167
return
WEXITSTATUS(status);
168
}
// join
169
173
174
void
OksSystem::Process::signal
(
int
signal_number)
const
{
175
const
int
status = ::kill(
m_process_id
,signal_number);
176
if
(status < 0) {
177
std::string message =
"on process "
+ this->
to_string
();
178
throw
OksSystem::OksSystemCallIssue(
ERS_HERE
, errno,
"kill"
, message.c_str());
179
}
180
}
// signal
181
187
188
bool
OksSystem::Process::exists
()
const
{
189
const
int
status = ::kill(
m_process_id
,0);
190
return
(status>=0);
191
}
// exists
192
197
198
bool
OksSystem::Process::equals
(
const
Process
&other)
const
throw
() {
199
return
m_process_id
== other.m_process_id;
200
}
// equals
201
206
207
void
OksSystem::Process::terminate
()
const
{
208
/*
209
struct timespec wait_time;
210
struct timespec remain_time;
211
wait_time.tv_sec = 0;
212
wait_time.tv_nsec = TERMINATION_WAIT;
213
*/
214
if
(
exists
()) {
215
signal
(SIGTERM);
216
}
217
return
;
218
}
219
220
/* This is a piece of crap
221
nanosleep(&wait_time,&remain_time);
222
if (exists()) {
223
signal(SIGQUIT);
224
} else {
225
return;
226
} //
227
nanosleep(&wait_time,&remain_time);
228
if (exists()) {
229
signal(SIGKILL);
230
} // exists
231
} // terminate
232
*/
236
237
std::string
OksSystem::Process::to_string
()
const
throw() {
238
std::ostringstream stream;
239
if
(!
m_process_name
.empty()) {
240
stream << m_process_name <<
' '
;
241
}
242
stream <<
"pid: "
<<
m_process_id
;
243
return
stream.str();
244
}
// to_string
245
248
249
pid_t
OksSystem::Process::process_id
()
const
throw() {
return
m_process_id
;}
250
256
257
std::ostream&
operator<<
(std::ostream& out,
const
OksSystem::Process
&proc) {
258
out << proc.
to_string
();
259
return
out;
260
}
// operator<<
261
268
269
bool
operator ==
(
const
OksSystem::Process
&a,
const
OksSystem::Process
&b)
throw
() {
270
return
a.equals(b);
271
}
// operator ==
272
279
280
bool
operator !=
(
const
OksSystem::Process
&a,
const
OksSystem::Process
&b)
throw
() {
281
return
! a.equals(b);
282
}
// operator !=
283
284
ERS_PRECONDITION
#define ERS_PRECONDITION(expression)
ERS_HERE
#define ERS_HERE
Definition
LocalContext.hpp:141
operator<<
void operator<<(TraceStreamer &x, const ers::Issue &r)
Definition
Logger.hxx:102
Process.hpp
OksSystem::Process
Wrapper for process manipulation.
Definition
Process.hpp:30
OksSystem::Process::TERMINATION_WAIT
static const int TERMINATION_WAIT
wait time before deciding a termination signal did not work
Definition
Process.hpp:40
OksSystem::Process::instance
static const Process * instance()
Definition
Process.cpp:77
OksSystem::Process::OK_EXIT_NAME
static const char *const OK_EXIT_NAME
string to describe 0 exit value
Definition
Process.hpp:36
OksSystem::Process::exit_text
static const char * exit_text(int return_value)
the textual description of standard exit codes
Definition
Process.cpp:47
OksSystem::Process::join
int join(bool throw_non_zero=false) const
Waits for the process to terminate.
Definition
Process.cpp:151
OksSystem::Process::equals
bool equals(const Process &other) const
Comparison method.
Definition
Process.cpp:198
OksSystem::Process::TEST_BASE_VALUE
static const int TEST_BASE_VALUE
first value for test manager exit codes
Definition
Process.hpp:38
OksSystem::Process::signal
void signal(int signal) const
Sends a signal to the process.
Definition
Process.cpp:174
OksSystem::Process::m_process_id
pid_t m_process_id
process identifier
Definition
Process.hpp:32
OksSystem::Process::terminate
void terminate() const
Terminate the process.
Definition
Process.cpp:207
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:88
OksSystem::Process::TEST_MAX_VALUE
static const int TEST_MAX_VALUE
last value for test manager exit codes
Definition
Process.hpp:39
OksSystem::Process::exit_pretty
static std::string exit_pretty(int return_value)
Definition
Process.cpp:60
OksSystem::Process::exists
bool exists() const
Does the process exist?
Definition
Process.cpp:188
OksSystem::Process::TEST_EXITS_NAMES
static const char *const TEST_EXITS_NAMES[]
names for testing exit values
Definition
Process.hpp:36
OksSystem::Process::process_id
pid_t process_id() const
the id of the process
Definition
Process.cpp:249
OksSystem::Process::s_instance
static Process * s_instance
singleton instance
Definition
Process.hpp:41
OksSystem::Process::~Process
~Process()
Destructor.
Definition
Process.cpp:133
OksSystem::Process::m_process_name
std::string m_process_name
process name - only to be used for display
Definition
Process.hpp:33
OksSystem::Process::Process
Process()
Builds a process representing the current process.
Definition
Process.cpp:98
OksSystem::Process::to_string
std::string to_string() const
String conversion method.
Definition
Process.cpp:237
ers.hpp
OksSystem::operator!=
bool operator!=(const Host &a, const Host &b)
inequality operator
Definition
Host.cpp:188
OksSystem::operator==
bool operator==(const Host &a, const Host &b)
equality operator
Definition
Host.cpp:184
exceptions.hpp
Generated on
for DUNE-DAQ by
1.17.0