1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 #include <libmemcached/common.h>
40 #include "libmemcached/assert.hpp"
46 #define MAX_ERROR_LENGTH 2048
47 struct memcached_error_t
51 struct memcached_error_t
*next
;
52 memcached_return_t rc
;
55 char message
[MAX_ERROR_LENGTH
];
58 static void _set(memcached_instance_st
& server
, Memcached
& memc
)
60 if (server
.error_messages
and server
.error_messages
->query_id
!= server
.root
->query_id
)
62 memcached_error_free(server
);
65 if (memc
.error_messages
)
67 if (memc
.error_messages
->rc
== MEMCACHED_TIMEOUT
)
69 server
.io_wait_count
.timeouts
++;
72 memcached_error_t
*error
= libmemcached_xmalloc(&memc
, memcached_error_t
);
75 memcpy(error
, memc
.error_messages
, sizeof(memcached_error_t
));
76 error
->next
= server
.error_messages
;
77 server
.error_messages
= error
;
83 static int error_log_fd
= -1;
86 static void _set(Memcached
& memc
, memcached_string_t
*str
, memcached_return_t
&rc
, const char *at
, int local_errno
= 0)
88 if (memc
.error_messages
&& memc
.error_messages
->query_id
!= memc
.query_id
)
90 memcached_error_free(memc
);
93 if (memcached_fatal(rc
) or rc
== MEMCACHED_CLIENT_ERROR
)
95 // For memory allocation we use our error since it is a bit more specific
96 if (local_errno
== ENOMEM
and rc
== MEMCACHED_ERRNO
)
98 rc
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
101 if (rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
)
106 if (rc
== MEMCACHED_ERRNO
and not local_errno
)
112 if (rc
== MEMCACHED_ERRNO
and local_errno
== ENOTCONN
)
114 rc
= MEMCACHED_CONNECTION_FAILURE
;
117 if (rc
== MEMCACHED_ERRNO
and local_errno
== ECONNRESET
)
119 rc
= MEMCACHED_CONNECTION_FAILURE
;
122 if (local_errno
== EINVAL
)
124 rc
= MEMCACHED_INVALID_ARGUMENTS
;
127 if (local_errno
== ECONNREFUSED
)
129 rc
= MEMCACHED_CONNECTION_FAILURE
;
132 if (rc
== MEMCACHED_TIMEOUT
)
136 memcached_error_t
*error
= libmemcached_xmalloc(&memc
, memcached_error_t
);
137 if (error
== NULL
) // Bad business if this happens
139 assert_msg(error
, "libmemcached_xmalloc() failed to allocate a memcached_error_t");
144 error
->query_id
= memc
.query_id
;
146 error
->local_errno
= local_errno
;
148 // MEMCACHED_CLIENT_ERROR is a special case because it is an error coming from the server
149 if (rc
== MEMCACHED_CLIENT_ERROR
)
153 if (str
and str
->size
)
155 assert(error
->local_errno
== 0);
156 error
->local_errno
= 0;
158 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %.*s",
160 int(str
->size
), str
->c_str
);
163 else if (local_errno
)
165 const char *errmsg_ptr
;
166 char errmsg
[MAX_ERROR_LENGTH
];
170 #if defined(STRERROR_R_CHAR_P) && STRERROR_R_CHAR_P
171 errmsg_ptr
= strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
172 #elif defined(HAVE_STRERROR_R) && HAVE_STRERROR_R
173 strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
175 #elif defined(HAVE_STRERROR) && HAVE_STRERROR
176 snprintf(errmsg
, sizeof(errmsg
), "%s", strerror(local_errno
));
180 if (str
and str
->size
and local_errno
)
182 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %s(%s), %.*s -> %s",
184 memcached_strerror(&memc
, rc
),
186 memcached_string_printf(*str
), at
);
190 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %s(%s) -> %s",
192 memcached_strerror(&memc
, rc
),
197 else if (rc
== MEMCACHED_PARSE_ERROR
and str
and str
->size
)
199 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %.*s -> %s",
201 int(str
->size
), str
->c_str
, at
);
203 else if (str
and str
->size
)
205 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %s, %.*s -> %s",
207 memcached_strerror(&memc
, rc
),
208 int(str
->size
), str
->c_str
, at
);
212 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %s -> %s",
214 memcached_strerror(&memc
, rc
), at
);
217 error
->next
= memc
.error_messages
;
218 memc
.error_messages
= error
;
222 if (error_log_fd
== -1)
224 // unlink("/tmp/libmemcachd.log");
225 if ((error_log_fd
= open("/tmp/libmemcachd.log", O_CREAT
| O_WRONLY
| O_APPEND
, 0644)) < 0)
231 ::write(error_log_fd
, error
->message
, error
->size
);
232 ::write(error_log_fd
, "\n", 1);
236 memcached_return_t
memcached_set_error(Memcached
& memc
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
238 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
239 memcached_string_t tmp
= { str
, length
};
240 return memcached_set_error(memc
, rc
, at
, tmp
);
243 memcached_return_t
memcached_set_error(memcached_instance_st
& self
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
245 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
246 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a Instance");
248 memcached_string_t tmp
= { str
, length
};
249 return memcached_set_error(self
, rc
, at
, tmp
);
252 #ifndef __INTEL_COMPILER
253 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
256 memcached_return_t
memcached_set_error(Memcached
& memc
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
258 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
259 if (memcached_fatal(rc
))
261 _set(memc
, &str
, rc
, at
);
267 memcached_return_t
memcached_set_parser_error(Memcached
& memc
,
269 const char *format
, ...)
274 va_start(args
, format
);
275 int length
= vsnprintf(buffer
, sizeof(buffer
), format
, args
);
278 return memcached_set_error(memc
, MEMCACHED_PARSE_ERROR
, at
, buffer
, length
);
281 static inline size_t append_host_to_string(memcached_instance_st
& self
, char* buffer
, const size_t buffer_length
)
286 case MEMCACHED_CONNECTION_TCP
:
287 case MEMCACHED_CONNECTION_UDP
:
288 size
+= snprintf(buffer
, buffer_length
, " host: %s:%d",
289 self
.hostname(), int(self
.port()));
292 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
293 size
+= snprintf(buffer
, buffer_length
, " socket: %s",
301 memcached_return_t
memcached_set_error(memcached_instance_st
& self
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
303 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
304 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_instance_st");
305 if (memcached_fatal(rc
) == false and rc
!= MEMCACHED_CLIENT_ERROR
)
310 char hostname_port_message
[MAX_ERROR_LENGTH
];
311 char* hostname_port_message_ptr
= hostname_port_message
;
315 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ",
316 memcached_string_printf(str
));
317 hostname_port_message_ptr
+= size
;
320 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
322 memcached_string_t error_host
= { hostname_port_message
, size_t(size
) };
324 assert_msg(self
.root
, "Programmer error, root was not set on instance");
327 _set(*self
.root
, &error_host
, rc
, at
);
328 _set(self
, (*self
.root
));
329 assert(self
.error_messages
);
330 assert(self
.root
->error_messages
);
331 assert(self
.error_messages
->rc
== self
.root
->error_messages
->rc
);
337 memcached_return_t
memcached_set_error(memcached_instance_st
& self
, memcached_return_t rc
, const char *at
)
339 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_instance_st");
340 if (memcached_fatal(rc
) == false)
345 char hostname_port
[MEMCACHED_NI_MAXHOST
+MEMCACHED_NI_MAXSERV
+ sizeof("host : ")];
346 size_t size
= append_host_to_string(self
, hostname_port
, sizeof(hostname_port
));
348 memcached_string_t error_host
= { hostname_port
, size
};
352 _set(*self
.root
, &error_host
, rc
, at
);
353 _set(self
, *self
.root
);
359 memcached_return_t
memcached_set_error(Memcached
& self
, memcached_return_t rc
, const char *at
)
361 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
362 if (memcached_fatal(rc
) == false)
367 _set(self
, NULL
, rc
, at
);
372 memcached_return_t
memcached_set_errno(Memcached
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
374 memcached_string_t tmp
= { str
, length
};
375 return memcached_set_errno(self
, local_errno
, at
, tmp
);
378 memcached_return_t
memcached_set_errno(memcached_instance_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
380 memcached_string_t tmp
= { str
, length
};
381 return memcached_set_errno(self
, local_errno
, at
, tmp
);
384 memcached_return_t
memcached_set_errno(Memcached
& self
, int local_errno
, const char *at
)
386 if (local_errno
== 0)
388 return MEMCACHED_SUCCESS
;
391 memcached_return_t rc
= MEMCACHED_ERRNO
;
392 _set(self
, NULL
, rc
, at
, local_errno
);
397 memcached_return_t
memcached_set_errno(Memcached
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
399 if (local_errno
== 0)
401 return MEMCACHED_SUCCESS
;
404 memcached_return_t rc
= MEMCACHED_ERRNO
;
405 _set(memc
, &str
, rc
, at
, local_errno
);
410 memcached_return_t
memcached_set_errno(memcached_instance_st
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
412 if (local_errno
== 0)
414 return MEMCACHED_SUCCESS
;
417 char hostname_port_message
[MAX_ERROR_LENGTH
];
418 char* hostname_port_message_ptr
= hostname_port_message
;
422 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ", memcached_string_printf(str
));
424 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
426 memcached_string_t error_host
= { hostname_port_message
, size
};
428 memcached_return_t rc
= MEMCACHED_ERRNO
;
429 if (self
.root
== NULL
)
434 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
435 _set(self
, (*self
.root
));
438 if (self
.root
->error_messages
->rc
!= self
.error_messages
->rc
)
440 fprintf(stderr
, "%s:%d %s != %s\n", __FILE__
, __LINE__
,
441 memcached_strerror(NULL
, self
.root
->error_messages
->rc
),
442 memcached_strerror(NULL
, self
.error_messages
->rc
));
449 memcached_return_t
memcached_set_errno(memcached_instance_st
& self
, int local_errno
, const char *at
)
451 if (local_errno
== 0)
453 return MEMCACHED_SUCCESS
;
456 char hostname_port_message
[MAX_ERROR_LENGTH
];
457 size_t size
= append_host_to_string(self
, hostname_port_message
, sizeof(hostname_port_message
));
459 memcached_string_t error_host
= { hostname_port_message
, size
};
461 memcached_return_t rc
= MEMCACHED_ERRNO
;
462 if (self
.root
== NULL
)
467 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
468 _set(self
, (*self
.root
));
473 static void _error_print(const memcached_error_t
*error
)
480 if (error
->size
== 0)
482 fprintf(stderr
, "\t%s\n", memcached_strerror(NULL
, error
->rc
) );
486 fprintf(stderr
, "\t%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
489 _error_print(error
->next
);
492 void memcached_error_print(const Memcached
*shell
)
494 const Memcached
* self
= memcached2Memcached(shell
);
500 _error_print(self
->error_messages
);
502 for (uint32_t x
= 0; x
< memcached_server_count(self
); x
++)
504 memcached_instance_st
* instance
= memcached_instance_by_position(self
, x
);
506 _error_print(instance
->error_messages
);
510 static void _error_free(memcached_error_t
*error
)
514 _error_free(error
->next
);
516 libmemcached_free(error
->root
, error
);
520 void memcached_error_free(Memcached
& self
)
522 _error_free(self
.error_messages
);
523 self
.error_messages
= NULL
;
526 void memcached_error_free(memcached_instance_st
& self
)
528 _error_free(self
.error_messages
);
529 self
.error_messages
= NULL
;
532 void memcached_error_free(memcached_server_st
& self
)
534 _error_free(self
.error_messages
);
535 self
.error_messages
= NULL
;
538 const char *memcached_error(const memcached_st
*memc
)
540 return memcached_last_error_message(memc
);
543 const char *memcached_last_error_message(const memcached_st
*shell
)
545 const Memcached
* memc
= memcached2Memcached(shell
);
548 if (memc
->error_messages
)
550 if (memc
->error_messages
->size
and memc
->error_messages
->message
[0])
552 return memc
->error_messages
->message
;
555 return memcached_strerror(memc
, memc
->error_messages
->rc
);
558 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
561 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
564 bool memcached_has_current_error(Memcached
&memc
)
566 if (memc
.error_messages
567 and memc
.error_messages
->query_id
== memc
.query_id
568 and memcached_failed(memc
.error_messages
->rc
))
576 bool memcached_has_current_error(memcached_instance_st
& server
)
578 return memcached_has_current_error(*(server
.root
));
581 memcached_return_t
memcached_last_error(const memcached_st
*shell
)
583 const Memcached
* memc
= memcached2Memcached(shell
);
586 if (memc
->error_messages
)
588 return memc
->error_messages
->rc
;
591 return MEMCACHED_SUCCESS
;
594 return MEMCACHED_INVALID_ARGUMENTS
;
597 int memcached_last_error_errno(const memcached_st
*shell
)
599 const Memcached
* memc
= memcached2Memcached(shell
);
605 if (memc
->error_messages
== NULL
)
610 return memc
->error_messages
->local_errno
;
613 const char *memcached_server_error(const memcached_instance_st
* server
)
620 if (server
->error_messages
== NULL
)
622 return memcached_strerror(server
->root
, MEMCACHED_SUCCESS
);
625 if (server
->error_messages
->size
== 0)
627 return memcached_strerror(server
->root
, server
->error_messages
->rc
);
630 return server
->error_messages
->message
;
634 memcached_error_t
*memcached_error_copy(const memcached_instance_st
& server
)
636 if (server
.error_messages
== NULL
)
641 memcached_error_t
*error
= libmemcached_xmalloc(server
.root
, memcached_error_t
);
642 memcpy(error
, server
.error_messages
, sizeof(memcached_error_t
));
648 memcached_return_t
memcached_server_error_return(const memcached_instance_st
* ptr
)
652 return MEMCACHED_INVALID_ARGUMENTS
;
655 if (ptr
and ptr
->error_messages
)
657 return ptr
->error_messages
->rc
;
660 return MEMCACHED_SUCCESS
;
663 memcached_return_t
memcached_instance_error_return(memcached_instance_st
* instance
)
665 if (instance
== NULL
)
667 return MEMCACHED_INVALID_ARGUMENTS
;
670 if (instance
and instance
->error_messages
)
672 return instance
->error_messages
->rc
;
675 return MEMCACHED_SUCCESS
;