1 // <system_error> implementation file
3 // Copyright (C) 2007-2022 Free Software Foundation, Inc.
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)
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.
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.
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/>.
26 #define _GLIBCXX_USE_CXX11_ABI 1
27 #define __sso_string __sso_stringxxx
29 #include <system_error>
30 #include <bits/functexcept.h>
35 #if defined(_WIN32) && !defined(__CYGWIN__)
51 constexpr constant_init() : obj() { }
53 ~constant_init() { /* do nothing, union member is not destroyed */ }
56 struct generic_error_category final
: public std::error_category
59 name() const noexcept final
62 _GLIBCXX_DEFAULT_ABI_TAG
64 message(int i
) const final
66 // XXX locale issues: how does one get or set loc.
67 // _GLIBCXX_HAVE_STRERROR_L, strerror_l(i, cloc)
68 return string(strerror(i
));
71 // Override this to avoid a virtual call to default_error_condition(i).
73 equivalent(int i
, const std::error_condition
& cond
) const noexcept final
74 { return i
== cond
.value() && *this == cond
.category(); }
77 __constinit constant_init
<generic_error_category
> generic_category_instance
{};
79 struct system_error_category final
: public std::error_category
82 name() const noexcept final
85 _GLIBCXX_DEFAULT_ABI_TAG
87 message(int i
) const final
89 #if defined(_WIN32) && !defined(__CYGWIN__)
92 = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
93 | FORMAT_MESSAGE_ALLOCATE_BUFFER
,
97 reinterpret_cast<LPTSTR
>(&buf
),
103 void operator()(void* p
) const { ::LocalFree(p
); }
105 std::unique_ptr
<char[], deleter
> guard(buf
);
106 if (len
> 3 && !__builtin_memcmp(buf
+ len
- 3, ".\r\n", 3)) [[likely
]]
108 return string(buf
, len
);
110 return string("Unknown error code");
112 // XXX locale issues: how does one get or set loc.
113 // _GLIBCXX_HAVE_STRERROR_L, strerror_l(i, cloc)
114 return string(strerror(i
));
119 default_error_condition(int ev
) const noexcept final
121 // Use generic category for all known POSIX errno values (including zero)
122 // and system category otherwise.
125 #if defined(_WIN32) && !defined(__CYGWIN__)
127 return {0, generic_category_instance
.obj
};
128 // Convert Windows error code into a corresponding POSIX errno value.
129 #define X(w, e) case ERROR_##w: return {e, generic_category_instance.obj};
130 // This list is based on Cygwin's winsup/cygwin/errno.cc
131 X (ACCESS_DENIED
, EACCES
);
132 X (ACTIVE_CONNECTIONS
, EAGAIN
);
133 X (ALREADY_EXISTS
, EEXIST
);
134 X (BAD_DEVICE
, ENODEV
);
135 X (BAD_EXE_FORMAT
, ENOEXEC
);
136 X (BAD_NETPATH
, ENOENT
);
137 X (BAD_NET_NAME
, ENOENT
);
138 X (BAD_NET_RESP
, ENOSYS
);
139 X (BAD_PATHNAME
, ENOENT
);
140 X (BAD_PIPE
, EINVAL
);
141 X (BAD_UNIT
, ENODEV
);
142 X (BAD_USERNAME
, EINVAL
);
143 X (BEGINNING_OF_MEDIA
, EIO
);
144 X (BROKEN_PIPE
, EPIPE
);
147 X (CALL_NOT_IMPLEMENTED
, ENOSYS
);
148 X (CANCELLED
, EINTR
);
149 X (CANNOT_MAKE
, EPERM
);
150 X (CHILD_NOT_COMPLETE
, EBUSY
);
151 X (COMMITMENT_LIMIT
, EAGAIN
);
152 X (CONNECTION_REFUSED
, ECONNREFUSED
);
154 X (DEVICE_DOOR_OPEN
, EIO
);
155 X (DEVICE_IN_USE
, EAGAIN
);
156 X (DEVICE_REQUIRES_CLEANING
, EIO
);
157 X (DEV_NOT_EXIST
, ENOENT
);
158 X (DIRECTORY
, ENOTDIR
);
159 X (DIR_NOT_EMPTY
, ENOTEMPTY
);
160 X (DISK_CORRUPT
, EIO
);
162 X (DISK_FULL
, ENOSPC
);
164 X (DS_GENERIC_ERROR
, EIO
);
166 X (END_OF_MEDIA
, ENOSPC
);
168 X (EOM_OVERFLOW
, EIO
);
169 X (EXE_MACHINE_TYPE_MISMATCH
, ENOEXEC
);
170 X (EXE_MARKED_INVALID
, ENOEXEC
);
171 X (FILEMARK_DETECTED
, EIO
);
172 X (FILENAME_EXCED_RANGE
, ENAMETOOLONG
);
173 X (FILE_CORRUPT
, EEXIST
);
174 X (FILE_EXISTS
, EEXIST
);
175 X (FILE_INVALID
, ENXIO
);
176 X (FILE_NOT_FOUND
, ENOENT
);
178 X (HANDLE_DISK_FULL
, ENOSPC
);
180 X (INVALID_ADDRESS
, EINVAL
);
181 X (INVALID_AT_INTERRUPT_TIME
, EINTR
);
182 X (INVALID_BLOCK_LENGTH
, EIO
);
183 X (INVALID_DATA
, EINVAL
);
184 X (INVALID_DRIVE
, ENODEV
);
185 X (INVALID_EA_NAME
, EINVAL
);
186 X (INVALID_EXE_SIGNATURE
, ENOEXEC
);
187 X (INVALID_HANDLE
, EBADF
);
188 X (INVALID_NAME
, ENOENT
);
189 X (INVALID_PARAMETER
, EINVAL
);
190 X (INVALID_SIGNAL_NUMBER
, EINVAL
);
191 X (IOPL_NOT_ENABLED
, ENOEXEC
);
193 X (IO_INCOMPLETE
, EAGAIN
);
194 X (IO_PENDING
, EAGAIN
);
195 X (LOCK_VIOLATION
, EBUSY
);
196 X (MAX_THRDS_REACHED
, EAGAIN
);
197 X (META_EXPANSION_TOO_LONG
, EINVAL
);
198 X (MOD_NOT_FOUND
, ENOENT
);
199 X (MORE_DATA
, EMSGSIZE
);
200 X (NEGATIVE_SEEK
, EINVAL
);
201 X (NETNAME_DELETED
, ENOENT
);
202 X (NOACCESS
, EFAULT
);
203 X (NONE_MAPPED
, EINVAL
);
204 X (NONPAGED_SYSTEM_RESOURCES
, EAGAIN
);
205 X (NOT_ENOUGH_MEMORY
, ENOMEM
);
206 X (NOT_ENOUGH_QUOTA
, EIO
);
208 X (NOT_OWNER
, EPERM
);
210 X (NOT_OWNER
, EACCES
);
212 X (NOT_SAME_DEVICE
, EXDEV
);
213 X (NOT_SUPPORTED
, ENOSYS
);
215 X (NO_DATA_DETECTED
, EIO
);
216 X (NO_MORE_SEARCH_HANDLES
, ENFILE
);
217 X (NO_PROC_SLOTS
, EAGAIN
);
218 X (NO_SIGNAL_SENT
, EIO
);
219 X (NO_SYSTEM_RESOURCES
, EFBIG
);
220 X (NO_TOKEN
, EINVAL
);
221 X (OPEN_FAILED
, EIO
);
222 X (OPEN_FILES
, EAGAIN
);
223 X (OUTOFMEMORY
, ENOMEM
);
224 X (PAGED_SYSTEM_RESOURCES
, EAGAIN
);
225 X (PAGEFILE_QUOTA
, EAGAIN
);
226 X (PATH_NOT_FOUND
, ENOENT
);
227 X (PIPE_BUSY
, EBUSY
);
228 X (PIPE_CONNECTED
, EBUSY
);
229 X (POSSIBLE_DEADLOCK
, EDEADLK
);
230 X (PRIVILEGE_NOT_HELD
, EPERM
);
231 X (PROCESS_ABORTED
, EFAULT
);
232 X (PROC_NOT_FOUND
, ESRCH
);
233 X (SECTOR_NOT_FOUND
, EINVAL
);
235 X (SERVICE_REQUEST_TIMEOUT
, EBUSY
);
236 X (SETMARK_DETECTED
, EIO
);
237 X (SHARING_BUFFER_EXCEEDED
, ENOLCK
);
238 X (SHARING_VIOLATION
, EBUSY
);
239 X (SIGNAL_PENDING
, EBUSY
);
240 X (SIGNAL_REFUSED
, EIO
);
241 X (THREAD_1_INACTIVE
, EINVAL
);
243 X (TOO_MANY_LINKS
, EMLINK
);
244 X (TOO_MANY_OPEN_FILES
, EMFILE
);
245 X (UNEXP_NET_ERR
, EIO
);
246 X (WORKING_SET_QUOTA
, EAGAIN
);
247 X (WRITE_PROTECT
, EROFS
);
251 // List of errno macros from [cerrno.syn].
252 // C11 only defines EDOM, EILSEQ and ERANGE, the rest are from POSIX.
253 // They expand to integer constant expressions with type int,
254 // and distinct positive values, suitable for use in #if directives.
255 // POSIX adds more macros (but they're not defined on all targets,
256 // see config/os/.../error_constants.h), and POSIX allows
257 // EAGAIN == EWOULDBLOCK and ENOTSUP == EOPNOTSUPP.
420 #if defined ENOTEMPTY && (!defined EEXIST || ENOTEMPTY != EEXIST)
421 // AIX sometimes uses the same value for EEXIST and ENOTEMPTY
424 #ifdef ENOTRECOVERABLE
425 case ENOTRECOVERABLE
:
430 #if defined ENOTSUP && (!defined ENOSYS || ENOTSUP != ENOSYS)
431 // zTPF uses the same value for ENOSYS and ENOTSUP
440 #if defined EOPNOTSUPP && (!defined ENOTSUP || EOPNOTSUPP != ENOTSUP)
458 #ifdef EPROTONOSUPPORT
459 case EPROTONOSUPPORT
:
483 #if defined EWOULDBLOCK && (!defined EAGAIN || EWOULDBLOCK != EAGAIN)
490 return std::error_condition(ev
, generic_category_instance
.obj
);
492 /* Additional system-dependent mappings from non-standard error codes
493 * to one of the POSIX values above would go here, e.g.
495 return std::error_condition(EINVAL, std::generic_category());
500 return std::error_condition(ev
, *this);
504 // Override this to avoid a virtual call to default_error_condition(i).
506 equivalent(int i
, const std::error_condition
& cond
) const noexcept final
507 { return system_error_category::default_error_condition(i
) == cond
; }
510 __constinit constant_init
<system_error_category
> system_category_instance
{};
513 namespace std
_GLIBCXX_VISIBILITY(default)
515 _GLIBCXX_BEGIN_NAMESPACE_VERSION
518 __throw_system_error(int __i
__attribute__((unused
)))
520 _GLIBCXX_THROW_OR_ABORT(system_error(__i
, generic_category_instance
.obj
));
523 error_category::~error_category() = default;
525 const error_category
&
526 _V2::system_category() noexcept
{ return system_category_instance
.obj
; }
528 const error_category
&
529 _V2::generic_category() noexcept
{ return generic_category_instance
.obj
; }
531 system_error::~system_error() = default;
534 error_category::default_error_condition(int __i
) const noexcept
535 { return error_condition(__i
, *this); }
538 error_category::equivalent(int __i
,
539 const error_condition
& __cond
) const noexcept
540 { return default_error_condition(__i
) == __cond
; }
543 error_category::equivalent(const error_code
& __code
, int __i
) const noexcept
544 { return *this == __code
.category() && __code
.value() == __i
; }
547 error_code::default_error_condition() const noexcept
548 { return category().default_error_condition(value()); }
550 #if _GLIBCXX_USE_CXX11_ABI
551 // Return error_category::message() as a COW string
553 error_category::_M_message(int i
) const
555 string msg
= this->message(i
);
556 return {msg
.c_str(), msg
.length()};
560 _GLIBCXX_END_NAMESPACE_VERSION