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