From d67e614a07cbf143293436d380aba9a022c0e31b Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 16 May 2013 16:11:54 +0200 Subject: [PATCH] lib: Add before/after hooks to async_connect This will facilitiate [un]become_root for smbd to connect safely to ctdbd. Signed-off-by: Volker Lendecke Reviewed-by: Christian Ambach --- lib/async_req/async_sock.c | 35 +++++++++++++++++++++++++++++++---- lib/async_req/async_sock.h | 10 ++++++---- source3/lib/ctdb_conn.c | 2 +- source3/lib/util_sock.c | 4 ++-- source3/libsmb/unexpected.c | 2 +- source3/torture/wbc_async.c | 2 +- 6 files changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/async_req/async_sock.c b/lib/async_req/async_sock.c index 9909bc6eb3a..59dde885921 100644 --- a/lib/async_req/async_sock.c +++ b/lib/async_req/async_sock.c @@ -217,6 +217,10 @@ struct async_connect_state { long old_sockflags; socklen_t address_len; struct sockaddr_storage address; + + void (*before_connect)(void *private_data); + void (*after_connect)(void *private_data); + void *private_data; }; static void async_connect_connected(struct tevent_context *ev, @@ -236,10 +240,12 @@ static void async_connect_connected(struct tevent_context *ev, * connect in an async state. This will be reset when the request is finished. */ -struct tevent_req *async_connect_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - int fd, const struct sockaddr *address, - socklen_t address_len) +struct tevent_req *async_connect_send( + TALLOC_CTX *mem_ctx, struct tevent_context *ev, int fd, + const struct sockaddr *address, socklen_t address_len, + void (*before_connect)(void *private_data), + void (*after_connect)(void *private_data), + void *private_data) { struct tevent_req *result; struct async_connect_state *state; @@ -258,6 +264,9 @@ struct tevent_req *async_connect_send(TALLOC_CTX *mem_ctx, state->fd = fd; state->sys_errno = 0; + state->before_connect = before_connect; + state->after_connect = after_connect; + state->private_data = private_data; state->old_sockflags = fcntl(fd, F_GETFL, 0); if (state->old_sockflags == -1) { @@ -273,7 +282,16 @@ struct tevent_req *async_connect_send(TALLOC_CTX *mem_ctx, set_blocking(fd, false); + if (state->before_connect != NULL) { + state->before_connect(state->private_data); + } + state->result = connect(fd, address, address_len); + + if (state->after_connect != NULL) { + state->after_connect(state->private_data); + } + if (state->result == 0) { tevent_req_done(result); goto done; @@ -328,8 +346,17 @@ static void async_connect_connected(struct tevent_context *ev, tevent_req_data(req, struct async_connect_state); int ret; + if (state->before_connect != NULL) { + state->before_connect(state->private_data); + } + ret = connect(state->fd, (struct sockaddr *)(void *)&state->address, state->address_len); + + if (state->after_connect != NULL) { + state->after_connect(state->private_data); + } + if (ret == 0) { state->sys_errno = 0; TALLOC_FREE(fde); diff --git a/lib/async_req/async_sock.h b/lib/async_req/async_sock.h index af917bcb836..494b92eb29f 100644 --- a/lib/async_req/async_sock.h +++ b/lib/async_req/async_sock.h @@ -40,10 +40,12 @@ struct tevent_req *recvfrom_send(TALLOC_CTX *mem_ctx, socklen_t *addr_len); ssize_t recvfrom_recv(struct tevent_req *req, int *perrno); -struct tevent_req *async_connect_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - int fd, const struct sockaddr *address, - socklen_t address_len); +struct tevent_req *async_connect_send( + TALLOC_CTX *mem_ctx, struct tevent_context *ev, int fd, + const struct sockaddr *address, socklen_t address_len, + void (*before_connect)(void *private_data), + void (*after_connect)(void *private_data), + void *private_data); int async_connect_recv(struct tevent_req *req, int *perrno); struct tevent_req *writev_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, diff --git a/source3/lib/ctdb_conn.c b/source3/lib/ctdb_conn.c index a96615fb6ca..d7bf6a57551 100644 --- a/source3/lib/ctdb_conn.c +++ b/source3/lib/ctdb_conn.c @@ -83,7 +83,7 @@ struct tevent_req *ctdb_conn_init_send(TALLOC_CTX *mem_ctx, subreq = async_connect_send(state, ev, state->conn->fd, (struct sockaddr *)&state->addr, - sizeof(state->addr)); + sizeof(state->addr), NULL, NULL, NULL); if (tevent_req_nomem(subreq, req)) { return tevent_req_post(req, ev); } diff --git a/source3/lib/util_sock.c b/source3/lib/util_sock.c index 8f212e5d851..eb38055d19a 100644 --- a/source3/lib/util_sock.c +++ b/source3/lib/util_sock.c @@ -586,7 +586,7 @@ struct tevent_req *open_socket_out_send(TALLOC_CTX *mem_ctx, subreq = async_connect_send(state, state->ev, state->fd, (struct sockaddr *)&state->ss, - state->salen); + state->salen, NULL, NULL, NULL); if ((subreq == NULL) || !tevent_req_set_endtime( subreq, state->ev, @@ -638,7 +638,7 @@ static void open_socket_out_connected(struct tevent_req *subreq) subreq = async_connect_send(state, state->ev, state->fd, (struct sockaddr *)&state->ss, - state->salen); + state->salen, NULL, NULL, NULL); if (tevent_req_nomem(subreq, req)) { return; } diff --git a/source3/libsmb/unexpected.c b/source3/libsmb/unexpected.c index f537b3d0a73..2c01bb75154 100644 --- a/source3/libsmb/unexpected.c +++ b/source3/libsmb/unexpected.c @@ -514,7 +514,7 @@ struct tevent_req *nb_packet_reader_send(TALLOC_CTX *mem_ctx, subreq = async_connect_send(state, ev, state->reader->sock, (struct sockaddr *)(void *)&state->addr, - sizeof(state->addr)); + sizeof(state->addr), NULL, NULL, NULL); if (tevent_req_nomem(subreq, req)) { return tevent_req_post(req, ev); } diff --git a/source3/torture/wbc_async.c b/source3/torture/wbc_async.c index 9252b58bc98..71e4de7c88e 100644 --- a/source3/torture/wbc_async.c +++ b/source3/torture/wbc_async.c @@ -288,7 +288,7 @@ static struct tevent_req *wb_connect_send(TALLOC_CTX *mem_ctx, subreq = async_connect_send(mem_ctx, ev, wb_ctx->fd, (struct sockaddr *)(void *)&sunaddr, - sizeof(sunaddr)); + sizeof(sunaddr), NULL, NULL, NULL); if (subreq == NULL) { goto nomem; } -- 2.11.4.GIT