MySQL 9.1.0
Source Code Documentation
exit_status.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef MYSQL_HARNESS_EXIT_STATUS_H_
25#define MYSQL_HARNESS_EXIT_STATUS_H_
26
27#ifndef _WIN32
28#include <sys/wait.h>
29#endif
30
31#include <cstdlib>
32#include <cstring> // strsignal
33#include <memory>
34#include <optional>
35#include <ostream>
36#include <sstream>
37
38/**
39 * exit status of processes.
40 *
41 * a process can report its exit status:
42 *
43 * - exited (via exit(num))
44 * - terminated (via a signal, exception, ...)
45 * - stopped or continued (via SIGSTOP, SIGCONT)
46 */
48 public:
49 struct terminated_t {}; // helper tag for a terminated exit-status
50 struct stopped_t {}; // helper tag for a stopped exit-status
51 struct continued_t {}; // helper tag for a continued exit-status
52 struct native_t {}; // helper tag for a native exit-status
53 struct exited_t {}; // helper tag for a exited exit-status
54
55 /**
56 * construct a exit-status of a exited process.
57 */
58 constexpr ExitStatus(int exit_code) : ExitStatus{exited_t{}, exit_code} {}
59
60 constexpr ExitStatus(exited_t, int exit_code)
61 : status_kind_{StatusKind::kExited}, status_{exit_code} {}
62
63 constexpr ExitStatus(native_t, int native_exit_code)
64 : ExitStatus{from_native(native_exit_code)} {}
65
66 constexpr ExitStatus(terminated_t, int signum)
67 : status_kind_{StatusKind::kSignalled}, status_{signum} {}
68
69 constexpr ExitStatus(stopped_t, int signum)
70 : status_kind_{StatusKind::kStopped}, status_{signum} {}
71
73 : status_kind_{StatusKind::kContinued}, status_{0} {}
74
75 /**
76 * check if the status is a clean exit.
77 *
78 * if true, contains the exit-code.
79 */
80 constexpr std::optional<int> exited() const {
81 if (status_kind_ != StatusKind::kExited) return {};
82
83 return status_;
84 }
85
86 /**
87 * check if the status is a terminated exit.
88 *
89 * if true, contains the signal number used to terminate the process.
90 */
91 constexpr std::optional<int> terminated() const {
92 if (status_kind_ != StatusKind::kSignalled) return {};
93
94 return status_;
95 }
96
97 /**
98 * check if the status is a stopped process.
99 *
100 * if true, contains the signal number used to stop the process.
101 */
102 constexpr std::optional<int> stopped() const {
103 if (status_kind_ != StatusKind::kStopped) return {};
104
105 return status_;
106 }
107
108 /**
109 * check if the status is continued process.
110 */
111 constexpr bool continued() const {
113 }
114
115 friend bool operator==(const ExitStatus &a, const ExitStatus &b) {
116 return a.status_kind_ == b.status_kind_ && a.status_ == b.status_;
117 }
118
119 private:
121
123
125
126 static constexpr ExitStatus from_native(int native_exit_code) {
127#ifndef _WIN32
128 if (WIFSIGNALED(native_exit_code)) {
129 return {terminated_t{}, WTERMSIG(native_exit_code)};
130 } else if (WIFEXITED(native_exit_code)) {
131 return {exited_t{}, WEXITSTATUS(native_exit_code)};
132 } else if (WIFSTOPPED(native_exit_code)) {
133 return {stopped_t{}, WSTOPSIG(native_exit_code)};
134 } else if (WIFCONTINUED(native_exit_code)) {
135 return {continued_t{}};
136 } else {
137 // shouldn't happen
138 return {exited_t{}, native_exit_code};
139 }
140#else
141 // the lower-byte contains the exit-code, everything else is a NTSTATUS
142 if (native_exit_code > 0xff || native_exit_code < 0) {
143 return {terminated_t{}, native_exit_code};
144 } else {
145 return {exited_t{}, native_exit_code};
146 }
147#endif
148 }
149};
150
151inline std::ostream &operator<<(std::ostream &os, const ExitStatus &st) {
152 if (auto code = st.exited()) {
153 os << "Exit(" << *code << ")";
154 } else if (auto code = st.terminated()) {
155#ifndef _WIN32
156 os << "Terminated(signal=" << *code << ") " << strsignal(*code);
157#else
158 std::ostringstream hexed;
159 hexed << std::showbase << std::hex << *code;
160
161 os << "Terminated(exception=" << hexed.str() << ") "
162 << std::system_category().message(*code);
163#endif
164 } else if (auto code = st.stopped()) {
165#ifndef _WIN32
166 os << "Stopped(signal=" << *code << ") " << strsignal(*code);
167#else
168 os << "Stopped(signal=" << *code << ")";
169#endif
170 } else if (st.continued()) {
171 os << "Continued()";
172 }
173 return os;
174}
175
176#endif
exit status of processes.
Definition: exit_status.h:47
StatusKind status_kind_
Definition: exit_status.h:122
constexpr std::optional< int > stopped() const
check if the status is a stopped process.
Definition: exit_status.h:102
constexpr ExitStatus(terminated_t, int signum)
Definition: exit_status.h:66
constexpr bool continued() const
check if the status is continued process.
Definition: exit_status.h:111
constexpr ExitStatus(native_t, int native_exit_code)
Definition: exit_status.h:63
static constexpr ExitStatus from_native(int native_exit_code)
Definition: exit_status.h:126
StatusKind
Definition: exit_status.h:120
constexpr ExitStatus(exited_t, int exit_code)
Definition: exit_status.h:60
int status_
Definition: exit_status.h:124
constexpr ExitStatus(continued_t)
Definition: exit_status.h:72
constexpr std::optional< int > terminated() const
check if the status is a terminated exit.
Definition: exit_status.h:91
constexpr ExitStatus(stopped_t, int signum)
Definition: exit_status.h:69
friend bool operator==(const ExitStatus &a, const ExitStatus &b)
Definition: exit_status.h:115
constexpr ExitStatus(int exit_code)
construct a exit-status of a exited process.
Definition: exit_status.h:58
constexpr std::optional< int > exited() const
check if the status is a clean exit.
Definition: exit_status.h:80
#define WEXITSTATUS(stat_val)
Definition: client_priv.h:41
std::ostream & operator<<(std::ostream &os, const ExitStatus &st)
Definition: exit_status.h:151
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2872
Definition: exit_status.h:51
Definition: exit_status.h:53
Definition: exit_status.h:52
Definition: exit_status.h:50
Definition: exit_status.h:49