libnl 3.5.0

error.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * lib/error.c Error Handling
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation version 2.1
8 * of the License.
9 *
10 * Copyright (c) 2008 Thomas Graf <tgraf@suug.ch>
11 */
12
13 #include <netlink-private/netlink.h>
14 #include <netlink/netlink.h>
15
16 static const char *errmsg[NLE_MAX+1] = {
17 [NLE_SUCCESS] = "Success",
18 [NLE_FAILURE] = "Unspecific failure",
19 [NLE_INTR] = "Interrupted system call",
20 [NLE_BAD_SOCK] = "Bad socket",
21 [NLE_AGAIN] = "Try again",
22 [NLE_NOMEM] = "Out of memory",
23 [NLE_EXIST] = "Object exists",
24 [NLE_INVAL] = "Invalid input data or parameter",
25 [NLE_RANGE] = "Input data out of range",
26 [NLE_MSGSIZE] = "Message size not sufficient",
27 [NLE_OPNOTSUPP] = "Operation not supported",
28 [NLE_AF_NOSUPPORT] = "Address family not supported",
29 [NLE_OBJ_NOTFOUND] = "Object not found",
30 [NLE_NOATTR] = "Attribute not available",
31 [NLE_MISSING_ATTR] = "Missing attribute",
32 [NLE_AF_MISMATCH] = "Address family mismatch",
33 [NLE_SEQ_MISMATCH] = "Message sequence number mismatch",
34 [NLE_MSG_OVERFLOW] = "Kernel reported message overflow",
35 [NLE_MSG_TRUNC] = "Kernel reported truncated message",
36 [NLE_NOADDR] = "Invalid address for specified address family",
37 [NLE_SRCRT_NOSUPPORT] = "Source based routing not supported",
38 [NLE_MSG_TOOSHORT] = "Netlink message is too short",
39 [NLE_MSGTYPE_NOSUPPORT] = "Netlink message type is not supported",
40 [NLE_OBJ_MISMATCH] = "Object type does not match cache",
41 [NLE_NOCACHE] = "Unknown or invalid cache type",
42 [NLE_BUSY] = "Object busy",
43 [NLE_PROTO_MISMATCH] = "Protocol mismatch",
44 [NLE_NOACCESS] = "No Access",
45 [NLE_PERM] = "Operation not permitted",
46 [NLE_PKTLOC_FILE] = "Unable to open packet location file",
47 [NLE_PARSE_ERR] = "Unable to parse object",
48 [NLE_NODEV] = "No such device",
49 [NLE_IMMUTABLE] = "Immutable attribute",
50 [NLE_DUMP_INTR] = "Dump inconsistency detected, interrupted",
51 [NLE_ATTRSIZE] = "Attribute max length exceeded",
52 };
53
54 /**
55 * Return error message for an error code
56 * @return error message
57 */
58 const char *nl_geterror(int error)
59 {
60 error = abs(error);
61
62 if (error > NLE_MAX)
63 error = NLE_FAILURE;
64
65 return errmsg[error];
66 }
67
68 /**
69 * Print a libnl error message
70 * @arg s error message prefix
71 *
72 * Prints the error message of the call that failed last.
73 *
74 * If s is not NULL and *s is not a null byte the argument
75 * string is printed, followed by a colon and a blank. Then
76 * the error message and a new-line.
77 */
78 void nl_perror(int error, const char *s)
79 {
80 if (s && *s)
81 fprintf(stderr, "%s: %s\n", s, nl_geterror(error));
82 else
83 fprintf(stderr, "%s\n", nl_geterror(error));
84 }
85
86 int nl_syserr2nlerr(int error)
87 {
88 error = abs(error);
89
90 switch (error) {
91 case EBADF: return NLE_BAD_SOCK;
92 case EADDRINUSE: return NLE_EXIST;
93 case EEXIST: return NLE_EXIST;
94 case EADDRNOTAVAIL: return NLE_NOADDR;
95 case ESRCH: /* fall through */
96 case ENOENT: return NLE_OBJ_NOTFOUND;
97 case EINTR: return NLE_INTR;
98 case EAGAIN: return NLE_AGAIN;
99 case ENOTSOCK: return NLE_BAD_SOCK;
100 case ENOPROTOOPT: return NLE_INVAL;
101 case EFAULT: return NLE_INVAL;
102 case EACCES: return NLE_NOACCESS;
103 case EINVAL: return NLE_INVAL;
104 case ENOBUFS: return NLE_NOMEM;
105 case ENOMEM: return NLE_NOMEM;
106 case EAFNOSUPPORT: return NLE_AF_NOSUPPORT;
107 case EPROTONOSUPPORT: return NLE_PROTO_MISMATCH;
108 case EOPNOTSUPP: return NLE_OPNOTSUPP;
109 case EPERM: return NLE_PERM;
110 case EBUSY: return NLE_BUSY;
111 case ERANGE: return NLE_RANGE;
112 case ENODEV: return NLE_NODEV;
113 default: return NLE_FAILURE;
114 }
115 }
116
117 /** @} */
118