0a4cba3f80002b766717724c9651ec2afa4116e8
1 /**
2 * collectd - src/common.c
3 * Copyright (C) 2005 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/
6 * or modify it under the terms of the GNU General Public Li-
7 * cence as published by the Free Software Foundation; either
8 * version 2 of the Licence, or any later version.
9 *
10 * This program is distributed in the hope that it will be use-
11 * ful, but WITHOUT ANY WARRANTY; without even the implied war-
12 * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public Licence for more details.
14 *
15 * You should have received a copy of the GNU General Public
16 * Licence along with this program; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
18 * USA.
19 *
20 * Authors:
21 * Florian octo Forster <octo at verplant.org>
22 * Niki W. Waibel <niki.waibel@gmx.net>
23 **/
25 #include "common.h"
26 #include "utils_debug.h"
28 #ifdef HAVE_LIBKSTAT
29 extern kstat_ctl_t *kc;
30 #endif
32 #ifdef HAVE_LIBRRD
33 static char *rra_def[] =
34 {
35 "RRA:AVERAGE:0.0:1:1500",
36 "RRA:AVERAGE:0.2:6:1500",
37 "RRA:AVERAGE:0.1:180:1680",
38 "RRA:AVERAGE:0.1:2160:1520",
39 "RRA:MIN:0.0:1:1500",
40 "RRA:MIN:0.2:6:1500",
41 "RRA:MIN:0.1:180:1680",
42 "RRA:MIN:0.1:2160:1520",
43 "RRA:MAX:0.0:1:1500",
44 "RRA:MAX:0.2:6:1500",
45 "RRA:MAX:0.1:180:1680",
46 "RRA:MAX:0.1:2160:1520",
47 NULL
48 };
49 static int rra_num = 12;
50 #endif /* HAVE_LIBRRD */
52 void sstrncpy (char *d, const char *s, int len)
53 {
54 strncpy (d, s, len);
55 d[len - 1] = '\0';
56 }
58 char *sstrdup (const char *s)
59 {
60 char *r;
62 if (s == NULL)
63 return (NULL);
65 if((r = strdup (s)) == NULL)
66 {
67 DBG ("Not enough memory.");
68 exit(3);
69 }
71 return (r);
72 }
74 void *smalloc (size_t size)
75 {
76 void *r;
78 if ((r = malloc (size)) == NULL)
79 {
80 DBG("Not enough memory.");
81 exit(3);
82 }
84 return r;
85 }
87 #if 0
88 void sfree (void **ptr)
89 {
90 if (ptr == NULL)
91 return;
93 if (*ptr != NULL)
94 free (*ptr);
96 *ptr = NULL;
97 }
98 #endif
100 int strsplit (char *string, char **fields, size_t size)
101 {
102 size_t i;
103 char *ptr;
105 i = 0;
106 ptr = string;
107 while ((fields[i] = strtok (ptr, " \t")) != NULL)
108 {
109 ptr = NULL;
110 i++;
112 if (i >= size)
113 break;
114 }
116 return (i);
117 }
119 int strjoin (char *dst, size_t dst_len,
120 char **fields, size_t fields_num,
121 const char *sep)
122 {
123 int field_len;
124 int sep_len;
125 int i;
127 memset (dst, '\0', dst_len);
129 if (fields_num <= 0)
130 return (-1);
132 sep_len = 0;
133 if (sep != NULL)
134 sep_len = strlen (sep);
136 for (i = 0; i < fields_num; i++)
137 {
138 if ((i > 0) && (sep_len > 0))
139 {
140 if (dst_len <= sep_len)
141 return (-1);
143 strncat (dst, sep, dst_len);
144 dst_len -= sep_len;
145 }
147 field_len = strlen (fields[i]);
149 if (dst_len <= field_len)
150 return (-1);
152 strncat (dst, fields[i], dst_len);
153 dst_len -= field_len;
154 }
156 return (strlen (dst));
157 }
159 int escape_slashes (char *buf, int buf_len)
160 {
161 int i;
163 if (strcmp (buf, "/") == 0)
164 {
165 if (buf_len < 5)
166 return (-1);
168 strncpy (buf, "root", buf_len);
169 return (0);
170 }
172 /* Move one to the left */
173 memmove (buf, buf + 1, buf_len - 1);
175 for (i = 0; i < buf_len - 1; i++)
176 {
177 if (buf[i] == '\0')
178 break;
179 else if (buf[i] == '/')
180 buf[i] = '_';
181 }
182 buf[i] = '\0';
184 return (0);
185 }
187 #ifdef HAVE_LIBRRD
188 int check_create_dir (const char *file_orig)
189 {
190 struct stat statbuf;
192 char file_copy[512];
193 char dir[512];
194 int dir_len = 512;
195 char *fields[16];
196 int fields_num;
197 char *ptr;
198 int last_is_file = 1;
199 int len;
200 int i;
202 /*
203 * Sanity checks first
204 */
205 if (file_orig == NULL)
206 return (-1);
208 if ((len = strlen (file_orig)) < 1)
209 return (-1);
210 else if (len >= 512)
211 return (-1);
213 /*
214 * If `file_orig' ends in a slash the last component is a directory,
215 * otherwise it's a file. Act accordingly..
216 */
217 if (file_orig[len - 1] == '/')
218 last_is_file = 0;
220 /*
221 * Create a copy for `strtok' to destroy
222 */
223 strncpy (file_copy, file_orig, 512);
224 file_copy[511] = '\0';
226 /*
227 * Break into components. This will eat up several slashes in a row and
228 * remove leading and trailing slashes..
229 */
230 ptr = file_copy;
231 fields_num = 0;
232 while ((fields[fields_num] = strtok (ptr, "/")) != NULL)
233 {
234 ptr = NULL;
235 fields_num++;
237 if (fields_num >= 16)
238 break;
239 }
241 /*
242 * For each component, do..
243 */
244 for (i = 0; i < (fields_num - last_is_file); i++)
245 {
246 /*
247 * Do not create directories that start with a dot. This
248 * prevents `../../' attacks and other likely malicious
249 * behavior.
250 */
251 if (fields[i][0] == '.')
252 {
253 syslog (LOG_ERR, "Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
254 return (-2);
255 }
257 /*
258 * Join the components together again
259 */
260 if (strjoin (dir, dir_len, fields, i + 1, "/") < 0)
261 {
262 syslog (LOG_ERR, "strjoin failed: `%s', component #%i", file_orig, i);
263 return (-1);
264 }
266 if (stat (dir, &statbuf) == -1)
267 {
268 if (errno == ENOENT)
269 {
270 if (mkdir (dir, 0755) == -1)
271 {
272 syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno));
273 return (-1);
274 }
275 }
276 else
277 {
278 syslog (LOG_ERR, "stat (%s): %s", dir, strerror (errno));
279 return (-1);
280 }
281 }
282 else if (!S_ISDIR (statbuf.st_mode))
283 {
284 syslog (LOG_ERR, "stat (%s): Not a directory!", dir);
285 return (-1);
286 }
287 }
289 return (0);
290 }
292 int rrd_create_file (char *filename, char **ds_def, int ds_num)
293 {
294 char **argv;
295 int argc;
296 int i, j;
297 int status = 0;
299 if (check_create_dir (filename))
300 return (-1);
302 argc = ds_num + rra_num + 4;
304 if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
305 {
306 syslog (LOG_ERR, "rrd_create failed: %s", strerror (errno));
307 return (-1);
308 }
310 argv[0] = "create";
311 argv[1] = filename;
312 argv[2] = "-s";
313 argv[3] = COLLECTD_STEP;
315 j = 4;
316 for (i = 0; i < ds_num; i++)
317 argv[j++] = ds_def[i];
318 for (i = 0; i < rra_num; i++)
319 argv[j++] = rra_def[i];
320 argv[j] = NULL;
322 optind = 0; /* bug in librrd? */
323 rrd_clear_error ();
324 if (rrd_create (argc, argv) == -1)
325 {
326 syslog (LOG_ERR, "rrd_create failed: %s: %s", filename, rrd_get_error ());
327 status = -1;
328 }
330 free (argv);
332 return (status);
333 }
334 #endif /* HAVE_LIBRRD */
336 int rrd_update_file (char *host, char *file, char *values,
337 char **ds_def, int ds_num)
338 {
339 #ifdef HAVE_LIBRRD
340 struct stat statbuf;
341 char full_file[1024];
342 char *argv[4] = { "update", full_file, values, NULL };
344 /* host == NULL => local mode */
345 if (host != NULL)
346 {
347 if (snprintf (full_file, 1024, "%s/%s", host, file) >= 1024)
348 return (-1);
349 }
350 else
351 {
352 if (snprintf (full_file, 1024, "%s", file) >= 1024)
353 return (-1);
354 }
356 if (stat (full_file, &statbuf) == -1)
357 {
358 if (errno == ENOENT)
359 {
360 if (rrd_create_file (full_file, ds_def, ds_num))
361 return (-1);
362 }
363 else
364 {
365 syslog (LOG_ERR, "stat %s: %s", full_file, strerror (errno));
366 return (-1);
367 }
368 }
369 else if (!S_ISREG (statbuf.st_mode))
370 {
371 syslog (LOG_ERR, "stat %s: Not a regular file!", full_file);
372 return (-1);
373 }
375 optind = 0; /* bug in librrd? */
376 rrd_clear_error ();
377 if (rrd_update (3, argv) == -1)
378 {
379 syslog (LOG_WARNING, "rrd_update failed: %s: %s", full_file, rrd_get_error ());
380 return (-1);
381 }
382 #endif /* HAVE_LIBRRD */
384 return (0);
385 }
387 #ifdef HAVE_LIBKSTAT
388 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
389 {
390 char ident[128];
392 if (kc == NULL)
393 return (-1);
395 snprintf (ident, 128, "%s,%i,%s", module, instance, name);
396 ident[127] = '\0';
398 if (*ksp_ptr == NULL)
399 {
400 if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL)
401 {
402 syslog (LOG_ERR, "Cound not find kstat %s", ident);
403 return (-1);
404 }
406 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
407 {
408 syslog (LOG_WARNING, "kstat %s has wrong type", ident);
409 *ksp_ptr = NULL;
410 return (-1);
411 }
412 }
414 #ifdef assert
415 assert (*ksp_ptr != NULL);
416 assert ((*ksp_ptr)->ks_type == KSTAT_TYPE_NAMED);
417 #endif
419 if (kstat_read (kc, *ksp_ptr, NULL) == -1)
420 {
421 syslog (LOG_WARNING, "kstat %s could not be read", ident);
422 return (-1);
423 }
425 if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED)
426 {
427 syslog (LOG_WARNING, "kstat %s has wrong type", ident);
428 return (-1);
429 }
431 return (0);
432 }
434 long long get_kstat_value (kstat_t *ksp, char *name)
435 {
436 kstat_named_t *kn;
437 long long retval = -1LL;
439 #ifdef assert
440 assert (ksp != NULL);
441 assert (ksp->ks_type == KSTAT_TYPE_NAMED);
442 #else
443 if (ksp == NULL)
444 {
445 fprintf (stderr, "ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__);
446 return (-1LL);
447 }
448 else if (ksp->ks_type != KSTAT_TYPE_NAMED)
449 {
450 fprintf (stderr, "ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__);
451 return (-1LL);
452 }
453 #endif
455 if ((kn = (kstat_named_t *) kstat_data_lookup (ksp, name)) == NULL)
456 return (retval);
458 if (kn->data_type == KSTAT_DATA_INT32)
459 retval = (long long) kn->value.i32;
460 else if (kn->data_type == KSTAT_DATA_UINT32)
461 retval = (long long) kn->value.ui32;
462 else if (kn->data_type == KSTAT_DATA_INT64)
463 retval = (long long) kn->value.i64; /* According to ANSI C99 `long long' must hold at least 64 bits */
464 else if (kn->data_type == KSTAT_DATA_UINT64)
465 retval = (long long) kn->value.ui64; /* XXX: Might overflow! */
466 else
467 syslog (LOG_WARNING, "get_kstat_value: Not a numeric value: %s", name);
469 return (retval);
470 }
471 #endif /* HAVE_LIBKSTAT */