Fix for lp:1123153 (poor use of strtol).
[awesomized/libmemcached] / libmemcached / stats.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <libmemcached/common.h>
38
39 static const char *memcached_stat_keys[] = {
40 "pid",
41 "uptime",
42 "time",
43 "version",
44 "pointer_size",
45 "rusage_user",
46 "rusage_system",
47 "curr_items",
48 "total_items",
49 "bytes",
50 "curr_connections",
51 "total_connections",
52 "connection_structures",
53 "cmd_get",
54 "cmd_set",
55 "get_hits",
56 "get_misses",
57 "evictions",
58 "bytes_read",
59 "bytes_written",
60 "limit_maxbytes",
61 "threads",
62 NULL
63 };
64
65 struct local_context
66 {
67 memcached_stat_fn func;
68 void *context;
69 const char *args;
70 const size_t args_length;
71
72 local_context(memcached_stat_fn func_arg,
73 void *context_arg,
74 const char *args_arg,
75 const size_t args_length_arg) :
76 func(func_arg),
77 context(context_arg),
78 args(args_arg),
79 args_length(args_length_arg)
80 { }
81 };
82
83
84 static memcached_return_t set_data(memcached_stat_st *memc_stat, const char *key, const char *value)
85 {
86
87 if (strlen(key) < 1)
88 {
89 WATCHPOINT_STRING(key);
90 return MEMCACHED_UNKNOWN_STAT_KEY;
91 }
92 else if (strcmp("pid", key) == 0)
93 {
94 errno= 0;
95 int64_t temp= strtoll(value, (char **)NULL, 10);
96 if (errno != 0)
97 {
98 return MEMCACHED_FAILURE;
99 }
100
101 if (temp <= INT32_MAX and ( sizeof(pid_t) == sizeof(int32_t) ))
102 {
103 memc_stat->pid= pid_t(temp);
104 }
105 else if (temp > -1)
106 {
107 memc_stat->pid= pid_t(temp);
108 }
109 else
110 {
111 // If we got a value less then -1 then something went wrong in the
112 // protocol
113 }
114 }
115 else if (not strcmp("uptime", key))
116 {
117 errno= 0;
118 memc_stat->uptime= strtoul(value, (char **)NULL, 10);
119 if (errno != 0)
120 {
121 return MEMCACHED_FAILURE;
122 }
123 }
124 else if (not strcmp("time", key))
125 {
126 errno= 0;
127 memc_stat->time= strtoul(value, (char **)NULL, 10);
128 if (errno != 0)
129 {
130 return MEMCACHED_FAILURE;
131 }
132 }
133 else if (not strcmp("version", key))
134 {
135 memcpy(memc_stat->version, value, strlen(value));
136 memc_stat->version[strlen(value)]= 0;
137 }
138 else if (not strcmp("pointer_size", key))
139 {
140 errno= 0;
141 memc_stat->pointer_size= strtoul(value, (char **)NULL, 10);
142 if (errno != 0)
143 {
144 return MEMCACHED_FAILURE;
145 }
146 }
147 else if (not strcmp("rusage_user", key))
148 {
149 char *walk_ptr;
150 for (walk_ptr= (char*)value; (!ispunct(*walk_ptr)); walk_ptr++) {};
151 *walk_ptr= 0;
152 walk_ptr++;
153
154 errno= 0;
155 memc_stat->rusage_user_seconds= strtoul(value, (char **)NULL, 10);
156 if (errno != 0)
157 {
158 return MEMCACHED_FAILURE;
159 }
160
161 errno= 0;
162 memc_stat->rusage_user_microseconds= strtoul(walk_ptr, (char **)NULL, 10);
163 if (errno != 0)
164 {
165 return MEMCACHED_FAILURE;
166 }
167 }
168 else if (not strcmp("rusage_system", key))
169 {
170 char *walk_ptr;
171 for (walk_ptr= (char*)value; (!ispunct(*walk_ptr)); walk_ptr++) {};
172 *walk_ptr= 0;
173 walk_ptr++;
174
175 errno= 0;
176 memc_stat->rusage_system_seconds= strtoul(value, (char **)NULL, 10);
177 if (errno != 0)
178 {
179 return MEMCACHED_FAILURE;
180 }
181
182 errno= 0;
183 memc_stat->rusage_system_microseconds= strtoul(walk_ptr, (char **)NULL, 10);
184 if (errno != 0)
185 {
186 return MEMCACHED_FAILURE;
187 }
188 }
189 else if (not strcmp("curr_items", key))
190 {
191 errno= 0;
192 memc_stat->curr_items= strtoul(value, (char **)NULL, 10);
193 if (errno != 0)
194 {
195 return MEMCACHED_FAILURE;
196 }
197 }
198 else if (not strcmp("total_items", key))
199 {
200 errno= 0;
201 memc_stat->total_items= strtoul(value, (char **)NULL, 10);
202 if (errno != 0)
203 {
204 return MEMCACHED_FAILURE;
205 }
206 }
207 else if (not strcmp("bytes_read", key))
208 {
209 errno= 0;
210 memc_stat->bytes_read= strtoull(value, (char **)NULL, 10);
211 if (errno != 0)
212 {
213 return MEMCACHED_FAILURE;
214 }
215 }
216 else if (not strcmp("bytes_written", key))
217 {
218 errno= 0;
219 memc_stat->bytes_written= strtoull(value, (char **)NULL, 10);
220 if (errno != 0)
221 {
222 return MEMCACHED_FAILURE;
223 }
224 }
225 else if (not strcmp("bytes", key))
226 {
227 errno= 0;
228 memc_stat->bytes= strtoull(value, (char **)NULL, 10);
229 if (errno != 0)
230 {
231 return MEMCACHED_FAILURE;
232 }
233 }
234 else if (not strcmp("curr_connections", key))
235 {
236 errno= 0;
237 memc_stat->curr_connections= strtoull(value, (char **)NULL, 10);
238 if (errno != 0)
239 {
240 return MEMCACHED_FAILURE;
241 }
242 }
243 else if (not strcmp("total_connections", key))
244 {
245 errno= 0;
246 memc_stat->total_connections= strtoull(value, (char **)NULL, 10);
247 if (errno != 0)
248 {
249 return MEMCACHED_FAILURE;
250 }
251 }
252 else if (not strcmp("connection_structures", key))
253 {
254 errno= 0;
255 memc_stat->connection_structures= strtoul(value, (char **)NULL, 10);
256 if (errno != 0)
257 {
258 return MEMCACHED_FAILURE;
259 }
260 }
261 else if (not strcmp("cmd_get", key))
262 {
263 errno= 0;
264 memc_stat->cmd_get= strtoull(value, (char **)NULL, 10);
265 if (errno != 0)
266 {
267 return MEMCACHED_FAILURE;
268 }
269 }
270 else if (not strcmp("cmd_set", key))
271 {
272 errno= 0;
273 memc_stat->cmd_set= strtoull(value, (char **)NULL, 10);
274 if (errno != 0)
275 {
276 return MEMCACHED_FAILURE;
277 }
278 }
279 else if (not strcmp("get_hits", key))
280 {
281 errno= 0;
282 memc_stat->get_hits= strtoull(value, (char **)NULL, 10);
283 if (errno != 0)
284 {
285 return MEMCACHED_FAILURE;
286 }
287 }
288 else if (not strcmp("get_misses", key))
289 {
290 errno= 0;
291 memc_stat->get_misses= strtoull(value, (char **)NULL, 10);
292 if (errno != 0)
293 {
294 return MEMCACHED_FAILURE;
295 }
296 }
297 else if (not strcmp("evictions", key))
298 {
299 errno= 0;
300 memc_stat->evictions= strtoull(value, (char **)NULL, 10);
301 if (errno != 0)
302 {
303 return MEMCACHED_FAILURE;
304 }
305 }
306 else if (not strcmp("limit_maxbytes", key))
307 {
308 errno= 0;
309 memc_stat->limit_maxbytes= strtoull(value, (char **)NULL, 10);
310 if (errno != 0)
311 {
312 return MEMCACHED_FAILURE;
313 }
314 }
315 else if (not strcmp("threads", key))
316 {
317 errno= 0;
318 memc_stat->threads= strtoul(value, (char **)NULL, 10);
319 if (errno != 0)
320 {
321 return MEMCACHED_FAILURE;
322 }
323 }
324 else if ((strcmp("delete_misses", key) == 0 or /* New stats in the 1.3 beta */
325 strcmp("delete_hits", key) == 0 or /* Just swallow them for now.. */
326 strcmp("incr_misses", key) == 0 or
327 strcmp("incr_hits", key) == 0 or
328 strcmp("decr_misses", key) == 0 or
329 strcmp("decr_hits", key) == 0 or
330 strcmp("cas_misses", key) == 0 or
331 strcmp("cas_hits", key) == 0 or
332 strcmp("cas_badval", key) == 0 or
333 strcmp("cmd_flush", key) == 0 or
334 strcmp("accepting_conns", key) == 0 or
335 strcmp("listen_disabled_num", key) == 0 or
336 strcmp("conn_yields", key) == 0 or
337 strcmp("auth_cmds", key) == 0 or
338 strcmp("auth_errors", key) == 0 or
339 strcmp("reclaimed", key) == 0) == 0)
340 {
341 WATCHPOINT_STRING(key);
342 /* return MEMCACHED_UNKNOWN_STAT_KEY; */
343 return MEMCACHED_SUCCESS;
344 }
345
346 return MEMCACHED_SUCCESS;
347 }
348
349 char *memcached_stat_get_value(const memcached_st *, memcached_stat_st *memc_stat,
350 const char *key, memcached_return_t *error)
351 {
352 memcached_return_t not_used;
353 if (error == NULL)
354 {
355 error= &not_used;
356 }
357
358 if (memc_stat == NULL)
359 {
360 *error= MEMCACHED_INVALID_ARGUMENTS;
361 return NULL;
362 }
363
364 char buffer[SMALL_STRING_LEN];
365 int length;
366
367 *error= MEMCACHED_SUCCESS;
368
369 if (memcmp("pid", key, sizeof("pid") -1) == 0)
370 {
371 length= snprintf(buffer, SMALL_STRING_LEN,"%lld", (signed long long)memc_stat->pid);
372 }
373 else if (not memcmp("uptime", key, sizeof("uptime") -1))
374 {
375 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->uptime);
376 }
377 else if (not memcmp("time", key, sizeof("time") -1))
378 {
379 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->time);
380 }
381 else if (not memcmp("version", key, sizeof("version") -1))
382 {
383 length= snprintf(buffer, SMALL_STRING_LEN,"%s", memc_stat->version);
384 }
385 else if (not memcmp("pointer_size", key, sizeof("pointer_size") -1))
386 {
387 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->pointer_size);
388 }
389 else if (not memcmp("rusage_user", key, sizeof("rusage_user") -1))
390 {
391 length= snprintf(buffer, SMALL_STRING_LEN,"%lu.%lu", memc_stat->rusage_user_seconds, memc_stat->rusage_user_microseconds);
392 }
393 else if (not memcmp("rusage_system", key, sizeof("rusage_system") -1))
394 {
395 length= snprintf(buffer, SMALL_STRING_LEN,"%lu.%lu", memc_stat->rusage_system_seconds, memc_stat->rusage_system_microseconds);
396 }
397 else if (not memcmp("curr_items", key, sizeof("curr_items") -1))
398 {
399 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->curr_items);
400 }
401 else if (not memcmp("total_items", key, sizeof("total_items") -1))
402 {
403 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->total_items);
404 }
405 else if (not memcmp("curr_connections", key, sizeof("curr_connections") -1))
406 {
407 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->curr_connections);
408 }
409 else if (not memcmp("total_connections", key, sizeof("total_connections") -1))
410 {
411 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->total_connections);
412 }
413 else if (not memcmp("connection_structures", key, sizeof("connection_structures") -1))
414 {
415 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->connection_structures);
416 }
417 else if (not memcmp("cmd_get", key, sizeof("cmd_get") -1))
418 {
419 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_get);
420 }
421 else if (not memcmp("cmd_set", key, sizeof("cmd_set") -1))
422 {
423 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_set);
424 }
425 else if (not memcmp("get_hits", key, sizeof("get_hits") -1))
426 {
427 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_hits);
428 }
429 else if (not memcmp("get_misses", key, sizeof("get_misses") -1))
430 {
431 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_misses);
432 }
433 else if (not memcmp("evictions", key, sizeof("evictions") -1))
434 {
435 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->evictions);
436 }
437 else if (not memcmp("bytes_read", key, sizeof("bytes_read") -1))
438 {
439 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_read);
440 }
441 else if (not memcmp("bytes_written", key, sizeof("bytes_written") -1))
442 {
443 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_written);
444 }
445 else if (not memcmp("bytes", key, sizeof("bytes") -1))
446 {
447 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes);
448 }
449 else if (not memcmp("limit_maxbytes", key, sizeof("limit_maxbytes") -1))
450 {
451 length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->limit_maxbytes);
452 }
453 else if (not memcmp("threads", key, sizeof("threads") -1))
454 {
455 length= snprintf(buffer, SMALL_STRING_LEN,"%lu", memc_stat->threads);
456 }
457 else
458 {
459 *error= MEMCACHED_NOTFOUND;
460 return NULL;
461 }
462
463 if (length >= SMALL_STRING_LEN || length < 0)
464 {
465 *error= MEMCACHED_FAILURE;
466 return NULL;
467 }
468
469 // User is responsible for free() memory, so use malloc()
470 char *ret= static_cast<char *>(malloc(size_t(length +1)));
471 memcpy(ret, buffer, (size_t) length);
472 ret[length]= '\0';
473
474 return ret;
475 }
476
477 static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat,
478 const char *args,
479 const size_t args_length,
480 org::libmemcached::Instance* instance,
481 struct local_context *check)
482 {
483 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
484 protocol_binary_request_stats request= {}; // = {.bytes= {0}};
485
486 initialize_binary_request(instance, request.message.header);
487
488 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_STAT;
489 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
490
491 if (args_length)
492 {
493 request.message.header.request.keylen= htons(uint16_t(args_length));
494 request.message.header.request.bodylen= htonl(uint32_t( args_length));
495
496 libmemcached_io_vector_st vector[]=
497 {
498 { request.bytes, sizeof(request.bytes) },
499 { args, args_length }
500 };
501
502 if (memcached_vdo(instance, vector, 2, true) != MEMCACHED_SUCCESS)
503 {
504 memcached_io_reset(instance);
505 return MEMCACHED_WRITE_FAILURE;
506 }
507 }
508 else
509 {
510 libmemcached_io_vector_st vector[]=
511 {
512 { request.bytes, sizeof(request.bytes) }
513 };
514
515 if (memcached_vdo(instance, vector, 1, true) != MEMCACHED_SUCCESS)
516 {
517 memcached_io_reset(instance);
518 return MEMCACHED_WRITE_FAILURE;
519 }
520 }
521
522 memcached_server_response_decrement(instance);
523 while (1)
524 {
525 memcached_return_t rc= memcached_response(instance, buffer, sizeof(buffer), NULL);
526
527 if (rc == MEMCACHED_END)
528 {
529 break;
530 }
531
532 if (rc != MEMCACHED_SUCCESS)
533 {
534 memcached_io_reset(instance);
535 return rc;
536 }
537
538 if (check && check->func)
539 {
540 size_t key_length= strlen(buffer);
541
542 check->func(instance,
543 buffer, key_length,
544 buffer+key_length+1, strlen(buffer+key_length+1),
545 check->context);
546 }
547
548 if (memc_stat)
549 {
550 if ((set_data(memc_stat, buffer, buffer + strlen(buffer) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY)
551 {
552 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
553 WATCHPOINT_ASSERT(0);
554 }
555 }
556 }
557
558 /*
559 * memcached_response will decrement the counter, so I need to reset it..
560 * todo: look at this and try to find a better solution.
561 * */
562 instance->cursor_active_= 0;
563
564 return MEMCACHED_SUCCESS;
565 }
566
567 static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat,
568 const char *args,
569 const size_t args_length,
570 org::libmemcached::Instance* instance,
571 struct local_context *check)
572 {
573 libmemcached_io_vector_st vector[]=
574 {
575 { memcached_literal_param("stats ") },
576 { args, args_length },
577 { memcached_literal_param("\r\n") }
578 };
579
580 memcached_return_t rc= memcached_vdo(instance, vector, 3, true);
581 if (memcached_success(rc))
582 {
583 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
584 while ((rc= memcached_response(instance, buffer, sizeof(buffer), NULL)) == MEMCACHED_STAT)
585 {
586 char *string_ptr= buffer;
587 string_ptr+= 5; /* Move past STAT */
588
589 char *end_ptr;
590 for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++) {};
591 char *key= string_ptr;
592 key[size_t(end_ptr-string_ptr)]= 0;
593
594 string_ptr= end_ptr + 1;
595 for (end_ptr= string_ptr; !(isspace(*end_ptr)); end_ptr++) {};
596 char *value= string_ptr;
597 value[(size_t)(end_ptr -string_ptr)]= 0;
598 #if 0
599 bool check_bool= bool(check);
600 bool check_func_bool= bool(check) ? bool(check->func) : false;
601 fprintf(stderr, "%s:%d %s %s %d:%d\n", __FILE__, __LINE__, key, value, check_bool, check_func_bool);
602 #endif
603
604 if (check and check->func)
605 {
606 check->func(instance,
607 key, strlen(key),
608 value, strlen(value),
609 check->context);
610 }
611
612 if (memc_stat)
613 {
614 if((set_data(memc_stat, key, value)) == MEMCACHED_UNKNOWN_STAT_KEY)
615 {
616 WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
617 WATCHPOINT_ASSERT(0);
618 }
619 }
620 }
621 }
622
623 if (rc == MEMCACHED_ERROR)
624 {
625 return MEMCACHED_INVALID_ARGUMENTS;
626 }
627
628 if (rc == MEMCACHED_END)
629 {
630 return MEMCACHED_SUCCESS;
631 }
632
633 return rc;
634 }
635
636 memcached_stat_st *memcached_stat(memcached_st *shell, char *args, memcached_return_t *error)
637 {
638 Memcached* self= memcached2Memcached(shell);
639 memcached_return_t unused;
640 if (error == NULL)
641 {
642 error= &unused;
643 }
644
645 if (memcached_failed(*error= initialize_query(self, true)))
646 {
647 return NULL;
648 }
649
650 if (memcached_is_udp(self))
651 {
652 *error= memcached_set_error(*self, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
653 return NULL;
654 }
655
656 memcached_return_t rc;
657 size_t args_length= 0;
658 if (args)
659 {
660 args_length= strlen(args);
661 rc= memcached_validate_key_length(args_length, self->flags.binary_protocol);
662 if (memcached_failed(rc))
663 {
664 *error= memcached_set_error(*self, rc, MEMCACHED_AT);
665 return NULL;
666 }
667 }
668
669 WATCHPOINT_ASSERT(error);
670
671 memcached_stat_st *stats= libmemcached_xcalloc(self, memcached_server_count(self), memcached_stat_st);
672 if (stats == NULL)
673 {
674 *error= memcached_set_error(*self, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
675 return NULL;
676 }
677
678 WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS);
679 rc= MEMCACHED_SUCCESS;
680 for (uint32_t x= 0; x < memcached_server_count(self); x++)
681 {
682 memcached_stat_st* stat_instance= stats +x;
683
684 stat_instance->pid= -1;
685 stat_instance->root= self;
686
687 org::libmemcached::Instance* instance= memcached_instance_fetch(self, x);
688
689 memcached_return_t temp_return;
690 if (memcached_is_binary(self))
691 {
692 temp_return= binary_stats_fetch(stat_instance, args, args_length, instance, NULL);
693 }
694 else
695 {
696 temp_return= ascii_stats_fetch(stat_instance, args, args_length, instance, NULL);
697 }
698
699 // Special case where "args" is invalid
700 if (temp_return == MEMCACHED_INVALID_ARGUMENTS)
701 {
702 rc= MEMCACHED_INVALID_ARGUMENTS;
703 break;
704 }
705
706 if (memcached_failed(temp_return))
707 {
708 rc= MEMCACHED_SOME_ERRORS;
709 }
710 }
711
712 *error= rc;
713
714 return stats;
715 }
716
717 memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char *args,
718 const char *hostname, in_port_t port)
719 {
720 memcached_st memc;
721
722 memcached_stat_st unused_memc_stat;
723 if (memc_stat == NULL)
724 {
725 memc_stat= &unused_memc_stat;
726 }
727
728 memset(memc_stat, 0, sizeof(memcached_stat_st));
729
730 memcached_st *memc_ptr= memcached_create(&memc);
731 if (memc_ptr == NULL)
732 {
733 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
734 }
735
736 memcached_return_t rc;
737 if (memcached_failed(rc= memcached_server_add(&memc, hostname, port)))
738 {
739 memcached_free(&memc);
740 return rc;
741 }
742
743 if (memcached_success(rc= initialize_query(memc_ptr, true)))
744 {
745 size_t args_length= 0;
746 if (args)
747 {
748 args_length= strlen(args);
749 rc= memcached_validate_key_length(args_length, memc.flags.binary_protocol);
750 }
751
752 if (memcached_success(rc))
753 {
754 org::libmemcached::Instance* instance= memcached_instance_fetch(memc_ptr, 0);
755 if (memc.flags.binary_protocol)
756 {
757 rc= binary_stats_fetch(memc_stat, args, args_length, instance, NULL);
758 }
759 else
760 {
761 rc= ascii_stats_fetch(memc_stat, args, args_length, instance, NULL);
762 }
763 }
764 }
765
766 memcached_free(&memc);
767
768 return rc;
769 }
770
771 /*
772 We make a copy of the keys since at some point in the not so distant future
773 we will add support for "found" keys.
774 */
775 char ** memcached_stat_get_keys(memcached_st *shell,
776 memcached_stat_st *,
777 memcached_return_t *error)
778 {
779 Memcached* memc= memcached2Memcached(shell);
780 if (memc)
781 {
782 char **list= static_cast<char **>(libmemcached_malloc(memc, sizeof(memcached_stat_keys)));
783 if (list == NULL)
784 {
785 if (error)
786 {
787 *error= memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
788 }
789
790 return NULL;
791 }
792
793 memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys));
794
795 if (error)
796 {
797 *error= MEMCACHED_SUCCESS;
798 }
799
800 return list;
801 }
802
803 return NULL;
804 }
805
806 void memcached_stat_free(const memcached_st *, memcached_stat_st *memc_stat)
807 {
808 WATCHPOINT_ASSERT(memc_stat); // Be polite, but when debugging catch this as an error
809 if (memc_stat)
810 {
811 libmemcached_free(memc_stat->root, memc_stat);
812 }
813 }
814
815 static memcached_return_t call_stat_fn(memcached_st *memc,
816 org::libmemcached::Instance* instance,
817 void *context)
818 {
819 if (memc)
820 {
821 local_context *check= (struct local_context *)context;
822
823 if (memcached_is_binary(memc))
824 {
825 return binary_stats_fetch(NULL, check->args, check->args_length, instance, check);
826 }
827 else
828 {
829 return ascii_stats_fetch(NULL, check->args, check->args_length, instance, check);
830 }
831 }
832
833 return MEMCACHED_INVALID_ARGUMENTS;
834 }
835
836 memcached_return_t memcached_stat_execute(memcached_st *shell, const char *args, memcached_stat_fn func, void *context)
837 {
838 Memcached* memc= memcached2Memcached(shell);
839 if (memcached_fatal(memcached_version(memc)))
840 {
841 return memcached_last_error(memc);
842 }
843
844 local_context check(func, context, args, args ? strlen(args) : 0);
845
846 return memcached_server_execute(memc, call_stat_fn, (void *)&check);
847 }