2 * RRDTool - src/rrd_client.c
3 * Copyright (C) 2008 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
23 #include "rrd_client.h"
30 #include <sys/types.h>
31 #include <sys/socket.h>
35 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
38 static ssize_t sread (void *buffer_void, size_t buffer_size) /* {{{ */
45 buffer = (char *) buffer_void;
47 buffer_free = buffer_size;
49 while (buffer_free > 0)
51 status = read (sd, buffer + buffer_used, buffer_free);
52 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
66 assert ((0 > status) || (buffer_free >= (size_t) status));
68 buffer_free = buffer_free - status;
69 buffer_used = buffer_used + status;
71 if (buffer[buffer_used - 1] == '\n')
75 if (buffer[buffer_used - 1] != '\n')
81 buffer[buffer_used - 1] = 0;
83 } /* }}} ssize_t sread */
85 static ssize_t swrite (const void *buf, size_t count) /* {{{ */
91 ptr = (const char *) buf;
96 status = write (sd, (const void *) ptr, nleft);
98 if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
104 nleft = nleft - status;
109 } /* }}} ssize_t swrite */
111 static int buffer_add_string (const char *str, /* {{{ */
112 char **buffer_ret, size_t *buffer_size_ret)
120 buffer = *buffer_ret;
121 buffer_size = *buffer_size_ret;
126 while (buffer_pos < buffer_size)
130 buffer[buffer_pos] = ' ';
135 else if ((str[i] == ' ') || (str[i] == '\\'))
137 if (buffer_pos >= (buffer_size - 1))
139 buffer[buffer_pos] = '\\';
141 buffer[buffer_pos] = str[i];
146 buffer[buffer_pos] = str[i];
150 } /* while (buffer_pos < buffer_size) */
155 *buffer_ret = buffer + buffer_pos;
156 *buffer_size_ret = buffer_size - buffer_pos;
159 } /* }}} int buffer_add_string */
161 static int buffer_add_value (const char *value, /* {{{ */
162 char **buffer_ret, size_t *buffer_size_ret)
166 if (strncmp (value, "N:", 2) == 0)
167 snprintf (temp, sizeof (temp), "%lu:%s",
168 (unsigned long) time (NULL), value + 2);
170 strncpy (temp, value, sizeof (temp));
171 temp[sizeof (temp) - 1] = 0;
173 return (buffer_add_string (temp, buffer_ret, buffer_size_ret));
174 } /* }}} int buffer_add_value */
176 static int rrdc_connect_unix (const char *path) /* {{{ */
178 struct sockaddr_un sa;
182 path = RRDD_SOCK_PATH;
184 pthread_mutex_lock (&lock);
188 pthread_mutex_unlock (&lock);
192 sd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
196 pthread_mutex_unlock (&lock);
200 memset (&sa, 0, sizeof (sa));
201 sa.sun_family = AF_UNIX;
202 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
204 status = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
208 pthread_mutex_unlock (&lock);
212 pthread_mutex_unlock (&lock);
215 } /* }}} int rrdc_connect_unix */
217 int rrdc_connect (const char *addr) /* {{{ */
219 struct addrinfo ai_hints;
220 struct addrinfo *ai_res;
221 struct addrinfo *ai_ptr;
225 addr = RRDD_SOCK_PATH;
227 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
228 return (rrdc_connect_unix (addr + strlen ("unix:")));
229 else if (addr[0] == '/')
230 return (rrdc_connect_unix (addr));
232 pthread_mutex_lock (&lock);
236 pthread_mutex_unlock (&lock);
240 memset (&ai_hints, 0, sizeof (ai_hints));
241 ai_hints.ai_flags = 0;
243 ai_hints.ai_flags |= AI_ADDRCONFIG;
245 ai_hints.ai_family = AF_UNSPEC;
246 ai_hints.ai_socktype = SOCK_STREAM;
249 status = getaddrinfo (addr, DEFAULT_PORT, &ai_hints, &ai_res);
252 pthread_mutex_unlock (&lock);
256 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
258 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
266 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
275 assert (status == 0);
278 pthread_mutex_unlock (&lock);
281 } /* }}} int rrdc_connect */
283 int rrdc_disconnect (void) /* {{{ */
285 pthread_mutex_lock (&lock);
289 pthread_mutex_unlock (&lock);
296 pthread_mutex_unlock (&lock);
299 } /* }}} int rrdc_disconnect */
301 int rrdc_update (const char *filename, int values_num, /* {{{ */
302 const char * const *values)
311 memset (buffer, 0, sizeof (buffer));
312 buffer_ptr = &buffer[0];
313 buffer_free = sizeof (buffer);
315 status = buffer_add_string ("update", &buffer_ptr, &buffer_free);
319 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
323 for (i = 0; i < values_num; i++)
325 status = buffer_add_value (values[i], &buffer_ptr, &buffer_free);
330 assert (buffer_free < sizeof (buffer));
331 buffer_size = sizeof (buffer) - buffer_free;
332 assert (buffer[buffer_size - 1] == ' ');
333 buffer[buffer_size - 1] = '\n';
335 pthread_mutex_lock (&lock);
339 pthread_mutex_unlock (&lock);
343 status = swrite (buffer, buffer_size);
346 pthread_mutex_unlock (&lock);
350 status = sread (buffer, sizeof (buffer));
354 pthread_mutex_unlock (&lock);
357 else if (status == 0)
359 pthread_mutex_unlock (&lock);
363 pthread_mutex_unlock (&lock);
365 status = atoi (buffer);
367 } /* }}} int rrdc_update */
369 int rrdc_flush (const char *filename) /* {{{ */
377 if (filename == NULL)
380 memset (buffer, 0, sizeof (buffer));
381 buffer_ptr = &buffer[0];
382 buffer_free = sizeof (buffer);
384 status = buffer_add_string ("flush", &buffer_ptr, &buffer_free);
388 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
392 assert (buffer_free < sizeof (buffer));
393 buffer_size = sizeof (buffer) - buffer_free;
394 assert (buffer[buffer_size - 1] == ' ');
395 buffer[buffer_size - 1] = '\n';
397 pthread_mutex_lock (&lock);
401 pthread_mutex_unlock (&lock);
405 status = swrite (buffer, buffer_size);
408 pthread_mutex_unlock (&lock);
412 status = sread (buffer, sizeof (buffer));
416 pthread_mutex_unlock (&lock);
419 else if (status == 0)
421 pthread_mutex_unlock (&lock);
425 pthread_mutex_unlock (&lock);
427 status = atoi (buffer);
429 } /* }}} int rrdc_flush */
432 * vim: set sw=2 sts=2 ts=8 et fdm=marker :