063b2ca3d1d6528d0a22c7304faab65408e398c2
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 struct timespec timeout = { 0, 500000000 }; /* .5 seconds */
215 connection_t conn;
216 int status;
218 errno = 0;
219 status = sdb_channel_select(chan, NULL, &conn, NULL, NULL, &timeout);
220 if (status) {
221 char buf[1024];
223 if (errno == ETIMEDOUT)
224 continue;
225 if (errno == EBADF) /* channel shut down */
226 break;
228 sdb_log(SDB_LOG_ERR, "sock: Failed to read from channel: %s",
229 sdb_strerror(errno, buf, sizeof(buf)));
230 continue;
231 }
233 if (conn.fd < 0)
234 continue;
236 if (conn.client_addr.ss_family != AF_UNIX) {
237 sdb_log(SDB_LOG_ERR, "sock: Accepted connection using unexpected "
238 "family type %d", conn.client_addr.ss_family);
239 continue;
240 }
242 /* XXX */
243 sdb_log(SDB_LOG_INFO, "Accepted connection on fd=%i\n", conn.fd);
244 close(conn.fd);
245 }
246 return NULL;
247 } /* connection_handler */
249 /*
250 * public API
251 */
253 sdb_fe_socket_t *
254 sdb_fe_sock_create(void)
255 {
256 sdb_fe_socket_t *sock;
258 sock = calloc(1, sizeof(*sock));
259 if (! sock)
260 return NULL;
261 return sock;
262 } /* sdb_fe_sock_create */
264 void
265 sdb_fe_sock_destroy(sdb_fe_socket_t *sock)
266 {
267 size_t i;
269 if (! sock)
270 return;
272 for (i = 0; i < sock->listeners_num; ++i) {
273 listener_destroy(sock->listeners + i);
274 }
275 if (sock->listeners)
276 free(sock->listeners);
277 free(sock);
278 } /* sdb_fe_sock_destroy */
280 int
281 sdb_fe_sock_add_listener(sdb_fe_socket_t *sock, const char *address)
282 {
283 listener_t *listener;
285 if ((! sock) || (! address))
286 return -1;
288 listener = listener_create(sock, address);
289 if (! listener)
290 return -1;
291 return 0;
292 } /* sdb_fe_sock_add_listener */
294 int
295 sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
296 {
297 sdb_channel_t *chan;
298 fd_set sockets;
299 int max_fd = 0;
300 size_t i;
302 /* XXX: make the number of threads configurable */
303 pthread_t handler_threads[5];
305 if ((! sock) || (! sock->listeners_num) || (! loop))
306 return -1;
308 FD_ZERO(&sockets);
310 for (i = 0; i < sock->listeners_num; ++i) {
311 listener_t *listener = sock->listeners + i;
313 if (listen(listener->sock_fd, /* backlog = */ 32)) {
314 char buf[1024];
315 sdb_log(SDB_LOG_ERR, "sock: Failed to listen on socket %s: %s",
316 listener->address, sdb_strerror(errno, buf, sizeof(buf)));
317 return -1;
318 }
320 FD_SET(listener->sock_fd, &sockets);
321 if (listener->sock_fd > max_fd)
322 max_fd = listener->sock_fd;
323 }
325 chan = sdb_channel_create(1024, sizeof(connection_t));
326 if (! chan)
327 return -1;
329 memset(&handler_threads, 0, sizeof(handler_threads));
330 /* XXX: error handling */
331 for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
332 pthread_create(&handler_threads[i], /* attr = */ NULL,
333 connection_handler, /* arg = */ chan);
335 while (loop->do_loop) {
336 fd_set ready = sockets;
337 int n;
339 struct timeval timeout = { 1, 0 }; /* one second */
341 errno = 0;
342 n = select(max_fd + 1, &ready, NULL, NULL, &timeout);
343 if (n < 0) {
344 char buf[1024];
346 if (errno == EINTR)
347 continue;
349 sdb_log(SDB_LOG_ERR, "sock: Failed to monitor sockets: %s",
350 sdb_strerror(errno, buf, sizeof(buf)));
351 return -1;
352 }
354 if (! n)
355 continue;
357 for (i = 0; i < sock->listeners_num; ++i) {
358 listener_t *listener = sock->listeners + i;
360 if (FD_ISSET(listener->sock_fd, &ready)) {
361 connection_t conn;
363 memset(&conn, 0, sizeof(conn));
364 conn.client_addr_len = sizeof(conn.client_addr);
366 conn.fd = accept(listener->sock_fd,
367 (struct sockaddr *)&conn.client_addr,
368 &conn.client_addr_len);
370 if (conn.fd < 0) {
371 char buf[1024];
372 sdb_log(SDB_LOG_ERR, "sock: Failed to accept remote "
373 "connection: %s", sdb_strerror(errno,
374 buf, sizeof(buf)));
375 continue;
376 }
378 sdb_channel_write(chan, &conn);
379 }
380 }
381 }
383 sdb_log(SDB_LOG_INFO, "sock: Waiting for connection handler threads "
384 "to terminate");
385 if (! sdb_channel_shutdown(chan))
386 for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
387 pthread_join(handler_threads[i], NULL);
388 /* else: we tried our best; let the operating system clean up */
389 return 0;
390 } /* sdb_fe_sock_listen_and_server */
392 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */