Code

connection: Let sdb_connection_accept() handle all connection setup.
authorSebastian Harl <sh@tokkee.org>
Thu, 15 Jan 2015 08:05:57 +0000 (09:05 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 15 Jan 2015 08:05:57 +0000 (09:05 +0100)
… rather than having it split up between the caller and accept().

src/frontend/connection.c
src/frontend/sock.c
src/include/frontend/connection.h

index 5f368892c0970f89c14085172eb45e2bdf61fece..6b359d347a9f14e3026ebcd6a6df37708eab7da7 100644 (file)
@@ -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
index 7cb985d9735530c30caba90c26cbe85159ecd027..1827886628ae6236e991251e6b1d8ecd9a5ab34c 100644 (file)
@@ -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 "
index 803a195e45c31c2233e4348556368ea8b88538b3..c4f2bc716a75de34b31e5160cecb12fcc6e19a5b 100644 (file)
@@ -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: