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