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
;
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
->tevent_handle
= messaging_dgm_register_tevent_context(
679 if (tevent_req_nomem(state
->tevent_handle
, req
)) {
680 return tevent_req_post(req
, ev
);
684 * We add ourselves to the "new_waiters" array, not the "waiters"
685 * array. If we are called from within messaging_read_done,
686 * messaging_dispatch_rec will be in an active for-loop on
687 * "waiters". We must be careful not to mess with this array, because
688 * it could mean that a single event is being delivered twice.
691 new_waiters_len
= talloc_array_length(msg_ctx
->new_waiters
);
693 if (new_waiters_len
== msg_ctx
->num_new_waiters
) {
694 struct tevent_req
**tmp
;
696 tmp
= talloc_realloc(msg_ctx
, msg_ctx
->new_waiters
,
697 struct tevent_req
*, new_waiters_len
+1);
698 if (tevent_req_nomem(tmp
, req
)) {
699 return tevent_req_post(req
, ev
);
701 msg_ctx
->new_waiters
= tmp
;
704 msg_ctx
->new_waiters
[msg_ctx
->num_new_waiters
] = req
;
705 msg_ctx
->num_new_waiters
+= 1;
706 tevent_req_set_cleanup_fn(req
, messaging_filtered_read_cleanup
);
711 static void messaging_filtered_read_cleanup(struct tevent_req
*req
,
712 enum tevent_req_state req_state
)
714 struct messaging_filtered_read_state
*state
= tevent_req_data(
715 req
, struct messaging_filtered_read_state
);
716 struct messaging_context
*msg_ctx
= state
->msg_ctx
;
719 tevent_req_set_cleanup_fn(req
, NULL
);
721 TALLOC_FREE(state
->tevent_handle
);
724 * Just set the [new_]waiters entry to NULL, be careful not to mess
725 * with the other "waiters" array contents. We are often called from
726 * within "messaging_dispatch_rec", which loops over
727 * "waiters". Messing with the "waiters" array will mess up that
731 for (i
=0; i
<msg_ctx
->num_waiters
; i
++) {
732 if (msg_ctx
->waiters
[i
] == req
) {
733 msg_ctx
->waiters
[i
] = NULL
;
738 for (i
=0; i
<msg_ctx
->num_new_waiters
; i
++) {
739 if (msg_ctx
->new_waiters
[i
] == req
) {
740 msg_ctx
->new_waiters
[i
] = NULL
;
746 static void messaging_filtered_read_done(struct tevent_req
*req
,
747 struct messaging_rec
*rec
)
749 struct messaging_filtered_read_state
*state
= tevent_req_data(
750 req
, struct messaging_filtered_read_state
);
752 state
->rec
= messaging_rec_dup(state
, rec
);
753 if (tevent_req_nomem(state
->rec
, req
)) {
756 tevent_req_done(req
);
759 int messaging_filtered_read_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
760 struct messaging_rec
**presult
)
762 struct messaging_filtered_read_state
*state
= tevent_req_data(
763 req
, struct messaging_filtered_read_state
);
766 if (tevent_req_is_unix_error(req
, &err
)) {
767 tevent_req_received(req
);
770 if (presult
!= NULL
) {
771 *presult
= talloc_move(mem_ctx
, &state
->rec
);
776 struct messaging_read_state
{
778 struct messaging_rec
*rec
;
781 static bool messaging_read_filter(struct messaging_rec
*rec
,
783 static void messaging_read_done(struct tevent_req
*subreq
);
785 struct tevent_req
*messaging_read_send(TALLOC_CTX
*mem_ctx
,
786 struct tevent_context
*ev
,
787 struct messaging_context
*msg
,
790 struct tevent_req
*req
, *subreq
;
791 struct messaging_read_state
*state
;
793 req
= tevent_req_create(mem_ctx
, &state
,
794 struct messaging_read_state
);
798 state
->msg_type
= msg_type
;
800 subreq
= messaging_filtered_read_send(state
, ev
, msg
,
801 messaging_read_filter
, state
);
802 if (tevent_req_nomem(subreq
, req
)) {
803 return tevent_req_post(req
, ev
);
805 tevent_req_set_callback(subreq
, messaging_read_done
, req
);
809 static bool messaging_read_filter(struct messaging_rec
*rec
,
812 struct messaging_read_state
*state
= talloc_get_type_abort(
813 private_data
, struct messaging_read_state
);
815 if (rec
->num_fds
!= 0) {
819 return rec
->msg_type
== state
->msg_type
;
822 static void messaging_read_done(struct tevent_req
*subreq
)
824 struct tevent_req
*req
= tevent_req_callback_data(
825 subreq
, struct tevent_req
);
826 struct messaging_read_state
*state
= tevent_req_data(
827 req
, struct messaging_read_state
);
830 ret
= messaging_filtered_read_recv(subreq
, state
, &state
->rec
);
832 if (tevent_req_error(req
, ret
)) {
835 tevent_req_done(req
);
838 int messaging_read_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
839 struct messaging_rec
**presult
)
841 struct messaging_read_state
*state
= tevent_req_data(
842 req
, struct messaging_read_state
);
845 if (tevent_req_is_unix_error(req
, &err
)) {
848 if (presult
!= NULL
) {
849 *presult
= talloc_move(mem_ctx
, &state
->rec
);
854 struct messaging_handler_state
{
855 struct tevent_context
*ev
;
856 struct messaging_context
*msg_ctx
;
858 bool (*handler
)(struct messaging_context
*msg_ctx
,
859 struct messaging_rec
**rec
, void *private_data
);
863 static void messaging_handler_got_msg(struct tevent_req
*subreq
);
865 struct tevent_req
*messaging_handler_send(
866 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
867 struct messaging_context
*msg_ctx
, uint32_t msg_type
,
868 bool (*handler
)(struct messaging_context
*msg_ctx
,
869 struct messaging_rec
**rec
, void *private_data
),
872 struct tevent_req
*req
, *subreq
;
873 struct messaging_handler_state
*state
;
875 req
= tevent_req_create(mem_ctx
, &state
,
876 struct messaging_handler_state
);
881 state
->msg_ctx
= msg_ctx
;
882 state
->msg_type
= msg_type
;
883 state
->handler
= handler
;
884 state
->private_data
= private_data
;
886 subreq
= messaging_read_send(state
, state
->ev
, state
->msg_ctx
,
888 if (tevent_req_nomem(subreq
, req
)) {
889 return tevent_req_post(req
, ev
);
891 tevent_req_set_callback(subreq
, messaging_handler_got_msg
, req
);
895 static void messaging_handler_got_msg(struct tevent_req
*subreq
)
897 struct tevent_req
*req
= tevent_req_callback_data(
898 subreq
, struct tevent_req
);
899 struct messaging_handler_state
*state
= tevent_req_data(
900 req
, struct messaging_handler_state
);
901 struct messaging_rec
*rec
;
905 ret
= messaging_read_recv(subreq
, state
, &rec
);
907 if (tevent_req_error(req
, ret
)) {
911 subreq
= messaging_read_send(state
, state
->ev
, state
->msg_ctx
,
913 if (tevent_req_nomem(subreq
, req
)) {
916 tevent_req_set_callback(subreq
, messaging_handler_got_msg
, req
);
918 ok
= state
->handler(state
->msg_ctx
, &rec
, state
->private_data
);
927 tevent_req_done(req
);
930 int messaging_handler_recv(struct tevent_req
*req
)
932 return tevent_req_simple_recv_unix(req
);
935 static bool messaging_append_new_waiters(struct messaging_context
*msg_ctx
)
937 if (msg_ctx
->num_new_waiters
== 0) {
941 if (talloc_array_length(msg_ctx
->waiters
) <
942 (msg_ctx
->num_waiters
+ msg_ctx
->num_new_waiters
)) {
943 struct tevent_req
**tmp
;
944 tmp
= talloc_realloc(
945 msg_ctx
, msg_ctx
->waiters
, struct tevent_req
*,
946 msg_ctx
->num_waiters
+ msg_ctx
->num_new_waiters
);
948 DEBUG(1, ("%s: talloc failed\n", __func__
));
951 msg_ctx
->waiters
= tmp
;
954 memcpy(&msg_ctx
->waiters
[msg_ctx
->num_waiters
], msg_ctx
->new_waiters
,
955 sizeof(struct tevent_req
*) * msg_ctx
->num_new_waiters
);
957 msg_ctx
->num_waiters
+= msg_ctx
->num_new_waiters
;
958 msg_ctx
->num_new_waiters
= 0;
963 static void messaging_dispatch_classic(struct messaging_context
*msg_ctx
,
964 struct messaging_rec
*rec
)
966 struct messaging_callback
*cb
, *next
;
968 for (cb
= msg_ctx
->callbacks
; cb
!= NULL
; cb
= next
) {
972 if (cb
->msg_type
!= rec
->msg_type
) {
977 * the old style callbacks don't support fd passing
979 for (j
=0; j
< rec
->num_fds
; j
++) {
980 int fd
= rec
->fds
[j
];
986 cb
->fn(msg_ctx
, cb
->private_data
, rec
->msg_type
,
987 rec
->src
, &rec
->buf
);
990 * we continue looking for matching messages after finding
991 * one. This matters for subsystems like the internal notify
992 * code which register more than one handler for the same
999 Dispatch one messaging_rec
1001 static void messaging_dispatch_rec(struct messaging_context
*msg_ctx
,
1002 struct tevent_context
*ev
,
1003 struct messaging_rec
*rec
)
1008 if (ev
== msg_ctx
->event_ctx
) {
1009 messaging_dispatch_classic(msg_ctx
, rec
);
1012 if (!messaging_append_new_waiters(msg_ctx
)) {
1013 for (j
=0; j
< rec
->num_fds
; j
++) {
1014 int fd
= rec
->fds
[j
];
1023 while (i
< msg_ctx
->num_waiters
) {
1024 struct tevent_req
*req
;
1025 struct messaging_filtered_read_state
*state
;
1027 req
= msg_ctx
->waiters
[i
];
1030 * This got cleaned up. In the meantime,
1031 * move everything down one. We need
1032 * to keep the order of waiters, as
1033 * other code may depend on this.
1035 if (i
< msg_ctx
->num_waiters
- 1) {
1036 memmove(&msg_ctx
->waiters
[i
],
1037 &msg_ctx
->waiters
[i
+1],
1038 sizeof(struct tevent_req
*) *
1039 (msg_ctx
->num_waiters
- i
- 1));
1041 msg_ctx
->num_waiters
-= 1;
1045 state
= tevent_req_data(
1046 req
, struct messaging_filtered_read_state
);
1047 if ((ev
== state
->ev
) &&
1048 state
->filter(rec
, state
->private_data
)) {
1049 messaging_filtered_read_done(req
, rec
);
1052 * Only the first one gets the fd-array
1061 if (ev
!= msg_ctx
->event_ctx
) {
1063 int fds
[rec
->num_fds
];
1067 * We've been listening on a nested event
1068 * context. Messages need to be handled in the main
1069 * event context, so post to ourselves
1072 iov
.iov_base
= rec
->buf
.data
;
1073 iov
.iov_len
= rec
->buf
.length
;
1075 for (i
=0; i
<rec
->num_fds
; i
++) {
1076 fds
[i
] = rec
->fds
[i
];
1079 ret
= messaging_post_self(
1080 msg_ctx
, rec
->src
, rec
->dest
, rec
->msg_type
,
1081 &iov
, 1, fds
, rec
->num_fds
);
1088 * If the fd-array isn't used, just close it.
1090 for (j
=0; j
< rec
->num_fds
; j
++) {
1091 int fd
= rec
->fds
[j
];
1098 static int mess_parent_dgm_cleanup(void *private_data
);
1099 static void mess_parent_dgm_cleanup_done(struct tevent_req
*req
);
1101 bool messaging_parent_dgm_cleanup_init(struct messaging_context
*msg
)
1103 struct tevent_req
*req
;
1105 req
= background_job_send(
1106 msg
, msg
->event_ctx
, msg
, NULL
, 0,
1107 lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
1109 mess_parent_dgm_cleanup
, msg
);
1113 tevent_req_set_callback(req
, mess_parent_dgm_cleanup_done
, msg
);
1117 static int mess_parent_dgm_cleanup(void *private_data
)
1121 ret
= messaging_dgm_wipe();
1122 DEBUG(10, ("messaging_dgm_wipe returned %s\n",
1123 ret
? strerror(ret
) : "ok"));
1124 return lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
1128 static void mess_parent_dgm_cleanup_done(struct tevent_req
*req
)
1130 struct messaging_context
*msg
= tevent_req_callback_data(
1131 req
, struct messaging_context
);
1134 status
= background_job_recv(req
);
1136 DEBUG(1, ("messaging dgm cleanup job ended with %s\n",
1137 nt_errstr(status
)));
1139 req
= background_job_send(
1140 msg
, msg
->event_ctx
, msg
, NULL
, 0,
1141 lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
1143 mess_parent_dgm_cleanup
, msg
);
1145 DEBUG(1, ("background_job_send failed\n"));
1148 tevent_req_set_callback(req
, mess_parent_dgm_cleanup_done
, msg
);
1151 int messaging_cleanup(struct messaging_context
*msg_ctx
, pid_t pid
)
1156 ret
= messaging_dgm_wipe();
1158 ret
= messaging_dgm_cleanup(pid
);
1164 struct tevent_context
*messaging_tevent_context(
1165 struct messaging_context
*msg_ctx
)
1167 return msg_ctx
->event_ctx
;
1170 struct server_id_db
*messaging_names_db(struct messaging_context
*msg_ctx
)
1172 return msg_ctx
->names_db
;