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, "sock: Failed to open UNIX socket: %s",
94 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, "sock: Failed to bind to UNIX socket: %s",
107 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 return NULL;
178 listener = realloc(sock->listeners,
179 sock->listeners_num * sizeof(*sock->listeners));
180 if (! listener)
181 return NULL;
182 sock->listeners = listener;
183 listener = sock->listeners + sock->listeners_num;
185 listener->sock_fd = -1;
186 listener->address = strdup(address);
187 if (! listener->address) {
188 listener_destroy(listener);
189 return NULL;
190 }
191 listener->type = type;
193 if (listener_impls[type].opener(listener)) {
194 listener_destroy(listener);
195 return NULL;
196 }
198 ++sock->listeners_num;
199 return listener;
200 } /* listener_create */
202 /*
203 * connection handler functions
204 */
206 static void *
207 connection_handler(void *data)
208 {
209 sdb_channel_t *chan = data;
211 assert(chan);
213 while (42) {
214 connection_t conn;
216 sdb_channel_select(chan, NULL, &conn, NULL, NULL, NULL);
217 if (conn.fd < 0)
218 continue;
220 if (conn.client_addr.ss_family != AF_UNIX) {
221 sdb_log(SDB_LOG_ERR, "Accepted connection using unexpected "
222 "family type %d", conn.client_addr.ss_family);
223 continue;
224 }
226 /* XXX */
227 sdb_log(SDB_LOG_INFO, "Accepted connection on fd=%i\n", conn.fd);
228 close(conn.fd);
229 }
230 return NULL;
231 } /* connection_handler */
233 /*
234 * public API
235 */
237 sdb_fe_socket_t *
238 sdb_fe_sock_create(void)
239 {
240 sdb_fe_socket_t *sock;
242 sock = calloc(1, sizeof(*sock));
243 if (! sock)
244 return NULL;
245 return sock;
246 } /* sdb_fe_sock_create */
248 void
249 sdb_fe_sock_destroy(sdb_fe_socket_t *sock)
250 {
251 size_t i;
253 if (! sock)
254 return;
256 for (i = 0; i < sock->listeners_num; ++i) {
257 listener_destroy(sock->listeners + i);
258 }
259 if (sock->listeners)
260 free(sock->listeners);
261 free(sock);
262 } /* sdb_fe_sock_destroy */
264 int
265 sdb_fe_sock_add_listener(sdb_fe_socket_t *sock, const char *address)
266 {
267 listener_t *listener;
269 if ((! sock) || (! address))
270 return -1;
272 listener = listener_create(sock, address);
273 if (! listener)
274 return -1;
275 return 0;
276 } /* sdb_fe_sock_add_listener */
278 int
279 sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
280 {
281 sdb_channel_t *chan;
282 fd_set sockets;
283 int max_fd = 0;
284 size_t i;
286 /* XXX: make the number of threads configurable */
287 pthread_t handler_threads[5];
289 if ((! sock) || (! sock->listeners_num) || (! loop))
290 return -1;
292 FD_ZERO(&sockets);
294 for (i = 0; i < sock->listeners_num; ++i) {
295 listener_t *listener = sock->listeners + i;
297 if (listen(listener->sock_fd, /* backlog = */ 32)) {
298 char buf[1024];
299 sdb_log(SDB_LOG_ERR, "sock: Failed to listen on socket %s: %s",
300 listener->address, sdb_strerror(errno, buf, sizeof(buf)));
301 return -1;
302 }
304 FD_SET(listener->sock_fd, &sockets);
305 if (listener->sock_fd > max_fd)
306 max_fd = listener->sock_fd;
307 }
309 chan = sdb_channel_create(1024, sizeof(connection_t));
310 if (! chan)
311 return -1;
313 memset(&handler_threads, 0, sizeof(handler_threads));
314 /* XXX: error handling */
315 for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
316 pthread_create(&handler_threads[i], /* attr = */ NULL,
317 connection_handler, /* arg = */ chan);
319 while (loop->do_loop) {
320 fd_set ready = sockets;
321 int n;
323 struct timeval timeout = { 1, 0 }; /* one second */
325 errno = 0;
326 n = select(max_fd + 1, &ready, NULL, NULL, &timeout);
327 if (n < 0) {
328 char buf[1024];
330 if (errno == EINTR)
331 continue;
333 sdb_log(SDB_LOG_ERR, "sock: Failed to monitor sockets: %s",
334 sdb_strerror(errno, buf, sizeof(buf)));
335 return -1;
336 }
338 if (! n)
339 continue;
341 for (i = 0; i < sock->listeners_num; ++i) {
342 listener_t *listener = sock->listeners + i;
344 if (FD_ISSET(listener->sock_fd, &ready)) {
345 connection_t conn;
347 memset(&conn, 0, sizeof(conn));
348 conn.client_addr_len = sizeof(conn.client_addr);
350 conn.fd = accept(listener->sock_fd,
351 (struct sockaddr *)&conn.client_addr,
352 &conn.client_addr_len);
354 if (conn.fd < 0) {
355 char buf[1024];
356 sdb_log(SDB_LOG_ERR, "sock: Failed to accept remote "
357 "connection: %s", sdb_strerror(errno,
358 buf, sizeof(buf)));
359 continue;
360 }
362 sdb_channel_write(chan, &conn);
363 }
364 }
365 }
367 for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
368 pthread_cancel(handler_threads[i]);
369 for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
370 pthread_join(handler_threads[i], NULL);
371 return 0;
372 } /* sdb_fe_sock_listen_and_server */
374 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */