1
// error_code support implementation file ----------------------------------//
3
// Copyright Beman Dawes 2002, 2006
5
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8
// See library home page at http://www.boost.org/libs/system
10
//----------------------------------------------------------------------------//
12
#include <boost/config/warning_disable.hpp>
14
// define BOOST_SYSTEM_SOURCE so that <boost/system/config.hpp> knows
15
// the library is being built (possibly exporting rather than importing code)
16
#define BOOST_SYSTEM_SOURCE
18
#include <boost/system/config.hpp>
19
#include <boost/system/error_code.hpp>
20
#include <boost/cerrno.hpp>
25
#include <cstring> // for strerror/strerror_r
27
# if defined( BOOST_WINDOWS_API )
29
# if !defined(WINAPI_FAMILY) || ((WINAPI_FAMILY & WINAPI_PARTITION_DESKTOP) != 0)
30
# include "local_free_on_destruction.hpp"
32
# ifndef ERROR_INCORRECT_SIZE
33
# define ERROR_INCORRECT_SIZE ERROR_BAD_ARGUMENTS
37
//----------------------------------------------------------------------------//
46
// standard error categories ---------------------------------------------//
48
class generic_error_category : public error_category
51
generic_error_category(){}
52
const char * name() const BOOST_SYSTEM_NOEXCEPT;
53
std::string message( int ev ) const;
56
class system_error_category : public error_category
59
system_error_category(){}
60
const char * name() const BOOST_SYSTEM_NOEXCEPT;
61
std::string message( int ev ) const;
62
error_condition default_error_condition( int ev ) const BOOST_SYSTEM_NOEXCEPT;
65
// generic_error_category implementation ---------------------------------//
67
const char * generic_error_category::name() const BOOST_SYSTEM_NOEXCEPT
72
std::string generic_error_category::message( int ev ) const
74
using namespace boost::system::errc;
76
using boost::system::errc::invalid_argument;
79
static std::string unknown_err( "Unknown error" );
80
// strerror_r is preferred because it is always thread safe,
81
// however, we fallback to strerror in certain cases because:
82
// -- Windows doesn't provide strerror_r.
83
// -- HP and Sun do provide strerror_r on newer systems, but there is
84
// no way to tell if is available at runtime and in any case their
85
// versions of strerror are thread safe anyhow.
86
// -- Linux only sometimes provides strerror_r.
87
// -- Tru64 provides strerror_r only when compiled -pthread.
88
// -- VMS doesn't provide strerror_r, but on this platform, strerror is
90
# if defined(BOOST_WINDOWS_API) || defined(__hpux) || defined(__sun)\
91
|| (defined(__linux) && (!defined(__USE_XOPEN2K) || defined(BOOST_SYSTEM_USE_STRERROR)))\
92
|| (defined(__osf__) && !defined(_REENTRANT))\
93
|| (defined(__INTEGRITY))\
95
|| (defined(__QNXNTO__))
96
const char * c_str = std::strerror( ev );
98
? std::string( c_str )
100
# else // use strerror_r
103
std::size_t sz = sizeof(buf);
104
# if defined(__CYGWIN__) || defined(__USE_GNU)
105
// Oddball version of strerror_r
106
const char * c_str = strerror_r( ev, bp, sz );
108
? std::string( c_str )
111
// POSIX version of strerror_r
115
// strerror_r returns 0 on success, otherwise ERANGE if buffer too small,
116
// invalid_argument if ev not a valid error number
118
const char * c_str = strerror( ev );
121
? std::string( c_str )
124
result = strerror_r( ev, bp, sz );
130
# if defined(__linux)
131
// Linux strerror_r returns -1 on error, with error number in errno
134
if ( result != ERANGE ) break;
135
if ( sz > sizeof(buf) ) std::free( bp );
137
if ( (bp = static_cast<char*>(std::malloc( sz ))) == 0 )
138
return std::string( "ENOMEM" );
142
# ifndef BOOST_NO_EXCEPTIONS
146
msg = ( ( result == invalid_argument ) ? "Unknown error" : bp );
149
# ifndef BOOST_NO_EXCEPTIONS
153
// just eat the exception
157
if ( sz > sizeof(buf) ) std::free( bp );
160
# endif // else POSIX version of strerror_r
161
# endif // else use strerror_r
163
// system_error_category implementation --------------------------------//
165
const char * system_error_category::name() const BOOST_SYSTEM_NOEXCEPT
170
error_condition system_error_category::default_error_condition( int ev ) const BOOST_SYSTEM_NOEXCEPT
172
using namespace boost::system::errc;
174
using boost::system::errc::invalid_argument;
177
# if defined(BOOST_WINDOWS_API)
178
# if defined(WINAPI_FAMILY) && ((WINAPI_FAMILY & WINAPI_PARTITION_APP) != 0)
179
// When using the Windows Runtime, most system errors are reported as HRESULTs.
180
// We want to map the common Win32 errors to their equivalent error condition,
181
// whether or not they are reported via an HRESULT.
182
if ( ev < 0 ) // Check for failed HRESULTs only.
183
if ( HRESULT_FACILITY( ev ) == FACILITY_WIN32 )
184
ev = HRESULT_CODE( ev );
190
case 0: return make_error_condition( success );
191
# if defined(BOOST_POSIX_API)
192
// POSIX-like O/S -> posix_errno decode table ---------------------------//
193
case E2BIG: return make_error_condition( argument_list_too_long );
194
case EACCES: return make_error_condition( permission_denied );
195
case EADDRINUSE: return make_error_condition( address_in_use );
196
case EADDRNOTAVAIL: return make_error_condition( address_not_available );
197
case EAFNOSUPPORT: return make_error_condition( address_family_not_supported );
198
case EAGAIN: return make_error_condition( resource_unavailable_try_again );
199
# if EALREADY != EBUSY // EALREADY and EBUSY are the same on QNX Neutrino
200
case EALREADY: return make_error_condition( connection_already_in_progress );
202
case EBADF: return make_error_condition( bad_file_descriptor );
203
case EBADMSG: return make_error_condition( bad_message );
204
case EBUSY: return make_error_condition( device_or_resource_busy );
205
case ECANCELED: return make_error_condition( operation_canceled );
206
case ECHILD: return make_error_condition( no_child_process );
207
case ECONNABORTED: return make_error_condition( connection_aborted );
208
case ECONNREFUSED: return make_error_condition( connection_refused );
209
case ECONNRESET: return make_error_condition( connection_reset );
210
case EDEADLK: return make_error_condition( resource_deadlock_would_occur );
211
case EDESTADDRREQ: return make_error_condition( destination_address_required );
212
case EDOM: return make_error_condition( argument_out_of_domain );
213
case EEXIST: return make_error_condition( file_exists );
214
case EFAULT: return make_error_condition( bad_address );
215
case EFBIG: return make_error_condition( file_too_large );
216
case EHOSTUNREACH: return make_error_condition( host_unreachable );
217
case EIDRM: return make_error_condition( identifier_removed );
218
case EILSEQ: return make_error_condition( illegal_byte_sequence );
219
case EINPROGRESS: return make_error_condition( operation_in_progress );
220
case EINTR: return make_error_condition( interrupted );
221
case EINVAL: return make_error_condition( invalid_argument );
222
case EIO: return make_error_condition( io_error );
223
case EISCONN: return make_error_condition( already_connected );
224
case EISDIR: return make_error_condition( is_a_directory );
225
case ELOOP: return make_error_condition( too_many_symbolic_link_levels );
226
case EMFILE: return make_error_condition( too_many_files_open );
227
case EMLINK: return make_error_condition( too_many_links );
228
case EMSGSIZE: return make_error_condition( message_size );
229
case ENAMETOOLONG: return make_error_condition( filename_too_long );
230
case ENETDOWN: return make_error_condition( network_down );
231
case ENETRESET: return make_error_condition( network_reset );
232
case ENETUNREACH: return make_error_condition( network_unreachable );
233
case ENFILE: return make_error_condition( too_many_files_open_in_system );
234
case ENOBUFS: return make_error_condition( no_buffer_space );
235
case ENODATA: return make_error_condition( no_message_available );
236
case ENODEV: return make_error_condition( no_such_device );
237
case ENOENT: return make_error_condition( no_such_file_or_directory );
238
case ENOEXEC: return make_error_condition( executable_format_error );
239
case ENOLCK: return make_error_condition( no_lock_available );
240
case ENOLINK: return make_error_condition( no_link );
241
case ENOMEM: return make_error_condition( not_enough_memory );
242
case ENOMSG: return make_error_condition( no_message );
243
case ENOPROTOOPT: return make_error_condition( no_protocol_option );
244
case ENOSPC: return make_error_condition( no_space_on_device );
245
case ENOSR: return make_error_condition( no_stream_resources );
246
case ENOSTR: return make_error_condition( not_a_stream );
247
case ENOSYS: return make_error_condition( function_not_supported );
248
case ENOTCONN: return make_error_condition( not_connected );
249
case ENOTDIR: return make_error_condition( not_a_directory );
250
# if ENOTEMPTY != EEXIST // AIX treats ENOTEMPTY and EEXIST as the same value
251
case ENOTEMPTY: return make_error_condition( directory_not_empty );
252
# endif // ENOTEMPTY != EEXIST
253
# if ENOTRECOVERABLE != ECONNRESET // the same on some Broadcom chips
254
case ENOTRECOVERABLE: return make_error_condition( state_not_recoverable );
255
# endif // ENOTRECOVERABLE != ECONNRESET
256
case ENOTSOCK: return make_error_condition( not_a_socket );
257
case ENOTSUP: return make_error_condition( not_supported );
258
case ENOTTY: return make_error_condition( inappropriate_io_control_operation );
259
case ENXIO: return make_error_condition( no_such_device_or_address );
260
# if EOPNOTSUPP != ENOTSUP
261
case EOPNOTSUPP: return make_error_condition( operation_not_supported );
262
# endif // EOPNOTSUPP != ENOTSUP
263
case EOVERFLOW: return make_error_condition( value_too_large );
264
# if EOWNERDEAD != ECONNABORTED // the same on some Broadcom chips
265
case EOWNERDEAD: return make_error_condition( owner_dead );
266
# endif // EOWNERDEAD != ECONNABORTED
267
case EPERM: return make_error_condition( operation_not_permitted );
268
case EPIPE: return make_error_condition( broken_pipe );
269
case EPROTO: return make_error_condition( protocol_error );
270
case EPROTONOSUPPORT: return make_error_condition( protocol_not_supported );
271
case EPROTOTYPE: return make_error_condition( wrong_protocol_type );
272
case ERANGE: return make_error_condition( result_out_of_range );
273
case EROFS: return make_error_condition( read_only_file_system );
274
case ESPIPE: return make_error_condition( invalid_seek );
275
case ESRCH: return make_error_condition( no_such_process );
276
case ETIME: return make_error_condition( stream_timeout );
277
case ETIMEDOUT: return make_error_condition( timed_out );
278
case ETXTBSY: return make_error_condition( text_file_busy );
279
# if EAGAIN != EWOULDBLOCK
280
case EWOULDBLOCK: return make_error_condition( operation_would_block );
281
# endif // EAGAIN != EWOULDBLOCK
282
case EXDEV: return make_error_condition( cross_device_link );
284
// Windows system -> posix_errno decode table ---------------------------//
285
// see WinError.h comments for descriptions of errors
286
case ERROR_ACCESS_DENIED: return make_error_condition( permission_denied );
287
case ERROR_ALREADY_EXISTS: return make_error_condition( file_exists );
288
case ERROR_BAD_UNIT: return make_error_condition( no_such_device );
289
case ERROR_BUFFER_OVERFLOW: return make_error_condition( filename_too_long );
290
case ERROR_BUSY: return make_error_condition( device_or_resource_busy );
291
case ERROR_BUSY_DRIVE: return make_error_condition( device_or_resource_busy );
292
case ERROR_CANNOT_MAKE: return make_error_condition( permission_denied );
293
case ERROR_CANTOPEN: return make_error_condition( io_error );
294
case ERROR_CANTREAD: return make_error_condition( io_error );
295
case ERROR_CANTWRITE: return make_error_condition( io_error );
296
case ERROR_CURRENT_DIRECTORY: return make_error_condition( permission_denied );
297
case ERROR_DEV_NOT_EXIST: return make_error_condition( no_such_device );
298
case ERROR_DEVICE_IN_USE: return make_error_condition( device_or_resource_busy );
299
case ERROR_DIR_NOT_EMPTY: return make_error_condition( directory_not_empty );
300
case ERROR_DIRECTORY: return make_error_condition( invalid_argument ); // WinError.h: "The directory name is invalid"
301
case ERROR_DISK_FULL: return make_error_condition( no_space_on_device );
302
case ERROR_FILE_EXISTS: return make_error_condition( file_exists );
303
case ERROR_FILE_NOT_FOUND: return make_error_condition( no_such_file_or_directory );
304
case ERROR_HANDLE_DISK_FULL: return make_error_condition( no_space_on_device );
305
case ERROR_INVALID_ACCESS: return make_error_condition( permission_denied );
306
case ERROR_INVALID_DRIVE: return make_error_condition( no_such_device );
307
case ERROR_INVALID_FUNCTION: return make_error_condition( function_not_supported );
308
case ERROR_INVALID_HANDLE: return make_error_condition( invalid_argument );
309
case ERROR_INVALID_NAME: return make_error_condition( invalid_argument );
310
case ERROR_LOCK_VIOLATION: return make_error_condition( no_lock_available );
311
case ERROR_LOCKED: return make_error_condition( no_lock_available );
312
case ERROR_NEGATIVE_SEEK: return make_error_condition( invalid_argument );
313
case ERROR_NOACCESS: return make_error_condition( permission_denied );
314
case ERROR_NOT_ENOUGH_MEMORY: return make_error_condition( not_enough_memory );
315
case ERROR_NOT_READY: return make_error_condition( resource_unavailable_try_again );
316
case ERROR_NOT_SAME_DEVICE: return make_error_condition( cross_device_link );
317
case ERROR_OPEN_FAILED: return make_error_condition( io_error );
318
case ERROR_OPEN_FILES: return make_error_condition( device_or_resource_busy );
319
case ERROR_OPERATION_ABORTED: return make_error_condition( operation_canceled );
320
case ERROR_OUTOFMEMORY: return make_error_condition( not_enough_memory );
321
case ERROR_PATH_NOT_FOUND: return make_error_condition( no_such_file_or_directory );
322
case ERROR_READ_FAULT: return make_error_condition( io_error );
323
case ERROR_RETRY: return make_error_condition( resource_unavailable_try_again );
324
case ERROR_SEEK: return make_error_condition( io_error );
325
case ERROR_SHARING_VIOLATION: return make_error_condition( permission_denied );
326
case ERROR_TOO_MANY_OPEN_FILES: return make_error_condition( too_many_files_open );
327
case ERROR_WRITE_FAULT: return make_error_condition( io_error );
328
case ERROR_WRITE_PROTECT: return make_error_condition( permission_denied );
329
case WSAEACCES: return make_error_condition( permission_denied );
330
case WSAEADDRINUSE: return make_error_condition( address_in_use );
331
case WSAEADDRNOTAVAIL: return make_error_condition( address_not_available );
332
case WSAEAFNOSUPPORT: return make_error_condition( address_family_not_supported );
333
case WSAEALREADY: return make_error_condition( connection_already_in_progress );
334
case WSAEBADF: return make_error_condition( bad_file_descriptor );
335
case WSAECONNABORTED: return make_error_condition( connection_aborted );
336
case WSAECONNREFUSED: return make_error_condition( connection_refused );
337
case WSAECONNRESET: return make_error_condition( connection_reset );
338
case WSAEDESTADDRREQ: return make_error_condition( destination_address_required );
339
case WSAEFAULT: return make_error_condition( bad_address );
340
case WSAEHOSTUNREACH: return make_error_condition( host_unreachable );
341
case WSAEINPROGRESS: return make_error_condition( operation_in_progress );
342
case WSAEINTR: return make_error_condition( interrupted );
343
case WSAEINVAL: return make_error_condition( invalid_argument );
344
case WSAEISCONN: return make_error_condition( already_connected );
345
case WSAEMFILE: return make_error_condition( too_many_files_open );
346
case WSAEMSGSIZE: return make_error_condition( message_size );
347
case WSAENAMETOOLONG: return make_error_condition( filename_too_long );
348
case WSAENETDOWN: return make_error_condition( network_down );
349
case WSAENETRESET: return make_error_condition( network_reset );
350
case WSAENETUNREACH: return make_error_condition( network_unreachable );
351
case WSAENOBUFS: return make_error_condition( no_buffer_space );
352
case WSAENOPROTOOPT: return make_error_condition( no_protocol_option );
353
case WSAENOTCONN: return make_error_condition( not_connected );
354
case WSAENOTSOCK: return make_error_condition( not_a_socket );
355
case WSAEOPNOTSUPP: return make_error_condition( operation_not_supported );
356
case WSAEPROTONOSUPPORT: return make_error_condition( protocol_not_supported );
357
case WSAEPROTOTYPE: return make_error_condition( wrong_protocol_type );
358
case WSAETIMEDOUT: return make_error_condition( timed_out );
359
case WSAEWOULDBLOCK: return make_error_condition( operation_would_block );
361
default: return error_condition( ev, system_category() );
365
# if !defined( BOOST_WINDOWS_API )
367
std::string system_error_category::message( int ev ) const
369
return generic_category().message( ev );
373
std::string system_error_category::message( int ev ) const
375
# if defined(WINAPI_FAMILY) && ((WINAPI_FAMILY & WINAPI_PARTITION_DESKTOP) == 0)
376
std::string str( 128, char() );
379
DWORD retval = ::FormatMessageA(
380
FORMAT_MESSAGE_FROM_SYSTEM |
381
FORMAT_MESSAGE_IGNORE_INSERTS,
384
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
392
str.resize( retval );
395
else if ( ::GetLastError() != ERROR_INSUFFICIENT_BUFFER )
397
return std::string("Unknown error");
401
str.resize( str.size() + str.size()/2 );
404
# elif !defined(BOOST_NO_ANSI_APIS)
406
DWORD retval = ::FormatMessageA(
407
FORMAT_MESSAGE_ALLOCATE_BUFFER |
408
FORMAT_MESSAGE_FROM_SYSTEM |
409
FORMAT_MESSAGE_IGNORE_INSERTS,
412
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
417
detail::local_free_on_destruction lfod(lpMsgBuf);
419
return std::string("Unknown error");
421
std::string str( static_cast<LPCSTR>(lpMsgBuf) );
422
# else // WinCE workaround
424
DWORD retval = ::FormatMessageW(
425
FORMAT_MESSAGE_ALLOCATE_BUFFER |
426
FORMAT_MESSAGE_FROM_SYSTEM |
427
FORMAT_MESSAGE_IGNORE_INSERTS,
430
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
435
detail::local_free_on_destruction lfod(lpMsgBuf);
437
return std::string("Unknown error");
439
int num_chars = (wcslen( static_cast<LPCWSTR>(lpMsgBuf) ) + 1) * 2;
440
LPSTR narrow_buffer = (LPSTR)_alloca( num_chars );
441
if (::WideCharToMultiByte(CP_ACP, 0, static_cast<LPCWSTR>(lpMsgBuf), -1, narrow_buffer, num_chars, NULL, NULL) == 0)
442
return std::string("Unknown error");
444
std::string str( narrow_buffer );
447
&& (str[str.size()-1] == '\n' || str[str.size()-1] == '\r') )
448
str.erase( str.size()-1 );
449
if ( str.size() && str[str.size()-1] == '.' )
450
{ str.erase( str.size()-1 ); }
455
} // unnamed namespace
458
# ifndef BOOST_SYSTEM_NO_DEPRECATED
459
BOOST_SYSTEM_DECL error_code throws; // "throw on error" special error_code;
460
// note that it doesn't matter if this
461
// isn't initialized before use since
462
// the only use is to take its
463
// address for comparison purposes
466
BOOST_SYSTEM_DECL const error_category & system_category() BOOST_SYSTEM_NOEXCEPT
468
static const system_error_category system_category_const;
469
return system_category_const;
472
BOOST_SYSTEM_DECL const error_category & generic_category() BOOST_SYSTEM_NOEXCEPT
474
static const generic_error_category generic_category_const;
475
return generic_category_const;
478
} // namespace system