Code

e2c52ce31b2cbf3a0dba089187de4efd8ceac5fb
[sysdb.git] / src / frontend / sock.c
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: %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, "sock: 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)
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)
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)
171         listener_t *listener;
172         int type;
174         type = get_type(address);
175         if (type < 0) {
176                 sdb_log(SDB_LOG_ERR, "sock: 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, "sock: 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, "sock: 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)
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, "sock: 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, "sock: Accepted connection using unexpected "
250                                         "family type %d", conn.client_addr.ss_family);
251                         continue;
252                 }
254                 /* XXX */
255                 sdb_log(SDB_LOG_INFO, "Accepted connection on fd=%i\n", conn.fd);
256                 close(conn.fd);
257         }
258         return NULL;
259 } /* connection_handler */
261 /*
262  * public API
263  */
265 sdb_fe_socket_t *
266 sdb_fe_sock_create(void)
268         sdb_fe_socket_t *sock;
270         sock = calloc(1, sizeof(*sock));
271         if (! sock)
272                 return NULL;
273         return sock;
274 } /* sdb_fe_sock_create */
276 void
277 sdb_fe_sock_destroy(sdb_fe_socket_t *sock)
279         size_t i;
281         if (! sock)
282                 return;
284         for (i = 0; i < sock->listeners_num; ++i) {
285                 listener_destroy(sock->listeners + i);
286         }
287         if (sock->listeners)
288                 free(sock->listeners);
289         free(sock);
290 } /* sdb_fe_sock_destroy */
292 int
293 sdb_fe_sock_add_listener(sdb_fe_socket_t *sock, const char *address)
295         listener_t *listener;
297         if ((! sock) || (! address))
298                 return -1;
300         listener = listener_create(sock, address);
301         if (! listener)
302                 return -1;
303         return 0;
304 } /* sdb_fe_sock_add_listener */
306 int
307 sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
309         sdb_channel_t *chan;
310         fd_set sockets;
311         int max_fd = 0;
312         size_t i;
314         /* XXX: make the number of threads configurable */
315         pthread_t handler_threads[5];
317         if ((! sock) || (! sock->listeners_num) || (! loop))
318                 return -1;
320         FD_ZERO(&sockets);
322         for (i = 0; i < sock->listeners_num; ++i) {
323                 listener_t *listener = sock->listeners + i;
325                 if (listen(listener->sock_fd, /* backlog = */ 32)) {
326                         char buf[1024];
327                         sdb_log(SDB_LOG_ERR, "sock: Failed to listen on socket %s: %s",
328                                         listener->address, sdb_strerror(errno, buf, sizeof(buf)));
329                         return -1;
330                 }
332                 FD_SET(listener->sock_fd, &sockets);
333                 if (listener->sock_fd > max_fd)
334                         max_fd = listener->sock_fd;
335         }
337         chan = sdb_channel_create(1024, sizeof(connection_t));
338         if (! chan)
339                 return -1;
341         memset(&handler_threads, 0, sizeof(handler_threads));
342         /* XXX: error handling */
343         for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
344                 pthread_create(&handler_threads[i], /* attr = */ NULL,
345                                 connection_handler, /* arg = */ chan);
347         while (loop->do_loop) {
348                 fd_set ready = sockets;
349                 int n;
351                 struct timeval timeout = { 1, 0 }; /* one second */
353                 errno = 0;
354                 n = select(max_fd + 1, &ready, NULL, NULL, &timeout);
355                 if (n < 0) {
356                         char buf[1024];
358                         if (errno == EINTR)
359                                 continue;
361                         sdb_log(SDB_LOG_ERR, "sock: Failed to monitor sockets: %s",
362                                         sdb_strerror(errno, buf, sizeof(buf)));
363                         return -1;
364                 }
366                 if (! n)
367                         continue;
369                 for (i = 0; i < sock->listeners_num; ++i) {
370                         listener_t *listener = sock->listeners + i;
372                         if (FD_ISSET(listener->sock_fd, &ready)) {
373                                 connection_t conn;
375                                 memset(&conn, 0, sizeof(conn));
376                                 conn.client_addr_len = sizeof(conn.client_addr);
378                                 conn.fd = accept(listener->sock_fd,
379                                                 (struct sockaddr *)&conn.client_addr,
380                                                 &conn.client_addr_len);
382                                 if (conn.fd < 0) {
383                                         char buf[1024];
384                                         sdb_log(SDB_LOG_ERR, "sock: Failed to accept remote "
385                                                         "connection: %s", sdb_strerror(errno,
386                                                                 buf, sizeof(buf)));
387                                         continue;
388                                 }
390                                 sdb_channel_write(chan, &conn);
391                         }
392                 }
393         }
395         sdb_log(SDB_LOG_INFO, "sock: Waiting for connection handler threads "
396                         "to terminate");
397         if (! sdb_channel_shutdown(chan))
398                 for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
399                         pthread_join(handler_threads[i], NULL);
400         /* else: we tried our best; let the operating system clean up */
401         return 0;
402 } /* sdb_fe_sock_listen_and_server */
404 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */