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(memcached_server_st
& 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 (local_errno
== EINVAL
)
111 rc
= MEMCACHED_INVALID_ARGUMENTS
;
114 if (local_errno
== ECONNREFUSED
)
116 rc
= MEMCACHED_CONNECTION_FAILURE
;
119 memcached_error_t
*error
= libmemcached_xmalloc(&memc
, memcached_error_t
);
120 if (error
== NULL
) // Bad business if this happens
126 error
->query_id
= memc
.query_id
;
128 error
->local_errno
= local_errno
;
130 const char *errmsg_ptr
;
131 char errmsg
[MAX_ERROR_LENGTH
];
137 #ifdef STRERROR_R_CHAR_P
138 errmsg_ptr
= strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
140 strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
146 if (str
and str
->size
and local_errno
)
148 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %s(%s), %.*s -> %s",
150 memcached_strerror(&memc
, rc
),
152 memcached_string_printf(*str
), at
);
154 else if (local_errno
)
156 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %s(%s) -> %s",
158 memcached_strerror(&memc
, rc
),
162 else if (rc
== MEMCACHED_PARSE_ERROR
and str
and str
->size
)
164 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %.*s -> %s",
166 int(str
->size
), str
->c_str
, at
);
168 else if (str
and str
->size
)
170 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %s, %.*s -> %s",
172 memcached_strerror(&memc
, rc
),
173 int(str
->size
), str
->c_str
, at
);
177 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "(%lu) %s -> %s",
179 memcached_strerror(&memc
, rc
), at
);
182 error
->next
= memc
.error_messages
;
183 memc
.error_messages
= error
;
186 if (error_log_fd
== -1)
188 // unlink("/tmp/libmemcachd.log");
189 if ((error_log_fd
= open("/tmp/libmemcachd.log", O_CREAT
| O_WRONLY
| O_APPEND
, 0644)) < 0)
195 ::write(error_log_fd
, error
->message
, error
->size
);
196 ::write(error_log_fd
, "\n", 1);
200 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
202 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
203 memcached_string_t tmp
= { str
, length
};
204 return memcached_set_error(memc
, rc
, at
, tmp
);
207 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
209 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
210 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_server_st");
212 memcached_string_t tmp
= { str
, length
};
213 return memcached_set_error(self
, rc
, at
, tmp
);
216 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
218 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
219 if (memcached_fatal(rc
) == false)
224 _set(memc
, &str
, rc
, at
);
229 memcached_return_t
memcached_set_parser_error(memcached_st
& memc
,
231 const char *format
, ...)
236 va_start(args
, format
);
237 int length
= vsnprintf(buffer
, sizeof(buffer
), format
, args
);
240 return memcached_set_error(memc
, MEMCACHED_PARSE_ERROR
, at
, buffer
, length
);
243 static inline size_t append_host_to_string(memcached_server_st
& self
, char* buffer
, const size_t buffer_length
)
248 case MEMCACHED_CONNECTION_TCP
:
249 case MEMCACHED_CONNECTION_UDP
:
250 size
+= snprintf(buffer
, buffer_length
, " host: %s:%d",
251 self
.hostname
, int(self
.port
));
254 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
255 size
+= snprintf(buffer
, buffer_length
, " socket: %s",
263 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
265 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
266 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_server_st");
267 if (memcached_fatal(rc
) == false)
272 char hostname_port_message
[MAX_ERROR_LENGTH
];
273 char* hostname_port_message_ptr
= hostname_port_message
;
277 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ",
278 memcached_string_printf(str
));
279 hostname_port_message_ptr
+= size
;
282 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
284 memcached_string_t error_host
= { hostname_port_message
, size_t(size
) };
287 if (self
.root
== NULL
)
292 _set(*self
.root
, &error_host
, rc
, at
);
293 _set(self
, (*self
.root
));
294 assert(self
.root
->error_messages
);
295 assert(self
.error_messages
);
300 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
)
302 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_server_st");
303 if (memcached_fatal(rc
) == false)
308 char hostname_port
[NI_MAXHOST
+NI_MAXSERV
+ sizeof("host : ")];
309 size_t size
= append_host_to_string(self
, hostname_port
, sizeof(hostname_port
));
311 memcached_string_t error_host
= { hostname_port
, size
};
313 if (self
.root
== NULL
)
318 _set(*self
.root
, &error_host
, rc
, at
);
319 _set(self
, *self
.root
);
324 memcached_return_t
memcached_set_error(memcached_st
& self
, memcached_return_t rc
, const char *at
)
326 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
327 if (memcached_fatal(rc
) == false)
332 _set(self
, NULL
, rc
, at
);
337 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
339 memcached_string_t tmp
= { str
, length
};
340 return memcached_set_errno(self
, local_errno
, at
, tmp
);
343 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
345 memcached_string_t tmp
= { str
, length
};
346 return memcached_set_errno(self
, local_errno
, at
, tmp
);
349 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
)
351 if (local_errno
== 0)
353 return MEMCACHED_SUCCESS
;
356 memcached_return_t rc
= MEMCACHED_ERRNO
;
357 _set(self
, NULL
, rc
, at
, local_errno
);
362 memcached_return_t
memcached_set_errno(memcached_st
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
364 if (local_errno
== 0)
366 return MEMCACHED_SUCCESS
;
369 memcached_return_t rc
= MEMCACHED_ERRNO
;
370 _set(memc
, &str
, rc
, at
, local_errno
);
375 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
377 if (local_errno
== 0)
379 return MEMCACHED_SUCCESS
;
382 char hostname_port_message
[MAX_ERROR_LENGTH
];
383 char* hostname_port_message_ptr
= hostname_port_message
;
387 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ", memcached_string_printf(str
));
389 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
391 memcached_string_t error_host
= { hostname_port_message
, size
};
393 memcached_return_t rc
= MEMCACHED_ERRNO
;
394 if (self
.root
== NULL
)
399 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
400 _set(self
, (*self
.root
));
405 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
)
407 if (local_errno
== 0)
409 return MEMCACHED_SUCCESS
;
412 char hostname_port_message
[MAX_ERROR_LENGTH
];
413 size_t size
= append_host_to_string(self
, hostname_port_message
, sizeof(hostname_port_message
));
415 memcached_string_t error_host
= { hostname_port_message
, size
};
417 memcached_return_t rc
= MEMCACHED_ERRNO
;
418 if (self
.root
== NULL
)
423 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
424 _set(self
, (*self
.root
));
429 static void _error_print(const memcached_error_t
*error
)
436 if (error
->size
== 0)
438 fprintf(stderr
, "\t%s\n", memcached_strerror(NULL
, error
->rc
) );
442 fprintf(stderr
, "\t%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
445 _error_print(error
->next
);
448 void memcached_error_print(const memcached_st
*self
)
455 _error_print(self
->error_messages
);
457 for (uint32_t x
= 0; x
< memcached_server_count(self
); x
++)
459 memcached_server_instance_st instance
= memcached_server_instance_by_position(self
, x
);
461 _error_print(instance
->error_messages
);
465 static void _error_free(memcached_error_t
*error
)
472 _error_free(error
->next
);
474 if (error
and error
->root
)
476 libmemcached_free(error
->root
, error
);
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(memcached_server_st
& self
)
492 _error_free(self
.error_messages
);
493 self
.error_messages
= NULL
;
496 const char *memcached_last_error_message(const memcached_st
*memc
)
500 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
503 if (memc
->error_messages
== NULL
)
505 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
508 if (memc
->error_messages
->size
== 0)
510 return memcached_strerror(memc
, memc
->error_messages
->rc
);
513 return memc
->error_messages
->message
;
516 bool memcached_has_current_error(memcached_st
&memc
)
518 if (memc
.error_messages
519 and memc
.error_messages
->query_id
== memc
.query_id
520 and memcached_failed(memc
.error_messages
->rc
))
528 bool memcached_has_current_error(memcached_server_st
& server
)
530 return memcached_has_current_error(*(server
.root
));
533 memcached_return_t
memcached_last_error(const memcached_st
*memc
)
537 return MEMCACHED_INVALID_ARGUMENTS
;
540 if (memc
->error_messages
== NULL
)
542 return MEMCACHED_SUCCESS
;
545 return memc
->error_messages
->rc
;
548 int memcached_last_error_errno(const memcached_st
*memc
)
555 if (memc
->error_messages
== NULL
)
560 return memc
->error_messages
->local_errno
;
563 const char *memcached_server_error(const memcached_server_instance_st server
)
570 if (server
->error_messages
== NULL
)
572 return memcached_strerror(server
->root
, MEMCACHED_SUCCESS
);
575 if (server
->error_messages
->size
== 0)
577 return memcached_strerror(server
->root
, server
->error_messages
->rc
);
580 return server
->error_messages
->message
;
584 memcached_error_t
*memcached_error_copy(const memcached_server_st
& server
)
586 if (server
.error_messages
== NULL
)
591 memcached_error_t
*error
= libmemcached_xmalloc(server
.root
, memcached_error_t
);
592 memcpy(error
, server
.error_messages
, sizeof(memcached_error_t
));
598 memcached_return_t
memcached_server_error_return(memcached_server_instance_st ptr
)
602 return MEMCACHED_INVALID_ARGUMENTS
;
605 if (ptr
and ptr
->error_messages
)
607 return ptr
->error_messages
->rc
;
610 return MEMCACHED_SUCCESS
;