Initial commit: rrdd, rrdc, librrdc.so
[rrdd.git] / src / librrdc.c
1 /**
2  * collectd - src/rrdc.c
3  * Copyright (C) 2008 Florian octo Forster
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "rrdd.h"
23 #include "rrdc.h"
24
25 #define SOCK_TEMPLATE "/tmp/rrdc.sock.XXXXXX"
26
27 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
28 static int sd;
29 static FILE *sh;
30
31 int rrdd_connect (const char *path)
32 {
33   struct sockaddr_un sa;
34   int status;
35
36   pthread_mutex_lock (&lock);
37
38   if (sh != NULL)
39   {
40     pthread_mutex_unlock (&lock);
41     return (0);
42   }
43
44   sd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
45   if (sd < 0)
46   {
47     status = errno;
48     pthread_mutex_unlock (&lock);
49     return (status);
50   }
51
52   memset (&sa, 0, sizeof (sa));
53   sa.sun_family = AF_UNIX;
54   strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
55
56   status = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
57   if (status != 0)
58   {
59     status = errno;
60     pthread_mutex_unlock (&lock);
61     return (status);
62   }
63
64   sh = fdopen (sd, "w+");
65   if (sh == NULL)
66   {
67     status = errno;
68     close (sd);
69     sd = -1;
70     pthread_mutex_unlock (&lock);
71     return (status);
72   }
73
74   pthread_mutex_unlock (&lock);
75
76   return (0);
77 } /* int rrdd_connect */
78
79 int rrdd_disconnect (void)
80 {
81   int status;
82
83   pthread_mutex_lock (&lock);
84
85   if (sh == NULL)
86   {
87     pthread_mutex_unlock (&lock);
88     return (-1);
89   }
90
91   status = fclose (sh);
92   if (status != 0)
93     status = errno;
94
95   sh = NULL;
96   sd = -1;
97
98   pthread_mutex_unlock (&lock);
99
100   return (status);
101 } /* int rrdd_disconnect */
102
103 int rrdd_update (const char *filename, int values_num,
104                 const char * const *values)
105 {
106   pthread_mutex_lock (&lock);
107
108   if (sh == NULL)
109   {
110     pthread_mutex_unlock (&lock);
111     return (-1);
112   }
113
114   pthread_mutex_unlock (&lock);
115
116   return (0);
117 } /* int rrd_update_daemon */
118
119 /*
120  * vim: set sw=2 sts=2 ts=8 et fdm=marker :
121  */