lib: Push down unique generation one level
[obnox/samba/samba-obnox.git] / source3 / lib / messages_dgm.c
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)));