cd333cc3ced478baf8c445a623e4dda114bbca43
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 = { 0 };
270 char *ptr;
271 char buffer[4096];
272 size_t i;
274 /* Read the first line, containing the status and a message */
275 ptr = fgets (buffer, sizeof (buffer), c->fh);
276 if (ptr == NULL)
277 {
278 lcc_set_errno (c, errno);
279 return (-1);
280 }
281 lcc_chomp (buffer);
282 LCC_DEBUG ("receive: <-- %s\n", buffer);
284 /* Convert the leading status to an integer and make `ptr' to point to the
285 * beginning of the message. */
286 ptr = NULL;
287 errno = 0;
288 res.status = (int) strtol (buffer, &ptr, 0);
289 if ((errno != 0) || (ptr == &buffer[0]))
290 {
291 lcc_set_errno (c, errno);
292 return (-1);
293 }
295 /* Skip white spaces after the status number */
296 while ((*ptr == ' ') || (*ptr == '\t'))
297 ptr++;
299 /* Now copy the message. */
300 strncpy (res.message, ptr, sizeof (res.message));
301 res.message[sizeof (res.message) - 1] = 0;
303 /* Error or no lines follow: We're done. */
304 if (res.status <= 0)
305 {
306 memcpy (ret_res, &res, sizeof (res));
307 return (0);
308 }
310 /* Allocate space for the char-pointers */
311 res.lines_num = (size_t) res.status;
312 res.status = 0;
313 res.lines = malloc (res.lines_num * sizeof (*res.lines));
314 if (res.lines == NULL)
315 {
316 lcc_set_errno (c, ENOMEM);
317 return (-1);
318 }
320 /* Now receive all the lines */
321 for (i = 0; i < res.lines_num; i++)
322 {
323 ptr = fgets (buffer, sizeof (buffer), c->fh);
324 if (ptr == NULL)
325 {
326 lcc_set_errno (c, errno);
327 break;
328 }
329 lcc_chomp (buffer);
330 LCC_DEBUG ("receive: <-- %s\n", buffer);
332 res.lines[i] = strdup (buffer);
333 if (res.lines[i] == NULL)
334 {
335 lcc_set_errno (c, ENOMEM);
336 break;
337 }
338 }
340 /* Check if the for-loop exited with an error. */
341 if (i < res.lines_num)
342 {
343 while (i > 0)
344 {
345 i--;
346 free (res.lines[i]);
347 }
348 free (res.lines);
349 return (-1);
350 }
352 memcpy (ret_res, &res, sizeof (res));
353 return (0);
354 } /* }}} int lcc_receive */
356 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
357 const char *command, lcc_response_t *ret_res)
358 {
359 lcc_response_t res = { 0 };
360 int status;
362 if (c->fh == NULL)
363 {
364 lcc_set_errno (c, EBADF);
365 return (-1);
366 }
368 status = lcc_send (c, command);
369 if (status != 0)
370 return (status);
372 status = lcc_receive (c, &res);
373 if (status == 0)
374 memcpy (ret_res, &res, sizeof (*ret_res));
376 return (status);
377 } /* }}} int lcc_sendreceive */
379 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
380 {
381 struct sockaddr_un sa = { 0 };
382 int fd;
383 int status;
385 assert (c != NULL);
386 assert (c->fh == NULL);
387 assert (path != NULL);
389 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
390 * others). */
391 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
392 if (fd < 0)
393 {
394 lcc_set_errno (c, errno);
395 return (-1);
396 }
398 sa.sun_family = AF_UNIX;
399 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
401 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
402 if (status != 0)
403 {
404 lcc_set_errno (c, errno);
405 close (fd);
406 return (-1);
407 }
409 c->fh = fdopen (fd, "r+");
410 if (c->fh == NULL)
411 {
412 lcc_set_errno (c, errno);
413 close (fd);
414 return (-1);
415 }
417 return (0);
418 } /* }}} int lcc_open_unixsocket */
420 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
421 const char *addr_orig)
422 {
423 struct addrinfo *ai_res;
424 struct addrinfo *ai_ptr;
425 char addr_copy[NI_MAXHOST];
426 char *addr;
427 char *port;
428 int fd;
429 int status;
431 assert (c != NULL);
432 assert (c->fh == NULL);
433 assert (addr_orig != NULL);
435 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
436 addr_copy[sizeof(addr_copy) - 1] = '\0';
437 addr = addr_copy;
439 port = NULL;
440 if (*addr == '[') /* IPv6+port format */
441 {
442 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
443 addr++;
445 port = strchr (addr, ']');
446 if (port == NULL)
447 {
448 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
449 return (-1);
450 }
451 *port = 0;
452 port++;
454 if (*port == ':')
455 port++;
456 else if (*port == 0)
457 port = NULL;
458 else
459 {
460 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
461 return (-1);
462 }
463 } /* if (*addr = ']') */
464 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
465 {
466 port = strrchr (addr, ':');
467 if (port != NULL)
468 {
469 *port = 0;
470 port++;
471 }
472 }
474 struct addrinfo ai_hints = {
475 .ai_family = AF_UNSPEC,
476 .ai_flags = AI_ADDRCONFIG,
477 .ai_socktype = SOCK_STREAM
478 };
480 status = getaddrinfo (addr,
481 port == NULL ? LCC_DEFAULT_PORT : port,
482 &ai_hints, &ai_res);
483 if (status != 0)
484 {
485 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
486 return (-1);
487 }
489 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
490 {
491 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
492 if (fd < 0)
493 {
494 status = errno;
495 continue;
496 }
498 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
499 if (status != 0)
500 {
501 status = errno;
502 close (fd);
503 continue;
504 }
506 c->fh = fdopen (fd, "r+");
507 if (c->fh == NULL)
508 {
509 status = errno;
510 close (fd);
511 continue;
512 }
514 assert (status == 0);
515 break;
516 } /* for (ai_ptr) */
518 if (status != 0)
519 {
520 lcc_set_errno (c, status);
521 freeaddrinfo (ai_res);
522 return (-1);
523 }
525 freeaddrinfo (ai_res);
526 return (0);
527 } /* }}} int lcc_open_netsocket */
529 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
530 {
531 int status = 0;
533 if (addr == NULL)
534 return (-1);
536 assert (c != NULL);
537 assert (c->fh == NULL);
538 assert (addr != NULL);
540 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
541 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
542 else if (addr[0] == '/')
543 status = lcc_open_unixsocket (c, addr);
544 else
545 status = lcc_open_netsocket (c, addr);
547 return (status);
548 } /* }}} int lcc_open_socket */
550 /*
551 * Public functions
552 */
553 unsigned int lcc_version (void) /* {{{ */
554 {
555 return (LCC_VERSION);
556 } /* }}} unsigned int lcc_version */
558 const char *lcc_version_string (void) /* {{{ */
559 {
560 return (LCC_VERSION_STRING);
561 } /* }}} const char *lcc_version_string */
563 const char *lcc_version_extra (void) /* {{{ */
564 {
565 return (LCC_VERSION_EXTRA);
566 } /* }}} const char *lcc_version_extra */
568 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
569 {
570 lcc_connection_t *c;
571 int status;
573 if (address == NULL)
574 return (-1);
576 if (ret_con == NULL)
577 return (-1);
579 c = calloc (1, sizeof (*c));
580 if (c == NULL)
581 return (-1);
583 status = lcc_open_socket (c, address);
584 if (status != 0)
585 {
586 lcc_disconnect (c);
587 return (status);
588 }
590 *ret_con = c;
591 return (0);
592 } /* }}} int lcc_connect */
594 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
595 {
596 if (c == NULL)
597 return (-1);
599 if (c->fh != NULL)
600 {
601 fclose (c->fh);
602 c->fh = NULL;
603 }
605 free (c);
606 return (0);
607 } /* }}} int lcc_disconnect */
609 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
610 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
611 {
612 char ident_str[6 * LCC_NAME_LEN];
613 char ident_esc[12 * LCC_NAME_LEN];
614 char command[14 * LCC_NAME_LEN];
616 lcc_response_t res;
617 size_t values_num;
618 gauge_t *values = NULL;
619 char **values_names = NULL;
621 size_t i;
622 int status;
624 if (c == NULL)
625 return (-1);
627 if (ident == NULL)
628 {
629 lcc_set_errno (c, EINVAL);
630 return (-1);
631 }
633 /* Build a commend with an escaped version of the identifier string. */
634 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
635 if (status != 0)
636 return (status);
638 snprintf (command, sizeof (command), "GETVAL %s",
639 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
640 command[sizeof (command) - 1] = 0;
642 /* Send talk to the daemon.. */
643 status = lcc_sendreceive (c, command, &res);
644 if (status != 0)
645 return (status);
647 if (res.status != 0)
648 {
649 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
650 lcc_response_free (&res);
651 return (-1);
652 }
654 values_num = res.lines_num;
656 #define BAIL_OUT(e) do { \
657 lcc_set_errno (c, (e)); \
658 free (values); \
659 if (values_names != NULL) { \
660 for (i = 0; i < values_num; i++) { \
661 free (values_names[i]); \
662 } \
663 } \
664 free (values_names); \
665 lcc_response_free (&res); \
666 return (-1); \
667 } while (0)
669 /* If neither the values nor the names are requested, return here.. */
670 if ((ret_values == NULL) && (ret_values_names == NULL))
671 {
672 if (ret_values_num != NULL)
673 *ret_values_num = values_num;
674 lcc_response_free (&res);
675 return (0);
676 }
678 /* Allocate space for the values */
679 if (ret_values != NULL)
680 {
681 values = malloc (values_num * sizeof (*values));
682 if (values == NULL)
683 BAIL_OUT (ENOMEM);
684 }
686 if (ret_values_names != NULL)
687 {
688 values_names = calloc (values_num, sizeof (*values_names));
689 if (values_names == NULL)
690 BAIL_OUT (ENOMEM);
691 }
693 for (i = 0; i < res.lines_num; i++)
694 {
695 char *key;
696 char *value;
697 char *endptr;
699 key = res.lines[i];
700 value = strchr (key, '=');
701 if (value == NULL)
702 BAIL_OUT (EILSEQ);
704 *value = 0;
705 value++;
707 if (values != NULL)
708 {
709 endptr = NULL;
710 errno = 0;
711 values[i] = strtod (value, &endptr);
713 if ((endptr == value) || (errno != 0))
714 BAIL_OUT (errno);
715 }
717 if (values_names != NULL)
718 {
719 values_names[i] = strdup (key);
720 if (values_names[i] == NULL)
721 BAIL_OUT (ENOMEM);
722 }
723 } /* for (i = 0; i < res.lines_num; i++) */
725 if (ret_values_num != NULL)
726 *ret_values_num = values_num;
727 if (ret_values != NULL)
728 *ret_values = values;
729 if (ret_values_names != NULL)
730 *ret_values_names = values_names;
732 lcc_response_free (&res);
734 return (0);
735 } /* }}} int lcc_getval */
737 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
738 {
739 char ident_str[6 * LCC_NAME_LEN];
740 char ident_esc[12 * LCC_NAME_LEN];
741 char command[1024] = "";
742 lcc_response_t res;
743 int status;
744 size_t i;
746 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
747 || (vl->values == NULL) || (vl->values_types == NULL))
748 {
749 lcc_set_errno (c, EINVAL);
750 return (-1);
751 }
753 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
754 &vl->identifier);
755 if (status != 0)
756 return (status);
758 SSTRCATF (command, "PUTVAL %s",
759 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
761 if (vl->interval > 0.0)
762 SSTRCATF (command, " interval=%.3f", vl->interval);
764 if (vl->time > 0.0)
765 SSTRCATF (command, " %.3f", vl->time);
766 else
767 SSTRCAT (command, " N");
769 for (i = 0; i < vl->values_len; i++)
770 {
771 if (vl->values_types[i] == LCC_TYPE_COUNTER)
772 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
773 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
774 {
775 if (isnan (vl->values[i].gauge))
776 SSTRCATF (command, ":U");
777 else
778 SSTRCATF (command, ":%g", vl->values[i].gauge);
779 }
780 else if (vl->values_types[i] == LCC_TYPE_DERIVE)
781 SSTRCATF (command, ":%"PRIu64, vl->values[i].derive);
782 else if (vl->values_types[i] == LCC_TYPE_ABSOLUTE)
783 SSTRCATF (command, ":%"PRIu64, vl->values[i].absolute);
785 } /* for (i = 0; i < vl->values_len; i++) */
787 status = lcc_sendreceive (c, command, &res);
788 if (status != 0)
789 return (status);
791 if (res.status != 0)
792 {
793 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
794 lcc_response_free (&res);
795 return (-1);
796 }
798 lcc_response_free (&res);
799 return (0);
800 } /* }}} int lcc_putval */
802 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
803 lcc_identifier_t *ident, int timeout)
804 {
805 char command[1024] = "";
806 lcc_response_t res;
807 int status;
809 if (c == NULL)
810 {
811 lcc_set_errno (c, EINVAL);
812 return (-1);
813 }
815 SSTRCPY (command, "FLUSH");
817 if (timeout > 0)
818 SSTRCATF (command, " timeout=%i", timeout);
820 if (plugin != NULL)
821 {
822 char buffer[2 * LCC_NAME_LEN];
823 SSTRCATF (command, " plugin=%s",
824 lcc_strescape (buffer, plugin, sizeof (buffer)));
825 }
827 if (ident != NULL)
828 {
829 char ident_str[6 * LCC_NAME_LEN];
830 char ident_esc[12 * LCC_NAME_LEN];
832 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
833 if (status != 0)
834 return (status);
836 SSTRCATF (command, " identifier=%s",
837 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
838 }
840 status = lcc_sendreceive (c, command, &res);
841 if (status != 0)
842 return (status);
844 if (res.status != 0)
845 {
846 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
847 lcc_response_free (&res);
848 return (-1);
849 }
851 lcc_response_free (&res);
852 return (0);
853 } /* }}} int lcc_flush */
855 /* TODO: Implement lcc_putnotif */
857 int lcc_listval (lcc_connection_t *c, /* {{{ */
858 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
859 {
860 lcc_response_t res;
861 size_t i;
862 int status;
864 lcc_identifier_t *ident;
865 size_t ident_num;
867 if (c == NULL)
868 return (-1);
870 if ((ret_ident == NULL) || (ret_ident_num == NULL))
871 {
872 lcc_set_errno (c, EINVAL);
873 return (-1);
874 }
876 status = lcc_sendreceive (c, "LISTVAL", &res);
877 if (status != 0)
878 return (status);
880 if (res.status != 0)
881 {
882 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
883 lcc_response_free (&res);
884 return (-1);
885 }
887 ident_num = res.lines_num;
888 ident = malloc (ident_num * sizeof (*ident));
889 if (ident == NULL)
890 {
891 lcc_response_free (&res);
892 lcc_set_errno (c, ENOMEM);
893 return (-1);
894 }
896 for (i = 0; i < res.lines_num; i++)
897 {
898 char *time_str;
899 char *ident_str;
901 /* First field is the time. */
902 time_str = res.lines[i];
904 /* Set `ident_str' to the beginning of the second field. */
905 ident_str = time_str;
906 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
907 ident_str++;
908 while ((*ident_str == ' ') || (*ident_str == '\t'))
909 {
910 *ident_str = 0;
911 ident_str++;
912 }
914 if (*ident_str == 0)
915 {
916 lcc_set_errno (c, EILSEQ);
917 status = -1;
918 break;
919 }
921 status = lcc_string_to_identifier (c, ident + i, ident_str);
922 if (status != 0)
923 break;
924 }
926 lcc_response_free (&res);
928 if (status != 0)
929 {
930 free (ident);
931 return (-1);
932 }
934 *ret_ident = ident;
935 *ret_ident_num = ident_num;
937 return (0);
938 } /* }}} int lcc_listval */
940 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
941 {
942 if (c == NULL)
943 return ("Invalid object");
944 return (c->errbuf);
945 } /* }}} const char *lcc_strerror */
947 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
948 char *string, size_t string_size, const lcc_identifier_t *ident)
949 {
950 if ((string == NULL) || (string_size < 6) || (ident == NULL))
951 {
952 lcc_set_errno (c, EINVAL);
953 return (-1);
954 }
956 if (ident->plugin_instance[0] == 0)
957 {
958 if (ident->type_instance[0] == 0)
959 snprintf (string, string_size, "%s/%s/%s",
960 ident->host,
961 ident->plugin,
962 ident->type);
963 else
964 snprintf (string, string_size, "%s/%s/%s-%s",
965 ident->host,
966 ident->plugin,
967 ident->type,
968 ident->type_instance);
969 }
970 else
971 {
972 if (ident->type_instance[0] == 0)
973 snprintf (string, string_size, "%s/%s-%s/%s",
974 ident->host,
975 ident->plugin,
976 ident->plugin_instance,
977 ident->type);
978 else
979 snprintf (string, string_size, "%s/%s-%s/%s-%s",
980 ident->host,
981 ident->plugin,
982 ident->plugin_instance,
983 ident->type,
984 ident->type_instance);
985 }
987 string[string_size - 1] = 0;
988 return (0);
989 } /* }}} int lcc_identifier_to_string */
991 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
992 lcc_identifier_t *ident, const char *string)
993 {
994 char *string_copy;
995 char *host;
996 char *plugin;
997 char *plugin_instance;
998 char *type;
999 char *type_instance;
1001 string_copy = strdup (string);
1002 if (string_copy == NULL)
1003 {
1004 lcc_set_errno (c, ENOMEM);
1005 return (-1);
1006 }
1008 host = string_copy;
1009 plugin = strchr (host, '/');
1010 if (plugin == NULL)
1011 {
1012 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1013 free (string_copy);
1014 return (-1);
1015 }
1016 *plugin = 0;
1017 plugin++;
1019 type = strchr (plugin, '/');
1020 if (type == NULL)
1021 {
1022 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1023 free (string_copy);
1024 return (-1);
1025 }
1026 *type = 0;
1027 type++;
1029 plugin_instance = strchr (plugin, '-');
1030 if (plugin_instance != NULL)
1031 {
1032 *plugin_instance = 0;
1033 plugin_instance++;
1034 }
1036 type_instance = strchr (type, '-');
1037 if (type_instance != NULL)
1038 {
1039 *type_instance = 0;
1040 type_instance++;
1041 }
1043 memset (ident, 0, sizeof (*ident));
1045 SSTRCPY (ident->host, host);
1046 SSTRCPY (ident->plugin, plugin);
1047 if (plugin_instance != NULL)
1048 SSTRCPY (ident->plugin_instance, plugin_instance);
1049 SSTRCPY (ident->type, type);
1050 if (type_instance != NULL)
1051 SSTRCPY (ident->type_instance, type_instance);
1053 free (string_copy);
1054 return (0);
1055 } /* }}} int lcc_string_to_identifier */
1057 int lcc_identifier_compare (const void *a, /* {{{ */
1058 const void *b)
1059 {
1060 const lcc_identifier_t *i0 = a;
1061 const lcc_identifier_t *i1 = b;
1062 int status;
1064 if ((i0 == NULL) && (i1 == NULL))
1065 return (0);
1066 else if (i0 == NULL)
1067 return (-1);
1068 else if (i1 == NULL)
1069 return (1);
1071 #define CMP_FIELD(f) do { \
1072 status = strcmp (i0->f, i1->f); \
1073 if (status != 0) \
1074 return (status); \
1075 } while (0);
1077 CMP_FIELD (host);
1078 CMP_FIELD (plugin);
1079 CMP_FIELD (plugin_instance);
1080 CMP_FIELD (type);
1081 CMP_FIELD (type_instance);
1083 #undef CMP_FIELD
1085 return (0);
1086 } /* }}} int lcc_identifier_compare */
1088 int lcc_sort_identifiers (lcc_connection_t *c, /* {{{ */
1089 lcc_identifier_t *idents, size_t idents_num)
1090 {
1091 if (idents == NULL)
1092 {
1093 lcc_set_errno (c, EINVAL);
1094 return (-1);
1095 }
1097 qsort (idents, idents_num, sizeof (*idents),
1098 lcc_identifier_compare);
1099 return (0);
1100 } /* }}} int lcc_sort_identifiers */
1102 /* vim: set sw=2 sts=2 et fdm=marker : */