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>
41 #define MAX_ERROR_LENGTH 2048
42 struct memcached_error_t
46 struct memcached_error_t
*next
;
47 memcached_return_t rc
;
50 char message
[MAX_ERROR_LENGTH
];
53 static void _set(org::libmemcached::Instance
& server
, memcached_st
& memc
)
55 if (server
.error_messages
and server
.error_messages
->query_id
!= server
.root
->query_id
)
57 memcached_error_free(server
);
60 if (memc
.error_messages
== NULL
)
65 memcached_error_t
*error
= libmemcached_xmalloc(&memc
, memcached_error_t
);
66 if (error
== NULL
) // Bad business if this happens
71 memcpy(error
, memc
.error_messages
, sizeof(memcached_error_t
));
72 error
->next
= server
.error_messages
;
73 server
.error_messages
= error
;
77 static int error_log_fd
= -1;
80 static void _set(memcached_st
& memc
, memcached_string_t
*str
, memcached_return_t
&rc
, const char *at
, int local_errno
= 0)
82 if (memc
.error_messages
&& memc
.error_messages
->query_id
!= memc
.query_id
)
84 memcached_error_free(memc
);
87 // For memory allocation we use our error since it is a bit more specific
88 if (local_errno
== ENOMEM
and rc
== MEMCACHED_ERRNO
)
90 rc
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
93 if (rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
)
98 if (rc
== MEMCACHED_ERRNO
and not local_errno
)
104 if (rc
== MEMCACHED_ERRNO
and local_errno
== ENOTCONN
)
106 rc
= MEMCACHED_CONNECTION_FAILURE
;
109 if (rc
== MEMCACHED_ERRNO
and local_errno
== ECONNRESET
)
111 rc
= MEMCACHED_CONNECTION_FAILURE
;
114 if (local_errno
== EINVAL
)
116 rc
= MEMCACHED_INVALID_ARGUMENTS
;
119 if (local_errno
== ECONNREFUSED
)
121 rc
= MEMCACHED_CONNECTION_FAILURE
;
124 memcached_error_t
*error
= libmemcached_xmalloc(&memc
, memcached_error_t
);
125 if (error
== NULL
) // Bad business if this happens
131 error
->query_id
= memc
.query_id
;
133 error
->local_errno
= local_errno
;
135 const char *errmsg_ptr
;
136 char errmsg
[MAX_ERROR_LENGTH
];
142 #ifdef STRERROR_R_CHAR_P
143 errmsg_ptr
= strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
144 #elif defined(HAVE_STRERROR_R) && HAVE_STRERROR_R
145 strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
147 #elif defined(HAVE_STRERROR) && HAVE_STRERROR
148 snprintf(errmsg
, sizeof(errmsg
), "%s", strerror(local_errno
));
154 if (str
and str
->size
and local_errno
)
156 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %s(%s), %.*s -> %s",
158 memcached_strerror(&memc
, rc
),
160 memcached_string_printf(*str
), at
);
162 else if (local_errno
)
164 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %s(%s) -> %s",
166 memcached_strerror(&memc
, rc
),
170 else if (rc
== MEMCACHED_PARSE_ERROR
and str
and str
->size
)
172 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %.*s -> %s",
174 int(str
->size
), str
->c_str
, at
);
176 else if (str
and str
->size
)
178 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %s, %.*s -> %s",
180 memcached_strerror(&memc
, rc
),
181 int(str
->size
), str
->c_str
, at
);
185 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %s -> %s",
187 memcached_strerror(&memc
, rc
), at
);
190 error
->next
= memc
.error_messages
;
191 memc
.error_messages
= error
;
194 if (error_log_fd
== -1)
196 // unlink("/tmp/libmemcachd.log");
197 if ((error_log_fd
= open("/tmp/libmemcachd.log", O_CREAT
| O_WRONLY
| O_APPEND
, 0644)) < 0)
203 ::write(error_log_fd
, error
->message
, error
->size
);
204 ::write(error_log_fd
, "\n", 1);
208 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
210 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
211 memcached_string_t tmp
= { str
, length
};
212 return memcached_set_error(memc
, rc
, at
, tmp
);
215 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
217 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
218 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a Instance");
220 memcached_string_t tmp
= { str
, length
};
221 return memcached_set_error(self
, rc
, at
, tmp
);
224 #ifndef __INTEL_COMPILER
225 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
228 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
230 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
231 if (memcached_fatal(rc
) == false)
236 _set(memc
, &str
, rc
, at
);
241 memcached_return_t
memcached_set_parser_error(memcached_st
& memc
,
243 const char *format
, ...)
248 va_start(args
, format
);
249 int length
= vsnprintf(buffer
, sizeof(buffer
), format
, args
);
252 return memcached_set_error(memc
, MEMCACHED_PARSE_ERROR
, at
, buffer
, length
);
255 static inline size_t append_host_to_string(org::libmemcached::Instance
& self
, char* buffer
, const size_t buffer_length
)
260 case MEMCACHED_CONNECTION_TCP
:
261 case MEMCACHED_CONNECTION_UDP
:
262 size
+= snprintf(buffer
, buffer_length
, " host: %s:%d",
263 self
.hostname
, int(self
.port()));
266 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
267 size
+= snprintf(buffer
, buffer_length
, " socket: %s",
275 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
277 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
278 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a org::libmemcached::Instance");
279 if (memcached_fatal(rc
) == false)
284 char hostname_port_message
[MAX_ERROR_LENGTH
];
285 char* hostname_port_message_ptr
= hostname_port_message
;
289 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ",
290 memcached_string_printf(str
));
291 hostname_port_message_ptr
+= size
;
294 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
296 memcached_string_t error_host
= { hostname_port_message
, size_t(size
) };
299 if (self
.root
== NULL
)
304 _set(*self
.root
, &error_host
, rc
, at
);
305 _set(self
, (*self
.root
));
306 assert(self
.root
->error_messages
);
307 assert(self
.error_messages
);
312 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
)
314 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a org::libmemcached::Instance");
315 if (memcached_fatal(rc
) == false)
320 char hostname_port
[NI_MAXHOST
+NI_MAXSERV
+ sizeof("host : ")];
321 size_t size
= append_host_to_string(self
, hostname_port
, sizeof(hostname_port
));
323 memcached_string_t error_host
= { hostname_port
, size
};
325 if (self
.root
== NULL
)
330 _set(*self
.root
, &error_host
, rc
, at
);
331 _set(self
, *self
.root
);
336 memcached_return_t
memcached_set_error(memcached_st
& self
, memcached_return_t rc
, const char *at
)
338 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
339 if (memcached_fatal(rc
) == false)
344 _set(self
, NULL
, rc
, at
);
349 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
351 memcached_string_t tmp
= { str
, length
};
352 return memcached_set_errno(self
, local_errno
, at
, tmp
);
355 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
357 memcached_string_t tmp
= { str
, length
};
358 return memcached_set_errno(self
, local_errno
, at
, tmp
);
361 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
)
363 if (local_errno
== 0)
365 return MEMCACHED_SUCCESS
;
368 memcached_return_t rc
= MEMCACHED_ERRNO
;
369 _set(self
, NULL
, rc
, at
, local_errno
);
374 memcached_return_t
memcached_set_errno(memcached_st
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
376 if (local_errno
== 0)
378 return MEMCACHED_SUCCESS
;
381 memcached_return_t rc
= MEMCACHED_ERRNO
;
382 _set(memc
, &str
, rc
, at
, local_errno
);
387 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
389 if (local_errno
== 0)
391 return MEMCACHED_SUCCESS
;
394 char hostname_port_message
[MAX_ERROR_LENGTH
];
395 char* hostname_port_message_ptr
= hostname_port_message
;
399 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ", memcached_string_printf(str
));
401 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
403 memcached_string_t error_host
= { hostname_port_message
, size
};
405 memcached_return_t rc
= MEMCACHED_ERRNO
;
406 if (self
.root
== NULL
)
411 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
412 _set(self
, (*self
.root
));
417 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
)
419 if (local_errno
== 0)
421 return MEMCACHED_SUCCESS
;
424 char hostname_port_message
[MAX_ERROR_LENGTH
];
425 size_t size
= append_host_to_string(self
, hostname_port_message
, sizeof(hostname_port_message
));
427 memcached_string_t error_host
= { hostname_port_message
, size
};
429 memcached_return_t rc
= MEMCACHED_ERRNO
;
430 if (self
.root
== NULL
)
435 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
436 _set(self
, (*self
.root
));
441 static void _error_print(const memcached_error_t
*error
)
448 if (error
->size
== 0)
450 fprintf(stderr
, "\t%s\n", memcached_strerror(NULL
, error
->rc
) );
454 fprintf(stderr
, "\t%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
457 _error_print(error
->next
);
460 void memcached_error_print(const memcached_st
*self
)
467 _error_print(self
->error_messages
);
469 for (uint32_t x
= 0; x
< memcached_server_count(self
); x
++)
471 org::libmemcached::Instance
* instance
= memcached_instance_by_position(self
, x
);
473 _error_print(instance
->error_messages
);
477 static void _error_free(memcached_error_t
*error
)
481 _error_free(error
->next
);
483 libmemcached_free(error
->root
, error
);
487 void memcached_error_free(memcached_st
& self
)
489 _error_free(self
.error_messages
);
490 self
.error_messages
= NULL
;
493 void memcached_error_free(org::libmemcached::Instance
& self
)
495 _error_free(self
.error_messages
);
496 self
.error_messages
= NULL
;
499 void memcached_error_free(memcached_server_st
& self
)
501 _error_free(self
.error_messages
);
502 self
.error_messages
= NULL
;
505 const char *memcached_error(const memcached_st
*memc
)
507 return memcached_last_error_message(memc
);
510 const char *memcached_last_error_message(const memcached_st
*memc
)
514 if (memc
->error_messages
)
516 if (memc
->error_messages
->size
== 0)
518 return memc
->error_messages
->message
;
521 return memcached_strerror(memc
, memc
->error_messages
->rc
);
524 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
527 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
530 bool memcached_has_current_error(memcached_st
&memc
)
532 if (memc
.error_messages
533 and memc
.error_messages
->query_id
== memc
.query_id
534 and memcached_failed(memc
.error_messages
->rc
))
542 bool memcached_has_current_error(org::libmemcached::Instance
& server
)
544 return memcached_has_current_error(*(server
.root
));
547 memcached_return_t
memcached_last_error(const memcached_st
*memc
)
551 if (memc
->error_messages
)
553 return memc
->error_messages
->rc
;
556 return MEMCACHED_SUCCESS
;
559 return MEMCACHED_INVALID_ARGUMENTS
;
562 int memcached_last_error_errno(const memcached_st
*memc
)
569 if (memc
->error_messages
== NULL
)
574 return memc
->error_messages
->local_errno
;
577 const char *memcached_server_error(const memcached_server_instance_st server
)
584 if (server
->error_messages
== NULL
)
586 return memcached_strerror(server
->root
, MEMCACHED_SUCCESS
);
589 if (server
->error_messages
->size
== 0)
591 return memcached_strerror(server
->root
, server
->error_messages
->rc
);
594 return server
->error_messages
->message
;
598 memcached_error_t
*memcached_error_copy(const org::libmemcached::Instance
& server
)
600 if (server
.error_messages
== NULL
)
605 memcached_error_t
*error
= libmemcached_xmalloc(server
.root
, memcached_error_t
);
606 memcpy(error
, server
.error_messages
, sizeof(memcached_error_t
));
612 memcached_return_t
memcached_server_error_return(memcached_server_instance_st ptr
)
616 return MEMCACHED_INVALID_ARGUMENTS
;
619 if (ptr
and ptr
->error_messages
)
621 return ptr
->error_messages
->rc
;
624 return MEMCACHED_SUCCESS
;
627 memcached_return_t
memcached_instance_error_return(org::libmemcached::Instance
* instance
)
629 if (instance
== NULL
)
631 return MEMCACHED_INVALID_ARGUMENTS
;
634 if (instance
and instance
->error_messages
)
636 return instance
->error_messages
->rc
;
639 return MEMCACHED_SUCCESS
;