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
));
145 strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
151 if (str
and str
->size
and local_errno
)
153 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %s(%s), %.*s -> %s",
155 memcached_strerror(&memc
, rc
),
157 memcached_string_printf(*str
), at
);
159 else if (local_errno
)
161 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %s(%s) -> %s",
163 memcached_strerror(&memc
, rc
),
167 else if (rc
== MEMCACHED_PARSE_ERROR
and str
and str
->size
)
169 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %.*s -> %s",
171 int(str
->size
), str
->c_str
, at
);
173 else if (str
and str
->size
)
175 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %s, %.*s -> %s",
177 memcached_strerror(&memc
, rc
),
178 int(str
->size
), str
->c_str
, at
);
182 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %s -> %s",
184 memcached_strerror(&memc
, rc
), at
);
187 error
->next
= memc
.error_messages
;
188 memc
.error_messages
= error
;
191 if (error_log_fd
== -1)
193 // unlink("/tmp/libmemcachd.log");
194 if ((error_log_fd
= open("/tmp/libmemcachd.log", O_CREAT
| O_WRONLY
| O_APPEND
, 0644)) < 0)
200 ::write(error_log_fd
, error
->message
, error
->size
);
201 ::write(error_log_fd
, "\n", 1);
205 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
207 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
208 memcached_string_t tmp
= { str
, length
};
209 return memcached_set_error(memc
, rc
, at
, tmp
);
212 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
214 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
215 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a Instance");
217 memcached_string_t tmp
= { str
, length
};
218 return memcached_set_error(self
, rc
, at
, tmp
);
221 #ifndef __INTEL_COMPILER
222 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
225 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
227 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
228 if (memcached_fatal(rc
) == false)
233 _set(memc
, &str
, rc
, at
);
238 memcached_return_t
memcached_set_parser_error(memcached_st
& memc
,
240 const char *format
, ...)
245 va_start(args
, format
);
246 int length
= vsnprintf(buffer
, sizeof(buffer
), format
, args
);
249 return memcached_set_error(memc
, MEMCACHED_PARSE_ERROR
, at
, buffer
, length
);
252 static inline size_t append_host_to_string(org::libmemcached::Instance
& self
, char* buffer
, const size_t buffer_length
)
257 case MEMCACHED_CONNECTION_TCP
:
258 case MEMCACHED_CONNECTION_UDP
:
259 size
+= snprintf(buffer
, buffer_length
, " host: %s:%d",
260 self
.hostname
, int(self
.port()));
263 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
264 size
+= snprintf(buffer
, buffer_length
, " socket: %s",
272 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
274 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
275 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a org::libmemcached::Instance");
276 if (memcached_fatal(rc
) == false)
281 char hostname_port_message
[MAX_ERROR_LENGTH
];
282 char* hostname_port_message_ptr
= hostname_port_message
;
286 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ",
287 memcached_string_printf(str
));
288 hostname_port_message_ptr
+= size
;
291 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
293 memcached_string_t error_host
= { hostname_port_message
, size_t(size
) };
296 if (self
.root
== NULL
)
301 _set(*self
.root
, &error_host
, rc
, at
);
302 _set(self
, (*self
.root
));
303 assert(self
.root
->error_messages
);
304 assert(self
.error_messages
);
309 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
)
311 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a org::libmemcached::Instance");
312 if (memcached_fatal(rc
) == false)
317 char hostname_port
[NI_MAXHOST
+NI_MAXSERV
+ sizeof("host : ")];
318 size_t size
= append_host_to_string(self
, hostname_port
, sizeof(hostname_port
));
320 memcached_string_t error_host
= { hostname_port
, size
};
322 if (self
.root
== NULL
)
327 _set(*self
.root
, &error_host
, rc
, at
);
328 _set(self
, *self
.root
);
333 memcached_return_t
memcached_set_error(memcached_st
& self
, memcached_return_t rc
, const char *at
)
335 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
336 if (memcached_fatal(rc
) == false)
341 _set(self
, NULL
, rc
, at
);
346 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
348 memcached_string_t tmp
= { str
, length
};
349 return memcached_set_errno(self
, local_errno
, at
, tmp
);
352 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
354 memcached_string_t tmp
= { str
, length
};
355 return memcached_set_errno(self
, local_errno
, at
, tmp
);
358 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
)
360 if (local_errno
== 0)
362 return MEMCACHED_SUCCESS
;
365 memcached_return_t rc
= MEMCACHED_ERRNO
;
366 _set(self
, NULL
, rc
, at
, local_errno
);
371 memcached_return_t
memcached_set_errno(memcached_st
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
373 if (local_errno
== 0)
375 return MEMCACHED_SUCCESS
;
378 memcached_return_t rc
= MEMCACHED_ERRNO
;
379 _set(memc
, &str
, rc
, at
, local_errno
);
384 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
386 if (local_errno
== 0)
388 return MEMCACHED_SUCCESS
;
391 char hostname_port_message
[MAX_ERROR_LENGTH
];
392 char* hostname_port_message_ptr
= hostname_port_message
;
396 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ", memcached_string_printf(str
));
398 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
400 memcached_string_t error_host
= { hostname_port_message
, size
};
402 memcached_return_t rc
= MEMCACHED_ERRNO
;
403 if (self
.root
== NULL
)
408 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
409 _set(self
, (*self
.root
));
414 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
)
416 if (local_errno
== 0)
418 return MEMCACHED_SUCCESS
;
421 char hostname_port_message
[MAX_ERROR_LENGTH
];
422 size_t size
= append_host_to_string(self
, hostname_port_message
, sizeof(hostname_port_message
));
424 memcached_string_t error_host
= { hostname_port_message
, size
};
426 memcached_return_t rc
= MEMCACHED_ERRNO
;
427 if (self
.root
== NULL
)
432 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
433 _set(self
, (*self
.root
));
438 static void _error_print(const memcached_error_t
*error
)
445 if (error
->size
== 0)
447 fprintf(stderr
, "\t%s\n", memcached_strerror(NULL
, error
->rc
) );
451 fprintf(stderr
, "\t%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
454 _error_print(error
->next
);
457 void memcached_error_print(const memcached_st
*self
)
464 _error_print(self
->error_messages
);
466 for (uint32_t x
= 0; x
< memcached_server_count(self
); x
++)
468 org::libmemcached::Instance
* instance
= memcached_instance_by_position(self
, x
);
470 _error_print(instance
->error_messages
);
474 static void _error_free(memcached_error_t
*error
)
478 _error_free(error
->next
);
480 libmemcached_free(error
->root
, error
);
484 void memcached_error_free(memcached_st
& self
)
486 _error_free(self
.error_messages
);
487 self
.error_messages
= NULL
;
490 void memcached_error_free(org::libmemcached::Instance
& self
)
492 _error_free(self
.error_messages
);
493 self
.error_messages
= NULL
;
496 void memcached_error_free(memcached_server_st
& self
)
498 _error_free(self
.error_messages
);
499 self
.error_messages
= NULL
;
502 const char *memcached_error(const memcached_st
*memc
)
504 return memcached_last_error_message(memc
);
507 const char *memcached_last_error_message(const memcached_st
*memc
)
511 if (memc
->error_messages
)
513 if (memc
->error_messages
->size
== 0)
515 return memc
->error_messages
->message
;
518 return memcached_strerror(memc
, memc
->error_messages
->rc
);
521 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
524 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
527 bool memcached_has_current_error(memcached_st
&memc
)
529 if (memc
.error_messages
530 and memc
.error_messages
->query_id
== memc
.query_id
531 and memcached_failed(memc
.error_messages
->rc
))
539 bool memcached_has_current_error(org::libmemcached::Instance
& server
)
541 return memcached_has_current_error(*(server
.root
));
544 memcached_return_t
memcached_last_error(const memcached_st
*memc
)
548 if (memc
->error_messages
)
550 return memc
->error_messages
->rc
;
553 return MEMCACHED_SUCCESS
;
556 return MEMCACHED_INVALID_ARGUMENTS
;
559 int memcached_last_error_errno(const memcached_st
*memc
)
566 if (memc
->error_messages
== NULL
)
571 return memc
->error_messages
->local_errno
;
574 const char *memcached_server_error(const memcached_server_instance_st server
)
581 if (server
->error_messages
== NULL
)
583 return memcached_strerror(server
->root
, MEMCACHED_SUCCESS
);
586 if (server
->error_messages
->size
== 0)
588 return memcached_strerror(server
->root
, server
->error_messages
->rc
);
591 return server
->error_messages
->message
;
595 memcached_error_t
*memcached_error_copy(const org::libmemcached::Instance
& server
)
597 if (server
.error_messages
== NULL
)
602 memcached_error_t
*error
= libmemcached_xmalloc(server
.root
, memcached_error_t
);
603 memcpy(error
, server
.error_messages
, sizeof(memcached_error_t
));
609 memcached_return_t
memcached_server_error_return(memcached_server_instance_st ptr
)
613 return MEMCACHED_INVALID_ARGUMENTS
;
616 if (ptr
and ptr
->error_messages
)
618 return ptr
->error_messages
->rc
;
621 return MEMCACHED_SUCCESS
;
624 memcached_return_t
memcached_instance_error_return(org::libmemcached::Instance
* instance
)
626 if (instance
== NULL
)
628 return MEMCACHED_INVALID_ARGUMENTS
;
631 if (instance
and instance
->error_messages
)
633 return instance
->error_messages
->rc
;
636 return MEMCACHED_SUCCESS
;