PUTVAL command: Fix memory leaks and duplicate frees.
[collectd.git] / src / utils_cmd_putval.c
1 /**
2  * collectd - src/utils_cmd_putval.c
3  * Copyright (C) 2007-2009  Florian octo Forster
4  * Copyright (C) 2016       Sebastian tokkee Harl
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *   Florian octo Forster <octo at collectd.org>
26  *   Sebastian tokkee Harl <sh at tokkee.org>
27  **/
28
29 #include "collectd.h"
30
31 #include "common.h"
32 #include "plugin.h"
33
34 #include "utils_cmds.h"
35 #include "utils_cmd_putval.h"
36 #include "utils_parse_option.h"
37 #include "utils_cmd_putval.h"
38
39 /*
40  * private helper functions
41  */
42
43 static int set_option (value_list_t *vl, const char *key, const char *value)
44 {
45         if ((vl == NULL) || (key == NULL) || (value == NULL))
46                 return (-1);
47
48         if (strcasecmp ("interval", key) == 0)
49         {
50                 double tmp;
51                 char *endptr;
52
53                 endptr = NULL;
54                 errno = 0;
55                 tmp = strtod (value, &endptr);
56
57                 if ((errno == 0) && (endptr != NULL)
58                                 && (endptr != value) && (tmp > 0.0))
59                         vl->interval = DOUBLE_TO_CDTIME_T (tmp);
60         }
61         else
62                 return (1);
63
64         return (0);
65 } /* int set_option */
66
67 /*
68  * public API
69  */
70
71 cmd_status_t cmd_parse_putval (size_t argc, char **argv,
72                 cmd_putval_t *ret_putval, cmd_error_handler_t *err)
73 {
74         cmd_status_t result;
75
76         char *identifier;
77         char *hostname;
78         char *plugin;
79         char *plugin_instance;
80         char *type;
81         char *type_instance;
82         int   status;
83
84         char *identifier_copy;
85
86         const data_set_t *ds;
87         value_list_t vl = VALUE_LIST_INIT;
88         size_t i;
89
90         if (argc < 2)
91         {
92                 cmd_error (CMD_PARSE_ERROR, err,
93                                 "Missing identifier and/or value-list.");
94                 return (CMD_PARSE_ERROR);
95         }
96
97         identifier = argv[0];
98
99         /* parse_identifier() modifies its first argument, returning pointers into
100          * it; retain the old value for later. */
101         identifier_copy = sstrdup (identifier);
102
103         status = parse_identifier (identifier, &hostname,
104                         &plugin, &plugin_instance,
105                         &type, &type_instance);
106         if (status != 0)
107         {
108                 DEBUG ("cmd_handle_putval: Cannot parse identifier `%s'.",
109                                 identifier_copy);
110                 cmd_error (CMD_PARSE_ERROR, err, "Cannot parse identifier `%s'.",
111                                 identifier_copy);
112                 sfree (identifier_copy);
113                 return (CMD_PARSE_ERROR);
114         }
115
116         if ((strlen (hostname) >= sizeof (vl.host))
117                         || (strlen (plugin) >= sizeof (vl.plugin))
118                         || ((plugin_instance != NULL)
119                                 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
120                         || ((type_instance != NULL)
121                                 && (strlen (type_instance) >= sizeof (vl.type_instance))))
122         {
123                 cmd_error (CMD_PARSE_ERROR, err, "Identifier too long.");
124                 sfree (identifier_copy);
125                 return (CMD_PARSE_ERROR);
126         }
127
128         sstrncpy (vl.host, hostname, sizeof (vl.host));
129         sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
130         sstrncpy (vl.type, type, sizeof (vl.type));
131         if (plugin_instance != NULL)
132                 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
133         if (type_instance != NULL)
134                 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
135
136         ds = plugin_get_ds (type);
137         if (ds == NULL)
138         {
139                 cmd_error (CMD_PARSE_ERROR, err, "1 Type `%s' isn't defined.", type);
140                 sfree (identifier_copy);
141                 return (CMD_PARSE_ERROR);
142         }
143
144         hostname = NULL;
145         plugin = NULL; plugin_instance = NULL;
146         type = NULL;   type_instance = NULL;
147
148         vl.values_len = ds->ds_num;
149         vl.values = malloc (vl.values_len * sizeof (*vl.values));
150         if (vl.values == NULL)
151         {
152                 cmd_error (CMD_ERROR, err, "malloc failed.");
153                 sfree (identifier_copy);
154                 return (CMD_ERROR);
155         }
156
157         ret_putval->raw_identifier = identifier_copy;
158         if (ret_putval->raw_identifier == NULL)
159         {
160                 cmd_error (CMD_ERROR, err, "malloc failed.");
161                 cmd_destroy_putval (ret_putval);
162                 sfree (vl.values);
163                 return (CMD_ERROR);
164         }
165
166         /* All the remaining fields are part of the option list. */
167         result = CMD_OK;
168         for (i = 1; i < argc; ++i)
169         {
170                 value_list_t *tmp;
171
172                 char *key   = NULL;
173                 char *value = NULL;
174
175                 status = cmd_parse_option (argv[i], &key, &value, err);
176                 if (status == CMD_OK)
177                 {
178                         assert (key != NULL);
179                         assert (value != NULL);
180                         set_option (&vl, key, value);
181                         continue;
182                 }
183                 else if (status != CMD_NO_OPTION)
184                 {
185                         /* parse_option failed, buffer has been modified.
186                          * => we need to abort */
187                         result = status;
188                         break;
189                 }
190                 /* else: cmd_parse_option did not find an option; treat this as a
191                  * value list. */
192
193                 status = parse_values (argv[i], &vl, ds);
194                 if (status != 0)
195                 {
196                         cmd_error (CMD_PARSE_ERROR, err, "Parsing the values string failed.");
197                         result = CMD_PARSE_ERROR;
198                         break;
199                 }
200
201                 tmp = (value_list_t *) realloc (ret_putval->vl,
202                                 (ret_putval->vl_num + 1) * sizeof(*ret_putval->vl));
203                 if (tmp == NULL)
204                 {
205                         cmd_error (CMD_ERROR, err, "realloc failed.");
206                         cmd_destroy_putval (ret_putval);
207                         result = CMD_ERROR;
208                         break;
209                 }
210
211                 ret_putval->vl = tmp;
212                 ret_putval->vl_num++;
213                 memcpy (&ret_putval->vl[ret_putval->vl_num - 1], &vl, sizeof (vl));
214         } /* while (*buffer != 0) */
215         /* Done parsing the options. */
216
217         if (result != CMD_OK)
218         {
219                 if (ret_putval->vl_num == 0)
220                         sfree (vl.values);
221                 cmd_destroy_putval (ret_putval);
222         }
223
224         return (result);
225 } /* cmd_status_t cmd_parse_putval */
226
227 void cmd_destroy_putval (cmd_putval_t *putval)
228 {
229         size_t i;
230
231         if (putval == NULL)
232                 return;
233
234         sfree (putval->raw_identifier);
235
236         for (i = 0; i < putval->vl_num; ++i)
237         {
238                 if (i == 0) /* values is shared between all entries */
239                         sfree (putval->vl[i].values);
240                 meta_data_destroy (putval->vl[i].meta);
241                 putval->vl[i].meta = NULL;
242         }
243         sfree (putval->vl);
244         putval->vl = NULL;
245         putval->vl_num = 0;
246 } /* void cmd_destroy_putval */
247
248 cmd_status_t cmd_handle_putval (FILE *fh, char *buffer)
249 {
250         cmd_error_handler_t err = { cmd_error_fh, fh };
251         cmd_t cmd;
252         size_t i;
253
254         int status;
255
256         DEBUG ("utils_cmd_putval: cmd_handle_putval (fh = %p, buffer = %s);",
257                         (void *) fh, buffer);
258
259         if ((status = cmd_parse (buffer, &cmd, &err)) != CMD_OK)
260                 return (status);
261         if (cmd.type != CMD_PUTVAL)
262         {
263                 cmd_error (CMD_UNKNOWN_COMMAND, &err, "Unexpected command: `%s'.",
264                                 CMD_TO_STRING (cmd.type));
265                 cmd_destroy (&cmd);
266                 return (CMD_UNKNOWN_COMMAND);
267         }
268
269         for (i = 0; i < cmd.cmd.putval.vl_num; ++i)
270                 plugin_dispatch_values (&cmd.cmd.putval.vl[i]);
271
272         if (fh != stdout)
273                 cmd_error (CMD_OK, &err, "Success: %i %s been dispatched.",
274                                 (int)cmd.cmd.putval.vl_num,
275                                 (cmd.cmd.putval.vl_num == 1) ? "value has" : "values have");
276
277         cmd_destroy (&cmd);
278         return (CMD_OK);
279 } /* int cmd_handle_putval */
280
281 int cmd_create_putval (char *ret, size_t ret_len, /* {{{ */
282         const data_set_t *ds, const value_list_t *vl)
283 {
284         char buffer_ident[6 * DATA_MAX_NAME_LEN];
285         char buffer_values[1024];
286         int status;
287
288         status = FORMAT_VL (buffer_ident, sizeof (buffer_ident), vl);
289         if (status != 0)
290                 return (status);
291         escape_string (buffer_ident, sizeof (buffer_ident));
292
293         status = format_values (buffer_values, sizeof (buffer_values),
294                         ds, vl, /* store rates = */ 0);
295         if (status != 0)
296                 return (status);
297         escape_string (buffer_values, sizeof (buffer_values));
298
299         ssnprintf (ret, ret_len,
300                         "PUTVAL %s interval=%.3f %s",
301                         buffer_ident,
302                         (vl->interval > 0)
303                         ? CDTIME_T_TO_DOUBLE (vl->interval)
304                         : CDTIME_T_TO_DOUBLE (plugin_get_interval ()),
305                         buffer_values);
306
307         return (0);
308 } /* }}} int cmd_create_putval */