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 server_id dest;
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 server_id pid, void *buf, size_t len,
74 static void message_register(int msg_type,
75 void (*fn)(int msg_type, struct server_id pid,
76 void *buf, size_t len,
80 /****************************************************************************
82 ****************************************************************************/
84 void gfree_messages(void)
86 struct dispatch_fns *dfn, *next;
88 /* delete the dispatch_fns list */
92 DLIST_REMOVE(dispatch_fns, dfn);
98 /****************************************************************************
99 Notifications come in as signals.
100 ****************************************************************************/
102 static void sig_usr1(void)
105 sys_select_signal(SIGUSR1);
108 static NTSTATUS message_send_pid(struct server_id pid, int msg_type,
109 const void *buf, size_t len,
110 BOOL duplicates_allowed);
112 /****************************************************************************
113 A useful function for testing the message system.
114 ****************************************************************************/
116 static void ping_message(int msg_type, struct server_id src,
117 void *buf, size_t len, void *private_data)
119 const char *msg = buf ? (const char *)buf : "none";
121 DEBUG(1,("INFO: Received PING message from PID %s [%s]\n",
122 procid_str_static(&src), msg));
123 message_send_pid(src, MSG_PONG, buf, len, True);
126 /****************************************************************************
127 Initialise the messaging functions.
128 ****************************************************************************/
130 static BOOL message_init(struct messaging_context *msg_ctx)
137 tdb = tdb_open_log(lock_path("messages.tdb"),
138 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
139 O_RDWR|O_CREAT,0600);
142 DEBUG(0,("ERROR: Failed to initialise messages database\n"));
146 /* Activate the per-hashchain freelist */
147 tdb_set_max_dead(tdb, 5);
149 CatchSignal(SIGUSR1, SIGNAL_CAST sig_usr1);
151 message_register(MSG_PING, ping_message, NULL);
153 /* Register some debugging related messages */
155 register_msg_pool_usage(msg_ctx);
156 register_dmalloc_msgs(msg_ctx);
157 debug_register_msgs(msg_ctx);
162 /*******************************************************************
163 Form a static tdb key from a pid.
164 ******************************************************************/
166 static TDB_DATA message_key_pid(struct server_id pid)
171 slprintf(key, sizeof(key)-1, "PID/%s", procid_str_static(&pid));
173 kbuf.dptr = (uint8 *)key;
174 kbuf.dsize = strlen(key)+1;
178 /****************************************************************************
179 Notify a process that it has a message. If the process doesn't exist
180 then delete its record in the database.
181 ****************************************************************************/
183 static NTSTATUS message_notify(struct server_id procid)
185 pid_t pid = procid.pid;
187 uid_t euid = geteuid();
190 * Doing kill with a non-positive pid causes messages to be
191 * sent to places we don't want.
197 /* If we're not root become so to send the message. */
199 set_effective_uid(0);
202 ret = kill(pid, SIGUSR1);
205 /* Go back to who we were. */
206 int saved_errno = errno;
207 restore_re_uid_fromroot();
212 if (errno == ESRCH) {
213 DEBUG(2,("pid %d doesn't exist - deleting messages record\n",
215 tdb_delete(tdb, message_key_pid(procid));
218 * INVALID_HANDLE is the closest I can think of -- vl
220 return NT_STATUS_INVALID_HANDLE;
223 DEBUG(2,("message to process %d failed - %s\n", (int)pid,
227 * No call to map_nt_error_from_unix -- don't want to link in
228 * errormap.o into lots of utils.
231 if (errno == EINVAL) return NT_STATUS_INVALID_PARAMETER;
232 if (errno == EPERM) return NT_STATUS_ACCESS_DENIED;
233 return NT_STATUS_UNSUCCESSFUL;
239 /****************************************************************************
240 Send a message to a particular pid.
241 ****************************************************************************/
243 static NTSTATUS message_send_pid_internal(struct server_id pid, int msg_type,
244 const void *buf, size_t len,
245 BOOL duplicates_allowed,
246 unsigned int timeout)
251 struct message_rec rec;
253 struct message_rec prec;
255 /* NULL pointer means implicit length zero. */
257 SMB_ASSERT(len == 0);
261 * Doing kill with a non-positive pid causes messages to be
262 * sent to places we don't want.
265 SMB_ASSERT(procid_to_pid(&pid) > 0);
267 rec.msg_version = MESSAGE_VERSION;
268 rec.msg_type = msg_type;
270 rec.src = procid_self();
271 rec.len = buf ? len : 0;
273 kbuf = message_key_pid(pid);
275 dbuf.dptr = (uint8 *)SMB_MALLOC(len + sizeof(rec));
277 return NT_STATUS_NO_MEMORY;
280 memcpy(dbuf.dptr, &rec, sizeof(rec));
282 memcpy((void *)((char*)dbuf.dptr+sizeof(rec)), buf, len);
284 dbuf.dsize = len + sizeof(rec);
286 if (duplicates_allowed) {
288 /* If duplicates are allowed we can just append the message
291 /* lock the record for the destination */
293 if (tdb_chainlock_with_timeout(tdb, kbuf,
295 DEBUG(0,("message_send_pid_internal: failed "
296 "to get chainlock with timeout "
298 return NT_STATUS_IO_TIMEOUT;
301 if (tdb_chainlock(tdb, kbuf) == -1) {
302 DEBUG(0,("message_send_pid_internal: failed "
303 "to get chainlock.\n"));
304 return NT_STATUS_LOCK_NOT_GRANTED;
307 tdb_append(tdb, kbuf, dbuf);
308 tdb_chainunlock(tdb, kbuf);
310 SAFE_FREE(dbuf.dptr);
311 errno = 0; /* paranoia */
312 return message_notify(pid);
315 /* lock the record for the destination */
317 if (tdb_chainlock_with_timeout(tdb, kbuf, timeout) == -1) {
318 DEBUG(0,("message_send_pid_internal: failed to get "
319 "chainlock with timeout %ul.\n", timeout));
320 return NT_STATUS_IO_TIMEOUT;
323 if (tdb_chainlock(tdb, kbuf) == -1) {
324 DEBUG(0,("message_send_pid_internal: failed to get "
326 return NT_STATUS_LOCK_NOT_GRANTED;
330 old_dbuf = tdb_fetch(tdb, kbuf);
332 if (!old_dbuf.dptr) {
333 /* its a new record */
335 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
336 tdb_chainunlock(tdb, kbuf);
338 SAFE_FREE(dbuf.dptr);
339 errno = 0; /* paranoia */
340 return message_notify(pid);
343 /* Not a new record. Check for duplicates. */
345 for(ptr = old_dbuf.dptr; ptr < old_dbuf.dptr + old_dbuf.dsize; ) {
347 * First check if the message header matches, then, if it's a
348 * non-zero sized message, check if the data matches. If so
349 * it's a duplicate and we can discard it. JRA.
352 if (!memcmp(ptr, &rec, sizeof(rec))) {
355 && !memcmp( ptr + sizeof(rec), buf, len))) {
356 tdb_chainunlock(tdb, kbuf);
357 DEBUG(10,("message_send_pid_internal: "
358 "discarding duplicate message.\n"));
359 SAFE_FREE(dbuf.dptr);
360 SAFE_FREE(old_dbuf.dptr);
364 memcpy(&prec, ptr, sizeof(prec));
365 ptr += sizeof(rec) + prec.len;
368 /* we're adding to an existing entry */
370 tdb_append(tdb, kbuf, dbuf);
371 tdb_chainunlock(tdb, kbuf);
373 SAFE_FREE(old_dbuf.dptr);
374 SAFE_FREE(dbuf.dptr);
376 errno = 0; /* paranoia */
377 return message_notify(pid);
380 /****************************************************************************
381 Send a message to a particular pid - no timeout.
382 ****************************************************************************/
384 static NTSTATUS message_send_pid(struct server_id pid, int msg_type,
385 const void *buf, size_t len,
386 BOOL duplicates_allowed)
388 return message_send_pid_internal(pid, msg_type, buf, len,
389 duplicates_allowed, 0);
392 /****************************************************************************
393 Count the messages pending for a particular pid. Expensive....
394 ****************************************************************************/
396 unsigned int messages_pending_for_pid(struct server_id pid)
401 unsigned int message_count = 0;
403 kbuf = message_key_pid(pid);
405 dbuf = tdb_fetch(tdb, kbuf);
406 if (dbuf.dptr == NULL || dbuf.dsize == 0) {
407 SAFE_FREE(dbuf.dptr);
411 for (buf = dbuf.dptr; dbuf.dsize > sizeof(struct message_rec);) {
412 struct message_rec rec;
413 memcpy(&rec, buf, sizeof(rec));
414 buf += (sizeof(rec) + rec.len);
415 dbuf.dsize -= (sizeof(rec) + rec.len);
419 SAFE_FREE(dbuf.dptr);
420 return message_count;
423 /****************************************************************************
424 Retrieve all messages for the current process.
425 ****************************************************************************/
427 static BOOL retrieve_all_messages(char **msgs_buf, size_t *total_len)
433 ZERO_STRUCT(null_dbuf);
438 kbuf = message_key_pid(pid_to_procid(sys_getpid()));
440 if (tdb_chainlock(tdb, kbuf) == -1)
443 dbuf = tdb_fetch(tdb, kbuf);
445 * Replace with an empty record to keep the allocated
448 tdb_store(tdb, kbuf, null_dbuf, TDB_REPLACE);
449 tdb_chainunlock(tdb, kbuf);
451 if (dbuf.dptr == NULL || dbuf.dsize == 0) {
452 SAFE_FREE(dbuf.dptr);
456 *msgs_buf = (char *)dbuf.dptr;
457 *total_len = dbuf.dsize;
462 /****************************************************************************
463 Parse out the next message for the current process.
464 ****************************************************************************/
466 static BOOL message_recv(char *msgs_buf, size_t total_len, int *msg_type,
467 struct server_id *src, char **buf, size_t *len)
469 struct message_rec rec;
470 char *ret_buf = *buf;
475 if (total_len - (ret_buf - msgs_buf) < sizeof(rec))
478 memcpy(&rec, ret_buf, sizeof(rec));
479 ret_buf += sizeof(rec);
481 if (rec.msg_version != MESSAGE_VERSION) {
482 DEBUG(0,("message version %d received (expected %d)\n",
483 rec.msg_version, MESSAGE_VERSION));
488 if (total_len - (ret_buf - msgs_buf) < rec.len)
493 *msg_type = rec.msg_type;
500 /****************************************************************************
501 Receive and dispatch any messages pending for this process.
502 JRA changed Dec 13 2006. Only one message handler now permitted per type.
503 *NOTE*: Dispatch functions must be able to cope with incoming
504 messages on an *odd* byte boundary.
505 ****************************************************************************/
507 void message_dispatch(void)
510 struct server_id src;
513 size_t len, total_len;
516 if (!received_signal)
519 DEBUG(10, ("message_dispatch: received_signal = %d\n",
524 if (!retrieve_all_messages(&msgs_buf, &total_len))
528 message_recv(msgs_buf, total_len, &msg_type, &src, &buf, &len);
530 struct dispatch_fns *dfn;
532 DEBUG(10,("message_dispatch: received msg_type=%d "
533 "src_pid=%u\n", msg_type,
534 (unsigned int) procid_to_pid(&src)));
537 for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
538 if (dfn->msg_type == msg_type) {
539 DEBUG(10,("message_dispatch: processing "
540 "message of type %d.\n", msg_type));
541 dfn->fn(msg_type, src,
542 len ? (void *)buf : NULL, len,
549 DEBUG(5,("message_dispatch: warning: no handler "
550 "registed for msg_type %d in pid %u\n",
551 msg_type, (unsigned int)sys_getpid()));
557 /****************************************************************************
558 Register/replace a dispatch function for a particular message type.
559 JRA changed Dec 13 2006. Only one message handler now permitted per type.
560 *NOTE*: Dispatch functions must be able to cope with incoming
561 messages on an *odd* byte boundary.
562 ****************************************************************************/
564 static void message_register(int msg_type,
565 void (*fn)(int msg_type, struct server_id pid,
566 void *buf, size_t len,
570 struct dispatch_fns *dfn;
572 for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
573 if (dfn->msg_type == msg_type) {
579 if (!(dfn = SMB_MALLOC_P(struct dispatch_fns))) {
580 DEBUG(0,("message_register: Not enough memory. malloc "
587 dfn->msg_type = msg_type;
589 dfn->private_data = private_data;
591 DLIST_ADD(dispatch_fns, dfn);
594 /****************************************************************************
595 De-register the function for a particular message type.
596 ****************************************************************************/
598 static void message_deregister(int msg_type)
600 struct dispatch_fns *dfn, *next;
602 for (dfn = dispatch_fns; dfn; dfn = next) {
604 if (dfn->msg_type == msg_type) {
605 DLIST_REMOVE(dispatch_fns, dfn);
621 /****************************************************************************
622 Send one of the messages for the broadcast.
623 ****************************************************************************/
625 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
628 struct connections_data crec;
629 struct msg_all *msg_all = (struct msg_all *)state;
632 if (dbuf.dsize != sizeof(crec))
635 memcpy(&crec, dbuf.dptr, sizeof(crec));
640 /* Don't send if the receiver hasn't registered an interest. */
642 if(!(crec.bcast_msg_flags & msg_all->msg_flag))
645 /* If the msg send fails because the pid was not found (i.e. smbd died),
646 * the msg has already been deleted from the messages.tdb.*/
648 status = message_send_pid(crec.pid, msg_all->msg_type,
649 msg_all->buf, msg_all->len,
650 msg_all->duplicates);
652 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
654 /* If the pid was not found delete the entry from
657 DEBUG(2,("pid %s doesn't exist - deleting connections "
658 "%d [%s]\n", procid_str_static(&crec.pid), crec.cnum,
660 tdb_delete(the_tdb, kbuf);
667 * Send a message to all smbd processes.
669 * It isn't very efficient, but should be OK for the sorts of
670 * applications that use it. When we need efficient broadcast we can add
673 * @param n_sent Set to the number of messages sent. This should be
674 * equal to the number of processes, but be careful for races.
676 * @retval True for success.
678 BOOL message_send_all(struct messaging_context *msg_ctx,
680 const void *buf, size_t len,
681 BOOL duplicates_allowed,
684 struct msg_all msg_all;
686 msg_all.msg_type = msg_type;
688 msg_all.msg_flag = FLAG_MSG_GENERAL;
689 else if (msg_type > 1000 && msg_type < 2000)
690 msg_all.msg_flag = FLAG_MSG_NMBD;
691 else if (msg_type > 2000 && msg_type < 2100)
692 msg_all.msg_flag = FLAG_MSG_PRINT_NOTIFY;
693 else if (msg_type > 2100 && msg_type < 3000)
694 msg_all.msg_flag = FLAG_MSG_PRINT_GENERAL;
695 else if (msg_type > 3000 && msg_type < 4000)
696 msg_all.msg_flag = FLAG_MSG_SMBD;
702 msg_all.duplicates = duplicates_allowed;
705 connections_traverse(traverse_fn, &msg_all);
707 *n_sent = msg_all.n_sent;
712 * Block and unblock receiving of messages. Allows removal of race conditions
713 * when doing a fork and changing message disposition.
716 void message_block(void)
718 BlockSignals(True, SIGUSR1);
721 void message_unblock(void)
723 BlockSignals(False, SIGUSR1);
727 * Samba4 API wrapper around the Samba3 implementation. Yes, I know, we could
728 * import the whole Samba4 thing, but I want notify.c from Samba4 in first.
731 struct messaging_callback {
732 struct messaging_callback *prev, *next;
734 void (*fn)(struct messaging_context *msg, void *private_data,
736 struct server_id server_id, DATA_BLOB *data);
740 struct messaging_context {
742 struct event_context *event_ctx;
743 struct messaging_callback *callbacks;
746 static int messaging_context_destructor(struct messaging_context *ctx)
748 struct messaging_callback *cb;
750 for (cb = ctx->callbacks; cb; cb = cb->next) {
752 * We unconditionally remove all instances of our callback
753 * from the tdb basis.
755 message_deregister(cb->msg_type);
760 struct event_context *messaging_event_context(struct messaging_context *msg_ctx)
762 return msg_ctx->event_ctx;
765 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
766 struct server_id server_id,
767 struct event_context *ev)
769 struct messaging_context *ctx;
771 if (!(ctx = TALLOC_ZERO_P(mem_ctx, struct messaging_context))) {
777 talloc_set_destructor(ctx, messaging_context_destructor);
779 if (!message_init(ctx)) {
780 DEBUG(0, ("message_init failed: %s\n", strerror(errno)));
787 static void messaging_callback(int msg_type, struct server_id pid,
788 void *buf, size_t len, void *private_data)
790 struct messaging_context *ctx = talloc_get_type_abort(
791 private_data, struct messaging_context);
792 struct messaging_callback *cb, *next;
794 for (cb = ctx->callbacks; cb; cb = next) {
796 * Allow a callback to remove itself
800 if (msg_type == cb->msg_type) {
803 blob.data = (uint8 *)buf;
806 cb->fn(ctx, cb->private_data, msg_type, pid, &blob);
812 * Register a dispatch function for a particular message type. Allow multiple
815 NTSTATUS messaging_register(struct messaging_context *ctx, void *private_data,
817 void (*fn)(struct messaging_context *msg,
820 struct server_id server_id,
823 struct messaging_callback *cb;
825 if (!(cb = talloc(ctx, struct messaging_callback))) {
826 return NT_STATUS_NO_MEMORY;
829 cb->msg_type = msg_type;
831 cb->private_data = private_data;
833 DLIST_ADD(ctx->callbacks, cb);
834 message_register(msg_type, messaging_callback, ctx);
839 De-register the function for a particular message type.
841 void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
844 struct messaging_callback *cb, *next;
846 for (cb = ctx->callbacks; cb; cb = next) {
848 if ((cb->msg_type == msg_type)
849 && (cb->private_data == private_data)) {
850 DLIST_REMOVE(ctx->callbacks, cb);
857 Send a message to a particular server
859 NTSTATUS messaging_send(struct messaging_context *msg_ctx,
860 struct server_id server,
861 uint32_t msg_type, const DATA_BLOB *data)
863 return message_send_pid_internal(server, msg_type, data->data,
864 data->length, True, 0);
867 NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
868 struct server_id server, uint32_t msg_type,
869 const uint8 *buf, size_t len)
871 DATA_BLOB blob = data_blob_const(buf, len);
872 return messaging_send(msg_ctx, server, msg_type, &blob);
875 NTSTATUS messaging_send_buf_with_timeout(struct messaging_context *msg_ctx,
876 struct server_id server,
878 const uint8 *buf, size_t len,
881 return message_send_pid_internal(server, msg_type, buf, len,