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;
181 assert (path != NULL);
183 pthread_mutex_lock (&lock);
187 pthread_mutex_unlock (&lock);
191 sd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
195 pthread_mutex_unlock (&lock);
199 memset (&sa, 0, sizeof (sa));
200 sa.sun_family = AF_UNIX;
201 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
203 status = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
207 pthread_mutex_unlock (&lock);
211 pthread_mutex_unlock (&lock);
214 } /* }}} int rrdc_connect_unix */
216 int rrdc_connect (const char *addr) /* {{{ */
218 struct addrinfo ai_hints;
219 struct addrinfo *ai_res;
220 struct addrinfo *ai_ptr;
224 addr = RRDCACHED_DEFAULT_ADDRESS;
226 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
227 return (rrdc_connect_unix (addr + strlen ("unix:")));
228 else if (addr[0] == '/')
229 return (rrdc_connect_unix (addr));
231 pthread_mutex_lock (&lock);
235 pthread_mutex_unlock (&lock);
239 memset (&ai_hints, 0, sizeof (ai_hints));
240 ai_hints.ai_flags = 0;
242 ai_hints.ai_flags |= AI_ADDRCONFIG;
244 ai_hints.ai_family = AF_UNSPEC;
245 ai_hints.ai_socktype = SOCK_STREAM;
248 status = getaddrinfo (addr, RRDCACHED_DEFAULT_PORT, &ai_hints, &ai_res);
251 pthread_mutex_unlock (&lock);
255 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
257 sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
265 status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
274 assert (status == 0);
277 pthread_mutex_unlock (&lock);
280 } /* }}} int rrdc_connect */
282 int rrdc_disconnect (void) /* {{{ */
284 pthread_mutex_lock (&lock);
288 pthread_mutex_unlock (&lock);
295 pthread_mutex_unlock (&lock);
298 } /* }}} int rrdc_disconnect */
300 int rrdc_update (const char *filename, int values_num, /* {{{ */
301 const char * const *values)
310 memset (buffer, 0, sizeof (buffer));
311 buffer_ptr = &buffer[0];
312 buffer_free = sizeof (buffer);
314 status = buffer_add_string ("update", &buffer_ptr, &buffer_free);
318 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
322 for (i = 0; i < values_num; i++)
324 status = buffer_add_value (values[i], &buffer_ptr, &buffer_free);
329 assert (buffer_free < sizeof (buffer));
330 buffer_size = sizeof (buffer) - buffer_free;
331 assert (buffer[buffer_size - 1] == ' ');
332 buffer[buffer_size - 1] = '\n';
334 pthread_mutex_lock (&lock);
338 pthread_mutex_unlock (&lock);
342 status = swrite (buffer, buffer_size);
345 pthread_mutex_unlock (&lock);
349 status = sread (buffer, sizeof (buffer));
353 pthread_mutex_unlock (&lock);
356 else if (status == 0)
358 pthread_mutex_unlock (&lock);
362 pthread_mutex_unlock (&lock);
364 status = atoi (buffer);
366 } /* }}} int rrdc_update */
368 int rrdc_flush (const char *filename) /* {{{ */
376 if (filename == NULL)
379 memset (buffer, 0, sizeof (buffer));
380 buffer_ptr = &buffer[0];
381 buffer_free = sizeof (buffer);
383 status = buffer_add_string ("flush", &buffer_ptr, &buffer_free);
387 status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
391 assert (buffer_free < sizeof (buffer));
392 buffer_size = sizeof (buffer) - buffer_free;
393 assert (buffer[buffer_size - 1] == ' ');
394 buffer[buffer_size - 1] = '\n';
396 pthread_mutex_lock (&lock);
400 pthread_mutex_unlock (&lock);
404 status = swrite (buffer, buffer_size);
407 pthread_mutex_unlock (&lock);
411 status = sread (buffer, sizeof (buffer));
415 pthread_mutex_unlock (&lock);
418 else if (status == 0)
420 pthread_mutex_unlock (&lock);
424 pthread_mutex_unlock (&lock);
426 status = atoi (buffer);
428 } /* }}} int rrdc_flush */
431 * vim: set sw=2 sts=2 ts=8 et fdm=marker :