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 "frontend/sock.h"
32 #include "utils/channel.h"
34 #include <assert.h>
36 #include <errno.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include <unistd.h>
43 #include <sys/time.h>
44 #include <sys/types.h>
45 #include <sys/select.h>
46 #include <sys/socket.h>
47 #include <sys/un.h>
49 #include <pthread.h>
51 /*
52 * private data types
53 */
55 typedef struct {
56 int fd;
57 struct sockaddr_storage client_addr;
58 socklen_t client_addr_len;
59 } connection_t;
61 typedef struct {
62 char *address;
63 int type;
65 int sock_fd;
66 } listener_t;
68 typedef struct {
69 int type;
70 const char *prefix;
72 int (*opener)(listener_t *);
73 } fe_listener_impl_t;
75 struct sdb_fe_socket {
76 listener_t *listeners;
77 size_t listeners_num;
78 };
80 /*
81 * connection management functions
82 */
84 static int
85 open_unix_sock(listener_t *listener)
86 {
87 struct sockaddr_un sa;
88 int status;
90 listener->sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
91 if (listener->sock_fd < 0) {
92 char buf[1024];
93 sdb_log(SDB_LOG_ERR, "frontend: Failed to open UNIX socket %s: %s",
94 listener->address, sdb_strerror(errno, buf, sizeof(buf)));
95 return -1;
96 }
98 memset(&sa, 0, sizeof(sa));
99 sa.sun_family = AF_UNIX;
100 strncpy(sa.sun_path, listener->address + strlen("unix:"),
101 sizeof(sa.sun_path));
103 status = bind(listener->sock_fd, (struct sockaddr *)&sa, sizeof(sa));
104 if (status) {
105 char buf[1024];
106 sdb_log(SDB_LOG_ERR, "frontend: Failed to bind to UNIX socket %s: %s",
107 listener->address, sdb_strerror(errno, buf, sizeof(buf)));
108 return -1;
109 }
110 return 0;
111 } /* open_unix_sock */
113 /*
114 * private variables
115 */
117 /* the enum has to be sorted the same as the implementations array
118 * to ensure that the type may be used as index into the array */
119 enum {
120 LISTENER_UNIXSOCK = 0,
121 };
122 static fe_listener_impl_t listener_impls[] = {
123 { LISTENER_UNIXSOCK, "unix", open_unix_sock },
124 };
126 /*
127 * private helper functions
128 */
130 static int
131 get_type(const char *address)
132 {
133 char *sep;
134 size_t len;
135 size_t i;
137 sep = strchr(address, (int)':');
138 if (! sep)
139 return -1;
141 assert(sep > address);
142 len = (size_t)(sep - address);
144 for (i = 0; i < SDB_STATIC_ARRAY_LEN(listener_impls); ++i) {
145 fe_listener_impl_t *impl = listener_impls + i;
147 if (!strncmp(address, impl->prefix, len)) {
148 assert(impl->type == (int)i);
149 return impl->type;
150 }
151 }
152 return -1;
153 } /* get_type */
155 static void
156 listener_destroy(listener_t *listener)
157 {
158 if (! listener)
159 return;
161 if (listener->sock_fd >= 0)
162 close(listener->sock_fd);
164 if (listener->address)
165 free(listener->address);
166 } /* listener_destroy */
168 static listener_t *
169 listener_create(sdb_fe_socket_t *sock, const char *address)
170 {
171 listener_t *listener;
172 int type;
174 type = get_type(address);
175 if (type < 0) {
176 sdb_log(SDB_LOG_ERR, "frontend: Unsupported address type specified "
177 "in listen address '%s'", address);
178 return NULL;
179 }
181 listener = realloc(sock->listeners,
182 sock->listeners_num * sizeof(*sock->listeners));
183 if (! listener) {
184 char buf[1024];
185 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate memory: %s",
186 sdb_strerror(errno, buf, sizeof(buf)));
187 return NULL;
188 }
190 sock->listeners = listener;
191 listener = sock->listeners + sock->listeners_num;
193 listener->sock_fd = -1;
194 listener->address = strdup(address);
195 if (! listener->address) {
196 char buf[1024];
197 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate memory: %s",
198 sdb_strerror(errno, buf, sizeof(buf)));
199 listener_destroy(listener);
200 return NULL;
201 }
202 listener->type = type;
204 if (listener_impls[type].opener(listener)) {
205 /* prints error */
206 listener_destroy(listener);
207 return NULL;
208 }
210 ++sock->listeners_num;
211 return listener;
212 } /* listener_create */
214 /*
215 * connection handler functions
216 */
218 static void *
219 connection_handler(void *data)
220 {
221 sdb_channel_t *chan = data;
223 assert(chan);
225 while (42) {
226 struct timespec timeout = { 0, 500000000 }; /* .5 seconds */
227 connection_t conn;
228 int status;
230 errno = 0;
231 status = sdb_channel_select(chan, NULL, &conn, NULL, NULL, &timeout);
232 if (status) {
233 char buf[1024];
235 if (errno == ETIMEDOUT)
236 continue;
237 if (errno == EBADF) /* channel shut down */
238 break;
240 sdb_log(SDB_LOG_ERR, "frontend: Failed to read from channel: %s",
241 sdb_strerror(errno, buf, sizeof(buf)));
242 continue;
243 }
245 if (conn.fd < 0)
246 continue;
248 if (conn.client_addr.ss_family != AF_UNIX) {
249 sdb_log(SDB_LOG_ERR, "frontend: Accepted connection using "
250 "unexpected family type %d", conn.client_addr.ss_family);
251 continue;
252 }
254 /* XXX */
255 sdb_log(SDB_LOG_INFO, "frontend: Accepted connection on fd=%i\n",
256 conn.fd);
257 close(conn.fd);
258 }
259 return NULL;
260 } /* connection_handler */
262 /*
263 * public API
264 */
266 sdb_fe_socket_t *
267 sdb_fe_sock_create(void)
268 {
269 sdb_fe_socket_t *sock;
271 sock = calloc(1, sizeof(*sock));
272 if (! sock)
273 return NULL;
274 return sock;
275 } /* sdb_fe_sock_create */
277 void
278 sdb_fe_sock_destroy(sdb_fe_socket_t *sock)
279 {
280 size_t i;
282 if (! sock)
283 return;
285 for (i = 0; i < sock->listeners_num; ++i) {
286 listener_destroy(sock->listeners + i);
287 }
288 if (sock->listeners)
289 free(sock->listeners);
290 free(sock);
291 } /* sdb_fe_sock_destroy */
293 int
294 sdb_fe_sock_add_listener(sdb_fe_socket_t *sock, const char *address)
295 {
296 listener_t *listener;
298 if ((! sock) || (! address))
299 return -1;
301 listener = listener_create(sock, address);
302 if (! listener)
303 return -1;
304 return 0;
305 } /* sdb_fe_sock_add_listener */
307 int
308 sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
309 {
310 sdb_channel_t *chan;
311 fd_set sockets;
312 int max_fd = 0;
313 size_t i;
315 /* XXX: make the number of threads configurable */
316 pthread_t handler_threads[5];
318 if ((! sock) || (! sock->listeners_num) || (! loop))
319 return -1;
321 FD_ZERO(&sockets);
323 for (i = 0; i < sock->listeners_num; ++i) {
324 listener_t *listener = sock->listeners + i;
326 if (listen(listener->sock_fd, /* backlog = */ 32)) {
327 char buf[1024];
328 sdb_log(SDB_LOG_ERR, "frontend: Failed to listen on socket %s: %s",
329 listener->address, sdb_strerror(errno, buf, sizeof(buf)));
330 return -1;
331 }
333 FD_SET(listener->sock_fd, &sockets);
334 if (listener->sock_fd > max_fd)
335 max_fd = listener->sock_fd;
336 }
338 chan = sdb_channel_create(1024, sizeof(connection_t));
339 if (! chan)
340 return -1;
342 memset(&handler_threads, 0, sizeof(handler_threads));
343 /* XXX: error handling */
344 for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
345 pthread_create(&handler_threads[i], /* attr = */ NULL,
346 connection_handler, /* arg = */ chan);
348 while (loop->do_loop) {
349 fd_set ready = sockets;
350 int n;
352 struct timeval timeout = { 1, 0 }; /* one second */
354 errno = 0;
355 n = select(max_fd + 1, &ready, NULL, NULL, &timeout);
356 if (n < 0) {
357 char buf[1024];
359 if (errno == EINTR)
360 continue;
362 sdb_log(SDB_LOG_ERR, "frontend: Failed to monitor sockets: %s",
363 sdb_strerror(errno, buf, sizeof(buf)));
364 return -1;
365 }
367 if (! n)
368 continue;
370 for (i = 0; i < sock->listeners_num; ++i) {
371 listener_t *listener = sock->listeners + i;
373 if (FD_ISSET(listener->sock_fd, &ready)) {
374 connection_t conn;
376 memset(&conn, 0, sizeof(conn));
377 conn.client_addr_len = sizeof(conn.client_addr);
379 conn.fd = accept(listener->sock_fd,
380 (struct sockaddr *)&conn.client_addr,
381 &conn.client_addr_len);
383 if (conn.fd < 0) {
384 char buf[1024];
385 sdb_log(SDB_LOG_ERR, "frontend: Failed to accept remote "
386 "connection: %s", sdb_strerror(errno,
387 buf, sizeof(buf)));
388 continue;
389 }
391 sdb_channel_write(chan, &conn);
392 }
393 }
394 }
396 sdb_log(SDB_LOG_INFO, "frontend: Waiting for connection handler threads "
397 "to terminate");
398 if (! sdb_channel_shutdown(chan))
399 for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
400 pthread_join(handler_threads[i], NULL);
401 /* else: we tried our best; let the operating system clean up */
402 return 0;
403 } /* sdb_fe_sock_listen_and_server */
405 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */