199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson */
499a2dd95SBruce Richardson
599a2dd95SBruce Richardson /* Use XSI-compliant portable version of strerror_r() */
699a2dd95SBruce Richardson #undef _GNU_SOURCE
733d66940SBruce Richardson #define _POSIX_C_SOURCE 200809L
899a2dd95SBruce Richardson
999a2dd95SBruce Richardson #include <stdio.h>
1099a2dd95SBruce Richardson #include <string.h>
1199a2dd95SBruce Richardson
1299a2dd95SBruce Richardson #include <rte_per_lcore.h>
1399a2dd95SBruce Richardson #include <rte_errno.h>
1499a2dd95SBruce Richardson
1599a2dd95SBruce Richardson #ifdef RTE_EXEC_ENV_WINDOWS
1699a2dd95SBruce Richardson #define strerror_r(errnum, buf, buflen) strerror_s(buf, buflen, errnum)
1799a2dd95SBruce Richardson #endif
1899a2dd95SBruce Richardson
1999a2dd95SBruce Richardson RTE_DEFINE_PER_LCORE(int, _rte_errno);
2099a2dd95SBruce Richardson
2199a2dd95SBruce Richardson const char *
rte_strerror(int errnum)2299a2dd95SBruce Richardson rte_strerror(int errnum)
2399a2dd95SBruce Richardson {
2499a2dd95SBruce Richardson /* BSD puts a colon in the "unknown error" messages, Linux doesn't */
2599a2dd95SBruce Richardson #ifdef RTE_EXEC_ENV_FREEBSD
2699a2dd95SBruce Richardson static const char *sep = ":";
2799a2dd95SBruce Richardson #else
2899a2dd95SBruce Richardson static const char *sep = "";
2999a2dd95SBruce Richardson #endif
3099a2dd95SBruce Richardson #define RETVAL_SZ 256
31*e578789dSTyler Retzlaff static RTE_DEFINE_PER_LCORE(char, retval[RETVAL_SZ]);
3299a2dd95SBruce Richardson char *ret = RTE_PER_LCORE(retval);
3399a2dd95SBruce Richardson
3499a2dd95SBruce Richardson /* since some implementations of strerror_r throw an error
3599a2dd95SBruce Richardson * themselves if errnum is too big, we handle that case here */
3699a2dd95SBruce Richardson if (errnum >= RTE_MAX_ERRNO)
37e14f1744SJie Zhou #ifdef RTE_EXEC_ENV_WINDOWS
38e14f1744SJie Zhou snprintf(ret, RETVAL_SZ, "Unknown error");
39e14f1744SJie Zhou #else
4099a2dd95SBruce Richardson snprintf(ret, RETVAL_SZ, "Unknown error%s %d", sep, errnum);
41e14f1744SJie Zhou #endif
4299a2dd95SBruce Richardson else
4399a2dd95SBruce Richardson switch (errnum){
4499a2dd95SBruce Richardson case E_RTE_SECONDARY:
4599a2dd95SBruce Richardson return "Invalid call in secondary process";
4699a2dd95SBruce Richardson case E_RTE_NO_CONFIG:
4799a2dd95SBruce Richardson return "Missing rte_config structure";
4899a2dd95SBruce Richardson default:
4999a2dd95SBruce Richardson if (strerror_r(errnum, ret, RETVAL_SZ) != 0)
5099a2dd95SBruce Richardson snprintf(ret, RETVAL_SZ, "Unknown error%s %d",
5199a2dd95SBruce Richardson sep, errnum);
5299a2dd95SBruce Richardson }
5399a2dd95SBruce Richardson
5499a2dd95SBruce Richardson return ret;
5599a2dd95SBruce Richardson }
56