From 01d7e26f7faa82da55aa66af28c5b224c7caa792 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 12 Oct 2015 21:30:30 +0200 Subject: [PATCH] lib: Push down unique generation one level Signed-off-by: Volker Lendecke Reviewed-by: Ralph Boehme --- source3/lib/messages.c | 6 ++---- source3/lib/messages_dgm.c | 22 +++++++++++++++++++--- source3/lib/messages_dgm.h | 2 +- source3/lib/messages_dgm_ref.c | 10 +++++++++- source3/lib/messages_dgm_ref.h | 2 +- source3/wscript_build | 3 ++- source4/lib/messaging/messaging.c | 2 +- 7 files changed, 35 insertions(+), 12 deletions(-) diff --git a/source3/lib/messages.c b/source3/lib/messages.c index 9025d7b0cd9..ef8e83dd6e6 100644 --- a/source3/lib/messages.c +++ b/source3/lib/messages.c @@ -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); diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c index 1acd2d738d7..006e917648f 100644 --- a/source3/lib/messages_dgm.c +++ b/source3/lib/messages_dgm.c @@ -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))); diff --git a/source3/lib/messages_dgm.h b/source3/lib/messages_dgm.h index d38cfcc65df..a9cbd81f6da 100644 --- a/source3/lib/messages_dgm.h +++ b/source3/lib/messages_dgm.h @@ -25,7 +25,7 @@ #include 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, diff --git a/source3/lib/messages_dgm_ref.c b/source3/lib/messages_dgm_ref.c index 0a6cbf72e48..dd9b965874e 100644 --- a/source3/lib/messages_dgm_ref.c +++ b/source3/lib/messages_dgm_ref.c @@ -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) { diff --git a/source3/lib/messages_dgm_ref.h b/source3/lib/messages_dgm_ref.h index 3df0c06f88d..8f0aff8ec60 100644 --- a/source3/lib/messages_dgm_ref.h +++ b/source3/lib/messages_dgm_ref.h @@ -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, diff --git a/source3/wscript_build b/source3/wscript_build index be0b7e67d57..1f9f757d033 100755 --- a/source3/wscript_build +++ b/source3/wscript_build @@ -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', diff --git a/source4/lib/messaging/messaging.c b/source4/lib/messaging/messaging.c index d91d175e4e5..bbc22ec0e28 100644 --- a/source4/lib/messaging/messaging.c +++ b/source4/lib/messaging/messaging.c @@ -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) { -- 2.34.1