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/iov_buf.h"
57 struct messaging_callback {
58 struct messaging_callback *prev, *next;
60 void (*fn)(struct messaging_context *msg, void *private_data,
62 struct server_id server_id, DATA_BLOB *data);
66 struct messaging_context {
68 struct tevent_context *event_ctx;
69 struct messaging_callback *callbacks;
71 struct tevent_req **new_waiters;
72 unsigned num_new_waiters;
74 struct tevent_req **waiters;
77 struct messaging_backend *remote;
80 struct messaging_hdr {
86 /****************************************************************************
87 A useful function for testing the message system.
88 ****************************************************************************/
90 static void ping_message(struct messaging_context *msg_ctx,
96 struct server_id_buf idbuf;
98 DEBUG(1, ("INFO: Received PING message from PID %s [%.*s]\n",
99 server_id_str_buf(src, &idbuf), (int)data->length,
100 data->data ? (char *)data->data : ""));
102 messaging_send(msg_ctx, src, MSG_PONG, data);
105 /****************************************************************************
106 Register/replace a dispatch function for a particular message type.
107 JRA changed Dec 13 2006. Only one message handler now permitted per type.
108 *NOTE*: Dispatch functions must be able to cope with incoming
109 messages on an *odd* byte boundary.
110 ****************************************************************************/
113 struct messaging_context *msg_ctx;
121 /****************************************************************************
122 Send one of the messages for the broadcast.
123 ****************************************************************************/
125 static int traverse_fn(struct db_record *rec, const struct server_id *id,
126 uint32_t msg_flags, void *state)
128 struct msg_all *msg_all = (struct msg_all *)state;
131 /* Don't send if the receiver hasn't registered an interest. */
133 if((msg_flags & msg_all->msg_flag) == 0) {
137 /* If the msg send fails because the pid was not found (i.e. smbd died),
138 * the msg has already been deleted from the messages.tdb.*/
140 status = messaging_send_buf(msg_all->msg_ctx, *id, msg_all->msg_type,
141 (const uint8_t *)msg_all->buf, msg_all->len);
143 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
144 struct server_id_buf idbuf;
147 * If the pid was not found delete the entry from
151 DEBUG(2, ("pid %s doesn't exist\n",
152 server_id_str_buf(*id, &idbuf)));
154 dbwrap_record_delete(rec);
161 * Send a message to all smbd processes.
163 * It isn't very efficient, but should be OK for the sorts of
164 * applications that use it. When we need efficient broadcast we can add
167 * @param n_sent Set to the number of messages sent. This should be
168 * equal to the number of processes, but be careful for races.
170 * @retval True for success.
172 bool message_send_all(struct messaging_context *msg_ctx,
174 const void *buf, size_t len,
177 struct msg_all msg_all;
179 msg_all.msg_type = msg_type;
180 if (msg_type < 0x100) {
181 msg_all.msg_flag = FLAG_MSG_GENERAL;
182 } else if (msg_type > 0x100 && msg_type < 0x200) {
183 msg_all.msg_flag = FLAG_MSG_NMBD;
184 } else if (msg_type > 0x200 && msg_type < 0x300) {
185 msg_all.msg_flag = FLAG_MSG_PRINT_GENERAL;
186 } else if (msg_type > 0x300 && msg_type < 0x400) {
187 msg_all.msg_flag = FLAG_MSG_SMBD;
188 } else if (msg_type > 0x400 && msg_type < 0x600) {
189 msg_all.msg_flag = FLAG_MSG_WINBIND;
190 } else if (msg_type > 4000 && msg_type < 5000) {
191 msg_all.msg_flag = FLAG_MSG_DBWRAP;
199 msg_all.msg_ctx = msg_ctx;
201 serverid_traverse(traverse_fn, &msg_all);
203 *n_sent = msg_all.n_sent;
207 static void messaging_recv_cb(const uint8_t *msg, size_t msg_len,
208 int *fds, size_t num_fds,
211 struct messaging_context *msg_ctx = talloc_get_type_abort(
212 private_data, struct messaging_context);
213 const struct messaging_hdr *hdr;
214 struct server_id_buf idbuf;
215 struct messaging_rec rec;
216 int64_t fds64[MIN(num_fds, INT8_MAX)];
219 if (msg_len < sizeof(*hdr)) {
220 for (i=0; i < num_fds; i++) {
223 DEBUG(1, ("message too short: %u\n", (unsigned)msg_len));
227 if (num_fds > INT8_MAX) {
228 for (i=0; i < num_fds; i++) {
231 DEBUG(1, ("too many fds: %u\n", (unsigned)num_fds));
236 * "consume" the fds by copying them and setting
237 * the original variable to -1
239 for (i=0; i < num_fds; i++) {
245 * messages_dgm guarantees alignment, so we can cast here
247 hdr = (const struct messaging_hdr *)msg;
249 DEBUG(10, ("%s: Received message 0x%x len %u (num_fds:%u) from %s\n",
250 __func__, (unsigned)hdr->msg_type,
251 (unsigned)(msg_len - sizeof(*hdr)),
253 server_id_str_buf(hdr->src, &idbuf)));
255 rec = (struct messaging_rec) {
256 .msg_version = MESSAGE_VERSION,
257 .msg_type = hdr->msg_type,
260 .buf.data = discard_const_p(uint8, msg) + sizeof(*hdr),
261 .buf.length = msg_len - sizeof(*hdr),
266 messaging_dispatch_rec(msg_ctx, &rec);
269 static int messaging_context_destructor(struct messaging_context *ctx)
273 messaging_dgm_destroy();
275 for (i=0; i<ctx->num_new_waiters; i++) {
276 if (ctx->new_waiters[i] != NULL) {
277 tevent_req_set_cleanup_fn(ctx->new_waiters[i], NULL);
278 ctx->new_waiters[i] = NULL;
281 for (i=0; i<ctx->num_waiters; i++) {
282 if (ctx->waiters[i] != NULL) {
283 tevent_req_set_cleanup_fn(ctx->waiters[i], NULL);
284 ctx->waiters[i] = NULL;
291 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
292 struct tevent_context *ev)
294 struct messaging_context *ctx;
298 if (!(ctx = talloc_zero(mem_ctx, struct messaging_context))) {
302 ctx->id = procid_self();
307 ret = messaging_dgm_init(ctx->event_ctx, ctx->id,
308 lp_cache_directory(), sec_initial_uid(),
309 messaging_recv_cb, ctx);
312 DEBUG(2, ("messaging_dgm_init failed: %s\n", strerror(ret)));
317 talloc_set_destructor(ctx, messaging_context_destructor);
319 if (lp_clustering()) {
320 status = messaging_ctdbd_init(ctx, ctx, &ctx->remote);
322 if (!NT_STATUS_IS_OK(status)) {
323 DEBUG(2, ("messaging_ctdbd_init failed: %s\n",
329 ctx->id.vnn = get_my_vnn();
331 messaging_register(ctx, NULL, MSG_PING, ping_message);
333 /* Register some debugging related messages */
335 register_msg_pool_usage(ctx);
336 register_dmalloc_msgs(ctx);
337 debug_register_msgs(ctx);
342 struct server_id messaging_server_id(const struct messaging_context *msg_ctx)
348 * re-init after a fork
350 NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
355 messaging_dgm_destroy();
357 msg_ctx->id = procid_self();
359 ret = messaging_dgm_init(msg_ctx->event_ctx, msg_ctx->id,
360 lp_cache_directory(), sec_initial_uid(),
361 messaging_recv_cb, msg_ctx);
363 DEBUG(0, ("messaging_dgm_init failed: %s\n", strerror(errno)));
364 return map_nt_error_from_unix(ret);
367 TALLOC_FREE(msg_ctx->remote);
369 if (lp_clustering()) {
370 status = messaging_ctdbd_init(msg_ctx, msg_ctx,
373 if (!NT_STATUS_IS_OK(status)) {
374 DEBUG(1, ("messaging_ctdbd_init failed: %s\n",
385 * Register a dispatch function for a particular message type. Allow multiple
388 NTSTATUS messaging_register(struct messaging_context *msg_ctx,
391 void (*fn)(struct messaging_context *msg,
394 struct server_id server_id,
397 struct messaging_callback *cb;
399 DEBUG(5, ("Registering messaging pointer for type %u - "
401 (unsigned)msg_type, private_data));
404 * Only one callback per type
407 for (cb = msg_ctx->callbacks; cb != NULL; cb = cb->next) {
408 /* we allow a second registration of the same message
409 type if it has a different private pointer. This is
410 needed in, for example, the internal notify code,
411 which creates a new notify context for each tree
412 connect, and expects to receive messages to each of
414 if (cb->msg_type == msg_type && private_data == cb->private_data) {
415 DEBUG(5,("Overriding messaging pointer for type %u - private_data=%p\n",
416 (unsigned)msg_type, private_data));
418 cb->private_data = private_data;
423 if (!(cb = talloc(msg_ctx, struct messaging_callback))) {
424 return NT_STATUS_NO_MEMORY;
427 cb->msg_type = msg_type;
429 cb->private_data = private_data;
431 DLIST_ADD(msg_ctx->callbacks, cb);
436 De-register the function for a particular message type.
438 void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
441 struct messaging_callback *cb, *next;
443 for (cb = ctx->callbacks; cb; cb = next) {
445 if ((cb->msg_type == msg_type)
446 && (cb->private_data == private_data)) {
447 DEBUG(5,("Deregistering messaging pointer for type %u - private_data=%p\n",
448 (unsigned)msg_type, private_data));
449 DLIST_REMOVE(ctx->callbacks, cb);
456 Send a message to a particular server
458 NTSTATUS messaging_send(struct messaging_context *msg_ctx,
459 struct server_id server, uint32_t msg_type,
460 const DATA_BLOB *data)
464 iov.iov_base = data->data;
465 iov.iov_len = data->length;
467 return messaging_send_iov(msg_ctx, server, msg_type, &iov, 1, NULL, 0);
470 NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
471 struct server_id server, uint32_t msg_type,
472 const uint8_t *buf, size_t len)
474 DATA_BLOB blob = data_blob_const(buf, len);
475 return messaging_send(msg_ctx, server, msg_type, &blob);
478 NTSTATUS messaging_send_iov(struct messaging_context *msg_ctx,
479 struct server_id server, uint32_t msg_type,
480 const struct iovec *iov, int iovlen,
481 const int *fds, size_t num_fds)
484 struct messaging_hdr hdr;
485 struct iovec iov2[iovlen+1];
487 if (server_id_is_disconnected(&server)) {
488 return NT_STATUS_INVALID_PARAMETER_MIX;
491 if (num_fds > INT8_MAX) {
492 return NT_STATUS_INVALID_PARAMETER_MIX;
495 if (!procid_is_local(&server)) {
497 return NT_STATUS_NOT_SUPPORTED;
500 ret = msg_ctx->remote->send_fn(msg_ctx->id, server,
501 msg_type, iov, iovlen,
505 return map_nt_error_from_unix(ret);
511 hdr = (struct messaging_hdr) {
512 .msg_type = msg_type,
516 iov2[0] = (struct iovec){ .iov_base = &hdr, .iov_len = sizeof(hdr) };
517 memcpy(&iov2[1], iov, iovlen * sizeof(*iov));
520 ret = messaging_dgm_send(server.pid, iov2, iovlen+1, fds, num_fds);
524 return map_nt_error_from_unix(ret);
529 static struct messaging_rec *messaging_rec_dup(TALLOC_CTX *mem_ctx,
530 struct messaging_rec *rec)
532 struct messaging_rec *result;
533 size_t fds_size = sizeof(int64_t) * rec->num_fds;
535 result = talloc_pooled_object(mem_ctx, struct messaging_rec, 2,
536 rec->buf.length + fds_size);
537 if (result == NULL) {
542 /* Doesn't fail, see talloc_pooled_object */
544 result->buf.data = talloc_memdup(result, rec->buf.data,
548 if (result->num_fds > 0) {
549 result->fds = talloc_array(result, int64_t, result->num_fds);
550 memcpy(result->fds, rec->fds, fds_size);
556 struct messaging_filtered_read_state {
557 struct tevent_context *ev;
558 struct messaging_context *msg_ctx;
561 bool (*filter)(struct messaging_rec *rec, void *private_data);
564 struct messaging_rec *rec;
567 static void messaging_filtered_read_cleanup(struct tevent_req *req,
568 enum tevent_req_state req_state);
570 struct tevent_req *messaging_filtered_read_send(
571 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
572 struct messaging_context *msg_ctx,
573 bool (*filter)(struct messaging_rec *rec, void *private_data),
576 struct tevent_req *req;
577 struct messaging_filtered_read_state *state;
578 size_t new_waiters_len;
580 req = tevent_req_create(mem_ctx, &state,
581 struct messaging_filtered_read_state);
586 state->msg_ctx = msg_ctx;
587 state->filter = filter;
588 state->private_data = private_data;
591 * We have to defer the callback here, as we might be called from
592 * within a different tevent_context than state->ev
594 tevent_req_defer_callback(req, state->ev);
596 state->tevent_handle = messaging_dgm_register_tevent_context(
598 if (tevent_req_nomem(state, req)) {
599 return tevent_req_post(req, ev);
603 * We add ourselves to the "new_waiters" array, not the "waiters"
604 * array. If we are called from within messaging_read_done,
605 * messaging_dispatch_rec will be in an active for-loop on
606 * "waiters". We must be careful not to mess with this array, because
607 * it could mean that a single event is being delivered twice.
610 new_waiters_len = talloc_array_length(msg_ctx->new_waiters);
612 if (new_waiters_len == msg_ctx->num_new_waiters) {
613 struct tevent_req **tmp;
615 tmp = talloc_realloc(msg_ctx, msg_ctx->new_waiters,
616 struct tevent_req *, new_waiters_len+1);
617 if (tevent_req_nomem(tmp, req)) {
618 return tevent_req_post(req, ev);
620 msg_ctx->new_waiters = tmp;
623 msg_ctx->new_waiters[msg_ctx->num_new_waiters] = req;
624 msg_ctx->num_new_waiters += 1;
625 tevent_req_set_cleanup_fn(req, messaging_filtered_read_cleanup);
630 static void messaging_filtered_read_cleanup(struct tevent_req *req,
631 enum tevent_req_state req_state)
633 struct messaging_filtered_read_state *state = tevent_req_data(
634 req, struct messaging_filtered_read_state);
635 struct messaging_context *msg_ctx = state->msg_ctx;
638 tevent_req_set_cleanup_fn(req, NULL);
640 TALLOC_FREE(state->tevent_handle);
643 * Just set the [new_]waiters entry to NULL, be careful not to mess
644 * with the other "waiters" array contents. We are often called from
645 * within "messaging_dispatch_rec", which loops over
646 * "waiters". Messing with the "waiters" array will mess up that
650 for (i=0; i<msg_ctx->num_waiters; i++) {
651 if (msg_ctx->waiters[i] == req) {
652 msg_ctx->waiters[i] = NULL;
657 for (i=0; i<msg_ctx->num_new_waiters; i++) {
658 if (msg_ctx->new_waiters[i] == req) {
659 msg_ctx->new_waiters[i] = NULL;
665 static void messaging_filtered_read_done(struct tevent_req *req,
666 struct messaging_rec *rec)
668 struct messaging_filtered_read_state *state = tevent_req_data(
669 req, struct messaging_filtered_read_state);
671 state->rec = messaging_rec_dup(state, rec);
672 if (tevent_req_nomem(state->rec, req)) {
675 tevent_req_done(req);
678 int messaging_filtered_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
679 struct messaging_rec **presult)
681 struct messaging_filtered_read_state *state = tevent_req_data(
682 req, struct messaging_filtered_read_state);
685 if (tevent_req_is_unix_error(req, &err)) {
686 tevent_req_received(req);
689 *presult = talloc_move(mem_ctx, &state->rec);
693 struct messaging_read_state {
695 struct messaging_rec *rec;
698 static bool messaging_read_filter(struct messaging_rec *rec,
700 static void messaging_read_done(struct tevent_req *subreq);
702 struct tevent_req *messaging_read_send(TALLOC_CTX *mem_ctx,
703 struct tevent_context *ev,
704 struct messaging_context *msg,
707 struct tevent_req *req, *subreq;
708 struct messaging_read_state *state;
710 req = tevent_req_create(mem_ctx, &state,
711 struct messaging_read_state);
715 state->msg_type = msg_type;
717 subreq = messaging_filtered_read_send(state, ev, msg,
718 messaging_read_filter, state);
719 if (tevent_req_nomem(subreq, req)) {
720 return tevent_req_post(req, ev);
722 tevent_req_set_callback(subreq, messaging_read_done, req);
726 static bool messaging_read_filter(struct messaging_rec *rec,
729 struct messaging_read_state *state = talloc_get_type_abort(
730 private_data, struct messaging_read_state);
732 if (rec->num_fds != 0) {
736 return rec->msg_type == state->msg_type;
739 static void messaging_read_done(struct tevent_req *subreq)
741 struct tevent_req *req = tevent_req_callback_data(
742 subreq, struct tevent_req);
743 struct messaging_read_state *state = tevent_req_data(
744 req, struct messaging_read_state);
747 ret = messaging_filtered_read_recv(subreq, state, &state->rec);
749 if (tevent_req_error(req, ret)) {
752 tevent_req_done(req);
755 int messaging_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
756 struct messaging_rec **presult)
758 struct messaging_read_state *state = tevent_req_data(
759 req, struct messaging_read_state);
762 if (tevent_req_is_unix_error(req, &err)) {
765 if (presult != NULL) {
766 *presult = talloc_move(mem_ctx, &state->rec);
771 static bool messaging_append_new_waiters(struct messaging_context *msg_ctx)
773 if (msg_ctx->num_new_waiters == 0) {
777 if (talloc_array_length(msg_ctx->waiters) <
778 (msg_ctx->num_waiters + msg_ctx->num_new_waiters)) {
779 struct tevent_req **tmp;
780 tmp = talloc_realloc(
781 msg_ctx, msg_ctx->waiters, struct tevent_req *,
782 msg_ctx->num_waiters + msg_ctx->num_new_waiters);
784 DEBUG(1, ("%s: talloc failed\n", __func__));
787 msg_ctx->waiters = tmp;
790 memcpy(&msg_ctx->waiters[msg_ctx->num_waiters], msg_ctx->new_waiters,
791 sizeof(struct tevent_req *) * msg_ctx->num_new_waiters);
793 msg_ctx->num_waiters += msg_ctx->num_new_waiters;
794 msg_ctx->num_new_waiters = 0;
800 Dispatch one messaging_rec
802 void messaging_dispatch_rec(struct messaging_context *msg_ctx,
803 struct messaging_rec *rec)
805 struct messaging_callback *cb, *next;
809 for (cb = msg_ctx->callbacks; cb != NULL; cb = next) {
811 if (cb->msg_type != rec->msg_type) {
816 * the old style callbacks don't support fd passing
818 for (j=0; j < rec->num_fds; j++) {
819 int fd = rec->fds[j];
825 cb->fn(msg_ctx, cb->private_data, rec->msg_type,
826 rec->src, &rec->buf);
829 * we continue looking for matching messages after finding
830 * one. This matters for subsystems like the internal notify
831 * code which register more than one handler for the same
836 if (!messaging_append_new_waiters(msg_ctx)) {
837 for (j=0; j < rec->num_fds; j++) {
838 int fd = rec->fds[j];
847 while (i < msg_ctx->num_waiters) {
848 struct tevent_req *req;
849 struct messaging_filtered_read_state *state;
851 req = msg_ctx->waiters[i];
854 * This got cleaned up. In the meantime,
855 * move everything down one. We need
856 * to keep the order of waiters, as
857 * other code may depend on this.
859 if (i < msg_ctx->num_waiters - 1) {
860 memmove(&msg_ctx->waiters[i],
861 &msg_ctx->waiters[i+1],
862 sizeof(struct tevent_req *) *
863 (msg_ctx->num_waiters - i - 1));
865 msg_ctx->num_waiters -= 1;
869 state = tevent_req_data(
870 req, struct messaging_filtered_read_state);
871 if (state->filter(rec, state->private_data)) {
872 messaging_filtered_read_done(req, rec);
875 * Only the first one gets the fd-array
885 * If the fd-array isn't used, just close it.
887 for (j=0; j < rec->num_fds; j++) {
888 int fd = rec->fds[j];
895 static int mess_parent_dgm_cleanup(void *private_data);
896 static void mess_parent_dgm_cleanup_done(struct tevent_req *req);
898 bool messaging_parent_dgm_cleanup_init(struct messaging_context *msg)
900 struct tevent_req *req;
902 req = background_job_send(
903 msg, msg->event_ctx, msg, NULL, 0,
904 lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
906 mess_parent_dgm_cleanup, msg);
910 tevent_req_set_callback(req, mess_parent_dgm_cleanup_done, msg);
914 static int mess_parent_dgm_cleanup(void *private_data)
918 ret = messaging_dgm_wipe();
919 DEBUG(10, ("messaging_dgm_wipe returned %s\n",
920 ret ? strerror(ret) : "ok"));
921 return lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
925 static void mess_parent_dgm_cleanup_done(struct tevent_req *req)
927 struct messaging_context *msg = tevent_req_callback_data(
928 req, struct messaging_context);
931 status = background_job_recv(req);
933 DEBUG(1, ("messaging dgm cleanup job ended with %s\n",
936 req = background_job_send(
937 msg, msg->event_ctx, msg, NULL, 0,
938 lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
940 mess_parent_dgm_cleanup, msg);
942 DEBUG(1, ("background_job_send failed\n"));
944 tevent_req_set_callback(req, mess_parent_dgm_cleanup_done, msg);
947 int messaging_cleanup(struct messaging_context *msg_ctx, pid_t pid)
952 ret = messaging_dgm_wipe();
954 ret = messaging_dgm_cleanup(pid);
960 struct tevent_context *messaging_tevent_context(
961 struct messaging_context *msg_ctx)
963 return msg_ctx->event_ctx;