DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
OksSystem::Process Class Reference

Wrapper for process manipulation. More...

#include <Process.hpp>

Collaboration diagram for OksSystem::Process:
[legend]

Public Member Functions

 Process ()
 Builds a process representing the current process.
 
 Process (const Process &other)
 copy constructor
 
 Process (pid_t pid)
 Builds a process with process id pid.
 
 Process (pid_t pid, const std::string &name)
 Builds a process with process id and process name.
 
 ~Process () throw ()
 Destructor

 
 operator pid_t () const throw ()
 Cast operator to pid.
 
int join (bool throw_non_zero=false) const
 Waits for the process to terminate.
 
void signal (int signal) const
 Sends a signal to the process.
 
bool exists () const
 Does the process exist?
 
bool equals (const Process &other) const throw ()
 Comparison method.
 
void terminate () const
 Terminate the process.
 
pid_t process_id () const throw ()
 the id of the process
 
std::string to_string () const throw ()
 String conversion method.
 

Static Public Member Functions

static const char * exit_text (int return_value)
 the textual description of standard exit codes
 
static std::string exit_pretty (int return_value)
 
static const Processinstance () throw ()
 
static void set_name (const std::string &name) throw ()
 

Protected Attributes

pid_t m_process_id
 process identifier
 
std::string m_process_name
 process name - only to be used for display
 

Static Protected Attributes

static const char *const SYS_EXITS_NAMES []
 names for standard system exit values
 
static const char *const TEST_EXITS_NAMES [] = { "undefined test", "test failed", "test unresolved", "test untested", "unsupported test" }
 names for testing exit values
 
static const char *const OK_EXIT_NAME = "ok"
 string to describe 0 exit value
 
static const char *const SIGNAL_EXIT_NAME
 string to describe signal exited value
 
static const int TEST_BASE_VALUE = 182
 first value for test manager exit codes
 
static const int TEST_MAX_VALUE = 186
 last value for test manager exit codes
 
static const int TERMINATION_WAIT = 100000
 wait time before deciding a termination signal did not work
 
static Processs_instance = 0
 singleton instance
 

Detailed Description

Wrapper for process manipulation.

This class represents a process It offers basic methods to manipulate a process

Author
Matthias Wiesmann
Version
1.0

Definition at line 25 of file Process.hpp.

Constructor & Destructor Documentation

◆ Process() [1/4]

OksSystem::Process::Process ( )

Builds a process representing the current process.

Constructor from current process For the current process, the singleton factory method instance should be used

Definition at line 93 of file Process.cpp.

93 {
94 m_process_id = ::getpid();
95} // Process
pid_t m_process_id
process identifier
Definition Process.hpp:27

◆ Process() [2/4]

OksSystem::Process::Process ( const Process & other)

copy constructor

Copy constructor

Parameters
otheroriginal instance

Definition at line 101 of file Process.cpp.

101 {
102 m_process_id = other.m_process_id;
103 m_process_name = other.m_process_name;
104} // Process
std::string m_process_name
process name - only to be used for display
Definition Process.hpp:28

◆ Process() [3/4]

OksSystem::Process::Process ( pid_t id)

Builds a process with process id pid.

Constructor for pid

Parameters
idpid of the process

Definition at line 110 of file Process.cpp.

110 {
111 m_process_id = id;
112} // Process

◆ Process() [4/4]

OksSystem::Process::Process ( pid_t id,
const std::string & name )

Builds a process with process id and process name.

Constructor from pid and process name

Parameters
idpid of the process
namedisplay name of the process

Definition at line 119 of file Process.cpp.

119 {
120 m_process_id = id;
121 m_process_name = name;
122}

◆ ~Process()

OksSystem::Process::~Process ( )
throw ( )

Destructor

Destructor

Definition at line 128 of file Process.cpp.

128 {
129 m_process_id = 0;
130} // ~Process

Member Function Documentation

◆ equals()

bool OksSystem::Process::equals ( const Process & other) const
throw ( )

Comparison method.

Checks if two process are equal

Parameters
otherthe process to compare this process to
Returns
true if both proceses are equal (same pid).

Definition at line 193 of file Process.cpp.

193 {
194 return m_process_id == other.m_process_id;
195} // equals

◆ exists()

bool OksSystem::Process::exists ( ) const

Does the process exist?

Checks if the process actually exists on the host

Returns
true if the process exists
Note
currently the check is done by sending signal 0 to the process in case of failure, we know process does not exist

Definition at line 183 of file Process.cpp.

183 {
184 const int status = ::kill(m_process_id,0);
185 return (status>=0);
186} // exists

◆ exit_pretty()

std::string OksSystem::Process::exit_pretty ( int return_value)
static

Builds a pretty string for an exit value

Parameters
return_valuethe integer returned by a program
Returns
if the code is recognised by exit_text, this text and the number between parenthesis if not, the return_value converter into a string

Definition at line 55 of file Process.cpp.

55 {
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
static const char * exit_text(int return_value)
the textual description of standard exit codes
Definition Process.cpp:42

◆ exit_text()

const char * OksSystem::Process::exit_text ( int return_value)
static

the textual description of standard exit codes

Gives textual representation of exit values. This method recognises return codes from sysexits and the values returned by the text-manager

Parameters
return_valuethe integer returned by a program
Returns
a pointer to the string desribing the code, or a null pointer if the code is not recognised

Definition at line 42 of file Process.cpp.

42 {
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
static const char *const OK_EXIT_NAME
string to describe 0 exit value
Definition Process.hpp:31
static const int TEST_BASE_VALUE
first value for test manager exit codes
Definition Process.hpp:33
static const char *const SYS_EXITS_NAMES[]
names for standard system exit values
Definition Process.hpp:29
static const int TEST_MAX_VALUE
last value for test manager exit codes
Definition Process.hpp:34
static const char *const TEST_EXITS_NAMES[]
names for testing exit values
Definition Process.hpp:31

◆ instance()

const OksSystem::Process * OksSystem::Process::instance ( )
throw ( )
static

Default current process instance If needed the instance is created

Returns
singleton instance for current process

Definition at line 72 of file Process.cpp.

72 {
73 if (0==s_instance) {
75 } // if
76 return s_instance;
77} // instance
Wrapper for process manipulation.
Definition Process.hpp:25
static Process * s_instance
singleton instance
Definition Process.hpp:36

◆ join()

int OksSystem::Process::join ( bool throw_non_zero = false) const

Waits for the process to terminate.

Waits for a process to terminate. If the process terminates normally, the return value is the termination status of the process. If the process stops or is signaled, an SignalIssue is thrown.

Returns
the termination status of the process

Definition at line 146 of file Process.cpp.

146 {
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
#define ERS_PRECONDITION(expression)
#define ERS_HERE
static const Process * instance()
Definition Process.cpp:72
bool equals(const Process &other) const
Comparison method.
Definition Process.cpp:193
std::string to_string() const
String conversion method.
Definition Process.cpp:232

◆ operator pid_t()

OksSystem::Process::operator pid_t ( ) const
throw ( )

Cast operator to pid.

Cast operator - converts to process-id

Definition at line 135 of file Process.cpp.

135 {
136 return m_process_id;
137} // operator pid_t

◆ process_id()

pid_t OksSystem::Process::process_id ( ) const
throw ( )

the id of the process

Returns
the process id for the process

Definition at line 244 of file Process.cpp.

244{ return m_process_id;}

◆ set_name()

void OksSystem::Process::set_name ( const std::string & name)
throw ( )
static

Sets the process name of the current process

Definition at line 83 of file Process.cpp.

83 {
84 (void) instance();
86} // set_name

◆ signal()

void OksSystem::Process::signal ( int signal_number) const

Sends a signal to the process.

Sends a signal to the process

Parameters
signal_numbernumber of the signal

Definition at line 169 of file Process.cpp.

169 {
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

◆ terminate()

void OksSystem::Process::terminate ( ) const

Terminate the process.

Terminates a process Termination is implemented by

  • sending the TERM signal

Definition at line 202 of file Process.cpp.

202 {
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}
void signal(int signal) const
Sends a signal to the process.
Definition Process.cpp:169
bool exists() const
Does the process exist?
Definition Process.cpp:183

◆ to_string()

std::string OksSystem::Process::to_string ( ) const
throw ( )

String conversion method.

Builds a string description of the process

Returns
textual description

Definition at line 232 of file Process.cpp.

232 {
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

Member Data Documentation

◆ m_process_id

pid_t OksSystem::Process::m_process_id
protected

process identifier

Definition at line 27 of file Process.hpp.

◆ m_process_name

std::string OksSystem::Process::m_process_name
protected

process name - only to be used for display

Definition at line 28 of file Process.hpp.

◆ OK_EXIT_NAME

const char *const OksSystem::Process::OK_EXIT_NAME = "ok"
staticprotected

string to describe 0 exit value

Definition at line 31 of file Process.hpp.

◆ s_instance

OksSystem::Process * OksSystem::Process::s_instance = 0
staticprotected

singleton instance

Definition at line 36 of file Process.hpp.

◆ SIGNAL_EXIT_NAME

const char* const OksSystem::Process::SIGNAL_EXIT_NAME
staticprotected

string to describe signal exited value

Definition at line 32 of file Process.hpp.

◆ SYS_EXITS_NAMES

const char *const OksSystem::Process::SYS_EXITS_NAMES
staticprotected
Initial value:
= { "command line usage error", "data format error", "cannot open input", "addressee unknown", "host name unknown", "service unavailable", "internal software error", "okssystem error",
"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" }

names for standard system exit values

Definition at line 29 of file Process.hpp.

37 :
38 static const char* exit_text(int return_value) ;
39 static std::string exit_pretty(int return_value) ;
40 static const Process *instance() throw() ;
41 static void set_name(const std::string &name) throw() ;
42 Process();
43 Process(const Process &other) ;
44 Process(pid_t pid) ;
45 Process(pid_t pid, const std::string &name);
46 ~Process() throw() ;
47 operator pid_t() const throw() ;
48 int join(bool throw_non_zero=false) const ;
49 void signal(int signal) const ;
50 bool exists() const ;
51 bool equals(const Process &other) const throw() ;
52 void terminate() const ;
53 pid_t process_id() const throw();
54 std::string to_string() const throw();
55 } ; // Process
56
57} // OksSystem
58
59std::ostream& operator<<(std::ostream& stream, const OksSystem::Process &proc) ;
60bool operator ==(const OksSystem::Process &a, const OksSystem::Process &b) throw();
61bool operator !=(const OksSystem::Process &a, const OksSystem::Process &b) throw();
62
63#endif
void terminate() const
Terminate the process.
Definition Process.cpp:202
static void set_name(const std::string &name)
Definition Process.cpp:83
static std::string exit_pretty(int return_value)
Definition Process.cpp:55
pid_t process_id() const
the id of the process
Definition Process.cpp:244

◆ TERMINATION_WAIT

const int OksSystem::Process::TERMINATION_WAIT = 100000
staticprotected

wait time before deciding a termination signal did not work

Wait time before deciding a termination signal did not work (in nanoseconds)

Definition at line 35 of file Process.hpp.

◆ TEST_BASE_VALUE

const int OksSystem::Process::TEST_BASE_VALUE = 182
staticprotected

first value for test manager exit codes

Lowest value for test manager result code

Definition at line 33 of file Process.hpp.

◆ TEST_EXITS_NAMES

const char *const OksSystem::Process::TEST_EXITS_NAMES = { "undefined test", "test failed", "test unresolved", "test untested", "unsupported test" }
staticprotected

names for testing exit values

Definition at line 31 of file Process.hpp.

◆ TEST_MAX_VALUE

const int OksSystem::Process::TEST_MAX_VALUE = 186
staticprotected

last value for test manager exit codes

Highest value for test manager result code

Definition at line 34 of file Process.hpp.


The documentation for this class was generated from the following files: