1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
// Copyright 2010 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "utils/process/status.hpp"
extern "C" {
#include <sys/wait.h>
}
#include "utils/optional.ipp"
#include "utils/sanity.hpp"
namespace process = utils::process;
using utils::none;
using utils::optional;
#if !defined(WCOREDUMP)
# define WCOREDUMP(x) false
#endif
/// Constructs a new status object based on the status value of waitpid(2).
///
/// \param dead_pid_ The PID of the process this status belonged to.
/// \param stat_loc The status value returnd by waitpid(2).
process::status::status(const int dead_pid_, int stat_loc) :
_dead_pid(dead_pid_),
_exited(WIFEXITED(stat_loc) ?
optional< int >(WEXITSTATUS(stat_loc)) : none),
_signaled(WIFSIGNALED(stat_loc) ?
optional< std::pair< int, bool > >(
std::make_pair(WTERMSIG(stat_loc), WCOREDUMP(stat_loc))) :
none)
{
}
/// Constructs a new status object based on fake values.
///
/// \param exited_ If not none, specifies the exit status of the program.
/// \param signaled_ If not none, specifies the termination signal and whether
/// the process dumped core or not.
process::status::status(const optional< int >& exited_,
const optional< std::pair< int, bool > >& signaled_) :
_dead_pid(-1),
_exited(exited_),
_signaled(signaled_)
{
}
/// Constructs a new status object based on a fake exit status.
///
/// \param exitstatus_ The exit code of the process.
///
/// \return A status object with fake data.
process::status
process::status::fake_exited(const int exitstatus_)
{
return status(utils::make_optional(exitstatus_), none);
}
/// Constructs a new status object based on a fake exit status.
///
/// \param termsig_ The termination signal of the process.
/// \param coredump_ Whether the process dumped core or not.
///
/// \return A status object with fake data.
process::status
process::status::fake_signaled(const int termsig_, const bool coredump_)
{
return status(none, utils::make_optional(std::make_pair(termsig_,
coredump_)));
}
/// Returns the PID of the process this status was taken from.
///
/// Please note that the process is already dead and gone from the system. This
/// PID can only be used for informational reasons and not to address the
/// process in any way.
///
/// \return The PID of the original process.
int
process::status::dead_pid(void) const
{
return _dead_pid;
}
/// Returns whether the process exited cleanly or not.
///
/// \return True if the process exited cleanly, false otherwise.
bool
process::status::exited(void) const
{
return _exited;
}
/// Returns the exit code of the process.
///
/// \pre The process must have exited cleanly (i.e. exited() must be true).
///
/// \return The exit code.
int
process::status::exitstatus(void) const
{
PRE(exited());
return _exited.get();
}
/// Returns whether the process terminated due to a signal or not.
///
/// \return True if the process terminated due to a signal, false otherwise.
bool
process::status::signaled(void) const
{
return _signaled;
}
/// Returns the signal that terminated the process.
///
/// \pre The process must have terminated by a signal (i.e. signaled() must be
/// true.
///
/// \return The signal number.
int
process::status::termsig(void) const
{
PRE(signaled());
return _signaled.get().first;
}
/// Returns whether the process core dumped or not.
///
/// This functionality may be unsupported in some platforms. In such cases,
/// this method returns false unconditionally.
///
/// \pre The process must have terminated by a signal (i.e. signaled() must be
/// true.
///
/// \return True if the process dumped core, false otherwise.
bool
process::status::coredump(void) const
{
PRE(signaled());
return _signaled.get().second;
}
|