DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Process.cpp
Go to the documentation of this file.
1/*
2 * Process.cxx
3 * OksSystem
4 *
5 * Created by Matthias Wiesmann on 18.01.05.
6 * Copyright 2005 CERN. All rights reserved.
7 *
8 */
9
10#include <sys/wait.h>
11#include <sys/types.h>
12#include <unistd.h>
13#include <sysexits.h>
14
15#include <iostream>
16#include <sstream>
17
18#include "ers/ers.hpp"
19
21#include "okssystem/Process.hpp"
22
23
27const int OksSystem::Process::TERMINATION_WAIT = 100000;
29const 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",
30 "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" };
31const char * const OksSystem::Process::TEST_EXITS_NAMES[] = { "undefined test", "test failed", "test unresolved", "test untested", "unsupported test" };
32const char * const OksSystem::Process::OK_EXIT_NAME = "ok";
33
35
42const char* OksSystem::Process::exit_text(int return_value) {
43 if ( 0==return_value) return OK_EXIT_NAME;
44 if ((return_value >= EX__BASE) && (return_value <= EX__MAX)) return SYS_EXITS_NAMES[return_value-EX__BASE];
45 if ((return_value >= TEST_BASE_VALUE) && (return_value <= TEST_MAX_VALUE)) return TEST_EXITS_NAMES[return_value-TEST_BASE_VALUE];
46 return 0;
47} // return_text
48
55std::string OksSystem::Process::exit_pretty(int return_value) {
56 std::ostringstream stream;
57 const char* text = OksSystem::Process::exit_text(return_value);
58 if (text) {
59 stream << text << '(' << return_value << ')';
60 } else {
61 stream << return_value ;
62 }
63 return stream.str();
64} // exit_string
65
66
73 if (0==s_instance) {
74 s_instance = new OksSystem::Process();
75 } // if
76 return s_instance;
77} // instance
78
79
83void OksSystem::Process::set_name(const std::string &name) throw() {
84 (void) instance();
85 s_instance->m_process_name = name;
86} // set_name
87
88
94 m_process_id = ::getpid();
95} // Process
96
102 m_process_id = other.m_process_id;
103 m_process_name = other.m_process_name;
104} // Process
105
111 m_process_id = id;
112} // Process
113
119OksSystem::Process::Process(pid_t id, const std::string &name) {
120 m_process_id = id;
121 m_process_name = name;
122}
123
124
129 m_process_id = 0;
130} // ~Process
131
135OksSystem::Process::operator pid_t() const throw() {
136 return m_process_id;
137} // operator pid_t
138
139
146int OksSystem::Process::join(bool throw_non_zero) const {
147 ERS_PRECONDITION(! equals(*instance()));
148 int status;
149 errno = 0;
150 pid_t pid = ::waitpid(m_process_id,&status,0);
151 if (pid!=m_process_id) {
152 std::string message = "on process " + this->to_string();
153 throw OksSystem::OksSystemCallIssue( ERS_HERE, errno, "waitpid", message.c_str() );
154 } // if
155 if (WIFEXITED(status)) {
156 int exit_status = WEXITSTATUS(status);
157 if ((throw_non_zero==false) || (exit_status==0)) return exit_status;
158 throw OksSystem::TerminationIssue(ERS_HERE, errno, exit_status);
159 } // exit status
160 if (WIFSIGNALED(status)) throw OksSystem::SignalIssue(ERS_HERE,errno,WTERMSIG(status));
161 if (WIFSTOPPED(status)) throw OksSystem::SignalIssue(ERS_HERE,errno,WSTOPSIG(status));
162 return WEXITSTATUS(status);
163} // join
164
169void OksSystem::Process::signal(int signal_number) const {
170 const int status = ::kill(m_process_id,signal_number);
171 if (status < 0) {
172 std::string message = "on process " + this->to_string();
173 throw OksSystem::OksSystemCallIssue(ERS_HERE, errno, "kill", message.c_str());
174 }
175} // signal
176
184 const int status = ::kill(m_process_id,0);
185 return (status>=0);
186} // exists
187
193bool OksSystem::Process::equals(const Process &other) const throw() {
194 return m_process_id == other.m_process_id;
195} // equals
196
203 /*
204 struct timespec wait_time;
205 struct timespec remain_time;
206 wait_time.tv_sec = 0;
207 wait_time.tv_nsec = TERMINATION_WAIT;
208 */
209 if (exists()) {
210 signal(SIGTERM);
211 }
212 return;
213}
214
215/* This is a piece of crap
216 nanosleep(&wait_time,&remain_time);
217 if (exists()) {
218 signal(SIGQUIT);
219 } else {
220 return;
221 } //
222 nanosleep(&wait_time,&remain_time);
223 if (exists()) {
224 signal(SIGKILL);
225 } // exists
226} // terminate
227*/
232std::string OksSystem::Process::to_string() const throw() {
233 std::ostringstream stream;
234 if (! m_process_name.empty()) {
235 stream << m_process_name << ' ';
236 }
237 stream << "pid: " << m_process_id;
238 return stream.str();
239} // to_string
240
244pid_t OksSystem::Process::process_id() const throw() { return m_process_id;}
245
252std::ostream& operator<<(std::ostream& out, const OksSystem::Process &proc) {
253 out << proc.to_string();
254 return out;
255} // operator<<
256
264bool operator ==(const OksSystem::Process &a, const OksSystem::Process &b) throw() {
265 return a.equals(b);
266} // operator ==
267
275bool operator !=(const OksSystem::Process &a, const OksSystem::Process &b) throw() {
276 return ! a.equals(b);
277} // operator !=
278
279
#define ERS_PRECONDITION(expression)
std::ostream & operator<<(std::ostream &stream, const OksSystem::File &file)
Definition File.cpp:852
#define ERS_HERE
Wrapper for process manipulation.
Definition Process.hpp:25
static const int TERMINATION_WAIT
wait time before deciding a termination signal did not work
Definition Process.hpp:35
static const Process * instance()
Definition Process.cpp:72
static const char *const OK_EXIT_NAME
string to describe 0 exit value
Definition Process.hpp:31
static const char * exit_text(int return_value)
the textual description of standard exit codes
Definition Process.cpp:42
int join(bool throw_non_zero=false) const
Waits for the process to terminate.
Definition Process.cpp:146
bool equals(const Process &other) const
Comparison method.
Definition Process.cpp:193
static const int TEST_BASE_VALUE
first value for test manager exit codes
Definition Process.hpp:33
void signal(int signal) const
Sends a signal to the process.
Definition Process.cpp:169
pid_t m_process_id
process identifier
Definition Process.hpp:27
void terminate() const
Terminate the process.
Definition Process.cpp:202
static const char *const SYS_EXITS_NAMES[]
names for standard system exit values
Definition Process.hpp:29
static void set_name(const std::string &name)
Definition Process.cpp:83
static const int TEST_MAX_VALUE
last value for test manager exit codes
Definition Process.hpp:34
static std::string exit_pretty(int return_value)
Definition Process.cpp:55
bool exists() const
Does the process exist?
Definition Process.cpp:183
static const char *const TEST_EXITS_NAMES[]
names for testing exit values
Definition Process.hpp:31
pid_t process_id() const
the id of the process
Definition Process.cpp:244
static Process * s_instance
singleton instance
Definition Process.hpp:36
~Process()
Destructor
Definition Process.cpp:128
std::string m_process_name
process name - only to be used for display
Definition Process.hpp:28
Process()
Builds a process representing the current process.
Definition Process.cpp:93
std::string to_string() const
String conversion method.
Definition Process.cpp:232
bool operator!=(const Host &a, const Host &b)
inequality operator
Definition Host.cpp:183
bool operator==(const Host &a, const Host &b)
equality operator
Definition Host.cpp:179