diff --git a/src/frontend/sock.c b/src/frontend/sock.c
index ba9b3a867bb637262f711a2a29618002f7310529..cac49e1db1648b93cc7cd55d933383dd274103f9 100644 (file)
--- a/src/frontend/sock.c
+++ b/src/frontend/sock.c
#include "sysdb.h"
#include "core/error.h"
#include "core/object.h"
+#include "frontend/connection-private.h"
#include "frontend/sock.h"
#include "utils/channel.h"
#include "utils/llist.h"
+#include "utils/strbuf.h"
#include <assert.h>
-
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
-#include <fcntl.h>
-
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <pthread.h>
-/* name of connection objects */
-#define CONN_FD_PREFIX "conn#"
-#define CONN_FD_PLACEHOLDER "XXXXXXX"
-
/*
* private data types
*/
-typedef struct {
- int fd;
- struct sockaddr_storage client_addr;
- socklen_t client_addr_len;
-} connection_t;
-
-typedef struct {
- sdb_object_t super;
- connection_t conn;
-} connection_obj_t;
-#define CONN(obj) ((connection_obj_t *)(obj))
-
typedef struct {
char *address;
int type;
listener_close(sock->listeners + i);
} /* socket_close */
-/*
- * private data types
- */
-
-static int
-connection_init(sdb_object_t *obj, va_list ap)
-{
- connection_t *conn;
- int sock_fd;
- int sock_fl;
-
- assert(obj);
- conn = &CONN(obj)->conn;
-
- sock_fd = va_arg(ap, int);
-
- conn->client_addr_len = sizeof(conn->client_addr);
- conn->fd = accept(sock_fd, (struct sockaddr *)&conn->client_addr,
- &conn->client_addr_len);
-
- if (conn->fd < 0) {
- char buf[1024];
- sdb_log(SDB_LOG_ERR, "frontend: Failed to accept remote "
- "connection: %s", sdb_strerror(errno,
- buf, sizeof(buf)));
- return -1;
- }
-
- if (conn->client_addr.ss_family != AF_UNIX) {
- sdb_log(SDB_LOG_ERR, "frontend: Accepted connection using "
- "unexpected family type %d", conn->client_addr.ss_family);
- return -1;
- }
-
- sock_fl = fcntl(conn->fd, F_GETFL);
- if (fcntl(conn->fd, F_SETFL, sock_fl | O_NONBLOCK)) {
- char buf[1024];
- sdb_log(SDB_LOG_ERR, "frontend: Failed to switch connection conn#%i "
- "to non-blocking mode: %s", conn->fd,
- sdb_strerror(errno, buf, sizeof(buf)));
- return -1;
- }
-
- sdb_log(SDB_LOG_DEBUG, "frontend: Accepted connection on fd=%i",
- conn->fd);
-
- /* update the object name */
- snprintf(obj->name + strlen(CONN_FD_PREFIX),
- strlen(CONN_FD_PLACEHOLDER), "%i", conn->fd);
- return 0;
-} /* connection_init */
-
-static void
-connection_destroy(sdb_object_t *obj)
-{
- connection_t *conn;
-
- assert(obj);
- conn = &CONN(obj)->conn;
-
- sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection on fd=%i", conn->fd);
- close(conn->fd);
- conn->fd = -1;
-} /* connection_destroy */
-
-static sdb_type_t connection_type = {
- /* size = */ sizeof(connection_obj_t),
- /* init = */ connection_init,
- /* destroy = */ connection_destroy,
-};
-
/*
* connection handler functions
*/
-/* returns negative value on error, 0 on EOF, number of packets else */
-static int
-connection_read(int fd)
-{
- int n = 0;
-
- while (42) {
- int32_t cmd;
- ssize_t status;
-
- errno = 0;
- status = read(fd, &cmd, sizeof(cmd));
- if (status < 0) {
- if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
- return n + 1;
- return (int)status;
- }
- else if (! status) /* EOF */
- return 0;
-
- /* XXX */
- sdb_log(SDB_LOG_DEBUG, "frontend: read command %i from fd=%i",
- cmd, fd);
- ++n;
- }
-
- return n + 1;
-} /* connection_read */
-
static void *
connection_handler(void *data)
{
while (42) {
struct timespec timeout = { 0, 500000000 }; /* .5 seconds */
- connection_obj_t *conn;
+ sdb_conn_t *conn;
int status;
errno = 0;
continue;
}
- status = connection_read(conn->conn.fd);
+ status = (int)sdb_connection_read(conn);
if (status <= 0) {
/* error or EOF -> close connection */
sdb_object_deref(SDB_OBJ(conn));
+ continue;
}
- else {
- if (sdb_llist_append(sock->open_connections, SDB_OBJ(conn))) {
- sdb_log(SDB_LOG_ERR, "frontend: Failed to re-append "
- "connection %s to list of open connections",
- SDB_OBJ(conn)->name);
- }
-
- /* pass ownership back to list; or destroy in case of an error */
- sdb_object_deref(SDB_OBJ(conn));
+
+ /* return the connection to the main loop */
+ if (sdb_llist_append(sock->open_connections, SDB_OBJ(conn))) {
+ sdb_log(SDB_LOG_ERR, "frontend: Failed to re-append "
+ "connection %s to list of open connections",
+ SDB_OBJ(conn)->name);
}
+
+ /* pass ownership back to list; or destroy in case of an error */
+ sdb_object_deref(SDB_OBJ(conn));
}
return NULL;
} /* connection_handler */
connection_accept(sdb_fe_socket_t *sock, listener_t *listener)
{
sdb_object_t *obj;
+ int status;
- /* the placeholder will be replaced with the accepted file
- * descriptor when initializing the object */
- obj = sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER,
- connection_type, listener->sock_fd);
+ obj = SDB_OBJ(sdb_connection_accept(listener->sock_fd));
if (! obj)
return -1;
- if (sdb_llist_append(sock->open_connections, obj)) {
+ status = sdb_llist_append(sock->open_connections, obj);
+ if (status)
sdb_log(SDB_LOG_ERR, "frontend: Failed to append "
"connection %s to list of open connections",
obj->name);
- sdb_object_deref(obj);
+
+ /* hand ownership over to the list; or destroy in case of an error */
+ sdb_object_deref(obj);
+ return status;
+} /* connection_accept */
+
+static int
+socket_handle_incoming(sdb_fe_socket_t *sock,
+ fd_set *ready, fd_set *exceptions)
+{
+ sdb_llist_iter_t *iter;
+ size_t i;
+
+ for (i = 0; i < sock->listeners_num; ++i) {
+ listener_t *listener = sock->listeners + i;
+ if (FD_ISSET(listener->sock_fd, ready))
+ if (connection_accept(sock, listener))
+ continue;
+ }
+
+ iter = sdb_llist_get_iter(sock->open_connections);
+ if (! iter) {
+ sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator "
+ "for open connections");
return -1;
}
- /* hand ownership over to the list */
- sdb_object_deref(obj);
+ while (sdb_llist_iter_has_next(iter)) {
+ sdb_object_t *obj = sdb_llist_iter_get_next(iter);
+
+ if (FD_ISSET(CONN(obj)->fd, exceptions))
+ sdb_log(SDB_LOG_INFO, "Exception on fd %d",
+ CONN(obj)->fd);
+
+ if (FD_ISSET(CONN(obj)->fd, ready)) {
+ sdb_llist_iter_remove_current(iter);
+ sdb_channel_write(sock->chan, &obj);
+ }
+ }
+ sdb_llist_iter_destroy(iter);
return 0;
-} /* connection_accept */
+} /* socket_handle_incoming */
/*
* public API
max_listen_fd = listener->sock_fd;
}
- sock->chan = sdb_channel_create(1024, sizeof(connection_obj_t *));
+ sock->chan = sdb_channel_create(1024, sizeof(sdb_conn_t *));
if (! sock->chan) {
socket_close(sock);
return -1;
connection_handler, /* arg = */ sock);
while (loop->do_loop) {
+ struct timeval timeout = { 1, 0 }; /* one second */
+ sdb_llist_iter_t *iter;
+
+ int max_fd = max_listen_fd;
fd_set ready;
fd_set exceptions;
- int max_fd;
int n;
- struct timeval timeout = { 1, 0 }; /* one second */
- sdb_llist_iter_t *iter;
-
FD_ZERO(&ready);
FD_ZERO(&exceptions);
ready = sockets;
- max_fd = max_listen_fd;
-
iter = sdb_llist_get_iter(sock->open_connections);
if (! iter) {
sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator "
while (sdb_llist_iter_has_next(iter)) {
sdb_object_t *obj = sdb_llist_iter_get_next(iter);
- FD_SET(CONN(obj)->conn.fd, &ready);
- FD_SET(CONN(obj)->conn.fd, &exceptions);
+ FD_SET(CONN(obj)->fd, &ready);
+ FD_SET(CONN(obj)->fd, &exceptions);
- if (CONN(obj)->conn.fd > max_fd)
- max_fd = CONN(obj)->conn.fd;
+ if (CONN(obj)->fd > max_fd)
+ max_fd = CONN(obj)->fd;
}
sdb_llist_iter_destroy(iter);
sdb_strerror(errno, buf, sizeof(buf)));
break;
}
-
- if (! n)
+ else if (! n)
continue;
- for (i = 0; i < sock->listeners_num; ++i) {
- listener_t *listener = sock->listeners + i;
- if (FD_ISSET(listener->sock_fd, &ready))
- if (connection_accept(sock, listener))
- continue;
- }
-
- iter = sdb_llist_get_iter(sock->open_connections);
- if (! iter) {
- sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator "
- "for open connections");
+ /* handle new and open connections */
+ if (socket_handle_incoming(sock, &ready, &exceptions))
break;
- }
-
- while (sdb_llist_iter_has_next(iter)) {
- sdb_object_t *obj = sdb_llist_iter_get_next(iter);
-
- if (FD_ISSET(CONN(obj)->conn.fd, &exceptions))
- sdb_log(SDB_LOG_INFO, "Exception on fd %d",
- CONN(obj)->conn.fd);
-
- if (FD_ISSET(CONN(obj)->conn.fd, &ready)) {
- sdb_llist_iter_remove_current(iter);
- sdb_channel_write(sock->chan, &obj);
- }
- }
- sdb_llist_iter_destroy(iter);
}
socket_close(sock);