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 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
223 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
224 if (memcached_fatal(rc
) == false)
229 _set(memc
, &str
, rc
, at
);
234 memcached_return_t
memcached_set_parser_error(memcached_st
& memc
,
236 const char *format
, ...)
241 va_start(args
, format
);
242 int length
= vsnprintf(buffer
, sizeof(buffer
), format
, args
);
245 return memcached_set_error(memc
, MEMCACHED_PARSE_ERROR
, at
, buffer
, length
);
248 static inline size_t append_host_to_string(org::libmemcached::Instance
& self
, char* buffer
, const size_t buffer_length
)
253 case MEMCACHED_CONNECTION_TCP
:
254 case MEMCACHED_CONNECTION_UDP
:
255 size
+= snprintf(buffer
, buffer_length
, " host: %s:%d",
256 self
.hostname
, int(self
.port()));
259 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
260 size
+= snprintf(buffer
, buffer_length
, " socket: %s",
268 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
270 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
271 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a org::libmemcached::Instance");
272 if (memcached_fatal(rc
) == false)
277 char hostname_port_message
[MAX_ERROR_LENGTH
];
278 char* hostname_port_message_ptr
= hostname_port_message
;
282 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ",
283 memcached_string_printf(str
));
284 hostname_port_message_ptr
+= size
;
287 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
289 memcached_string_t error_host
= { hostname_port_message
, size_t(size
) };
292 if (self
.root
== NULL
)
297 _set(*self
.root
, &error_host
, rc
, at
);
298 _set(self
, (*self
.root
));
299 assert(self
.root
->error_messages
);
300 assert(self
.error_messages
);
305 memcached_return_t
memcached_set_error(org::libmemcached::Instance
& self
, memcached_return_t rc
, const char *at
)
307 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a org::libmemcached::Instance");
308 if (memcached_fatal(rc
) == false)
313 char hostname_port
[NI_MAXHOST
+NI_MAXSERV
+ sizeof("host : ")];
314 size_t size
= append_host_to_string(self
, hostname_port
, sizeof(hostname_port
));
316 memcached_string_t error_host
= { hostname_port
, size
};
318 if (self
.root
== NULL
)
323 _set(*self
.root
, &error_host
, rc
, at
);
324 _set(self
, *self
.root
);
329 memcached_return_t
memcached_set_error(memcached_st
& self
, memcached_return_t rc
, const char *at
)
331 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
332 if (memcached_fatal(rc
) == false)
337 _set(self
, NULL
, rc
, at
);
342 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
344 memcached_string_t tmp
= { str
, length
};
345 return memcached_set_errno(self
, local_errno
, at
, tmp
);
348 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
350 memcached_string_t tmp
= { str
, length
};
351 return memcached_set_errno(self
, local_errno
, at
, tmp
);
354 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
)
356 if (local_errno
== 0)
358 return MEMCACHED_SUCCESS
;
361 memcached_return_t rc
= MEMCACHED_ERRNO
;
362 _set(self
, NULL
, rc
, at
, local_errno
);
367 memcached_return_t
memcached_set_errno(memcached_st
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
369 if (local_errno
== 0)
371 return MEMCACHED_SUCCESS
;
374 memcached_return_t rc
= MEMCACHED_ERRNO
;
375 _set(memc
, &str
, rc
, at
, local_errno
);
380 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
382 if (local_errno
== 0)
384 return MEMCACHED_SUCCESS
;
387 char hostname_port_message
[MAX_ERROR_LENGTH
];
388 char* hostname_port_message_ptr
= hostname_port_message
;
392 size
= snprintf(hostname_port_message_ptr
, sizeof(hostname_port_message
), "%.*s, ", memcached_string_printf(str
));
394 size
+= append_host_to_string(self
, hostname_port_message_ptr
, sizeof(hostname_port_message
) -size
);
396 memcached_string_t error_host
= { hostname_port_message
, size
};
398 memcached_return_t rc
= MEMCACHED_ERRNO
;
399 if (self
.root
== NULL
)
404 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
405 _set(self
, (*self
.root
));
410 memcached_return_t
memcached_set_errno(org::libmemcached::Instance
& self
, int local_errno
, const char *at
)
412 if (local_errno
== 0)
414 return MEMCACHED_SUCCESS
;
417 char hostname_port_message
[MAX_ERROR_LENGTH
];
418 size_t size
= append_host_to_string(self
, hostname_port_message
, sizeof(hostname_port_message
));
420 memcached_string_t error_host
= { hostname_port_message
, size
};
422 memcached_return_t rc
= MEMCACHED_ERRNO
;
423 if (self
.root
== NULL
)
428 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
429 _set(self
, (*self
.root
));
434 static void _error_print(const memcached_error_t
*error
)
441 if (error
->size
== 0)
443 fprintf(stderr
, "\t%s\n", memcached_strerror(NULL
, error
->rc
) );
447 fprintf(stderr
, "\t%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
450 _error_print(error
->next
);
453 void memcached_error_print(const memcached_st
*self
)
460 _error_print(self
->error_messages
);
462 for (uint32_t x
= 0; x
< memcached_server_count(self
); x
++)
464 org::libmemcached::Instance
* instance
= memcached_instance_by_position(self
, x
);
466 _error_print(instance
->error_messages
);
470 static void _error_free(memcached_error_t
*error
)
474 _error_free(error
->next
);
476 libmemcached_free(error
->root
, error
);
480 void memcached_error_free(memcached_st
& self
)
482 _error_free(self
.error_messages
);
483 self
.error_messages
= NULL
;
486 void memcached_error_free(org::libmemcached::Instance
& self
)
488 _error_free(self
.error_messages
);
489 self
.error_messages
= NULL
;
492 void memcached_error_free(memcached_server_st
& self
)
494 _error_free(self
.error_messages
);
495 self
.error_messages
= NULL
;
498 const char *memcached_last_error_message(const memcached_st
*memc
)
502 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
505 if (memc
->error_messages
== NULL
)
507 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
510 if (memc
->error_messages
->size
== 0)
512 return memcached_strerror(memc
, memc
->error_messages
->rc
);
515 return memc
->error_messages
->message
;
518 bool memcached_has_current_error(memcached_st
&memc
)
520 if (memc
.error_messages
521 and memc
.error_messages
->query_id
== memc
.query_id
522 and memcached_failed(memc
.error_messages
->rc
))
530 bool memcached_has_current_error(org::libmemcached::Instance
& server
)
532 return memcached_has_current_error(*(server
.root
));
535 memcached_return_t
memcached_last_error(const memcached_st
*memc
)
539 return MEMCACHED_INVALID_ARGUMENTS
;
542 if (memc
->error_messages
== NULL
)
544 return MEMCACHED_SUCCESS
;
547 return memc
->error_messages
->rc
;
550 int memcached_last_error_errno(const memcached_st
*memc
)
557 if (memc
->error_messages
== NULL
)
562 return memc
->error_messages
->local_errno
;
565 const char *memcached_server_error(const memcached_server_instance_st server
)
572 if (server
->error_messages
== NULL
)
574 return memcached_strerror(server
->root
, MEMCACHED_SUCCESS
);
577 if (server
->error_messages
->size
== 0)
579 return memcached_strerror(server
->root
, server
->error_messages
->rc
);
582 return server
->error_messages
->message
;
586 memcached_error_t
*memcached_error_copy(const org::libmemcached::Instance
& server
)
588 if (server
.error_messages
== NULL
)
593 memcached_error_t
*error
= libmemcached_xmalloc(server
.root
, memcached_error_t
);
594 memcpy(error
, server
.error_messages
, sizeof(memcached_error_t
));
600 memcached_return_t
memcached_server_error_return(memcached_server_instance_st ptr
)
604 return MEMCACHED_INVALID_ARGUMENTS
;
607 if (ptr
and ptr
->error_messages
)
609 return ptr
->error_messages
->rc
;
612 return MEMCACHED_SUCCESS
;
615 memcached_return_t
memcached_instance_error_return(org::libmemcached::Instance
* instance
)
617 if (instance
== NULL
)
619 return MEMCACHED_INVALID_ARGUMENTS
;
622 if (instance
and instance
->error_messages
)
624 return instance
->error_messages
->rc
;
627 return MEMCACHED_SUCCESS
;