New upstream version 18.11-rc2
[deb_dpdk.git] / lib / librte_eal / common / eal_common_errno.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 /* Use XSI-compliant portable version of strerror_r() */
6 #undef _GNU_SOURCE
7
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdarg.h>
12 #include <errno.h>
13
14 #include <rte_per_lcore.h>
15 #include <rte_errno.h>
16 #include <rte_string_fns.h>
17
18 RTE_DEFINE_PER_LCORE(int, _rte_errno);
19
20 const char *
21 rte_strerror(int errnum)
22 {
23         /* BSD puts a colon in the "unknown error" messages, Linux doesn't */
24 #ifdef RTE_EXEC_ENV_BSDAPP
25         static const char *sep = ":";
26 #else
27         static const char *sep = "";
28 #endif
29 #define RETVAL_SZ 256
30         static RTE_DEFINE_PER_LCORE(char[RETVAL_SZ], retval);
31         char *ret = RTE_PER_LCORE(retval);
32
33         /* since some implementations of strerror_r throw an error
34          * themselves if errnum is too big, we handle that case here */
35         if (errnum >= RTE_MAX_ERRNO)
36                 snprintf(ret, RETVAL_SZ, "Unknown error%s %d", sep, errnum);
37         else
38                 switch (errnum){
39                 case E_RTE_SECONDARY:
40                         return "Invalid call in secondary process";
41                 case E_RTE_NO_CONFIG:
42                         return "Missing rte_config structure";
43                 default:
44                         if (strerror_r(errnum, ret, RETVAL_SZ) != 0)
45                                 snprintf(ret, RETVAL_SZ, "Unknown error%s %d",
46                                                 sep, errnum);
47                 }
48
49         return ret;
50 }