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
== NULL
)
67 memcached_error_t
*error
= libmemcached_xmalloc(&memc
, memcached_error_t
);
68 if (error
== NULL
) // Bad business if this happens
73 memcpy(error
, memc
.error_messages
, sizeof(memcached_error_t
));
74 error
->next
= server
.error_messages
;
75 server
.error_messages
= error
;
79 static int error_log_fd
= -1;
82 static void _set(memcached_st
& memc
, memcached_string_t
*str
, memcached_return_t
&rc
, const char *at
, int local_errno
= 0)
84 if (memc
.error_messages
&& memc
.error_messages
->query_id
!= memc
.query_id
)
86 memcached_error_free(memc
);
89 // For memory allocation we use our error since it is a bit more specific
90 if (local_errno
== ENOMEM
and rc
== MEMCACHED_ERRNO
)
92 rc
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
95 if (rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
)
100 if (rc
== MEMCACHED_ERRNO
and not local_errno
)
106 if (rc
== MEMCACHED_ERRNO
and local_errno
== ENOTCONN
)
108 rc
= MEMCACHED_CONNECTION_FAILURE
;
111 if (rc
== MEMCACHED_ERRNO
and local_errno
== ECONNRESET
)
113 rc
= MEMCACHED_CONNECTION_FAILURE
;
116 if (local_errno
== EINVAL
)
118 rc
= MEMCACHED_INVALID_ARGUMENTS
;
121 if (local_errno
== ECONNREFUSED
)
123 rc
= MEMCACHED_CONNECTION_FAILURE
;
126 memcached_error_t
*error
= libmemcached_xmalloc(&memc
, memcached_error_t
);
127 if (error
== NULL
) // Bad business if this happens
133 error
->query_id
= memc
.query_id
;
135 error
->local_errno
= local_errno
;
137 const char *errmsg_ptr
;
138 char errmsg
[MAX_ERROR_LENGTH
];
144 #if defined(STRERROR_R_CHAR_P) && STRERROR_R_CHAR_P
145 errmsg_ptr
= strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
146 #elif defined(HAVE_STRERROR_R) && HAVE_STRERROR_R
147 strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
149 #elif defined(HAVE_STRERROR) && HAVE_STRERROR
150 snprintf(errmsg
, sizeof(errmsg
), "%s", strerror(local_errno
));
156 if (str
and str
->size
and local_errno
)
158 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %s(%s), %.*s -> %s",
160 memcached_strerror(&memc
, rc
),
162 memcached_string_printf(*str
), at
);
164 else if (local_errno
)
166 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %s(%s) -> %s",
168 memcached_strerror(&memc
, rc
),
172 else if (rc
== MEMCACHED_PARSE_ERROR
and str
and str
->size
)
174 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %.*s -> %s",
176 int(str
->size
), str
->c_str
, at
);
178 else if (str
and str
->size
)
180 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %s, %.*s -> %s",
182 memcached_strerror(&memc
, rc
),
183 int(str
->size
), str
->c_str
, at
);
187 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%p) %s -> %s",
189 memcached_strerror(&memc
, rc
), at
);
192 error
->next
= memc
.error_messages
;
193 memc
.error_messages
= error
;
196 if (error_log_fd
== -1)
198 // unlink("/tmp/libmemcachd.log");
199 if ((error_log_fd
= open("/tmp/libmemcachd.log", O_CREAT
| O_WRONLY
| O_APPEND
, 0644)) < 0)
205 ::write(error_log_fd
, error
->message
, error
->size
);
206 ::write(error_log_fd
, "\n", 1);
210 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
212 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
213 memcached_string_t tmp
= { str
, length
};
214 return memcached_set_error(memc
, rc
, at
, tmp
);
217 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
219 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
220 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a Instance");
222 memcached_string_t tmp
= { str
, length
};
223 return memcached_set_error(self
, rc
, at
, tmp
);
226 #ifndef __INTEL_COMPILER
227 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
230 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
232 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
233 if (memcached_fatal(rc
) == false)
238 _set(memc
, &str
, rc
, at
);
243 memcached_return_t
memcached_set_parser_error(memcached_st
& memc
,
245 const char *format
, ...)
250 va_start(args
, format
);
251 int length
= vsnprintf(buffer
, sizeof(buffer
), format
, args
);
254 return memcached_set_error(memc
, MEMCACHED_PARSE_ERROR
, at
, buffer
, length
);
257 static inline size_t append_host_to_string(org::libmemcached::Instance
& self
, char* buffer
, const size_t buffer_length
)
262 case MEMCACHED_CONNECTION_TCP
:
263 case MEMCACHED_CONNECTION_UDP
:
264 size
+= snprintf(buffer
, buffer_length
, " host: %s:%d",
265 self
.hostname(), int(self
.port()));
268 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
269 size
+= snprintf(buffer
, buffer_length
, " socket: %s",
277 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
279 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
280 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a org::libmemcached::Instance");
281 if (memcached_fatal(rc
) == false and rc
!= MEMCACHED_CLIENT_ERROR
)
286 char hostname_port_message
[MAX_ERROR_LENGTH
];
287 char* hostname_port_message_ptr
= hostname_port_message
;
291 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ",
292 memcached_string_printf(str
));
293 hostname_port_message_ptr
+= size
;
296 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
298 memcached_string_t error_host
= { hostname_port_message
, size_t(size
) };
300 assert_msg(self
.root
, "Programmer error, root was not set on instance");
301 if (self
.root
== NULL
)
306 _set(*self
.root
, &error_host
, rc
, at
);
307 _set(self
, (*self
.root
));
308 assert(self
.root
->error_messages
);
309 assert(self
.error_messages
);
310 assert(self
.error_messages
->rc
== self
.root
->error_messages
->rc
);
315 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
)
317 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a org::libmemcached::Instance");
318 if (memcached_fatal(rc
) == false)
323 char hostname_port
[MEMCACHED_NI_MAXHOST
+MEMCACHED_NI_MAXSERV
+ sizeof("host : ")];
324 size_t size
= append_host_to_string(self
, hostname_port
, sizeof(hostname_port
));
326 memcached_string_t error_host
= { hostname_port
, size
};
328 if (self
.root
== NULL
)
333 _set(*self
.root
, &error_host
, rc
, at
);
334 _set(self
, *self
.root
);
339 memcached_return_t
memcached_set_error(memcached_st
& self
, memcached_return_t rc
, const char *at
)
341 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
342 if (memcached_fatal(rc
) == false)
347 _set(self
, NULL
, rc
, at
);
352 memcached_return_t
memcached_set_errno(memcached_st
& 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(org::libmemcached::Instance
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
360 memcached_string_t tmp
= { str
, length
};
361 return memcached_set_errno(self
, local_errno
, at
, tmp
);
364 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
)
366 if (local_errno
== 0)
368 return MEMCACHED_SUCCESS
;
371 memcached_return_t rc
= MEMCACHED_ERRNO
;
372 _set(self
, NULL
, rc
, at
, local_errno
);
377 memcached_return_t
memcached_set_errno(memcached_st
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
379 if (local_errno
== 0)
381 return MEMCACHED_SUCCESS
;
384 memcached_return_t rc
= MEMCACHED_ERRNO
;
385 _set(memc
, &str
, rc
, at
, local_errno
);
390 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
392 if (local_errno
== 0)
394 return MEMCACHED_SUCCESS
;
397 char hostname_port_message
[MAX_ERROR_LENGTH
];
398 char* hostname_port_message_ptr
= hostname_port_message
;
402 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ", memcached_string_printf(str
));
404 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
406 memcached_string_t error_host
= { hostname_port_message
, size
};
408 memcached_return_t rc
= MEMCACHED_ERRNO
;
409 if (self
.root
== NULL
)
414 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
415 _set(self
, (*self
.root
));
418 if (self
.root
->error_messages
->rc
!= self
.error_messages
->rc
)
420 fprintf(stderr
, "%s:%d %s != %s\n", __FILE__
, __LINE__
,
421 memcached_strerror(NULL
, self
.root
->error_messages
->rc
),
422 memcached_strerror(NULL
, self
.error_messages
->rc
));
429 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
)
431 if (local_errno
== 0)
433 return MEMCACHED_SUCCESS
;
436 char hostname_port_message
[MAX_ERROR_LENGTH
];
437 size_t size
= append_host_to_string(self
, hostname_port_message
, sizeof(hostname_port_message
));
439 memcached_string_t error_host
= { hostname_port_message
, size
};
441 memcached_return_t rc
= MEMCACHED_ERRNO
;
442 if (self
.root
== NULL
)
447 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
448 _set(self
, (*self
.root
));
453 static void _error_print(const memcached_error_t
*error
)
460 if (error
->size
== 0)
462 fprintf(stderr
, "\t%s\n", memcached_strerror(NULL
, error
->rc
) );
466 fprintf(stderr
, "\t%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
469 _error_print(error
->next
);
472 void memcached_error_print(const memcached_st
*shell
)
474 const Memcached
* self
= memcached2Memcached(shell
);
480 _error_print(self
->error_messages
);
482 for (uint32_t x
= 0; x
< memcached_server_count(self
); x
++)
484 org::libmemcached::Instance
* instance
= memcached_instance_by_position(self
, x
);
486 _error_print(instance
->error_messages
);
490 static void _error_free(memcached_error_t
*error
)
494 _error_free(error
->next
);
496 libmemcached_free(error
->root
, error
);
500 void memcached_error_free(memcached_st
& self
)
502 _error_free(self
.error_messages
);
503 self
.error_messages
= NULL
;
506 void memcached_error_free(org::libmemcached::Instance
& self
)
508 _error_free(self
.error_messages
);
509 self
.error_messages
= NULL
;
512 void memcached_error_free(memcached_server_st
& self
)
514 _error_free(self
.error_messages
);
515 self
.error_messages
= NULL
;
518 const char *memcached_error(const memcached_st
*memc
)
520 return memcached_last_error_message(memc
);
523 const char *memcached_last_error_message(const memcached_st
*shell
)
525 const Memcached
* memc
= memcached2Memcached(shell
);
528 if (memc
->error_messages
)
530 if (memc
->error_messages
->size
== 0)
532 return memc
->error_messages
->message
;
535 return memcached_strerror(memc
, memc
->error_messages
->rc
);
538 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
541 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
544 bool memcached_has_current_error(memcached_st
&memc
)
546 if (memc
.error_messages
547 and memc
.error_messages
->query_id
== memc
.query_id
548 and memcached_failed(memc
.error_messages
->rc
))
556 bool memcached_has_current_error(org::libmemcached::Instance
& server
)
558 return memcached_has_current_error(*(server
.root
));
561 memcached_return_t
memcached_last_error(const memcached_st
*shell
)
563 const Memcached
* memc
= memcached2Memcached(shell
);
566 if (memc
->error_messages
)
568 return memc
->error_messages
->rc
;
571 return MEMCACHED_SUCCESS
;
574 return MEMCACHED_INVALID_ARGUMENTS
;
577 int memcached_last_error_errno(const memcached_st
*shell
)
579 const Memcached
* memc
= memcached2Memcached(shell
);
585 if (memc
->error_messages
== NULL
)
590 return memc
->error_messages
->local_errno
;
593 const char *memcached_server_error(const memcached_server_instance_st server
)
600 if (server
->error_messages
== NULL
)
602 return memcached_strerror(server
->root
, MEMCACHED_SUCCESS
);
605 if (server
->error_messages
->size
== 0)
607 return memcached_strerror(server
->root
, server
->error_messages
->rc
);
610 return server
->error_messages
->message
;
614 memcached_error_t
*memcached_error_copy(const org::libmemcached::Instance
& server
)
616 if (server
.error_messages
== NULL
)
621 memcached_error_t
*error
= libmemcached_xmalloc(server
.root
, memcached_error_t
);
622 memcpy(error
, server
.error_messages
, sizeof(memcached_error_t
));
628 memcached_return_t
memcached_server_error_return(memcached_server_instance_st ptr
)
632 return MEMCACHED_INVALID_ARGUMENTS
;
635 if (ptr
and ptr
->error_messages
)
637 return ptr
->error_messages
->rc
;
640 return MEMCACHED_SUCCESS
;
643 memcached_return_t
memcached_instance_error_return(org::libmemcached::Instance
* instance
)
645 if (instance
== NULL
)
647 return MEMCACHED_INVALID_ARGUMENTS
;
650 if (instance
and instance
->error_messages
)
652 return instance
->error_messages
->rc
;
655 return MEMCACHED_SUCCESS
;