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 and rc
!= MEMCACHED_CLIENT_ERROR
)
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
) };
298 assert_msg(self
.root
, "Programmer error, root was not set on instance");
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
);
308 assert(self
.error_messages
->rc
== self
.root
->error_messages
->rc
);
313 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
)
315 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a org::libmemcached::Instance");
316 if (memcached_fatal(rc
) == false)
321 char hostname_port
[NI_MAXHOST
+NI_MAXSERV
+ sizeof("host : ")];
322 size_t size
= append_host_to_string(self
, hostname_port
, sizeof(hostname_port
));
324 memcached_string_t error_host
= { hostname_port
, size
};
326 if (self
.root
== NULL
)
331 _set(*self
.root
, &error_host
, rc
, at
);
332 _set(self
, *self
.root
);
337 memcached_return_t
memcached_set_error(memcached_st
& self
, memcached_return_t rc
, const char *at
)
339 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
340 if (memcached_fatal(rc
) == false)
345 _set(self
, NULL
, rc
, at
);
350 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
352 memcached_string_t tmp
= { str
, length
};
353 return memcached_set_errno(self
, local_errno
, at
, tmp
);
356 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
358 memcached_string_t tmp
= { str
, length
};
359 return memcached_set_errno(self
, local_errno
, at
, tmp
);
362 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
)
364 if (local_errno
== 0)
366 return MEMCACHED_SUCCESS
;
369 memcached_return_t rc
= MEMCACHED_ERRNO
;
370 _set(self
, NULL
, rc
, at
, local_errno
);
375 memcached_return_t
memcached_set_errno(memcached_st
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
377 if (local_errno
== 0)
379 return MEMCACHED_SUCCESS
;
382 memcached_return_t rc
= MEMCACHED_ERRNO
;
383 _set(memc
, &str
, rc
, at
, local_errno
);
388 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
390 if (local_errno
== 0)
392 return MEMCACHED_SUCCESS
;
395 char hostname_port_message
[MAX_ERROR_LENGTH
];
396 char* hostname_port_message_ptr
= hostname_port_message
;
400 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ", memcached_string_printf(str
));
402 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
404 memcached_string_t error_host
= { hostname_port_message
, size
};
406 memcached_return_t rc
= MEMCACHED_ERRNO
;
407 if (self
.root
== NULL
)
412 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
413 _set(self
, (*self
.root
));
416 if (self
.root
->error_messages
->rc
!= self
.error_messages
->rc
)
418 fprintf(stderr
, "%s:%d %s != %s\n", __FILE__
, __LINE__
,
419 memcached_strerror(NULL
, self
.root
->error_messages
->rc
),
420 memcached_strerror(NULL
, self
.error_messages
->rc
));
427 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
)
429 if (local_errno
== 0)
431 return MEMCACHED_SUCCESS
;
434 char hostname_port_message
[MAX_ERROR_LENGTH
];
435 size_t size
= append_host_to_string(self
, hostname_port_message
, sizeof(hostname_port_message
));
437 memcached_string_t error_host
= { hostname_port_message
, size
};
439 memcached_return_t rc
= MEMCACHED_ERRNO
;
440 if (self
.root
== NULL
)
445 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
446 _set(self
, (*self
.root
));
451 static void _error_print(const memcached_error_t
*error
)
458 if (error
->size
== 0)
460 fprintf(stderr
, "\t%s\n", memcached_strerror(NULL
, error
->rc
) );
464 fprintf(stderr
, "\t%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
467 _error_print(error
->next
);
470 void memcached_error_print(const memcached_st
*self
)
477 _error_print(self
->error_messages
);
479 for (uint32_t x
= 0; x
< memcached_server_count(self
); x
++)
481 org::libmemcached::Instance
* instance
= memcached_instance_by_position(self
, x
);
483 _error_print(instance
->error_messages
);
487 static void _error_free(memcached_error_t
*error
)
491 _error_free(error
->next
);
493 libmemcached_free(error
->root
, error
);
497 void memcached_error_free(memcached_st
& self
)
499 _error_free(self
.error_messages
);
500 self
.error_messages
= NULL
;
503 void memcached_error_free(org::libmemcached::Instance
& self
)
505 _error_free(self
.error_messages
);
506 self
.error_messages
= NULL
;
509 void memcached_error_free(memcached_server_st
& self
)
511 _error_free(self
.error_messages
);
512 self
.error_messages
= NULL
;
515 const char *memcached_error(const memcached_st
*memc
)
517 return memcached_last_error_message(memc
);
520 const char *memcached_last_error_message(const memcached_st
*memc
)
524 if (memc
->error_messages
)
526 if (memc
->error_messages
->size
== 0)
528 return memc
->error_messages
->message
;
531 return memcached_strerror(memc
, memc
->error_messages
->rc
);
534 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
537 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
540 bool memcached_has_current_error(memcached_st
&memc
)
542 if (memc
.error_messages
543 and memc
.error_messages
->query_id
== memc
.query_id
544 and memcached_failed(memc
.error_messages
->rc
))
552 bool memcached_has_current_error(org::libmemcached::Instance
& server
)
554 return memcached_has_current_error(*(server
.root
));
557 memcached_return_t
memcached_last_error(const memcached_st
*memc
)
561 if (memc
->error_messages
)
563 return memc
->error_messages
->rc
;
566 return MEMCACHED_SUCCESS
;
569 return MEMCACHED_INVALID_ARGUMENTS
;
572 int memcached_last_error_errno(const memcached_st
*memc
)
579 if (memc
->error_messages
== NULL
)
584 return memc
->error_messages
->local_errno
;
587 const char *memcached_server_error(const memcached_server_instance_st server
)
594 if (server
->error_messages
== NULL
)
596 return memcached_strerror(server
->root
, MEMCACHED_SUCCESS
);
599 if (server
->error_messages
->size
== 0)
601 return memcached_strerror(server
->root
, server
->error_messages
->rc
);
604 return server
->error_messages
->message
;
608 memcached_error_t
*memcached_error_copy(const org::libmemcached::Instance
& server
)
610 if (server
.error_messages
== NULL
)
615 memcached_error_t
*error
= libmemcached_xmalloc(server
.root
, memcached_error_t
);
616 memcpy(error
, server
.error_messages
, sizeof(memcached_error_t
));
622 memcached_return_t
memcached_server_error_return(memcached_server_instance_st ptr
)
626 return MEMCACHED_INVALID_ARGUMENTS
;
629 if (ptr
and ptr
->error_messages
)
631 return ptr
->error_messages
->rc
;
634 return MEMCACHED_SUCCESS
;
637 memcached_return_t
memcached_instance_error_return(org::libmemcached::Instance
* instance
)
639 if (instance
== NULL
)
641 return MEMCACHED_INVALID_ARGUMENTS
;
644 if (instance
and instance
->error_messages
)
646 return instance
->error_messages
->rc
;
649 return MEMCACHED_SUCCESS
;