6729ff
From 770d684ee974efde80186fe579ff85bc4c038b83 Mon Sep 17 00:00:00 2001
6729ff
From: Volker Lendecke <vl@samba.org>
6729ff
Date: Thu, 21 Nov 2013 21:05:29 +0100
6729ff
Subject: [PATCH] smbd: Fix bug 10284
6729ff
6729ff
If we msg_read_send on a nonempty channel, we create one
6729ff
tevent_immediate. If we directly receive another message and from
6729ff
within the msg_read_send's tevent_req callback we immediately do
6729ff
another msg_read_send, we end up with two tevent_immediate events for
6729ff
msg_channel_trigger with just one incoming message. Test to follow.
6729ff
6729ff
This patch simplifies msg_channel.c by removing the explicit immediate
6729ff
events. Instead, it relies on the implicit immediate event available
6729ff
via tevent_req_defer_callback. For messages received from tdb with
6729ff
a msg_read_send req pending, we directly finish that request without
6729ff
putting the message on the queue.
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10284
6729ff
Signed-off-by: Volker Lendecke <vl@samba.org>
6729ff
Reviewed-by: Stefan Metzmacher <metze@samba.org>
6729ff
(cherry picked from commit 6b6920b02905661ae661a894e3bd8d2c744d7003)
6729ff
---
6729ff
 source3/lib/msg_channel.c | 100 ++++++++++++++--------------------------------
6729ff
 1 file changed, 31 insertions(+), 69 deletions(-)
6729ff
6729ff
diff --git a/source3/lib/msg_channel.c b/source3/lib/msg_channel.c
6729ff
index 625d07c..8e23fd4 100644
6729ff
--- a/source3/lib/msg_channel.c
6729ff
+++ b/source3/lib/msg_channel.c
6729ff
@@ -41,9 +41,6 @@ static void msg_channel_init_got_ctdb(struct tevent_req *subreq);
6729ff
 static void msg_channel_init_got_msg(struct messaging_context *msg,
6729ff
 			       void *priv, uint32_t msg_type,
6729ff
 			       struct server_id server_id, DATA_BLOB *data);
6729ff
-static void msg_channel_trigger(struct tevent_context *ev,
6729ff
-				struct tevent_immediate *im,
6729ff
-				void *priv);
6729ff
 static int msg_channel_destructor(struct msg_channel *s);
6729ff
 
6729ff
 struct tevent_req *msg_channel_init_send(TALLOC_CTX *mem_ctx,
6729ff
@@ -157,6 +154,12 @@ fail:
6729ff
 	return err;
6729ff
 }
6729ff
 
6729ff
+struct msg_read_state {
6729ff
+	struct tevent_context *ev;
6729ff
+	struct msg_channel *channel;
6729ff
+	struct messaging_rec *rec;
6729ff
+};
6729ff
+
6729ff
 static void msg_channel_init_got_msg(struct messaging_context *msg,
6729ff
 				     void *priv, uint32_t msg_type,
6729ff
 				     struct server_id server_id,
6729ff
@@ -167,7 +170,6 @@ static void msg_channel_init_got_msg(struct messaging_context *msg,
6729ff
 	struct messaging_rec *rec;
6729ff
 	struct messaging_rec **msgs;
6729ff
 	size_t num_msgs;
6729ff
-	struct tevent_immediate *im;
6729ff
 
6729ff
 	rec = talloc(s, struct messaging_rec);
6729ff
 	if (rec == NULL) {
6729ff
@@ -184,6 +186,19 @@ static void msg_channel_init_got_msg(struct messaging_context *msg,
6729ff
 	}
6729ff
 	rec->buf.length = data->length;
6729ff
 
6729ff
+	if (s->pending_req != NULL) {
6729ff
+		struct tevent_req *req = s->pending_req;
6729ff
+		struct msg_read_state *state = tevent_req_data(
6729ff
+			req, struct msg_read_state);
6729ff
+
6729ff
+		s->pending_req = NULL;
6729ff
+
6729ff
+		state->rec = talloc_move(state, &rec);
6729ff
+		tevent_req_defer_callback(req, s->ev);
6729ff
+		tevent_req_done(req);
6729ff
+		return;
6729ff
+	}
6729ff
+
6729ff
 	num_msgs = talloc_array_length(s->msgs);
6729ff
 	msgs = talloc_realloc(s, s->msgs, struct messaging_rec *, num_msgs+1);
6729ff
 	if (msgs == NULL) {
6729ff
@@ -192,28 +207,11 @@ static void msg_channel_init_got_msg(struct messaging_context *msg,
6729ff
 	s->msgs = msgs;
6729ff
 	s->msgs[num_msgs] = talloc_move(s->msgs, &rec);
6729ff
 
6729ff
-	if (s->pending_req == NULL) {
6729ff
-		return;
6729ff
-	}
6729ff
-
6729ff
-	im = tevent_create_immediate(s);
6729ff
-	if (im == NULL) {
6729ff
-		goto fail;
6729ff
-	}
6729ff
-	tevent_schedule_immediate(im, s->ev, msg_channel_trigger, s);
6729ff
 	return;
6729ff
 fail:
6729ff
 	TALLOC_FREE(rec);
6729ff
 }
6729ff
 
6729ff
-struct msg_read_state {
6729ff
-	struct tevent_context *ev;
6729ff
-	struct tevent_req *req;
6729ff
-	struct msg_channel *channel;
6729ff
-	struct messaging_rec *rec;
6729ff
-};
6729ff
-
6729ff
-static int msg_read_state_destructor(struct msg_read_state *s);
6729ff
 static void msg_read_got_ctdb(struct tevent_req *subreq);
6729ff
 
6729ff
 struct tevent_req *msg_read_send(TALLOC_CTX *mem_ctx,
6729ff
@@ -221,7 +219,6 @@ struct tevent_req *msg_read_send(TALLOC_CTX *mem_ctx,
6729ff
 				 struct msg_channel *channel)
6729ff
 {
6729ff
 	struct tevent_req *req;
6729ff
-	struct tevent_immediate *im;
6729ff
 	struct msg_read_state *state;
6729ff
 	void *msg_tdb_event;
6729ff
 	size_t num_msgs;
6729ff
@@ -231,28 +228,28 @@ struct tevent_req *msg_read_send(TALLOC_CTX *mem_ctx,
6729ff
 		return NULL;
6729ff
 	}
6729ff
 	state->ev = ev;
6729ff
-	state->req = req;
6729ff
 	state->channel = channel;
6729ff
 
6729ff
 	if (channel->pending_req != NULL) {
6729ff
 		tevent_req_error(req, EBUSY);
6729ff
 		return tevent_req_post(req, ev);
6729ff
 	}
6729ff
-	channel->pending_req = req;
6729ff
-	channel->ev = ev;
6729ff
-	talloc_set_destructor(state, msg_read_state_destructor);
6729ff
 
6729ff
 	num_msgs = talloc_array_length(channel->msgs);
6729ff
 	if (num_msgs != 0) {
6729ff
-		im = tevent_create_immediate(channel->ev);
6729ff
-		if (tevent_req_nomem(im, req)) {
6729ff
-			return tevent_req_post(req, ev);
6729ff
-		}
6729ff
-		tevent_schedule_immediate(im, channel->ev, msg_channel_trigger,
6729ff
-					  channel);
6729ff
-		return req;
6729ff
+		state->rec = talloc_move(state, &channel->msgs[0]);
6729ff
+		memmove(channel->msgs, channel->msgs+1,
6729ff
+			sizeof(struct messaging_rec *) * (num_msgs-1));
6729ff
+		channel->msgs = talloc_realloc(
6729ff
+			channel, channel->msgs, struct messaging_rec *,
6729ff
+			num_msgs - 1);
6729ff
+		tevent_req_done(req);
6729ff
+		return tevent_req_post(req, ev);
6729ff
 	}
6729ff
 
6729ff
+	channel->pending_req = req;
6729ff
+	channel->ev = ev;
6729ff
+
6729ff
 	msg_tdb_event = messaging_tdb_event(state, channel->msg, ev);
6729ff
 	if (tevent_req_nomem(msg_tdb_event, req)) {
6729ff
 		return tevent_req_post(req, ev);
6729ff
@@ -271,42 +268,6 @@ struct tevent_req *msg_read_send(TALLOC_CTX *mem_ctx,
6729ff
 	return req;
6729ff
 }
6729ff
 
6729ff
-static int msg_read_state_destructor(struct msg_read_state *s)
6729ff
-{
6729ff
-	assert(s->channel->pending_req == s->req);
6729ff
-	s->channel->pending_req = NULL;
6729ff
-	return 0;
6729ff
-}
6729ff
-
6729ff
-static void msg_channel_trigger(struct tevent_context *ev,
6729ff
-			       struct tevent_immediate *im,
6729ff
-			       void *priv)
6729ff
-{
6729ff
-	struct msg_channel *channel;
6729ff
-	struct tevent_req *req;
6729ff
-	struct msg_read_state *state;
6729ff
-	size_t num_msgs;
6729ff
-
6729ff
-	channel = talloc_get_type_abort(priv, struct msg_channel);
6729ff
-	req = channel->pending_req;
6729ff
-	state = tevent_req_data(req, struct msg_read_state);
6729ff
-
6729ff
-	talloc_set_destructor(state, NULL);
6729ff
-	msg_read_state_destructor(state);
6729ff
-
6729ff
-	num_msgs = talloc_array_length(channel->msgs);
6729ff
-	assert(num_msgs > 0);
6729ff
-
6729ff
-	state->rec = talloc_move(state, &channel->msgs[0]);
6729ff
-
6729ff
-	memmove(channel->msgs, channel->msgs+1,
6729ff
-		sizeof(struct messaging_rec *) * (num_msgs-1));
6729ff
-	channel->msgs = talloc_realloc(
6729ff
-		channel, channel->msgs, struct messaging_rec *, num_msgs - 1);
6729ff
-
6729ff
-	tevent_req_done(req);
6729ff
-}
6729ff
-
6729ff
 static void msg_read_got_ctdb(struct tevent_req *subreq)
6729ff
 {
6729ff
 	struct tevent_req *req = tevent_req_callback_data(
6729ff
@@ -368,5 +329,6 @@ int msg_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
6729ff
 		return err;
6729ff
 	}
6729ff
 	*prec = talloc_move(mem_ctx, &state->rec);
6729ff
+	tevent_req_received(req);
6729ff
 	return 0;
6729ff
 }
6729ff
-- 
6729ff
1.8.1.2
6729ff