Bind plugin: plug a few leaks
[collectd.git] / src / utils_cmd_listval.c
1 /**
2  * collectd - src/utils_cmd_listval.c
3  * Copyright (C) 2008       Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #include "utils_cache.h"
33 #include "utils_cmd_listval.h"
34 #include "utils_parse_option.h"
35
36 #define free_everything_and_return(status)                                     \
37   do {                                                                         \
38     for (size_t j = 0; j < number; j++) {                                      \
39       sfree(names[j]);                                                         \
40       names[j] = NULL;                                                         \
41     }                                                                          \
42     sfree(names);                                                              \
43     sfree(times);                                                              \
44     return (status);                                                           \
45   } while (0)
46
47 #define print_to_socket(fh, ...)                                               \
48   do {                                                                         \
49     if (fprintf(fh, __VA_ARGS__) < 0) {                                        \
50       char errbuf[1024];                                                       \
51       WARNING("handle_listval: failed to write to socket #%i: %s", fileno(fh), \
52               sstrerror(errno, errbuf, sizeof(errbuf)));                       \
53       free_everything_and_return(-1);                                          \
54     }                                                                          \
55     fflush(fh);                                                                \
56   } while (0)
57
58 int handle_listval(FILE *fh, char *buffer) {
59   char *command;
60   char **names = NULL;
61   cdtime_t *times = NULL;
62   size_t number = 0;
63   int status;
64
65   DEBUG("utils_cmd_listval: handle_listval (fh = %p, buffer = %s);", (void *)fh,
66         buffer);
67
68   command = NULL;
69   status = parse_string(&buffer, &command);
70   if (status != 0) {
71     print_to_socket(fh, "-1 Cannot parse command.\n");
72     free_everything_and_return(-1);
73   }
74   assert(command != NULL);
75
76   if (strcasecmp("LISTVAL", command) != 0) {
77     print_to_socket(fh, "-1 Unexpected command: `%s'.\n", command);
78     free_everything_and_return(-1);
79   }
80
81   if (*buffer != 0) {
82     print_to_socket(fh, "-1 Garbage after end of command: %s\n", buffer);
83     free_everything_and_return(-1);
84   }
85
86   status = uc_get_names(&names, &times, &number);
87   if (status != 0) {
88     DEBUG("command listval: uc_get_names failed with status %i", status);
89     print_to_socket(fh, "-1 uc_get_names failed.\n");
90     free_everything_and_return(-1);
91   }
92
93   print_to_socket(fh, "%i Value%s found\n", (int)number,
94                   (number == 1) ? "" : "s");
95   for (size_t i = 0; i < number; i++)
96     print_to_socket(fh, "%.3f %s\n", CDTIME_T_TO_DOUBLE(times[i]), names[i]);
97
98   free_everything_and_return(0);
99 } /* int handle_listval */
100
101 /* vim: set sw=2 sts=2 ts=8 : */