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
using namespace boost::system;
26
using namespace boost::system::errc;
28
#include <cstring> // for strerror/strerror_r
30
# if defined( BOOST_WINDOWS_API )
32
# include "local_free_on_destruction.hpp"
33
# ifndef ERROR_INCORRECT_SIZE
34
# define ERROR_INCORRECT_SIZE ERROR_BAD_ARGUMENTS
38
//----------------------------------------------------------------------------//
42
// standard error categories ---------------------------------------------//
44
class generic_error_category : public error_category
47
generic_error_category(){}
48
const char * name() const;
49
std::string message( int ev ) const;
52
class system_error_category : public error_category
55
system_error_category(){}
56
const char * name() const;
57
std::string message( int ev ) const;
58
error_condition default_error_condition( int ev ) const;
61
// generic_error_category implementation ---------------------------------//
63
const char * generic_error_category::name() const
68
std::string generic_error_category::message( int ev ) const
70
static std::string unknown_err( "Unknown error" );
71
// strerror_r is preferred because it is always thread safe,
72
// however, we fallback to strerror in certain cases because:
73
// -- Windows doesn't provide strerror_r.
74
// -- HP and Sundo provide strerror_r on newer systems, but there is
75
// no way to tell if is available at runtime and in any case their
76
// versions of strerror are thread safe anyhow.
77
// -- Linux only sometimes provides strerror_r.
78
// -- Tru64 provides strerror_r only when compiled -pthread.
79
// -- VMS doesn't provide strerror_r, but on this platform, strerror is
81
# if defined(BOOST_WINDOWS_API) || defined(__hpux) || defined(__sun)\
82
|| (defined(__linux) && (!defined(__USE_XOPEN2K) || defined(BOOST_SYSTEM_USE_STRERROR)))\
83
|| (defined(__osf__) && !defined(_REENTRANT))\
85
|| (defined(__QNXNTO__))
86
const char * c_str = std::strerror( ev );
88
? std::string( c_str )
90
# else // use strerror_r
93
std::size_t sz = sizeof(buf);
94
# if defined(__CYGWIN__) || defined(__USE_GNU)
95
// Oddball version of strerror_r
96
const char * c_str = strerror_r( ev, bp, sz );
98
? std::string( c_str )
101
// POSIX version of strerror_r
105
// strerror_r returns 0 on success, otherwise ERANGE if buffer too small,
106
// invalid_argument if ev not a valid error number
108
const char * c_str = strerror( ev );
111
? std::string( c_str )
114
result = strerror_r( ev, bp, sz );
120
# if defined(__linux)
121
// Linux strerror_r returns -1 on error, with error number in errno
124
if ( result != ERANGE ) break;
125
if ( sz > sizeof(buf) ) std::free( bp );
127
if ( (bp = static_cast<char*>(std::malloc( sz ))) == 0 )
128
return std::string( "ENOMEM" );
134
msg = ( ( result == invalid_argument ) ? "Unknown error" : bp );
137
# ifndef BOOST_NO_EXCEPTIONS
141
// just eat the exception
145
if ( sz > sizeof(buf) ) std::free( bp );
148
# endif // else POSIX version of strerror_r
149
# endif // else use strerror_r
151
// system_error_category implementation --------------------------------//
153
const char * system_error_category::name() const
158
error_condition system_error_category::default_error_condition( int ev ) const
162
case 0: return make_error_condition( success );
163
# if defined(BOOST_POSIX_API)
164
// POSIX-like O/S -> posix_errno decode table ---------------------------//
165
case E2BIG: return make_error_condition( argument_list_too_long );
166
case EACCES: return make_error_condition( permission_denied );
167
case EADDRINUSE: return make_error_condition( address_in_use );
168
case EADDRNOTAVAIL: return make_error_condition( address_not_available );
169
case EAFNOSUPPORT: return make_error_condition( address_family_not_supported );
170
case EAGAIN: return make_error_condition( resource_unavailable_try_again );
171
# if EALREADY != EBUSY // EALREADY and EBUSY are the same on QNX Neutrino
172
case EALREADY: return make_error_condition( connection_already_in_progress );
174
case EBADF: return make_error_condition( bad_file_descriptor );
175
case EBADMSG: return make_error_condition( bad_message );
176
case EBUSY: return make_error_condition( device_or_resource_busy );
177
case ECANCELED: return make_error_condition( operation_canceled );
178
case ECHILD: return make_error_condition( no_child_process );
179
case ECONNABORTED: return make_error_condition( connection_aborted );
180
case ECONNREFUSED: return make_error_condition( connection_refused );
181
case ECONNRESET: return make_error_condition( connection_reset );
182
case EDEADLK: return make_error_condition( resource_deadlock_would_occur );
183
case EDESTADDRREQ: return make_error_condition( destination_address_required );
184
case EDOM: return make_error_condition( argument_out_of_domain );
185
case EEXIST: return make_error_condition( file_exists );
186
case EFAULT: return make_error_condition( bad_address );
187
case EFBIG: return make_error_condition( file_too_large );
188
case EHOSTUNREACH: return make_error_condition( host_unreachable );
189
case EIDRM: return make_error_condition( identifier_removed );
190
case EILSEQ: return make_error_condition( illegal_byte_sequence );
191
case EINPROGRESS: return make_error_condition( operation_in_progress );
192
case EINTR: return make_error_condition( interrupted );
193
case EINVAL: return make_error_condition( invalid_argument );
194
case EIO: return make_error_condition( io_error );
195
case EISCONN: return make_error_condition( already_connected );
196
case EISDIR: return make_error_condition( is_a_directory );
197
case ELOOP: return make_error_condition( too_many_symbolic_link_levels );
198
case EMFILE: return make_error_condition( too_many_files_open );
199
case EMLINK: return make_error_condition( too_many_links );
200
case EMSGSIZE: return make_error_condition( message_size );
201
case ENAMETOOLONG: return make_error_condition( filename_too_long );
202
case ENETDOWN: return make_error_condition( network_down );
203
case ENETRESET: return make_error_condition( network_reset );
204
case ENETUNREACH: return make_error_condition( network_unreachable );
205
case ENFILE: return make_error_condition( too_many_files_open_in_system );
206
case ENOBUFS: return make_error_condition( no_buffer_space );
207
case ENODATA: return make_error_condition( no_message_available );
208
case ENODEV: return make_error_condition( no_such_device );
209
case ENOENT: return make_error_condition( no_such_file_or_directory );
210
case ENOEXEC: return make_error_condition( executable_format_error );
211
case ENOLCK: return make_error_condition( no_lock_available );
212
case ENOLINK: return make_error_condition( no_link );
213
case ENOMEM: return make_error_condition( not_enough_memory );
214
case ENOMSG: return make_error_condition( no_message );
215
case ENOPROTOOPT: return make_error_condition( no_protocol_option );
216
case ENOSPC: return make_error_condition( no_space_on_device );
217
case ENOSR: return make_error_condition( no_stream_resources );
218
case ENOSTR: return make_error_condition( not_a_stream );
219
case ENOSYS: return make_error_condition( function_not_supported );
220
case ENOTCONN: return make_error_condition( not_connected );
221
case ENOTDIR: return make_error_condition( not_a_directory );
222
# if ENOTEMPTY != EEXIST // AIX treats ENOTEMPTY and EEXIST as the same value
223
case ENOTEMPTY: return make_error_condition( directory_not_empty );
224
# endif // ENOTEMPTY != EEXIST
225
# if ENOTRECOVERABLE != ECONNRESET // the same on some Broadcom chips
226
case ENOTRECOVERABLE: return make_error_condition( state_not_recoverable );
227
# endif // ENOTRECOVERABLE != ECONNRESET
228
case ENOTSOCK: return make_error_condition( not_a_socket );
229
case ENOTSUP: return make_error_condition( not_supported );
230
case ENOTTY: return make_error_condition( inappropriate_io_control_operation );
231
case ENXIO: return make_error_condition( no_such_device_or_address );
232
# if EOPNOTSUPP != ENOTSUP
233
case EOPNOTSUPP: return make_error_condition( operation_not_supported );
234
# endif // EOPNOTSUPP != ENOTSUP
235
case EOVERFLOW: return make_error_condition( value_too_large );
236
# if EOWNERDEAD != ECONNABORTED // the same on some Broadcom chips
237
case EOWNERDEAD: return make_error_condition( owner_dead );
238
# endif // EOWNERDEAD != ECONNABORTED
239
case EPERM: return make_error_condition( operation_not_permitted );
240
case EPIPE: return make_error_condition( broken_pipe );
241
case EPROTO: return make_error_condition( protocol_error );
242
case EPROTONOSUPPORT: return make_error_condition( protocol_not_supported );
243
case EPROTOTYPE: return make_error_condition( wrong_protocol_type );
244
case ERANGE: return make_error_condition( result_out_of_range );
245
case EROFS: return make_error_condition( read_only_file_system );
246
case ESPIPE: return make_error_condition( invalid_seek );
247
case ESRCH: return make_error_condition( no_such_process );
248
case ETIME: return make_error_condition( stream_timeout );
249
case ETIMEDOUT: return make_error_condition( timed_out );
250
case ETXTBSY: return make_error_condition( text_file_busy );
251
# if EAGAIN != EWOULDBLOCK
252
case EWOULDBLOCK: return make_error_condition( operation_would_block );
253
# endif // EAGAIN != EWOULDBLOCK
254
case EXDEV: return make_error_condition( cross_device_link );
256
// Windows system -> posix_errno decode table ---------------------------//
257
// see WinError.h comments for descriptions of errors
258
case ERROR_ACCESS_DENIED: return make_error_condition( permission_denied );
259
case ERROR_ALREADY_EXISTS: return make_error_condition( file_exists );
260
case ERROR_BAD_UNIT: return make_error_condition( no_such_device );
261
case ERROR_BUFFER_OVERFLOW: return make_error_condition( filename_too_long );
262
case ERROR_BUSY: return make_error_condition( device_or_resource_busy );
263
case ERROR_BUSY_DRIVE: return make_error_condition( device_or_resource_busy );
264
case ERROR_CANNOT_MAKE: return make_error_condition( permission_denied );
265
case ERROR_CANTOPEN: return make_error_condition( io_error );
266
case ERROR_CANTREAD: return make_error_condition( io_error );
267
case ERROR_CANTWRITE: return make_error_condition( io_error );
268
case ERROR_CURRENT_DIRECTORY: return make_error_condition( permission_denied );
269
case ERROR_DEV_NOT_EXIST: return make_error_condition( no_such_device );
270
case ERROR_DEVICE_IN_USE: return make_error_condition( device_or_resource_busy );
271
case ERROR_DIR_NOT_EMPTY: return make_error_condition( directory_not_empty );
272
case ERROR_DIRECTORY: return make_error_condition( invalid_argument ); // WinError.h: "The directory name is invalid"
273
case ERROR_DISK_FULL: return make_error_condition( no_space_on_device );
274
case ERROR_FILE_EXISTS: return make_error_condition( file_exists );
275
case ERROR_FILE_NOT_FOUND: return make_error_condition( no_such_file_or_directory );
276
case ERROR_HANDLE_DISK_FULL: return make_error_condition( no_space_on_device );
277
case ERROR_INVALID_ACCESS: return make_error_condition( permission_denied );
278
case ERROR_INVALID_DRIVE: return make_error_condition( no_such_device );
279
case ERROR_INVALID_FUNCTION: return make_error_condition( function_not_supported );
280
case ERROR_INVALID_HANDLE: return make_error_condition( invalid_argument );
281
case ERROR_INVALID_NAME: return make_error_condition( invalid_argument );
282
case ERROR_LOCK_VIOLATION: return make_error_condition( no_lock_available );
283
case ERROR_LOCKED: return make_error_condition( no_lock_available );
284
case ERROR_NEGATIVE_SEEK: return make_error_condition( invalid_argument );
285
case ERROR_NOACCESS: return make_error_condition( permission_denied );
286
case ERROR_NOT_ENOUGH_MEMORY: return make_error_condition( not_enough_memory );
287
case ERROR_NOT_READY: return make_error_condition( resource_unavailable_try_again );
288
case ERROR_NOT_SAME_DEVICE: return make_error_condition( cross_device_link );
289
case ERROR_OPEN_FAILED: return make_error_condition( io_error );
290
case ERROR_OPEN_FILES: return make_error_condition( device_or_resource_busy );
291
case ERROR_OPERATION_ABORTED: return make_error_condition( operation_canceled );
292
case ERROR_OUTOFMEMORY: return make_error_condition( not_enough_memory );
293
case ERROR_PATH_NOT_FOUND: return make_error_condition( no_such_file_or_directory );
294
case ERROR_READ_FAULT: return make_error_condition( io_error );
295
case ERROR_RETRY: return make_error_condition( resource_unavailable_try_again );
296
case ERROR_SEEK: return make_error_condition( io_error );
297
case ERROR_SHARING_VIOLATION: return make_error_condition( permission_denied );
298
case ERROR_TOO_MANY_OPEN_FILES: return make_error_condition( too_many_files_open );
299
case ERROR_WRITE_FAULT: return make_error_condition( io_error );
300
case ERROR_WRITE_PROTECT: return make_error_condition( permission_denied );
301
case WSAEACCES: return make_error_condition( permission_denied );
302
case WSAEADDRINUSE: return make_error_condition( address_in_use );
303
case WSAEADDRNOTAVAIL: return make_error_condition( address_not_available );
304
case WSAEAFNOSUPPORT: return make_error_condition( address_family_not_supported );
305
case WSAEALREADY: return make_error_condition( connection_already_in_progress );
306
case WSAEBADF: return make_error_condition( bad_file_descriptor );
307
case WSAECONNABORTED: return make_error_condition( connection_aborted );
308
case WSAECONNREFUSED: return make_error_condition( connection_refused );
309
case WSAECONNRESET: return make_error_condition( connection_reset );
310
case WSAEDESTADDRREQ: return make_error_condition( destination_address_required );
311
case WSAEFAULT: return make_error_condition( bad_address );
312
case WSAEHOSTUNREACH: return make_error_condition( host_unreachable );
313
case WSAEINPROGRESS: return make_error_condition( operation_in_progress );
314
case WSAEINTR: return make_error_condition( interrupted );
315
case WSAEINVAL: return make_error_condition( invalid_argument );
316
case WSAEISCONN: return make_error_condition( already_connected );
317
case WSAEMFILE: return make_error_condition( too_many_files_open );
318
case WSAEMSGSIZE: return make_error_condition( message_size );
319
case WSAENAMETOOLONG: return make_error_condition( filename_too_long );
320
case WSAENETDOWN: return make_error_condition( network_down );
321
case WSAENETRESET: return make_error_condition( network_reset );
322
case WSAENETUNREACH: return make_error_condition( network_unreachable );
323
case WSAENOBUFS: return make_error_condition( no_buffer_space );
324
case WSAENOPROTOOPT: return make_error_condition( no_protocol_option );
325
case WSAENOTCONN: return make_error_condition( not_connected );
326
case WSAENOTSOCK: return make_error_condition( not_a_socket );
327
case WSAEOPNOTSUPP: return make_error_condition( operation_not_supported );
328
case WSAEPROTONOSUPPORT: return make_error_condition( protocol_not_supported );
329
case WSAEPROTOTYPE: return make_error_condition( wrong_protocol_type );
330
case WSAETIMEDOUT: return make_error_condition( timed_out );
331
case WSAEWOULDBLOCK: return make_error_condition( operation_would_block );
333
default: return error_condition( ev, system_category() );
337
# if !defined( BOOST_WINDOWS_API )
339
std::string system_error_category::message( int ev ) const
341
return generic_category().message( ev );
345
std::string system_error_category::message( int ev ) const
347
# ifndef BOOST_NO_ANSI_APIS
349
DWORD retval = ::FormatMessageA(
350
FORMAT_MESSAGE_ALLOCATE_BUFFER |
351
FORMAT_MESSAGE_FROM_SYSTEM |
352
FORMAT_MESSAGE_IGNORE_INSERTS,
355
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
360
detail::local_free_on_destruction lfod(lpMsgBuf);
362
return std::string("Unknown error");
364
std::string str( static_cast<LPCSTR>(lpMsgBuf) );
365
# else // WinCE workaround
367
DWORD retval = ::FormatMessageW(
368
FORMAT_MESSAGE_ALLOCATE_BUFFER |
369
FORMAT_MESSAGE_FROM_SYSTEM |
370
FORMAT_MESSAGE_IGNORE_INSERTS,
373
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
378
detail::local_free_on_destruction lfod(lpMsgBuf);
380
return std::string("Unknown error");
382
int num_chars = (wcslen( static_cast<LPCWSTR>(lpMsgBuf) ) + 1) * 2;
383
LPSTR narrow_buffer = (LPSTR)_alloca( num_chars );
384
if (::WideCharToMultiByte(CP_ACP, 0, static_cast<LPCWSTR>(lpMsgBuf), -1, narrow_buffer, num_chars, NULL, NULL) == 0)
385
return std::string("Unknown error");
387
std::string str( narrow_buffer );
390
&& (str[str.size()-1] == '\n' || str[str.size()-1] == '\r') )
391
str.erase( str.size()-1 );
392
if ( str.size() && str[str.size()-1] == '.' )
393
{ str.erase( str.size()-1 ); }
398
} // unnamed namespace
405
# ifndef BOOST_SYSTEM_NO_DEPRECATED
406
BOOST_SYSTEM_DECL error_code throws; // "throw on error" special error_code;
407
// note that it doesn't matter if this
408
// isn't initialized before use since
409
// the only use is to take its
410
// address for comparison purposes
413
BOOST_SYSTEM_DECL const error_category & system_category()
415
static const system_error_category system_category_const;
416
return system_category_const;
419
BOOST_SYSTEM_DECL const error_category & generic_category()
421
static const generic_error_category generic_category_const;
422
return generic_category_const;
425
} // namespace system