a389a142b94f8a1a609a02a2ae1773239dc0e624
1 /*
2 * SysDB - src/frontend/sock.c
3 * Copyright (C) 2013 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 #include "sysdb.h"
29 #include "core/error.h"
30 #include "core/object.h"
31 #include "frontend/sock.h"
33 #include "utils/channel.h"
34 #include "utils/llist.h"
36 #include <assert.h>
38 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
44 #include <unistd.h>
46 #include <fcntl.h>
48 #include <sys/time.h>
49 #include <sys/types.h>
50 #include <sys/select.h>
51 #include <sys/socket.h>
52 #include <sys/un.h>
54 #include <pthread.h>
56 /* name of connection objects */
57 #define CONN_FD_PREFIX "conn#"
58 #define CONN_FD_PLACEHOLDER "XXXXXXX"
60 /*
61 * private data types
62 */
64 typedef struct {
65 int fd;
66 struct sockaddr_storage client_addr;
67 socklen_t client_addr_len;
68 } connection_t;
70 typedef struct {
71 sdb_object_t super;
72 connection_t conn;
73 } connection_obj_t;
74 #define CONN(obj) ((connection_obj_t *)(obj))
76 typedef struct {
77 char *address;
78 int type;
80 int sock_fd;
81 } listener_t;
83 typedef struct {
84 int type;
85 const char *prefix;
87 int (*opener)(listener_t *);
88 } fe_listener_impl_t;
90 struct sdb_fe_socket {
91 listener_t *listeners;
92 size_t listeners_num;
94 sdb_llist_t *open_connections;
95 };
97 /*
98 * connection management functions
99 */
101 static int
102 open_unix_sock(listener_t *listener)
103 {
104 struct sockaddr_un sa;
105 int status;
107 listener->sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
108 if (listener->sock_fd < 0) {
109 char buf[1024];
110 sdb_log(SDB_LOG_ERR, "frontend: Failed to open UNIX socket %s: %s",
111 listener->address, sdb_strerror(errno, buf, sizeof(buf)));
112 return -1;
113 }
115 memset(&sa, 0, sizeof(sa));
116 sa.sun_family = AF_UNIX;
117 strncpy(sa.sun_path, listener->address + strlen("unix:"),
118 sizeof(sa.sun_path));
120 status = bind(listener->sock_fd, (struct sockaddr *)&sa, sizeof(sa));
121 if (status) {
122 char buf[1024];
123 sdb_log(SDB_LOG_ERR, "frontend: Failed to bind to UNIX socket %s: %s",
124 listener->address, sdb_strerror(errno, buf, sizeof(buf)));
125 return -1;
126 }
127 return 0;
128 } /* open_unix_sock */
130 /*
131 * private variables
132 */
134 /* the enum has to be sorted the same as the implementations array
135 * to ensure that the type may be used as index into the array */
136 enum {
137 LISTENER_UNIXSOCK = 0,
138 };
139 static fe_listener_impl_t listener_impls[] = {
140 { LISTENER_UNIXSOCK, "unix", open_unix_sock },
141 };
143 /*
144 * private helper functions
145 */
147 static int
148 get_type(const char *address)
149 {
150 char *sep;
151 size_t len;
152 size_t i;
154 sep = strchr(address, (int)':');
155 if (! sep)
156 return -1;
158 assert(sep > address);
159 len = (size_t)(sep - address);
161 for (i = 0; i < SDB_STATIC_ARRAY_LEN(listener_impls); ++i) {
162 fe_listener_impl_t *impl = listener_impls + i;
164 if (!strncmp(address, impl->prefix, len)) {
165 assert(impl->type == (int)i);
166 return impl->type;
167 }
168 }
169 return -1;
170 } /* get_type */
172 static void
173 listener_destroy(listener_t *listener)
174 {
175 if (! listener)
176 return;
178 if (listener->sock_fd >= 0)
179 close(listener->sock_fd);
181 if (listener->address)
182 free(listener->address);
183 } /* listener_destroy */
185 static listener_t *
186 listener_create(sdb_fe_socket_t *sock, const char *address)
187 {
188 listener_t *listener;
189 int type;
191 type = get_type(address);
192 if (type < 0) {
193 sdb_log(SDB_LOG_ERR, "frontend: Unsupported address type specified "
194 "in listen address '%s'", address);
195 return NULL;
196 }
198 listener = realloc(sock->listeners,
199 sock->listeners_num * sizeof(*sock->listeners));
200 if (! listener) {
201 char buf[1024];
202 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate memory: %s",
203 sdb_strerror(errno, buf, sizeof(buf)));
204 return NULL;
205 }
207 sock->listeners = listener;
208 listener = sock->listeners + sock->listeners_num;
210 listener->sock_fd = -1;
211 listener->address = strdup(address);
212 if (! listener->address) {
213 char buf[1024];
214 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate memory: %s",
215 sdb_strerror(errno, buf, sizeof(buf)));
216 listener_destroy(listener);
217 return NULL;
218 }
219 listener->type = type;
221 if (listener_impls[type].opener(listener)) {
222 /* prints error */
223 listener_destroy(listener);
224 return NULL;
225 }
227 ++sock->listeners_num;
228 return listener;
229 } /* listener_create */
231 /*
232 * private data types
233 */
235 static int
236 connection_init(sdb_object_t *obj, va_list ap)
237 {
238 connection_t *conn;
239 int sock_fd;
240 int sock_fl;
242 assert(obj);
243 conn = &CONN(obj)->conn;
245 sock_fd = va_arg(ap, int);
247 conn->client_addr_len = sizeof(conn->client_addr);
248 conn->fd = accept(sock_fd, (struct sockaddr *)&conn->client_addr,
249 &conn->client_addr_len);
251 if (conn->fd < 0) {
252 char buf[1024];
253 sdb_log(SDB_LOG_ERR, "frontend: Failed to accept remote "
254 "connection: %s", sdb_strerror(errno,
255 buf, sizeof(buf)));
256 return -1;
257 }
259 if (conn->client_addr.ss_family != AF_UNIX) {
260 sdb_log(SDB_LOG_ERR, "frontend: Accepted connection using "
261 "unexpected family type %d", conn->client_addr.ss_family);
262 return -1;
263 }
265 sock_fl = fcntl(conn->fd, F_GETFL);
266 if (fcntl(conn->fd, F_SETFL, sock_fl | O_NONBLOCK)) {
267 char buf[1024];
268 sdb_log(SDB_LOG_ERR, "frontend: Failed to switch connection conn#%i "
269 "to non-blocking mode: %s", conn->fd,
270 sdb_strerror(errno, buf, sizeof(buf)));
271 return -1;
272 }
274 sdb_log(SDB_LOG_DEBUG, "frontend: Accepted connection on fd=%i",
275 conn->fd);
277 /* update the object name */
278 snprintf(obj->name + strlen(CONN_FD_PREFIX),
279 strlen(CONN_FD_PLACEHOLDER), "%i", conn->fd);
280 return 0;
281 } /* connection_init */
283 static void
284 connection_destroy(sdb_object_t *obj)
285 {
286 connection_t *conn;
288 assert(obj);
289 conn = &CONN(obj)->conn;
291 close(conn->fd);
292 conn->fd = -1;
293 } /* connection_destroy */
295 static sdb_type_t connection_type = {
296 /* size = */ sizeof(connection_obj_t),
297 /* init = */ connection_init,
298 /* destroy = */ connection_destroy,
299 };
301 /*
302 * connection handler functions
303 */
305 static void *
306 connection_handler(void *data)
307 {
308 sdb_channel_t *chan = data;
310 assert(chan);
312 while (42) {
313 struct timespec timeout = { 0, 500000000 }; /* .5 seconds */
314 connection_obj_t *conn;
315 int status;
317 errno = 0;
318 status = sdb_channel_select(chan, NULL, &conn, NULL, NULL, &timeout);
319 if (status) {
320 char buf[1024];
322 if (errno == ETIMEDOUT)
323 continue;
324 if (errno == EBADF) /* channel shut down */
325 break;
327 sdb_log(SDB_LOG_ERR, "frontend: Failed to read from channel: %s",
328 sdb_strerror(errno, buf, sizeof(buf)));
329 continue;
330 }
332 /* XXX */
333 sdb_log(SDB_LOG_INFO, "frontend: Data available on connection fd=%i\n",
334 conn->conn.fd);
335 }
336 return NULL;
337 } /* connection_handler */
339 static int
340 accept_connection(sdb_fe_socket_t *sock, listener_t *listener)
341 {
342 sdb_object_t *obj;
344 /* the X's will be replaced with the accepted file descriptor
345 * when initializing the object */
346 obj = sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER,
347 connection_type, listener->sock_fd);
348 if (! obj)
349 return -1;
351 if (sdb_llist_append(sock->open_connections, obj)) {
352 sdb_log(SDB_LOG_ERR, "frontend: Failed to append "
353 "connection %s to list of open connections",
354 obj->name);
355 sdb_object_deref(obj);
356 return -1;
357 }
359 /* hand ownership over to the list */
360 sdb_object_deref(obj);
361 return 0;
362 } /* accept_connection */
364 /*
365 * public API
366 */
368 sdb_fe_socket_t *
369 sdb_fe_sock_create(void)
370 {
371 sdb_fe_socket_t *sock;
373 sock = calloc(1, sizeof(*sock));
374 if (! sock)
375 return NULL;
377 sock->open_connections = sdb_llist_create();
378 if (! sock->open_connections) {
379 sdb_fe_sock_destroy(sock);
380 return NULL;
381 }
382 return sock;
383 } /* sdb_fe_sock_create */
385 void
386 sdb_fe_sock_destroy(sdb_fe_socket_t *sock)
387 {
388 size_t i;
390 if (! sock)
391 return;
393 for (i = 0; i < sock->listeners_num; ++i) {
394 listener_destroy(sock->listeners + i);
395 }
396 if (sock->listeners)
397 free(sock->listeners);
398 sock->listeners = NULL;
400 sdb_llist_destroy(sock->open_connections);
401 sock->open_connections = NULL;
402 free(sock);
403 } /* sdb_fe_sock_destroy */
405 int
406 sdb_fe_sock_add_listener(sdb_fe_socket_t *sock, const char *address)
407 {
408 listener_t *listener;
410 if ((! sock) || (! address))
411 return -1;
413 listener = listener_create(sock, address);
414 if (! listener)
415 return -1;
416 return 0;
417 } /* sdb_fe_sock_add_listener */
419 int
420 sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
421 {
422 sdb_channel_t *chan;
423 fd_set sockets;
424 int max_listen_fd = 0;
425 size_t i;
427 /* XXX: make the number of threads configurable */
428 pthread_t handler_threads[5];
430 if ((! sock) || (! sock->listeners_num) || (! loop))
431 return -1;
433 FD_ZERO(&sockets);
435 for (i = 0; i < sock->listeners_num; ++i) {
436 listener_t *listener = sock->listeners + i;
438 if (listen(listener->sock_fd, /* backlog = */ 32)) {
439 char buf[1024];
440 sdb_log(SDB_LOG_ERR, "frontend: Failed to listen on socket %s: %s",
441 listener->address, sdb_strerror(errno, buf, sizeof(buf)));
442 return -1;
443 }
445 FD_SET(listener->sock_fd, &sockets);
446 if (listener->sock_fd > max_listen_fd)
447 max_listen_fd = listener->sock_fd;
448 }
450 chan = sdb_channel_create(1024, sizeof(connection_obj_t *));
451 if (! chan)
452 return -1;
454 memset(&handler_threads, 0, sizeof(handler_threads));
455 /* XXX: error handling */
456 for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
457 pthread_create(&handler_threads[i], /* attr = */ NULL,
458 connection_handler, /* arg = */ chan);
460 while (loop->do_loop) {
461 fd_set ready;
462 fd_set exceptions;
463 int max_fd;
464 int n;
466 struct timeval timeout = { 1, 0 }; /* one second */
467 sdb_llist_iter_t *iter;
469 FD_ZERO(&ready);
470 FD_ZERO(&exceptions);
472 ready = sockets;
474 max_fd = max_listen_fd;
476 iter = sdb_llist_get_iter(sock->open_connections);
477 if (! iter) {
478 sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator "
479 "for open connections");
480 return -1;
481 }
483 while (sdb_llist_iter_has_next(iter)) {
484 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
485 FD_SET(CONN(obj)->conn.fd, &ready);
486 FD_SET(CONN(obj)->conn.fd, &exceptions);
488 if (CONN(obj)->conn.fd > max_fd)
489 max_fd = CONN(obj)->conn.fd;
490 }
491 sdb_llist_iter_destroy(iter);
493 errno = 0;
494 n = select(max_fd + 1, &ready, NULL, &exceptions, &timeout);
495 if (n < 0) {
496 char buf[1024];
498 if (errno == EINTR)
499 continue;
501 sdb_log(SDB_LOG_ERR, "frontend: Failed to monitor sockets: %s",
502 sdb_strerror(errno, buf, sizeof(buf)));
503 return -1;
504 }
506 if (! n)
507 continue;
509 for (i = 0; i < sock->listeners_num; ++i) {
510 listener_t *listener = sock->listeners + i;
511 if (FD_ISSET(listener->sock_fd, &ready))
512 if (accept_connection(sock, listener))
513 continue;
514 }
516 iter = sdb_llist_get_iter(sock->open_connections);
517 if (! iter) {
518 sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator "
519 "for open connections");
520 return -1;
521 }
523 while (sdb_llist_iter_has_next(iter)) {
524 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
526 if (FD_ISSET(CONN(obj)->conn.fd, &exceptions))
527 sdb_log(SDB_LOG_INFO, "Exception on fd %d",
528 CONN(obj)->conn.fd);
530 if (FD_ISSET(CONN(obj)->conn.fd, &ready)) {
531 sdb_log(SDB_LOG_INFO, "Data on fd %d", CONN(obj)->conn.fd);
532 sdb_llist_iter_remove_current(iter);
533 sdb_channel_write(chan, &obj);
534 }
535 }
536 sdb_llist_iter_destroy(iter);
537 }
539 sdb_log(SDB_LOG_INFO, "frontend: Waiting for connection handler threads "
540 "to terminate");
541 if (! sdb_channel_shutdown(chan))
542 for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
543 pthread_join(handler_threads[i], NULL);
544 /* else: we tried our best; let the operating system clean up */
545 return 0;
546 } /* sdb_fe_sock_listen_and_server */
548 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */