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>
43 #define MAX_ERROR_LENGTH 2048
44 struct memcached_error_t
48 struct memcached_error_t
*next
;
49 memcached_return_t rc
;
52 char message
[MAX_ERROR_LENGTH
];
55 static void _set(org::libmemcached::Instance
& server
, Memcached
& memc
)
57 if (server
.error_messages
and server
.error_messages
->query_id
!= server
.root
->query_id
)
59 memcached_error_free(server
);
62 if (memc
.error_messages
)
64 if (memc
.error_messages
->rc
== MEMCACHED_TIMEOUT
)
66 server
.io_wait_count
.timeouts
++;
69 memcached_error_t
*error
= libmemcached_xmalloc(&memc
, memcached_error_t
);
72 memcpy(error
, memc
.error_messages
, sizeof(memcached_error_t
));
73 error
->next
= server
.error_messages
;
74 server
.error_messages
= error
;
80 static int error_log_fd
= -1;
83 static void _set(memcached_st
& memc
, memcached_string_t
*str
, memcached_return_t
&rc
, const char *at
, int local_errno
= 0)
85 if (memc
.error_messages
&& memc
.error_messages
->query_id
!= memc
.query_id
)
87 memcached_error_free(memc
);
90 // For memory allocation we use our error since it is a bit more specific
91 if (local_errno
== ENOMEM
and rc
== MEMCACHED_ERRNO
)
93 rc
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
96 if (rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
)
101 if (rc
== MEMCACHED_ERRNO
and not local_errno
)
107 if (rc
== MEMCACHED_ERRNO
and local_errno
== ENOTCONN
)
109 rc
= MEMCACHED_CONNECTION_FAILURE
;
112 if (rc
== MEMCACHED_ERRNO
and local_errno
== ECONNRESET
)
114 rc
= MEMCACHED_CONNECTION_FAILURE
;
117 if (local_errno
== EINVAL
)
119 rc
= MEMCACHED_INVALID_ARGUMENTS
;
122 if (local_errno
== ECONNREFUSED
)
124 rc
= MEMCACHED_CONNECTION_FAILURE
;
127 memcached_error_t
*error
= libmemcached_xmalloc(&memc
, memcached_error_t
);
128 if (error
== NULL
) // Bad business if this happens
134 error
->query_id
= memc
.query_id
;
136 error
->local_errno
= local_errno
;
138 const char *errmsg_ptr
;
139 char errmsg
[MAX_ERROR_LENGTH
];
145 #if defined(STRERROR_R_CHAR_P) && STRERROR_R_CHAR_P
146 errmsg_ptr
= strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
147 #elif defined(HAVE_STRERROR_R) && HAVE_STRERROR_R
148 strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
150 #elif defined(HAVE_STRERROR) && HAVE_STRERROR
151 snprintf(errmsg
, sizeof(errmsg
), "%s", strerror(local_errno
));
157 if (str
and str
->size
and local_errno
)
159 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %s(%s), %.*s -> %s",
161 memcached_strerror(&memc
, rc
),
163 memcached_string_printf(*str
), at
);
165 else if (local_errno
)
167 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %s(%s) -> %s",
169 memcached_strerror(&memc
, rc
),
173 else if (rc
== MEMCACHED_PARSE_ERROR
and str
and str
->size
)
175 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %.*s -> %s",
177 int(str
->size
), str
->c_str
, at
);
179 else if (str
and str
->size
)
181 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %s, %.*s -> %s",
183 memcached_strerror(&memc
, rc
),
184 int(str
->size
), str
->c_str
, at
);
188 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %s -> %s",
190 memcached_strerror(&memc
, rc
), at
);
193 error
->next
= memc
.error_messages
;
194 memc
.error_messages
= error
;
197 if (error_log_fd
== -1)
199 // unlink("/tmp/libmemcachd.log");
200 if ((error_log_fd
= open("/tmp/libmemcachd.log", O_CREAT
| O_WRONLY
| O_APPEND
, 0644)) < 0)
206 ::write(error_log_fd
, error
->message
, error
->size
);
207 ::write(error_log_fd
, "\n", 1);
211 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
213 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
214 memcached_string_t tmp
= { str
, length
};
215 return memcached_set_error(memc
, rc
, at
, tmp
);
218 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
220 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
221 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a Instance");
223 memcached_string_t tmp
= { str
, length
};
224 return memcached_set_error(self
, rc
, at
, tmp
);
227 #ifndef __INTEL_COMPILER
228 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
231 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
233 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
234 if (memcached_fatal(rc
) == false)
239 _set(memc
, &str
, rc
, at
);
244 memcached_return_t
memcached_set_parser_error(memcached_st
& memc
,
246 const char *format
, ...)
251 va_start(args
, format
);
252 int length
= vsnprintf(buffer
, sizeof(buffer
), format
, args
);
255 return memcached_set_error(memc
, MEMCACHED_PARSE_ERROR
, at
, buffer
, length
);
258 static inline size_t append_host_to_string(org::libmemcached::Instance
& self
, char* buffer
, const size_t buffer_length
)
263 case MEMCACHED_CONNECTION_TCP
:
264 case MEMCACHED_CONNECTION_UDP
:
265 size
+= snprintf(buffer
, buffer_length
, " host: %s:%d",
266 self
.hostname(), int(self
.port()));
269 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
270 size
+= snprintf(buffer
, buffer_length
, " socket: %s",
278 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
280 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
281 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a org::libmemcached::Instance");
282 if (memcached_fatal(rc
) == false and rc
!= MEMCACHED_CLIENT_ERROR
)
287 char hostname_port_message
[MAX_ERROR_LENGTH
];
288 char* hostname_port_message_ptr
= hostname_port_message
;
292 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ",
293 memcached_string_printf(str
));
294 hostname_port_message_ptr
+= size
;
297 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
299 memcached_string_t error_host
= { hostname_port_message
, size_t(size
) };
301 assert_msg(self
.root
, "Programmer error, root was not set on instance");
302 if (self
.root
== NULL
)
307 _set(*self
.root
, &error_host
, rc
, at
);
308 _set(self
, (*self
.root
));
309 assert(self
.root
->error_messages
);
310 assert(self
.error_messages
);
311 assert(self
.error_messages
->rc
== self
.root
->error_messages
->rc
);
316 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
)
318 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a org::libmemcached::Instance");
319 if (memcached_fatal(rc
) == false)
324 char hostname_port
[MEMCACHED_NI_MAXHOST
+MEMCACHED_NI_MAXSERV
+ sizeof("host : ")];
325 size_t size
= append_host_to_string(self
, hostname_port
, sizeof(hostname_port
));
327 memcached_string_t error_host
= { hostname_port
, size
};
329 if (self
.root
== NULL
)
334 _set(*self
.root
, &error_host
, rc
, at
);
335 _set(self
, *self
.root
);
340 memcached_return_t
memcached_set_error(memcached_st
& self
, memcached_return_t rc
, const char *at
)
342 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
343 if (memcached_fatal(rc
) == false)
348 _set(self
, NULL
, rc
, at
);
353 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
355 memcached_string_t tmp
= { str
, length
};
356 return memcached_set_errno(self
, local_errno
, at
, tmp
);
359 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
361 memcached_string_t tmp
= { str
, length
};
362 return memcached_set_errno(self
, local_errno
, at
, tmp
);
365 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
)
367 if (local_errno
== 0)
369 return MEMCACHED_SUCCESS
;
372 memcached_return_t rc
= MEMCACHED_ERRNO
;
373 _set(self
, NULL
, rc
, at
, local_errno
);
378 memcached_return_t
memcached_set_errno(memcached_st
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
380 if (local_errno
== 0)
382 return MEMCACHED_SUCCESS
;
385 memcached_return_t rc
= MEMCACHED_ERRNO
;
386 _set(memc
, &str
, rc
, at
, local_errno
);
391 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
393 if (local_errno
== 0)
395 return MEMCACHED_SUCCESS
;
398 char hostname_port_message
[MAX_ERROR_LENGTH
];
399 char* hostname_port_message_ptr
= hostname_port_message
;
403 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ", memcached_string_printf(str
));
405 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
407 memcached_string_t error_host
= { hostname_port_message
, size
};
409 memcached_return_t rc
= MEMCACHED_ERRNO
;
410 if (self
.root
== NULL
)
415 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
416 _set(self
, (*self
.root
));
419 if (self
.root
->error_messages
->rc
!= self
.error_messages
->rc
)
421 fprintf(stderr
, "%s:%d %s != %s\n", __FILE__
, __LINE__
,
422 memcached_strerror(NULL
, self
.root
->error_messages
->rc
),
423 memcached_strerror(NULL
, self
.error_messages
->rc
));
430 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
)
432 if (local_errno
== 0)
434 return MEMCACHED_SUCCESS
;
437 char hostname_port_message
[MAX_ERROR_LENGTH
];
438 size_t size
= append_host_to_string(self
, hostname_port_message
, sizeof(hostname_port_message
));
440 memcached_string_t error_host
= { hostname_port_message
, size
};
442 memcached_return_t rc
= MEMCACHED_ERRNO
;
443 if (self
.root
== NULL
)
448 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
449 _set(self
, (*self
.root
));
454 static void _error_print(const memcached_error_t
*error
)
461 if (error
->size
== 0)
463 fprintf(stderr
, "\t%s\n", memcached_strerror(NULL
, error
->rc
) );
467 fprintf(stderr
, "\t%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
470 _error_print(error
->next
);
473 void memcached_error_print(const memcached_st
*shell
)
475 const Memcached
* self
= memcached2Memcached(shell
);
481 _error_print(self
->error_messages
);
483 for (uint32_t x
= 0; x
< memcached_server_count(self
); x
++)
485 org::libmemcached::Instance
* instance
= memcached_instance_by_position(self
, x
);
487 _error_print(instance
->error_messages
);
491 static void _error_free(memcached_error_t
*error
)
495 _error_free(error
->next
);
497 libmemcached_free(error
->root
, error
);
501 void memcached_error_free(memcached_st
& self
)
503 _error_free(self
.error_messages
);
504 self
.error_messages
= NULL
;
507 void memcached_error_free(org::libmemcached::Instance
& self
)
509 _error_free(self
.error_messages
);
510 self
.error_messages
= NULL
;
513 void memcached_error_free(memcached_server_st
& self
)
515 _error_free(self
.error_messages
);
516 self
.error_messages
= NULL
;
519 const char *memcached_error(const memcached_st
*memc
)
521 return memcached_last_error_message(memc
);
524 const char *memcached_last_error_message(const memcached_st
*shell
)
526 const Memcached
* memc
= memcached2Memcached(shell
);
529 if (memc
->error_messages
)
531 if (memc
->error_messages
->size
== 0)
533 return memc
->error_messages
->message
;
536 return memcached_strerror(memc
, memc
->error_messages
->rc
);
539 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
542 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
545 bool memcached_has_current_error(memcached_st
&memc
)
547 if (memc
.error_messages
548 and memc
.error_messages
->query_id
== memc
.query_id
549 and memcached_failed(memc
.error_messages
->rc
))
557 bool memcached_has_current_error(org::libmemcached::Instance
& server
)
559 return memcached_has_current_error(*(server
.root
));
562 memcached_return_t
memcached_last_error(const memcached_st
*shell
)
564 const Memcached
* memc
= memcached2Memcached(shell
);
567 if (memc
->error_messages
)
569 return memc
->error_messages
->rc
;
572 return MEMCACHED_SUCCESS
;
575 return MEMCACHED_INVALID_ARGUMENTS
;
578 int memcached_last_error_errno(const memcached_st
*shell
)
580 const Memcached
* memc
= memcached2Memcached(shell
);
586 if (memc
->error_messages
== NULL
)
591 return memc
->error_messages
->local_errno
;
594 const char *memcached_server_error(const memcached_server_instance_st server
)
601 if (server
->error_messages
== NULL
)
603 return memcached_strerror(server
->root
, MEMCACHED_SUCCESS
);
606 if (server
->error_messages
->size
== 0)
608 return memcached_strerror(server
->root
, server
->error_messages
->rc
);
611 return server
->error_messages
->message
;
615 memcached_error_t
*memcached_error_copy(const org::libmemcached::Instance
& server
)
617 if (server
.error_messages
== NULL
)
622 memcached_error_t
*error
= libmemcached_xmalloc(server
.root
, memcached_error_t
);
623 memcpy(error
, server
.error_messages
, sizeof(memcached_error_t
));
629 memcached_return_t
memcached_server_error_return(memcached_server_instance_st ptr
)
633 return MEMCACHED_INVALID_ARGUMENTS
;
636 if (ptr
and ptr
->error_messages
)
638 return ptr
->error_messages
->rc
;
641 return MEMCACHED_SUCCESS
;
644 memcached_return_t
memcached_instance_error_return(org::libmemcached::Instance
* instance
)
646 if (instance
== NULL
)
648 return MEMCACHED_INVALID_ARGUMENTS
;
651 if (instance
and instance
->error_messages
)
653 return instance
->error_messages
->rc
;
656 return MEMCACHED_SUCCESS
;