lib: Push down unique generation one level
authorVolker Lendecke <vl@samba.org>
Mon, 12 Oct 2015 19:30:30 +0000 (21:30 +0200)
committerVolker Lendecke <vl@samba.org>
Mon, 19 Oct 2015 10:09:10 +0000 (12:09 +0200)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
source3/lib/messages.c
source3/lib/messages_dgm.c
source3/lib/messages_dgm.h
source3/lib/messages_dgm_ref.c
source3/lib/messages_dgm_ref.h
source3/wscript_build
source4/lib/messaging/messaging.c

index 9025d7b0cd9397c65a51c8ac00e633d8df2d52d3..ef8e83dd6e61fa6b13133dd6ffde4cec8d5e97e3 100644 (file)
@@ -303,7 +303,6 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
        ctx->id = (struct server_id) {
                .pid = getpid(), .vnn = NONCLUSTER_VNN
        };
-       ctx->id.unique_id = serverid_get_random_unique_id();
 
        ctx->event_ctx = ev;
 
@@ -340,7 +339,7 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
        }
 
        ctx->msg_dgm_ref = messaging_dgm_ref(
-               ctx, ctx->event_ctx, ctx->id.unique_id,
+               ctx, ctx->event_ctx, &ctx->id.unique_id,
                priv_path, lck_path, messaging_recv_cb, ctx, &ret);
 
        if (ctx->msg_dgm_ref == NULL) {
@@ -400,10 +399,9 @@ NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
        msg_ctx->id = (struct server_id) {
                .pid = getpid(), .vnn = msg_ctx->id.vnn
        };
-       msg_ctx->id.unique_id = serverid_get_random_unique_id();
 
        msg_ctx->msg_dgm_ref = messaging_dgm_ref(
-               msg_ctx, msg_ctx->event_ctx, msg_ctx->id.unique_id,
+               msg_ctx, msg_ctx->event_ctx, &msg_ctx->id.unique_id,
                private_path("msg.sock"), lock_path("msg.lock"),
                messaging_recv_cb, msg_ctx, &ret);
 
index 1acd2d738d774c61b4c3e297808d611f0f815cdb..006e917648fed20bce419a509c9d95b45ce560d1 100644 (file)
@@ -29,6 +29,7 @@
 #include "lib/param/param.h"
 #include "poll_funcs/poll_funcs_tevent.h"
 #include "unix_msg/unix_msg.h"
+#include "lib/util/genrand.h"
 
 struct sun_path_buf {
        /*
@@ -67,12 +68,13 @@ static int messaging_dgm_context_destructor(struct messaging_dgm_context *c);
 
 static int messaging_dgm_lockfile_create(struct messaging_dgm_context *ctx,
                                         pid_t pid, int *plockfile_fd,
-                                        uint64_t unique)
+                                        uint64_t *punique)
 {
        char buf[64];
        int lockfile_fd;
        struct sun_path_buf lockfile_name;
        struct flock lck;
+       uint64_t unique;
        int unique_len, ret;
        ssize_t written;
 
@@ -104,6 +106,19 @@ static int messaging_dgm_lockfile_create(struct messaging_dgm_context *ctx,
                goto fail_close;
        }
 
+       /*
+        * Directly using the binary value for
+        * SERVERID_UNIQUE_ID_NOT_TO_VERIFY is a layering
+        * violation. But including all of ndr here just for this
+        * seems to be a bit overkill to me. Also, messages_dgm might
+        * be replaced sooner or later by something streams-based,
+        * where unique_id generation will be handled differently.
+        */
+
+       do {
+               generate_random_buffer((uint8_t *)&unique, sizeof(unique));
+       } while (unique == UINT64_C(0xFFFFFFFFFFFFFFFF));
+
        unique_len = snprintf(buf, sizeof(buf), "%ju\n", (uintmax_t)unique);
 
        /* shorten a potentially preexisting file */
@@ -124,6 +139,7 @@ static int messaging_dgm_lockfile_create(struct messaging_dgm_context *ctx,
        }
 
        *plockfile_fd = lockfile_fd;
+       *punique = unique;
        return 0;
 
 fail_unlink:
@@ -134,7 +150,7 @@ fail_close:
 }
 
 int messaging_dgm_init(struct tevent_context *ev,
-                      uint64_t unique,
+                      uint64_t *punique,
                       const char *socket_dir,
                       const char *lockfile_dir,
                       void (*recv_cb)(const uint8_t *msg,
@@ -186,7 +202,7 @@ int messaging_dgm_init(struct tevent_context *ev,
        }
 
        ret = messaging_dgm_lockfile_create(ctx, ctx->pid, &ctx->lockfile_fd,
-                                           unique);
+                                           punique);
        if (ret != 0) {
                DEBUG(1, ("%s: messaging_dgm_create_lockfile failed: %s\n",
                          __func__, strerror(ret)));
index d38cfcc65df37c0ac454c990118dbb7e0daf6619..a9cbd81f6da607f27d6bf07a8126d9a13462e799 100644 (file)
@@ -25,7 +25,7 @@
 #include <tevent.h>
 
 int messaging_dgm_init(struct tevent_context *ev,
-                      uint64_t unique,
+                      uint64_t *unique,
                       const char *socket_dir,
                       const char *lockfile_dir,
                       void (*recv_cb)(const uint8_t *msg,
index 0a6cbf72e48fe4c92b8cadc20facb993c4278c6c..dd9b965874e0033cff37ff552bb61511816e14e8 100644 (file)
@@ -40,7 +40,7 @@ static void msg_dgm_ref_recv(const uint8_t *msg, size_t msg_len,
                             int *fds, size_t num_fds, void *private_data);
 
 void *messaging_dgm_ref(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
-                       uint64_t unique,
+                       uint64_t *unique,
                        const char *socket_dir,
                        const char *lockfile_dir,
                        void (*recv_cb)(const uint8_t *msg, size_t msg_len,
@@ -82,6 +82,14 @@ void *messaging_dgm_ref(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
                }
                dgm_pid = getpid();
        } else {
+               int ret;
+               ret = messaging_dgm_get_unique(getpid(), unique);
+               if (ret != 0) {
+                       TALLOC_FREE(result);
+                       *err = ret;
+                       return NULL;
+               }
+
                result->tevent_handle = messaging_dgm_register_tevent_context(
                        result, ev);
                if (result->tevent_handle == NULL) {
index 3df0c06f88df7c413cae4e6f3b179ed09ee7a178..8f0aff8ec600f07ff9f5ff607ec2b5b0d522e12a 100644 (file)
@@ -25,7 +25,7 @@
 #include "replace.h"
 
 void *messaging_dgm_ref(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
-                       uint64_t unique,
+                       uint64_t *unique,
                        const char *socket_dir,
                        const char *lockfile_dir,
                        void (*recv_cb)(const uint8_t *msg, size_t msg_len,
index be0b7e67d57445a2c2535c4b8d06923d62e0be83..1f9f757d0339f2e43ce6bbf3ddc3f5c49ff00a59 100755 (executable)
@@ -299,7 +299,8 @@ bld.SAMBA3_SUBSYSTEM('TDB_LIB',
 
 bld.SAMBA3_LIBRARY('messages_dgm',
                    source='''lib/messages_dgm.c lib/messages_dgm_ref.c''',
-                   deps='talloc UNIX_MSG POLL_FUNCS_TEVENT samba-debug',
+                   deps='''talloc UNIX_MSG POLL_FUNCS_TEVENT samba-debug
+                           genrand''',
                    private_library=True)
 
 bld.SAMBA3_LIBRARY('messages_util',
index d91d175e4e5d694f1806daea8d56faf9fca9812e..bbc22ec0e2844edab10f3d1f0d267be88bf37339 100644 (file)
@@ -342,7 +342,7 @@ struct imessaging_context *imessaging_init(TALLOC_CTX *mem_ctx,
        }
 
        msg->msg_dgm_ref = messaging_dgm_ref(
-               msg, ev, server_id.unique_id, msg->sock_dir, msg->lock_dir,
+               msg, ev, &server_id.unique_id, msg->sock_dir, msg->lock_dir,
                imessaging_dgm_recv, msg, &ret);
 
        if (msg->msg_dgm_ref == NULL) {