1 /*
2 * SysDB - src/utils/unixsock.c
3 * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "utils/error.h"
33 #include "utils/unixsock.h"
35 #include <assert.h>
36 #include <errno.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
42 #include <string.h>
43 #include <strings.h>
45 #include <unistd.h>
47 #include <sys/socket.h>
48 #include <sys/un.h>
50 /*
51 * private data types
52 */
54 struct sdb_unixsock_client {
55 char *path;
56 FILE *fh;
58 int shutdown;
59 };
61 #define SDB_SHUT_RD (1 << SHUT_RD)
62 #define SDB_SHUT_WR (1 << SHUT_WR)
63 #define SDB_SHUT_RDWR (SDB_SHUT_RD | SDB_SHUT_WR)
65 /*
66 * private helper functions
67 */
69 static int
70 sdb_unixsock_get_column_count(const char *string, const char *delim)
71 {
72 int count = 1;
74 assert(string);
76 if ((! delim) || (*string == '\0'))
77 return 1;
79 if ((delim[0] == '\0') || (delim[1] == '\0')) {
80 while ((string = strchr(string, (int)delim[0]))) {
81 ++string;
82 ++count;
83 }
84 }
85 else {
86 while ((string = strpbrk(string, delim))) {
87 ++string;
88 ++count;
89 }
90 }
91 return count;
92 } /* sdb_unixsock_get_column_count */
94 static int
95 sdb_unixsock_client_process_one_line(sdb_unixsock_client_t *client,
96 char *line, sdb_unixsock_client_data_cb callback,
97 sdb_object_t *user_data, const char *delim,
98 int column_count, int *types)
99 {
100 sdb_data_t data[column_count];
101 char *orig_line = line;
103 int i;
105 assert(column_count > 0);
107 for (i = 0; i < column_count; ++i) {
108 char *next;
110 if (! line) { /* this must no happen */
111 sdb_log(SDB_LOG_ERR, "unixsock: Unexpected EOL while "
112 "parsing line (expected %i columns delimited by '%s'; "
113 "got %i): %s", column_count, delim,
114 /* last line number */ i, orig_line);
115 return -1;
116 }
118 if ((delim[0] == '\0') || (delim[1] == '\0'))
119 next = strchr(line, (int)delim[0]);
120 else
121 next = strpbrk(line, delim);
123 if (next) {
124 *next = '\0';
125 ++next;
126 }
128 if (sdb_data_parse(line,
129 types ? types[i] : SDB_TYPE_STRING, &data[i]))
130 return -1;
132 line = next;
133 }
135 if (callback(client, (size_t)column_count, data, user_data))
136 return -1;
137 return 0;
138 } /* sdb_unixsock_client_process_one_line */
140 /*
141 * public API
142 */
144 sdb_unixsock_client_t *
145 sdb_unixsock_client_create(const char *path)
146 {
147 sdb_unixsock_client_t *client;
149 if (! path)
150 return NULL;
152 client = malloc(sizeof(*client));
153 if (! client)
154 return NULL;
155 memset(client, 0, sizeof(*client));
156 client->fh = NULL;
158 client->path = strdup(path);
159 if (! client->path) {
160 sdb_unixsock_client_destroy(client);
161 return NULL;
162 }
164 client->shutdown = 0;
165 return client;
166 } /* sdb_unixsock_client_create */
168 int
169 sdb_unixsock_client_connect(sdb_unixsock_client_t *client)
170 {
171 struct sockaddr_un sa;
172 int fd;
174 if ((! client) || (! client->path))
175 return -1;
177 memset(&sa, 0, sizeof(sa));
179 if (client->fh)
180 fclose(client->fh);
182 fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
183 if (fd < 0) {
184 char errbuf[1024];
185 sdb_log(SDB_LOG_ERR, "unixsock: Failed to open socket: %s",
186 sdb_strerror(errno, errbuf, sizeof(errbuf)));
187 return -1;
188 }
190 sa.sun_family = AF_UNIX;
191 strncpy(sa.sun_path, client->path, sizeof(sa.sun_path));
192 sa.sun_path[sizeof(sa.sun_path) - 1] = '\0';
194 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa))) {
195 char errbuf[1024];
196 sdb_log(SDB_LOG_ERR, "unixsock: Failed to connect to %s: %s",
197 sa.sun_path, sdb_strerror(errno, errbuf, sizeof(errbuf)));
198 close(fd);
199 return -1;
200 }
202 client->fh = fdopen(fd, "r+");
203 if (! client->fh) {
204 char errbuf[1024];
205 sdb_log(SDB_LOG_ERR, "unixsock: Failed to open I/O "
206 "stream for %s: %s", sa.sun_path,
207 sdb_strerror(errno, errbuf, sizeof(errbuf)));
208 close(fd);
209 return -1;
210 }
212 /* enable line-buffering */
213 setvbuf(client->fh, NULL, _IOLBF, 0);
215 client->shutdown = 0;
216 return 0;
217 } /* sdb_unixsock_client_connect */
219 int
220 sdb_unixsock_client_send(sdb_unixsock_client_t *client,
221 const char *msg)
222 {
223 int status;
225 if ((! client) || (! client->fh))
226 return -1;
228 if (client->shutdown & SDB_SHUT_WR) /* reconnect */
229 sdb_unixsock_client_connect(client);
231 status = fprintf(client->fh, "%s\r\n", msg);
232 if (status < 0) {
233 char errbuf[1024];
234 sdb_log(SDB_LOG_ERR, "unixsock: Failed to write to "
235 "socket (%s): %s", client->path,
236 sdb_strerror(errno, errbuf, sizeof(errbuf)));
237 return status;
238 }
239 return status;
240 } /* sdb_unixsock_client_send */
242 char *
243 sdb_unixsock_client_recv(sdb_unixsock_client_t *client,
244 char *buffer, size_t buflen)
245 {
246 char *tmp;
248 if ((! client) || (! client->fh) || (! buffer))
249 return NULL;
251 if (client->shutdown & SDB_SHUT_RD) /* reconnect */
252 sdb_unixsock_client_connect(client);
254 tmp = NULL;
255 while (tmp == NULL) {
256 errno = 0;
257 tmp = fgets(buffer, (int)buflen - 1, client->fh);
258 if (! tmp) {
259 if ((errno == EAGAIN) || (errno == EINTR))
260 continue;
262 if (! feof(client->fh)) {
263 char errbuf[1024];
264 sdb_log(SDB_LOG_ERR, "unixsock: Failed to read "
265 "from socket (%s): %s", client->path,
266 sdb_strerror(errno, errbuf, sizeof(errbuf)));
267 }
268 return NULL;
269 }
270 }
271 buffer[buflen - 1] = '\0';
273 buflen = strlen(buffer);
274 while (buflen && ((buffer[buflen - 1] == '\n') || (buffer[buflen - 1] == '\r'))) {
275 buffer[buflen - 1] = '\0';
276 --buflen;
277 }
278 return buffer;
279 } /* sdb_unixsock_client_recv */
281 int
282 sdb_unixsock_client_process_lines(sdb_unixsock_client_t *client,
283 sdb_unixsock_client_data_cb callback, sdb_object_t *user_data,
284 long int max_lines, const char *delim, int n_cols, ...)
285 {
286 int *types = NULL;
287 int success = 0;
289 if ((! client) || (! client->fh) || (! callback))
290 return -1;
292 if (n_cols > 0) {
293 va_list ap;
294 int i;
296 types = calloc((size_t)n_cols, sizeof(*types));
297 if (! types)
298 return -1;
300 va_start(ap, n_cols);
302 for (i = 0; i < n_cols; ++i) {
303 types[i] = va_arg(ap, int);
305 if ((types[i] < 1) || (types[i] > SDB_TYPE_BINARY)) {
306 sdb_log(SDB_LOG_ERR, "unixsock: Unknown column "
307 "type %i while processing response from the "
308 "UNIX socket @ %s.", types[i], client->path);
309 va_end(ap);
310 free(types);
311 return -1;
312 }
313 }
315 va_end(ap);
316 }
318 while (42) {
319 char buffer[1024];
320 char *line;
322 int column_count;
324 if (! max_lines)
325 break;
327 if (max_lines > 0)
328 --max_lines;
330 sdb_unixsock_client_clearerr(client);
331 line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
333 if (! line)
334 break;
336 column_count = sdb_unixsock_get_column_count(line, delim);
338 if ((n_cols >= 0) && (n_cols != column_count)) {
339 sdb_log(SDB_LOG_ERR, "unixsock: number of columns (%i) "
340 "does not match the number of requested columns (%i) "
341 "while processing response from the UNIX socket @ %s: %s",
342 column_count, n_cols, client->path, line);
343 continue;
344 }
346 if (column_count <= 0) /* no data */
347 continue;
349 if (! sdb_unixsock_client_process_one_line(client, line, callback,
350 user_data, delim, column_count, types))
351 ++success;
352 }
354 free(types);
356 if ((max_lines > 0)
357 || ((max_lines < 0) && (! sdb_unixsock_client_eof(client)))
358 || sdb_unixsock_client_error(client)) {
359 char errbuf[1024];
360 sdb_log(SDB_LOG_ERR, "unixsock: Unexpected end of data while "
361 "reading from socket (%s): %s", client->path,
362 sdb_strerror(errno, errbuf, sizeof(errbuf)));
363 return -1;
364 }
365 if (! success)
366 return -1;
367 return 0;
368 } /* sdb_unixsock_client_process_lines */
370 int
371 sdb_unixsock_client_shutdown(sdb_unixsock_client_t *client, int how)
372 {
373 int status;
375 if (! client) {
376 errno = ENOTSOCK;
377 return -1;
378 }
380 fflush(client->fh);
381 status = shutdown(fileno(client->fh), how);
383 if (! status) {
384 if (how == SHUT_RDWR)
385 client->shutdown |= SDB_SHUT_RDWR;
386 else
387 client->shutdown |= 1 << how;
388 }
389 return status;
390 } /* sdb_unixsock_client_shutdown */
392 void
393 sdb_unixsock_client_clearerr(sdb_unixsock_client_t *client)
394 {
395 if ((! client) || (! client->fh))
396 return;
397 clearerr(client->fh);
398 } /* sdb_unixsock_client_clearerr */
400 int
401 sdb_unixsock_client_eof(sdb_unixsock_client_t *client)
402 {
403 if ((! client) || (! client->fh)) {
404 errno = EBADF;
405 return -1;
406 }
407 return feof(client->fh);
408 } /* sdb_unixsock_client_eof */
410 int
411 sdb_unixsock_client_error(sdb_unixsock_client_t *client)
412 {
413 if ((! client) || (! client->fh)) {
414 errno = EBADF;
415 return -1;
416 }
417 return ferror(client->fh);
418 } /* sdb_unixsock_client_error */
420 void
421 sdb_unixsock_client_destroy(sdb_unixsock_client_t *client)
422 {
423 if (! client)
424 return;
426 if (client->path)
427 free(client->path);
428 client->path = NULL;
430 if (client->fh)
431 fclose(client->fh);
432 client->fh = NULL;
434 free(client);
435 } /* sdb_unixsock_client_destroy */
437 const char *
438 sdb_unixsock_client_path(sdb_unixsock_client_t *client)
439 {
440 if (! client)
441 return NULL;
442 return client->path;
443 } /* sdb_unixsock_client_path */
445 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */