File: errno_to_string.cc

package info (click to toggle)
xapian-core 1.4.29-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 22,840 kB
  • sloc: cpp: 92,356; ansic: 9,948; sh: 5,026; perl: 850; makefile: 509; javascript: 360; tcl: 319; python: 40
file content (124 lines) | stat: -rw-r--r-- 4,537 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/** @file
 * @brief Convert errno value to std::string, thread-safely if possible
 */
/* Copyright (C) 2014,2015,2016,2021 Olly Betts
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <config.h>

#include "errno_to_string.h"

// On Linux, sys_errlist and sys_nerr need <stdio.h>, though the man page
// says <errno.h>.  The man page seems to match other platforms such as
// NetBSD, so include both headers to ensure we get them.
#include <errno.h>
// <cstring> doesn't give us strerror_r() with Sun C++ 5.9.
#include <string.h>
#if defined HAVE__SYS_ERRLIST_AND__SYS_NERR || \
    defined HAVE_SYS_ERRLIST_AND_SYS_NERR
# include <stdio.h>
// With mingw and MSVC they're provided by <stdlib.h>.
# include <stdlib.h>
#endif

#include "str.h"

using namespace std;

void
errno_to_string(int e, string& s)
{
    // Use a thread-safe way to convert an errno value to a string if possible.
#ifdef HAVE_STRERRORDESC_NP
    // GNU-specific replacement for sys_errlist and sys_nerr, added in glibc
    // 2.32.
    const char* desc = strerrordesc_np(e);
    if (desc) {
	s += desc;
    } else {
	s += "Unknown error ";
	s += str(e);
    }
#elif defined HAVE__SYS_ERRLIST_AND__SYS_NERR
    // Old-style Unix fixed array of strings.
    if (e >= 0 && e < _sys_nerr && _sys_errlist[e]) {
	s += _sys_errlist[e];
    } else {
	s += "Unknown error ";
	s += str(e);
    }
#elif defined HAVE_SYS_ERRLIST_AND_SYS_NERR
    // Old-style Unix fixed array of strings.
    if (e >= 0 && e < sys_nerr && sys_errlist[e]) {
	s += sys_errlist[e];
    } else {
	s += "Unknown error ";
	s += str(e);
    }
#elif HAVE_DECL_STRERROR_R
    // POSIX specifies strerror_r() to provide a thread-safe way to translate
    // an errno value to a string.
    //
    // Unhelpfully this requires us to pass in a buffer, but we don't know how
    // big to make it to reliably be able to convert all possible errno values,
    // and the implementation is permitted to return localised strings so the
    // maximum possible length may vary depending on the current locale
    // settings.
    //
    // If the buffer passed is too small, then with older glibc errno gets
    // stomped on, so growing the buffer on error and retrying isn't a great
    // answer.  Hence we only use strerror_r() if we don't have a better
    // alternative.
    //
    // Another reason to have support for alternative approaches is that
    // strerror_r() is marked as "optional" by POSIX.
    //
    // A further complication is there's a GNU-specific strerror_r() with a
    // different return value type.
    //
    // The strerror_r(3) man page on Linux suggests a buffer size of 1024
    // characters, noting that glibc uses this size for strerror().  The
    // actual longest on Linux in English is EILSEQ which needs 50 bytes.
    char buf[1024];
# ifdef STRERROR_R_CHAR_P
    // Returns char* pointing to string describing error.
    s += strerror_r(e, buf, sizeof(buf));
# else
    // XSI-compliant strerror_r returns int:  0 means success; a positive error
    // number should be returned on error, but glibc < 2.13 returns -1 and sets
    // errno.
    int r = strerror_r(e, buf, sizeof(buf));
    if (r == 0) {
	s += buf;
    } else {
	s += "Unknown error ";
	s += str(e);
    }
# endif
#else
    // Not thread safe.
    //
    // We can assume the return value is non-NULL because "C99 and POSIX.1-2008
    // require the return value to be non-NULL" and we require C++11 which
    // incorporates C99.
    s += strerror(e);
#endif
}