MySQL 8.4.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 <iostream>
34#include <memory>
35#include <optional>
36#include <ostream>
37#include <sstream>
38
39/**
40 * exit status of processes.
41 *
42 * a process can report its exit status:
43 *
44 * - exited (via exit(num))
45 * - terminated (via a signal, exception, ...)
46 * - stopped or continued (via SIGSTOP, SIGCONT)
47 */
49 public:
50 struct terminated_t {}; // helper tag for a terminated exit-status
51 struct stopped_t {}; // helper tag for a stopped exit-status
52 struct continued_t {}; // helper tag for a continued exit-status
53 struct native_t {}; // helper tag for a native exit-status
54 struct exited_t {}; // helper tag for a exited exit-status
55
56 /**
57 * construct a exit-status of a exited process.
58 */
59 constexpr ExitStatus(int exit_code) : ExitStatus{exited_t{}, exit_code} {}
60
61 constexpr ExitStatus(exited_t, int exit_code)
62 : status_kind_{StatusKind::kExited}, status_{exit_code} {}
63
64 constexpr ExitStatus(native_t, int native_exit_code)
65 : ExitStatus{from_native(native_exit_code)} {}
66
67 constexpr ExitStatus(terminated_t, int signum)
68 : status_kind_{StatusKind::kSignalled}, status_{signum} {}
69
70 constexpr ExitStatus(stopped_t, int signum)
71 : status_kind_{StatusKind::kStopped}, status_{signum} {}
72
74 : status_kind_{StatusKind::kContinued}, status_{0} {}
75
76 /**
77 * check if the status is a clean exit.
78 *
79 * if true, contains the exit-code.
80 */
81 constexpr std::optional<int> exited() const {
82 if (status_kind_ != StatusKind::kExited) return {};
83
84 return status_;
85 }
86
87 /**
88 * check if the status is a terminated exit.
89 *
90 * if true, contains the signal number used to terminate the process.
91 */
92 constexpr std::optional<int> terminated() const {
93 if (status_kind_ != StatusKind::kSignalled) return {};
94
95 return status_;
96 }
97
98 /**
99 * check if the status is a stopped process.
100 *
101 * if true, contains the signal number used to stop the process.
102 */
103 constexpr std::optional<int> stopped() const {
104 if (status_kind_ != StatusKind::kStopped) return {};
105
106 return status_;
107 }
108
109 /**
110 * check if the status is continued process.
111 */
112 constexpr bool continued() const {
114 }
115
116 friend bool operator==(const ExitStatus &a, const ExitStatus &b) {
117 return a.status_kind_ == b.status_kind_ && a.status_ == b.status_;
118 }
119
120 private:
122
124
126
127 static constexpr ExitStatus from_native(int native_exit_code) {
128#ifndef _WIN32
129 if (WIFSIGNALED(native_exit_code)) {
130 return {terminated_t{}, WTERMSIG(native_exit_code)};
131 } else if (WIFEXITED(native_exit_code)) {
132 return {exited_t{}, WEXITSTATUS(native_exit_code)};
133 } else if (WIFSTOPPED(native_exit_code)) {
134 return {stopped_t{}, WSTOPSIG(native_exit_code)};
135 } else if (WIFCONTINUED(native_exit_code)) {
136 return {continued_t{}};
137 } else {
138 // shouldn't happen
139 return {exited_t{}, native_exit_code};
140 }
141#else
142 // the lower-byte contains the exit-code, everything else is a NTSTATUS
143 if (native_exit_code > 0xff || native_exit_code < 0) {
144 return {terminated_t{}, native_exit_code};
145 } else {
146 return {exited_t{}, native_exit_code};
147 }
148#endif
149 }
150};
151
152inline std::ostream &operator<<(std::ostream &os, const ExitStatus &st) {
153 if (auto code = st.exited()) {
154 os << "Exit(" << *code << ")";
155 } else if (auto code = st.terminated()) {
156#ifndef _WIN32
157 os << "Terminated(signal=" << *code << ") " << strsignal(*code);
158#else
159 std::ostringstream hexed;
160 hexed << std::showbase << std::hex << *code;
161
162 os << "Terminated(exception=" << hexed.str() << ") "
163 << std::system_category().message(*code);
164#endif
165 } else if (auto code = st.stopped()) {
166#ifndef _WIN32
167 os << "Stopped(signal=" << *code << ") " << strsignal(*code);
168#else
169 os << "Stopped(signal=" << *code << ")";
170#endif
171 } else if (st.continued()) {
172 os << "Continued()";
173 }
174 return os;
175}
176
177#endif
exit status of processes.
Definition: exit_status.h:48
StatusKind status_kind_
Definition: exit_status.h:123
constexpr std::optional< int > stopped() const
check if the status is a stopped process.
Definition: exit_status.h:103
constexpr ExitStatus(terminated_t, int signum)
Definition: exit_status.h:67
constexpr bool continued() const
check if the status is continued process.
Definition: exit_status.h:112
constexpr ExitStatus(native_t, int native_exit_code)
Definition: exit_status.h:64
static constexpr ExitStatus from_native(int native_exit_code)
Definition: exit_status.h:127
StatusKind
Definition: exit_status.h:121
constexpr ExitStatus(exited_t, int exit_code)
Definition: exit_status.h:61
int status_
Definition: exit_status.h:125
constexpr ExitStatus(continued_t)
Definition: exit_status.h:73
constexpr std::optional< int > terminated() const
check if the status is a terminated exit.
Definition: exit_status.h:92
constexpr ExitStatus(stopped_t, int signum)
Definition: exit_status.h:70
friend bool operator==(const ExitStatus &a, const ExitStatus &b)
Definition: exit_status.h:116
constexpr ExitStatus(int exit_code)
construct a exit-status of a exited process.
Definition: exit_status.h:59
constexpr std::optional< int > exited() const
check if the status is a clean exit.
Definition: exit_status.h:81
#define WEXITSTATUS(stat_val)
Definition: client_priv.h:41
std::ostream & operator<<(std::ostream &os, const ExitStatus &st)
Definition: exit_status.h:152
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2870
Definition: exit_status.h:52
Definition: exit_status.h:54
Definition: exit_status.h:53
Definition: exit_status.h:51
Definition: exit_status.h:50