blob: 926f6f1c1f07c3892d971ddfd7ab117eb16e947a [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===---------------------- system_error.cpp ------------------------------===//
2//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00004//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
Jonathan Roelofsa409d592014-09-02 20:34:23 +000010#include "__config"
Dan Alberta76dfbd2015-01-06 17:34:51 +000011
12#define _LIBCPP_BUILDING_SYSTEM_ERROR
Howard Hinnant3e519522010-05-11 19:42:16 +000013#include "system_error"
Dan Alberta76dfbd2015-01-06 17:34:51 +000014
Eric Fiseliere8fd1642015-08-18 21:08:54 +000015#include "include/config_elast.h"
Eric Fiselier9778a6d2016-06-14 03:45:31 +000016#include "cerrno"
Howard Hinnant3e519522010-05-11 19:42:16 +000017#include "cstring"
Eric Fiselier9778a6d2016-06-14 03:45:31 +000018#include "cstdio"
19#include "cstdlib"
20#include "cassert"
Dan Alberta76dfbd2015-01-06 17:34:51 +000021#include "string"
Eric Fiselier9778a6d2016-06-14 03:45:31 +000022#include "string.h"
Howard Hinnant3e519522010-05-11 19:42:16 +000023
Dan Albert953d7d42016-06-15 20:20:32 +000024#if defined(__ANDROID__)
25#include <android/api-level.h>
26#endif
27
Howard Hinnant3e519522010-05-11 19:42:16 +000028_LIBCPP_BEGIN_NAMESPACE_STD
29
30// class error_category
31
Howard Hinnanta62f2892011-05-26 19:48:01 +000032error_category::error_category() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000033{
34}
35
Howard Hinnanta62f2892011-05-26 19:48:01 +000036error_category::~error_category() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000037{
38}
39
40error_condition
Howard Hinnanta62f2892011-05-26 19:48:01 +000041error_category::default_error_condition(int ev) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000042{
43 return error_condition(ev, *this);
44}
45
46bool
Howard Hinnanta62f2892011-05-26 19:48:01 +000047error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000048{
49 return default_error_condition(code) == condition;
50}
51
52bool
Howard Hinnanta62f2892011-05-26 19:48:01 +000053error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000054{
55 return *this == code.category() && code.value() == condition;
56}
57
Eric Fiselier9778a6d2016-06-14 03:45:31 +000058namespace {
59
60// GLIBC also uses 1024 as the maximum buffer size internally.
61constexpr size_t strerror_buff_size = 1024;
62
63string do_strerror_r(int ev);
64
Dan Albert953d7d42016-06-15 20:20:32 +000065#if defined(__linux__) && !defined(_LIBCPP_HAS_MUSL_LIBC) \
66 && (!defined(__ANDROID__) || __ANDROID_API__ >= 23)
Eric Fiselier9778a6d2016-06-14 03:45:31 +000067// GNU Extended version
68string do_strerror_r(int ev) {
69 char buffer[strerror_buff_size];
70 char* ret = ::strerror_r(ev, buffer, strerror_buff_size);
71 return string(ret);
72}
73#else
74// POSIX version
75string do_strerror_r(int ev) {
76 char buffer[strerror_buff_size];
77 const int old_errno = errno;
Eric Fiselier61df7902016-06-14 06:08:10 +000078 int ret;
79 if ((ret = ::strerror_r(ev, buffer, strerror_buff_size)) != 0) {
Eric Fiselier79e05742016-06-14 06:03:20 +000080 // If `ret == -1` then the error is specified using `errno`, otherwise
81 // `ret` represents the error.
82 const int new_errno = ret == -1 ? errno : ret;
Eric Fiselier9778a6d2016-06-14 03:45:31 +000083 errno = old_errno;
84 if (new_errno == EINVAL) {
85 std::snprintf(buffer, strerror_buff_size, "Unknown error %d", ev);
86 return string(buffer);
87 } else {
88 assert(new_errno == ERANGE);
89 // FIXME maybe? 'strerror_buff_size' is likely to exceed the
90 // maximum error size so ERANGE shouldn't be returned.
91 std::abort();
92 }
93 }
94 return string(buffer);
95}
96#endif
97
98} // end namespace
99
Howard Hinnant3e519522010-05-11 19:42:16 +0000100string
101__do_message::message(int ev) const
102{
Eric Fiselier9778a6d2016-06-14 03:45:31 +0000103#if defined(_LIBCPP_HAS_NO_THREADS)
104 return string(::strerror(ev));
105#else
106 return do_strerror_r(ev);
107#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000108}
109
110class _LIBCPP_HIDDEN __generic_error_category
111 : public __do_message
112{
113public:
Howard Hinnanta62f2892011-05-26 19:48:01 +0000114 virtual const char* name() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000115 virtual string message(int ev) const;
116};
117
118const char*
Howard Hinnanta62f2892011-05-26 19:48:01 +0000119__generic_error_category::name() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000120{
121 return "generic";
122}
123
124string
125__generic_error_category::message(int ev) const
126{
Jonathan Roelofsa409d592014-09-02 20:34:23 +0000127#ifdef _LIBCPP_ELAST
128 if (ev > _LIBCPP_ELAST)
Howard Hinnant128ba712010-05-24 17:49:41 +0000129 return string("unspecified generic_category error");
Jonathan Roelofsa409d592014-09-02 20:34:23 +0000130#endif // _LIBCPP_ELAST
Howard Hinnant128ba712010-05-24 17:49:41 +0000131 return __do_message::message(ev);
Howard Hinnant3e519522010-05-11 19:42:16 +0000132}
133
134const error_category&
Howard Hinnanta62f2892011-05-26 19:48:01 +0000135generic_category() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000136{
137 static __generic_error_category s;
138 return s;
139}
140
141class _LIBCPP_HIDDEN __system_error_category
142 : public __do_message
143{
144public:
Howard Hinnanta62f2892011-05-26 19:48:01 +0000145 virtual const char* name() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000146 virtual string message(int ev) const;
Howard Hinnanta62f2892011-05-26 19:48:01 +0000147 virtual error_condition default_error_condition(int ev) const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +0000148};
149
150const char*
Howard Hinnanta62f2892011-05-26 19:48:01 +0000151__system_error_category::name() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000152{
153 return "system";
154}
155
156string
157__system_error_category::message(int ev) const
158{
Jonathan Roelofsa409d592014-09-02 20:34:23 +0000159#ifdef _LIBCPP_ELAST
160 if (ev > _LIBCPP_ELAST)
Howard Hinnant128ba712010-05-24 17:49:41 +0000161 return string("unspecified system_category error");
Jonathan Roelofsa409d592014-09-02 20:34:23 +0000162#endif // _LIBCPP_ELAST
Howard Hinnant128ba712010-05-24 17:49:41 +0000163 return __do_message::message(ev);
Howard Hinnant3e519522010-05-11 19:42:16 +0000164}
165
166error_condition
Howard Hinnanta62f2892011-05-26 19:48:01 +0000167__system_error_category::default_error_condition(int ev) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000168{
Jonathan Roelofsa409d592014-09-02 20:34:23 +0000169#ifdef _LIBCPP_ELAST
170 if (ev > _LIBCPP_ELAST)
Howard Hinnant128ba712010-05-24 17:49:41 +0000171 return error_condition(ev, system_category());
Jonathan Roelofsa409d592014-09-02 20:34:23 +0000172#endif // _LIBCPP_ELAST
Howard Hinnant128ba712010-05-24 17:49:41 +0000173 return error_condition(ev, generic_category());
Howard Hinnant3e519522010-05-11 19:42:16 +0000174}
175
176const error_category&
Howard Hinnanta62f2892011-05-26 19:48:01 +0000177system_category() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000178{
179 static __system_error_category s;
180 return s;
181}
182
183// error_condition
184
185string
186error_condition::message() const
187{
188 return __cat_->message(__val_);
189}
190
191// error_code
192
193string
194error_code::message() const
195{
196 return __cat_->message(__val_);
197}
198
199// system_error
200
201string
202system_error::__init(const error_code& ec, string what_arg)
203{
204 if (ec)
205 {
206 if (!what_arg.empty())
207 what_arg += ": ";
208 what_arg += ec.message();
209 }
Richard Trieu1c545ba2015-04-30 21:47:28 +0000210 return what_arg;
Howard Hinnant3e519522010-05-11 19:42:16 +0000211}
212
213system_error::system_error(error_code ec, const string& what_arg)
214 : runtime_error(__init(ec, what_arg)),
215 __ec_(ec)
216{
217}
218
219system_error::system_error(error_code ec, const char* what_arg)
220 : runtime_error(__init(ec, what_arg)),
221 __ec_(ec)
222{
223}
224
225system_error::system_error(error_code ec)
226 : runtime_error(__init(ec, "")),
227 __ec_(ec)
228{
229}
230
231system_error::system_error(int ev, const error_category& ecat, const string& what_arg)
232 : runtime_error(__init(error_code(ev, ecat), what_arg)),
233 __ec_(error_code(ev, ecat))
234{
235}
236
237system_error::system_error(int ev, const error_category& ecat, const char* what_arg)
238 : runtime_error(__init(error_code(ev, ecat), what_arg)),
239 __ec_(error_code(ev, ecat))
240{
241}
242
243system_error::system_error(int ev, const error_category& ecat)
244 : runtime_error(__init(error_code(ev, ecat), "")),
245 __ec_(error_code(ev, ecat))
246{
247}
248
Howard Hinnanta62f2892011-05-26 19:48:01 +0000249system_error::~system_error() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000250{
251}
252
253void
254__throw_system_error(int ev, const char* what_arg)
255{
Howard Hinnant54b409f2010-08-11 17:04:31 +0000256#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000257 throw system_error(error_code(ev, system_category()), what_arg);
Howard Hinnante00e6f22013-03-28 18:56:26 +0000258#else
259 (void)ev;
260 (void)what_arg;
Marshall Clowd437fa52016-08-25 15:09:01 +0000261 _VSTD::abort();
Howard Hinnant54b409f2010-08-11 17:04:31 +0000262#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000263}
264
265_LIBCPP_END_NAMESPACE_STD