Code

frontend: Trigger the main thread after putting a connection back in the list.
[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 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/object.h"
34 #include "frontend/connection-private.h"
35 #include "frontend/sock.h"
37 #include "utils/channel.h"
38 #include "utils/error.h"
39 #include "utils/llist.h"
40 #include "utils/os.h"
41 #include "utils/strbuf.h"
43 #include <assert.h>
44 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
50 #include <unistd.h>
52 #include <sys/time.h>
53 #include <sys/types.h>
54 #include <sys/select.h>
55 #include <sys/socket.h>
56 #include <sys/param.h>
57 #include <sys/un.h>
59 #ifdef HAVE_UCRED_H
60 #       include <ucred.h>
61 #endif
62 #ifdef HAVE_SYS_UCRED_H
63 #       include <sys/ucred.h>
64 #endif
66 #include <pwd.h>
68 #include <fcntl.h>
69 #include <netdb.h>
70 #include <libgen.h>
71 #include <pthread.h>
73 /*
74  * private data types
75  */
77 typedef struct {
78         char *address;
79         int   type;
81         int sock_fd;
82         int (*setup)(sdb_conn_t *, void *);
83 } listener_t;
85 typedef struct {
86         int type;
87         const char *prefix;
89         int (*open)(listener_t *);
90         void (*close)(listener_t *);
91 } fe_listener_impl_t;
93 struct sdb_fe_socket {
94         listener_t *listeners;
95         size_t listeners_num;
97         /* list of open, idle connections; active connections will be passed on to
98          * the connection handler threads which place them back after handling
99          * pending actions; the trigger pipe is used to notify the main thread
100          * after adding a connection (back) to the list */
101         sdb_llist_t *open_connections;
102         int trigger[2];
103 #define TRIGGER_READ 0
104 #define TRIGGER_WRITE 1
106         /* channel used for communication between main
107          * and connection handler threads */
108         sdb_channel_t *chan;
109 };
111 /*
112  * connection management functions
113  */
115 static int
116 setup_unixsock(sdb_conn_t *conn, void __attribute__((unused)) *user_data)
118         uid_t uid;
120         struct passwd pw_entry;
121         struct passwd *result = NULL;
122         char buf[1024];
124 #ifdef SO_PEERCRED
125         struct ucred cred;
126         socklen_t len = sizeof(cred);
128         if (getsockopt(conn->fd, SOL_SOCKET, SO_PEERCRED, &cred, &len)
129                         || (len != sizeof(cred))) {
130                 char errbuf[1024];
131                 sdb_log(SDB_LOG_ERR, "frontend: Failed to determine peer for "
132                                 "connection conn#%i: %s", conn->fd,
133                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
134                 return -1;
135         }
136         uid = cred.uid;
137 #else /* SO_PEERCRED */
138         sdb_log(SDB_LOG_ERR, "frontend: Failed to determine peer for "
139                         "connection conn#%i: operation not supported", conn->fd);
140         return -1;
141 #endif
143         memset(&pw_entry, 0, sizeof(pw_entry));
144         if (getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result) || (! result)
145                         || (! (conn->username = strdup(result->pw_name)))) {
146                 char errbuf[1024];
147                 sdb_log(SDB_LOG_ERR, "frontend: Failed to determine peer for "
148                                 "connection conn#%i: %s", conn->fd,
149                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
150                 return -1;
151         }
152         return 0;
153 } /* setup_unixsock */
155 static int
156 open_unixsock(listener_t *listener)
158         char *addr_copy;
159         char *base_dir;
160         struct sockaddr_un sa;
161         int status;
163         listener->sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
164         if (listener->sock_fd < 0) {
165                 char buf[1024];
166                 sdb_log(SDB_LOG_ERR, "frontend: Failed to open UNIX socket %s: %s",
167                                 listener->address, sdb_strerror(errno, buf, sizeof(buf)));
168                 return -1;
169         }
171         memset(&sa, 0, sizeof(sa));
172         sa.sun_family = AF_UNIX;
173         strncpy(sa.sun_path, listener->address, sizeof(sa.sun_path));
175         addr_copy = strdup(listener->address);
176         if (! addr_copy) {
177                 char errbuf[1024];
178                 sdb_log(SDB_LOG_ERR, "frontend: strdup failed: %s",
179                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
180                 return -1;
181         }
182         base_dir = dirname(addr_copy);
184         /* ensure that the directory exists */
185         if (sdb_mkdir_all(base_dir, 0777)) {
186                 char errbuf[1024];
187                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create directory '%s': %s",
188                                 base_dir, sdb_strerror(errno, errbuf, sizeof(errbuf)));
189                 free(addr_copy);
190                 return -1;
191         }
192         free(addr_copy);
194         if (unlink(listener->address) && (errno != ENOENT)) {
195                 char errbuf[1024];
196                 sdb_log(SDB_LOG_WARNING, "frontend: Failed to remove stale UNIX "
197                                 "socket %s: %s", listener->address,
198                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
199         }
201         status = bind(listener->sock_fd, (struct sockaddr *)&sa, sizeof(sa));
202         if (status) {
203                 char buf[1024];
204                 sdb_log(SDB_LOG_ERR, "frontend: Failed to bind to UNIX socket %s: %s",
205                                 listener->address, sdb_strerror(errno, buf, sizeof(buf)));
206                 return -1;
207         }
209         listener->setup = setup_unixsock;
210         return 0;
211 } /* open_unixsock */
213 static void
214 close_unixsock(listener_t *listener)
216         assert(listener);
218         if (! listener->address)
219                 return;
221         if (listener->sock_fd >= 0)
222                 close(listener->sock_fd);
223         listener->sock_fd = -1;
225         unlink(listener->address);
226 } /* close_unixsock */
228 static int
229 open_tcp(listener_t *listener)
231         struct addrinfo *ai, *ai_list = NULL;
232         int status;
234         assert(listener);
236         if ((status = sdb_resolve(SDB_NET_TCP, listener->address, &ai_list))) {
237                 sdb_log(SDB_LOG_ERR, "frontend: Failed to resolve '%s': %s",
238                                 listener->address, gai_strerror(status));
239                 return -1;
240         }
242         for (ai = ai_list; ai != NULL; ai = ai->ai_next) {
243                 char errbuf[1024];
244                 int reuse = 1;
246                 listener->sock_fd = socket(ai->ai_family,
247                                 ai->ai_socktype, ai->ai_protocol);
248                 if (listener->sock_fd < 0) {
249                         sdb_log(SDB_LOG_ERR, "frontend: Failed to open socket for %s: %s",
250                                         listener->address,
251                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
252                         continue;
253                 }
255                 if (setsockopt(listener->sock_fd, SOL_SOCKET, SO_REUSEADDR,
256                                         &reuse, sizeof(reuse)) < 0) {
257                         sdb_log(SDB_LOG_ERR, "frontend: Failed to set socket option: %s",
258                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
259                         close(listener->sock_fd);
260                         listener->sock_fd = -1;
261                         continue;
262                 }
264                 if (bind(listener->sock_fd, ai->ai_addr, ai->ai_addrlen) < 0) {
265                         char host[1024], port[32];
266                         getnameinfo(ai->ai_addr, ai->ai_addrlen, host, sizeof(host),
267                                         port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
268                         sdb_log(SDB_LOG_ERR, "frontend: Failed to bind to %s:%s: %s",
269                                         host, port, sdb_strerror(errno, errbuf, sizeof(errbuf)));
270                         close(listener->sock_fd);
271                         listener->sock_fd = -1;
272                         continue;
273                 }
274                 break;
275         }
276         freeaddrinfo(ai_list);
278         if (listener->sock_fd < 0)
279                 return -1;
280         return 0;
281 } /* open_tcp */
283 static void
284 close_tcp(listener_t *listener)
286         assert(listener);
288         if (listener->sock_fd >= 0)
289                 close(listener->sock_fd);
290         listener->sock_fd = -1;
291 } /* close_tcp */
293 /*
294  * private variables
295  */
297 /* the enum has to be sorted the same as the implementations array
298  * to ensure that the type may be used as index into the array */
299 enum {
300         LISTENER_TCP = 0, /* this is the default */
301         LISTENER_UNIXSOCK,
302 };
303 static fe_listener_impl_t listener_impls[] = {
304         { LISTENER_TCP,      "tcp",  open_tcp,      close_tcp },
305         { LISTENER_UNIXSOCK, "unix", open_unixsock, close_unixsock },
306 };
308 /*
309  * private helper functions
310  */
312 static int
313 listener_listen(listener_t *listener)
315         assert(listener);
317         /* try to reopen */
318         if (listener->sock_fd < 0)
319                 if (listener_impls[listener->type].open(listener))
320                         return -1;
321         assert(listener->sock_fd >= 0);
323         if (listen(listener->sock_fd, /* backlog = */ 32)) {
324                 char buf[1024];
325                 sdb_log(SDB_LOG_ERR, "frontend: Failed to listen on socket %s: %s",
326                                 listener->address, sdb_strerror(errno, buf, sizeof(buf)));
327                 return -1;
328         }
329         return 0;
330 } /* listener_listen */
332 static void
333 listener_close(listener_t *listener)
335         assert(listener);
337         if (listener_impls[listener->type].close)
338                 listener_impls[listener->type].close(listener);
340         if (listener->sock_fd >= 0)
341                 close(listener->sock_fd);
342         listener->sock_fd = -1;
343 } /* listener_close */
345 static int
346 get_type(const char *address)
348         char *sep;
349         size_t len;
350         size_t i;
352         if (*address == '/')
353                 return LISTENER_UNIXSOCK;
354         sep = strchr(address, (int)':');
355         if (! sep)
356                 return listener_impls[0].type;
358         assert(sep > address);
359         len = (size_t)(sep - address);
361         for (i = 0; i < SDB_STATIC_ARRAY_LEN(listener_impls); ++i) {
362                 fe_listener_impl_t *impl = listener_impls + i;
364                 if (!strncmp(address, impl->prefix, len)) {
365                         assert(impl->type == (int)i);
366                         return impl->type;
367                 }
368         }
369         return -1;
370 } /* get_type */
372 static void
373 listener_destroy(listener_t *listener)
375         if (! listener)
376                 return;
378         listener_close(listener);
380         if (listener->address)
381                 free(listener->address);
382         listener->address = NULL;
383 } /* listener_destroy */
385 static listener_t *
386 listener_create(sdb_fe_socket_t *sock, const char *address)
388         listener_t *listener;
389         size_t len;
390         int type;
392         type = get_type(address);
393         if (type < 0) {
394                 sdb_log(SDB_LOG_ERR, "frontend: Unsupported address type specified "
395                                 "in listen address '%s'", address);
396                 return NULL;
397         }
399         listener = realloc(sock->listeners,
400                         (sock->listeners_num + 1) * sizeof(*sock->listeners));
401         if (! listener) {
402                 char buf[1024];
403                 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate memory: %s",
404                                 sdb_strerror(errno, buf, sizeof(buf)));
405                 return NULL;
406         }
408         sock->listeners = listener;
409         listener = sock->listeners + sock->listeners_num;
411         len = strlen(listener_impls[type].prefix);
412         if ((! strncmp(address, listener_impls[type].prefix, len))
413                         && (address[len] == ':'))
414                 address += strlen(listener_impls[type].prefix) + 1;
416         listener->sock_fd = -1;
417         listener->address = strdup(address);
418         if (! listener->address) {
419                 char buf[1024];
420                 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate memory: %s",
421                                 sdb_strerror(errno, buf, sizeof(buf)));
422                 listener_destroy(listener);
423                 return NULL;
424         }
425         listener->type = type;
426         listener->setup = NULL;
428         if (listener_impls[type].open(listener)) {
429                 /* prints error */
430                 listener_destroy(listener);
431                 return NULL;
432         }
434         ++sock->listeners_num;
435         return listener;
436 } /* listener_create */
438 static void
439 socket_clear(sdb_fe_socket_t *sock)
441         size_t i;
443         assert(sock);
444         for (i = 0; i < sock->listeners_num; ++i)
445                 listener_destroy(sock->listeners + i);
446         if (sock->listeners)
447                 free(sock->listeners);
448         sock->listeners = NULL;
449         sock->listeners_num = 0;
450 } /* socket_clear */
452 static void
453 socket_close(sdb_fe_socket_t *sock)
455         size_t i;
457         assert(sock);
458         for (i = 0; i < sock->listeners_num; ++i)
459                 listener_close(sock->listeners + i);
460 } /* socket_close */
462 /*
463  * connection handler functions
464  */
466 static void *
467 connection_handler(void *data)
469         sdb_fe_socket_t *sock = data;
471         assert(sock);
473         while (42) {
474                 struct timespec timeout = { 0, 500000000 }; /* .5 seconds */
475                 sdb_conn_t *conn;
476                 int status;
478                 errno = 0;
479                 status = sdb_channel_select(sock->chan, /* read */ NULL, &conn,
480                                 /* write */ NULL, NULL, &timeout);
481                 if (status) {
482                         char buf[1024];
484                         if (errno == ETIMEDOUT)
485                                 continue;
486                         if (errno == EBADF) /* channel shut down */
487                                 break;
489                         sdb_log(SDB_LOG_ERR, "frontend: Failed to read from channel: %s",
490                                         sdb_strerror(errno, buf, sizeof(buf)));
491                         continue;
492                 }
494                 status = (int)sdb_connection_handle(conn);
495                 if (status <= 0) {
496                         /* error or EOF -> close connection */
497                         sdb_object_deref(SDB_OBJ(conn));
498                         continue;
499                 }
501                 /* return the connection to the main loop */
502                 if (sdb_llist_append(sock->open_connections, SDB_OBJ(conn))) {
503                         sdb_log(SDB_LOG_ERR, "frontend: Failed to re-append "
504                                         "connection %s to list of open connections",
505                                         SDB_OBJ(conn)->name);
506                 }
507                 write(sock->trigger[TRIGGER_WRITE], "", 1);
509                 /* pass ownership back to list; or destroy in case of an error */
510                 sdb_object_deref(SDB_OBJ(conn));
511         }
512         return NULL;
513 } /* connection_handler */
515 static int
516 connection_accept(sdb_fe_socket_t *sock, listener_t *listener)
518         sdb_object_t *obj;
519         int status;
521         obj = SDB_OBJ(sdb_connection_accept(listener->sock_fd,
522                                 listener->setup, NULL));
523         if (! obj)
524                 return -1;
526         status = sdb_llist_append(sock->open_connections, obj);
527         if (status)
528                 sdb_log(SDB_LOG_ERR, "frontend: Failed to append "
529                                 "connection %s to list of open connections",
530                                 obj->name);
532         /* hand ownership over to the list; or destroy in case of an error */
533         sdb_object_deref(obj);
534         return status;
535 } /* connection_accept */
537 static int
538 socket_handle_incoming(sdb_fe_socket_t *sock,
539                 fd_set *ready, fd_set *exceptions)
541         sdb_llist_iter_t *iter;
542         size_t i;
544         for (i = 0; i < sock->listeners_num; ++i) {
545                 listener_t *listener = sock->listeners + i;
546                 if (FD_ISSET(listener->sock_fd, ready))
547                         if (connection_accept(sock, listener))
548                                 continue;
549         }
551         iter = sdb_llist_get_iter(sock->open_connections);
552         if (! iter) {
553                 sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator "
554                                 "for open connections");
555                 return -1;
556         }
558         while (sdb_llist_iter_has_next(iter)) {
559                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
561                 if (FD_ISSET(CONN(obj)->fd, exceptions)) {
562                         sdb_log(SDB_LOG_INFO, "Exception on fd %d",
563                                         CONN(obj)->fd);
564                         /* close the connection */
565                         sdb_llist_iter_remove_current(iter);
566                         sdb_object_deref(obj);
567                         continue;
568                 }
570                 if (FD_ISSET(CONN(obj)->fd, ready)) {
571                         sdb_llist_iter_remove_current(iter);
572                         sdb_channel_write(sock->chan, &obj);
573                 }
574         }
575         sdb_llist_iter_destroy(iter);
576         return 0;
577 } /* socket_handle_incoming */
579 /*
580  * public API
581  */
583 sdb_fe_socket_t *
584 sdb_fe_sock_create(void)
586         sdb_fe_socket_t *sock;
587         int flags;
589         sock = calloc(1, sizeof(*sock));
590         if (! sock)
591                 return NULL;
592         sock->trigger[TRIGGER_READ] = sock->trigger[TRIGGER_WRITE] = -1;
594         sock->open_connections = sdb_llist_create();
595         if (! sock->open_connections) {
596                 sdb_fe_sock_destroy(sock);
597                 return NULL;
598         }
600         if (pipe(sock->trigger)) {
601                 char errbuf[1024];
602                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create pipe: %s",
603                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
604                 sdb_fe_sock_destroy(sock);
605                 return NULL;
606         }
607         /* TODO: Can we do a zero-byte write as well to trigger select()?
608          * Linux does not seem to support the I_SWROPT ioctl on a pipe. */
609         flags = fcntl(sock->trigger[TRIGGER_WRITE], F_GETFL);
610         if (fcntl(sock->trigger[TRIGGER_WRITE], F_SETFL, flags | O_NONBLOCK)) {
611                 char errbuf[1024];
612                 sdb_log(SDB_LOG_ERR, "frontend: Failed to switch pipe to non-blocking "
613                                 "mode: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
614                 sdb_fe_sock_destroy(sock);
615                 return NULL;
616         }
617         flags = fcntl(sock->trigger[TRIGGER_READ], F_GETFL);
618         if (fcntl(sock->trigger[TRIGGER_READ], F_SETFL, flags | O_NONBLOCK)) {
619                 char errbuf[1024];
620                 sdb_log(SDB_LOG_ERR, "frontend: Failed to switch pipe to non-blocking "
621                                 "mode: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
622                 sdb_fe_sock_destroy(sock);
623                 return NULL;
624         }
625         return sock;
626 } /* sdb_fe_sock_create */
628 void
629 sdb_fe_sock_destroy(sdb_fe_socket_t *sock)
631         if (! sock)
632                 return;
634         socket_clear(sock);
636         if (sock->trigger[TRIGGER_WRITE] >= 0)
637                 close(sock->trigger[TRIGGER_WRITE]);
638         if (sock->trigger[TRIGGER_READ] >= 0)
639                 close(sock->trigger[TRIGGER_READ]);
640         sock->trigger[TRIGGER_READ] = sock->trigger[TRIGGER_WRITE] = -1;
642         sdb_llist_destroy(sock->open_connections);
643         sock->open_connections = NULL;
644         free(sock);
645 } /* sdb_fe_sock_destroy */
647 int
648 sdb_fe_sock_add_listener(sdb_fe_socket_t *sock, const char *address)
650         listener_t *listener;
652         if ((! sock) || (! address))
653                 return -1;
655         listener = listener_create(sock, address);
656         if (! listener)
657                 return -1;
658         return 0;
659 } /* sdb_fe_sock_add_listener */
661 void
662 sdb_fe_sock_clear_listeners(sdb_fe_socket_t *sock)
664         if (! sock)
665                 return;
667         socket_clear(sock);
668 } /* sdb_fe_sock_clear_listeners */
670 int
671 sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
673         fd_set sockets;
674         int max_listen_fd = 0;
675         size_t i;
677         pthread_t handler_threads[loop->num_threads];
678         size_t num_threads;
680         if ((! sock) || (! sock->listeners_num) || sock->chan
681                         || (! loop) || (loop->num_threads <= 0))
682                 return -1;
684         if (! loop->do_loop)
685                 return 0;
687         FD_ZERO(&sockets);
688         for (i = 0; i < sock->listeners_num; ++i) {
689                 listener_t *listener = sock->listeners + i;
691                 if (listener_listen(listener)) {
692                         socket_close(sock);
693                         return -1;
694                 }
696                 FD_SET(listener->sock_fd, &sockets);
697                 if (listener->sock_fd > max_listen_fd)
698                         max_listen_fd = listener->sock_fd;
699         }
701         sock->chan = sdb_channel_create(1024, sizeof(sdb_conn_t *));
702         if (! sock->chan) {
703                 socket_close(sock);
704                 return -1;
705         }
707         sdb_log(SDB_LOG_INFO, "frontend: Starting %zu connection "
708                         "handler thread%s managing %zu listener%s",
709                         loop->num_threads, loop->num_threads == 1 ? "" : "s",
710                         sock->listeners_num, sock->listeners_num == 1 ? "" : "s");
712         num_threads = loop->num_threads;
713         memset(&handler_threads, 0, sizeof(handler_threads));
714         for (i = 0; i < num_threads; ++i) {
715                 errno = 0;
716                 if (pthread_create(&handler_threads[i], /* attr = */ NULL,
717                                         connection_handler, /* arg = */ sock)) {
718                         char errbuf[1024];
719                         sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
720                                         "connection handler thread: %s",
721                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
722                         num_threads = i;
723                         break;
724                 }
725         }
727         while (loop->do_loop && num_threads) {
728                 struct timeval timeout = { 1, 0 }; /* one second */
729                 sdb_llist_iter_t *iter;
731                 int max_fd = max_listen_fd;
732                 fd_set ready;
733                 fd_set exceptions;
734                 int n;
736                 FD_ZERO(&ready);
737                 FD_ZERO(&exceptions);
739                 ready = sockets;
740                 FD_SET(sock->trigger[TRIGGER_READ], &ready);
742                 iter = sdb_llist_get_iter(sock->open_connections);
743                 if (! iter) {
744                         sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator "
745                                         "for open connections");
746                         break;
747                 }
749                 while (sdb_llist_iter_has_next(iter)) {
750                         sdb_object_t *obj = sdb_llist_iter_get_next(iter);
752                         if (CONN(obj)->fd < 0) {
753                                 sdb_llist_iter_remove_current(iter);
754                                 sdb_object_deref(obj);
755                                 continue;
756                         }
758                         FD_SET(CONN(obj)->fd, &ready);
759                         FD_SET(CONN(obj)->fd, &exceptions);
761                         if (CONN(obj)->fd > max_fd)
762                                 max_fd = CONN(obj)->fd;
763                 }
764                 sdb_llist_iter_destroy(iter);
766                 errno = 0;
767                 n = select(max_fd + 1, &ready, NULL, &exceptions, &timeout);
768                 if (n < 0) {
769                         char buf[1024];
771                         if (errno == EINTR)
772                                 continue;
774                         sdb_log(SDB_LOG_ERR, "frontend: Failed to monitor sockets: %s",
775                                         sdb_strerror(errno, buf, sizeof(buf)));
776                         break;
777                 }
778                 else if (! n)
779                         continue;
781                 if (FD_ISSET(sock->trigger[TRIGGER_READ], &ready)) {
782                         char buf[1024];
783                         while (read(sock->trigger[TRIGGER_READ], buf, sizeof(buf)) > 0)
784                                 /* do nothing */;
785                 }
787                 /* handle new and open connections */
788                 if (socket_handle_incoming(sock, &ready, &exceptions))
789                         break;
790         }
792         socket_close(sock);
794         sdb_log(SDB_LOG_INFO, "frontend: Waiting for connection handler threads "
795                         "to terminate");
796         if (! sdb_channel_shutdown(sock->chan))
797                 for (i = 0; i < num_threads; ++i)
798                         pthread_join(handler_threads[i], NULL);
799         /* else: we tried our best; let the operating system clean up */
801         sdb_channel_destroy(sock->chan);
802         sock->chan = NULL;
804         if (! num_threads)
805                 return -1;
806         return 0;
807 } /* sdb_fe_sock_listen_and_server */
809 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */