unixsock plugin: Use `src/utils_cmd_listval.[ch]' and remove the local cache.
[collectd.git] / src / unixsock.c
1 /**
2  * collectd - src/unixsock.c
3  * Copyright (C) 2007,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  * Author:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26
27 #include "utils_cmd_flush.h"
28 #include "utils_cmd_getval.h"
29 #include "utils_cmd_listval.h"
30 #include "utils_cmd_putval.h"
31 #include "utils_cmd_putnotif.h"
32
33 /* Folks without pthread will need to disable this plugin. */
34 #include <pthread.h>
35
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 #include <sys/un.h>
39
40 #include <grp.h>
41
42 #ifndef UNIX_PATH_MAX
43 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
44 #endif
45
46 #define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
47
48 /*
49  * Private variables
50  */
51 /* valid configuration file keys */
52 static const char *config_keys[] =
53 {
54         "SocketFile",
55         "SocketGroup",
56         "SocketPerms"
57 };
58 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
59
60 static int loop = 0;
61
62 /* socket configuration */
63 static int   sock_fd    = -1;
64 static char *sock_file  = NULL;
65 static char *sock_group = NULL;
66 static int   sock_perms = S_IRWXU | S_IRWXG;
67
68 static pthread_t listen_thread = (pthread_t) 0;
69
70 /*
71  * Functions
72  */
73 static int us_open_socket (void)
74 {
75         struct sockaddr_un sa;
76         int status;
77
78         sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
79         if (sock_fd < 0)
80         {
81                 char errbuf[1024];
82                 ERROR ("unixsock plugin: socket failed: %s",
83                                 sstrerror (errno, errbuf, sizeof (errbuf)));
84                 return (-1);
85         }
86
87         memset (&sa, '\0', sizeof (sa));
88         sa.sun_family = AF_UNIX;
89         strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
90                         sizeof (sa.sun_path) - 1);
91         /* unlink (sa.sun_path); */
92
93         DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
94
95         status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
96         if (status != 0)
97         {
98                 char errbuf[1024];
99                 sstrerror (errno, errbuf, sizeof (errbuf));
100                 ERROR ("unixsock plugin: bind failed: %s", errbuf);
101                 close (sock_fd);
102                 sock_fd = -1;
103                 return (-1);
104         }
105
106         chmod (sa.sun_path, sock_perms);
107
108         status = listen (sock_fd, 8);
109         if (status != 0)
110         {
111                 char errbuf[1024];
112                 ERROR ("unixsock plugin: listen failed: %s",
113                                 sstrerror (errno, errbuf, sizeof (errbuf)));
114                 close (sock_fd);
115                 sock_fd = -1;
116                 return (-1);
117         }
118
119         do
120         {
121                 char *grpname;
122                 struct group *g;
123                 struct group sg;
124                 char grbuf[2048];
125
126                 grpname = (sock_group != NULL) ? sock_group : COLLECTD_GRP_NAME;
127                 g = NULL;
128
129                 status = getgrnam_r (grpname, &sg, grbuf, sizeof (grbuf), &g);
130                 if (status != 0)
131                 {
132                         char errbuf[1024];
133                         WARNING ("unixsock plugin: getgrnam_r (%s) failed: %s", grpname,
134                                         sstrerror (errno, errbuf, sizeof (errbuf)));
135                         break;
136                 }
137                 if (g == NULL)
138                 {
139                         WARNING ("unixsock plugin: No such group: `%s'",
140                                         grpname);
141                         break;
142                 }
143
144                 if (chown ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
145                                         (uid_t) -1, g->gr_gid) != 0)
146                 {
147                         char errbuf[1024];
148                         WARNING ("unixsock plugin: chown (%s, -1, %i) failed: %s",
149                                         (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
150                                         (int) g->gr_gid,
151                                         sstrerror (errno, errbuf, sizeof (errbuf)));
152                 }
153         } while (0);
154
155         return (0);
156 } /* int us_open_socket */
157
158 static void *us_handle_client (void *arg)
159 {
160         int fd;
161         FILE *fh;
162         char buffer[1024];
163         char *fields[128];
164         int   fields_num;
165
166         fd = *((int *) arg);
167         free (arg);
168         arg = NULL;
169
170         DEBUG ("Reading from fd #%i", fd);
171
172         fh = fdopen (fd, "r+");
173         if (fh == NULL)
174         {
175                 char errbuf[1024];
176                 ERROR ("unixsock plugin: fdopen failed: %s",
177                                 sstrerror (errno, errbuf, sizeof (errbuf)));
178                 close (fd);
179                 pthread_exit ((void *) 1);
180         }
181
182         while (fgets (buffer, sizeof (buffer), fh) != NULL)
183         {
184                 int len;
185
186                 len = strlen (buffer);
187                 while ((len > 0)
188                                 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
189                         buffer[--len] = '\0';
190
191                 if (len == 0)
192                         continue;
193
194                 DEBUG ("fgets -> buffer = %s; len = %i;", buffer, len);
195
196                 fields_num = strsplit (buffer, fields,
197                                 sizeof (fields) / sizeof (fields[0]));
198
199                 if (fields_num < 1)
200                 {
201                         close (fd);
202                         break;
203                 }
204
205                 if (strcasecmp (fields[0], "getval") == 0)
206                 {
207                         handle_getval (fh, fields, fields_num);
208                 }
209                 else if (strcasecmp (fields[0], "putval") == 0)
210                 {
211                         handle_putval (fh, fields, fields_num);
212                 }
213                 else if (strcasecmp (fields[0], "listval") == 0)
214                 {
215                         handle_listval (fh, fields, fields_num);
216                 }
217                 else if (strcasecmp (fields[0], "putnotif") == 0)
218                 {
219                         handle_putnotif (fh, fields, fields_num);
220                 }
221                 else if (strcasecmp (fields[0], "flush") == 0)
222                 {
223                         handle_flush (fh, fields, fields_num);
224                 }
225                 else
226                 {
227                         fprintf (fh, "-1 Unknown command: %s\n", fields[0]);
228                         fflush (fh);
229                 }
230         } /* while (fgets) */
231
232         DEBUG ("Exiting..");
233         close (fd);
234
235         pthread_exit ((void *) 0);
236         return ((void *) 0);
237 } /* void *us_handle_client */
238
239 static void *us_server_thread (void *arg)
240 {
241         int  status;
242         int *remote_fd;
243         pthread_t th;
244         pthread_attr_t th_attr;
245
246         if (us_open_socket () != 0)
247                 pthread_exit ((void *) 1);
248
249         while (loop != 0)
250         {
251                 DEBUG ("unixsock plugin: Calling accept..");
252                 status = accept (sock_fd, NULL, NULL);
253                 if (status < 0)
254                 {
255                         char errbuf[1024];
256
257                         if (errno == EINTR)
258                                 continue;
259
260                         ERROR ("unixsock plugin: accept failed: %s",
261                                         sstrerror (errno, errbuf, sizeof (errbuf)));
262                         close (sock_fd);
263                         sock_fd = -1;
264                         pthread_exit ((void *) 1);
265                 }
266
267                 remote_fd = (int *) malloc (sizeof (int));
268                 if (remote_fd == NULL)
269                 {
270                         char errbuf[1024];
271                         WARNING ("unixsock plugin: malloc failed: %s",
272                                         sstrerror (errno, errbuf, sizeof (errbuf)));
273                         close (status);
274                         continue;
275                 }
276                 *remote_fd = status;
277
278                 DEBUG ("Spawning child to handle connection on fd #%i", *remote_fd);
279
280                 pthread_attr_init (&th_attr);
281                 pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
282
283                 status = pthread_create (&th, &th_attr, us_handle_client, (void *) remote_fd);
284                 if (status != 0)
285                 {
286                         char errbuf[1024];
287                         WARNING ("unixsock plugin: pthread_create failed: %s",
288                                         sstrerror (errno, errbuf, sizeof (errbuf)));
289                         close (*remote_fd);
290                         free (remote_fd);
291                         continue;
292                 }
293         } /* while (loop) */
294
295         close (sock_fd);
296         sock_fd = -1;
297
298         status = unlink ((sock_file != NULL) ? sock_file : US_DEFAULT_PATH);
299         if (status != 0)
300         {
301                 char errbuf[1024];
302                 NOTICE ("unixsock plugin: unlink (%s) failed: %s",
303                                 (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
304                                 sstrerror (errno, errbuf, sizeof (errbuf)));
305         }
306
307         return ((void *) 0);
308 } /* void *us_server_thread */
309
310 static int us_config (const char *key, const char *val)
311 {
312         if (strcasecmp (key, "SocketFile") == 0)
313         {
314                 char *new_sock_file = strdup (val);
315                 if (new_sock_file == NULL)
316                         return (1);
317
318                 sfree (sock_file);
319                 sock_file = new_sock_file;
320         }
321         else if (strcasecmp (key, "SocketGroup") == 0)
322         {
323                 char *new_sock_group = strdup (val);
324                 if (new_sock_group == NULL)
325                         return (1);
326
327                 sfree (sock_group);
328                 sock_group = new_sock_group;
329         }
330         else if (strcasecmp (key, "SocketPerms") == 0)
331         {
332                 sock_perms = (int) strtol (val, NULL, 8);
333         }
334         else
335         {
336                 return (-1);
337         }
338
339         return (0);
340 } /* int us_config */
341
342 static int us_init (void)
343 {
344         int status;
345
346         loop = 1;
347
348         status = pthread_create (&listen_thread, NULL, us_server_thread, NULL);
349         if (status != 0)
350         {
351                 char errbuf[1024];
352                 ERROR ("unixsock plugin: pthread_create failed: %s",
353                                 sstrerror (errno, errbuf, sizeof (errbuf)));
354                 return (-1);
355         }
356
357         return (0);
358 } /* int us_init */
359
360 static int us_shutdown (void)
361 {
362         void *ret;
363
364         loop = 0;
365
366         if (listen_thread != (pthread_t) 0)
367         {
368                 pthread_kill (listen_thread, SIGTERM);
369                 pthread_join (listen_thread, &ret);
370                 listen_thread = (pthread_t) 0;
371         }
372
373         plugin_unregister_init ("unixsock");
374         plugin_unregister_write ("unixsock");
375         plugin_unregister_shutdown ("unixsock");
376
377         return (0);
378 } /* int us_shutdown */
379
380 void module_register (void)
381 {
382         plugin_register_config ("unixsock", us_config,
383                         config_keys, config_keys_num);
384         plugin_register_init ("unixsock", us_init);
385         plugin_register_shutdown ("unixsock", us_shutdown);
386 } /* void module_register (void) */
387
388 /* vim: set sw=4 ts=4 sts=4 tw=78 : */