From: Amitay Isaacs Date: Fri, 27 May 2016 02:38:20 +0000 (+1000) Subject: ctdb-tests: Re-use async accept wrapper from async_req X-Git-Tag: tdb-1.3.10~911 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=30317325041b18031cb02519f7195a3cbbe0fb25;p=thirdparty%2Fsamba.git ctdb-tests: Re-use async accept wrapper from async_req Signed-off-by: Amitay Isaacs Reviewed-by: Martin Schwenke --- diff --git a/ctdb/tests/src/comm_server_test.c b/ctdb/tests/src/comm_server_test.c index 036baac6740..86b5658b949 100644 --- a/ctdb/tests/src/comm_server_test.c +++ b/ctdb/tests/src/comm_server_test.c @@ -22,87 +22,12 @@ #include +#include "lib/async_req/async_sock.h" + #include "common/pkt_read.c" #include "common/pkt_write.c" #include "common/comm.c" - -struct accept_state { - int listen_fd; - struct tevent_fd *fde; - int client_fd; -}; - -static void accept_handler(struct tevent_context *ev, struct tevent_fd *fde, - uint16_t flags, void *private_data); - -static struct tevent_req *accept_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - int listen_fd) -{ - struct tevent_req *req; - struct accept_state *state; - - req = tevent_req_create(mem_ctx, &state, struct accept_state); - if (req == NULL) { - return NULL; - } - - state->listen_fd = listen_fd; - - state->fde = tevent_add_fd(ev, state, listen_fd, TEVENT_FD_READ, - accept_handler, req); - if (tevent_req_nomem(state->fde, req)) { - return tevent_req_post(req, ev); - } - return req; -} - -static void accept_handler(struct tevent_context *ev, struct tevent_fd *fde, - uint16_t flags, void *private_data) -{ - struct tevent_req *req = talloc_get_type_abort( - private_data, struct tevent_req); - struct accept_state *state = tevent_req_data( - req, struct accept_state); - struct sockaddr addr; - socklen_t addrlen = sizeof(addr); - int ret; - - TALLOC_FREE(state->fde); - - if ((flags & TEVENT_FD_READ) == 0) { - tevent_req_error(req, EIO); - return; - } - - ret = accept(state->listen_fd, &addr, &addrlen); - if (ret == -1) { - tevent_req_error(req, errno); - return; - } - - state->client_fd = ret; - tevent_req_done(req); -} - -static int accept_recv(struct tevent_req *req, int *perr) -{ - struct accept_state *state = tevent_req_data( - req, struct accept_state); - int err; - - if (tevent_req_is_unix_error(req, &err)) { - if (perr != NULL) { - *perr = err; - } - return -1; - } - - return state->client_fd; -} - - struct echo_state { struct tevent_context *ev; int fd; @@ -250,7 +175,7 @@ static void socket_process_client(struct tevent_req *subreq) int client_fd; int err = 0; - client_fd = accept_recv(subreq, &err); + client_fd = accept_recv(subreq, NULL, NULL, &err); TALLOC_FREE(subreq); state->num_clients++; diff --git a/ctdb/tests/src/fake_ctdbd.c b/ctdb/tests/src/fake_ctdbd.c index 462ecb7753d..35eec9ff62f 100644 --- a/ctdb/tests/src/fake_ctdbd.c +++ b/ctdb/tests/src/fake_ctdbd.c @@ -30,6 +30,7 @@ #include "lib/util/tevent_unix.h" #include "lib/util/debug.h" #include "lib/util/samba_util.h" +#include "lib/async_req/async_sock.h" #include "protocol/protocol.h" #include "protocol/protocol_api.h" @@ -96,86 +97,6 @@ struct ctdbd_context { bool takeover_disabled; }; - -/* - * async wrapper around accept(2) - */ - -struct accept_state { - int listen_fd; - struct tevent_fd *fde; - int client_fd; -}; - -static void accept_handler(struct tevent_context *ev, struct tevent_fd *fde, - uint16_t flags, void *private_data); - -static struct tevent_req *accept_send(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - int listen_fd) -{ - struct tevent_req *req; - struct accept_state *state; - - req = tevent_req_create(mem_ctx, &state, struct accept_state); - if (req == NULL) { - return NULL; - } - - state->listen_fd = listen_fd; - - state->fde = tevent_add_fd(ev, state, listen_fd, TEVENT_FD_READ, - accept_handler, req); - if (tevent_req_nomem(state->fde, req)) { - return tevent_req_post(req, ev); - } - return req; -} - -static void accept_handler(struct tevent_context *ev, struct tevent_fd *fde, - uint16_t flags, void *private_data) -{ - struct tevent_req *req = talloc_get_type_abort( - private_data, struct tevent_req); - struct accept_state *state = tevent_req_data( - req, struct accept_state); - struct sockaddr addr; - socklen_t addrlen = sizeof(addr); - int ret; - - TALLOC_FREE(state->fde); - - if ((flags & TEVENT_FD_READ) == 0) { - tevent_req_error(req, EIO); - return; - } - - ret = accept(state->listen_fd, &addr, &addrlen); - if (ret == -1) { - tevent_req_error(req, errno); - return; - } - - state->client_fd = ret; - tevent_req_done(req); -} - -static int accept_recv(struct tevent_req *req, int *perr) -{ - struct accept_state *state = tevent_req_data( - req, struct accept_state); - int err; - - if (tevent_req_is_unix_error(req, &err)) { - if (perr != NULL) { - *perr = err; - } - return -1; - } - - return state->client_fd; -} - /* * Parse routines */ @@ -2028,7 +1949,7 @@ static void server_new_client(struct tevent_req *subreq) int client_fd; int ret = 0; - client_fd = accept_recv(subreq, &ret); + client_fd = accept_recv(subreq, NULL, NULL, &ret); TALLOC_FREE(subreq); if (client_fd == -1) { tevent_req_error(req, ret); diff --git a/ctdb/wscript b/ctdb/wscript index 744ba966cc9..44c376db65b 100755 --- a/ctdb/wscript +++ b/ctdb/wscript @@ -34,6 +34,7 @@ samba_dist.DIST_DIRS('''ctdb:. lib/replace:lib/replace lib/talloc:lib/talloc third_party/popt:third_party/popt lib/util:lib/util lib/tdb_wrap:lib/tdb_wrap lib/ccan:lib/ccan libcli/util:libcli/util + lib/async_req:lib/async_req buildtools:buildtools third_party/waf:third_party/waf''') manpages = [ @@ -276,6 +277,7 @@ def build(bld): bld.RECURSE('lib/tdb_wrap') bld.RECURSE('lib/util') + bld.RECURSE('lib/async_req') bld.RECURSE('lib/talloc') bld.RECURSE('lib/tevent') @@ -654,7 +656,7 @@ def build(bld): bld.SAMBA_BINARY(target, source=src, - deps='talloc tevent tdb tevent-util', + deps='talloc tevent tdb tevent-util LIBASYNC_REQ', install_path='${CTDB_TEST_LIBDIR}') bld.SAMBA_BINARY('reqid_test', @@ -708,7 +710,7 @@ def build(bld): bld.SAMBA_BINARY('fake_ctdbd', source='tests/src/fake_ctdbd.c', deps='''ctdb-util ctdb-protocol ctdb-system - samba-util tevent-util popt''', + samba-util tevent-util LIBASYNC_REQ popt''', install_path='${CTDB_TEST_LIBDIR}') if bld.env.HAVE_INFINIBAND: