[PATCH] Some more messaging work

Volker Lendecke Volker.Lendecke at SerNet.DE
Sat Aug 9 09:31:52 MDT 2014


Hi!

Attached find a set of patches that make messages_dgm a lot
more independent of source3/ infrastructure. It's not fully
there yet, but it's an important step towards source4
usability I guess.

Review&Push would be appreciated.

Thanks,

Volker

-- 
SerNet GmbH, Bahnhofsallee 1b, 37081 Göttingen
phone: +49-551-370000-0, fax: +49-551-370000-9
AG Göttingen, HRB 2816, GF: Dr. Johannes Loxen
http://www.sernet.de, mailto:kontakt at sernet.de
-------------- next part --------------
From 94d3fa478362dfd1e8259903619af2c6d54786e7 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 09:44:41 +0000
Subject: [PATCH 01/21] messaging_dgm: Receive through a cb function

This avoids calling messaging_dispatch_rec directly from messaging_dgm.c

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/include/messages.h |  9 ++++++++-
 source3/lib/messages.c     | 27 +++++++++++++++++++++++++--
 source3/lib/messages_dgm.c | 34 ++++++++++++++++++++++------------
 3 files changed, 55 insertions(+), 15 deletions(-)

diff --git a/source3/include/messages.h b/source3/include/messages.h
index b718dd7..32c4097 100644
--- a/source3/include/messages.h
+++ b/source3/include/messages.h
@@ -75,7 +75,14 @@ struct messaging_backend {
 
 int messaging_dgm_init(struct messaging_context *msg_ctx,
 		       TALLOC_CTX *mem_ctx,
-		       struct messaging_backend **presult);
+		       struct messaging_backend **presult,
+		       void (*recv_cb)(int msg_type,
+				       struct server_id src,
+				       struct server_id dst,
+				       const uint8_t *msg,
+				       size_t msg_len,
+				       void *private_data),
+		       void *recv_cb_private_data);
 int messaging_dgm_cleanup(struct messaging_context *msg_ctx, pid_t pid);
 int messaging_dgm_wipe(struct messaging_context *msg_ctx);
 void *messaging_dgm_register_tevent_context(TALLOC_CTX *mem_ctx,
diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index bbc5183..2e80bab 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -201,6 +201,27 @@ bool message_send_all(struct messaging_context *msg_ctx,
 	return true;
 }
 
+static void messaging_recv_cb(int msg_type,
+			      struct server_id src, struct server_id dst,
+			      const uint8_t *msg, size_t msg_len,
+			      void *private_data)
+{
+	struct messaging_context *msg_ctx = talloc_get_type_abort(
+		private_data, struct messaging_context);
+	struct messaging_rec rec;
+
+	rec = (struct messaging_rec) {
+		.msg_version = MESSAGE_VERSION,
+		.msg_type = msg_type,
+		.src = src,
+		.dest = dst,
+		.buf.data = discard_const_p(uint8, msg),
+		.buf.length = msg_len
+	};
+
+	messaging_dispatch_rec(msg_ctx, &rec);
+}
+
 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, 
 					 struct tevent_context *ev)
 {
@@ -223,7 +244,8 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 	ctx->event_ctx = ev;
 	ctx->have_context = &have_context;
 
-	ret = messaging_dgm_init(ctx, ctx, &ctx->local);
+	ret = messaging_dgm_init(ctx, ctx, &ctx->local,
+				 messaging_recv_cb, ctx);
 
 	if (ret != 0) {
 		DEBUG(2, ("messaging_dgm_init failed: %s\n", strerror(ret)));
@@ -281,7 +303,8 @@ NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
 
 	msg_ctx->id = procid_self();
 
-	ret = messaging_dgm_init(msg_ctx, msg_ctx, &msg_ctx->local);
+	ret = messaging_dgm_init(msg_ctx, msg_ctx, &msg_ctx->local,
+				 messaging_recv_cb, msg_ctx);
 	if (ret != 0) {
 		DEBUG(0, ("messaging_dgm_init failed: %s\n", strerror(errno)));
 		return map_nt_error_from_unix(ret);
diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index 8b897f6..4f9e07c 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -35,6 +35,12 @@ struct messaging_dgm_context {
 	struct unix_msg_ctx *dgm_ctx;
 	char *cache_dir;
 	int lockfile_fd;
+
+	void (*recv_cb)(int msg_type,
+			struct server_id src, struct server_id dst,
+			const uint8_t *msg, size_t msg_len,
+			void *private_data);
+	void *recv_cb_private_data;
 };
 
 struct messaging_dgm_hdr {
@@ -167,7 +173,14 @@ static int messaging_dgm_lockfile_remove(TALLOC_CTX *tmp_ctx,
 
 int messaging_dgm_init(struct messaging_context *msg_ctx,
 		       TALLOC_CTX *mem_ctx,
-		       struct messaging_backend **presult)
+		       struct messaging_backend **presult,
+		       void (*recv_cb)(int msg_type,
+				       struct server_id src,
+				       struct server_id dst,
+				       const uint8_t *msg,
+				       size_t msg_len,
+				       void *private_data),
+		       void *recv_cb_private_data)
 {
 	struct messaging_backend *result;
 	struct messaging_dgm_context *ctx;
@@ -198,6 +211,9 @@ int messaging_dgm_init(struct messaging_context *msg_ctx,
 	result->send_fn = messaging_dgm_send;
 	ctx->msg_ctx = msg_ctx;
 
+	ctx->recv_cb = recv_cb;
+	ctx->recv_cb_private_data = recv_cb_private_data;
+
 	ctx->cache_dir = talloc_strdup(ctx, cache_dir);
 	if (ctx->cache_dir == NULL) {
 		goto fail_nomem;
@@ -335,7 +351,6 @@ static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
 	struct messaging_dgm_context *dgm_ctx = talloc_get_type_abort(
 		private_data, struct messaging_dgm_context);
 	struct messaging_dgm_hdr *hdr;
-	struct messaging_rec rec;
 	struct server_id_buf idbuf;
 
 	if (msg_len < sizeof(*hdr)) {
@@ -348,18 +363,13 @@ static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
 	 */
 	hdr = (struct messaging_dgm_hdr *)msg;
 
-	rec.msg_version = hdr->msg_version;
-	rec.msg_type = hdr->msg_type;
-	rec.dest = hdr->dst;
-	rec.src = hdr->src;
-	rec.buf.data = msg + sizeof(*hdr);
-	rec.buf.length = msg_len - sizeof(*hdr);
-
 	DEBUG(10, ("%s: Received message 0x%x len %u from %s\n", __func__,
-		   (unsigned)hdr->msg_type, (unsigned)rec.buf.length,
-		   server_id_str_buf(rec.src, &idbuf)));
+		   (unsigned)hdr->msg_type, (unsigned)(msg_len - sizeof(*hdr)),
+		   server_id_str_buf(hdr->src, &idbuf)));
 
-	messaging_dispatch_rec(dgm_ctx->msg_ctx, &rec);
+	dgm_ctx->recv_cb(hdr->msg_type, hdr->src, hdr->dst,
+			 msg + sizeof(*hdr), msg_len - sizeof(*hdr),
+			 dgm_ctx->recv_cb_private_data);
 }
 
 int messaging_dgm_cleanup(struct messaging_context *msg_ctx, pid_t pid)
-- 
1.8.1.2


From ecb2586efdf051dbcf78416f3ef44aa7fad534a2 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 09:52:28 +0000
Subject: [PATCH 02/21] messaging3: Explicitly pass tevent_context to
 messaging_dgm_init

One dependency less on messaging_context()

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/include/messages.h | 1 +
 source3/lib/messages.c     | 5 +++--
 source3/lib/messages_dgm.c | 4 ++--
 3 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/source3/include/messages.h b/source3/include/messages.h
index 32c4097..4cc9053 100644
--- a/source3/include/messages.h
+++ b/source3/include/messages.h
@@ -75,6 +75,7 @@ struct messaging_backend {
 
 int messaging_dgm_init(struct messaging_context *msg_ctx,
 		       TALLOC_CTX *mem_ctx,
+		       struct tevent_context *ev,
 		       struct messaging_backend **presult,
 		       void (*recv_cb)(int msg_type,
 				       struct server_id src,
diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index 2e80bab..33fd45e 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -244,7 +244,7 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 	ctx->event_ctx = ev;
 	ctx->have_context = &have_context;
 
-	ret = messaging_dgm_init(ctx, ctx, &ctx->local,
+	ret = messaging_dgm_init(ctx, ctx, ctx->event_ctx, &ctx->local,
 				 messaging_recv_cb, ctx);
 
 	if (ret != 0) {
@@ -303,7 +303,8 @@ NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
 
 	msg_ctx->id = procid_self();
 
-	ret = messaging_dgm_init(msg_ctx, msg_ctx, &msg_ctx->local,
+	ret = messaging_dgm_init(msg_ctx, msg_ctx, msg_ctx->event_ctx,
+				 &msg_ctx->local,
 				 messaging_recv_cb, msg_ctx);
 	if (ret != 0) {
 		DEBUG(0, ("messaging_dgm_init failed: %s\n", strerror(errno)));
diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index 4f9e07c..ebd98fe 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -173,6 +173,7 @@ static int messaging_dgm_lockfile_remove(TALLOC_CTX *tmp_ctx,
 
 int messaging_dgm_init(struct messaging_context *msg_ctx,
 		       TALLOC_CTX *mem_ctx,
+		       struct tevent_context *ev,
 		       struct messaging_backend **presult,
 		       void (*recv_cb)(int msg_type,
 				       struct server_id src,
@@ -249,8 +250,7 @@ int messaging_dgm_init(struct messaging_context *msg_ctx,
 	}
 
 	ctx->tevent_handle = poll_funcs_tevent_register(
-		ctx, ctx->msg_callbacks,
-		messaging_tevent_context(msg_ctx));
+		ctx, ctx->msg_callbacks, ev);
 	if (ctx->tevent_handle == NULL) {
 		goto fail_nomem;
 	}
-- 
1.8.1.2


From 65679a8a345b65898215c43482fd3ec9b5656992 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 09:56:39 +0000
Subject: [PATCH 03/21] messaging3: Explicitly pass server_id to
 messaging_dgm_init

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/include/messages.h | 1 +
 source3/lib/messages.c     | 6 +++---
 source3/lib/messages_dgm.c | 6 ++++--
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/source3/include/messages.h b/source3/include/messages.h
index 4cc9053..2882483 100644
--- a/source3/include/messages.h
+++ b/source3/include/messages.h
@@ -76,6 +76,7 @@ struct messaging_backend {
 int messaging_dgm_init(struct messaging_context *msg_ctx,
 		       TALLOC_CTX *mem_ctx,
 		       struct tevent_context *ev,
+		       struct server_id pid,
 		       struct messaging_backend **presult,
 		       void (*recv_cb)(int msg_type,
 				       struct server_id src,
diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index 33fd45e..ef87ebb 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -244,8 +244,8 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 	ctx->event_ctx = ev;
 	ctx->have_context = &have_context;
 
-	ret = messaging_dgm_init(ctx, ctx, ctx->event_ctx, &ctx->local,
-				 messaging_recv_cb, ctx);
+	ret = messaging_dgm_init(ctx, ctx, ctx->event_ctx, ctx->id,
+				 &ctx->local, messaging_recv_cb, ctx);
 
 	if (ret != 0) {
 		DEBUG(2, ("messaging_dgm_init failed: %s\n", strerror(ret)));
@@ -304,7 +304,7 @@ NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
 	msg_ctx->id = procid_self();
 
 	ret = messaging_dgm_init(msg_ctx, msg_ctx, msg_ctx->event_ctx,
-				 &msg_ctx->local,
+				 msg_ctx->id, &msg_ctx->local,
 				 messaging_recv_cb, msg_ctx);
 	if (ret != 0) {
 		DEBUG(0, ("messaging_dgm_init failed: %s\n", strerror(errno)));
diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index ebd98fe..1e71c78 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -30,6 +30,7 @@
 
 struct messaging_dgm_context {
 	struct messaging_context *msg_ctx;
+	struct server_id pid;
 	struct poll_funcs *msg_callbacks;
 	void *tevent_handle;
 	struct unix_msg_ctx *dgm_ctx;
@@ -174,6 +175,7 @@ static int messaging_dgm_lockfile_remove(TALLOC_CTX *tmp_ctx,
 int messaging_dgm_init(struct messaging_context *msg_ctx,
 		       TALLOC_CTX *mem_ctx,
 		       struct tevent_context *ev,
+		       struct server_id pid,
 		       struct messaging_backend **presult,
 		       void (*recv_cb)(int msg_type,
 				       struct server_id src,
@@ -185,7 +187,6 @@ int messaging_dgm_init(struct messaging_context *msg_ctx,
 {
 	struct messaging_backend *result;
 	struct messaging_dgm_context *ctx;
-	struct server_id pid = messaging_server_id(msg_ctx);
 	int ret;
 	bool ok;
 	const char *cache_dir;
@@ -211,6 +212,7 @@ int messaging_dgm_init(struct messaging_context *msg_ctx,
 	result->private_data = ctx;
 	result->send_fn = messaging_dgm_send;
 	ctx->msg_ctx = msg_ctx;
+	ctx->pid = pid;
 
 	ctx->recv_cb = recv_cb;
 	ctx->recv_cb_private_data = recv_cb_private_data;
@@ -287,7 +289,7 @@ fail_nomem:
 
 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c)
 {
-	struct server_id pid = messaging_server_id(c->msg_ctx);
+	struct server_id pid = c->pid;
 
 	/*
 	 * First delete the socket to avoid races. The lockfile is the
-- 
1.8.1.2


From 2d44d36453138835220d51c070944ed96a60298b Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 09:58:50 +0000
Subject: [PATCH 04/21] messaging_dgm: Remove unused "messaging_context"

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/include/messages.h | 3 +--
 source3/lib/messages.c     | 4 ++--
 source3/lib/messages_dgm.c | 5 +----
 3 files changed, 4 insertions(+), 8 deletions(-)

diff --git a/source3/include/messages.h b/source3/include/messages.h
index 2882483..669d7f1 100644
--- a/source3/include/messages.h
+++ b/source3/include/messages.h
@@ -73,8 +73,7 @@ struct messaging_backend {
 	void *private_data;
 };
 
-int messaging_dgm_init(struct messaging_context *msg_ctx,
-		       TALLOC_CTX *mem_ctx,
+int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		       struct tevent_context *ev,
 		       struct server_id pid,
 		       struct messaging_backend **presult,
diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index ef87ebb..5cb3fde 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -244,7 +244,7 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 	ctx->event_ctx = ev;
 	ctx->have_context = &have_context;
 
-	ret = messaging_dgm_init(ctx, ctx, ctx->event_ctx, ctx->id,
+	ret = messaging_dgm_init(ctx, ctx->event_ctx, ctx->id,
 				 &ctx->local, messaging_recv_cb, ctx);
 
 	if (ret != 0) {
@@ -303,7 +303,7 @@ NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
 
 	msg_ctx->id = procid_self();
 
-	ret = messaging_dgm_init(msg_ctx, msg_ctx, msg_ctx->event_ctx,
+	ret = messaging_dgm_init(msg_ctx, msg_ctx->event_ctx,
 				 msg_ctx->id, &msg_ctx->local,
 				 messaging_recv_cb, msg_ctx);
 	if (ret != 0) {
diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index 1e71c78..238c9e3 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -29,7 +29,6 @@
 #include "librpc/gen_ndr/messaging.h"
 
 struct messaging_dgm_context {
-	struct messaging_context *msg_ctx;
 	struct server_id pid;
 	struct poll_funcs *msg_callbacks;
 	void *tevent_handle;
@@ -172,8 +171,7 @@ static int messaging_dgm_lockfile_remove(TALLOC_CTX *tmp_ctx,
 	return ret;
 }
 
-int messaging_dgm_init(struct messaging_context *msg_ctx,
-		       TALLOC_CTX *mem_ctx,
+int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		       struct tevent_context *ev,
 		       struct server_id pid,
 		       struct messaging_backend **presult,
@@ -211,7 +209,6 @@ int messaging_dgm_init(struct messaging_context *msg_ctx,
 
 	result->private_data = ctx;
 	result->send_fn = messaging_dgm_send;
-	ctx->msg_ctx = msg_ctx;
 	ctx->pid = pid;
 
 	ctx->recv_cb = recv_cb;
-- 
1.8.1.2


From 75365062f63be5528936dc1a15ceb4007a5df8a9 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 11:01:00 +0000
Subject: [PATCH 05/21] messaging3: Add messaging_cleanup

Rename smbcontrol's dgm-cleanup to msg-cleanup. We haven't published
this UI yet :-)

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/include/messages.h |  2 ++
 source3/lib/messages.c     | 13 +++++++++++++
 source3/utils/smbcontrol.c | 10 +++-------
 3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/source3/include/messages.h b/source3/include/messages.h
index 669d7f1..32c636f 100644
--- a/source3/include/messages.h
+++ b/source3/include/messages.h
@@ -151,6 +151,8 @@ struct tevent_req *messaging_read_send(TALLOC_CTX *mem_ctx,
 int messaging_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
 			struct messaging_rec **presult);
 
+int messaging_cleanup(struct messaging_context *msg_ctx, pid_t pid);
+
 bool messaging_parent_dgm_cleanup_init(struct messaging_context *msg);
 
 #include "librpc/gen_ndr/ndr_messaging.h"
diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index 5cb3fde..78a867a 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -928,6 +928,19 @@ static void mess_parent_dgm_cleanup_done(struct tevent_req *req)
 	tevent_req_set_callback(req, mess_parent_dgm_cleanup_done, msg);
 }
 
+int messaging_cleanup(struct messaging_context *msg_ctx, pid_t pid)
+{
+	int ret;
+
+	if (pid == 0) {
+		ret = messaging_dgm_wipe(msg_ctx);
+	} else {
+		ret = messaging_dgm_cleanup(msg_ctx, pid);
+	}
+
+	return ret;
+}
+
 struct messaging_backend *messaging_local_backend(
 	struct messaging_context *msg_ctx)
 {
diff --git a/source3/utils/smbcontrol.c b/source3/utils/smbcontrol.c
index edd2edc..7c516c4 100644
--- a/source3/utils/smbcontrol.c
+++ b/source3/utils/smbcontrol.c
@@ -968,18 +968,14 @@ static bool do_num_children(struct tevent_context *ev_ctx,
 	return num_replies;
 }
 
-static bool do_dgm_cleanup(struct tevent_context *ev_ctx,
+static bool do_msg_cleanup(struct tevent_context *ev_ctx,
 			   struct messaging_context *msg_ctx,
 			   const struct server_id pid,
 			   const int argc, const char **argv)
 {
 	int ret;
 
-	if (pid.pid != 0) {
-		ret = messaging_dgm_cleanup(msg_ctx, pid.pid);
-	} else {
-		ret = messaging_dgm_wipe(msg_ctx);
-	}
+	ret = messaging_cleanup(msg_ctx, pid.pid);
 
 	printf("cleanup(%u) returned %s\n", (unsigned)pid.pid,
 	       ret ? strerror(ret) : "ok");
@@ -1397,7 +1393,7 @@ static const struct {
 	{ "notify-cleanup", do_notify_cleanup },
 	{ "num-children", do_num_children,
 	  "Print number of smbd child processes" },
-	{ "dgm-cleanup", do_dgm_cleanup },
+	{ "msg-cleanup", do_msg_cleanup },
 	{ "noop", do_noop, "Do nothing" },
 	{ NULL }
 };
-- 
1.8.1.2


From f137482ead725d9529ddcb0dc35717e9e358df8e Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 11:05:02 +0000
Subject: [PATCH 06/21] smbd: Use messaging_cleanup()

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/smbd/server.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/source3/smbd/server.c b/source3/smbd/server.c
index bc07c0f..bea25cb 100644
--- a/source3/smbd/server.c
+++ b/source3/smbd/server.c
@@ -420,8 +420,8 @@ static void remove_child_pid(struct smbd_parent_context *parent,
 
 	child_id = pid_to_procid(pid);
 
-	ret = messaging_dgm_cleanup(parent->msg_ctx, pid);
-	DEBUG(10, ("%s: messaging_dgm_cleanup returned %s\n",
+	ret = messaging_cleanup(parent->msg_ctx, pid);
+	DEBUG(10, ("%s: messaging_cleanup returned %s\n",
 		   __func__, ret ? strerror(ret) : "ok"));
 
 	for (child = parent->children; child != NULL; child = child->next) {
-- 
1.8.1.2


From 0814b96d33946df269357a54c4323dae3a5beb0a Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 11:23:46 +0000
Subject: [PATCH 07/21] messaging3: Directly refer to messaging_dgm in
 messages.c

This removes the messaging_backend abstraction layer from messages_dgm.c. That
layer was introduced for ctdb and is still used there. But as the messaging_dgm
interface is very slim anyway, I don't think directly calling it is too bad.

Why this commit? It is another step towards making messages_dgm
independent of messages.[ch], thus it might become usable in other
contexts like ctdb and source4

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/include/messages.h | 16 +++++++------
 source3/lib/messages.c     | 27 +++++++++-------------
 source3/lib/messages_dgm.c | 57 +++++++++++++---------------------------------
 3 files changed, 36 insertions(+), 64 deletions(-)

diff --git a/source3/include/messages.h b/source3/include/messages.h
index 32c636f..3281610 100644
--- a/source3/include/messages.h
+++ b/source3/include/messages.h
@@ -73,21 +73,25 @@ struct messaging_backend {
 	void *private_data;
 };
 
+struct messaging_dgm_context;
 int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		       struct tevent_context *ev,
 		       struct server_id pid,
-		       struct messaging_backend **presult,
 		       void (*recv_cb)(int msg_type,
 				       struct server_id src,
 				       struct server_id dst,
 				       const uint8_t *msg,
 				       size_t msg_len,
 				       void *private_data),
-		       void *recv_cb_private_data);
-int messaging_dgm_cleanup(struct messaging_context *msg_ctx, pid_t pid);
-int messaging_dgm_wipe(struct messaging_context *msg_ctx);
+		       void *recv_cb_private_data,
+		       struct messaging_dgm_context **pctx);
+int messaging_dgm_send(struct messaging_dgm_context *ctx,
+		       struct server_id src, struct server_id pid,
+		       int msg_type, const struct iovec *iov, int iovlen);
+int messaging_dgm_cleanup(struct messaging_dgm_context *ctx, pid_t pid);
+int messaging_dgm_wipe(struct messaging_dgm_context *ctx);
 void *messaging_dgm_register_tevent_context(TALLOC_CTX *mem_ctx,
-					    struct messaging_context *msg_ctx,
+					    struct messaging_dgm_context *ctx,
 					    struct tevent_context *ev);
 
 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
@@ -105,8 +109,6 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 struct server_id messaging_server_id(const struct messaging_context *msg_ctx);
 struct tevent_context *messaging_tevent_context(
 	struct messaging_context *msg_ctx);
-struct messaging_backend *messaging_local_backend(
-	struct messaging_context *msg_ctx);
 
 /*
  * re-init after a fork
diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index 78a867a..18376bb 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -72,7 +72,8 @@ struct messaging_context {
 	struct tevent_req **waiters;
 	unsigned num_waiters;
 
-	struct messaging_backend *local;
+	struct messaging_dgm_context *local;
+
 	struct messaging_backend *remote;
 
 	bool *have_context;
@@ -245,7 +246,7 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 	ctx->have_context = &have_context;
 
 	ret = messaging_dgm_init(ctx, ctx->event_ctx, ctx->id,
-				 &ctx->local, messaging_recv_cb, ctx);
+				 messaging_recv_cb, ctx, &ctx->local);
 
 	if (ret != 0) {
 		DEBUG(2, ("messaging_dgm_init failed: %s\n", strerror(ret)));
@@ -304,8 +305,8 @@ NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
 	msg_ctx->id = procid_self();
 
 	ret = messaging_dgm_init(msg_ctx, msg_ctx->event_ctx,
-				 msg_ctx->id, &msg_ctx->local,
-				 messaging_recv_cb, msg_ctx);
+				 msg_ctx->id, messaging_recv_cb, msg_ctx,
+				 &msg_ctx->local);
 	if (ret != 0) {
 		DEBUG(0, ("messaging_dgm_init failed: %s\n", strerror(errno)));
 		return map_nt_error_from_unix(ret);
@@ -468,8 +469,8 @@ NTSTATUS messaging_send_iov(struct messaging_context *msg_ctx,
 		return NT_STATUS_OK;
 	}
 
-	ret = msg_ctx->local->send_fn(msg_ctx->id, server, msg_type,
-				      iov, iovlen, msg_ctx->local);
+	ret = messaging_dgm_send(msg_ctx->local, msg_ctx->id, server, msg_type,
+				 iov, iovlen);
 	if (ret != 0) {
 		return map_nt_error_from_unix(ret);
 	}
@@ -536,7 +537,7 @@ struct tevent_req *messaging_filtered_read_send(
 	tevent_req_defer_callback(req, state->ev);
 
 	state->tevent_handle = messaging_dgm_register_tevent_context(
-		state, msg_ctx, ev);
+		state, msg_ctx->local, ev);
 	if (tevent_req_nomem(state, req)) {
 		return tevent_req_post(req, ev);
 	}
@@ -899,7 +900,7 @@ static int mess_parent_dgm_cleanup(void *private_data)
 		private_data, struct messaging_context);
 	int ret;
 
-	ret = messaging_dgm_wipe(msg_ctx);
+	ret = messaging_dgm_wipe(msg_ctx->local);
 	DEBUG(10, ("messaging_dgm_wipe returned %s\n",
 		   ret ? strerror(ret) : "ok"));
 	return lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
@@ -933,20 +934,14 @@ int messaging_cleanup(struct messaging_context *msg_ctx, pid_t pid)
 	int ret;
 
 	if (pid == 0) {
-		ret = messaging_dgm_wipe(msg_ctx);
+		ret = messaging_dgm_wipe(msg_ctx->local);
 	} else {
-		ret = messaging_dgm_cleanup(msg_ctx, pid);
+		ret = messaging_dgm_cleanup(msg_ctx->local, pid);
 	}
 
 	return ret;
 }
 
-struct messaging_backend *messaging_local_backend(
-	struct messaging_context *msg_ctx)
-{
-	return msg_ctx->local;
-}
-
 struct tevent_context *messaging_tevent_context(
 	struct messaging_context *msg_ctx)
 {
diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index 238c9e3..2f985a9 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -50,10 +50,6 @@ struct messaging_dgm_hdr {
 	struct server_id src;
 };
 
-static int messaging_dgm_send(struct server_id src,
-			      struct server_id pid, int msg_type,
-			      const struct iovec *iov, int iovlen,
-			      struct messaging_backend *backend);
 static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
 			       uint8_t *msg, size_t msg_len,
 			       void *private_data);
@@ -174,16 +170,15 @@ static int messaging_dgm_lockfile_remove(TALLOC_CTX *tmp_ctx,
 int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		       struct tevent_context *ev,
 		       struct server_id pid,
-		       struct messaging_backend **presult,
 		       void (*recv_cb)(int msg_type,
 				       struct server_id src,
 				       struct server_id dst,
 				       const uint8_t *msg,
 				       size_t msg_len,
 				       void *private_data),
-		       void *recv_cb_private_data)
+		       void *recv_cb_private_data,
+		       struct messaging_dgm_context **pctx)
 {
-	struct messaging_backend *result;
 	struct messaging_dgm_context *ctx;
 	int ret;
 	bool ok;
@@ -198,19 +193,11 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		return errno;
 	}
 
-	result = talloc(mem_ctx, struct messaging_backend);
-	if (result == NULL) {
-		goto fail_nomem;
-	}
-	ctx = talloc_zero(result, struct messaging_dgm_context);
+	ctx = talloc_zero(mem_ctx, struct messaging_dgm_context);
 	if (ctx == NULL) {
 		goto fail_nomem;
 	}
-
-	result->private_data = ctx;
-	result->send_fn = messaging_dgm_send;
 	ctx->pid = pid;
-
 	ctx->recv_cb = recv_cb;
 	ctx->recv_cb_private_data = recv_cb_private_data;
 
@@ -228,7 +215,7 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 				sizeof(socket_address.sun_path),
 				"%s/%u", socket_dir, (unsigned)pid.pid);
 	if (sockname_len >= sizeof(socket_address.sun_path)) {
-		TALLOC_FREE(result);
+		TALLOC_FREE(ctx);
 		return ENAMETOOLONG;
 	}
 
@@ -239,7 +226,7 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 	if (ret != 0) {
 		DEBUG(1, ("%s: messaging_dgm_create_lockfile failed: %s\n",
 			  __func__, strerror(ret)));
-		TALLOC_FREE(result);
+		TALLOC_FREE(ctx);
 		return ret;
 	}
 
@@ -258,7 +245,7 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 					      0700);
 	if (!ok) {
 		DEBUG(1, ("Could not create socket directory\n"));
-		TALLOC_FREE(result);
+		TALLOC_FREE(ctx);
 		return EACCES;
 	}
 	TALLOC_FREE(socket_dir);
@@ -271,16 +258,16 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 			    messaging_dgm_recv, ctx, &ctx->dgm_ctx);
 	if (ret != 0) {
 		DEBUG(1, ("unix_msg_init failed: %s\n", strerror(ret)));
-		TALLOC_FREE(result);
+		TALLOC_FREE(ctx);
 		return ret;
 	}
 	talloc_set_destructor(ctx, messaging_dgm_context_destructor);
 
-	*presult = result;
+	*pctx = ctx;
 	return 0;
 
 fail_nomem:
-	TALLOC_FREE(result);
+	TALLOC_FREE(ctx);
 	return ENOMEM;
 }
 
@@ -301,13 +288,10 @@ static int messaging_dgm_context_destructor(struct messaging_dgm_context *c)
 	return 0;
 }
 
-static int messaging_dgm_send(struct server_id src,
-			      struct server_id pid, int msg_type,
-			      const struct iovec *iov, int iovlen,
-			      struct messaging_backend *backend)
+int messaging_dgm_send(struct messaging_dgm_context *ctx,
+		       struct server_id src, struct server_id pid,
+		       int msg_type, const struct iovec *iov, int iovlen)
 {
-	struct messaging_dgm_context *ctx = talloc_get_type_abort(
-		backend->private_data, struct messaging_dgm_context);
 	struct messaging_dgm_hdr hdr;
 	struct iovec iov2[iovlen + 1];
 	struct server_id_buf idbuf;
@@ -371,11 +355,8 @@ static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
 			 dgm_ctx->recv_cb_private_data);
 }
 
-int messaging_dgm_cleanup(struct messaging_context *msg_ctx, pid_t pid)
+int messaging_dgm_cleanup(struct messaging_dgm_context *ctx, pid_t pid)
 {
-	struct messaging_backend *be = messaging_local_backend(msg_ctx);
-	struct messaging_dgm_context *ctx = talloc_get_type_abort(
-		be->private_data, struct messaging_dgm_context);
 	char *lockfile_name, *socket_name;
 	int fd, ret;
 	struct flock lck = {};
@@ -423,11 +404,8 @@ int messaging_dgm_cleanup(struct messaging_context *msg_ctx, pid_t pid)
 	return 0;
 }
 
-int messaging_dgm_wipe(struct messaging_context *msg_ctx)
+int messaging_dgm_wipe(struct messaging_dgm_context *ctx)
 {
-	struct messaging_backend *be = messaging_local_backend(msg_ctx);
-	struct messaging_dgm_context *ctx = talloc_get_type_abort(
-		be->private_data, struct messaging_dgm_context);
 	char *msgdir_name;
 	DIR *msgdir;
 	struct dirent *dp;
@@ -471,7 +449,7 @@ int messaging_dgm_wipe(struct messaging_context *msg_ctx)
 			continue;
 		}
 
-		ret = messaging_dgm_cleanup(msg_ctx, pid);
+		ret = messaging_dgm_cleanup(ctx, pid);
 		DEBUG(10, ("messaging_dgm_cleanup(%lu) returned %s\n",
 			   pid, ret ? strerror(ret) : "ok"));
 	}
@@ -481,11 +459,8 @@ int messaging_dgm_wipe(struct messaging_context *msg_ctx)
 }
 
 void *messaging_dgm_register_tevent_context(TALLOC_CTX *mem_ctx,
-					    struct messaging_context *msg_ctx,
+					    struct messaging_dgm_context *ctx,
 					    struct tevent_context *ev)
 {
-	struct messaging_backend *be = messaging_local_backend(msg_ctx);
-	struct messaging_dgm_context *ctx = talloc_get_type_abort(
-		be->private_data, struct messaging_dgm_context);
 	return poll_funcs_tevent_register(mem_ctx, ctx->msg_callbacks, ev);
 }
-- 
1.8.1.2


From 6441099c4e9636d685c8dbffb9433bf285a90a8a Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 11:33:26 +0000
Subject: [PATCH 08/21] messaging3: Move sec_init() call out of
 messaging_dgm_init()

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/lib/messages.c     | 2 ++
 source3/lib/messages_dgm.c | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index 18376bb..a7d5fbc 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -245,6 +245,8 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 	ctx->event_ctx = ev;
 	ctx->have_context = &have_context;
 
+	sec_init();
+
 	ret = messaging_dgm_init(ctx, ctx->event_ctx, ctx->id,
 				 messaging_recv_cb, ctx, &ctx->local);
 
diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index 2f985a9..ca62789 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -219,8 +219,6 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		return ENAMETOOLONG;
 	}
 
-	sec_init();
-
 	ret = messaging_dgm_lockfile_create(ctx, cache_dir, pid.pid,
 					    &ctx->lockfile_fd, pid.unique_id);
 	if (ret != 0) {
-- 
1.8.1.2


From afedc243f013a18198613b52d01da7952054dc0a Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 11:34:24 +0000
Subject: [PATCH 09/21] messaging3: Move [un]become_root() calls out of
 messaging_dgm_send()

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/lib/messages.c     | 3 +++
 source3/lib/messages_dgm.c | 2 --
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index a7d5fbc..06d0564 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -471,8 +471,11 @@ NTSTATUS messaging_send_iov(struct messaging_context *msg_ctx,
 		return NT_STATUS_OK;
 	}
 
+	become_root();
 	ret = messaging_dgm_send(msg_ctx->local, msg_ctx->id, server, msg_type,
 				 iov, iovlen);
+	unbecome_root();
+
 	if (ret != 0) {
 		return map_nt_error_from_unix(ret);
 	}
diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index ca62789..abb6287 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -318,9 +318,7 @@ int messaging_dgm_send(struct messaging_dgm_context *ctx,
 	iov2[0].iov_len = sizeof(hdr);
 	memcpy(iov2+1, iov, iovlen*sizeof(struct iovec));
 
-	become_root();
 	ret = unix_msg_send(ctx->dgm_ctx, &dst, iov2, iovlen + 1);
-	unbecome_root();
 
 	return ret;
 }
-- 
1.8.1.2


From 0b0c260f5fc10654a923fac510f72f8b48702a3e Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 11:38:36 +0000
Subject: [PATCH 10/21] messaging3: Pass cache_dir to messaging_dgm_init()

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/include/messages.h | 1 +
 source3/lib/messages.c     | 6 ++++--
 source3/lib/messages_dgm.c | 7 +------
 3 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/source3/include/messages.h b/source3/include/messages.h
index 3281610..8d8268e 100644
--- a/source3/include/messages.h
+++ b/source3/include/messages.h
@@ -77,6 +77,7 @@ struct messaging_dgm_context;
 int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		       struct tevent_context *ev,
 		       struct server_id pid,
+		       const char *cache_dir,
 		       void (*recv_cb)(int msg_type,
 				       struct server_id src,
 				       struct server_id dst,
diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index 06d0564..a2954ed 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -248,6 +248,7 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 	sec_init();
 
 	ret = messaging_dgm_init(ctx, ctx->event_ctx, ctx->id,
+				 lp_cache_directory(),
 				 messaging_recv_cb, ctx, &ctx->local);
 
 	if (ret != 0) {
@@ -306,8 +307,9 @@ NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
 
 	msg_ctx->id = procid_self();
 
-	ret = messaging_dgm_init(msg_ctx, msg_ctx->event_ctx,
-				 msg_ctx->id, messaging_recv_cb, msg_ctx,
+	ret = messaging_dgm_init(msg_ctx, msg_ctx->event_ctx, msg_ctx->id,
+				 lp_cache_directory(),
+				 messaging_recv_cb, msg_ctx,
 				 &msg_ctx->local);
 	if (ret != 0) {
 		DEBUG(0, ("messaging_dgm_init failed: %s\n", strerror(errno)));
diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index abb6287..0f6cddc 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -170,6 +170,7 @@ static int messaging_dgm_lockfile_remove(TALLOC_CTX *tmp_ctx,
 int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		       struct tevent_context *ev,
 		       struct server_id pid,
+		       const char *cache_dir,
 		       void (*recv_cb)(int msg_type,
 				       struct server_id src,
 				       struct server_id dst,
@@ -182,17 +183,11 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 	struct messaging_dgm_context *ctx;
 	int ret;
 	bool ok;
-	const char *cache_dir;
 	char *socket_dir;
 	struct sockaddr_un socket_address;
 	size_t sockname_len;
 	uint64_t cookie;
 
-	cache_dir = lp_cache_directory();
-	if (cache_dir == NULL) {
-		return errno;
-	}
-
 	ctx = talloc_zero(mem_ctx, struct messaging_dgm_context);
 	if (ctx == NULL) {
 		goto fail_nomem;
-- 
1.8.1.2


From 761650e34f0cee905fccaaf97b915099c06d90dd Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 11:47:32 +0000
Subject: [PATCH 11/21] messaging3: Pass dir_owner to messaging_dgm_init()

---
 source3/include/messages.h |  1 +
 source3/lib/messages.c     |  4 ++--
 source3/lib/messages_dgm.c | 11 ++++++-----
 3 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/source3/include/messages.h b/source3/include/messages.h
index 8d8268e..81f29db 100644
--- a/source3/include/messages.h
+++ b/source3/include/messages.h
@@ -78,6 +78,7 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		       struct tevent_context *ev,
 		       struct server_id pid,
 		       const char *cache_dir,
+		       uid_t dir_owner,
 		       void (*recv_cb)(int msg_type,
 				       struct server_id src,
 				       struct server_id dst,
diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index a2954ed..37953ba 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -248,7 +248,7 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 	sec_init();
 
 	ret = messaging_dgm_init(ctx, ctx->event_ctx, ctx->id,
-				 lp_cache_directory(),
+				 lp_cache_directory(), sec_initial_uid(),
 				 messaging_recv_cb, ctx, &ctx->local);
 
 	if (ret != 0) {
@@ -308,7 +308,7 @@ NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
 	msg_ctx->id = procid_self();
 
 	ret = messaging_dgm_init(msg_ctx, msg_ctx->event_ctx, msg_ctx->id,
-				 lp_cache_directory(),
+				 lp_cache_directory(), sec_initial_uid(),
 				 messaging_recv_cb, msg_ctx,
 				 &msg_ctx->local);
 	if (ret != 0) {
diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index 0f6cddc..16e7deb 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -57,7 +57,8 @@ static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c);
 
 static int messaging_dgm_lockfile_create(TALLOC_CTX *tmp_ctx,
-					 const char *cache_dir, pid_t pid,
+					 const char *cache_dir,
+					 uid_t dir_owner, pid_t pid,
 					 int *plockfile_fd, uint64_t unique)
 {
 	fstring buf;
@@ -74,7 +75,7 @@ static int messaging_dgm_lockfile_create(TALLOC_CTX *tmp_ctx,
 		return ENOMEM;
 	}
 
-	ok = directory_create_or_exist_strict(dir, sec_initial_uid(), 0755);
+	ok = directory_create_or_exist_strict(dir, dir_owner, 0755);
 	if (!ok) {
 		ret = errno;
 		DEBUG(1, ("%s: Could not create lock directory: %s\n",
@@ -171,6 +172,7 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		       struct tevent_context *ev,
 		       struct server_id pid,
 		       const char *cache_dir,
+		       uid_t dir_owner,
 		       void (*recv_cb)(int msg_type,
 				       struct server_id src,
 				       struct server_id dst,
@@ -214,7 +216,7 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		return ENAMETOOLONG;
 	}
 
-	ret = messaging_dgm_lockfile_create(ctx, cache_dir, pid.pid,
+	ret = messaging_dgm_lockfile_create(ctx, cache_dir, dir_owner, pid.pid,
 					    &ctx->lockfile_fd, pid.unique_id);
 	if (ret != 0) {
 		DEBUG(1, ("%s: messaging_dgm_create_lockfile failed: %s\n",
@@ -234,8 +236,7 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		goto fail_nomem;
 	}
 
-	ok = directory_create_or_exist_strict(socket_dir, sec_initial_uid(),
-					      0700);
+	ok = directory_create_or_exist_strict(socket_dir, dir_owner, 0700);
 	if (!ok) {
 		DEBUG(1, ("Could not create socket directory\n"));
 		TALLOC_FREE(ctx);
-- 
1.8.1.2


From 6c58c02f8aa1c15c670a9ca615a75efd51c40cea Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 11:51:13 +0000
Subject: [PATCH 12/21] messaging3: Avoid "enum messaging_type" in messages_dgm

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/lib/messages_dgm.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index 16e7deb..f0d468d 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -26,7 +26,6 @@
 #include "lib/param/param.h"
 #include "poll_funcs/poll_funcs_tevent.h"
 #include "unix_msg/unix_msg.h"
-#include "librpc/gen_ndr/messaging.h"
 
 struct messaging_dgm_context {
 	struct server_id pid;
@@ -45,7 +44,7 @@ struct messaging_dgm_context {
 
 struct messaging_dgm_hdr {
 	uint32_t msg_version;
-	enum messaging_type msg_type;
+	int msg_type;
 	struct server_id dst;
 	struct server_id src;
 };
-- 
1.8.1.2


From a5c58f4afc81b053735fdbacbe028b2748e4aebd Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 11:55:33 +0000
Subject: [PATCH 13/21] messaging3: I don't see 2 versions running
 concurrently...

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/lib/messages_dgm.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index f0d468d..758ebc1 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -43,7 +43,6 @@ struct messaging_dgm_context {
 };
 
 struct messaging_dgm_hdr {
-	uint32_t msg_version;
 	int msg_type;
 	struct server_id dst;
 	struct server_id src;
@@ -300,7 +299,6 @@ int messaging_dgm_send(struct messaging_dgm_context *ctx,
 		return ENAMETOOLONG;
 	}
 
-	hdr.msg_version = MESSAGE_VERSION;
 	hdr.msg_type = msg_type & MSG_TYPE_MASK;
 	hdr.dst = pid;
 	hdr.src = src;
-- 
1.8.1.2


From a709652d0830f187e87b91ac06ffdb9250d9a476 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 11:56:18 +0000
Subject: [PATCH 14/21] messaging3: Pass on msg_type unmasked

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/lib/messages_dgm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index 758ebc1..958cc80 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -299,7 +299,7 @@ int messaging_dgm_send(struct messaging_dgm_context *ctx,
 		return ENAMETOOLONG;
 	}
 
-	hdr.msg_type = msg_type & MSG_TYPE_MASK;
+	hdr.msg_type = msg_type;
 	hdr.dst = pid;
 	hdr.src = src;
 
-- 
1.8.1.2


From ed8cc37cb8cefcf7b35b57cb8fd1b2bffc1b59d1 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 17 Jul 2014 11:57:51 +0000
Subject: [PATCH 15/21] messaging3: Add messages_dgm.h

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/include/messages.h | 24 +-----------------------
 source3/lib/messages_dgm.c |  2 +-
 source3/lib/messages_dgm.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 24 deletions(-)
 create mode 100644 source3/lib/messages_dgm.h

diff --git a/source3/include/messages.h b/source3/include/messages.h
index 81f29db..862e5ea 100644
--- a/source3/include/messages.h
+++ b/source3/include/messages.h
@@ -59,6 +59,7 @@
 #define MSG_SRVID_SAMBA 0x0000000100000000LL
 
 #include "librpc/gen_ndr/server_id.h"
+#include "lib/messages_dgm.h"
 
 #define MSG_BROADCAST_PID_STR	"0:0"
 
@@ -73,29 +74,6 @@ struct messaging_backend {
 	void *private_data;
 };
 
-struct messaging_dgm_context;
-int messaging_dgm_init(TALLOC_CTX *mem_ctx,
-		       struct tevent_context *ev,
-		       struct server_id pid,
-		       const char *cache_dir,
-		       uid_t dir_owner,
-		       void (*recv_cb)(int msg_type,
-				       struct server_id src,
-				       struct server_id dst,
-				       const uint8_t *msg,
-				       size_t msg_len,
-				       void *private_data),
-		       void *recv_cb_private_data,
-		       struct messaging_dgm_context **pctx);
-int messaging_dgm_send(struct messaging_dgm_context *ctx,
-		       struct server_id src, struct server_id pid,
-		       int msg_type, const struct iovec *iov, int iovlen);
-int messaging_dgm_cleanup(struct messaging_dgm_context *ctx, pid_t pid);
-int messaging_dgm_wipe(struct messaging_dgm_context *ctx);
-void *messaging_dgm_register_tevent_context(TALLOC_CTX *mem_ctx,
-					    struct messaging_dgm_context *ctx,
-					    struct tevent_context *ev);
-
 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
 			      TALLOC_CTX *mem_ctx,
 			      struct messaging_backend **presult);
diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index 958cc80..7b9afe8 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -22,7 +22,7 @@
 #include "lib/util/debug.h"
 #include "lib/unix_msg/unix_msg.h"
 #include "system/filesys.h"
-#include "messages.h"
+#include "lib/messages_dgm.h"
 #include "lib/param/param.h"
 #include "poll_funcs/poll_funcs_tevent.h"
 #include "unix_msg/unix_msg.h"
diff --git a/source3/lib/messages_dgm.h b/source3/lib/messages_dgm.h
new file mode 100644
index 0000000..0172d7b
--- /dev/null
+++ b/source3/lib/messages_dgm.h
@@ -0,0 +1,46 @@
+/*
+ * Unix SMB/CIFS implementation.
+ * messages_dgm.c header
+ * Copyright (C) Volker Lendecke 2014
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _MESSAGES_DGM_H_
+#define _MESSAGES_DGM_H_
+
+struct messaging_dgm_context;
+int messaging_dgm_init(TALLOC_CTX *mem_ctx,
+		       struct tevent_context *ev,
+		       struct server_id pid,
+		       const char *cache_dir,
+		       uid_t dir_owner,
+		       void (*recv_cb)(int msg_type,
+				       struct server_id src,
+				       struct server_id dst,
+				       const uint8_t *msg,
+				       size_t msg_len,
+				       void *private_data),
+		       void *recv_cb_private_data,
+		       struct messaging_dgm_context **pctx);
+int messaging_dgm_send(struct messaging_dgm_context *ctx,
+		       struct server_id src, struct server_id pid,
+		       int msg_type, const struct iovec *iov, int iovlen);
+int messaging_dgm_cleanup(struct messaging_dgm_context *ctx, pid_t pid);
+int messaging_dgm_wipe(struct messaging_dgm_context *ctx);
+void *messaging_dgm_register_tevent_context(TALLOC_CTX *mem_ctx,
+					    struct messaging_dgm_context *ctx,
+					    struct tevent_context *ev);
+
+#endif
-- 
1.8.1.2


From 6619992d1fe0f54f427fda57b22cb7a3c7d867e2 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Sun, 20 Jul 2014 14:51:47 +0200
Subject: [PATCH 16/21] messaging3: Protect messaging_dgm against multiple
 contexts

We can't rely on posix locking within a process

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/lib/messages_dgm.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index 7b9afe8..d36bf10 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -40,6 +40,8 @@ struct messaging_dgm_context {
 			const uint8_t *msg, size_t msg_len,
 			void *private_data);
 	void *recv_cb_private_data;
+
+	bool *have_dgm_context;
 };
 
 struct messaging_dgm_hdr {
@@ -187,6 +189,11 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 	struct sockaddr_un socket_address;
 	size_t sockname_len;
 	uint64_t cookie;
+	static bool have_dgm_context = false;
+
+	if (have_dgm_context) {
+		return EEXIST;
+	}
 
 	ctx = talloc_zero(mem_ctx, struct messaging_dgm_context);
 	if (ctx == NULL) {
@@ -255,6 +262,8 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 	}
 	talloc_set_destructor(ctx, messaging_dgm_context_destructor);
 
+	ctx->have_dgm_context = &have_dgm_context;
+
 	*pctx = ctx;
 	return 0;
 
@@ -277,6 +286,11 @@ static int messaging_dgm_context_destructor(struct messaging_dgm_context *c)
 		(void)messaging_dgm_lockfile_remove(c, c->cache_dir, pid.pid);
 	}
 	close(c->lockfile_fd);
+
+	if (c->have_dgm_context != NULL) {
+		*c->have_dgm_context = false;
+	}
+
 	return 0;
 }
 
-- 
1.8.1.2


From 4e6859bb411b8d95779b4dc650cce278b3cb46b8 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Sat, 9 Aug 2014 17:18:04 +0200
Subject: [PATCH 17/21] messaging3: Remove one-context protection from
 messages.c

messages_dgm.c takes care of it.

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/lib/messages.c | 22 ----------------------
 1 file changed, 22 deletions(-)

diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index 37953ba..12419f8 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -75,12 +75,8 @@ struct messaging_context {
 	struct messaging_dgm_context *local;
 
 	struct messaging_backend *remote;
-
-	bool *have_context;
 };
 
-static int messaging_context_destructor(struct messaging_context *msg_ctx);
-
 /****************************************************************************
  A useful function for testing the message system.
 ****************************************************************************/
@@ -229,13 +225,6 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 	struct messaging_context *ctx;
 	NTSTATUS status;
 	int ret;
-	static bool have_context = false;
-
-	if (have_context) {
-		DEBUG(0, ("No two messaging contexts per process\n"));
-		return NULL;
-	}
-
 
 	if (!(ctx = talloc_zero(mem_ctx, struct messaging_context))) {
 		return NULL;
@@ -243,7 +232,6 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 
 	ctx->id = procid_self();
 	ctx->event_ctx = ev;
-	ctx->have_context = &have_context;
 
 	sec_init();
 
@@ -277,19 +265,9 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 	register_dmalloc_msgs(ctx);
 	debug_register_msgs(ctx);
 
-	have_context = true;
-	talloc_set_destructor(ctx, messaging_context_destructor);
-
 	return ctx;
 }
 
-static int messaging_context_destructor(struct messaging_context *msg_ctx)
-{
-	SMB_ASSERT(*msg_ctx->have_context);
-	*msg_ctx->have_context = false;
-	return 0;
-}
-
 struct server_id messaging_server_id(const struct messaging_context *msg_ctx)
 {
 	return msg_ctx->id;
-- 
1.8.1.2


From 3bcb03cd1edc64a5e3e8057f071a6df63cfc5325 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Fri, 25 Jul 2014 10:42:19 +0000
Subject: [PATCH 18/21] messaging3: Save 48 bytes .text

---
 source3/lib/messages_dgm.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index d36bf10..48c6690 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -65,7 +65,7 @@ static int messaging_dgm_lockfile_create(TALLOC_CTX *tmp_ctx,
 	char *dir;
 	char *lockfile_name;
 	int lockfile_fd;
-	struct flock lck = {};
+	struct flock lck;
 	int unique_len, ret;
 	ssize_t written;
 	bool ok;
@@ -101,10 +101,10 @@ static int messaging_dgm_lockfile_create(TALLOC_CTX *tmp_ctx,
 		goto fail_free;
 	}
 
-	lck.l_type = F_WRLCK;
-	lck.l_whence = SEEK_SET;
-	lck.l_start = 0;
-	lck.l_len = 0;
+	lck = (struct flock) {
+		.l_type = F_WRLCK,
+		.l_whence = SEEK_SET
+	};
 
 	ret = fcntl(lockfile_fd, F_SETLK, &lck);
 	if (ret == -1) {
-- 
1.8.1.2


From 133d5ae8bfbec3abe0afb150e75299ca4ab8922c Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Fri, 25 Jul 2014 11:03:11 +0000
Subject: [PATCH 19/21] messaging3: Only store the pid in messaging_dgm_context

That's all we need here

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/lib/messages_dgm.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index 48c6690..3cd3f69 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -28,7 +28,7 @@
 #include "unix_msg/unix_msg.h"
 
 struct messaging_dgm_context {
-	struct server_id pid;
+	pid_t pid;
 	struct poll_funcs *msg_callbacks;
 	void *tevent_handle;
 	struct unix_msg_ctx *dgm_ctx;
@@ -199,7 +199,7 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 	if (ctx == NULL) {
 		goto fail_nomem;
 	}
-	ctx->pid = pid;
+	ctx->pid = pid.pid;
 	ctx->recv_cb = recv_cb;
 	ctx->recv_cb_private_data = recv_cb_private_data;
 
@@ -274,16 +274,14 @@ fail_nomem:
 
 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c)
 {
-	struct server_id pid = c->pid;
-
 	/*
 	 * First delete the socket to avoid races. The lockfile is the
 	 * indicator that we're still around.
 	 */
 	unix_msg_free(c->dgm_ctx);
 
-	if (getpid() == pid.pid) {
-		(void)messaging_dgm_lockfile_remove(c, c->cache_dir, pid.pid);
+	if (getpid() == c->pid) {
+		(void)messaging_dgm_lockfile_remove(c, c->cache_dir, c->pid);
 	}
 	close(c->lockfile_fd);
 
-- 
1.8.1.2


From 522842291110c7589acc620b4ae3d675c93956dd Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Sun, 27 Jul 2014 12:29:26 +0200
Subject: [PATCH 20/21] messaging3: Move messaging_hdr handling to messages.c.

This makes messages_dgm a simple byte-transport across processes that
knows almost nothing about server_id etc.

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/lib/messages.c     | 49 +++++++++++++++++++++++++++++--------
 source3/lib/messages_dgm.c | 60 +++++++---------------------------------------
 source3/lib/messages_dgm.h | 10 +++-----
 3 files changed, 51 insertions(+), 68 deletions(-)

diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index 12419f8..7aa660a 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -77,6 +77,12 @@ struct messaging_context {
 	struct messaging_backend *remote;
 };
 
+struct messaging_hdr {
+	int msg_type;
+	struct server_id dst;
+	struct server_id src;
+};
+
 /****************************************************************************
  A useful function for testing the message system.
 ****************************************************************************/
@@ -198,22 +204,36 @@ bool message_send_all(struct messaging_context *msg_ctx,
 	return true;
 }
 
-static void messaging_recv_cb(int msg_type,
-			      struct server_id src, struct server_id dst,
-			      const uint8_t *msg, size_t msg_len,
+static void messaging_recv_cb(const uint8_t *msg, size_t msg_len,
 			      void *private_data)
 {
 	struct messaging_context *msg_ctx = talloc_get_type_abort(
 		private_data, struct messaging_context);
+	const struct messaging_hdr *hdr;
+	struct server_id_buf idbuf;
 	struct messaging_rec rec;
 
+	if (msg_len < sizeof(*hdr)) {
+		DEBUG(1, ("message too short: %u\n", (unsigned)msg_len));
+		return;
+	}
+
+	/*
+	 * messages_dgm guarantees alignment, so we can cast here
+	 */
+	hdr = (const struct messaging_hdr *)msg;
+
+	DEBUG(10, ("%s: Received message 0x%x len %u from %s\n", __func__,
+		   (unsigned)hdr->msg_type, (unsigned)(msg_len - sizeof(*hdr)),
+		   server_id_str_buf(hdr->src, &idbuf)));
+
 	rec = (struct messaging_rec) {
 		.msg_version = MESSAGE_VERSION,
-		.msg_type = msg_type,
-		.src = src,
-		.dest = dst,
-		.buf.data = discard_const_p(uint8, msg),
-		.buf.length = msg_len
+		.msg_type = hdr->msg_type,
+		.src = hdr->src,
+		.dest = hdr->dst,
+		.buf.data = discard_const_p(uint8, msg) + sizeof(*hdr),
+		.buf.length = msg_len - sizeof(*hdr)
 	};
 
 	messaging_dispatch_rec(msg_ctx, &rec);
@@ -417,6 +437,8 @@ NTSTATUS messaging_send_iov(struct messaging_context *msg_ctx,
 			    const struct iovec *iov, int iovlen)
 {
 	int ret;
+	struct messaging_hdr hdr;
+	struct iovec iov2[iovlen+1];
 
 	if (server_id_is_disconnected(&server)) {
 		return NT_STATUS_INVALID_PARAMETER_MIX;
@@ -451,9 +473,16 @@ NTSTATUS messaging_send_iov(struct messaging_context *msg_ctx,
 		return NT_STATUS_OK;
 	}
 
+	hdr = (struct messaging_hdr) {
+		.msg_type = msg_type,
+		.dst = server,
+		.src = msg_ctx->id
+	};
+	iov2[0] = (struct iovec){ .iov_base = &hdr, .iov_len = sizeof(hdr) };
+	memcpy(&iov2[1], iov, iovlen * sizeof(*iov));
+
 	become_root();
-	ret = messaging_dgm_send(msg_ctx->local, msg_ctx->id, server, msg_type,
-				 iov, iovlen);
+	ret = messaging_dgm_send(msg_ctx->local, server.pid, iov2, iovlen+1);
 	unbecome_root();
 
 	if (ret != 0) {
diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index 3cd3f69..ea29a44 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -35,21 +35,14 @@ struct messaging_dgm_context {
 	char *cache_dir;
 	int lockfile_fd;
 
-	void (*recv_cb)(int msg_type,
-			struct server_id src, struct server_id dst,
-			const uint8_t *msg, size_t msg_len,
+	void (*recv_cb)(const uint8_t *msg,
+			size_t msg_len,
 			void *private_data);
 	void *recv_cb_private_data;
 
 	bool *have_dgm_context;
 };
 
-struct messaging_dgm_hdr {
-	int msg_type;
-	struct server_id dst;
-	struct server_id src;
-};
-
 static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
 			       uint8_t *msg, size_t msg_len,
 			       void *private_data);
@@ -173,10 +166,7 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		       struct server_id pid,
 		       const char *cache_dir,
 		       uid_t dir_owner,
-		       void (*recv_cb)(int msg_type,
-				       struct server_id src,
-				       struct server_id dst,
-				       const uint8_t *msg,
+		       void (*recv_cb)(const uint8_t *msg,
 				       size_t msg_len,
 				       void *private_data),
 		       void *recv_cb_private_data,
@@ -292,13 +282,9 @@ static int messaging_dgm_context_destructor(struct messaging_dgm_context *c)
 	return 0;
 }
 
-int messaging_dgm_send(struct messaging_dgm_context *ctx,
-		       struct server_id src, struct server_id pid,
-		       int msg_type, const struct iovec *iov, int iovlen)
+int messaging_dgm_send(struct messaging_dgm_context *ctx, pid_t pid,
+		       const struct iovec *iov, int iovlen)
 {
-	struct messaging_dgm_hdr hdr;
-	struct iovec iov2[iovlen + 1];
-	struct server_id_buf idbuf;
 	struct sockaddr_un dst;
 	ssize_t dst_pathlen;
 	int ret;
@@ -306,24 +292,14 @@ int messaging_dgm_send(struct messaging_dgm_context *ctx,
 	dst = (struct sockaddr_un) { .sun_family = AF_UNIX };
 
 	dst_pathlen = snprintf(dst.sun_path, sizeof(dst.sun_path),
-			       "%s/msg/%u", ctx->cache_dir, (unsigned)pid.pid);
+			       "%s/msg/%u", ctx->cache_dir, (unsigned)pid);
 	if (dst_pathlen >= sizeof(dst.sun_path)) {
 		return ENAMETOOLONG;
 	}
 
-	hdr.msg_type = msg_type;
-	hdr.dst = pid;
-	hdr.src = src;
-
-	DEBUG(10, ("%s: Sending message 0x%x to %s\n", __func__,
-		   (unsigned)hdr.msg_type,
-		   server_id_str_buf(pid, &idbuf)));
+	DEBUG(10, ("%s: Sending message to %u\n", __func__, (unsigned)pid));
 
-	iov2[0].iov_base = &hdr;
-	iov2[0].iov_len = sizeof(hdr);
-	memcpy(iov2+1, iov, iovlen*sizeof(struct iovec));
-
-	ret = unix_msg_send(ctx->dgm_ctx, &dst, iov2, iovlen + 1);
+	ret = unix_msg_send(ctx->dgm_ctx, &dst, iov, iovlen);
 
 	return ret;
 }
@@ -334,26 +310,8 @@ static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
 {
 	struct messaging_dgm_context *dgm_ctx = talloc_get_type_abort(
 		private_data, struct messaging_dgm_context);
-	struct messaging_dgm_hdr *hdr;
-	struct server_id_buf idbuf;
-
-	if (msg_len < sizeof(*hdr)) {
-		DEBUG(1, ("message too short: %u\n", (unsigned)msg_len));
-		return;
-	}
-
-	/*
-	 * unix_msg guarantees alignment, so we can cast here
-	 */
-	hdr = (struct messaging_dgm_hdr *)msg;
-
-	DEBUG(10, ("%s: Received message 0x%x len %u from %s\n", __func__,
-		   (unsigned)hdr->msg_type, (unsigned)(msg_len - sizeof(*hdr)),
-		   server_id_str_buf(hdr->src, &idbuf)));
 
-	dgm_ctx->recv_cb(hdr->msg_type, hdr->src, hdr->dst,
-			 msg + sizeof(*hdr), msg_len - sizeof(*hdr),
-			 dgm_ctx->recv_cb_private_data);
+	dgm_ctx->recv_cb(msg, msg_len, dgm_ctx->recv_cb_private_data);
 }
 
 int messaging_dgm_cleanup(struct messaging_dgm_context *ctx, pid_t pid)
diff --git a/source3/lib/messages_dgm.h b/source3/lib/messages_dgm.h
index 0172d7b..b403117 100644
--- a/source3/lib/messages_dgm.h
+++ b/source3/lib/messages_dgm.h
@@ -26,17 +26,13 @@ int messaging_dgm_init(TALLOC_CTX *mem_ctx,
 		       struct server_id pid,
 		       const char *cache_dir,
 		       uid_t dir_owner,
-		       void (*recv_cb)(int msg_type,
-				       struct server_id src,
-				       struct server_id dst,
-				       const uint8_t *msg,
+		       void (*recv_cb)(const uint8_t *msg,
 				       size_t msg_len,
 				       void *private_data),
 		       void *recv_cb_private_data,
 		       struct messaging_dgm_context **pctx);
-int messaging_dgm_send(struct messaging_dgm_context *ctx,
-		       struct server_id src, struct server_id pid,
-		       int msg_type, const struct iovec *iov, int iovlen);
+int messaging_dgm_send(struct messaging_dgm_context *ctx, pid_t pid,
+		       const struct iovec *iov, int iovlen);
 int messaging_dgm_cleanup(struct messaging_dgm_context *ctx, pid_t pid);
 int messaging_dgm_wipe(struct messaging_dgm_context *ctx);
 void *messaging_dgm_register_tevent_context(TALLOC_CTX *mem_ctx,
-- 
1.8.1.2


From 8b53a00df29596b83db3090195c12f56b880fb56 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Sun, 27 Jul 2014 12:31:21 +0200
Subject: [PATCH 21/21] messaging3: Include messages_dgm.h only in messages.c

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/include/messages.h | 1 -
 source3/lib/messages.c     | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)

diff --git a/source3/include/messages.h b/source3/include/messages.h
index 862e5ea..7543301 100644
--- a/source3/include/messages.h
+++ b/source3/include/messages.h
@@ -59,7 +59,6 @@
 #define MSG_SRVID_SAMBA 0x0000000100000000LL
 
 #include "librpc/gen_ndr/server_id.h"
-#include "lib/messages_dgm.h"
 
 #define MSG_BROADCAST_PID_STR	"0:0"
 
diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index 7aa660a..25d3f01 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -51,6 +51,7 @@
 #include "messages.h"
 #include "lib/util/tevent_unix.h"
 #include "lib/background.h"
+#include "lib/messages_dgm.h"
 
 struct messaging_callback {
 	struct messaging_callback *prev, *next;
-- 
1.8.1.2



More information about the samba-technical mailing list