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 "librpc/gen_ndr/messaging.h"
50 #include "librpc/gen_ndr/ndr_messaging.h"
52 struct messaging_callback {
53 struct messaging_callback *prev, *next;
55 void (*fn)(struct messaging_context *msg, void *private_data,
57 struct server_id server_id, DATA_BLOB *data);
61 /****************************************************************************
62 A useful function for testing the message system.
63 ****************************************************************************/
65 static void ping_message(struct messaging_context *msg_ctx,
71 const char *msg = data->data ? (const char *)data->data : "none";
73 DEBUG(1,("INFO: Received PING message from PID %s [%s]\n",
74 procid_str_static(&src), msg));
75 messaging_send(msg_ctx, src, MSG_PONG, data);
78 /****************************************************************************
79 Register/replace a dispatch function for a particular message type.
80 JRA changed Dec 13 2006. Only one message handler now permitted per type.
81 *NOTE*: Dispatch functions must be able to cope with incoming
82 messages on an *odd* byte boundary.
83 ****************************************************************************/
86 struct messaging_context *msg_ctx;
94 /****************************************************************************
95 Send one of the messages for the broadcast.
96 ****************************************************************************/
98 static int traverse_fn(struct db_record *rec,
99 const struct connections_key *ckey,
100 const struct connections_data *crec,
103 struct msg_all *msg_all = (struct msg_all *)state;
106 if (crec->cnum != -1)
109 /* Don't send if the receiver hasn't registered an interest. */
111 if(!(crec->bcast_msg_flags & msg_all->msg_flag))
114 /* If the msg send fails because the pid was not found (i.e. smbd died),
115 * the msg has already been deleted from the messages.tdb.*/
117 status = messaging_send_buf(msg_all->msg_ctx,
118 crec->pid, msg_all->msg_type,
119 (uint8 *)msg_all->buf, msg_all->len);
121 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
123 /* If the pid was not found delete the entry from connections.tdb */
125 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
126 procid_str_static(&crec->pid), crec->cnum,
129 rec->delete_rec(rec);
136 * Send a message to all smbd processes.
138 * It isn't very efficient, but should be OK for the sorts of
139 * applications that use it. When we need efficient broadcast we can add
142 * @param n_sent Set to the number of messages sent. This should be
143 * equal to the number of processes, but be careful for races.
145 * @retval True for success.
147 BOOL message_send_all(struct messaging_context *msg_ctx,
149 const void *buf, size_t len,
152 struct msg_all msg_all;
154 msg_all.msg_type = msg_type;
156 msg_all.msg_flag = FLAG_MSG_GENERAL;
157 else if (msg_type > 1000 && msg_type < 2000)
158 msg_all.msg_flag = FLAG_MSG_NMBD;
159 else if (msg_type > 2000 && msg_type < 2100)
160 msg_all.msg_flag = FLAG_MSG_PRINT_NOTIFY;
161 else if (msg_type > 2100 && msg_type < 3000)
162 msg_all.msg_flag = FLAG_MSG_PRINT_GENERAL;
163 else if (msg_type > 3000 && msg_type < 4000)
164 msg_all.msg_flag = FLAG_MSG_SMBD;
171 msg_all.msg_ctx = msg_ctx;
173 connections_forall(traverse_fn, &msg_all);
175 *n_sent = msg_all.n_sent;
179 struct event_context *messaging_event_context(struct messaging_context *msg_ctx)
181 return msg_ctx->event_ctx;
184 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
185 struct server_id server_id,
186 struct event_context *ev)
188 struct messaging_context *ctx;
191 if (!(ctx = TALLOC_ZERO_P(mem_ctx, struct messaging_context))) {
198 status = messaging_tdb_init(ctx, ctx, &ctx->local);
200 if (!NT_STATUS_IS_OK(status)) {
201 DEBUG(0, ("messaging_tdb_init failed: %s\n",
207 #ifdef CLUSTER_SUPPORT
208 if (lp_clustering()) {
209 status = messaging_ctdbd_init(ctx, ctx, &ctx->remote);
211 if (!NT_STATUS_IS_OK(status)) {
212 DEBUG(0, ("messaging_ctdb_init failed: %s\n",
220 messaging_register(ctx, NULL, MSG_PING, ping_message);
222 /* Register some debugging related messages */
224 register_msg_pool_usage(ctx);
225 register_dmalloc_msgs(ctx);
226 debug_register_msgs(ctx);
232 * re-init after a fork
234 NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
236 #ifdef CLUSTER_SUPPORT
238 TALLOC_FREE(msg_ctx->remote);
240 if (lp_clustering()) {
243 status = messaging_ctdbd_init(msg_ctx, msg_ctx,
246 if (!NT_STATUS_IS_OK(status)) {
247 DEBUG(0, ("messaging_ctdb_init failed: %s\n",
260 * Register a dispatch function for a particular message type. Allow multiple
263 NTSTATUS messaging_register(struct messaging_context *msg_ctx,
266 void (*fn)(struct messaging_context *msg,
269 struct server_id server_id,
272 struct messaging_callback *cb;
275 * Only one callback per type
278 for (cb = msg_ctx->callbacks; cb != NULL; cb = cb->next) {
279 if (cb->msg_type == msg_type) {
281 cb->private_data = private_data;
286 if (!(cb = talloc(msg_ctx, struct messaging_callback))) {
287 return NT_STATUS_NO_MEMORY;
290 cb->msg_type = msg_type;
292 cb->private_data = private_data;
294 DLIST_ADD(msg_ctx->callbacks, cb);
299 De-register the function for a particular message type.
301 void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
304 struct messaging_callback *cb, *next;
306 for (cb = ctx->callbacks; cb; cb = next) {
308 if ((cb->msg_type == msg_type)
309 && (cb->private_data == private_data)) {
310 DLIST_REMOVE(ctx->callbacks, cb);
317 Send a message to a particular server
319 NTSTATUS messaging_send(struct messaging_context *msg_ctx,
320 struct server_id server, uint32_t msg_type,
321 const DATA_BLOB *data)
323 return msg_ctx->local->send_fn(msg_ctx, server, msg_type, data,
327 NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
328 struct server_id server, uint32_t msg_type,
329 const uint8 *buf, size_t len)
331 DATA_BLOB blob = data_blob_const(buf, len);
332 return messaging_send(msg_ctx, server, msg_type, &blob);
336 Dispatch one messsaging_rec
338 void messaging_dispatch_rec(struct messaging_context *msg_ctx,
339 struct messaging_rec *rec)
341 struct messaging_callback *cb, *next;
343 for (cb = msg_ctx->callbacks; cb != NULL; cb = next) {
345 if (cb->msg_type == rec->msg_type) {
346 cb->fn(msg_ctx, cb->private_data, rec->msg_type,
347 rec->src, &rec->buf);