s3-messaging/ctdb: split messaging_ctdbd_init()
authorRalph Boehme <slow@samba.org>
Sat, 9 Jul 2016 11:20:01 +0000 (13:20 +0200)
committerRalph Boehme <slow@samba.org>
Mon, 11 Jul 2016 18:05:06 +0000 (20:05 +0200)
Split out and internal function from messaging_ctdbd_init() that does
the connection setup. Keep the conn object allocation in
messaging_ctdbd_init().

This is in preperation of adding messaging_ctdbd_reinit() which will use
the new internal function as well.

messaging_ctdbd_init_internal() has a new reinit flag,
messaging_ctdbd_init() calls with reinit=false resulting in unmodified
behaviour.

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
source3/lib/messages_ctdbd.c

index 1645ccf457e27f83f2c407138903f05ef69fa9c1..cdf8fb7ca0f52900e45b2488944ed707577f670f 100644 (file)
@@ -174,41 +174,40 @@ static void messaging_ctdbd_readable(struct tevent_context *ev,
        ctdbd_socket_readable(conn);
 }
 
-int messaging_ctdbd_init(struct messaging_context *msg_ctx,
-                        TALLOC_CTX *mem_ctx,
-                        struct messaging_backend **presult)
+static int messaging_ctdbd_init_internal(struct messaging_context *msg_ctx,
+                                        TALLOC_CTX *mem_ctx,
+                                        struct messaging_ctdbd_context *ctx,
+                                        bool reinit)
 {
-       struct messaging_backend *result;
-       struct messaging_ctdbd_context *ctx;
        struct tevent_context *ev;
        int ret, ctdb_fd;
 
-       if (!(result = talloc(mem_ctx, struct messaging_backend))) {
-               DEBUG(0, ("talloc failed\n"));
-               return ENOMEM;
-       }
-
-       if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
-               DEBUG(0, ("talloc failed\n"));
-               TALLOC_FREE(result);
-               return ENOMEM;
-       }
-
-       ret = ctdbd_init_connection(ctx, lp_ctdbd_socket(),
-                                   lp_ctdb_timeout(), &ctx->conn);
-
-       if (ret != 0) {
-               DBG_DEBUG("ctdbd_init_connection failed: %s\n",
-                         strerror(ret));
-               TALLOC_FREE(result);
-               return ret;
+       if (reinit) {
+               ret = ctdbd_reinit_connection(ctx,
+                                             lp_ctdbd_socket(),
+                                             lp_ctdb_timeout(),
+                                             ctx->conn);
+               if (ret != 0) {
+                       DBG_ERR("ctdbd_reinit_connection failed: %s\n",
+                               strerror(ret));
+                       return ret;
+               }
+       } else {
+               ret = ctdbd_init_connection(ctx,
+                                           lp_ctdbd_socket(),
+                                           lp_ctdb_timeout(),
+                                           &ctx->conn);
+               if (ret != 0) {
+                       DBG_ERR("ctdbd_init_connection failed: %s\n",
+                               strerror(ret));
+                       return ret;
+               }
        }
 
        ret = register_with_ctdbd(ctx->conn, MSG_SRVID_SAMBA, NULL, NULL);
        if (ret != 0) {
                DBG_DEBUG("Could not register MSG_SRVID_SAMBA: %s\n",
                          strerror(ret));
-               TALLOC_FREE(result);
                return ret;
        }
 
@@ -217,7 +216,6 @@ int messaging_ctdbd_init(struct messaging_context *msg_ctx,
        if (ret != 0) {
                DEBUG(10, ("register_with_ctdbd failed: %s\n",
                           strerror(ret)));
-               TALLOC_FREE(result);
                return ret;
        }
 
@@ -227,7 +225,6 @@ int messaging_ctdbd_init(struct messaging_context *msg_ctx,
        ctx->fde = tevent_add_fd(ev, ctx, ctdb_fd, TEVENT_FD_READ,
                                 messaging_ctdbd_readable, ctx->conn);
        if (ctx->fde == NULL) {
-               TALLOC_FREE(result);
                return ENOMEM;
        }
 
@@ -237,6 +234,34 @@ int messaging_ctdbd_init(struct messaging_context *msg_ctx,
 
        set_my_vnn(ctdbd_vnn(ctx->conn));
 
+       return 0;
+}
+
+int messaging_ctdbd_init(struct messaging_context *msg_ctx,
+                        TALLOC_CTX *mem_ctx,
+                        struct messaging_backend **presult)
+{
+       struct messaging_backend *result;
+       struct messaging_ctdbd_context *ctx;
+       int ret;
+
+       if (!(result = talloc(mem_ctx, struct messaging_backend))) {
+               DEBUG(0, ("talloc failed\n"));
+               return ENOMEM;
+       }
+
+       if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
+               DEBUG(0, ("talloc failed\n"));
+               TALLOC_FREE(result);
+               return ENOMEM;
+       }
+
+       ret = messaging_ctdbd_init_internal(msg_ctx, mem_ctx, ctx, false);
+       if (ret != 0) {
+               TALLOC_FREE(result);
+               return ret;
+       }
+
        result->send_fn = messaging_ctdb_send;
        result->private_data = (void *)ctx;