From 6ae8a44475eef39fa376a657df581a52b5472697 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 15 Jan 2015 09:05:57 +0100 Subject: [PATCH] connection: Let sdb_connection_accept() handle all connection setup. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit … rather than having it split up between the caller and accept(). --- src/frontend/connection.c | 11 +++++++++-- src/frontend/sock.c | 26 +++++++------------------- src/include/frontend/connection.h | 12 ++++++++++-- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/frontend/connection.c b/src/frontend/connection.c index 5f36889..6b359d3 100644 --- a/src/frontend/connection.c +++ b/src/frontend/connection.c @@ -407,15 +407,22 @@ sdb_connection_enable_logging(void) } /* sdb_connection_enable_logging */ sdb_conn_t * -sdb_connection_accept(int fd) +sdb_connection_accept(int fd, sdb_conn_setup_cb setup, void *user_data) { + sdb_conn_t *conn; + if (fd < 0) return NULL; /* the placeholder will be replaced with the accepted file * descriptor when initializing the object */ - return CONN(sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER, + conn = CONN(sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER, connection_type, fd)); + if (setup && (setup(conn, user_data) < 0)) { + sdb_object_deref(SDB_OBJ(conn)); + return NULL; + } + return conn; } /* sdb_connection_create */ void diff --git a/src/frontend/sock.c b/src/frontend/sock.c index 7cb985d..1827886 100644 --- a/src/frontend/sock.c +++ b/src/frontend/sock.c @@ -78,8 +78,7 @@ typedef struct { int type; int sock_fd; - int (*accept)(sdb_conn_t *); - int (*peer)(sdb_conn_t *); + int (*setup)(sdb_conn_t *, void *); } listener_t; typedef struct { @@ -106,7 +105,7 @@ struct sdb_fe_socket { */ static int -unixsock_peer(sdb_conn_t *conn) +setup_unixsock(sdb_conn_t *conn, void __attribute__((unused)) *user_data) { uid_t uid; @@ -143,7 +142,7 @@ unixsock_peer(sdb_conn_t *conn) return -1; } return 0; -} /* unixsock_peer */ +} /* setup_unixsock */ static int open_unixsock(listener_t *listener) @@ -199,7 +198,7 @@ open_unixsock(listener_t *listener) return -1; } - listener->peer = unixsock_peer; + listener->setup = setup_unixsock; return 0; } /* open_unixsock */ @@ -416,8 +415,7 @@ listener_create(sdb_fe_socket_t *sock, const char *address) return NULL; } listener->type = type; - listener->accept = NULL; - listener->peer = NULL; + listener->setup = NULL; if (listener_impls[type].open(listener)) { /* prints error */ @@ -511,21 +509,11 @@ connection_accept(sdb_fe_socket_t *sock, listener_t *listener) sdb_object_t *obj; int status; - obj = SDB_OBJ(sdb_connection_accept(listener->sock_fd)); + obj = SDB_OBJ(sdb_connection_accept(listener->sock_fd, + listener->setup, NULL)); if (! obj) return -1; - if (listener->accept && listener->accept(CONN(obj))) { - /* accept() is expected to log an error */ - sdb_object_deref(obj); - return -1; - } - if (listener->peer && listener->peer(CONN(obj))) { - /* peer() is expected to log an error */ - sdb_object_deref(obj); - return -1; - } - status = sdb_llist_append(sock->open_connections, obj); if (status) sdb_log(SDB_LOG_ERR, "frontend: Failed to append " diff --git a/src/include/frontend/connection.h b/src/include/frontend/connection.h index 803a195..c4f2bc7 100644 --- a/src/include/frontend/connection.h +++ b/src/include/frontend/connection.h @@ -58,6 +58,12 @@ typedef struct { } sdb_conn_node_t; #define SDB_CONN_NODE(obj) ((sdb_conn_node_t *)(obj)) +/* + * sdb_conn_setup_cb is a callback function used to setup a connection. For + * example, it may be used to initialize session information. + */ +typedef int (*sdb_conn_setup_cb)(sdb_conn_t *, void *); + /* * sdb_connection_enable_logging: * Enable logging of connection-related messages to the current client @@ -75,14 +81,16 @@ sdb_connection_enable_logging(void); /* * sdb_connection_accpet: * Accept a new connection on the specified file-descriptor 'fd' and return a - * newly allocated connection object. + * newly allocated connection object. If specified, the setup callback is used + * to setup the newly created connection. It will receive the connection + * object and the specified user data as its arguments. * * Returns: * - 0 on success * - a negative value else */ sdb_conn_t * -sdb_connection_accept(int fd); +sdb_connection_accept(int fd, sdb_conn_setup_cb setup, void *user_data); /* * sdb_connection_close: -- 2.30.2