1 /**
2 * libcollectdclient - src/libcollectdclient/client.c
3 * Copyright (C) 2008 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #if !defined(__GNUC__) || !__GNUC__
27 # define __attribute__(x) /**/
28 #endif
30 #include "lcc_features.h"
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 #include <string.h>
39 #include <assert.h>
40 #include <errno.h>
41 #include <math.h>
42 #include <netdb.h>
44 #include "client.h"
46 /* NI_MAXHOST has been obsoleted by RFC 3493 which is a reason for SunOS 5.11
47 * to no longer define it. We'll use the old, RFC 2553 value here. */
48 #ifndef NI_MAXHOST
49 # define NI_MAXHOST 1025
50 #endif
52 /* OpenBSD doesn't have EPROTO, FreeBSD doesn't have EILSEQ. Oh what joy! */
53 #ifndef EILSEQ
54 # ifdef EPROTO
55 # define EILSEQ EPROTO
56 # else
57 # define EILSEQ EINVAL
58 # endif
59 #endif
61 /* Secure/static macros. They work like `strcpy' and `strcat', but assure null
62 * termination. They work for static buffers only, because they use `sizeof'.
63 * The `SSTRCATF' combines the functionality of `snprintf' and `strcat' which
64 * is very useful to add formatted stuff to the end of a buffer. */
65 #define SSTRCPY(d,s) do { \
66 strncpy ((d), (s), sizeof (d)); \
67 (d)[sizeof (d) - 1] = 0; \
68 } while (0)
70 #define SSTRCAT(d,s) do { \
71 size_t _l = strlen (d); \
72 strncpy ((d) + _l, (s), sizeof (d) - _l); \
73 (d)[sizeof (d) - 1] = 0; \
74 } while (0)
76 #define SSTRCATF(d, ...) do { \
77 char _b[sizeof (d)]; \
78 snprintf (_b, sizeof (_b), __VA_ARGS__); \
79 _b[sizeof (_b) - 1] = 0; \
80 SSTRCAT ((d), _b); \
81 } while (0)
84 #define LCC_SET_ERRSTR(c, ...) do { \
85 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
86 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
87 } while (0)
89 #if COLLECT_DEBUG
90 # define LCC_DEBUG(...) printf (__VA_ARGS__)
91 #else
92 # define LCC_DEBUG(...) /**/
93 #endif
95 /*
96 * Types
97 */
98 struct lcc_connection_s
99 {
100 FILE *fh;
101 char errbuf[1024];
102 };
104 struct lcc_response_s
105 {
106 int status;
107 char message[1024];
108 char **lines;
109 size_t lines_num;
110 };
111 typedef struct lcc_response_s lcc_response_t;
113 /*
114 * Private functions
115 */
116 /* Even though Posix requires "strerror_r" to return an "int",
117 * some systems (e.g. the GNU libc) return a "char *" _and_
118 * ignore the second argument ... -tokkee */
119 static char *sstrerror (int errnum, char *buf, size_t buflen)
120 {
121 buf[0] = 0;
123 #if !HAVE_STRERROR_R
124 snprintf (buf, buflen, "Error #%i; strerror_r is not available.", errnum);
125 /* #endif !HAVE_STRERROR_R */
127 #elif STRERROR_R_CHAR_P
128 {
129 char *temp;
130 temp = strerror_r (errnum, buf, buflen);
131 if (buf[0] == 0)
132 {
133 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
134 strncpy (buf, temp, buflen);
135 else
136 strncpy (buf, "strerror_r did not return "
137 "an error message", buflen);
138 }
139 }
140 /* #endif STRERROR_R_CHAR_P */
142 #else
143 if (strerror_r (errnum, buf, buflen) != 0)
144 {
145 snprintf (buf, buflen, "Error #%i; "
146 "Additionally, strerror_r failed.",
147 errnum);
148 }
149 #endif /* STRERROR_R_CHAR_P */
151 buf[buflen - 1] = 0;
153 return (buf);
154 } /* char *sstrerror */
156 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
157 {
158 if (c == NULL)
159 return (-1);
161 sstrerror (err, c->errbuf, sizeof (c->errbuf));
162 c->errbuf[sizeof (c->errbuf) - 1] = 0;
164 return (0);
165 } /* }}} int lcc_set_errno */
167 static char *lcc_strescape (char *dest, const char *src, size_t dest_size) /* {{{ */
168 {
169 size_t dest_pos;
170 size_t src_pos;
172 if ((dest == NULL) || (src == NULL))
173 return (NULL);
175 dest_pos = 0;
176 src_pos = 0;
178 assert (dest_size >= 3);
180 dest[dest_pos] = '"';
181 dest_pos++;
183 while (42)
184 {
185 if ((dest_pos == (dest_size - 2))
186 || (src[src_pos] == 0))
187 break;
189 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
190 {
191 /* Check if there is enough space for both characters.. */
192 if (dest_pos == (dest_size - 3))
193 break;
195 dest[dest_pos] = '\\';
196 dest_pos++;
197 }
199 dest[dest_pos] = src[src_pos];
200 dest_pos++;
201 src_pos++;
202 }
204 assert (dest_pos <= (dest_size - 2));
206 dest[dest_pos] = '"';
207 dest_pos++;
209 dest[dest_pos] = 0;
210 dest_pos++;
211 src_pos++;
213 return (dest);
214 } /* }}} char *lcc_strescape */
216 /* lcc_chomp: Removes all control-characters at the end of a string. */
217 static void lcc_chomp (char *str) /* {{{ */
218 {
219 size_t str_len;
221 str_len = strlen (str);
222 while (str_len > 0)
223 {
224 if (str[str_len - 1] >= 32)
225 break;
226 str[str_len - 1] = 0;
227 str_len--;
228 }
229 } /* }}} void lcc_chomp */
231 static int lcc_identifier_cmp (const void *a, const void *b)
232 {
233 const lcc_identifier_t *ident_a, *ident_b;
235 int status;
237 ident_a = a;
238 ident_b = b;
240 status = strcasecmp (ident_a->host, ident_b->host);
241 if (status != 0)
242 return (status);
244 status = strcmp (ident_a->plugin, ident_b->plugin);
245 if (status != 0)
246 return (status);
248 if ((*ident_a->plugin_instance != '\0') || (*ident_b->plugin_instance != '\0'))
249 {
250 if (*ident_a->plugin_instance == '\0')
251 return (-1);
252 else if (*ident_b->plugin_instance == '\0')
253 return (1);
255 status = strcmp (ident_a->plugin_instance, ident_b->plugin_instance);
256 if (status != 0)
257 return (status);
258 }
260 status = strcmp (ident_a->type, ident_b->type);
261 if (status != 0)
262 return (status);
264 if ((*ident_a->type_instance != '\0') || (*ident_b->type_instance != '\0'))
265 {
266 if (*ident_a->type_instance == '\0')
267 return (-1);
268 else if (*ident_b->type_instance == '\0')
269 return (1);
271 status = strcmp (ident_a->type_instance, ident_b->type_instance);
272 if (status != 0)
273 return (status);
274 }
275 return (0);
276 } /* }}} int lcc_identifier_cmp */
278 static void lcc_response_free (lcc_response_t *res) /* {{{ */
279 {
280 size_t i;
282 if (res == NULL)
283 return;
285 for (i = 0; i < res->lines_num; i++)
286 free (res->lines[i]);
287 free (res->lines);
288 res->lines = NULL;
289 } /* }}} void lcc_response_free */
291 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
292 {
293 int status;
295 LCC_DEBUG ("send: --> %s\n", command);
297 status = fprintf (c->fh, "%s\r\n", command);
298 if (status < 0)
299 {
300 lcc_set_errno (c, errno);
301 return (-1);
302 }
304 return (0);
305 } /* }}} int lcc_send */
307 static int lcc_receive (lcc_connection_t *c, /* {{{ */
308 lcc_response_t *ret_res)
309 {
310 lcc_response_t res;
311 char *ptr;
312 char buffer[4096];
313 size_t i;
315 memset (&res, 0, sizeof (res));
317 /* Read the first line, containing the status and a message */
318 ptr = fgets (buffer, sizeof (buffer), c->fh);
319 if (ptr == NULL)
320 {
321 lcc_set_errno (c, errno);
322 return (-1);
323 }
324 lcc_chomp (buffer);
325 LCC_DEBUG ("receive: <-- %s\n", buffer);
327 /* Convert the leading status to an integer and make `ptr' to point to the
328 * beginning of the message. */
329 ptr = NULL;
330 errno = 0;
331 res.status = strtol (buffer, &ptr, 0);
332 if ((errno != 0) || (ptr == &buffer[0]))
333 {
334 lcc_set_errno (c, errno);
335 return (-1);
336 }
338 /* Skip white spaces after the status number */
339 while ((*ptr == ' ') || (*ptr == '\t'))
340 ptr++;
342 /* Now copy the message. */
343 strncpy (res.message, ptr, sizeof (res.message));
344 res.message[sizeof (res.message) - 1] = 0;
346 /* Error or no lines follow: We're done. */
347 if (res.status <= 0)
348 {
349 memcpy (ret_res, &res, sizeof (res));
350 return (0);
351 }
353 /* Allocate space for the char-pointers */
354 res.lines_num = (size_t) res.status;
355 res.status = 0;
356 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
357 if (res.lines == NULL)
358 {
359 lcc_set_errno (c, ENOMEM);
360 return (-1);
361 }
363 /* Now receive all the lines */
364 for (i = 0; i < res.lines_num; i++)
365 {
366 ptr = fgets (buffer, sizeof (buffer), c->fh);
367 if (ptr == NULL)
368 {
369 lcc_set_errno (c, errno);
370 break;
371 }
372 lcc_chomp (buffer);
373 LCC_DEBUG ("receive: <-- %s\n", buffer);
375 res.lines[i] = strdup (buffer);
376 if (res.lines[i] == NULL)
377 {
378 lcc_set_errno (c, ENOMEM);
379 break;
380 }
381 }
383 /* Check if the for-loop exited with an error. */
384 if (i < res.lines_num)
385 {
386 while (i > 0)
387 {
388 i--;
389 free (res.lines[i]);
390 }
391 free (res.lines);
392 return (-1);
393 }
395 memcpy (ret_res, &res, sizeof (res));
396 return (0);
397 } /* }}} int lcc_receive */
399 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
400 const char *command, lcc_response_t *ret_res)
401 {
402 lcc_response_t res;
403 int status;
405 if (c->fh == NULL)
406 {
407 lcc_set_errno (c, EBADF);
408 return (-1);
409 }
411 status = lcc_send (c, command);
412 if (status != 0)
413 return (status);
415 memset (&res, 0, sizeof (res));
416 status = lcc_receive (c, &res);
417 if (status == 0)
418 memcpy (ret_res, &res, sizeof (*ret_res));
420 return (status);
421 } /* }}} int lcc_sendreceive */
423 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
424 {
425 struct sockaddr_un sa;
426 int fd;
427 int status;
429 assert (c != NULL);
430 assert (c->fh == NULL);
431 assert (path != NULL);
433 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
434 * others). */
435 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
436 if (fd < 0)
437 {
438 lcc_set_errno (c, errno);
439 return (-1);
440 }
442 memset (&sa, 0, sizeof (sa));
443 sa.sun_family = AF_UNIX;
444 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
446 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
447 if (status != 0)
448 {
449 lcc_set_errno (c, errno);
450 close (fd);
451 return (-1);
452 }
454 c->fh = fdopen (fd, "r+");
455 if (c->fh == NULL)
456 {
457 lcc_set_errno (c, errno);
458 close (fd);
459 return (-1);
460 }
462 return (0);
463 } /* }}} int lcc_open_unixsocket */
465 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
466 const char *addr_orig)
467 {
468 struct addrinfo ai_hints;
469 struct addrinfo *ai_res;
470 struct addrinfo *ai_ptr;
471 char addr_copy[NI_MAXHOST];
472 char *addr;
473 char *port;
474 int fd;
475 int status;
477 assert (c != NULL);
478 assert (c->fh == NULL);
479 assert (addr_orig != NULL);
481 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
482 addr_copy[sizeof(addr_copy) - 1] = '\0';
483 addr = addr_copy;
485 memset (&ai_hints, 0, sizeof (ai_hints));
486 ai_hints.ai_flags = 0;
487 #ifdef AI_ADDRCONFIG
488 ai_hints.ai_flags |= AI_ADDRCONFIG;
489 #endif
490 ai_hints.ai_family = AF_UNSPEC;
491 ai_hints.ai_socktype = SOCK_STREAM;
493 port = NULL;
494 if (*addr == '[') /* IPv6+port format */
495 {
496 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
497 addr++;
499 port = strchr (addr, ']');
500 if (port == NULL)
501 {
502 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
503 return (-1);
504 }
505 *port = 0;
506 port++;
508 if (*port == ':')
509 port++;
510 else if (*port == 0)
511 port = NULL;
512 else
513 {
514 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
515 return (-1);
516 }
517 } /* if (*addr = ']') */
518 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
519 {
520 port = strrchr (addr, ':');
521 if (port != NULL)
522 {
523 *port = 0;
524 port++;
525 }
526 }
528 ai_res = NULL;
529 status = getaddrinfo (addr,
530 port == NULL ? LCC_DEFAULT_PORT : port,
531 &ai_hints, &ai_res);
532 if (status != 0)
533 {
534 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
535 return (-1);
536 }
538 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
539 {
540 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
541 if (fd < 0)
542 {
543 status = errno;
544 fd = -1;
545 continue;
546 }
548 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
549 if (status != 0)
550 {
551 status = errno;
552 close (fd);
553 fd = -1;
554 continue;
555 }
557 c->fh = fdopen (fd, "r+");
558 if (c->fh == NULL)
559 {
560 status = errno;
561 close (fd);
562 fd = -1;
563 continue;
564 }
566 assert (status == 0);
567 break;
568 } /* for (ai_ptr) */
570 if (status != 0)
571 {
572 lcc_set_errno (c, status);
573 return (-1);
574 }
576 return (0);
577 } /* }}} int lcc_open_netsocket */
579 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
580 {
581 int status = 0;
583 if (addr == NULL)
584 return (-1);
586 assert (c != NULL);
587 assert (c->fh == NULL);
588 assert (addr != NULL);
590 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
591 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
592 else if (addr[0] == '/')
593 status = lcc_open_unixsocket (c, addr);
594 else
595 status = lcc_open_netsocket (c, addr);
597 return (status);
598 } /* }}} int lcc_open_socket */
600 /*
601 * Public functions
602 */
603 unsigned int lcc_version (void) /* {{{ */
604 {
605 return (LCC_VERSION);
606 } /* }}} unsigned int lcc_version */
608 const char *lcc_version_string (void) /* {{{ */
609 {
610 return (LCC_VERSION_STRING);
611 } /* }}} const char *lcc_version_string */
613 const char *lcc_version_extra (void) /* {{{ */
614 {
615 return (LCC_VERSION_EXTRA);
616 } /* }}} const char *lcc_version_extra */
618 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
619 {
620 lcc_connection_t *c;
621 int status;
623 if (address == NULL)
624 return (-1);
626 if (ret_con == NULL)
627 return (-1);
629 c = (lcc_connection_t *) malloc (sizeof (*c));
630 if (c == NULL)
631 return (-1);
632 memset (c, 0, sizeof (*c));
634 status = lcc_open_socket (c, address);
635 if (status != 0)
636 {
637 lcc_disconnect (c);
638 return (status);
639 }
641 *ret_con = c;
642 return (0);
643 } /* }}} int lcc_connect */
645 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
646 {
647 if (c == NULL)
648 return (-1);
650 if (c->fh != NULL)
651 {
652 fclose (c->fh);
653 c->fh = NULL;
654 }
656 free (c);
657 return (0);
658 } /* }}} int lcc_disconnect */
660 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
661 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
662 {
663 char ident_str[6 * LCC_NAME_LEN];
664 char ident_esc[12 * LCC_NAME_LEN];
665 char command[14 * LCC_NAME_LEN];
667 lcc_response_t res;
668 size_t values_num;
669 gauge_t *values = NULL;
670 char **values_names = NULL;
672 size_t i;
673 int status;
675 if (c == NULL)
676 return (-1);
678 if (ident == NULL)
679 {
680 lcc_set_errno (c, EINVAL);
681 return (-1);
682 }
684 /* Build a commend with an escaped version of the identifier string. */
685 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
686 if (status != 0)
687 return (status);
689 snprintf (command, sizeof (command), "GETVAL %s",
690 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
691 command[sizeof (command) - 1] = 0;
693 /* Send talk to the daemon.. */
694 status = lcc_sendreceive (c, command, &res);
695 if (status != 0)
696 return (status);
698 if (res.status != 0)
699 {
700 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
701 lcc_response_free (&res);
702 return (-1);
703 }
705 values_num = res.lines_num;
707 #define BAIL_OUT(e) do { \
708 lcc_set_errno (c, (e)); \
709 free (values); \
710 if (values_names != NULL) { \
711 for (i = 0; i < values_num; i++) { \
712 free (values_names[i]); \
713 } \
714 } \
715 free (values_names); \
716 lcc_response_free (&res); \
717 return (-1); \
718 } while (0)
720 /* If neither the values nor the names are requested, return here.. */
721 if ((ret_values == NULL) && (ret_values_names == NULL))
722 {
723 if (ret_values_num != NULL)
724 *ret_values_num = values_num;
725 lcc_response_free (&res);
726 return (0);
727 }
729 /* Allocate space for the values */
730 if (ret_values != NULL)
731 {
732 values = (gauge_t *) malloc (values_num * sizeof (*values));
733 if (values == NULL)
734 BAIL_OUT (ENOMEM);
735 }
737 if (ret_values_names != NULL)
738 {
739 values_names = (char **) calloc (values_num, sizeof (*values_names));
740 if (values_names == NULL)
741 BAIL_OUT (ENOMEM);
742 }
744 for (i = 0; i < res.lines_num; i++)
745 {
746 char *key;
747 char *value;
748 char *endptr;
750 key = res.lines[i];
751 value = strchr (key, '=');
752 if (value == NULL)
753 BAIL_OUT (EILSEQ);
755 *value = 0;
756 value++;
758 if (values != NULL)
759 {
760 endptr = NULL;
761 errno = 0;
762 values[i] = strtod (value, &endptr);
764 if ((endptr == value) || (errno != 0))
765 BAIL_OUT (errno);
766 }
768 if (values_names != NULL)
769 {
770 values_names[i] = strdup (key);
771 if (values_names[i] == NULL)
772 BAIL_OUT (ENOMEM);
773 }
774 } /* for (i = 0; i < res.lines_num; i++) */
776 if (ret_values_num != NULL)
777 *ret_values_num = values_num;
778 if (ret_values != NULL)
779 *ret_values = values;
780 if (ret_values_names != NULL)
781 *ret_values_names = values_names;
783 lcc_response_free (&res);
785 return (0);
786 } /* }}} int lcc_getval */
788 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
789 {
790 char ident_str[6 * LCC_NAME_LEN];
791 char ident_esc[12 * LCC_NAME_LEN];
792 char command[1024] = "";
793 lcc_response_t res;
794 int status;
795 size_t i;
797 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
798 || (vl->values == NULL) || (vl->values_types == NULL))
799 {
800 lcc_set_errno (c, EINVAL);
801 return (-1);
802 }
804 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
805 &vl->identifier);
806 if (status != 0)
807 return (status);
809 SSTRCATF (command, "PUTVAL %s",
810 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
812 if (vl->interval > 0)
813 SSTRCATF (command, " interval=%i", vl->interval);
815 if (vl->time > 0)
816 SSTRCATF (command, " %u", (unsigned int) vl->time);
817 else
818 SSTRCAT (command, " N");
820 for (i = 0; i < vl->values_len; i++)
821 {
822 if (vl->values_types[i] == LCC_TYPE_COUNTER)
823 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
824 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
825 {
826 if (isnan (vl->values[i].gauge))
827 SSTRCATF (command, ":U");
828 else
829 SSTRCATF (command, ":%g", vl->values[i].gauge);
830 }
831 else if (vl->values_types[i] == LCC_TYPE_DERIVE)
832 SSTRCATF (command, ":%"PRIu64, vl->values[i].derive);
833 else if (vl->values_types[i] == LCC_TYPE_ABSOLUTE)
834 SSTRCATF (command, ":%"PRIu64, vl->values[i].absolute);
836 } /* for (i = 0; i < vl->values_len; i++) */
838 status = lcc_sendreceive (c, command, &res);
839 if (status != 0)
840 return (status);
842 if (res.status != 0)
843 {
844 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
845 lcc_response_free (&res);
846 return (-1);
847 }
849 lcc_response_free (&res);
850 return (0);
851 } /* }}} int lcc_putval */
853 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
854 lcc_identifier_t *ident, int timeout)
855 {
856 char command[1024] = "";
857 lcc_response_t res;
858 int status;
860 if (c == NULL)
861 {
862 lcc_set_errno (c, EINVAL);
863 return (-1);
864 }
866 SSTRCPY (command, "FLUSH");
868 if (timeout > 0)
869 SSTRCATF (command, " timeout=%i", timeout);
871 if (plugin != NULL)
872 {
873 char buffer[2 * LCC_NAME_LEN];
874 SSTRCATF (command, " plugin=%s",
875 lcc_strescape (buffer, plugin, sizeof (buffer)));
876 }
878 if (ident != NULL)
879 {
880 char ident_str[6 * LCC_NAME_LEN];
881 char ident_esc[12 * LCC_NAME_LEN];
883 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
884 if (status != 0)
885 return (status);
887 SSTRCATF (command, " identifier=%s",
888 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
889 }
891 status = lcc_sendreceive (c, command, &res);
892 if (status != 0)
893 return (status);
895 if (res.status != 0)
896 {
897 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
898 lcc_response_free (&res);
899 return (-1);
900 }
902 lcc_response_free (&res);
903 return (0);
904 } /* }}} int lcc_flush */
906 /* TODO: Implement lcc_putnotif */
908 int lcc_listval (lcc_connection_t *c, /* {{{ */
909 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
910 {
911 lcc_response_t res;
912 size_t i;
913 int status;
915 lcc_identifier_t *ident;
916 size_t ident_num;
918 if (c == NULL)
919 return (-1);
921 if ((ret_ident == NULL) || (ret_ident_num == NULL))
922 {
923 lcc_set_errno (c, EINVAL);
924 return (-1);
925 }
927 status = lcc_sendreceive (c, "LISTVAL", &res);
928 if (status != 0)
929 return (status);
931 if (res.status != 0)
932 {
933 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
934 lcc_response_free (&res);
935 return (-1);
936 }
938 ident_num = res.lines_num;
939 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
940 if (ident == NULL)
941 {
942 lcc_response_free (&res);
943 lcc_set_errno (c, ENOMEM);
944 return (-1);
945 }
947 for (i = 0; i < res.lines_num; i++)
948 {
949 char *time_str;
950 char *ident_str;
952 /* First field is the time. */
953 time_str = res.lines[i];
955 /* Set `ident_str' to the beginning of the second field. */
956 ident_str = time_str;
957 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
958 ident_str++;
959 while ((*ident_str == ' ') || (*ident_str == '\t'))
960 {
961 *ident_str = 0;
962 ident_str++;
963 }
965 if (*ident_str == 0)
966 {
967 lcc_set_errno (c, EILSEQ);
968 status = -1;
969 break;
970 }
972 status = lcc_string_to_identifier (c, ident + i, ident_str);
973 if (status != 0)
974 break;
975 }
977 lcc_response_free (&res);
979 if (status != 0)
980 {
981 free (ident);
982 return (-1);
983 }
985 *ret_ident = ident;
986 *ret_ident_num = ident_num;
988 return (0);
989 } /* }}} int lcc_listval */
991 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
992 {
993 if (c == NULL)
994 return ("Invalid object");
995 return (c->errbuf);
996 } /* }}} const char *lcc_strerror */
998 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
999 char *string, size_t string_size, const lcc_identifier_t *ident)
1000 {
1001 if ((string == NULL) || (string_size < 6) || (ident == NULL))
1002 {
1003 lcc_set_errno (c, EINVAL);
1004 return (-1);
1005 }
1007 if (ident->plugin_instance[0] == 0)
1008 {
1009 if (ident->type_instance[0] == 0)
1010 snprintf (string, string_size, "%s/%s/%s",
1011 ident->host,
1012 ident->plugin,
1013 ident->type);
1014 else
1015 snprintf (string, string_size, "%s/%s/%s-%s",
1016 ident->host,
1017 ident->plugin,
1018 ident->type,
1019 ident->type_instance);
1020 }
1021 else
1022 {
1023 if (ident->type_instance[0] == 0)
1024 snprintf (string, string_size, "%s/%s-%s/%s",
1025 ident->host,
1026 ident->plugin,
1027 ident->plugin_instance,
1028 ident->type);
1029 else
1030 snprintf (string, string_size, "%s/%s-%s/%s-%s",
1031 ident->host,
1032 ident->plugin,
1033 ident->plugin_instance,
1034 ident->type,
1035 ident->type_instance);
1036 }
1038 string[string_size - 1] = 0;
1039 return (0);
1040 } /* }}} int lcc_identifier_to_string */
1042 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
1043 lcc_identifier_t *ident, const char *string)
1044 {
1045 char *string_copy;
1046 char *host;
1047 char *plugin;
1048 char *plugin_instance;
1049 char *type;
1050 char *type_instance;
1052 string_copy = strdup (string);
1053 if (string_copy == NULL)
1054 {
1055 lcc_set_errno (c, ENOMEM);
1056 return (-1);
1057 }
1059 host = string_copy;
1060 plugin = strchr (host, '/');
1061 if (plugin == NULL)
1062 {
1063 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1064 free (string_copy);
1065 return (-1);
1066 }
1067 *plugin = 0;
1068 plugin++;
1070 type = strchr (plugin, '/');
1071 if (type == NULL)
1072 {
1073 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1074 free (string_copy);
1075 return (-1);
1076 }
1077 *type = 0;
1078 type++;
1080 plugin_instance = strchr (plugin, '-');
1081 if (plugin_instance != NULL)
1082 {
1083 *plugin_instance = 0;
1084 plugin_instance++;
1085 }
1087 type_instance = strchr (type, '-');
1088 if (type_instance != NULL)
1089 {
1090 *type_instance = 0;
1091 type_instance++;
1092 }
1094 memset (ident, 0, sizeof (*ident));
1096 SSTRCPY (ident->host, host);
1097 SSTRCPY (ident->plugin, plugin);
1098 if (plugin_instance != NULL)
1099 SSTRCPY (ident->plugin_instance, plugin_instance);
1100 SSTRCPY (ident->type, type);
1101 if (type_instance != NULL)
1102 SSTRCPY (ident->type_instance, type_instance);
1104 free (string_copy);
1105 return (0);
1106 } /* }}} int lcc_string_to_identifier */
1108 int lcc_sort_identifiers (lcc_connection_t *c, /* {{{ */
1109 lcc_identifier_t *idents, size_t idents_num)
1110 {
1111 if (idents == NULL)
1112 {
1113 lcc_set_errno (c, EINVAL);
1114 return (-1);
1115 }
1117 qsort (idents, idents_num, sizeof (*idents), lcc_identifier_cmp);
1118 return (0);
1119 } /* }}} int lcc_sort_identifiers */
1121 /* vim: set sw=2 sts=2 et fdm=marker : */