1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/build_bug.h>
3 #include <linux/errno.h>
4 #include <linux/errname.h>
5 #include <linux/kernel.h>
6 #include <linux/math.h>
9 * Ensure these tables do not accidentally become gigantic if some
10 * huge errno makes it in. On most architectures, the first table will
11 * only have about 140 entries, but mips and parisc have more sparsely
12 * allocated errnos (with EHWPOISON = 257 on parisc, and EDQUOT = 1133
13 * on mips), so this wastes a bit of space on those - though we
14 * special case the EDQUOT case.
16 #define E(err) [err + BUILD_BUG_ON_ZERO(err <= 0 || err > 300)] = "-" #err
17 static const char *names_0[] = {
24 E(EAGAIN), /* EWOULDBLOCK */
35 E(ECANCELED), /* ECANCELLED */
40 E(ECONNREFUSED), /* EREFUSED */
42 E(EDEADLK), /* EDEADLOCK */
43 #if EDEADLK != EDEADLOCK /* mips, sparc, powerpc */
169 #ifdef EREFUSED /* parisc */
170 static_assert(EREFUSED == ECONNREFUSED);
172 #ifdef ECANCELLED /* parisc */
173 static_assert(ECANCELLED == ECANCELED);
175 static_assert(EAGAIN == EWOULDBLOCK); /* everywhere */
177 #define E(err) [err - 512 + BUILD_BUG_ON_ZERO(err < 512 || err > 550)] = "-" #err
178 static const char *names_512[] = {
183 E(ERESTART_RESTARTBLOCK),
201 static const char *__errname(unsigned err)
203 if (err < ARRAY_SIZE(names_0))
205 if (err >= 512 && err - 512 < ARRAY_SIZE(names_512))
206 return names_512[err - 512];
208 if (IS_ENABLED(CONFIG_MIPS) && err == EDQUOT) /* 1133 */
214 * errname(EIO) -> "EIO"
215 * errname(-EIO) -> "-EIO"
217 const char *errname(int err)
219 const char *name = __errname(abs(err));
223 return err > 0 ? name + 1 : name;