errname.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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>
  7. /*
  8. * Ensure these tables do not accidentally become gigantic if some
  9. * huge errno makes it in. On most architectures, the first table will
  10. * only have about 140 entries, but mips and parisc have more sparsely
  11. * allocated errnos (with EHWPOISON = 257 on parisc, and EDQUOT = 1133
  12. * on mips), so this wastes a bit of space on those - though we
  13. * special case the EDQUOT case.
  14. */
  15. #define E(err) [err + BUILD_BUG_ON_ZERO(err <= 0 || err > 300)] = "-" #err
  16. static const char *names_0[] = {
  17. E(E2BIG),
  18. E(EACCES),
  19. E(EADDRINUSE),
  20. E(EADDRNOTAVAIL),
  21. E(EADV),
  22. E(EAFNOSUPPORT),
  23. E(EAGAIN), /* EWOULDBLOCK */
  24. E(EALREADY),
  25. E(EBADE),
  26. E(EBADF),
  27. E(EBADFD),
  28. E(EBADMSG),
  29. E(EBADR),
  30. E(EBADRQC),
  31. E(EBADSLT),
  32. E(EBFONT),
  33. E(EBUSY),
  34. E(ECANCELED), /* ECANCELLED */
  35. E(ECHILD),
  36. E(ECHRNG),
  37. E(ECOMM),
  38. E(ECONNABORTED),
  39. E(ECONNREFUSED), /* EREFUSED */
  40. E(ECONNRESET),
  41. E(EDEADLK), /* EDEADLOCK */
  42. #if EDEADLK != EDEADLOCK /* mips, sparc, powerpc */
  43. E(EDEADLOCK),
  44. #endif
  45. E(EDESTADDRREQ),
  46. E(EDOM),
  47. E(EDOTDOT),
  48. #ifndef CONFIG_MIPS
  49. E(EDQUOT),
  50. #endif
  51. E(EEXIST),
  52. E(EFAULT),
  53. E(EFBIG),
  54. E(EHOSTDOWN),
  55. E(EHOSTUNREACH),
  56. E(EHWPOISON),
  57. E(EIDRM),
  58. E(EILSEQ),
  59. #ifdef EINIT
  60. E(EINIT),
  61. #endif
  62. E(EINPROGRESS),
  63. E(EINTR),
  64. E(EINVAL),
  65. E(EIO),
  66. E(EISCONN),
  67. E(EISDIR),
  68. E(EISNAM),
  69. E(EKEYEXPIRED),
  70. E(EKEYREJECTED),
  71. E(EKEYREVOKED),
  72. E(EL2HLT),
  73. E(EL2NSYNC),
  74. E(EL3HLT),
  75. E(EL3RST),
  76. E(ELIBACC),
  77. E(ELIBBAD),
  78. E(ELIBEXEC),
  79. E(ELIBMAX),
  80. E(ELIBSCN),
  81. E(ELNRNG),
  82. E(ELOOP),
  83. E(EMEDIUMTYPE),
  84. E(EMFILE),
  85. E(EMLINK),
  86. E(EMSGSIZE),
  87. E(EMULTIHOP),
  88. E(ENAMETOOLONG),
  89. E(ENAVAIL),
  90. E(ENETDOWN),
  91. E(ENETRESET),
  92. E(ENETUNREACH),
  93. E(ENFILE),
  94. E(ENOANO),
  95. E(ENOBUFS),
  96. E(ENOCSI),
  97. E(ENODATA),
  98. E(ENODEV),
  99. E(ENOENT),
  100. E(ENOEXEC),
  101. E(ENOKEY),
  102. E(ENOLCK),
  103. E(ENOLINK),
  104. E(ENOMEDIUM),
  105. E(ENOMEM),
  106. E(ENOMSG),
  107. E(ENONET),
  108. E(ENOPKG),
  109. E(ENOPROTOOPT),
  110. E(ENOSPC),
  111. E(ENOSR),
  112. E(ENOSTR),
  113. E(ENOSYS),
  114. E(ENOTBLK),
  115. E(ENOTCONN),
  116. E(ENOTDIR),
  117. E(ENOTEMPTY),
  118. E(ENOTNAM),
  119. E(ENOTRECOVERABLE),
  120. E(ENOTSOCK),
  121. E(ENOTTY),
  122. E(ENOTUNIQ),
  123. E(ENXIO),
  124. E(EOPNOTSUPP),
  125. E(EOVERFLOW),
  126. E(EOWNERDEAD),
  127. E(EPERM),
  128. E(EPFNOSUPPORT),
  129. E(EPIPE),
  130. #ifdef EPROCLIM
  131. E(EPROCLIM),
  132. #endif
  133. E(EPROTO),
  134. E(EPROTONOSUPPORT),
  135. E(EPROTOTYPE),
  136. E(ERANGE),
  137. E(EREMCHG),
  138. #ifdef EREMDEV
  139. E(EREMDEV),
  140. #endif
  141. E(EREMOTE),
  142. E(EREMOTEIO),
  143. E(ERESTART),
  144. E(ERFKILL),
  145. E(EROFS),
  146. #ifdef ERREMOTE
  147. E(ERREMOTE),
  148. #endif
  149. E(ESHUTDOWN),
  150. E(ESOCKTNOSUPPORT),
  151. E(ESPIPE),
  152. E(ESRCH),
  153. E(ESRMNT),
  154. E(ESTALE),
  155. E(ESTRPIPE),
  156. E(ETIME),
  157. E(ETIMEDOUT),
  158. E(ETOOMANYREFS),
  159. E(ETXTBSY),
  160. E(EUCLEAN),
  161. E(EUNATCH),
  162. E(EUSERS),
  163. E(EXDEV),
  164. E(EXFULL),
  165. };
  166. #undef E
  167. #ifdef EREFUSED /* parisc */
  168. static_assert(EREFUSED == ECONNREFUSED);
  169. #endif
  170. #ifdef ECANCELLED /* parisc */
  171. static_assert(ECANCELLED == ECANCELED);
  172. #endif
  173. static_assert(EAGAIN == EWOULDBLOCK); /* everywhere */
  174. #define E(err) [err - 512 + BUILD_BUG_ON_ZERO(err < 512 || err > 550)] = "-" #err
  175. static const char *names_512[] = {
  176. E(ERESTARTSYS),
  177. E(ERESTARTNOINTR),
  178. E(ERESTARTNOHAND),
  179. E(ENOIOCTLCMD),
  180. E(ERESTART_RESTARTBLOCK),
  181. E(EPROBE_DEFER),
  182. E(EOPENSTALE),
  183. E(ENOPARAM),
  184. E(EBADHANDLE),
  185. E(ENOTSYNC),
  186. E(EBADCOOKIE),
  187. E(ENOTSUPP),
  188. E(ETOOSMALL),
  189. E(ESERVERFAULT),
  190. E(EBADTYPE),
  191. E(EJUKEBOX),
  192. E(EIOCBQUEUED),
  193. E(ERECALLCONFLICT),
  194. };
  195. #undef E
  196. static const char *__errname(unsigned err)
  197. {
  198. if (err < ARRAY_SIZE(names_0))
  199. return names_0[err];
  200. if (err >= 512 && err - 512 < ARRAY_SIZE(names_512))
  201. return names_512[err - 512];
  202. /* But why? */
  203. if (IS_ENABLED(CONFIG_MIPS) && err == EDQUOT) /* 1133 */
  204. return "-EDQUOT";
  205. return NULL;
  206. }
  207. /*
  208. * errname(EIO) -> "EIO"
  209. * errname(-EIO) -> "-EIO"
  210. */
  211. const char *errname(int err)
  212. {
  213. const char *name = __errname(abs(err));
  214. if (!name)
  215. return NULL;
  216. return err > 0 ? name + 1 : name;
  217. }