error.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. #include "gear_config.h"
  39. #include <libgearman/common.h>
  40. #include "libgearman/assert.hpp"
  41. #include "libgearman/log.hpp"
  42. #include <cerrno>
  43. #include <cstdarg>
  44. #include <cstdio>
  45. #include <cstring>
  46. #include <sys/types.h>
  47. #include <unistd.h>
  48. static void correct_from_errno(gearman_universal_st& universal)
  49. {
  50. if (universal.error_code() == GEARMAN_ERRNO)
  51. {
  52. switch (universal.last_errno())
  53. {
  54. case EFAULT:
  55. case ENOMEM:
  56. universal.error_code(GEARMAN_MEMORY_ALLOCATION_FAILURE);
  57. break;
  58. case EINVAL:
  59. universal.error_code(GEARMAN_INVALID_ARGUMENT);
  60. break;
  61. case ECONNRESET:
  62. case EHOSTDOWN:
  63. case EPIPE:
  64. universal.error_code(GEARMAN_LOST_CONNECTION);
  65. break;
  66. case ECONNREFUSED:
  67. case ENETUNREACH:
  68. case ETIMEDOUT:
  69. universal.error_code(GEARMAN_COULD_NOT_CONNECT);
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. else
  76. {
  77. universal.last_errno(0);
  78. }
  79. }
  80. #ifdef __clang__
  81. # pragma clang diagnostic push
  82. #else
  83. # pragma GCC diagnostic push
  84. #endif
  85. #pragma GCC diagnostic ignored "-Wswitch-enum"
  86. static bool no_error_message(gearman_return_t rc)
  87. {
  88. switch (rc)
  89. {
  90. case GEARMAN_SUCCESS:
  91. case GEARMAN_IO_WAIT:
  92. case GEARMAN_INVALID_SERVER_OPTION:
  93. case GEARMAN_WORK_EXCEPTION:
  94. case GEARMAN_WORK_FAIL:
  95. case GEARMAN_SHUTDOWN_GRACEFUL:
  96. case GEARMAN_SHUTDOWN:
  97. return true;
  98. default:
  99. break;
  100. }
  101. return false;
  102. }
  103. #pragma GCC diagnostic pop
  104. #pragma GCC diagnostic push
  105. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  106. gearman_return_t gearman_universal_set_error(gearman_universal_st& universal,
  107. gearman_return_t rc,
  108. const char *function,
  109. const char *position,
  110. const char *format, ...)
  111. {
  112. if (no_error_message(rc))
  113. {
  114. return universal.error_code(rc);
  115. }
  116. va_list args;
  117. universal.error_code(rc);
  118. correct_from_errno(universal);
  119. char last_error[GEARMAN_MAX_ERROR_SIZE];
  120. va_start(args, format);
  121. int length= vsnprintf(last_error, GEARMAN_MAX_ERROR_SIZE, format, args);
  122. va_end(args);
  123. if (length > int(GEARMAN_MAX_ERROR_SIZE) or length < 0)
  124. {
  125. assert(length > int(GEARMAN_MAX_ERROR_SIZE));
  126. assert(length < 0);
  127. last_error[GEARMAN_MAX_ERROR_SIZE -1]= 0;
  128. }
  129. if (rc == GEARMAN_GETADDRINFO)
  130. {
  131. universal._error.error("%s pid(%u)", last_error, getpid());
  132. }
  133. else
  134. {
  135. universal._error.error("%s(%s) %s -> %s pid(%u)",
  136. function, gearman_strerror(universal._error.error_code()), last_error, position, getpid());
  137. }
  138. gearman_log_error(universal,
  139. universal._error.error_code() == GEARMAN_MEMORY_ALLOCATION_FAILURE ? GEARMAN_VERBOSE_FATAL : GEARMAN_VERBOSE_ERROR);
  140. return universal._error.error_code();
  141. }
  142. #pragma GCC diagnostic pop
  143. #pragma GCC diagnostic push
  144. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  145. gearman_return_t gearman_universal_set_gerror(gearman_universal_st& universal,
  146. gearman_return_t rc,
  147. const char *func,
  148. const char *position)
  149. {
  150. if (no_error_message(rc))
  151. {
  152. return universal.error_code(rc);
  153. }
  154. universal.error_code(rc);
  155. correct_from_errno(universal);
  156. universal._error.error("%s(%s) -> %s pid(%u)", func, gearman_strerror(rc), position, getpid());
  157. gearman_log_error(universal,
  158. universal.error_code() == GEARMAN_MEMORY_ALLOCATION_FAILURE ? GEARMAN_VERBOSE_FATAL : GEARMAN_VERBOSE_ERROR);
  159. return rc;
  160. }
  161. #pragma GCC diagnostic pop
  162. #pragma GCC diagnostic push
  163. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  164. gearman_return_t gearman_universal_set_perror(gearman_universal_st &universal,
  165. const gearman_return_t rc,
  166. const int _system_errno,
  167. const char *function, const char *position,
  168. const char *format, ...)
  169. {
  170. if (_system_errno)
  171. {
  172. switch (_system_errno)
  173. {
  174. case ENOMEM:
  175. universal._error.error_code(GEARMAN_MEMORY_ALLOCATION_FAILURE);
  176. break;
  177. default:
  178. universal._error.error_code(rc);
  179. break;
  180. }
  181. universal._error.system_error(_system_errno);
  182. correct_from_errno(universal);
  183. const char *errmsg_ptr;
  184. char errmsg[GEARMAN_MAX_ERROR_SIZE];
  185. errmsg[0]= 0;
  186. #ifdef STRERROR_R_CHAR_P
  187. errmsg_ptr= strerror_r(universal._error.system_error(), errmsg, sizeof(errmsg));
  188. #else
  189. strerror_r(universal._error.system_error(), errmsg, sizeof(errmsg));
  190. errmsg_ptr= errmsg;
  191. #endif
  192. int length;
  193. if (format)
  194. {
  195. char last_error[GEARMAN_MAX_ERROR_SIZE];
  196. va_list args;
  197. va_start(args, format);
  198. errno= 0;
  199. length= vsnprintf(last_error, GEARMAN_MAX_ERROR_SIZE, format, args);
  200. va_end(args);
  201. if (length > int(GEARMAN_MAX_ERROR_SIZE) or length < 0)
  202. {
  203. assert(length > int(GEARMAN_MAX_ERROR_SIZE));
  204. assert(length < 0);
  205. last_error[GEARMAN_MAX_ERROR_SIZE -1]= 0;
  206. }
  207. universal._error.error("%s(%s) %s -> %s pid(%u)", function, errmsg_ptr, last_error, position, getpid());
  208. }
  209. else
  210. {
  211. universal._error.error("%s(%s) -> %s pid(%d)", function, errmsg_ptr, position, getpid());
  212. }
  213. gearman_log_error(universal,
  214. universal._error.error_code() == GEARMAN_MEMORY_ALLOCATION_FAILURE ? GEARMAN_VERBOSE_FATAL : GEARMAN_VERBOSE_ERROR);
  215. return universal._error.error_code();
  216. }
  217. return GEARMAN_SUCCESS;
  218. }
  219. #pragma GCC diagnostic pop
  220. #pragma GCC diagnostic push
  221. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  222. const char* error_st::error(const char * __restrict format__, ...)
  223. {
  224. if (format__)
  225. {
  226. va_list args;
  227. va_start(args, format__);
  228. vsnprintf(_last_error, GEARMAN_MAX_ERROR_SIZE, format__, args);
  229. va_end(args);
  230. _last_error[GEARMAN_MAX_ERROR_SIZE -1]= 0;
  231. return _last_error;
  232. }
  233. _last_error[0]= 0;
  234. return NULL;
  235. }
  236. #pragma GCC diagnostic pop