]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-tests: Re-use async accept wrapper from async_req
authorAmitay Isaacs <amitay@gmail.com>
Fri, 27 May 2016 02:38:20 +0000 (12:38 +1000)
committerAmitay Isaacs <amitay@samba.org>
Wed, 8 Jun 2016 08:33:19 +0000 (10:33 +0200)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/tests/src/comm_server_test.c
ctdb/tests/src/fake_ctdbd.c
ctdb/wscript

index 036baac674086a1d7758b221349f0c730fda27fa..86b5658b949f79b672021f2e72d580d96defd4a1 100644 (file)
 
 #include <assert.h>
 
+#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++;
index 462ecb7753d88a6490972247aef73abc462806bc..35eec9ff62fba1428e087dccdb0b40b6ceeb7794 100644 (file)
@@ -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);
index 744ba966cc9966246df92c1ba593e38922f6e392..44c376db65b8603e1ad1e147272060c9969a6d0b 100755 (executable)
@@ -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: