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
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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
50 /* the locking database handle */
51 static TDB_CONTEXT *tdb;
52 static int received_signal;
54 /* change the message version with any incompatible changes in the protocol */
55 #define MESSAGE_VERSION 1
60 struct process_id dest;
61 struct process_id src;
65 /* we have a linked list of dispatch handlers */
66 static struct dispatch_fns {
67 struct dispatch_fns *next, *prev;
69 void (*fn)(int msg_type, struct process_id pid, void *buf, size_t len,
74 /****************************************************************************
76 ****************************************************************************/
78 void gfree_messages(void)
80 struct dispatch_fns *dfn, *next;
82 /* delete the dispatch_fns list */
86 DLIST_REMOVE(dispatch_fns, dfn);
92 /****************************************************************************
93 Notifications come in as signals.
94 ****************************************************************************/
96 static void sig_usr1(void)
99 sys_select_signal(SIGUSR1);
102 /****************************************************************************
103 A useful function for testing the message system.
104 ****************************************************************************/
106 static void ping_message(int msg_type, struct process_id src,
107 void *buf, size_t len, void *private_data)
109 const char *msg = buf ? (const char *)buf : "none";
111 DEBUG(1,("INFO: Received PING message from PID %s [%s]\n",
112 procid_str_static(&src), msg));
113 message_send_pid(src, MSG_PONG, buf, len, True);
116 /****************************************************************************
117 Initialise the messaging functions.
118 ****************************************************************************/
120 BOOL message_init(void)
127 tdb = tdb_open_log(lock_path("messages.tdb"),
128 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
129 O_RDWR|O_CREAT,0600);
132 DEBUG(0,("ERROR: Failed to initialise messages database\n"));
136 CatchSignal(SIGUSR1, SIGNAL_CAST sig_usr1);
138 message_register(MSG_PING, ping_message, NULL);
140 /* Register some debugging related messages */
142 register_msg_pool_usage();
143 register_dmalloc_msgs();
148 /*******************************************************************
149 Form a static tdb key from a pid.
150 ******************************************************************/
152 static TDB_DATA message_key_pid(struct process_id pid)
157 slprintf(key, sizeof(key)-1, "PID/%s", procid_str_static(&pid));
159 kbuf.dptr = (char *)key;
160 kbuf.dsize = strlen(key)+1;
164 /****************************************************************************
165 Notify a process that it has a message. If the process doesn't exist
166 then delete its record in the database.
167 ****************************************************************************/
169 static BOOL message_notify(struct process_id procid)
171 pid_t pid = procid.pid;
173 uid_t euid = geteuid();
176 * Doing kill with a non-positive pid causes messages to be
177 * sent to places we don't want.
183 become_root_uid_only();
186 ret = kill(pid, SIGUSR1);
189 unbecome_root_uid_only();
193 if (errno == ESRCH) {
194 DEBUG(2,("pid %d doesn't exist - deleting messages record\n", (int)pid));
195 tdb_delete(tdb, message_key_pid(procid));
197 DEBUG(2,("message to process %d failed - %s\n", (int)pid, strerror(errno)));
205 /****************************************************************************
206 Send a message to a particular pid.
207 ****************************************************************************/
209 static BOOL message_send_pid_internal(struct process_id pid, int msg_type,
210 const void *buf, size_t len,
211 BOOL duplicates_allowed,
212 unsigned int timeout)
217 struct message_rec rec;
219 struct message_rec prec;
221 /* NULL pointer means implicit length zero. */
223 SMB_ASSERT(len == 0);
227 * Doing kill with a non-positive pid causes messages to be
228 * sent to places we don't want.
231 SMB_ASSERT(procid_to_pid(&pid) > 0);
233 rec.msg_version = MESSAGE_VERSION;
234 rec.msg_type = msg_type;
236 rec.src = procid_self();
237 rec.len = buf ? len : 0;
239 kbuf = message_key_pid(pid);
241 dbuf.dptr = (char *)SMB_MALLOC(len + sizeof(rec));
245 memcpy(dbuf.dptr, &rec, sizeof(rec));
247 memcpy((void *)((char*)dbuf.dptr+sizeof(rec)), buf, len);
249 dbuf.dsize = len + sizeof(rec);
251 if (duplicates_allowed) {
253 /* If duplicates are allowed we can just append the message and return. */
255 /* lock the record for the destination */
257 if (tdb_chainlock_with_timeout(tdb, kbuf, timeout) == -1) {
258 DEBUG(0,("message_send_pid_internal: failed to get chainlock with timeout %ul.\n", timeout));
262 if (tdb_chainlock(tdb, kbuf) == -1) {
263 DEBUG(0,("message_send_pid_internal: failed to get chainlock.\n"));
267 tdb_append(tdb, kbuf, dbuf);
268 tdb_chainunlock(tdb, kbuf);
270 SAFE_FREE(dbuf.dptr);
271 errno = 0; /* paranoia */
272 return message_notify(pid);
275 /* lock the record for the destination */
277 if (tdb_chainlock_with_timeout(tdb, kbuf, timeout) == -1) {
278 DEBUG(0,("message_send_pid_internal: failed to get chainlock with timeout %ul.\n", timeout));
282 if (tdb_chainlock(tdb, kbuf) == -1) {
283 DEBUG(0,("message_send_pid_internal: failed to get chainlock.\n"));
288 old_dbuf = tdb_fetch(tdb, kbuf);
290 if (!old_dbuf.dptr) {
291 /* its a new record */
293 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
294 tdb_chainunlock(tdb, kbuf);
296 SAFE_FREE(dbuf.dptr);
297 errno = 0; /* paranoia */
298 return message_notify(pid);
301 /* Not a new record. Check for duplicates. */
303 for(ptr = (char *)old_dbuf.dptr; ptr < old_dbuf.dptr + old_dbuf.dsize; ) {
305 * First check if the message header matches, then, if it's a non-zero
306 * sized message, check if the data matches. If so it's a duplicate and
307 * we can discard it. JRA.
310 if (!memcmp(ptr, &rec, sizeof(rec))) {
311 if (!len || (len && !memcmp( ptr + sizeof(rec), buf, len))) {
312 tdb_chainunlock(tdb, kbuf);
313 DEBUG(10,("message_send_pid_internal: discarding duplicate message.\n"));
314 SAFE_FREE(dbuf.dptr);
315 SAFE_FREE(old_dbuf.dptr);
319 memcpy(&prec, ptr, sizeof(prec));
320 ptr += sizeof(rec) + prec.len;
323 /* we're adding to an existing entry */
325 tdb_append(tdb, kbuf, dbuf);
326 tdb_chainunlock(tdb, kbuf);
328 SAFE_FREE(old_dbuf.dptr);
329 SAFE_FREE(dbuf.dptr);
331 errno = 0; /* paranoia */
332 return message_notify(pid);
335 /****************************************************************************
336 Send a message to a particular pid - no timeout.
337 ****************************************************************************/
339 BOOL message_send_pid(struct process_id pid, int msg_type, const void *buf, size_t len, BOOL duplicates_allowed)
341 return message_send_pid_internal(pid, msg_type, buf, len, duplicates_allowed, 0);
344 /****************************************************************************
345 Send a message to a particular pid, with timeout in seconds.
346 ****************************************************************************/
348 BOOL message_send_pid_with_timeout(struct process_id pid, int msg_type, const void *buf, size_t len,
349 BOOL duplicates_allowed, unsigned int timeout)
351 return message_send_pid_internal(pid, msg_type, buf, len, duplicates_allowed, timeout);
354 /****************************************************************************
355 Count the messages pending for a particular pid. Expensive....
356 ****************************************************************************/
358 unsigned int messages_pending_for_pid(struct process_id pid)
363 unsigned int message_count = 0;
365 kbuf = message_key_pid(pid);
367 dbuf = tdb_fetch(tdb, kbuf);
368 if (dbuf.dptr == NULL || dbuf.dsize == 0) {
369 SAFE_FREE(dbuf.dptr);
373 for (buf = dbuf.dptr; dbuf.dsize > sizeof(struct message_rec);) {
374 struct message_rec rec;
375 memcpy(&rec, buf, sizeof(rec));
376 buf += (sizeof(rec) + rec.len);
377 dbuf.dsize -= (sizeof(rec) + rec.len);
381 SAFE_FREE(dbuf.dptr);
382 return message_count;
385 /****************************************************************************
386 Retrieve all messages for the current process.
387 ****************************************************************************/
389 static BOOL retrieve_all_messages(char **msgs_buf, size_t *total_len)
395 ZERO_STRUCT(null_dbuf);
400 kbuf = message_key_pid(pid_to_procid(sys_getpid()));
402 if (tdb_chainlock(tdb, kbuf) == -1)
405 dbuf = tdb_fetch(tdb, kbuf);
407 * Replace with an empty record to keep the allocated
410 tdb_store(tdb, kbuf, null_dbuf, TDB_REPLACE);
411 tdb_chainunlock(tdb, kbuf);
413 if (dbuf.dptr == NULL || dbuf.dsize == 0) {
414 SAFE_FREE(dbuf.dptr);
418 *msgs_buf = dbuf.dptr;
419 *total_len = dbuf.dsize;
424 /****************************************************************************
425 Parse out the next message for the current process.
426 ****************************************************************************/
428 static BOOL message_recv(char *msgs_buf, size_t total_len, int *msg_type,
429 struct process_id *src, char **buf, size_t *len)
431 struct message_rec rec;
432 char *ret_buf = *buf;
437 if (total_len - (ret_buf - msgs_buf) < sizeof(rec))
440 memcpy(&rec, ret_buf, sizeof(rec));
441 ret_buf += sizeof(rec);
443 if (rec.msg_version != MESSAGE_VERSION) {
444 DEBUG(0,("message version %d received (expected %d)\n", rec.msg_version, MESSAGE_VERSION));
449 if (total_len - (ret_buf - msgs_buf) < rec.len)
454 *msg_type = rec.msg_type;
461 /****************************************************************************
462 Receive and dispatch any messages pending for this process.
463 JRA changed Dec 13 2006. Only one message handler now permitted per type.
464 *NOTE*: Dispatch functions must be able to cope with incoming
465 messages on an *odd* byte boundary.
466 ****************************************************************************/
468 void message_dispatch(void)
471 struct process_id src;
474 size_t len, total_len;
477 if (!received_signal)
480 DEBUG(10,("message_dispatch: received_signal = %d\n", received_signal));
484 if (!retrieve_all_messages(&msgs_buf, &total_len))
487 for (buf = msgs_buf; message_recv(msgs_buf, total_len, &msg_type, &src, &buf, &len); buf += len) {
488 struct dispatch_fns *dfn;
490 DEBUG(10,("message_dispatch: received msg_type=%d "
491 "src_pid=%u\n", msg_type,
492 (unsigned int) procid_to_pid(&src)));
495 for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
496 if (dfn->msg_type == msg_type) {
497 DEBUG(10,("message_dispatch: processing message of type %d.\n", msg_type));
498 dfn->fn(msg_type, src,
499 len ? (void *)buf : NULL, len,
506 DEBUG(5,("message_dispatch: warning: no handler registed for "
507 "msg_type %d in pid %u\n",
508 msg_type, (unsigned int)sys_getpid()));
514 /****************************************************************************
515 Register/replace a dispatch function for a particular message type.
516 JRA changed Dec 13 2006. Only one message handler now permitted per type.
517 *NOTE*: Dispatch functions must be able to cope with incoming
518 messages on an *odd* byte boundary.
519 ****************************************************************************/
521 void message_register(int msg_type,
522 void (*fn)(int msg_type, struct process_id pid,
523 void *buf, size_t len,
527 struct dispatch_fns *dfn;
529 for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
530 if (dfn->msg_type == msg_type) {
536 dfn = SMB_MALLOC_P(struct dispatch_fns);
542 dfn->msg_type = msg_type;
544 dfn->private_data = private_data;
546 DLIST_ADD(dispatch_fns, dfn);
550 DEBUG(0,("message_register: Not enough memory. malloc failed!\n"));
554 /****************************************************************************
555 De-register the function for a particular message type.
556 ****************************************************************************/
558 void message_deregister(int msg_type)
560 struct dispatch_fns *dfn, *next;
562 for (dfn = dispatch_fns; dfn; dfn = next) {
564 if (dfn->msg_type == msg_type) {
565 DLIST_REMOVE(dispatch_fns, dfn);
581 /****************************************************************************
582 Send one of the messages for the broadcast.
583 ****************************************************************************/
585 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
587 struct connections_data crec;
588 struct msg_all *msg_all = (struct msg_all *)state;
590 if (dbuf.dsize != sizeof(crec))
593 memcpy(&crec, dbuf.dptr, sizeof(crec));
598 /* Don't send if the receiver hasn't registered an interest. */
600 if(!(crec.bcast_msg_flags & msg_all->msg_flag))
603 /* If the msg send fails because the pid was not found (i.e. smbd died),
604 * the msg has already been deleted from the messages.tdb.*/
606 if (!message_send_pid(crec.pid, msg_all->msg_type,
607 msg_all->buf, msg_all->len,
608 msg_all->duplicates)) {
610 /* If the pid was not found delete the entry from connections.tdb */
612 if (errno == ESRCH) {
613 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
614 procid_str_static(&crec.pid),
615 crec.cnum, crec.name));
616 tdb_delete(the_tdb, kbuf);
624 * Send a message to all smbd processes.
626 * It isn't very efficient, but should be OK for the sorts of
627 * applications that use it. When we need efficient broadcast we can add
630 * @param n_sent Set to the number of messages sent. This should be
631 * equal to the number of processes, but be careful for races.
633 * @retval True for success.
635 BOOL message_send_all(TDB_CONTEXT *conn_tdb, int msg_type,
636 const void *buf, size_t len,
637 BOOL duplicates_allowed,
640 struct msg_all msg_all;
642 msg_all.msg_type = msg_type;
644 msg_all.msg_flag = FLAG_MSG_GENERAL;
645 else if (msg_type > 1000 && msg_type < 2000)
646 msg_all.msg_flag = FLAG_MSG_NMBD;
647 else if (msg_type > 2000 && msg_type < 2100)
648 msg_all.msg_flag = FLAG_MSG_PRINT_NOTIFY;
649 else if (msg_type > 2100 && msg_type < 3000)
650 msg_all.msg_flag = FLAG_MSG_PRINT_GENERAL;
651 else if (msg_type > 3000 && msg_type < 4000)
652 msg_all.msg_flag = FLAG_MSG_SMBD;
658 msg_all.duplicates = duplicates_allowed;
661 tdb_traverse(conn_tdb, traverse_fn, &msg_all);
663 *n_sent = msg_all.n_sent;
668 * Block and unblock receiving of messages. Allows removal of race conditions
669 * when doing a fork and changing message disposition.
672 void message_block(void)
674 BlockSignals(True, SIGUSR1);
677 void message_unblock(void)
679 BlockSignals(False, SIGUSR1);
683 * Samba4 API wrapper around the Samba3 implementation. Yes, I know, we could
684 * import the whole Samba4 thing, but I want notify.c from Samba4 in first.
687 struct messaging_callback {
688 struct messaging_callback *prev, *next;
690 void (*fn)(struct messaging_context *msg, void *private_data,
692 struct server_id server_id, DATA_BLOB *data);
696 struct messaging_context {
698 struct messaging_callback *callbacks;
701 static int messaging_context_destructor(struct messaging_context *ctx)
703 struct messaging_callback *cb;
705 for (cb = ctx->callbacks; cb; cb = cb->next) {
707 * We unconditionally remove all instances of our callback
708 * from the tdb basis.
710 message_deregister(cb->msg_type);
715 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
716 struct server_id server_id,
717 struct event_context *ev)
719 struct messaging_context *ctx;
721 if (!(ctx = TALLOC_ZERO_P(mem_ctx, struct messaging_context))) {
726 talloc_set_destructor(ctx, messaging_context_destructor);
730 static void messaging_callback(int msg_type, struct process_id pid,
731 void *buf, size_t len, void *private_data)
733 struct messaging_context *ctx = talloc_get_type_abort(
734 private_data, struct messaging_context);
735 struct messaging_callback *cb, *next;
737 for (cb = ctx->callbacks; cb; cb = next) {
739 * Allow a callback to remove itself
743 if (msg_type == cb->msg_type) {
747 blob.data = (uint8 *)buf;
751 cb->fn(ctx, cb->private_data, msg_type, id, &blob);
757 * Register a dispatch function for a particular message type. Allow multiple
760 NTSTATUS messaging_register(struct messaging_context *ctx, void *private_data,
762 void (*fn)(struct messaging_context *msg,
765 struct server_id server_id,
768 struct messaging_callback *cb;
770 if (!(cb = talloc(ctx, struct messaging_callback))) {
771 return NT_STATUS_NO_MEMORY;
774 cb->msg_type = msg_type;
776 cb->private_data = private_data;
778 DLIST_ADD(ctx->callbacks, cb);
779 message_register(msg_type, messaging_callback, ctx);
784 De-register the function for a particular message type.
786 void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
789 struct messaging_callback *cb, *next;
791 for (cb = ctx->callbacks; cb; cb = next) {
793 if ((cb->msg_type == msg_type)
794 && (cb->private_data == private_data)) {
795 DLIST_REMOVE(ctx->callbacks, cb);