error.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <[email protected]>
  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. };
  44. /**
  45. * Return error message for an error code
  46. * @return error message
  47. */
  48. const char *nl_geterror(int error)
  49. {
  50. error = abs(error);
  51. if (error > NLE_MAX)
  52. error = NLE_FAILURE;
  53. return errmsg[error];
  54. }
  55. /**
  56. * Print a libnl error message
  57. * @arg s error message prefix
  58. *
  59. * Prints the error message of the call that failed last.
  60. *
  61. * If s is not NULL and *s is not a null byte the argument
  62. * string is printed, followed by a colon and a blank. Then
  63. * the error message and a new-line.
  64. */
  65. void nl_perror(int error, const char *s)
  66. {
  67. if (s && *s)
  68. fprintf(stderr, "%s: %s\n", s, nl_geterror(error));
  69. else
  70. fprintf(stderr, "%s\n", nl_geterror(error));
  71. }
  72. int nl_syserr2nlerr(int error)
  73. {
  74. error = abs(error);
  75. switch (error) {
  76. case EBADF: return NLE_BAD_SOCK;
  77. case EADDRINUSE: return NLE_EXIST;
  78. case EEXIST: return NLE_EXIST;
  79. case EADDRNOTAVAIL: return NLE_NOADDR;
  80. case ENOENT: return NLE_OBJ_NOTFOUND;
  81. case EINTR: return NLE_INTR;
  82. case EAGAIN: return NLE_AGAIN;
  83. case ENOTSOCK: return NLE_BAD_SOCK;
  84. case ENOPROTOOPT: return NLE_INVAL;
  85. case EFAULT: return NLE_INVAL;
  86. case EACCES: return NLE_NOACCESS;
  87. case EINVAL: return NLE_INVAL;
  88. case ENOBUFS: return NLE_NOMEM;
  89. case ENOMEM: return NLE_NOMEM;
  90. case EAFNOSUPPORT: return NLE_AF_NOSUPPORT;
  91. case EPROTONOSUPPORT: return NLE_PROTO_MISMATCH;
  92. case EOPNOTSUPP: return NLE_OPNOTSUPP;
  93. case EPERM: return NLE_PERM;
  94. case EBUSY: return NLE_BUSY;
  95. default: return NLE_FAILURE;
  96. }
  97. }
  98. /** @} */