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)))
108 nleft = nleft - status;
113 } /* }}} ssize_t swrite */
115 static int buffer_add_string (const char *str, /* {{{ */
116 char **buffer_ret, size_t *buffer_size_ret)
124 buffer = *buffer_ret;
125 buffer_size = *buffer_size_ret;
130 while (buffer_pos < buffer_size)
134 buffer[buffer_pos] = ' ';
139 else if ((str[i] == ' ') || (str[i] == '\\'))
141 if (buffer_pos >= (buffer_size - 1))
143 buffer[buffer_pos] = '\\';
145 buffer[buffer_pos] = str[i];
150 buffer[buffer_pos] = str[i];
154 } /* while (buffer_pos < buffer_size) */
159 *buffer_ret = buffer + buffer_pos;
160 *buffer_size_ret = buffer_size - buffer_pos;
163 } /* }}} int buffer_add_string */
165 static int buffer_add_value (const char *value, /* {{{ */
166 char **buffer_ret, size_t *buffer_size_ret)
170 if (strncmp (value, "N:", 2) == 0)
171 snprintf (temp, sizeof (temp), "%lu:%s",
172 (unsigned long) time (NULL), value + 2);
174 strncpy (temp, value, sizeof (temp));
175 temp[sizeof (temp) - 1] = 0;
177 return (buffer_add_string (temp, buffer_ret, buffer_size_ret));
178 } /* }}} int buffer_add_value */
180 static int rrdc_connect_unix (const char *path) /* {{{ */
182 struct sockaddr_un sa;
185 assert (path != NULL);
187 pthread_mutex_lock (&lock);
191 pthread_mutex_unlock (&lock);
195 sd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
199 pthread_mutex_unlock (&lock);
203 memset (&sa, 0, sizeof (sa));
204 sa.sun_family = AF_UNIX;
205 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
207 status = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
211 pthread_mutex_unlock (&lock);
215 pthread_mutex_unlock (&lock);
218 } /* }}} int rrdc_connect_unix */
220 int rrdc_connect (const char *addr) /* {{{ */
222 struct addrinfo ai_hints;
223 struct addrinfo *ai_res;
224 struct addrinfo *ai_ptr;
228 addr = RRDCACHED_DEFAULT_ADDRESS;
230 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
231 return (rrdc_connect_unix (addr + strlen ("unix:")));
232 else if (addr[0] == '/')
233 return (rrdc_connect_unix (addr));
235 pthread_mutex_lock (&lock);
239 pthread_mutex_unlock (&lock);
243 memset (&ai_hints, 0, sizeof (ai_hints));
244 ai_hints.ai_flags = 0;
246 ai_hints.ai_flags |= AI_ADDRCONFIG;
248 ai_hints.ai_family = AF_UNSPEC;
249 ai_hints.ai_socktype = SOCK_STREAM;
252 status = getaddrinfo (addr, RRDCACHED_DEFAULT_PORT, &ai_hints, &ai_res);
255 pthread_mutex_unlock (&lock);
259 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
261 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
269 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
278 assert (status == 0);
281 pthread_mutex_unlock (&lock);
284 } /* }}} int rrdc_connect */
286 int rrdc_disconnect (void) /* {{{ */
288 pthread_mutex_lock (&lock);
292 pthread_mutex_unlock (&lock);
299 pthread_mutex_unlock (&lock);
302 } /* }}} int rrdc_disconnect */
304 int rrdc_update (const char *filename, int values_num, /* {{{ */
305 const char * const *values)
314 memset (buffer, 0, sizeof (buffer));
315 buffer_ptr = &buffer[0];
316 buffer_free = sizeof (buffer);
318 status = buffer_add_string ("update", &buffer_ptr, &buffer_free);
322 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
326 for (i = 0; i < values_num; i++)
328 status = buffer_add_value (values[i], &buffer_ptr, &buffer_free);
333 assert (buffer_free < sizeof (buffer));
334 buffer_size = sizeof (buffer) - buffer_free;
335 assert (buffer[buffer_size - 1] == ' ');
336 buffer[buffer_size - 1] = '\n';
338 pthread_mutex_lock (&lock);
342 pthread_mutex_unlock (&lock);
346 status = swrite (buffer, buffer_size);
349 pthread_mutex_unlock (&lock);
353 status = sread (buffer, sizeof (buffer));
357 pthread_mutex_unlock (&lock);
360 else if (status == 0)
362 pthread_mutex_unlock (&lock);
366 pthread_mutex_unlock (&lock);
368 status = atoi (buffer);
370 } /* }}} int rrdc_update */
372 int rrdc_flush (const char *filename) /* {{{ */
380 if (filename == NULL)
383 memset (buffer, 0, sizeof (buffer));
384 buffer_ptr = &buffer[0];
385 buffer_free = sizeof (buffer);
387 status = buffer_add_string ("flush", &buffer_ptr, &buffer_free);
391 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
395 assert (buffer_free < sizeof (buffer));
396 buffer_size = sizeof (buffer) - buffer_free;
397 assert (buffer[buffer_size - 1] == ' ');
398 buffer[buffer_size - 1] = '\n';
400 pthread_mutex_lock (&lock);
404 pthread_mutex_unlock (&lock);
408 status = swrite (buffer, buffer_size);
411 pthread_mutex_unlock (&lock);
415 status = sread (buffer, sizeof (buffer));
419 pthread_mutex_unlock (&lock);
422 else if (status == 0)
424 pthread_mutex_unlock (&lock);
428 pthread_mutex_unlock (&lock);
430 status = atoi (buffer);
432 } /* }}} int rrdc_flush */
435 * vim: set sw=2 sts=2 ts=8 et fdm=marker :