DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
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"
27
30
32 100000;
33
34const 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};
44const char* const OksSystem::Process::TEST_EXITS_NAMES[] = { "undefined test",
45 "test failed",
46 "test unresolved",
47 "test untested",
48 "unsupported test" };
49const char* const OksSystem::Process::OK_EXIT_NAME = "ok";
50
52
58
59const char*
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
77std::string
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
97{
98 if (0 == s_instance) {
100 } // if
101 return s_instance;
102} // instance
103
106
107void
108OksSystem::Process::set_name(const std::string& name) throw()
109{
110 (void)instance();
111 s_instance->m_process_name = name;
112} // set_name
113
117
119{
120 m_process_id = ::getpid();
121} // Process
122
126
128{
131} // Process
132
136
138{
139 m_process_id = id;
140} // Process
141
146
147OksSystem::Process::Process(pid_t id, const std::string& name)
148{
149 m_process_id = id;
150 m_process_name = name;
151}
152
155
157{
158 m_process_id = 0;
159} // ~Process
160
163
164OksSystem::Process::operator pid_t() const throw()
165{
166 return m_process_id;
167} // operator pid_t
168
174
175int
176OksSystem::Process::join(bool throw_non_zero) const
177{
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
203void
204OksSystem::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
219bool
221{
222 const int status = ::kill(m_process_id, 0);
223 return (status >= 0);
224} // exists
225
230
231bool
232OksSystem::Process::equals(const Process& other) const throw()
233{
234 return m_process_id == other.m_process_id;
235} // equals
236
241
242void
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
274std::string
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
288pid_t
290{
291 return m_process_id;
292}
293
299
300std::ostream&
301operator<<(std::ostream& out, const OksSystem::Process& proc)
302{
303 out << proc.to_string();
304 return out;
305} // operator<<
306
313
314bool
316{
317 return a.equals(b);
318} // operator ==
319
326
327bool
329{
330 return !a.equals(b);
331} // operator !=
#define ERS_PRECONDITION(expression)
bool operator==(const OksSystem::File &a, const OksSystem::File &b)
Definition File.cpp:956
bool operator!=(const OksSystem::File &a, const OksSystem::File &b)
Definition File.cpp:962
#define ERS_HERE
void operator<<(TraceStreamer &x, const ers::Issue &r)
Definition Logger.hxx:128
Wrapper for process manipulation.
Definition Process.hpp:31
static const int TERMINATION_WAIT
wait time before deciding a termination signal did not work
Definition Process.hpp:41
static const Process * instance()
Definition Process.cpp:96
static const char *const OK_EXIT_NAME
string to describe 0 exit value
Definition Process.hpp:37
static const char * exit_text(int return_value)
the textual description of standard exit codes
Definition Process.cpp:60
int join(bool throw_non_zero=false) const
Waits for the process to terminate.
Definition Process.cpp:176
bool equals(const Process &other) const
Comparison method.
Definition Process.cpp:232
static const int TEST_BASE_VALUE
first value for test manager exit codes
Definition Process.hpp:39
void signal(int signal) const
Sends a signal to the process.
Definition Process.cpp:204
pid_t m_process_id
process identifier
Definition Process.hpp:33
void terminate() const
Terminate the process.
Definition Process.cpp:243
static const char *const SYS_EXITS_NAMES[]
names for standard system exit values
Definition Process.hpp:34
static void set_name(const std::string &name)
Definition Process.cpp:108
static const int TEST_MAX_VALUE
last value for test manager exit codes
Definition Process.hpp:40
static std::string exit_pretty(int return_value)
Definition Process.cpp:78
bool exists() const
Does the process exist?
Definition Process.cpp:220
static const char *const TEST_EXITS_NAMES[]
names for testing exit values
Definition Process.hpp:44
pid_t process_id() const
the id of the process
Definition Process.cpp:289
static Process * s_instance
singleton instance
Definition Process.hpp:42
~Process()
Destructor.
Definition Process.cpp:156
std::string m_process_name
process name - only to be used for display
Definition Process.hpp:34
Process()
Builds a process representing the current process.
Definition Process.cpp:118
std::string to_string() const
String conversion method.
Definition Process.cpp:275