system_error.cc revision 1.1.1.4
1// <system_error> implementation file
2
3// Copyright (C) 2007-2016 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
35namespace
36{
37  using std::string;
38
39  struct generic_error_category : public std::error_category
40  {
41    virtual const char*
42    name() const noexcept
43    { return "generic"; }
44
45    _GLIBCXX_DEFAULT_ABI_TAG
46    virtual string
47    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*
58    name() const noexcept
59    { return "system"; }
60
61    _GLIBCXX_DEFAULT_ABI_TAG
62    virtual string
63    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
71    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#ifdef ENOTSUP
255      case ENOTSUP:
256#endif
257#ifdef ENOTTY
258      case ENOTTY:
259#endif
260#ifdef ENXIO
261      case ENXIO:
262#endif
263#if defined EOPNOTSUPP && (!defined ENOTSUP || EOPNOTSUPP != ENOTSUP)
264      case EOPNOTSUPP:
265#endif
266#ifdef EOVERFLOW
267      case EOVERFLOW:
268#endif
269#ifdef EOWNERDEAD
270      case EOWNERDEAD:
271#endif
272#ifdef EPERM
273      case EPERM:
274#endif
275#ifdef EPIPE
276      case EPIPE:
277#endif
278#ifdef EPROTO
279      case EPROTO:
280#endif
281#ifdef EPROTONOSUPPORT
282      case EPROTONOSUPPORT:
283#endif
284#ifdef EPROTOTYPE
285      case EPROTOTYPE:
286#endif
287      case ERANGE:
288#ifdef EROFS
289      case EROFS:
290#endif
291#ifdef ESPIPE
292      case ESPIPE:
293#endif
294#ifdef ESRCH
295      case ESRCH:
296#endif
297#ifdef ETIME
298      case ETIME:
299#endif
300#ifdef ETIMEDOUT
301      case ETIMEDOUT:
302#endif
303#ifdef ETXTBSY
304      case ETXTBSY:
305#endif
306#if defined EWOULDBLOCK && (!defined EAGAIN || EWOULDBLOCK != EAGAIN)
307      case EWOULDBLOCK:
308#endif
309#ifdef EXDEV
310      case EXDEV:
311#endif
312        return std::error_condition(ev, std::generic_category());
313
314      /* Additional system-dependent mappings from non-standard error codes
315       * to one of the POSIX values above would go here, e.g.
316      case EBLAH:
317	return std::error_condition(EINVAL, std::generic_category());
318       */
319
320      default:
321	return std::error_condition(ev, std::system_category());
322      }
323    }
324  };
325
326  const generic_error_category generic_category_instance{};
327  const system_error_category system_category_instance{};
328}
329
330namespace std _GLIBCXX_VISIBILITY(default)
331{
332_GLIBCXX_BEGIN_NAMESPACE_VERSION
333
334  void
335  __throw_system_error(int __i __attribute__((unused)))
336  {
337    _GLIBCXX_THROW_OR_ABORT(system_error(error_code(__i, generic_category())));
338  }
339
340  error_category::~error_category() noexcept = default;
341
342  const error_category&
343  _V2::system_category() noexcept { return system_category_instance; }
344
345  const error_category&
346  _V2::generic_category() noexcept { return generic_category_instance; }
347
348  system_error::~system_error() noexcept = default;
349
350  error_condition
351  error_category::default_error_condition(int __i) const noexcept
352  { return error_condition(__i, *this); }
353
354  bool
355  error_category::equivalent(int __i,
356			     const error_condition& __cond) const noexcept
357  { return default_error_condition(__i) == __cond; }
358
359  bool
360  error_category::equivalent(const error_code& __code, int __i) const noexcept
361  { return *this == __code.category() && __code.value() == __i; }
362
363  error_condition
364  error_code::default_error_condition() const noexcept
365  { return category().default_error_condition(value()); }
366
367#if _GLIBCXX_USE_CXX11_ABI
368  // Return error_category::message() as a COW string
369  __cow_string
370  error_category::_M_message(int i) const
371  {
372    string msg = this->message(i);
373    return {msg.c_str(), msg.length()};
374  }
375#endif
376
377_GLIBCXX_END_NAMESPACE_VERSION
378} // namespace
379