1 // <system_error> implementation file
2 
3 // Copyright (C) 2007-2021 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 
26 #define _GLIBCXX_USE_CXX11_ABI 1
27 #define __sso_string __sso_stringxxx
28 #include <cstring>
29 #include <system_error>
30 #include <bits/functexcept.h>
31 #include <limits>
32 #include <errno.h>
33 #undef __sso_string
34 
35 namespace
36 {
37   using std::string;
38 
39   struct generic_error_category : public std::error_category
40   {
41     virtual const char*
name__anon18f9d0e50111::generic_error_category42     name() const noexcept
43     { return "generic"; }
44 
45     _GLIBCXX_DEFAULT_ABI_TAG
46     virtual string
message__anon18f9d0e50111::generic_error_category47     message(int i) const
48     {
49       // XXX locale issues: how does one get or set loc.
50       // _GLIBCXX_HAVE_STRERROR_L, strerror_l(i, cloc)
51       return string(strerror(i));
52     }
53   };
54 
55   struct system_error_category : public std::error_category
56   {
57     virtual const char*
name__anon18f9d0e50111::system_error_category58     name() const noexcept
59     { return "system"; }
60 
61     _GLIBCXX_DEFAULT_ABI_TAG
62     virtual string
message__anon18f9d0e50111::system_error_category63     message(int i) const
64     {
65       // XXX locale issues: how does one get or set loc.
66       // _GLIBCXX_HAVE_STRERROR_L, strerror_l(i, cloc)
67       return string(strerror(i));
68     }
69 
70     virtual std::error_condition
default_error_condition__anon18f9d0e50111::system_error_category71     default_error_condition(int ev) const noexcept
72     {
73       switch (ev)
74       {
75       // List of errno macros from [cerrno.syn].
76       // C11 only defines EDOM, EILSEQ and ERANGE, the rest are from POSIX.
77       // They expand to integer constant expressions with type int,
78       // and distinct positive values, suitable for use in #if directives.
79       // POSIX adds more macros (but they're not defined on all targets,
80       // see config/os/*/error_constants.h), and POSIX allows
81       // EAGAIN == EWOULDBLOCK and ENOTSUP == EOPNOTSUPP.
82 
83 #ifdef E2BIG
84       case E2BIG:
85 #endif
86 #ifdef EACCES
87       case EACCES:
88 #endif
89 #ifdef EADDRINUSE
90       case EADDRINUSE:
91 #endif
92 #ifdef EADDRNOTAVAIL
93       case EADDRNOTAVAIL:
94 #endif
95 #ifdef EAFNOSUPPORT
96       case EAFNOSUPPORT:
97 #endif
98 #ifdef EAGAIN
99       case EAGAIN:
100 #endif
101 #ifdef EALREADY
102       case EALREADY:
103 #endif
104 #ifdef EBADF
105       case EBADF:
106 #endif
107 #ifdef EBADMSG
108       case EBADMSG:
109 #endif
110 #ifdef EBUSY
111       case EBUSY:
112 #endif
113 #ifdef ECANCELED
114       case ECANCELED:
115 #endif
116 #ifdef ECHILD
117       case ECHILD:
118 #endif
119 #ifdef ECONNABORTED
120       case ECONNABORTED:
121 #endif
122 #ifdef ECONNREFUSED
123       case ECONNREFUSED:
124 #endif
125 #ifdef ECONNRESET
126       case ECONNRESET:
127 #endif
128 #ifdef EDEADLK
129       case EDEADLK:
130 #endif
131 #ifdef EDESTADDRREQ
132       case EDESTADDRREQ:
133 #endif
134       case EDOM:
135 #ifdef EEXIST
136       case EEXIST:
137 #endif
138 #ifdef EFAULT
139       case EFAULT:
140 #endif
141 #ifdef EFBIG
142       case EFBIG:
143 #endif
144 #ifdef EHOSTUNREACH
145       case EHOSTUNREACH:
146 #endif
147 #ifdef EIDRM
148       case EIDRM:
149 #endif
150       case EILSEQ:
151 #ifdef EINPROGRESS
152       case EINPROGRESS:
153 #endif
154 #ifdef EINTR
155       case EINTR:
156 #endif
157 #ifdef EINVAL
158       case EINVAL:
159 #endif
160 #ifdef EIO
161       case EIO:
162 #endif
163 #ifdef EISCONN
164       case EISCONN:
165 #endif
166 #ifdef EISDIR
167       case EISDIR:
168 #endif
169 #ifdef ELOOP
170       case ELOOP:
171 #endif
172 #ifdef EMFILE
173       case EMFILE:
174 #endif
175 #ifdef EMLINK
176       case EMLINK:
177 #endif
178 #ifdef EMSGSIZE
179       case EMSGSIZE:
180 #endif
181 #ifdef ENAMETOOLONG
182       case ENAMETOOLONG:
183 #endif
184 #ifdef ENETDOWN
185       case ENETDOWN:
186 #endif
187 #ifdef ENETRESET
188       case ENETRESET:
189 #endif
190 #ifdef ENETUNREACH
191       case ENETUNREACH:
192 #endif
193 #ifdef ENFILE
194       case ENFILE:
195 #endif
196 #ifdef ENOBUFS
197       case ENOBUFS:
198 #endif
199 #ifdef ENODATA
200       case ENODATA:
201 #endif
202 #ifdef ENODEV
203       case ENODEV:
204 #endif
205 #ifdef ENOENT
206       case ENOENT:
207 #endif
208 #ifdef ENOEXEC
209       case ENOEXEC:
210 #endif
211 #ifdef ENOLCK
212       case ENOLCK:
213 #endif
214 #ifdef ENOLINK
215       case ENOLINK:
216 #endif
217 #ifdef ENOMEM
218       case ENOMEM:
219 #endif
220 #ifdef ENOMSG
221       case ENOMSG:
222 #endif
223 #ifdef ENOPROTOOPT
224       case ENOPROTOOPT:
225 #endif
226 #ifdef ENOSPC
227       case ENOSPC:
228 #endif
229 #ifdef ENOSR
230       case ENOSR:
231 #endif
232 #ifdef ENOSTR
233       case ENOSTR:
234 #endif
235 #ifdef ENOSYS
236       case ENOSYS:
237 #endif
238 #ifdef ENOTCONN
239       case ENOTCONN:
240 #endif
241 #ifdef ENOTDIR
242       case ENOTDIR:
243 #endif
244 #if defined ENOTEMPTY && (!defined EEXIST || ENOTEMPTY != EEXIST)
245       // AIX sometimes uses the same value for EEXIST and ENOTEMPTY
246       case ENOTEMPTY:
247 #endif
248 #ifdef ENOTRECOVERABLE
249       case ENOTRECOVERABLE:
250 #endif
251 #ifdef ENOTSOCK
252       case ENOTSOCK:
253 #endif
254 #if defined ENOTSUP && (!defined ENOSYS || ENOTSUP != ENOSYS)
255       // zTPF uses the same value for ENOSYS and ENOTSUP
256       case ENOTSUP:
257 #endif
258 #ifdef ENOTTY
259       case ENOTTY:
260 #endif
261 #ifdef ENXIO
262       case ENXIO:
263 #endif
264 #if defined EOPNOTSUPP && (!defined ENOTSUP || EOPNOTSUPP != ENOTSUP)
265       case EOPNOTSUPP:
266 #endif
267 #ifdef EOVERFLOW
268       case EOVERFLOW:
269 #endif
270 #ifdef EOWNERDEAD
271       case EOWNERDEAD:
272 #endif
273 #ifdef EPERM
274       case EPERM:
275 #endif
276 #ifdef EPIPE
277       case EPIPE:
278 #endif
279 #ifdef EPROTO
280       case EPROTO:
281 #endif
282 #ifdef EPROTONOSUPPORT
283       case EPROTONOSUPPORT:
284 #endif
285 #ifdef EPROTOTYPE
286       case EPROTOTYPE:
287 #endif
288       case ERANGE:
289 #ifdef EROFS
290       case EROFS:
291 #endif
292 #ifdef ESPIPE
293       case ESPIPE:
294 #endif
295 #ifdef ESRCH
296       case ESRCH:
297 #endif
298 #ifdef ETIME
299       case ETIME:
300 #endif
301 #ifdef ETIMEDOUT
302       case ETIMEDOUT:
303 #endif
304 #ifdef ETXTBSY
305       case ETXTBSY:
306 #endif
307 #if defined EWOULDBLOCK && (!defined EAGAIN || EWOULDBLOCK != EAGAIN)
308       case EWOULDBLOCK:
309 #endif
310 #ifdef EXDEV
311       case EXDEV:
312 #endif
313         return std::error_condition(ev, std::generic_category());
314 
315       /* Additional system-dependent mappings from non-standard error codes
316        * to one of the POSIX values above would go here, e.g.
317       case EBLAH:
318 	return std::error_condition(EINVAL, std::generic_category());
319        */
320 
321       default:
322 	return std::error_condition(ev, std::system_category());
323       }
324     }
325   };
326 
327   const generic_error_category generic_category_instance{};
328   const system_error_category system_category_instance{};
329 }
330 
331 namespace std _GLIBCXX_VISIBILITY(default)
332 {
333 _GLIBCXX_BEGIN_NAMESPACE_VERSION
334 
335   void
__throw_system_error(int __i)336   __throw_system_error(int __i __attribute__((unused)))
337   {
338     _GLIBCXX_THROW_OR_ABORT(system_error(error_code(__i, generic_category())));
339   }
340 
341   error_category::~error_category() noexcept = default;
342 
343   const error_category&
system_category()344   _V2::system_category() noexcept { return system_category_instance; }
345 
346   const error_category&
generic_category()347   _V2::generic_category() noexcept { return generic_category_instance; }
348 
349   system_error::~system_error() noexcept = default;
350 
351   error_condition
default_error_condition(int __i) const352   error_category::default_error_condition(int __i) const noexcept
353   { return error_condition(__i, *this); }
354 
355   bool
equivalent(int __i,const error_condition & __cond) const356   error_category::equivalent(int __i,
357 			     const error_condition& __cond) const noexcept
358   { return default_error_condition(__i) == __cond; }
359 
360   bool
equivalent(const error_code & __code,int __i) const361   error_category::equivalent(const error_code& __code, int __i) const noexcept
362   { return *this == __code.category() && __code.value() == __i; }
363 
364   error_condition
default_error_condition() const365   error_code::default_error_condition() const noexcept
366   { return category().default_error_condition(value()); }
367 
368 #if _GLIBCXX_USE_CXX11_ABI
369   // Return error_category::message() as a COW string
370   __cow_string
_M_message(int i) const371   error_category::_M_message(int i) const
372   {
373     string msg = this->message(i);
374     return {msg.c_str(), msg.length()};
375   }
376 #endif
377 
378 _GLIBCXX_END_NAMESPACE_VERSION
379 } // namespace
380