diff --git a/src/frontend/sock.c b/src/frontend/sock.c
index 667b616d49105214d5a9ad7f814d2ce43b387bfb..d7b2ee97324ddef9d32246f9be163ec30152f998 100644 (file)
--- a/src/frontend/sock.c
+++ b/src/frontend/sock.c
#include "utils/channel.h"
#include "utils/llist.h"
#include "utils/channel.h"
#include "utils/llist.h"
+#include "utils/strbuf.h"
#include <assert.h>
#include <assert.h>
-
#include <errno.h>
#include <errno.h>
+#include <arpa/inet.h>
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
*/
typedef struct {
*/
typedef struct {
+ sdb_object_t super;
+
+ /* connection and client information */
int fd;
struct sockaddr_storage client_addr;
socklen_t client_addr_len;
int fd;
struct sockaddr_storage client_addr;
socklen_t client_addr_len;
-} connection_t;
-typedef struct {
- sdb_object_t super;
- connection_t conn;
+ /* read buffer */
+ sdb_strbuf_t *buf;
+
+ /* state information for the currently executed command */
+ uint32_t cmd;
+ uint32_t cmd_len;
} connection_obj_t;
#define CONN(obj) ((connection_obj_t *)(obj))
} connection_obj_t;
#define CONN(obj) ((connection_obj_t *)(obj))
size_t listeners_num;
sdb_llist_t *open_connections;
size_t listeners_num;
sdb_llist_t *open_connections;
+
+ /* channel used for communication between main
+ * and connection handler threads */
+ sdb_channel_t *chan;
};
/*
};
/*
if (listener->sock_fd >= 0)
close(listener->sock_fd);
if (listener->sock_fd >= 0)
close(listener->sock_fd);
+ listener->sock_fd = -1;
if (listener->address)
free(listener->address);
if (listener->address)
free(listener->address);
return listener;
} /* listener_create */
return listener;
} /* listener_create */
+static int
+listener_listen(listener_t *listener)
+{
+ assert(listener);
+
+ /* try to reopen */
+ if (listener->sock_fd < 0)
+ if (listener_impls[listener->type].opener(listener))
+ return -1;
+ assert(listener->sock_fd >= 0);
+
+ if (listen(listener->sock_fd, /* backlog = */ 32)) {
+ char buf[1024];
+ sdb_log(SDB_LOG_ERR, "frontend: Failed to listen on socket %s: %s",
+ listener->address, sdb_strerror(errno, buf, sizeof(buf)));
+ return -1;
+ }
+ return 0;
+} /* listener_listen */
+
+static void
+listener_close(listener_t *listener)
+{
+ assert(listener);
+
+ if (listener->sock_fd < 0)
+ return;
+
+ close(listener->sock_fd);
+ listener->sock_fd = -1;
+} /* listener_close */
+
+static void
+socket_close(sdb_fe_socket_t *sock)
+{
+ size_t i;
+
+ assert(sock);
+ for (i = 0; i < sock->listeners_num; ++i)
+ listener_close(sock->listeners + i);
+} /* socket_close */
+
/*
* private data types
*/
/*
* private data types
*/
static int
connection_init(sdb_object_t *obj, va_list ap)
{
static int
connection_init(sdb_object_t *obj, va_list ap)
{
- connection_t *conn;
+ connection_obj_t *conn;
int sock_fd;
int sock_fl;
assert(obj);
int sock_fd;
int sock_fl;
assert(obj);
- conn = &CONN(obj)->conn;
+ conn = CONN(obj);
sock_fd = va_arg(ap, int);
sock_fd = va_arg(ap, int);
+ CONN(obj)->buf = sdb_strbuf_create(/* size = */ 128);
+ if (! CONN(obj)->buf) {
+ sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate a read buffer "
+ "for a new remote connection");
+ return -1;
+ }
+
conn->client_addr_len = sizeof(conn->client_addr);
conn->fd = accept(sock_fd, (struct sockaddr *)&conn->client_addr,
&conn->client_addr_len);
conn->client_addr_len = sizeof(conn->client_addr);
conn->fd = accept(sock_fd, (struct sockaddr *)&conn->client_addr,
&conn->client_addr_len);
static void
connection_destroy(sdb_object_t *obj)
{
static void
connection_destroy(sdb_object_t *obj)
{
- connection_t *conn;
+ connection_obj_t *conn;
+ size_t len;
assert(obj);
assert(obj);
- conn = &CONN(obj)->conn;
+ conn = CONN(obj);
+
+ len = sdb_strbuf_len(conn->buf);
+ if (len)
+ sdb_log(SDB_LOG_INFO, "frontend: Discarding incomplete command "
+ "(%zu bytes left in buffer)", len);
+ sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection on fd=%i", conn->fd);
close(conn->fd);
conn->fd = -1;
close(conn->fd);
conn->fd = -1;
+
+ sdb_strbuf_destroy(CONN(obj)->buf);
} /* connection_destroy */
static sdb_type_t connection_type = {
} /* connection_destroy */
static sdb_type_t connection_type = {
* connection handler functions
*/
* connection handler functions
*/
+static uint32_t
+connection_get_int32(connection_obj_t *conn, size_t offset)
+{
+ const char *data;
+ uint32_t n;
+
+ assert(conn && (sdb_strbuf_len(conn->buf) >= offset + sizeof(uint32_t)));
+
+ data = sdb_strbuf_string(conn->buf);
+ memcpy(&n, data + offset, sizeof(n));
+ n = ntohl(n);
+ return n;
+} /* connection_get_int32 */
+
+static int
+command_handle(connection_obj_t *conn)
+{
+ assert(conn && conn->cmd && conn->cmd_len);
+ /* XXX */
+ sdb_strbuf_skip(conn->buf, conn->cmd_len);
+ return 0;
+} /* command_handle */
+
+/* initialize the connection state information */
+static int
+command_init(connection_obj_t *conn)
+{
+ assert(conn && (! conn->cmd) && (! conn->cmd_len));
+
+ conn->cmd = connection_get_int32(conn, 0);
+ conn->cmd_len = connection_get_int32(conn, sizeof(uint32_t));
+ sdb_strbuf_skip(conn->buf, 2 * sizeof(uint32_t));
+ return 0;
+} /* command_init */
+
+/* returns negative value on error, 0 on EOF, number of octets else */
+static ssize_t
+connection_read(connection_obj_t *conn)
+{
+ ssize_t n = 0;
+
+ while (42) {
+ ssize_t status;
+
+ errno = 0;
+ status = sdb_strbuf_read(conn->buf, conn->fd, 1024);
+ if (status < 0) {
+ if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
+ return n;
+ return (int)status;
+ }
+ else if (! status) /* EOF */
+ return n;
+
+ n += status;
+ }
+
+ return n;
+} /* connection_read */
+
static void *
connection_handler(void *data)
{
static void *
connection_handler(void *data)
{
- sdb_channel_t *chan = data;
+ sdb_fe_socket_t *sock = data;
- assert(chan);
+ assert(sock);
while (42) {
struct timespec timeout = { 0, 500000000 }; /* .5 seconds */
while (42) {
struct timespec timeout = { 0, 500000000 }; /* .5 seconds */
int status;
errno = 0;
int status;
errno = 0;
- status = sdb_channel_select(chan, NULL, &conn, NULL, NULL, &timeout);
+ status = sdb_channel_select(sock->chan, /* read */ NULL, &conn,
+ /* write */ NULL, NULL, &timeout);
if (status) {
char buf[1024];
if (status) {
char buf[1024];
continue;
}
continue;
}
- /* XXX */
- sdb_log(SDB_LOG_INFO, "frontend: Data available on connection fd=%i\n",
- conn->conn.fd);
+ status = (int)connection_read(conn);
+ if (status <= 0) {
+ /* error or EOF -> close connection */
+ sdb_object_deref(SDB_OBJ(conn));
+ continue;
+ }
+
+ if (conn->cmd_len && (sdb_strbuf_len(conn->buf) >= conn->cmd_len))
+ command_handle(conn);
+ else if (sdb_strbuf_len(conn->buf) >= 2 * sizeof(int32_t))
+ command_init(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 */
}
return NULL;
} /* connection_handler */
return 0;
} /* connection_accept */
return 0;
} /* 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;
+ }
+
+ 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;
+} /* socket_handle_incoming */
+
/*
* public API
*/
/*
* public API
*/
int
sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
{
int
sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop)
{
- sdb_channel_t *chan;
fd_set sockets;
int max_listen_fd = 0;
size_t i;
fd_set sockets;
int max_listen_fd = 0;
size_t i;
/* XXX: make the number of threads configurable */
pthread_t handler_threads[5];
/* XXX: make the number of threads configurable */
pthread_t handler_threads[5];
- if ((! sock) || (! sock->listeners_num) || (! loop))
+ if ((! sock) || (! sock->listeners_num) || (! loop) || sock->chan)
return -1;
FD_ZERO(&sockets);
return -1;
FD_ZERO(&sockets);
-
for (i = 0; i < sock->listeners_num; ++i) {
listener_t *listener = sock->listeners + i;
for (i = 0; i < sock->listeners_num; ++i) {
listener_t *listener = sock->listeners + i;
- if (listen(listener->sock_fd, /* backlog = */ 32)) {
- char buf[1024];
- sdb_log(SDB_LOG_ERR, "frontend: Failed to listen on socket %s: %s",
- listener->address, sdb_strerror(errno, buf, sizeof(buf)));
+ if (listener_listen(listener)) {
+ socket_close(sock);
return -1;
}
return -1;
}
max_listen_fd = listener->sock_fd;
}
max_listen_fd = listener->sock_fd;
}
- chan = sdb_channel_create(1024, sizeof(connection_obj_t *));
- if (! chan)
+ sock->chan = sdb_channel_create(1024, sizeof(connection_obj_t *));
+ if (! sock->chan) {
+ socket_close(sock);
return -1;
return -1;
+ }
memset(&handler_threads, 0, sizeof(handler_threads));
/* XXX: error handling */
for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
pthread_create(&handler_threads[i], /* attr = */ NULL,
memset(&handler_threads, 0, sizeof(handler_threads));
/* XXX: error handling */
for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
pthread_create(&handler_threads[i], /* attr = */ NULL,
- connection_handler, /* arg = */ chan);
+ connection_handler, /* arg = */ sock);
while (loop->do_loop) {
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;
fd_set ready;
fd_set exceptions;
- int max_fd;
int n;
int n;
- struct timeval timeout = { 1, 0 }; /* one second */
- sdb_llist_iter_t *iter;
-
FD_ZERO(&ready);
FD_ZERO(&exceptions);
ready = sockets;
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 "
"for open connections");
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;
+ break;
}
while (sdb_llist_iter_has_next(iter)) {
sdb_object_t *obj = sdb_llist_iter_get_next(iter);
}
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_llist_iter_destroy(iter);
sdb_log(SDB_LOG_ERR, "frontend: Failed to monitor sockets: %s",
sdb_strerror(errno, buf, sizeof(buf)));
sdb_log(SDB_LOG_ERR, "frontend: Failed to monitor sockets: %s",
sdb_strerror(errno, buf, sizeof(buf)));
- return -1;
+ break;
}
}
-
- if (! n)
+ else if (! n)
continue;
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");
- return -1;
- }
-
- 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_log(SDB_LOG_INFO, "Data on fd %d", CONN(obj)->conn.fd);
- sdb_llist_iter_remove_current(iter);
- sdb_channel_write(chan, &obj);
- }
- }
- sdb_llist_iter_destroy(iter);
+ /* handle new and open connections */
+ if (socket_handle_incoming(sock, &ready, &exceptions))
+ break;
}
}
+ socket_close(sock);
+
sdb_log(SDB_LOG_INFO, "frontend: Waiting for connection handler threads "
"to terminate");
sdb_log(SDB_LOG_INFO, "frontend: Waiting for connection handler threads "
"to terminate");
- if (! sdb_channel_shutdown(chan))
+ if (! sdb_channel_shutdown(sock->chan))
for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
pthread_join(handler_threads[i], NULL);
/* else: we tried our best; let the operating system clean up */
for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i)
pthread_join(handler_threads[i], NULL);
/* else: we tried our best; let the operating system clean up */
+
+ sdb_channel_destroy(sock->chan);
+ sock->chan = NULL;
return 0;
} /* sdb_fe_sock_listen_and_server */
return 0;
} /* sdb_fe_sock_listen_and_server */