1 // Copyright 2010 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/process/status.hpp"
35 #include "utils/optional.ipp"
36 #include "utils/sanity.hpp"
38 namespace process
= utils::process
;
41 using utils::optional
;
43 #if !defined(WCOREDUMP)
44 # define WCOREDUMP(x) false
48 /// Constructs a new status object based on the status value of waitpid(2).
50 /// \param dead_pid_ The PID of the process this status belonged to.
51 /// \param stat_loc The status value returnd by waitpid(2).
52 process::status::status(const int dead_pid_
, int stat_loc
) :
54 _exited(WIFEXITED(stat_loc
) ?
55 optional
< int >(WEXITSTATUS(stat_loc
)) : none
),
56 _signaled(WIFSIGNALED(stat_loc
) ?
57 optional
< std::pair
< int, bool > >(
58 std::make_pair(WTERMSIG(stat_loc
), WCOREDUMP(stat_loc
))) :
64 /// Constructs a new status object based on fake values.
66 /// \param exited_ If not none, specifies the exit status of the program.
67 /// \param signaled_ If not none, specifies the termination signal and whether
68 /// the process dumped core or not.
69 process::status::status(const optional
< int >& exited_
,
70 const optional
< std::pair
< int, bool > >& signaled_
) :
78 /// Constructs a new status object based on a fake exit status.
80 /// \param exitstatus_ The exit code of the process.
82 /// \return A status object with fake data.
84 process::status::fake_exited(const int exitstatus_
)
86 return status(utils::make_optional(exitstatus_
), none
);
90 /// Constructs a new status object based on a fake exit status.
92 /// \param termsig_ The termination signal of the process.
93 /// \param coredump_ Whether the process dumped core or not.
95 /// \return A status object with fake data.
97 process::status::fake_signaled(const int termsig_
, const bool coredump_
)
99 return status(none
, utils::make_optional(std::make_pair(termsig_
,
104 /// Returns the PID of the process this status was taken from.
106 /// Please note that the process is already dead and gone from the system. This
107 /// PID can only be used for informational reasons and not to address the
108 /// process in any way.
110 /// \return The PID of the original process.
112 process::status::dead_pid(void) const
118 /// Returns whether the process exited cleanly or not.
120 /// \return True if the process exited cleanly, false otherwise.
122 process::status::exited(void) const
128 /// Returns the exit code of the process.
130 /// \pre The process must have exited cleanly (i.e. exited() must be true).
132 /// \return The exit code.
134 process::status::exitstatus(void) const
137 return _exited
.get();
141 /// Returns whether the process terminated due to a signal or not.
143 /// \return True if the process terminated due to a signal, false otherwise.
145 process::status::signaled(void) const
151 /// Returns the signal that terminated the process.
153 /// \pre The process must have terminated by a signal (i.e. signaled() must be
156 /// \return The signal number.
158 process::status::termsig(void) const
161 return _signaled
.get().first
;
165 /// Returns whether the process core dumped or not.
167 /// This functionality may be unsupported in some platforms. In such cases,
168 /// this method returns false unconditionally.
170 /// \pre The process must have terminated by a signal (i.e. signaled() must be
173 /// \return True if the process dumped core, false otherwise.
175 process::status::coredump(void) const
178 return _signaled
.get().second
;