2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
19 static const char * const error_codes[] = {
22 "Allocation size overflow",
23 "Integer is too large",
26 "Operation not supported",
29 "Option doesn't match",
30 "Record field not initialized",
31 "Array entry not initialized",
36 "Floating-point infinity",
37 "System returned invalid data",
45 "Gethostbyname error",
56 static const char * const system_error_codes[] = {
57 "Operation not permitted",
58 "No such file or directory",
60 "Interrupted system call",
62 "No such device or address",
63 "Argument list too long",
71 "Block device required",
72 "Device or resource busy",
79 "File table overflow",
80 "Too many open files",
84 "No space left on device",
86 "Read-only file system",
89 "Math argument out of domain of func",
90 "Math result not representable",
91 "Resource deadlock would occur",
93 "No record locks available",
94 "Invalid system call number",
95 "Directory not empty",
96 "Too many symbolic links encountered",
97 "No message of desired type",
99 "Channel number out of range",
100 "Level 2 not synchronized",
103 "Link number out of range",
104 "Protocol driver not attached",
105 "No CSI structure available",
108 "Invalid request descriptor",
111 "Invalid request code",
113 "Bad font file format",
114 "Device not a stream",
117 "Out of streams resources",
118 "Machine is not on the network",
119 "Package not installed",
121 "Link has been severed",
124 "Communication error on send",
126 "Multihop attempted",
127 "RFS specific error",
128 "Not a data message",
129 "Value too large for defined data type",
130 "Name not unique on network",
131 "File descriptor in bad state",
132 "Remote address changed",
133 "Can not access a needed shared library",
134 "Accessing a corrupted shared library",
135 ".lib section in a.out corrupted",
136 "Attempting to link in too many shared libraries",
137 "Cannot exec a shared library directly",
138 "Illegal byte sequence",
139 "Interrupted system call should be restarted",
140 "Streams pipe error",
142 "Socket operation on non-socket",
143 "Destination address required",
145 "Protocol wrong type for socket",
146 "Protocol not available",
147 "Protocol not supported",
148 "Socket type not supported",
149 "Operation not supported on transport endpoint",
150 "Protocol family not supported",
151 "Address family not supported by protocol",
152 "Address already in use",
153 "Cannot assign requested address",
155 "Network is unreachable",
156 "Network dropped connection because of reset",
157 "Software caused connection abort",
158 "Connection reset by peer",
159 "No buffer space available",
160 "Transport endpoint is already connected",
161 "Transport endpoint is not connected",
162 "Cannot send after transport endpoint shutdown",
163 "Too many references: cannot splice",
164 "Connection timed out",
165 "Connection refused",
168 "Operation already in progress",
169 "Operation now in progress",
171 "Structure needs cleaning",
172 "Not a XENIX named type file",
173 "No XENIX semaphores available",
174 "Is a named type file",
179 "Operation Canceled",
180 "Required key not available",
182 "Key has been revoked",
183 "Key was rejected by service",
185 "State not recoverable",
186 "Operation not possible due to RF-kill",
187 "Memory page has hardware error",
190 static const char attr_cold *error_ajla_decode(int error)
192 if (likely(error >= AJLA_ERROR_BASE) && likely(error < AJLA_ERROR_N))
193 return error_codes[error - AJLA_ERROR_BASE];
197 void attr_cold trace(const char attr_unused *m, ...)
207 void attr_cold stderr_msg(const char *m, ...)
215 void attr_cold debug(const char *m, ...)
223 void attr_cold warning(const char *m, ...)
231 attr_noreturn attr_cold fatal(const char *m, ...)
239 attr_noreturn attr_cold internal(const char *position, const char *m, ...)
243 internal_v(position, m, l);
247 void attr_cold attr_printf(3,4) fatal_mayfail(ajla_error_t e, ajla_error_t *mayfail, const char *m, ...)
249 if (mayfail == MEM_DONT_TRY_TO_FREE)
251 if (unlikely(!mayfail)) {
261 void attr_cold attr_printf(3,4) fatal_warning_mayfail(ajla_error_t e, ajla_error_t *mayfail, const char *m, ...)
263 if (unlikely(!mayfail)) {
274 if (mayfail != MEM_DONT_TRY_TO_FREE)
279 struct system_error_table_entry {
284 static const struct system_error_table_entry errno_to_system_error[] = {
286 { EPERM, SYSTEM_ERROR_EPERM },
289 { ENOENT, SYSTEM_ERROR_ENOENT },
292 { ESRCH, SYSTEM_ERROR_ESRCH },
295 { EINTR, SYSTEM_ERROR_EINTR },
298 { EIO, SYSTEM_ERROR_EIO },
301 { ENXIO, SYSTEM_ERROR_ENXIO },
304 { E2BIG, SYSTEM_ERROR_E2BIG },
307 { ENOEXEC, SYSTEM_ERROR_ENOEXEC },
310 { EBADF, SYSTEM_ERROR_EBADF },
313 { ECHILD, SYSTEM_ERROR_ECHILD },
316 { EAGAIN, SYSTEM_ERROR_EAGAIN },
319 { ENOMEM, SYSTEM_ERROR_ENOMEM },
322 { EACCES, SYSTEM_ERROR_EACCES },
325 { EFAULT, SYSTEM_ERROR_EFAULT },
328 { ENOTBLK, SYSTEM_ERROR_ENOTBLK },
331 { EBUSY, SYSTEM_ERROR_EBUSY },
334 { EEXIST, SYSTEM_ERROR_EEXIST },
337 { EXDEV, SYSTEM_ERROR_EXDEV },
340 { ENODEV, SYSTEM_ERROR_ENODEV },
343 { ENOTDIR, SYSTEM_ERROR_ENOTDIR },
346 { EISDIR, SYSTEM_ERROR_EISDIR },
349 { EINVAL, SYSTEM_ERROR_EINVAL },
352 { ENFILE, SYSTEM_ERROR_ENFILE },
355 { EMFILE, SYSTEM_ERROR_EMFILE },
358 { ENOTTY, SYSTEM_ERROR_ENOTTY },
361 { ETXTBSY, SYSTEM_ERROR_ETXTBSY },
364 { EFBIG, SYSTEM_ERROR_EFBIG },
367 { ENOSPC, SYSTEM_ERROR_ENOSPC },
370 { ESPIPE, SYSTEM_ERROR_ESPIPE },
373 { EROFS, SYSTEM_ERROR_EROFS },
376 { EMLINK, SYSTEM_ERROR_EMLINK },
379 { EPIPE, SYSTEM_ERROR_EPIPE },
382 { EDOM, SYSTEM_ERROR_EDOM },
385 { ERANGE, SYSTEM_ERROR_ERANGE },
388 { EDEADLK, SYSTEM_ERROR_EDEADLK },
391 { ENAMETOOLONG, SYSTEM_ERROR_ENAMETOOLONG },
394 { ENOLCK, SYSTEM_ERROR_ENOLCK },
397 { ENOSYS, SYSTEM_ERROR_ENOSYS },
400 { ENOTEMPTY, SYSTEM_ERROR_ENOTEMPTY },
403 { ELOOP, SYSTEM_ERROR_ELOOP },
406 { EWOULDBLOCK, SYSTEM_ERROR_EAGAIN },
409 { ENOMSG, SYSTEM_ERROR_ENOMSG },
412 { EIDRM, SYSTEM_ERROR_EIDRM },
415 { ECHRNG, SYSTEM_ERROR_ECHRNG },
418 { EL2NSYNC, SYSTEM_ERROR_EL2NSYNC },
421 { EL3HLT, SYSTEM_ERROR_EL3HLT },
424 { EL3RST, SYSTEM_ERROR_EL3RST },
427 { ELNRNG, SYSTEM_ERROR_ELNRNG },
430 { EUNATCH, SYSTEM_ERROR_EUNATCH },
433 { ENOCSI, SYSTEM_ERROR_ENOCSI },
436 { EL2HLT, SYSTEM_ERROR_EL2HLT },
439 { EBADE, SYSTEM_ERROR_EBADE },
442 { EBADR, SYSTEM_ERROR_EBADR },
445 { EXFULL, SYSTEM_ERROR_EXFULL },
448 { ENOANO, SYSTEM_ERROR_ENOANO },
451 { EBADRQC, SYSTEM_ERROR_EBADRQC },
454 { EBADSLT, SYSTEM_ERROR_EBADSLT },
457 { EDEADLOCK, SYSTEM_ERROR_EDEADLK },
460 { EBFONT, SYSTEM_ERROR_EBFONT },
463 { ENOSTR, SYSTEM_ERROR_ENOSTR },
466 { ENODATA, SYSTEM_ERROR_ENODATA },
469 { ETIME, SYSTEM_ERROR_ETIME },
472 { ENOSR, SYSTEM_ERROR_ENOSR },
475 { ENONET, SYSTEM_ERROR_ENONET },
478 { ENOPKG, SYSTEM_ERROR_ENOPKG },
481 { EREMOTE, SYSTEM_ERROR_EREMOTE },
484 { ENOLINK, SYSTEM_ERROR_ENOLINK },
487 { EADV, SYSTEM_ERROR_EADV },
490 { ESRMNT, SYSTEM_ERROR_ESRMNT },
493 { ECOMM, SYSTEM_ERROR_ECOMM },
496 { EPROTO, SYSTEM_ERROR_EPROTO },
499 { EMULTIHOP, SYSTEM_ERROR_EMULTIHOP },
502 { EDOTDOT, SYSTEM_ERROR_EDOTDOT },
505 { EBADMSG, SYSTEM_ERROR_EBADMSG },
508 { EOVERFLOW, SYSTEM_ERROR_EOVERFLOW },
511 { ENOTUNIQ, SYSTEM_ERROR_ENOTUNIQ },
514 { EBADFD, SYSTEM_ERROR_EBADFD },
517 { EREMCHG, SYSTEM_ERROR_EREMCHG },
520 { ELIBACC, SYSTEM_ERROR_ELIBACC },
523 { ELIBBAD, SYSTEM_ERROR_ELIBBAD },
526 { ELIBSCN, SYSTEM_ERROR_ELIBSCN },
529 { ELIBMAX, SYSTEM_ERROR_ELIBMAX },
532 { ELIBEXEC, SYSTEM_ERROR_ELIBEXEC },
535 { EILSEQ, SYSTEM_ERROR_EILSEQ },
538 { ERESTART, SYSTEM_ERROR_ERESTART },
541 { ESTRPIPE, SYSTEM_ERROR_ESTRPIPE },
544 { EUSERS, SYSTEM_ERROR_EUSERS },
547 { ENOTSOCK, SYSTEM_ERROR_ENOTSOCK },
550 { EDESTADDRREQ, SYSTEM_ERROR_EDESTADDRREQ },
553 { EMSGSIZE, SYSTEM_ERROR_EMSGSIZE },
556 { EPROTOTYPE, SYSTEM_ERROR_EPROTOTYPE },
559 { ENOPROTOOPT, SYSTEM_ERROR_ENOPROTOOPT },
561 #ifdef EPROTONOSUPPORT
562 { EPROTONOSUPPORT, SYSTEM_ERROR_EPROTONOSUPPORT },
564 #ifdef ESOCKTNOSUPPORT
565 { ESOCKTNOSUPPORT, SYSTEM_ERROR_ESOCKTNOSUPPORT },
568 { EOPNOTSUPP, SYSTEM_ERROR_EOPNOTSUPP },
571 { EPFNOSUPPORT, SYSTEM_ERROR_EPFNOSUPPORT },
574 { EAFNOSUPPORT, SYSTEM_ERROR_EAFNOSUPPORT },
577 { EADDRINUSE, SYSTEM_ERROR_EADDRINUSE },
580 { EADDRNOTAVAIL, SYSTEM_ERROR_EADDRNOTAVAIL },
583 { ENETDOWN, SYSTEM_ERROR_ENETDOWN },
586 { ENETUNREACH, SYSTEM_ERROR_ENETUNREACH },
589 { ENETRESET, SYSTEM_ERROR_ENETRESET },
592 { ECONNABORTED, SYSTEM_ERROR_ECONNABORTED },
595 { ECONNRESET, SYSTEM_ERROR_ECONNRESET },
598 { ENOBUFS, SYSTEM_ERROR_ENOBUFS },
601 { EISCONN, SYSTEM_ERROR_EISCONN },
604 { ENOTCONN, SYSTEM_ERROR_ENOTCONN },
607 { ESHUTDOWN, SYSTEM_ERROR_ESHUTDOWN },
610 { ETOOMANYREFS, SYSTEM_ERROR_ETOOMANYREFS },
613 { ETIMEDOUT, SYSTEM_ERROR_ETIMEDOUT },
616 { ECONNREFUSED, SYSTEM_ERROR_ECONNREFUSED },
619 { EHOSTDOWN, SYSTEM_ERROR_EHOSTDOWN },
622 { EHOSTUNREACH, SYSTEM_ERROR_EHOSTUNREACH },
625 { EALREADY, SYSTEM_ERROR_EALREADY },
628 { EINPROGRESS, SYSTEM_ERROR_EINPROGRESS },
631 { ESTALE, SYSTEM_ERROR_ESTALE },
634 { EUCLEAN, SYSTEM_ERROR_EUCLEAN },
637 { ENOTNAM, SYSTEM_ERROR_ENOTNAM },
640 { ENAVAIL, SYSTEM_ERROR_ENAVAIL },
643 { EISNAM, SYSTEM_ERROR_EISNAM },
646 { EREMOTEIO, SYSTEM_ERROR_EREMOTEIO },
649 { EDQUOT, SYSTEM_ERROR_EDQUOT },
652 { ENOMEDIUM, SYSTEM_ERROR_ENOMEDIUM },
655 { EMEDIUMTYPE, SYSTEM_ERROR_EMEDIUMTYPE },
658 { ECANCELED, SYSTEM_ERROR_ECANCELED },
661 { ENOKEY, SYSTEM_ERROR_ENOKEY },
664 { EKEYEXPIRED, SYSTEM_ERROR_EKEYEXPIRED },
667 { EKEYREVOKED, SYSTEM_ERROR_EKEYREVOKED },
670 { EKEYREJECTED, SYSTEM_ERROR_EKEYREJECTED },
673 { EOWNERDEAD, SYSTEM_ERROR_EOWNERDEAD },
675 #ifdef ENOTRECOVERABLE
676 { ENOTRECOVERABLE, SYSTEM_ERROR_ENOTRECOVERABLE },
679 { ERFKILL, SYSTEM_ERROR_ERFKILL },
682 { EHWPOISON, SYSTEM_ERROR_EHWPOISON },
686 ajla_error_t error_from_errno(int ec, int errn)
689 for (i = 0; i < n_array_elements(errno_to_system_error); i++) {
690 if (unlikely(errn == errno_to_system_error[i].errn))
691 return error_ajla_aux(ec, AJLA_ERROR_SYSTEM, errno_to_system_error[i].sys_error);
693 return error_ajla_aux(ec, AJLA_ERROR_ERRNO, errn);