PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/libmemcached/error.cc

https://github.com/Metaswitch/libmemcached-upstream
C++ | 623 lines | 476 code | 109 blank | 38 comment | 110 complexity | 638a9788db39a25b882623dd871d8f37 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * LibMemcached
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include <libmemcached/common.h>
  38. #include <cstdarg>
  39. #define MAX_ERROR_LENGTH 2048
  40. struct memcached_error_t
  41. {
  42. memcached_st *root;
  43. uint64_t query_id;
  44. struct memcached_error_t *next;
  45. memcached_return_t rc;
  46. int local_errno;
  47. size_t size;
  48. char message[MAX_ERROR_LENGTH];
  49. };
  50. static void _set(org::libmemcached::Instance& server, memcached_st& memc)
  51. {
  52. if (server.error_messages and server.error_messages->query_id != server.root->query_id)
  53. {
  54. memcached_error_free(server);
  55. }
  56. if (memc.error_messages == NULL)
  57. {
  58. return;
  59. }
  60. memcached_error_t *error= libmemcached_xmalloc(&memc, memcached_error_t);
  61. if (error == NULL) // Bad business if this happens
  62. {
  63. return;
  64. }
  65. memcpy(error, memc.error_messages, sizeof(memcached_error_t));
  66. error->next= server.error_messages;
  67. server.error_messages= error;
  68. }
  69. #if 0
  70. static int error_log_fd= -1;
  71. #endif
  72. static void _set(memcached_st& memc, memcached_string_t *str, memcached_return_t &rc, const char *at, int local_errno= 0)
  73. {
  74. if (memc.error_messages && memc.error_messages->query_id != memc.query_id)
  75. {
  76. memcached_error_free(memc);
  77. }
  78. // For memory allocation we use our error since it is a bit more specific
  79. if (local_errno == ENOMEM and rc == MEMCACHED_ERRNO)
  80. {
  81. rc= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
  82. }
  83. if (rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE)
  84. {
  85. local_errno= ENOMEM;
  86. }
  87. if (rc == MEMCACHED_ERRNO and not local_errno)
  88. {
  89. local_errno= errno;
  90. rc= MEMCACHED_ERRNO;
  91. }
  92. if (rc == MEMCACHED_ERRNO and local_errno == ENOTCONN)
  93. {
  94. rc= MEMCACHED_CONNECTION_FAILURE;
  95. }
  96. if (local_errno == EINVAL)
  97. {
  98. rc= MEMCACHED_INVALID_ARGUMENTS;
  99. }
  100. if (local_errno == ECONNREFUSED)
  101. {
  102. rc= MEMCACHED_CONNECTION_FAILURE;
  103. }
  104. memcached_error_t *error= libmemcached_xmalloc(&memc, memcached_error_t);
  105. if (error == NULL) // Bad business if this happens
  106. {
  107. return;
  108. }
  109. error->root= &memc;
  110. error->query_id= memc.query_id;
  111. error->rc= rc;
  112. error->local_errno= local_errno;
  113. const char *errmsg_ptr;
  114. char errmsg[MAX_ERROR_LENGTH];
  115. errmsg[0]= 0;
  116. errmsg_ptr= errmsg;
  117. if (local_errno)
  118. {
  119. #ifdef STRERROR_R_CHAR_P
  120. errmsg_ptr= strerror_r(local_errno, errmsg, sizeof(errmsg));
  121. #else
  122. strerror_r(local_errno, errmsg, sizeof(errmsg));
  123. errmsg_ptr= errmsg;
  124. #endif
  125. }
  126. if (str and str->size and local_errno)
  127. {
  128. error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%lu) %s(%s), %.*s -> %s",
  129. long(error->root),
  130. memcached_strerror(&memc, rc),
  131. errmsg_ptr,
  132. memcached_string_printf(*str), at);
  133. }
  134. else if (local_errno)
  135. {
  136. error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%lu) %s(%s) -> %s",
  137. long(error->root),
  138. memcached_strerror(&memc, rc),
  139. errmsg_ptr,
  140. at);
  141. }
  142. else if (rc == MEMCACHED_PARSE_ERROR and str and str->size)
  143. {
  144. error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%lu) %.*s -> %s",
  145. long(error->root),
  146. int(str->size), str->c_str, at);
  147. }
  148. else if (str and str->size)
  149. {
  150. error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%lu) %s, %.*s -> %s",
  151. long(error->root),
  152. memcached_strerror(&memc, rc),
  153. int(str->size), str->c_str, at);
  154. }
  155. else
  156. {
  157. error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%lu) %s -> %s",
  158. long(error->root),
  159. memcached_strerror(&memc, rc), at);
  160. }
  161. error->next= memc.error_messages;
  162. memc.error_messages= error;
  163. #if 0
  164. if (error_log_fd == -1)
  165. {
  166. // unlink("/tmp/libmemcachd.log");
  167. if ((error_log_fd= open("/tmp/libmemcachd.log", O_CREAT | O_WRONLY | O_APPEND, 0644)) < 0)
  168. {
  169. perror("open");
  170. error_log_fd= -1;
  171. }
  172. }
  173. ::write(error_log_fd, error->message, error->size);
  174. ::write(error_log_fd, "\n", 1);
  175. #endif
  176. }
  177. memcached_return_t memcached_set_error(memcached_st& memc, memcached_return_t rc, const char *at, const char *str, size_t length)
  178. {
  179. assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
  180. memcached_string_t tmp= { str, length };
  181. return memcached_set_error(memc, rc, at, tmp);
  182. }
  183. memcached_return_t memcached_set_error(org::libmemcached::Instance& self, memcached_return_t rc, const char *at, const char *str, size_t length)
  184. {
  185. assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
  186. assert_msg(rc != MEMCACHED_SOME_ERRORS, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a Instance");
  187. memcached_string_t tmp= { str, length };
  188. return memcached_set_error(self, rc, at, tmp);
  189. }
  190. memcached_return_t memcached_set_error(memcached_st& memc, memcached_return_t rc, const char *at, memcached_string_t& str)
  191. {
  192. assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
  193. if (memcached_fatal(rc) == false)
  194. {
  195. return rc;
  196. }
  197. _set(memc, &str, rc, at);
  198. return rc;
  199. }
  200. memcached_return_t memcached_set_parser_error(memcached_st& memc,
  201. const char *at,
  202. const char *format, ...)
  203. {
  204. va_list args;
  205. char buffer[BUFSIZ];
  206. va_start(args, format);
  207. int length= vsnprintf(buffer, sizeof(buffer), format, args);
  208. va_end(args);
  209. return memcached_set_error(memc, MEMCACHED_PARSE_ERROR, at, buffer, length);
  210. }
  211. static inline size_t append_host_to_string(org::libmemcached::Instance& self, char* buffer, const size_t buffer_length)
  212. {
  213. size_t size= 0;
  214. switch (self.type)
  215. {
  216. case MEMCACHED_CONNECTION_TCP:
  217. case MEMCACHED_CONNECTION_UDP:
  218. size+= snprintf(buffer, buffer_length, " host: %s:%d",
  219. self.hostname, int(self.port()));
  220. break;
  221. case MEMCACHED_CONNECTION_UNIX_SOCKET:
  222. size+= snprintf(buffer, buffer_length, " socket: %s",
  223. self.hostname);
  224. break;
  225. }
  226. return size;
  227. }
  228. memcached_return_t memcached_set_error(org::libmemcached::Instance& self, memcached_return_t rc, const char *at, memcached_string_t& str)
  229. {
  230. assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
  231. assert_msg(rc != MEMCACHED_SOME_ERRORS, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a org::libmemcached::Instance");
  232. if (memcached_fatal(rc) == false)
  233. {
  234. return rc;
  235. }
  236. char hostname_port_message[MAX_ERROR_LENGTH];
  237. char* hostname_port_message_ptr= hostname_port_message;
  238. int size= 0;
  239. if (str.size)
  240. {
  241. size= snprintf(hostname_port_message_ptr, sizeof(hostname_port_message), "%.*s, ",
  242. memcached_string_printf(str));
  243. hostname_port_message_ptr+= size;
  244. }
  245. size+= append_host_to_string(self, hostname_port_message_ptr, sizeof(hostname_port_message) -size);
  246. memcached_string_t error_host= { hostname_port_message, size_t(size) };
  247. assert(self.root);
  248. if (self.root == NULL)
  249. {
  250. return rc;
  251. }
  252. _set(*self.root, &error_host, rc, at);
  253. _set(self, (*self.root));
  254. assert(self.root->error_messages);
  255. assert(self.error_messages);
  256. return rc;
  257. }
  258. memcached_return_t memcached_set_error(org::libmemcached::Instance& self, memcached_return_t rc, const char *at)
  259. {
  260. assert_msg(rc != MEMCACHED_SOME_ERRORS, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a org::libmemcached::Instance");
  261. if (memcached_fatal(rc) == false)
  262. {
  263. return rc;
  264. }
  265. char hostname_port[NI_MAXHOST +NI_MAXSERV + sizeof("host : ")];
  266. size_t size= append_host_to_string(self, hostname_port, sizeof(hostname_port));
  267. memcached_string_t error_host= { hostname_port, size};
  268. if (self.root == NULL)
  269. {
  270. return rc;
  271. }
  272. _set(*self.root, &error_host, rc, at);
  273. _set(self, *self.root);
  274. return rc;
  275. }
  276. memcached_return_t memcached_set_error(memcached_st& self, memcached_return_t rc, const char *at)
  277. {
  278. assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
  279. if (memcached_fatal(rc) == false)
  280. {
  281. return rc;
  282. }
  283. _set(self, NULL, rc, at);
  284. return rc;
  285. }
  286. memcached_return_t memcached_set_errno(memcached_st& self, int local_errno, const char *at, const char *str, size_t length)
  287. {
  288. memcached_string_t tmp= { str, length };
  289. return memcached_set_errno(self, local_errno, at, tmp);
  290. }
  291. memcached_return_t memcached_set_errno(org::libmemcached::Instance& self, int local_errno, const char *at, const char *str, size_t length)
  292. {
  293. memcached_string_t tmp= { str, length };
  294. return memcached_set_errno(self, local_errno, at, tmp);
  295. }
  296. memcached_return_t memcached_set_errno(memcached_st& self, int local_errno, const char *at)
  297. {
  298. if (local_errno == 0)
  299. {
  300. return MEMCACHED_SUCCESS;
  301. }
  302. memcached_return_t rc= MEMCACHED_ERRNO;
  303. _set(self, NULL, rc, at, local_errno);
  304. return rc;
  305. }
  306. memcached_return_t memcached_set_errno(memcached_st& memc, int local_errno, const char *at, memcached_string_t& str)
  307. {
  308. if (local_errno == 0)
  309. {
  310. return MEMCACHED_SUCCESS;
  311. }
  312. memcached_return_t rc= MEMCACHED_ERRNO;
  313. _set(memc, &str, rc, at, local_errno);
  314. return rc;
  315. }
  316. memcached_return_t memcached_set_errno(org::libmemcached::Instance& self, int local_errno, const char *at, memcached_string_t& str)
  317. {
  318. if (local_errno == 0)
  319. {
  320. return MEMCACHED_SUCCESS;
  321. }
  322. char hostname_port_message[MAX_ERROR_LENGTH];
  323. char* hostname_port_message_ptr= hostname_port_message;
  324. size_t size= 0;
  325. if (str.size)
  326. {
  327. size= snprintf(hostname_port_message_ptr, sizeof(hostname_port_message), "%.*s, ", memcached_string_printf(str));
  328. }
  329. size+= append_host_to_string(self, hostname_port_message_ptr, sizeof(hostname_port_message) -size);
  330. memcached_string_t error_host= { hostname_port_message, size };
  331. memcached_return_t rc= MEMCACHED_ERRNO;
  332. if (self.root == NULL)
  333. {
  334. return rc;
  335. }
  336. _set(*self.root, &error_host, rc, at, local_errno);
  337. _set(self, (*self.root));
  338. return rc;
  339. }
  340. memcached_return_t memcached_set_errno(org::libmemcached::Instance& self, int local_errno, const char *at)
  341. {
  342. if (local_errno == 0)
  343. {
  344. return MEMCACHED_SUCCESS;
  345. }
  346. char hostname_port_message[MAX_ERROR_LENGTH];
  347. size_t size= append_host_to_string(self, hostname_port_message, sizeof(hostname_port_message));
  348. memcached_string_t error_host= { hostname_port_message, size };
  349. memcached_return_t rc= MEMCACHED_ERRNO;
  350. if (self.root == NULL)
  351. {
  352. return rc;
  353. }
  354. _set(*self.root, &error_host, rc, at, local_errno);
  355. _set(self, (*self.root));
  356. return rc;
  357. }
  358. static void _error_print(const memcached_error_t *error)
  359. {
  360. if (error == NULL)
  361. {
  362. return;
  363. }
  364. if (error->size == 0)
  365. {
  366. fprintf(stderr, "\t%s\n", memcached_strerror(NULL, error->rc) );
  367. }
  368. else
  369. {
  370. fprintf(stderr, "\t%s %s\n", memcached_strerror(NULL, error->rc), error->message);
  371. }
  372. _error_print(error->next);
  373. }
  374. void memcached_error_print(const memcached_st *self)
  375. {
  376. if (self == NULL)
  377. {
  378. return;
  379. }
  380. _error_print(self->error_messages);
  381. for (uint32_t x= 0; x < memcached_server_count(self); x++)
  382. {
  383. org::libmemcached::Instance* instance= memcached_instance_by_position(self, x);
  384. _error_print(instance->error_messages);
  385. }
  386. }
  387. static void _error_free(memcached_error_t *error)
  388. {
  389. if (error)
  390. {
  391. _error_free(error->next);
  392. libmemcached_free(error->root, error);
  393. }
  394. }
  395. void memcached_error_free(memcached_st& self)
  396. {
  397. _error_free(self.error_messages);
  398. self.error_messages= NULL;
  399. }
  400. void memcached_error_free(org::libmemcached::Instance& self)
  401. {
  402. _error_free(self.error_messages);
  403. self.error_messages= NULL;
  404. }
  405. void memcached_error_free(memcached_server_st& self)
  406. {
  407. _error_free(self.error_messages);
  408. self.error_messages= NULL;
  409. }
  410. const char *memcached_last_error_message(const memcached_st *memc)
  411. {
  412. if (memc == NULL)
  413. {
  414. return memcached_strerror(memc, MEMCACHED_INVALID_ARGUMENTS);
  415. }
  416. if (memc->error_messages == NULL)
  417. {
  418. return memcached_strerror(memc, MEMCACHED_SUCCESS);
  419. }
  420. if (memc->error_messages->size == 0)
  421. {
  422. return memcached_strerror(memc, memc->error_messages->rc);
  423. }
  424. return memc->error_messages->message;
  425. }
  426. bool memcached_has_current_error(memcached_st &memc)
  427. {
  428. if (memc.error_messages
  429. and memc.error_messages->query_id == memc.query_id
  430. and memcached_failed(memc.error_messages->rc))
  431. {
  432. return true;
  433. }
  434. return false;
  435. }
  436. bool memcached_has_current_error(org::libmemcached::Instance& server)
  437. {
  438. return memcached_has_current_error(*(server.root));
  439. }
  440. memcached_return_t memcached_last_error(const memcached_st *memc)
  441. {
  442. if (memc == NULL)
  443. {
  444. return MEMCACHED_INVALID_ARGUMENTS;
  445. }
  446. if (memc->error_messages == NULL)
  447. {
  448. return MEMCACHED_SUCCESS;
  449. }
  450. return memc->error_messages->rc;
  451. }
  452. int memcached_last_error_errno(const memcached_st *memc)
  453. {
  454. if (memc == NULL)
  455. {
  456. return 0;
  457. }
  458. if (memc->error_messages == NULL)
  459. {
  460. return 0;
  461. }
  462. return memc->error_messages->local_errno;
  463. }
  464. const char *memcached_server_error(const memcached_server_instance_st server)
  465. {
  466. if (server == NULL)
  467. {
  468. return NULL;
  469. }
  470. if (server->error_messages == NULL)
  471. {
  472. return memcached_strerror(server->root, MEMCACHED_SUCCESS);
  473. }
  474. if (server->error_messages->size == 0)
  475. {
  476. return memcached_strerror(server->root, server->error_messages->rc);
  477. }
  478. return server->error_messages->message;
  479. }
  480. memcached_error_t *memcached_error_copy(const org::libmemcached::Instance& server)
  481. {
  482. if (server.error_messages == NULL)
  483. {
  484. return NULL;
  485. }
  486. memcached_error_t *error= libmemcached_xmalloc(server.root, memcached_error_t);
  487. memcpy(error, server.error_messages, sizeof(memcached_error_t));
  488. error->next= NULL;
  489. return error;
  490. }
  491. memcached_return_t memcached_server_error_return(memcached_server_instance_st ptr)
  492. {
  493. if (ptr == NULL)
  494. {
  495. return MEMCACHED_INVALID_ARGUMENTS;
  496. }
  497. if (ptr and ptr->error_messages)
  498. {
  499. return ptr->error_messages->rc;
  500. }
  501. return MEMCACHED_SUCCESS;
  502. }
  503. memcached_return_t memcached_instance_error_return(org::libmemcached::Instance* instance)
  504. {
  505. if (instance == NULL)
  506. {
  507. return MEMCACHED_INVALID_ARGUMENTS;
  508. }
  509. if (instance and instance->error_messages)
  510. {
  511. return instance->error_messages->rc;
  512. }
  513. return MEMCACHED_SUCCESS;
  514. }