errno.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pj/errno.h>
  20. #include <pj/log.h>
  21. #include <pj/string.h>
  22. #include <pj/compat/string.h>
  23. #include <pj/compat/stdarg.h>
  24. #include <pj/assert.h>
  25. /* Prototype for platform specific error message, which will be defined
  26. * in separate file.
  27. */
  28. PJ_BEGIN_DECL
  29. PJ_DECL(int) platform_strerror(pj_os_err_type code,
  30. char *buf, pj_size_t bufsize );
  31. PJ_END_DECL
  32. #ifndef PJLIB_MAX_ERR_MSG_HANDLER
  33. # define PJLIB_MAX_ERR_MSG_HANDLER 10
  34. #endif
  35. /* Error message handler. */
  36. static unsigned err_msg_hnd_cnt;
  37. static struct err_msg_hnd
  38. {
  39. pj_status_t begin;
  40. pj_status_t end;
  41. pj_str_t (*strerror)(pj_status_t, char*, pj_size_t);
  42. } err_msg_hnd[PJLIB_MAX_ERR_MSG_HANDLER];
  43. /* PJLIB's own error codes/messages */
  44. #if defined(PJ_HAS_ERROR_STRING) && PJ_HAS_ERROR_STRING!=0
  45. static const struct
  46. {
  47. int code;
  48. const char *msg;
  49. } err_str[] =
  50. {
  51. PJ_BUILD_ERR(PJ_EUNKNOWN, "Unknown Error" ),
  52. PJ_BUILD_ERR(PJ_EPENDING, "Pending operation" ),
  53. PJ_BUILD_ERR(PJ_ETOOMANYCONN, "Too many connecting sockets" ),
  54. PJ_BUILD_ERR(PJ_EINVAL, "Invalid value or argument" ),
  55. PJ_BUILD_ERR(PJ_ENAMETOOLONG, "Name too long" ),
  56. PJ_BUILD_ERR(PJ_ENOTFOUND, "Not found" ),
  57. PJ_BUILD_ERR(PJ_ENOMEM, "Not enough memory" ),
  58. PJ_BUILD_ERR(PJ_EBUG, "BUG DETECTED!" ),
  59. PJ_BUILD_ERR(PJ_ETIMEDOUT, "Operation timed out" ),
  60. PJ_BUILD_ERR(PJ_ETOOMANY, "Too many objects of the specified type"),
  61. PJ_BUILD_ERR(PJ_EBUSY, "Object is busy"),
  62. PJ_BUILD_ERR(PJ_ENOTSUP, "Option/operation is not supported"),
  63. PJ_BUILD_ERR(PJ_EINVALIDOP, "Invalid operation"),
  64. PJ_BUILD_ERR(PJ_ECANCELLED, "Operation cancelled"),
  65. PJ_BUILD_ERR(PJ_EEXISTS, "Object already exists" ),
  66. PJ_BUILD_ERR(PJ_EEOF, "End of file" ),
  67. PJ_BUILD_ERR(PJ_ETOOBIG, "Size is too big"),
  68. PJ_BUILD_ERR(PJ_ERESOLVE, "gethostbyname() has returned error"),
  69. PJ_BUILD_ERR(PJ_ETOOSMALL, "Size is too short"),
  70. PJ_BUILD_ERR(PJ_EIGNORED, "Ignored"),
  71. PJ_BUILD_ERR(PJ_EIPV6NOTSUP, "IPv6 is not supported"),
  72. PJ_BUILD_ERR(PJ_EAFNOTSUP, "Unsupported address family"),
  73. PJ_BUILD_ERR(PJ_EGONE, "Object no longer exists"),
  74. PJ_BUILD_ERR(PJ_ESOCKETSTOP, "Socket is in bad state")
  75. };
  76. #endif /* PJ_HAS_ERROR_STRING */
  77. /*
  78. * pjlib_error()
  79. *
  80. * Retrieve message string for PJLIB's own error code.
  81. */
  82. static int pjlib_error(pj_status_t code, char *buf, pj_size_t size)
  83. {
  84. int len;
  85. #if defined(PJ_HAS_ERROR_STRING) && PJ_HAS_ERROR_STRING!=0
  86. unsigned i;
  87. for (i=0; i<PJ_ARRAY_SIZE(err_str); ++i) {
  88. if (err_str[i].code == code) {
  89. pj_size_t len2 = pj_ansi_strlen(err_str[i].msg);
  90. if (len2 >= size) len2 = size-1;
  91. pj_memcpy(buf, err_str[i].msg, len2);
  92. buf[len2] = '\0';
  93. return (int)len2;
  94. }
  95. }
  96. #endif
  97. len = pj_ansi_snprintf( buf, size, "Unknown pjlib error %d", code);
  98. if (len < 1 || len >= (int)size)
  99. len = (int)(size - 1);
  100. return len;
  101. }
  102. #define IN_RANGE(val,start,end) ((val)>=(start) && (val)<(end))
  103. /* Register strerror handle. */
  104. PJ_DEF(pj_status_t) pj_register_strerror( pj_status_t start,
  105. pj_status_t space,
  106. pj_error_callback f)
  107. {
  108. unsigned i;
  109. /* Check arguments. */
  110. PJ_ASSERT_RETURN(start && space && f, PJ_EINVAL);
  111. /* Check if there aren't too many handlers registered. */
  112. PJ_ASSERT_RETURN(err_msg_hnd_cnt < PJ_ARRAY_SIZE(err_msg_hnd),
  113. PJ_ETOOMANY);
  114. /* Start error must be greater than PJ_ERRNO_START_USER */
  115. PJ_ASSERT_RETURN(start >= PJ_ERRNO_START_USER, PJ_EEXISTS);
  116. /* Check that no existing handler has covered the specified range. */
  117. for (i=0; i<err_msg_hnd_cnt; ++i) {
  118. if (IN_RANGE(start, err_msg_hnd[i].begin, err_msg_hnd[i].end) ||
  119. IN_RANGE(start+space-1, err_msg_hnd[i].begin, err_msg_hnd[i].end))
  120. {
  121. if (err_msg_hnd[i].begin == start &&
  122. err_msg_hnd[i].end == (start+space) &&
  123. err_msg_hnd[i].strerror == f)
  124. {
  125. /* The same range and handler has already been registered */
  126. return PJ_SUCCESS;
  127. }
  128. return PJ_EEXISTS;
  129. }
  130. }
  131. /* Register the handler. */
  132. err_msg_hnd[err_msg_hnd_cnt].begin = start;
  133. err_msg_hnd[err_msg_hnd_cnt].end = start + space;
  134. err_msg_hnd[err_msg_hnd_cnt].strerror = f;
  135. ++err_msg_hnd_cnt;
  136. return PJ_SUCCESS;
  137. }
  138. /* Internal PJLIB function called by pj_shutdown() to clear error handlers */
  139. void pj_errno_clear_handlers(void)
  140. {
  141. err_msg_hnd_cnt = 0;
  142. pj_bzero(err_msg_hnd, sizeof(err_msg_hnd));
  143. }
  144. /*
  145. * pj_strerror()
  146. */
  147. PJ_DEF(pj_str_t) pj_strerror( pj_status_t statcode,
  148. char *buf, pj_size_t bufsize )
  149. {
  150. int len = -1;
  151. pj_str_t errstr;
  152. pj_assert(buf && bufsize);
  153. if (statcode == PJ_SUCCESS) {
  154. len = pj_ansi_snprintf( buf, bufsize, "Success");
  155. } else if (statcode < PJ_ERRNO_START + PJ_ERRNO_SPACE_SIZE) {
  156. len = pj_ansi_snprintf( buf, bufsize, "Unknown error %d", statcode);
  157. } else if (statcode < PJ_ERRNO_START_STATUS + PJ_ERRNO_SPACE_SIZE) {
  158. len = pjlib_error(statcode, buf, bufsize);
  159. } else if (statcode < PJ_ERRNO_START_SYS + PJ_ERRNO_SPACE_SIZE) {
  160. len = platform_strerror(PJ_STATUS_TO_OS(statcode), buf, bufsize);
  161. } else {
  162. unsigned i;
  163. /* Find user handler to get the error message. */
  164. for (i=0; i<err_msg_hnd_cnt; ++i) {
  165. if (IN_RANGE(statcode, err_msg_hnd[i].begin, err_msg_hnd[i].end)) {
  166. return (*err_msg_hnd[i].strerror)(statcode, buf, bufsize);
  167. }
  168. }
  169. /* Handler not found! */
  170. len = pj_ansi_snprintf( buf, bufsize, "Unknown error %d", statcode);
  171. }
  172. if (len < 1 || len >= (int)bufsize) {
  173. len = (int)(bufsize - 1);
  174. buf[len] = '\0';
  175. }
  176. errstr.ptr = buf;
  177. errstr.slen = len;
  178. return errstr;
  179. }
  180. #if PJ_LOG_MAX_LEVEL >= 1
  181. static void invoke_log(const char *sender, int level, const char *format, ...)
  182. {
  183. va_list arg;
  184. va_start(arg, format);
  185. pj_log(sender, level, format, arg);
  186. va_end(arg);
  187. }
  188. static void pj_perror_imp(int log_level, const char *sender,
  189. pj_status_t status,
  190. const char *title_fmt, va_list marker)
  191. {
  192. char titlebuf[PJ_PERROR_TITLE_BUF_SIZE];
  193. char errmsg[PJ_ERR_MSG_SIZE];
  194. int len;
  195. /* Build the title */
  196. len = pj_ansi_vsnprintf(titlebuf, sizeof(titlebuf), title_fmt, marker);
  197. if (len < 0 || len >= (int)sizeof(titlebuf))
  198. pj_ansi_strxcpy(titlebuf, "Error", sizeof(titlebuf));
  199. /* Get the error */
  200. pj_strerror(status, errmsg, sizeof(errmsg));
  201. /* Send to log */
  202. invoke_log(sender, log_level, "%s: %s", titlebuf, errmsg);
  203. }
  204. PJ_DEF(void) pj_perror(int log_level, const char *sender, pj_status_t status,
  205. const char *title_fmt, ...)
  206. {
  207. va_list marker;
  208. va_start(marker, title_fmt);
  209. pj_perror_imp(log_level, sender, status, title_fmt, marker);
  210. va_end(marker);
  211. }
  212. PJ_DEF(void) pj_perror_1(const char *sender, pj_status_t status,
  213. const char *title_fmt, ...)
  214. {
  215. va_list marker;
  216. va_start(marker, title_fmt);
  217. pj_perror_imp(1, sender, status, title_fmt, marker);
  218. va_end(marker);
  219. }
  220. #else /* #if PJ_LOG_MAX_LEVEL >= 1 */
  221. PJ_DEF(void) pj_perror(int log_level, const char *sender, pj_status_t status,
  222. const char *title_fmt, ...)
  223. {
  224. }
  225. #endif /* #if PJ_LOG_MAX_LEVEL >= 1 */
  226. #if PJ_LOG_MAX_LEVEL >= 2
  227. PJ_DEF(void) pj_perror_2(const char *sender, pj_status_t status,
  228. const char *title_fmt, ...)
  229. {
  230. va_list marker;
  231. va_start(marker, title_fmt);
  232. pj_perror_imp(2, sender, status, title_fmt, marker);
  233. va_end(marker);
  234. }
  235. #endif
  236. #if PJ_LOG_MAX_LEVEL >= 3
  237. PJ_DEF(void) pj_perror_3(const char *sender, pj_status_t status,
  238. const char *title_fmt, ...)
  239. {
  240. va_list marker;
  241. va_start(marker, title_fmt);
  242. pj_perror_imp(3, sender, status, title_fmt, marker);
  243. va_end(marker);
  244. }
  245. #endif
  246. #if PJ_LOG_MAX_LEVEL >= 4
  247. PJ_DEF(void) pj_perror_4(const char *sender, pj_status_t status,
  248. const char *title_fmt, ...)
  249. {
  250. va_list marker;
  251. va_start(marker, title_fmt);
  252. pj_perror_imp(4, sender, status, title_fmt, marker);
  253. va_end(marker);
  254. }
  255. #endif
  256. #if PJ_LOG_MAX_LEVEL >= 5
  257. PJ_DEF(void) pj_perror_5(const char *sender, pj_status_t status,
  258. const char *title_fmt, ...)
  259. {
  260. va_list marker;
  261. va_start(marker, title_fmt);
  262. pj_perror_imp(5, sender, status, title_fmt, marker);
  263. va_end(marker);
  264. }
  265. #endif
  266. #if PJ_LOG_MAX_LEVEL >= 6
  267. PJ_DEF(void) pj_perror_6(const char *sender, pj_status_t status,
  268. const char *title_fmt, ...)
  269. {
  270. va_list marker;
  271. va_start(marker, title_fmt);
  272. pj_perror_imp(6, sender, status, title_fmt, marker);
  273. va_end(marker);
  274. }
  275. #endif