2012-09-08 08:17:54 +02:00
|
|
|
/* $OpenBSD: strerror_r.c,v 1.6 2005/08/08 08:05:37 espie Exp $ */
|
|
|
|
/* Public Domain <marc@snafu.org> */
|
|
|
|
|
2014-08-19 02:28:32 +02:00
|
|
|
// G++ automatically defines _GNU_SOURCE, which then means that <string.h>
|
|
|
|
// gives us the GNU variant.
|
|
|
|
#undef _GNU_SOURCE
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-09-08 08:17:54 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
#include <async_safe/log.h>
|
|
|
|
|
2013-02-12 17:40:24 +01:00
|
|
|
#include "private/ErrnoRestorer.h"
|
|
|
|
|
2012-09-08 08:17:54 +02:00
|
|
|
struct Pair {
|
|
|
|
int code;
|
|
|
|
const char* msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* __code_string_lookup(const Pair* strings, int code) {
|
2018-08-03 02:31:13 +02:00
|
|
|
for (size_t i = 0; strings[i].msg != nullptr; ++i) {
|
2012-09-08 08:17:54 +02:00
|
|
|
if (strings[i].code == code) {
|
|
|
|
return strings[i].msg;
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2012-09-08 08:17:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const Pair _sys_error_strings[] = {
|
|
|
|
#define __BIONIC_ERRDEF(x,y,z) { x, z },
|
2016-04-14 02:08:43 +02:00
|
|
|
#include "private/bionic_errdefs.h"
|
2018-08-03 02:31:13 +02:00
|
|
|
{ 0, nullptr }
|
2012-09-08 08:17:54 +02:00
|
|
|
};
|
|
|
|
|
2012-09-22 03:27:40 +02:00
|
|
|
extern "C" __LIBC_HIDDEN__ const char* __strerror_lookup(int error_number) {
|
2012-09-18 00:23:35 +02:00
|
|
|
return __code_string_lookup(_sys_error_strings, error_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const Pair _sys_signal_strings[] = {
|
2014-02-12 04:57:06 +01:00
|
|
|
#define __BIONIC_SIGDEF(signal_number, signal_description) { signal_number, signal_description },
|
2016-04-14 02:08:43 +02:00
|
|
|
#include "private/bionic_sigdefs.h"
|
2018-08-03 02:31:13 +02:00
|
|
|
{ 0, nullptr }
|
2012-09-18 00:23:35 +02:00
|
|
|
};
|
|
|
|
|
2012-09-22 03:27:40 +02:00
|
|
|
extern "C" __LIBC_HIDDEN__ const char* __strsignal_lookup(int signal_number) {
|
2012-09-18 00:23:35 +02:00
|
|
|
return __code_string_lookup(_sys_signal_strings, signal_number);
|
|
|
|
}
|
|
|
|
|
2012-09-08 08:17:54 +02:00
|
|
|
int strerror_r(int error_number, char* buf, size_t buf_len) {
|
2013-02-12 17:40:24 +01:00
|
|
|
ErrnoRestorer errno_restorer;
|
2012-09-08 08:17:54 +02:00
|
|
|
size_t length;
|
|
|
|
|
2012-09-18 00:23:35 +02:00
|
|
|
const char* error_name = __strerror_lookup(error_number);
|
2018-08-03 02:31:13 +02:00
|
|
|
if (error_name != nullptr) {
|
2014-07-10 21:34:23 +02:00
|
|
|
length = strlcpy(buf, error_name, buf_len);
|
2012-09-08 08:17:54 +02:00
|
|
|
} else {
|
2017-04-25 02:48:32 +02:00
|
|
|
length = async_safe_format_buffer(buf, buf_len, "Unknown error %d", error_number);
|
2012-09-08 08:17:54 +02:00
|
|
|
}
|
|
|
|
if (length >= buf_len) {
|
2013-02-12 17:40:24 +01:00
|
|
|
errno_restorer.override(ERANGE);
|
2012-09-08 08:17:54 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-19 02:28:32 +02:00
|
|
|
extern "C" char* __gnu_strerror_r(int error_number, char* buf, size_t buf_len) {
|
|
|
|
ErrnoRestorer errno_restorer; // The glibc strerror_r doesn't set errno if it truncates...
|
|
|
|
strerror_r(error_number, buf, buf_len);
|
|
|
|
return buf; // ...and just returns whatever fit.
|
|
|
|
}
|
|
|
|
|
2012-09-22 03:27:40 +02:00
|
|
|
extern "C" __LIBC_HIDDEN__ const char* __strsignal(int signal_number, char* buf, size_t buf_len) {
|
2012-09-18 00:23:35 +02:00
|
|
|
const char* signal_name = __strsignal_lookup(signal_number);
|
2018-08-03 02:31:13 +02:00
|
|
|
if (signal_name != nullptr) {
|
2012-09-08 08:17:54 +02:00
|
|
|
return signal_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* prefix = "Unknown";
|
|
|
|
if (signal_number >= SIGRTMIN && signal_number <= SIGRTMAX) {
|
|
|
|
prefix = "Real-time";
|
|
|
|
signal_number -= SIGRTMIN;
|
|
|
|
}
|
|
|
|
size_t length = snprintf(buf, buf_len, "%s signal %d", prefix, signal_number);
|
|
|
|
if (length >= buf_len) {
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2012-09-08 08:17:54 +02:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|