}
}
-struct messaging_selfsend_state {
- struct messaging_context *msg;
- struct messaging_rec rec;
-};
-
-static void messaging_trigger_self(struct tevent_context *ev,
- struct tevent_immediate *im,
- void *private_data);
+static bool messaging_is_self_send(const struct messaging_context *msg_ctx,
+ const struct server_id *dst)
+{
+ return ((msg_ctx->id.vnn == dst->vnn) &&
+ (msg_ctx->id.pid == dst->pid));
+}
/*
Send a message to a particular server
struct server_id server, uint32_t msg_type,
const DATA_BLOB *data)
{
- if (server_id_is_disconnected(&server)) {
- return NT_STATUS_INVALID_PARAMETER_MIX;
- }
+ struct iovec iov;
- if (!procid_is_local(&server)) {
- return msg_ctx->remote->send_fn(msg_ctx, server,
- msg_type, data,
- msg_ctx->remote);
- }
-
- if (server_id_equal(&msg_ctx->id, &server)) {
- struct messaging_selfsend_state *state;
- struct tevent_immediate *im;
-
- state = talloc_pooled_object(
- msg_ctx, struct messaging_selfsend_state,
- 1, data->length);
- if (state == NULL) {
- return NT_STATUS_NO_MEMORY;
- }
- state->msg = msg_ctx;
- state->rec.msg_version = MESSAGE_VERSION;
- state->rec.msg_type = msg_type & MSG_TYPE_MASK;
- state->rec.dest = server;
- state->rec.src = msg_ctx->id;
-
- /* Can't fail, it's a pooled_object */
- state->rec.buf = data_blob_talloc(
- state, data->data, data->length);
-
- im = tevent_create_immediate(state);
- if (im == NULL) {
- TALLOC_FREE(state);
- return NT_STATUS_NO_MEMORY;
- }
-
- tevent_schedule_immediate(im, msg_ctx->event_ctx,
- messaging_trigger_self, state);
- return NT_STATUS_OK;
- }
+ iov.iov_base = data->data;
+ iov.iov_len = data->length;
- return msg_ctx->local->send_fn(msg_ctx, server, msg_type, data,
- msg_ctx->local);
-}
-
-static void messaging_trigger_self(struct tevent_context *ev,
- struct tevent_immediate *im,
- void *private_data)
-{
- struct messaging_selfsend_state *state = talloc_get_type_abort(
- private_data, struct messaging_selfsend_state);
- messaging_dispatch_rec(state->msg, &state->rec);
- TALLOC_FREE(state);
+ return messaging_send_iov(msg_ctx, server, msg_type, &iov, 1);
}
NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
struct server_id server, uint32_t msg_type,
const struct iovec *iov, int iovlen)
{
- uint8_t *buf;
- NTSTATUS status;
+ if (server_id_is_disconnected(&server)) {
+ return NT_STATUS_INVALID_PARAMETER_MIX;
+ }
- buf = iov_buf(talloc_tos(), iov, iovlen);
- if (buf == NULL) {
- return NT_STATUS_NO_MEMORY;
+ if (!procid_is_local(&server)) {
+ return msg_ctx->remote->send_fn(msg_ctx->id, server,
+ msg_type, iov, iovlen,
+ msg_ctx->remote);
}
- status = messaging_send_buf(msg_ctx, server, msg_type,
- buf, talloc_get_size(buf));
+ if (messaging_is_self_send(msg_ctx, &server)) {
+ struct messaging_rec rec;
+ uint8_t *buf;
+ DATA_BLOB data;
+
+ buf = iov_buf(talloc_tos(), iov, iovlen);
+ if (buf == NULL) {
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ data = data_blob_const(buf, talloc_get_size(buf));
+
+ rec.msg_version = MESSAGE_VERSION;
+ rec.msg_type = msg_type & MSG_TYPE_MASK;
+ rec.dest = server;
+ rec.src = msg_ctx->id;
+ rec.buf = data;
+ messaging_dispatch_rec(msg_ctx, &rec);
+ TALLOC_FREE(buf);
+ return NT_STATUS_OK;
+ }
- TALLOC_FREE(buf);
- return status;
+ return msg_ctx->local->send_fn(msg_ctx->id, server, msg_type,
+ iov, iovlen, msg_ctx->local);
}
static struct messaging_rec *messaging_rec_dup(TALLOC_CTX *mem_ctx,
return result;
}
-struct messaging_read_state {
+struct messaging_filtered_read_state {
struct tevent_context *ev;
struct messaging_context *msg_ctx;
- uint32_t msg_type;
+ void *tevent_handle;
+
+ bool (*filter)(struct messaging_rec *rec, void *private_data);
+ void *private_data;
+
struct messaging_rec *rec;
};
-static void messaging_read_cleanup(struct tevent_req *req,
- enum tevent_req_state req_state);
+static void messaging_filtered_read_cleanup(struct tevent_req *req,
+ enum tevent_req_state req_state);
-struct tevent_req *messaging_read_send(TALLOC_CTX *mem_ctx,
- struct tevent_context *ev,
- struct messaging_context *msg_ctx,
- uint32_t msg_type)
+struct tevent_req *messaging_filtered_read_send(
+ TALLOC_CTX *mem_ctx, struct tevent_context *ev,
+ struct messaging_context *msg_ctx,
+ bool (*filter)(struct messaging_rec *rec, void *private_data),
+ void *private_data)
{
struct tevent_req *req;
- struct messaging_read_state *state;
+ struct messaging_filtered_read_state *state;
size_t new_waiters_len;
req = tevent_req_create(mem_ctx, &state,
- struct messaging_read_state);
+ struct messaging_filtered_read_state);
if (req == NULL) {
return NULL;
}
state->ev = ev;
state->msg_ctx = msg_ctx;
- state->msg_type = msg_type;
+ state->filter = filter;
+ state->private_data = private_data;
+
+ /*
+ * We have to defer the callback here, as we might be called from
+ * within a different tevent_context than state->ev
+ */
+ tevent_req_defer_callback(req, state->ev);
+
+ state->tevent_handle = messaging_dgm_register_tevent_context(
+ state, msg_ctx, ev);
+ if (tevent_req_nomem(state, req)) {
+ return tevent_req_post(req, ev);
+ }
+
+ /*
+ * We add ourselves to the "new_waiters" array, not the "waiters"
+ * array. If we are called from within messaging_read_done,
+ * messaging_dispatch_rec will be in an active for-loop on
+ * "waiters". We must be careful not to mess with this array, because
+ * it could mean that a single event is being delivered twice.
+ */
new_waiters_len = talloc_array_length(msg_ctx->new_waiters);
msg_ctx->new_waiters[msg_ctx->num_new_waiters] = req;
msg_ctx->num_new_waiters += 1;
- tevent_req_set_cleanup_fn(req, messaging_read_cleanup);
+ tevent_req_set_cleanup_fn(req, messaging_filtered_read_cleanup);
return req;
}
-static void messaging_read_cleanup(struct tevent_req *req,
- enum tevent_req_state req_state)
+static void messaging_filtered_read_cleanup(struct tevent_req *req,
+ enum tevent_req_state req_state)
{
- struct messaging_read_state *state = tevent_req_data(
- req, struct messaging_read_state);
+ struct messaging_filtered_read_state *state = tevent_req_data(
+ req, struct messaging_filtered_read_state);
struct messaging_context *msg_ctx = state->msg_ctx;
unsigned i;
tevent_req_set_cleanup_fn(req, NULL);
+ TALLOC_FREE(state->tevent_handle);
+
+ /*
+ * Just set the [new_]waiters entry to NULL, be careful not to mess
+ * with the other "waiters" array contents. We are often called from
+ * within "messaging_dispatch_rec", which loops over
+ * "waiters". Messing with the "waiters" array will mess up that
+ * for-loop.
+ */
+
for (i=0; i<msg_ctx->num_waiters; i++) {
if (msg_ctx->waiters[i] == req) {
msg_ctx->waiters[i] = NULL;
}
}
-static void messaging_read_done(struct tevent_req *req,
- struct messaging_rec *rec)
+static void messaging_filtered_read_done(struct tevent_req *req,
+ struct messaging_rec *rec)
{
- struct messaging_read_state *state = tevent_req_data(
- req, struct messaging_read_state);
+ struct messaging_filtered_read_state *state = tevent_req_data(
+ req, struct messaging_filtered_read_state);
state->rec = messaging_rec_dup(state, rec);
if (tevent_req_nomem(state->rec, req)) {
tevent_req_done(req);
}
+int messaging_filtered_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+ struct messaging_rec **presult)
+{
+ struct messaging_filtered_read_state *state = tevent_req_data(
+ req, struct messaging_filtered_read_state);
+ int err;
+
+ if (tevent_req_is_unix_error(req, &err)) {
+ tevent_req_received(req);
+ return err;
+ }
+ *presult = talloc_move(mem_ctx, &state->rec);
+ return 0;
+}
+
+struct messaging_read_state {
+ uint32_t msg_type;
+ struct messaging_rec *rec;
+};
+
+static bool messaging_read_filter(struct messaging_rec *rec,
+ void *private_data);
+static void messaging_read_done(struct tevent_req *subreq);
+
+struct tevent_req *messaging_read_send(TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct messaging_context *msg,
+ uint32_t msg_type)
+{
+ struct tevent_req *req, *subreq;
+ struct messaging_read_state *state;
+
+ req = tevent_req_create(mem_ctx, &state,
+ struct messaging_read_state);
+ if (req == NULL) {
+ return NULL;
+ }
+ state->msg_type = msg_type;
+
+ subreq = messaging_filtered_read_send(state, ev, msg,
+ messaging_read_filter, state);
+ if (tevent_req_nomem(subreq, req)) {
+ return tevent_req_post(req, ev);
+ }
+ tevent_req_set_callback(subreq, messaging_read_done, req);
+ return req;
+}
+
+static bool messaging_read_filter(struct messaging_rec *rec,
+ void *private_data)
+{
+ struct messaging_read_state *state = talloc_get_type_abort(
+ private_data, struct messaging_read_state);
+
+ return rec->msg_type == state->msg_type;
+}
+
+static void messaging_read_done(struct tevent_req *subreq)
+{
+ struct tevent_req *req = tevent_req_callback_data(
+ subreq, struct tevent_req);
+ struct messaging_read_state *state = tevent_req_data(
+ req, struct messaging_read_state);
+ int ret;
+
+ ret = messaging_filtered_read_recv(subreq, state, &state->rec);
+ TALLOC_FREE(subreq);
+ if (tevent_req_error(req, ret)) {
+ return;
+ }
+ tevent_req_done(req);
+}
+
int messaging_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
struct messaging_rec **presult)
{
int err;
if (tevent_req_is_unix_error(req, &err)) {
- tevent_req_received(req);
return err;
}
if (presult != NULL) {
return true;
}
+struct messaging_defer_callback_state {
+ struct messaging_context *msg_ctx;
+ struct messaging_rec *rec;
+ void (*fn)(struct messaging_context *msg, void *private_data,
+ uint32_t msg_type, struct server_id server_id,
+ DATA_BLOB *data);
+ void *private_data;
+};
+
+static void messaging_defer_callback_trigger(struct tevent_context *ev,
+ struct tevent_immediate *im,
+ void *private_data);
+
+static void messaging_defer_callback(
+ struct messaging_context *msg_ctx, struct messaging_rec *rec,
+ void (*fn)(struct messaging_context *msg, void *private_data,
+ uint32_t msg_type, struct server_id server_id,
+ DATA_BLOB *data),
+ void *private_data)
+{
+ struct messaging_defer_callback_state *state;
+ struct tevent_immediate *im;
+
+ state = talloc(msg_ctx, struct messaging_defer_callback_state);
+ if (state == NULL) {
+ DEBUG(1, ("talloc failed\n"));
+ return;
+ }
+ state->msg_ctx = msg_ctx;
+ state->fn = fn;
+ state->private_data = private_data;
+
+ state->rec = messaging_rec_dup(state, rec);
+ if (state->rec == NULL) {
+ DEBUG(1, ("talloc failed\n"));
+ TALLOC_FREE(state);
+ return;
+ }
+
+ im = tevent_create_immediate(state);
+ if (im == NULL) {
+ DEBUG(1, ("tevent_create_immediate failed\n"));
+ TALLOC_FREE(state);
+ return;
+ }
+ tevent_schedule_immediate(im, msg_ctx->event_ctx,
+ messaging_defer_callback_trigger, state);
+}
+
+static void messaging_defer_callback_trigger(struct tevent_context *ev,
+ struct tevent_immediate *im,
+ void *private_data)
+{
+ struct messaging_defer_callback_state *state = talloc_get_type_abort(
+ private_data, struct messaging_defer_callback_state);
+ struct messaging_rec *rec = state->rec;
+
+ state->fn(state->msg_ctx, state->private_data, rec->msg_type, rec->src,
+ &rec->buf);
+ TALLOC_FREE(state);
+}
+
/*
Dispatch one messaging_rec
*/
for (cb = msg_ctx->callbacks; cb != NULL; cb = next) {
next = cb->next;
- if (cb->msg_type == rec->msg_type) {
+ if (cb->msg_type != rec->msg_type) {
+ continue;
+ }
+
+ if (messaging_is_self_send(msg_ctx, &rec->dest)) {
+ /*
+ * This is a self-send. We are called here from
+ * messaging_send(), and we don't want to directly
+ * recurse into the callback but go via a
+ * tevent_loop_once
+ */
+ messaging_defer_callback(msg_ctx, rec, cb->fn,
+ cb->private_data);
+ } else {
+ /*
+ * This comes from a different process. we are called
+ * from the event loop, so we should call back
+ * directly.
+ */
cb->fn(msg_ctx, cb->private_data, rec->msg_type,
rec->src, &rec->buf);
- /* we continue looking for matching messages
- after finding one. This matters for
- subsystems like the internal notify code
- which register more than one handler for
- the same message type */
}
+ /*
+ * we continue looking for matching messages after finding
+ * one. This matters for subsystems like the internal notify
+ * code which register more than one handler for the same
+ * message type
+ */
}
if (!messaging_append_new_waiters(msg_ctx)) {
i = 0;
while (i < msg_ctx->num_waiters) {
struct tevent_req *req;
- struct messaging_read_state *state;
+ struct messaging_filtered_read_state *state;
req = msg_ctx->waiters[i];
if (req == NULL) {
* to keep the order of waiters, as
* other code may depend on this.
*/
- if (i < msg_ctx->num_waiters - 1) {
+ if (i < msg_ctx->num_waiters - 1) {
memmove(&msg_ctx->waiters[i],
&msg_ctx->waiters[i+1],
sizeof(struct tevent_req *) *
- (msg_ctx->num_waiters - i - 1));
+ (msg_ctx->num_waiters - i - 1));
}
msg_ctx->num_waiters -= 1;
continue;
}
- state = tevent_req_data(req, struct messaging_read_state);
- if (state->msg_type == rec->msg_type) {
- messaging_read_done(req, rec);
+ state = tevent_req_data(
+ req, struct messaging_filtered_read_state);
+ if (state->filter(rec, state->private_data)) {
+ messaging_filtered_read_done(req, rec);
}
i += 1;
}
- return;
}
static int mess_parent_dgm_cleanup(void *private_data);
req = background_job_send(
msg, msg->event_ctx, msg, NULL, 0,
- lp_parm_int(-1, "messaging", "messaging dgm cleanup interval", 60*15),
+ lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
+ 60*15),
mess_parent_dgm_cleanup, msg);
if (req == NULL) {
return false;
status = messaging_dgm_wipe(msg_ctx);
DEBUG(10, ("messaging_dgm_wipe returned %s\n", nt_errstr(status)));
- return lp_parm_int(-1, "messaging", "messaging dgm cleanup interval", 60*15);
+ return lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
+ 60*15);
}
static void mess_parent_dgm_cleanup_done(struct tevent_req *req)
status = background_job_recv(req);
TALLOC_FREE(req);
- DEBUG(1, ("messaging dgm cleanup job ended with %s\n", nt_errstr(status)));
+ DEBUG(1, ("messaging dgm cleanup job ended with %s\n",
+ nt_errstr(status)));
req = background_job_send(
msg, msg->event_ctx, msg, NULL, 0,
- lp_parm_int(-1, "messaging", "messaging dgm cleanup interval", 60*15),
+ lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
+ 60*15),
mess_parent_dgm_cleanup, msg);
if (req == NULL) {
DEBUG(1, ("background_job_send failed\n"));
tevent_req_set_callback(req, mess_parent_dgm_cleanup_done, msg);
}
+struct messaging_backend *messaging_local_backend(
+ struct messaging_context *msg_ctx)
+{
+ return msg_ctx->local;
+}
+
/** @} **/