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/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
26#include "okssystem/Process.hpp"
27
28
31
32const int OksSystem::Process::TERMINATION_WAIT = 100000;
33
34const 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" };
36const char * const OksSystem::Process::TEST_EXITS_NAMES[] = { "undefined test", "test failed", "test unresolved", "test untested", "unsupported test" };
37const char * const OksSystem::Process::OK_EXIT_NAME = "ok";
38
40
46
47const 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
60std::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
78 if (0==s_instance) {
80 } // if
81 return s_instance;
82} // instance
83
84
87
88void 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
99 m_process_id = ::getpid();
100} // Process
101
105
109} // Process
110
114
116 m_process_id = id;
117} // Process
118
123
124OksSystem::Process::Process(pid_t id, const std::string &name) {
125 m_process_id = id;
126 m_process_name = name;
127}
128
129
132
134 m_process_id = 0;
135} // ~Process
136
139
140OksSystem::Process::operator pid_t() const throw() {
141 return m_process_id;
142} // operator pid_t
143
144
150
151int OksSystem::Process::join(bool throw_non_zero) const {
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
174void 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
189 const int status = ::kill(m_process_id,0);
190 return (status>=0);
191} // exists
192
197
198bool OksSystem::Process::equals(const Process &other) const throw() {
199 return m_process_id == other.m_process_id;
200} // equals
201
206
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
237std::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
249pid_t OksSystem::Process::process_id() const throw() { return m_process_id;}
250
256
257std::ostream& operator<<(std::ostream& out, const OksSystem::Process &proc) {
258 out << proc.to_string();
259 return out;
260} // operator<<
261
268
269bool operator ==(const OksSystem::Process &a, const OksSystem::Process &b) throw() {
270 return a.equals(b);
271} // operator ==
272
279
280bool operator !=(const OksSystem::Process &a, const OksSystem::Process &b) throw() {
281 return ! a.equals(b);
282} // operator !=
283
284
#define ERS_PRECONDITION(expression)
#define ERS_HERE
void operator<<(TraceStreamer &x, const ers::Issue &r)
Definition Logger.hxx:102
Wrapper for process manipulation.
Definition Process.hpp:30
static const int TERMINATION_WAIT
wait time before deciding a termination signal did not work
Definition Process.hpp:40
static const Process * instance()
Definition Process.cpp:77
static const char *const OK_EXIT_NAME
string to describe 0 exit value
Definition Process.hpp:36
static const char * exit_text(int return_value)
the textual description of standard exit codes
Definition Process.cpp:47
int join(bool throw_non_zero=false) const
Waits for the process to terminate.
Definition Process.cpp:151
bool equals(const Process &other) const
Comparison method.
Definition Process.cpp:198
static const int TEST_BASE_VALUE
first value for test manager exit codes
Definition Process.hpp:38
void signal(int signal) const
Sends a signal to the process.
Definition Process.cpp:174
pid_t m_process_id
process identifier
Definition Process.hpp:32
void terminate() const
Terminate the process.
Definition Process.cpp:207
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:88
static const int TEST_MAX_VALUE
last value for test manager exit codes
Definition Process.hpp:39
static std::string exit_pretty(int return_value)
Definition Process.cpp:60
bool exists() const
Does the process exist?
Definition Process.cpp:188
static const char *const TEST_EXITS_NAMES[]
names for testing exit values
Definition Process.hpp:36
pid_t process_id() const
the id of the process
Definition Process.cpp:249
static Process * s_instance
singleton instance
Definition Process.hpp:41
~Process()
Destructor.
Definition Process.cpp:133
std::string m_process_name
process name - only to be used for display
Definition Process.hpp:33
Process()
Builds a process representing the current process.
Definition Process.cpp:98
std::string to_string() const
String conversion method.
Definition Process.cpp:237
bool operator!=(const Host &a, const Host &b)
inequality operator
Definition Host.cpp:188
bool operator==(const Host &a, const Host &b)
equality operator
Definition Host.cpp:184