1 /**
2 * libcollectdclient - src/libcollectdclient/client.c
3 * Copyright (C) 2008-2012 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 **/
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #if !defined(__GNUC__) || !__GNUC__
32 # define __attribute__(x) /**/
33 #endif
35 #include "collectd/lcc_features.h"
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/un.h>
43 #include <string.h>
44 #include <assert.h>
45 #include <errno.h>
46 #include <math.h>
47 #include <netdb.h>
49 #include "collectd/client.h"
51 /* NI_MAXHOST has been obsoleted by RFC 3493 which is a reason for SunOS 5.11
52 * to no longer define it. We'll use the old, RFC 2553 value here. */
53 #ifndef NI_MAXHOST
54 # define NI_MAXHOST 1025
55 #endif
57 /* OpenBSD doesn't have EPROTO, FreeBSD doesn't have EILSEQ. Oh what joy! */
58 #ifndef EILSEQ
59 # ifdef EPROTO
60 # define EILSEQ EPROTO
61 # else
62 # define EILSEQ EINVAL
63 # endif
64 #endif
66 /* Secure/static macros. They work like `strcpy' and `strcat', but assure null
67 * termination. They work for static buffers only, because they use `sizeof'.
68 * The `SSTRCATF' combines the functionality of `snprintf' and `strcat' which
69 * is very useful to add formatted stuff to the end of a buffer. */
70 #define SSTRCPY(d,s) do { \
71 strncpy ((d), (s), sizeof (d)); \
72 (d)[sizeof (d) - 1] = 0; \
73 } while (0)
75 #define SSTRCAT(d,s) do { \
76 size_t _l = strlen (d); \
77 strncpy ((d) + _l, (s), sizeof (d) - _l); \
78 (d)[sizeof (d) - 1] = 0; \
79 } while (0)
81 #define SSTRCATF(d, ...) do { \
82 char _b[sizeof (d)]; \
83 snprintf (_b, sizeof (_b), __VA_ARGS__); \
84 _b[sizeof (_b) - 1] = 0; \
85 SSTRCAT ((d), _b); \
86 } while (0)
89 #define LCC_SET_ERRSTR(c, ...) do { \
90 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
91 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
92 } while (0)
94 #if COLLECT_DEBUG
95 # define LCC_DEBUG(...) printf (__VA_ARGS__)
96 #else
97 # define LCC_DEBUG(...) /**/
98 #endif
100 /*
101 * Types
102 */
103 struct lcc_connection_s
104 {
105 FILE *fh;
106 char errbuf[1024];
107 };
109 struct lcc_response_s
110 {
111 int status;
112 char message[1024];
113 char **lines;
114 size_t lines_num;
115 };
116 typedef struct lcc_response_s lcc_response_t;
118 /*
119 * Private functions
120 */
121 /* Even though Posix requires "strerror_r" to return an "int",
122 * some systems (e.g. the GNU libc) return a "char *" _and_
123 * ignore the second argument ... -tokkee */
124 static char *sstrerror (int errnum, char *buf, size_t buflen)
125 {
126 buf[0] = 0;
128 #if !HAVE_STRERROR_R
129 snprintf (buf, buflen, "Error #%i; strerror_r is not available.", errnum);
130 /* #endif !HAVE_STRERROR_R */
132 #elif STRERROR_R_CHAR_P
133 {
134 char *temp;
135 temp = strerror_r (errnum, buf, buflen);
136 if (buf[0] == 0)
137 {
138 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
139 strncpy (buf, temp, buflen);
140 else
141 strncpy (buf, "strerror_r did not return "
142 "an error message", buflen);
143 }
144 }
145 /* #endif STRERROR_R_CHAR_P */
147 #else
148 if (strerror_r (errnum, buf, buflen) != 0)
149 {
150 snprintf (buf, buflen, "Error #%i; "
151 "Additionally, strerror_r failed.",
152 errnum);
153 }
154 #endif /* STRERROR_R_CHAR_P */
156 buf[buflen - 1] = 0;
158 return (buf);
159 } /* char *sstrerror */
161 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
162 {
163 if (c == NULL)
164 return (-1);
166 sstrerror (err, c->errbuf, sizeof (c->errbuf));
167 c->errbuf[sizeof (c->errbuf) - 1] = 0;
169 return (0);
170 } /* }}} int lcc_set_errno */
172 static char *lcc_strescape (char *dest, const char *src, size_t dest_size) /* {{{ */
173 {
174 size_t dest_pos;
175 size_t src_pos;
177 if ((dest == NULL) || (src == NULL))
178 return (NULL);
180 dest_pos = 0;
181 src_pos = 0;
183 assert (dest_size >= 3);
185 dest[dest_pos] = '"';
186 dest_pos++;
188 while (42)
189 {
190 if ((dest_pos == (dest_size - 2))
191 || (src[src_pos] == 0))
192 break;
194 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
195 {
196 /* Check if there is enough space for both characters.. */
197 if (dest_pos == (dest_size - 3))
198 break;
200 dest[dest_pos] = '\\';
201 dest_pos++;
202 }
204 dest[dest_pos] = src[src_pos];
205 dest_pos++;
206 src_pos++;
207 }
209 assert (dest_pos <= (dest_size - 2));
211 dest[dest_pos] = '"';
212 dest_pos++;
214 dest[dest_pos] = 0;
215 dest_pos++;
216 src_pos++;
218 return (dest);
219 } /* }}} char *lcc_strescape */
221 /* lcc_chomp: Removes all control-characters at the end of a string. */
222 static void lcc_chomp (char *str) /* {{{ */
223 {
224 size_t str_len;
226 str_len = strlen (str);
227 while (str_len > 0)
228 {
229 if (str[str_len - 1] >= 32)
230 break;
231 str[str_len - 1] = 0;
232 str_len--;
233 }
234 } /* }}} void lcc_chomp */
236 static void lcc_response_free (lcc_response_t *res) /* {{{ */
237 {
238 size_t i;
240 if (res == NULL)
241 return;
243 for (i = 0; i < res->lines_num; i++)
244 free (res->lines[i]);
245 free (res->lines);
246 res->lines = NULL;
247 } /* }}} void lcc_response_free */
249 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
250 {
251 int status;
253 LCC_DEBUG ("send: --> %s\n", command);
255 status = fprintf (c->fh, "%s\r\n", command);
256 if (status < 0)
257 {
258 lcc_set_errno (c, errno);
259 return (-1);
260 }
261 fflush(c->fh);
263 return (0);
264 } /* }}} int lcc_send */
266 static int lcc_receive (lcc_connection_t *c, /* {{{ */
267 lcc_response_t *ret_res)
268 {
269 lcc_response_t res;
270 char *ptr;
271 char buffer[4096];
272 size_t i;
274 memset (&res, 0, sizeof (res));
276 /* Read the first line, containing the status and a message */
277 ptr = fgets (buffer, sizeof (buffer), c->fh);
278 if (ptr == NULL)
279 {
280 lcc_set_errno (c, errno);
281 return (-1);
282 }
283 lcc_chomp (buffer);
284 LCC_DEBUG ("receive: <-- %s\n", buffer);
286 /* Convert the leading status to an integer and make `ptr' to point to the
287 * beginning of the message. */
288 ptr = NULL;
289 errno = 0;
290 res.status = (int) strtol (buffer, &ptr, 0);
291 if ((errno != 0) || (ptr == &buffer[0]))
292 {
293 lcc_set_errno (c, errno);
294 return (-1);
295 }
297 /* Skip white spaces after the status number */
298 while ((*ptr == ' ') || (*ptr == '\t'))
299 ptr++;
301 /* Now copy the message. */
302 strncpy (res.message, ptr, sizeof (res.message));
303 res.message[sizeof (res.message) - 1] = 0;
305 /* Error or no lines follow: We're done. */
306 if (res.status <= 0)
307 {
308 memcpy (ret_res, &res, sizeof (res));
309 return (0);
310 }
312 /* Allocate space for the char-pointers */
313 res.lines_num = (size_t) res.status;
314 res.status = 0;
315 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
316 if (res.lines == NULL)
317 {
318 lcc_set_errno (c, ENOMEM);
319 return (-1);
320 }
322 /* Now receive all the lines */
323 for (i = 0; i < res.lines_num; i++)
324 {
325 ptr = fgets (buffer, sizeof (buffer), c->fh);
326 if (ptr == NULL)
327 {
328 lcc_set_errno (c, errno);
329 break;
330 }
331 lcc_chomp (buffer);
332 LCC_DEBUG ("receive: <-- %s\n", buffer);
334 res.lines[i] = strdup (buffer);
335 if (res.lines[i] == NULL)
336 {
337 lcc_set_errno (c, ENOMEM);
338 break;
339 }
340 }
342 /* Check if the for-loop exited with an error. */
343 if (i < res.lines_num)
344 {
345 while (i > 0)
346 {
347 i--;
348 free (res.lines[i]);
349 }
350 free (res.lines);
351 return (-1);
352 }
354 memcpy (ret_res, &res, sizeof (res));
355 return (0);
356 } /* }}} int lcc_receive */
358 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
359 const char *command, lcc_response_t *ret_res)
360 {
361 lcc_response_t res;
362 int status;
364 if (c->fh == NULL)
365 {
366 lcc_set_errno (c, EBADF);
367 return (-1);
368 }
370 status = lcc_send (c, command);
371 if (status != 0)
372 return (status);
374 memset (&res, 0, sizeof (res));
375 status = lcc_receive (c, &res);
376 if (status == 0)
377 memcpy (ret_res, &res, sizeof (*ret_res));
379 return (status);
380 } /* }}} int lcc_sendreceive */
382 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
383 {
384 struct sockaddr_un sa;
385 int fd;
386 int status;
388 assert (c != NULL);
389 assert (c->fh == NULL);
390 assert (path != NULL);
392 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
393 * others). */
394 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
395 if (fd < 0)
396 {
397 lcc_set_errno (c, errno);
398 return (-1);
399 }
401 memset (&sa, 0, sizeof (sa));
402 sa.sun_family = AF_UNIX;
403 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
405 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
406 if (status != 0)
407 {
408 lcc_set_errno (c, errno);
409 close (fd);
410 return (-1);
411 }
413 c->fh = fdopen (fd, "r+");
414 if (c->fh == NULL)
415 {
416 lcc_set_errno (c, errno);
417 close (fd);
418 return (-1);
419 }
421 return (0);
422 } /* }}} int lcc_open_unixsocket */
424 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
425 const char *addr_orig)
426 {
427 struct addrinfo ai_hints;
428 struct addrinfo *ai_res;
429 struct addrinfo *ai_ptr;
430 char addr_copy[NI_MAXHOST];
431 char *addr;
432 char *port;
433 int fd;
434 int status;
436 assert (c != NULL);
437 assert (c->fh == NULL);
438 assert (addr_orig != NULL);
440 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
441 addr_copy[sizeof(addr_copy) - 1] = '\0';
442 addr = addr_copy;
444 memset (&ai_hints, 0, sizeof (ai_hints));
445 ai_hints.ai_flags = 0;
446 #ifdef AI_ADDRCONFIG
447 ai_hints.ai_flags |= AI_ADDRCONFIG;
448 #endif
449 ai_hints.ai_family = AF_UNSPEC;
450 ai_hints.ai_socktype = SOCK_STREAM;
452 port = NULL;
453 if (*addr == '[') /* IPv6+port format */
454 {
455 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
456 addr++;
458 port = strchr (addr, ']');
459 if (port == NULL)
460 {
461 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
462 return (-1);
463 }
464 *port = 0;
465 port++;
467 if (*port == ':')
468 port++;
469 else if (*port == 0)
470 port = NULL;
471 else
472 {
473 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
474 return (-1);
475 }
476 } /* if (*addr = ']') */
477 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
478 {
479 port = strrchr (addr, ':');
480 if (port != NULL)
481 {
482 *port = 0;
483 port++;
484 }
485 }
487 ai_res = NULL;
488 status = getaddrinfo (addr,
489 port == NULL ? LCC_DEFAULT_PORT : port,
490 &ai_hints, &ai_res);
491 if (status != 0)
492 {
493 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
494 return (-1);
495 }
497 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
498 {
499 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
500 if (fd < 0)
501 {
502 status = errno;
503 continue;
504 }
506 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
507 if (status != 0)
508 {
509 status = errno;
510 close (fd);
511 continue;
512 }
514 c->fh = fdopen (fd, "r+");
515 if (c->fh == NULL)
516 {
517 status = errno;
518 close (fd);
519 continue;
520 }
522 assert (status == 0);
523 break;
524 } /* for (ai_ptr) */
526 if (status != 0)
527 {
528 lcc_set_errno (c, status);
529 return (-1);
530 }
532 return (0);
533 } /* }}} int lcc_open_netsocket */
535 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
536 {
537 int status = 0;
539 if (addr == NULL)
540 return (-1);
542 assert (c != NULL);
543 assert (c->fh == NULL);
544 assert (addr != NULL);
546 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
547 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
548 else if (addr[0] == '/')
549 status = lcc_open_unixsocket (c, addr);
550 else
551 status = lcc_open_netsocket (c, addr);
553 return (status);
554 } /* }}} int lcc_open_socket */
556 /*
557 * Public functions
558 */
559 unsigned int lcc_version (void) /* {{{ */
560 {
561 return (LCC_VERSION);
562 } /* }}} unsigned int lcc_version */
564 const char *lcc_version_string (void) /* {{{ */
565 {
566 return (LCC_VERSION_STRING);
567 } /* }}} const char *lcc_version_string */
569 const char *lcc_version_extra (void) /* {{{ */
570 {
571 return (LCC_VERSION_EXTRA);
572 } /* }}} const char *lcc_version_extra */
574 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
575 {
576 lcc_connection_t *c;
577 int status;
579 if (address == NULL)
580 return (-1);
582 if (ret_con == NULL)
583 return (-1);
585 c = (lcc_connection_t *) malloc (sizeof (*c));
586 if (c == NULL)
587 return (-1);
588 memset (c, 0, sizeof (*c));
590 status = lcc_open_socket (c, address);
591 if (status != 0)
592 {
593 lcc_disconnect (c);
594 return (status);
595 }
597 *ret_con = c;
598 return (0);
599 } /* }}} int lcc_connect */
601 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
602 {
603 if (c == NULL)
604 return (-1);
606 if (c->fh != NULL)
607 {
608 fclose (c->fh);
609 c->fh = NULL;
610 }
612 free (c);
613 return (0);
614 } /* }}} int lcc_disconnect */
616 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
617 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
618 {
619 char ident_str[6 * LCC_NAME_LEN];
620 char ident_esc[12 * LCC_NAME_LEN];
621 char command[14 * LCC_NAME_LEN];
623 lcc_response_t res;
624 size_t values_num;
625 gauge_t *values = NULL;
626 char **values_names = NULL;
628 size_t i;
629 int status;
631 if (c == NULL)
632 return (-1);
634 if (ident == NULL)
635 {
636 lcc_set_errno (c, EINVAL);
637 return (-1);
638 }
640 /* Build a commend with an escaped version of the identifier string. */
641 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
642 if (status != 0)
643 return (status);
645 snprintf (command, sizeof (command), "GETVAL %s",
646 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
647 command[sizeof (command) - 1] = 0;
649 /* Send talk to the daemon.. */
650 status = lcc_sendreceive (c, command, &res);
651 if (status != 0)
652 return (status);
654 if (res.status != 0)
655 {
656 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
657 lcc_response_free (&res);
658 return (-1);
659 }
661 values_num = res.lines_num;
663 #define BAIL_OUT(e) do { \
664 lcc_set_errno (c, (e)); \
665 free (values); \
666 if (values_names != NULL) { \
667 for (i = 0; i < values_num; i++) { \
668 free (values_names[i]); \
669 } \
670 } \
671 free (values_names); \
672 lcc_response_free (&res); \
673 return (-1); \
674 } while (0)
676 /* If neither the values nor the names are requested, return here.. */
677 if ((ret_values == NULL) && (ret_values_names == NULL))
678 {
679 if (ret_values_num != NULL)
680 *ret_values_num = values_num;
681 lcc_response_free (&res);
682 return (0);
683 }
685 /* Allocate space for the values */
686 if (ret_values != NULL)
687 {
688 values = (gauge_t *) malloc (values_num * sizeof (*values));
689 if (values == NULL)
690 BAIL_OUT (ENOMEM);
691 }
693 if (ret_values_names != NULL)
694 {
695 values_names = (char **) calloc (values_num, sizeof (*values_names));
696 if (values_names == NULL)
697 BAIL_OUT (ENOMEM);
698 }
700 for (i = 0; i < res.lines_num; i++)
701 {
702 char *key;
703 char *value;
704 char *endptr;
706 key = res.lines[i];
707 value = strchr (key, '=');
708 if (value == NULL)
709 BAIL_OUT (EILSEQ);
711 *value = 0;
712 value++;
714 if (values != NULL)
715 {
716 endptr = NULL;
717 errno = 0;
718 values[i] = strtod (value, &endptr);
720 if ((endptr == value) || (errno != 0))
721 BAIL_OUT (errno);
722 }
724 if (values_names != NULL)
725 {
726 values_names[i] = strdup (key);
727 if (values_names[i] == NULL)
728 BAIL_OUT (ENOMEM);
729 }
730 } /* for (i = 0; i < res.lines_num; i++) */
732 if (ret_values_num != NULL)
733 *ret_values_num = values_num;
734 if (ret_values != NULL)
735 *ret_values = values;
736 if (ret_values_names != NULL)
737 *ret_values_names = values_names;
739 lcc_response_free (&res);
741 return (0);
742 } /* }}} int lcc_getval */
744 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
745 {
746 char ident_str[6 * LCC_NAME_LEN];
747 char ident_esc[12 * LCC_NAME_LEN];
748 char command[1024] = "";
749 lcc_response_t res;
750 int status;
751 size_t i;
753 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
754 || (vl->values == NULL) || (vl->values_types == NULL))
755 {
756 lcc_set_errno (c, EINVAL);
757 return (-1);
758 }
760 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
761 &vl->identifier);
762 if (status != 0)
763 return (status);
765 SSTRCATF (command, "PUTVAL %s",
766 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
768 if (vl->interval > 0.0)
769 SSTRCATF (command, " interval=%.3f", vl->interval);
771 if (vl->time > 0.0)
772 SSTRCATF (command, " %.3f", vl->time);
773 else
774 SSTRCAT (command, " N");
776 for (i = 0; i < vl->values_len; i++)
777 {
778 if (vl->values_types[i] == LCC_TYPE_COUNTER)
779 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
780 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
781 {
782 if (isnan (vl->values[i].gauge))
783 SSTRCATF (command, ":U");
784 else
785 SSTRCATF (command, ":%g", vl->values[i].gauge);
786 }
787 else if (vl->values_types[i] == LCC_TYPE_DERIVE)
788 SSTRCATF (command, ":%"PRIu64, vl->values[i].derive);
789 else if (vl->values_types[i] == LCC_TYPE_ABSOLUTE)
790 SSTRCATF (command, ":%"PRIu64, vl->values[i].absolute);
792 } /* for (i = 0; i < vl->values_len; i++) */
794 status = lcc_sendreceive (c, command, &res);
795 if (status != 0)
796 return (status);
798 if (res.status != 0)
799 {
800 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
801 lcc_response_free (&res);
802 return (-1);
803 }
805 lcc_response_free (&res);
806 return (0);
807 } /* }}} int lcc_putval */
809 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
810 lcc_identifier_t *ident, int timeout)
811 {
812 char command[1024] = "";
813 lcc_response_t res;
814 int status;
816 if (c == NULL)
817 {
818 lcc_set_errno (c, EINVAL);
819 return (-1);
820 }
822 SSTRCPY (command, "FLUSH");
824 if (timeout > 0)
825 SSTRCATF (command, " timeout=%i", timeout);
827 if (plugin != NULL)
828 {
829 char buffer[2 * LCC_NAME_LEN];
830 SSTRCATF (command, " plugin=%s",
831 lcc_strescape (buffer, plugin, sizeof (buffer)));
832 }
834 if (ident != NULL)
835 {
836 char ident_str[6 * LCC_NAME_LEN];
837 char ident_esc[12 * LCC_NAME_LEN];
839 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
840 if (status != 0)
841 return (status);
843 SSTRCATF (command, " identifier=%s",
844 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
845 }
847 status = lcc_sendreceive (c, command, &res);
848 if (status != 0)
849 return (status);
851 if (res.status != 0)
852 {
853 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
854 lcc_response_free (&res);
855 return (-1);
856 }
858 lcc_response_free (&res);
859 return (0);
860 } /* }}} int lcc_flush */
862 /* TODO: Implement lcc_putnotif */
864 int lcc_listval (lcc_connection_t *c, /* {{{ */
865 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
866 {
867 lcc_response_t res;
868 size_t i;
869 int status;
871 lcc_identifier_t *ident;
872 size_t ident_num;
874 if (c == NULL)
875 return (-1);
877 if ((ret_ident == NULL) || (ret_ident_num == NULL))
878 {
879 lcc_set_errno (c, EINVAL);
880 return (-1);
881 }
883 status = lcc_sendreceive (c, "LISTVAL", &res);
884 if (status != 0)
885 return (status);
887 if (res.status != 0)
888 {
889 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
890 lcc_response_free (&res);
891 return (-1);
892 }
894 ident_num = res.lines_num;
895 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
896 if (ident == NULL)
897 {
898 lcc_response_free (&res);
899 lcc_set_errno (c, ENOMEM);
900 return (-1);
901 }
903 for (i = 0; i < res.lines_num; i++)
904 {
905 char *time_str;
906 char *ident_str;
908 /* First field is the time. */
909 time_str = res.lines[i];
911 /* Set `ident_str' to the beginning of the second field. */
912 ident_str = time_str;
913 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
914 ident_str++;
915 while ((*ident_str == ' ') || (*ident_str == '\t'))
916 {
917 *ident_str = 0;
918 ident_str++;
919 }
921 if (*ident_str == 0)
922 {
923 lcc_set_errno (c, EILSEQ);
924 status = -1;
925 break;
926 }
928 status = lcc_string_to_identifier (c, ident + i, ident_str);
929 if (status != 0)
930 break;
931 }
933 lcc_response_free (&res);
935 if (status != 0)
936 {
937 free (ident);
938 return (-1);
939 }
941 *ret_ident = ident;
942 *ret_ident_num = ident_num;
944 return (0);
945 } /* }}} int lcc_listval */
947 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
948 {
949 if (c == NULL)
950 return ("Invalid object");
951 return (c->errbuf);
952 } /* }}} const char *lcc_strerror */
954 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
955 char *string, size_t string_size, const lcc_identifier_t *ident)
956 {
957 if ((string == NULL) || (string_size < 6) || (ident == NULL))
958 {
959 lcc_set_errno (c, EINVAL);
960 return (-1);
961 }
963 if (ident->plugin_instance[0] == 0)
964 {
965 if (ident->type_instance[0] == 0)
966 snprintf (string, string_size, "%s/%s/%s",
967 ident->host,
968 ident->plugin,
969 ident->type);
970 else
971 snprintf (string, string_size, "%s/%s/%s-%s",
972 ident->host,
973 ident->plugin,
974 ident->type,
975 ident->type_instance);
976 }
977 else
978 {
979 if (ident->type_instance[0] == 0)
980 snprintf (string, string_size, "%s/%s-%s/%s",
981 ident->host,
982 ident->plugin,
983 ident->plugin_instance,
984 ident->type);
985 else
986 snprintf (string, string_size, "%s/%s-%s/%s-%s",
987 ident->host,
988 ident->plugin,
989 ident->plugin_instance,
990 ident->type,
991 ident->type_instance);
992 }
994 string[string_size - 1] = 0;
995 return (0);
996 } /* }}} int lcc_identifier_to_string */
998 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
999 lcc_identifier_t *ident, const char *string)
1000 {
1001 char *string_copy;
1002 char *host;
1003 char *plugin;
1004 char *plugin_instance;
1005 char *type;
1006 char *type_instance;
1008 string_copy = strdup (string);
1009 if (string_copy == NULL)
1010 {
1011 lcc_set_errno (c, ENOMEM);
1012 return (-1);
1013 }
1015 host = string_copy;
1016 plugin = strchr (host, '/');
1017 if (plugin == NULL)
1018 {
1019 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1020 free (string_copy);
1021 return (-1);
1022 }
1023 *plugin = 0;
1024 plugin++;
1026 type = strchr (plugin, '/');
1027 if (type == NULL)
1028 {
1029 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1030 free (string_copy);
1031 return (-1);
1032 }
1033 *type = 0;
1034 type++;
1036 plugin_instance = strchr (plugin, '-');
1037 if (plugin_instance != NULL)
1038 {
1039 *plugin_instance = 0;
1040 plugin_instance++;
1041 }
1043 type_instance = strchr (type, '-');
1044 if (type_instance != NULL)
1045 {
1046 *type_instance = 0;
1047 type_instance++;
1048 }
1050 memset (ident, 0, sizeof (*ident));
1052 SSTRCPY (ident->host, host);
1053 SSTRCPY (ident->plugin, plugin);
1054 if (plugin_instance != NULL)
1055 SSTRCPY (ident->plugin_instance, plugin_instance);
1056 SSTRCPY (ident->type, type);
1057 if (type_instance != NULL)
1058 SSTRCPY (ident->type_instance, type_instance);
1060 free (string_copy);
1061 return (0);
1062 } /* }}} int lcc_string_to_identifier */
1064 int lcc_identifier_compare (const lcc_identifier_t *i0, /* {{{ */
1065 const lcc_identifier_t *i1)
1066 {
1067 int status;
1069 if ((i0 == NULL) && (i1 == NULL))
1070 return (0);
1071 else if (i0 == NULL)
1072 return (-1);
1073 else if (i1 == NULL)
1074 return (1);
1076 #define CMP_FIELD(f) do { \
1077 status = strcmp (i0->f, i1->f); \
1078 if (status != 0) \
1079 return (status); \
1080 } while (0);
1082 CMP_FIELD (host);
1083 CMP_FIELD (plugin);
1084 CMP_FIELD (plugin_instance);
1085 CMP_FIELD (type);
1086 CMP_FIELD (type_instance);
1088 #undef CMP_FIELD
1090 return (0);
1091 } /* }}} int lcc_identifier_compare */
1093 int lcc_sort_identifiers (lcc_connection_t *c, /* {{{ */
1094 lcc_identifier_t *idents, size_t idents_num)
1095 {
1096 if (idents == NULL)
1097 {
1098 lcc_set_errno (c, EINVAL);
1099 return (-1);
1100 }
1102 qsort (idents, idents_num, sizeof (*idents),
1103 (void *) lcc_identifier_compare);
1104 return (0);
1105 } /* }}} int lcc_sort_identifiers */
1107 /* vim: set sw=2 sts=2 et fdm=marker : */