error.c 3.5 KB

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