2 Unix SMB/CIFS implementation.
3 Samba internal messaging functions
4 Copyright (C) Andrew Tridgell 2000
5 Copyright (C) 2001 by Martin Pool
6 Copyright (C) 2002 by Jeremy Allison
7 Copyright (C) 2007 by Volker Lendecke
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 @defgroup messages Internal messaging framework
28 @brief Module for internal messaging between Samba daemons.
30 The idea is that if a part of Samba wants to do communication with
31 another Samba process then it will do a message_register() of a
32 dispatch function, and use message_send_pid() to send messages to
35 The dispatch function is given the pid of the sender, and it can
36 use that to reply by message_send_pid(). See ping_message() for a
39 @caution Dispatch functions must be able to cope with incoming
40 messages on an *odd* byte boundary.
42 This system doesn't have any inherent size limitations but is not
43 very efficient for large messages or when messages are sent in very
49 #include "dbwrap/dbwrap.h"
52 #include "lib/util/tevent_unix.h"
53 #include "lib/background.h"
54 #include "lib/messages_dgm.h"
55 #include "lib/util/iov_buf.h"
56 #include "lib/util/server_id_db.h"
57 #include "lib/messages_dgm_ref.h"
58 #include "lib/messages_util.h"
60 struct messaging_callback {
61 struct messaging_callback *prev, *next;
63 void (*fn)(struct messaging_context *msg, void *private_data,
65 struct server_id server_id, DATA_BLOB *data);
69 struct messaging_context {
71 struct tevent_context *event_ctx;
72 struct messaging_callback *callbacks;
74 struct tevent_req **new_waiters;
75 unsigned num_new_waiters;
77 struct tevent_req **waiters;
81 struct messaging_backend *remote;
83 struct server_id_db *names_db;
86 static struct messaging_rec *messaging_rec_dup(TALLOC_CTX *mem_ctx,
87 struct messaging_rec *rec);
88 static void messaging_dispatch_rec(struct messaging_context *msg_ctx,
89 struct tevent_context *ev,
90 struct messaging_rec *rec);
92 /****************************************************************************
93 A useful function for testing the message system.
94 ****************************************************************************/
96 static void ping_message(struct messaging_context *msg_ctx,
102 struct server_id_buf idbuf;
104 DEBUG(1, ("INFO: Received PING message from PID %s [%.*s]\n",
105 server_id_str_buf(src, &idbuf), (int)data->length,
106 data->data ? (char *)data->data : ""));
108 messaging_send(msg_ctx, src, MSG_PONG, data);
111 static struct messaging_rec *messaging_rec_create(
112 TALLOC_CTX *mem_ctx, struct server_id src, struct server_id dst,
113 uint32_t msg_type, const struct iovec *iov, int iovlen,
114 const int *fds, size_t num_fds)
118 struct messaging_rec *result;
120 if (num_fds > INT8_MAX) {
124 buflen = iov_buflen(iov, iovlen);
128 buf = talloc_array(mem_ctx, uint8_t, buflen);
132 iov_buf(iov, iovlen, buf, buflen);
135 struct messaging_rec rec;
136 int64_t fds64[num_fds];
139 for (i=0; i<num_fds; i++) {
143 rec = (struct messaging_rec) {
144 .msg_version = MESSAGE_VERSION, .msg_type = msg_type,
145 .src = src, .dest = dst,
146 .buf.data = buf, .buf.length = buflen,
147 .num_fds = num_fds, .fds = fds64,
150 result = messaging_rec_dup(mem_ctx, &rec);
158 static void messaging_recv_cb(struct tevent_context *ev,
159 const uint8_t *msg, size_t msg_len,
160 int *fds, size_t num_fds,
163 struct messaging_context *msg_ctx = talloc_get_type_abort(
164 private_data, struct messaging_context);
165 struct server_id_buf idbuf;
166 struct messaging_rec rec;
167 int64_t fds64[MIN(num_fds, INT8_MAX)];
170 if (msg_len < MESSAGE_HDR_LENGTH) {
171 DBG_WARNING("message too short: %zu\n", msg_len);
175 if (num_fds > INT8_MAX) {
176 DBG_WARNING("too many fds: %zu\n", num_fds);
181 * "consume" the fds by copying them and setting
182 * the original variable to -1
184 for (i=0; i < num_fds; i++) {
189 rec = (struct messaging_rec) {
190 .msg_version = MESSAGE_VERSION,
191 .buf.data = discard_const_p(uint8_t, msg) + MESSAGE_HDR_LENGTH,
192 .buf.length = msg_len - MESSAGE_HDR_LENGTH,
197 message_hdr_get(&rec.msg_type, &rec.src, &rec.dest, msg);
199 DBG_DEBUG("Received message 0x%x len %zu (num_fds:%zu) from %s\n",
200 (unsigned)rec.msg_type, rec.buf.length, num_fds,
201 server_id_str_buf(rec.src, &idbuf));
203 messaging_dispatch_rec(msg_ctx, ev, &rec);
207 for (i=0; i < num_fds; i++) {
212 static int messaging_context_destructor(struct messaging_context *ctx)
216 for (i=0; i<ctx->num_new_waiters; i++) {
217 if (ctx->new_waiters[i] != NULL) {
218 tevent_req_set_cleanup_fn(ctx->new_waiters[i], NULL);
219 ctx->new_waiters[i] = NULL;
222 for (i=0; i<ctx->num_waiters; i++) {
223 if (ctx->waiters[i] != NULL) {
224 tevent_req_set_cleanup_fn(ctx->waiters[i], NULL);
225 ctx->waiters[i] = NULL;
232 static const char *private_path(const char *name)
234 return talloc_asprintf(talloc_tos(), "%s/%s", lp_private_dir(), name);
237 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
238 struct tevent_context *ev)
240 struct messaging_context *ctx;
242 const char *lck_path;
243 const char *priv_path;
246 if (!(ctx = talloc_zero(mem_ctx, struct messaging_context))) {
250 ctx->id = (struct server_id) {
251 .pid = getpid(), .vnn = NONCLUSTER_VNN
258 lck_path = lock_path("msg.lock");
259 if (lck_path == NULL) {
264 ok = directory_create_or_exist_strict(lck_path, sec_initial_uid(),
267 DEBUG(10, ("%s: Could not create lock directory: %s\n",
268 __func__, strerror(errno)));
273 priv_path = private_path("msg.sock");
274 if (priv_path == NULL) {
279 ok = directory_create_or_exist_strict(priv_path, sec_initial_uid(),
282 DEBUG(10, ("%s: Could not create msg directory: %s\n",
283 __func__, strerror(errno)));
288 ctx->msg_dgm_ref = messaging_dgm_ref(
289 ctx, ctx->event_ctx, &ctx->id.unique_id,
290 priv_path, lck_path, messaging_recv_cb, ctx, &ret);
292 if (ctx->msg_dgm_ref == NULL) {
293 DEBUG(2, ("messaging_dgm_ref failed: %s\n", strerror(ret)));
298 talloc_set_destructor(ctx, messaging_context_destructor);
300 if (lp_clustering()) {
301 ret = messaging_ctdbd_init(ctx, ctx, &ctx->remote);
304 DEBUG(2, ("messaging_ctdbd_init failed: %s\n",
310 ctx->id.vnn = get_my_vnn();
312 ctx->names_db = server_id_db_init(
313 ctx, ctx->id, lp_lock_directory(), 0,
314 TDB_INCOMPATIBLE_HASH|TDB_CLEAR_IF_FIRST);
315 if (ctx->names_db == NULL) {
316 DEBUG(10, ("%s: server_id_db_init failed\n", __func__));
321 messaging_register(ctx, NULL, MSG_PING, ping_message);
323 /* Register some debugging related messages */
325 register_msg_pool_usage(ctx);
326 register_dmalloc_msgs(ctx);
327 debug_register_msgs(ctx);
330 struct server_id_buf tmp;
331 DBG_DEBUG("my id: %s\n", server_id_str_buf(ctx->id, &tmp));
337 struct server_id messaging_server_id(const struct messaging_context *msg_ctx)
343 * re-init after a fork
345 NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
350 TALLOC_FREE(msg_ctx->msg_dgm_ref);
352 msg_ctx->id = (struct server_id) {
353 .pid = getpid(), .vnn = msg_ctx->id.vnn
356 lck_path = lock_path("msg.lock");
357 if (lck_path == NULL) {
358 return NT_STATUS_NO_MEMORY;
361 msg_ctx->msg_dgm_ref = messaging_dgm_ref(
362 msg_ctx, msg_ctx->event_ctx, &msg_ctx->id.unique_id,
363 private_path("msg.sock"), lck_path,
364 messaging_recv_cb, msg_ctx, &ret);
366 if (msg_ctx->msg_dgm_ref == NULL) {
367 DEBUG(2, ("messaging_dgm_ref failed: %s\n", strerror(ret)));
368 return map_nt_error_from_unix(ret);
371 if (lp_clustering()) {
372 ret = messaging_ctdbd_reinit(msg_ctx, msg_ctx,
376 DEBUG(1, ("messaging_ctdbd_init failed: %s\n",
378 return map_nt_error_from_unix(ret);
382 server_id_db_reinit(msg_ctx->names_db, msg_ctx->id);
389 * Register a dispatch function for a particular message type. Allow multiple
392 NTSTATUS messaging_register(struct messaging_context *msg_ctx,
395 void (*fn)(struct messaging_context *msg,
398 struct server_id server_id,
401 struct messaging_callback *cb;
403 DEBUG(5, ("Registering messaging pointer for type %u - "
405 (unsigned)msg_type, private_data));
408 * Only one callback per type
411 for (cb = msg_ctx->callbacks; cb != NULL; cb = cb->next) {
412 /* we allow a second registration of the same message
413 type if it has a different private pointer. This is
414 needed in, for example, the internal notify code,
415 which creates a new notify context for each tree
416 connect, and expects to receive messages to each of
418 if (cb->msg_type == msg_type && private_data == cb->private_data) {
419 DEBUG(5,("Overriding messaging pointer for type %u - private_data=%p\n",
420 (unsigned)msg_type, private_data));
422 cb->private_data = private_data;
427 if (!(cb = talloc(msg_ctx, struct messaging_callback))) {
428 return NT_STATUS_NO_MEMORY;
431 cb->msg_type = msg_type;
433 cb->private_data = private_data;
435 DLIST_ADD(msg_ctx->callbacks, cb);
440 De-register the function for a particular message type.
442 void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
445 struct messaging_callback *cb, *next;
447 for (cb = ctx->callbacks; cb; cb = next) {
449 if ((cb->msg_type == msg_type)
450 && (cb->private_data == private_data)) {
451 DEBUG(5,("Deregistering messaging pointer for type %u - private_data=%p\n",
452 (unsigned)msg_type, private_data));
453 DLIST_REMOVE(ctx->callbacks, cb);
460 Send a message to a particular server
462 NTSTATUS messaging_send(struct messaging_context *msg_ctx,
463 struct server_id server, uint32_t msg_type,
464 const DATA_BLOB *data)
466 struct iovec iov = {0};
469 iov.iov_base = data->data;
470 iov.iov_len = data->length;
473 return messaging_send_iov(msg_ctx, server, msg_type, &iov, 1, NULL, 0);
476 NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
477 struct server_id server, uint32_t msg_type,
478 const uint8_t *buf, size_t len)
480 DATA_BLOB blob = data_blob_const(buf, len);
481 return messaging_send(msg_ctx, server, msg_type, &blob);
484 struct messaging_post_state {
485 struct messaging_context *msg_ctx;
486 struct messaging_rec *rec;
489 static void messaging_post_handler(struct tevent_context *ev,
490 struct tevent_immediate *ti,
493 static int messaging_post_self(struct messaging_context *msg_ctx,
494 struct server_id src, struct server_id dst,
496 const struct iovec *iov, int iovlen,
497 const int *fds, size_t num_fds)
499 struct tevent_immediate *ti;
500 struct messaging_post_state *state;
502 state = talloc(msg_ctx, struct messaging_post_state);
506 state->msg_ctx = msg_ctx;
508 ti = tevent_create_immediate(state);
512 state->rec = messaging_rec_create(
513 state, src, dst, msg_type, iov, iovlen, fds, num_fds);
514 if (state->rec == NULL) {
518 tevent_schedule_immediate(ti, msg_ctx->event_ctx,
519 messaging_post_handler, state);
527 static void messaging_post_handler(struct tevent_context *ev,
528 struct tevent_immediate *ti,
531 struct messaging_post_state *state = talloc_get_type_abort(
532 private_data, struct messaging_post_state);
533 messaging_dispatch_rec(state->msg_ctx, ev, state->rec);
537 int messaging_send_iov_from(struct messaging_context *msg_ctx,
538 struct server_id src, struct server_id dst,
540 const struct iovec *iov, int iovlen,
541 const int *fds, size_t num_fds)
544 uint8_t hdr[MESSAGE_HDR_LENGTH];
545 struct iovec iov2[iovlen+1];
547 if (server_id_is_disconnected(&dst)) {
551 if (num_fds > INT8_MAX) {
555 if (dst.vnn != msg_ctx->id.vnn) {
560 ret = msg_ctx->remote->send_fn(src, dst,
561 msg_type, iov, iovlen,
567 if (server_id_equal(&dst, &msg_ctx->id)) {
568 ret = messaging_post_self(msg_ctx, src, dst, msg_type,
569 iov, iovlen, fds, num_fds);
573 message_hdr_put(hdr, msg_type, src, dst);
574 iov2[0] = (struct iovec){ .iov_base = hdr, .iov_len = sizeof(hdr) };
575 memcpy(&iov2[1], iov, iovlen * sizeof(*iov));
577 ret = messaging_dgm_send(dst.pid, iov2, iovlen+1, fds, num_fds);
581 ret = messaging_dgm_send(dst.pid, iov2, iovlen+1,
589 NTSTATUS messaging_send_iov(struct messaging_context *msg_ctx,
590 struct server_id dst, uint32_t msg_type,
591 const struct iovec *iov, int iovlen,
592 const int *fds, size_t num_fds)
596 ret = messaging_send_iov_from(msg_ctx, msg_ctx->id, dst, msg_type,
597 iov, iovlen, fds, num_fds);
599 return map_nt_error_from_unix(ret);
604 static struct messaging_rec *messaging_rec_dup(TALLOC_CTX *mem_ctx,
605 struct messaging_rec *rec)
607 struct messaging_rec *result;
608 size_t fds_size = sizeof(int64_t) * rec->num_fds;
611 payload_len = rec->buf.length + fds_size;
612 if (payload_len < rec->buf.length) {
617 result = talloc_pooled_object(mem_ctx, struct messaging_rec, 2,
619 if (result == NULL) {
624 /* Doesn't fail, see talloc_pooled_object */
626 result->buf.data = talloc_memdup(result, rec->buf.data,
630 if (result->num_fds > 0) {
631 result->fds = talloc_memdup(result, rec->fds, fds_size);
637 struct messaging_filtered_read_state {
638 struct tevent_context *ev;
639 struct messaging_context *msg_ctx;
640 struct messaging_dgm_fde *fde;
642 bool (*filter)(struct messaging_rec *rec, void *private_data);
645 struct messaging_rec *rec;
648 static void messaging_filtered_read_cleanup(struct tevent_req *req,
649 enum tevent_req_state req_state);
651 struct tevent_req *messaging_filtered_read_send(
652 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
653 struct messaging_context *msg_ctx,
654 bool (*filter)(struct messaging_rec *rec, void *private_data),
657 struct tevent_req *req;
658 struct messaging_filtered_read_state *state;
659 size_t new_waiters_len;
661 req = tevent_req_create(mem_ctx, &state,
662 struct messaging_filtered_read_state);
667 state->msg_ctx = msg_ctx;
668 state->filter = filter;
669 state->private_data = private_data;
672 * We have to defer the callback here, as we might be called from
673 * within a different tevent_context than state->ev
675 tevent_req_defer_callback(req, state->ev);
677 state->fde = messaging_dgm_register_tevent_context(state, ev);
678 if (tevent_req_nomem(state->fde, req)) {
679 return tevent_req_post(req, ev);
683 * We add ourselves to the "new_waiters" array, not the "waiters"
684 * array. If we are called from within messaging_read_done,
685 * messaging_dispatch_rec will be in an active for-loop on
686 * "waiters". We must be careful not to mess with this array, because
687 * it could mean that a single event is being delivered twice.
690 new_waiters_len = talloc_array_length(msg_ctx->new_waiters);
692 if (new_waiters_len == msg_ctx->num_new_waiters) {
693 struct tevent_req **tmp;
695 tmp = talloc_realloc(msg_ctx, msg_ctx->new_waiters,
696 struct tevent_req *, new_waiters_len+1);
697 if (tevent_req_nomem(tmp, req)) {
698 return tevent_req_post(req, ev);
700 msg_ctx->new_waiters = tmp;
703 msg_ctx->new_waiters[msg_ctx->num_new_waiters] = req;
704 msg_ctx->num_new_waiters += 1;
705 tevent_req_set_cleanup_fn(req, messaging_filtered_read_cleanup);
710 static void messaging_filtered_read_cleanup(struct tevent_req *req,
711 enum tevent_req_state req_state)
713 struct messaging_filtered_read_state *state = tevent_req_data(
714 req, struct messaging_filtered_read_state);
715 struct messaging_context *msg_ctx = state->msg_ctx;
718 tevent_req_set_cleanup_fn(req, NULL);
720 TALLOC_FREE(state->fde);
723 * Just set the [new_]waiters entry to NULL, be careful not to mess
724 * with the other "waiters" array contents. We are often called from
725 * within "messaging_dispatch_rec", which loops over
726 * "waiters". Messing with the "waiters" array will mess up that
730 for (i=0; i<msg_ctx->num_waiters; i++) {
731 if (msg_ctx->waiters[i] == req) {
732 msg_ctx->waiters[i] = NULL;
737 for (i=0; i<msg_ctx->num_new_waiters; i++) {
738 if (msg_ctx->new_waiters[i] == req) {
739 msg_ctx->new_waiters[i] = NULL;
745 static void messaging_filtered_read_done(struct tevent_req *req,
746 struct messaging_rec *rec)
748 struct messaging_filtered_read_state *state = tevent_req_data(
749 req, struct messaging_filtered_read_state);
751 state->rec = messaging_rec_dup(state, rec);
752 if (tevent_req_nomem(state->rec, req)) {
755 tevent_req_done(req);
758 int messaging_filtered_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
759 struct messaging_rec **presult)
761 struct messaging_filtered_read_state *state = tevent_req_data(
762 req, struct messaging_filtered_read_state);
765 if (tevent_req_is_unix_error(req, &err)) {
766 tevent_req_received(req);
769 if (presult != NULL) {
770 *presult = talloc_move(mem_ctx, &state->rec);
775 struct messaging_read_state {
777 struct messaging_rec *rec;
780 static bool messaging_read_filter(struct messaging_rec *rec,
782 static void messaging_read_done(struct tevent_req *subreq);
784 struct tevent_req *messaging_read_send(TALLOC_CTX *mem_ctx,
785 struct tevent_context *ev,
786 struct messaging_context *msg,
789 struct tevent_req *req, *subreq;
790 struct messaging_read_state *state;
792 req = tevent_req_create(mem_ctx, &state,
793 struct messaging_read_state);
797 state->msg_type = msg_type;
799 subreq = messaging_filtered_read_send(state, ev, msg,
800 messaging_read_filter, state);
801 if (tevent_req_nomem(subreq, req)) {
802 return tevent_req_post(req, ev);
804 tevent_req_set_callback(subreq, messaging_read_done, req);
808 static bool messaging_read_filter(struct messaging_rec *rec,
811 struct messaging_read_state *state = talloc_get_type_abort(
812 private_data, struct messaging_read_state);
814 if (rec->num_fds != 0) {
818 return rec->msg_type == state->msg_type;
821 static void messaging_read_done(struct tevent_req *subreq)
823 struct tevent_req *req = tevent_req_callback_data(
824 subreq, struct tevent_req);
825 struct messaging_read_state *state = tevent_req_data(
826 req, struct messaging_read_state);
829 ret = messaging_filtered_read_recv(subreq, state, &state->rec);
831 if (tevent_req_error(req, ret)) {
834 tevent_req_done(req);
837 int messaging_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
838 struct messaging_rec **presult)
840 struct messaging_read_state *state = tevent_req_data(
841 req, struct messaging_read_state);
844 if (tevent_req_is_unix_error(req, &err)) {
847 if (presult != NULL) {
848 *presult = talloc_move(mem_ctx, &state->rec);
853 struct messaging_handler_state {
854 struct tevent_context *ev;
855 struct messaging_context *msg_ctx;
857 bool (*handler)(struct messaging_context *msg_ctx,
858 struct messaging_rec **rec, void *private_data);
862 static void messaging_handler_got_msg(struct tevent_req *subreq);
864 struct tevent_req *messaging_handler_send(
865 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
866 struct messaging_context *msg_ctx, uint32_t msg_type,
867 bool (*handler)(struct messaging_context *msg_ctx,
868 struct messaging_rec **rec, void *private_data),
871 struct tevent_req *req, *subreq;
872 struct messaging_handler_state *state;
874 req = tevent_req_create(mem_ctx, &state,
875 struct messaging_handler_state);
880 state->msg_ctx = msg_ctx;
881 state->msg_type = msg_type;
882 state->handler = handler;
883 state->private_data = private_data;
885 subreq = messaging_read_send(state, state->ev, state->msg_ctx,
887 if (tevent_req_nomem(subreq, req)) {
888 return tevent_req_post(req, ev);
890 tevent_req_set_callback(subreq, messaging_handler_got_msg, req);
894 static void messaging_handler_got_msg(struct tevent_req *subreq)
896 struct tevent_req *req = tevent_req_callback_data(
897 subreq, struct tevent_req);
898 struct messaging_handler_state *state = tevent_req_data(
899 req, struct messaging_handler_state);
900 struct messaging_rec *rec;
904 ret = messaging_read_recv(subreq, state, &rec);
906 if (tevent_req_error(req, ret)) {
910 subreq = messaging_read_send(state, state->ev, state->msg_ctx,
912 if (tevent_req_nomem(subreq, req)) {
915 tevent_req_set_callback(subreq, messaging_handler_got_msg, req);
917 ok = state->handler(state->msg_ctx, &rec, state->private_data);
926 tevent_req_done(req);
929 int messaging_handler_recv(struct tevent_req *req)
931 return tevent_req_simple_recv_unix(req);
934 static bool messaging_append_new_waiters(struct messaging_context *msg_ctx)
936 if (msg_ctx->num_new_waiters == 0) {
940 if (talloc_array_length(msg_ctx->waiters) <
941 (msg_ctx->num_waiters + msg_ctx->num_new_waiters)) {
942 struct tevent_req **tmp;
943 tmp = talloc_realloc(
944 msg_ctx, msg_ctx->waiters, struct tevent_req *,
945 msg_ctx->num_waiters + msg_ctx->num_new_waiters);
947 DEBUG(1, ("%s: talloc failed\n", __func__));
950 msg_ctx->waiters = tmp;
953 memcpy(&msg_ctx->waiters[msg_ctx->num_waiters], msg_ctx->new_waiters,
954 sizeof(struct tevent_req *) * msg_ctx->num_new_waiters);
956 msg_ctx->num_waiters += msg_ctx->num_new_waiters;
957 msg_ctx->num_new_waiters = 0;
962 static void messaging_dispatch_classic(struct messaging_context *msg_ctx,
963 struct messaging_rec *rec)
965 struct messaging_callback *cb, *next;
967 for (cb = msg_ctx->callbacks; cb != NULL; cb = next) {
971 if (cb->msg_type != rec->msg_type) {
976 * the old style callbacks don't support fd passing
978 for (j=0; j < rec->num_fds; j++) {
979 int fd = rec->fds[j];
985 cb->fn(msg_ctx, cb->private_data, rec->msg_type,
986 rec->src, &rec->buf);
989 * we continue looking for matching messages after finding
990 * one. This matters for subsystems like the internal notify
991 * code which register more than one handler for the same
998 Dispatch one messaging_rec
1000 static void messaging_dispatch_rec(struct messaging_context *msg_ctx,
1001 struct tevent_context *ev,
1002 struct messaging_rec *rec)
1007 if (ev == msg_ctx->event_ctx) {
1008 messaging_dispatch_classic(msg_ctx, rec);
1011 if (!messaging_append_new_waiters(msg_ctx)) {
1012 for (j=0; j < rec->num_fds; j++) {
1013 int fd = rec->fds[j];
1022 while (i < msg_ctx->num_waiters) {
1023 struct tevent_req *req;
1024 struct messaging_filtered_read_state *state;
1026 req = msg_ctx->waiters[i];
1029 * This got cleaned up. In the meantime,
1030 * move everything down one. We need
1031 * to keep the order of waiters, as
1032 * other code may depend on this.
1034 if (i < msg_ctx->num_waiters - 1) {
1035 memmove(&msg_ctx->waiters[i],
1036 &msg_ctx->waiters[i+1],
1037 sizeof(struct tevent_req *) *
1038 (msg_ctx->num_waiters - i - 1));
1040 msg_ctx->num_waiters -= 1;
1044 state = tevent_req_data(
1045 req, struct messaging_filtered_read_state);
1046 if ((ev == state->ev) &&
1047 state->filter(rec, state->private_data)) {
1048 messaging_filtered_read_done(req, rec);
1051 * Only the first one gets the fd-array
1060 if (ev != msg_ctx->event_ctx) {
1062 int fds[rec->num_fds];
1066 * We've been listening on a nested event
1067 * context. Messages need to be handled in the main
1068 * event context, so post to ourselves
1071 iov.iov_base = rec->buf.data;
1072 iov.iov_len = rec->buf.length;
1074 for (i=0; i<rec->num_fds; i++) {
1075 fds[i] = rec->fds[i];
1078 ret = messaging_post_self(
1079 msg_ctx, rec->src, rec->dest, rec->msg_type,
1080 &iov, 1, fds, rec->num_fds);
1087 * If the fd-array isn't used, just close it.
1089 for (j=0; j < rec->num_fds; j++) {
1090 int fd = rec->fds[j];
1097 static int mess_parent_dgm_cleanup(void *private_data);
1098 static void mess_parent_dgm_cleanup_done(struct tevent_req *req);
1100 bool messaging_parent_dgm_cleanup_init(struct messaging_context *msg)
1102 struct tevent_req *req;
1104 req = background_job_send(
1105 msg, msg->event_ctx, msg, NULL, 0,
1106 lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
1108 mess_parent_dgm_cleanup, msg);
1112 tevent_req_set_callback(req, mess_parent_dgm_cleanup_done, msg);
1116 static int mess_parent_dgm_cleanup(void *private_data)
1120 ret = messaging_dgm_wipe();
1121 DEBUG(10, ("messaging_dgm_wipe returned %s\n",
1122 ret ? strerror(ret) : "ok"));
1123 return lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
1127 static void mess_parent_dgm_cleanup_done(struct tevent_req *req)
1129 struct messaging_context *msg = tevent_req_callback_data(
1130 req, struct messaging_context);
1133 status = background_job_recv(req);
1135 DEBUG(1, ("messaging dgm cleanup job ended with %s\n",
1136 nt_errstr(status)));
1138 req = background_job_send(
1139 msg, msg->event_ctx, msg, NULL, 0,
1140 lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
1142 mess_parent_dgm_cleanup, msg);
1144 DEBUG(1, ("background_job_send failed\n"));
1147 tevent_req_set_callback(req, mess_parent_dgm_cleanup_done, msg);
1150 int messaging_cleanup(struct messaging_context *msg_ctx, pid_t pid)
1155 ret = messaging_dgm_wipe();
1157 ret = messaging_dgm_cleanup(pid);
1163 struct tevent_context *messaging_tevent_context(
1164 struct messaging_context *msg_ctx)
1166 return msg_ctx->event_ctx;
1169 struct server_id_db *messaging_names_db(struct messaging_context *msg_ctx)
1171 return msg_ctx->names_db;