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 /****************************************************************************
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 static NTSTATUS message_send_pid(struct server_id pid, int msg_type,
103 const void *buf, size_t len,
104 BOOL duplicates_allowed);
106 /****************************************************************************
107 A useful function for testing the message system.
108 ****************************************************************************/
110 static void ping_message(int msg_type, struct server_id src,
111 void *buf, size_t len, void *private_data)
113 const char *msg = buf ? (const char *)buf : "none";
115 DEBUG(1,("INFO: Received PING message from PID %s [%s]\n",
116 procid_str_static(&src), msg));
117 message_send_pid(src, MSG_PONG, buf, len, True);
120 /****************************************************************************
121 Initialise the messaging functions.
122 ****************************************************************************/
124 static BOOL message_init(struct messaging_context *msg_ctx)
131 tdb = tdb_open_log(lock_path("messages.tdb"),
132 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
133 O_RDWR|O_CREAT,0600);
136 DEBUG(0,("ERROR: Failed to initialise messages database\n"));
140 /* Activate the per-hashchain freelist */
141 tdb_set_max_dead(tdb, 5);
143 CatchSignal(SIGUSR1, SIGNAL_CAST sig_usr1);
145 message_register(MSG_PING, ping_message, NULL);
147 /* Register some debugging related messages */
149 register_msg_pool_usage(msg_ctx);
150 register_dmalloc_msgs();
151 debug_register_msgs(msg_ctx);
156 /*******************************************************************
157 Form a static tdb key from a pid.
158 ******************************************************************/
160 static TDB_DATA message_key_pid(struct server_id pid)
165 slprintf(key, sizeof(key)-1, "PID/%s", procid_str_static(&pid));
167 kbuf.dptr = (uint8 *)key;
168 kbuf.dsize = strlen(key)+1;
172 /****************************************************************************
173 Notify a process that it has a message. If the process doesn't exist
174 then delete its record in the database.
175 ****************************************************************************/
177 static NTSTATUS message_notify(struct server_id procid)
179 pid_t pid = procid.pid;
181 uid_t euid = geteuid();
184 * Doing kill with a non-positive pid causes messages to be
185 * sent to places we don't want.
191 /* If we're not root become so to send the message. */
193 set_effective_uid(0);
196 ret = kill(pid, SIGUSR1);
199 /* Go back to who we were. */
200 int saved_errno = errno;
201 restore_re_uid_fromroot();
206 if (errno == ESRCH) {
207 DEBUG(2,("pid %d doesn't exist - deleting messages record\n",
209 tdb_delete(tdb, message_key_pid(procid));
212 * INVALID_HANDLE is the closest I can think of -- vl
214 return NT_STATUS_INVALID_HANDLE;
217 DEBUG(2,("message to process %d failed - %s\n", (int)pid,
221 * No call to map_nt_error_from_unix -- don't want to link in
222 * errormap.o into lots of utils.
225 if (errno == EINVAL) return NT_STATUS_INVALID_PARAMETER;
226 if (errno == EPERM) return NT_STATUS_ACCESS_DENIED;
227 return NT_STATUS_UNSUCCESSFUL;
233 /****************************************************************************
234 Send a message to a particular pid.
235 ****************************************************************************/
237 static NTSTATUS message_send_pid_internal(struct server_id pid, int msg_type,
238 const void *buf, size_t len,
239 BOOL duplicates_allowed,
240 unsigned int timeout)
245 struct message_rec rec;
247 struct message_rec prec;
249 /* NULL pointer means implicit length zero. */
251 SMB_ASSERT(len == 0);
255 * Doing kill with a non-positive pid causes messages to be
256 * sent to places we don't want.
259 SMB_ASSERT(procid_to_pid(&pid) > 0);
261 rec.msg_version = MESSAGE_VERSION;
262 rec.msg_type = msg_type;
264 rec.src = procid_self();
265 rec.len = buf ? len : 0;
267 kbuf = message_key_pid(pid);
269 dbuf.dptr = (uint8 *)SMB_MALLOC(len + sizeof(rec));
271 return NT_STATUS_NO_MEMORY;
274 memcpy(dbuf.dptr, &rec, sizeof(rec));
276 memcpy((void *)((char*)dbuf.dptr+sizeof(rec)), buf, len);
278 dbuf.dsize = len + sizeof(rec);
280 if (duplicates_allowed) {
282 /* If duplicates are allowed we can just append the message and return. */
284 /* lock the record for the destination */
286 if (tdb_chainlock_with_timeout(tdb, kbuf, timeout) == -1) {
287 DEBUG(0,("message_send_pid_internal: failed to get "
288 "chainlock with timeout %ul.\n", timeout));
289 return NT_STATUS_IO_TIMEOUT;
292 if (tdb_chainlock(tdb, kbuf) == -1) {
293 DEBUG(0,("message_send_pid_internal: failed to get "
295 return NT_STATUS_LOCK_NOT_GRANTED;
298 tdb_append(tdb, kbuf, dbuf);
299 tdb_chainunlock(tdb, kbuf);
301 SAFE_FREE(dbuf.dptr);
302 errno = 0; /* paranoia */
303 return message_notify(pid);
306 /* lock the record for the destination */
308 if (tdb_chainlock_with_timeout(tdb, kbuf, timeout) == -1) {
309 DEBUG(0,("message_send_pid_internal: failed to get chainlock "
310 "with timeout %ul.\n", timeout));
311 return NT_STATUS_IO_TIMEOUT;
314 if (tdb_chainlock(tdb, kbuf) == -1) {
315 DEBUG(0,("message_send_pid_internal: failed to get "
317 return NT_STATUS_LOCK_NOT_GRANTED;
321 old_dbuf = tdb_fetch(tdb, kbuf);
323 if (!old_dbuf.dptr) {
324 /* its a new record */
326 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
327 tdb_chainunlock(tdb, kbuf);
329 SAFE_FREE(dbuf.dptr);
330 errno = 0; /* paranoia */
331 return message_notify(pid);
334 /* Not a new record. Check for duplicates. */
336 for(ptr = old_dbuf.dptr; ptr < old_dbuf.dptr + old_dbuf.dsize; ) {
338 * First check if the message header matches, then, if it's a non-zero
339 * sized message, check if the data matches. If so it's a duplicate and
340 * we can discard it. JRA.
343 if (!memcmp(ptr, &rec, sizeof(rec))) {
344 if (!len || (len && !memcmp( ptr + sizeof(rec), buf, len))) {
345 tdb_chainunlock(tdb, kbuf);
346 DEBUG(10,("message_send_pid_internal: discarding "
347 "duplicate message.\n"));
348 SAFE_FREE(dbuf.dptr);
349 SAFE_FREE(old_dbuf.dptr);
353 memcpy(&prec, ptr, sizeof(prec));
354 ptr += sizeof(rec) + prec.len;
357 /* we're adding to an existing entry */
359 tdb_append(tdb, kbuf, dbuf);
360 tdb_chainunlock(tdb, kbuf);
362 SAFE_FREE(old_dbuf.dptr);
363 SAFE_FREE(dbuf.dptr);
365 errno = 0; /* paranoia */
366 return message_notify(pid);
369 /****************************************************************************
370 Send a message to a particular pid - no timeout.
371 ****************************************************************************/
373 static NTSTATUS message_send_pid(struct server_id pid, int msg_type,
374 const void *buf, size_t len,
375 BOOL duplicates_allowed)
377 return message_send_pid_internal(pid, msg_type, buf, len,
378 duplicates_allowed, 0);
381 /****************************************************************************
382 Count the messages pending for a particular pid. Expensive....
383 ****************************************************************************/
385 unsigned int messages_pending_for_pid(struct server_id pid)
390 unsigned int message_count = 0;
392 kbuf = message_key_pid(pid);
394 dbuf = tdb_fetch(tdb, kbuf);
395 if (dbuf.dptr == NULL || dbuf.dsize == 0) {
396 SAFE_FREE(dbuf.dptr);
400 for (buf = dbuf.dptr; dbuf.dsize > sizeof(struct message_rec);) {
401 struct message_rec rec;
402 memcpy(&rec, buf, sizeof(rec));
403 buf += (sizeof(rec) + rec.len);
404 dbuf.dsize -= (sizeof(rec) + rec.len);
408 SAFE_FREE(dbuf.dptr);
409 return message_count;
412 /****************************************************************************
413 Retrieve all messages for the current process.
414 ****************************************************************************/
416 static BOOL retrieve_all_messages(char **msgs_buf, size_t *total_len)
422 ZERO_STRUCT(null_dbuf);
427 kbuf = message_key_pid(pid_to_procid(sys_getpid()));
429 if (tdb_chainlock(tdb, kbuf) == -1)
432 dbuf = tdb_fetch(tdb, kbuf);
434 * Replace with an empty record to keep the allocated
437 tdb_store(tdb, kbuf, null_dbuf, TDB_REPLACE);
438 tdb_chainunlock(tdb, kbuf);
440 if (dbuf.dptr == NULL || dbuf.dsize == 0) {
441 SAFE_FREE(dbuf.dptr);
445 *msgs_buf = (char *)dbuf.dptr;
446 *total_len = dbuf.dsize;
451 /****************************************************************************
452 Parse out the next message for the current process.
453 ****************************************************************************/
455 static BOOL message_recv(char *msgs_buf, size_t total_len, int *msg_type,
456 struct server_id *src, char **buf, size_t *len)
458 struct message_rec rec;
459 char *ret_buf = *buf;
464 if (total_len - (ret_buf - msgs_buf) < sizeof(rec))
467 memcpy(&rec, ret_buf, sizeof(rec));
468 ret_buf += sizeof(rec);
470 if (rec.msg_version != MESSAGE_VERSION) {
471 DEBUG(0,("message version %d received (expected %d)\n", rec.msg_version, MESSAGE_VERSION));
476 if (total_len - (ret_buf - msgs_buf) < rec.len)
481 *msg_type = rec.msg_type;
488 /****************************************************************************
489 Receive and dispatch any messages pending for this process.
490 JRA changed Dec 13 2006. Only one message handler now permitted per type.
491 *NOTE*: Dispatch functions must be able to cope with incoming
492 messages on an *odd* byte boundary.
493 ****************************************************************************/
495 void message_dispatch(void)
498 struct server_id src;
501 size_t len, total_len;
504 if (!received_signal)
507 DEBUG(10,("message_dispatch: received_signal = %d\n", received_signal));
511 if (!retrieve_all_messages(&msgs_buf, &total_len))
514 for (buf = msgs_buf; message_recv(msgs_buf, total_len, &msg_type, &src, &buf, &len); buf += len) {
515 struct dispatch_fns *dfn;
517 DEBUG(10,("message_dispatch: received msg_type=%d "
518 "src_pid=%u\n", msg_type,
519 (unsigned int) procid_to_pid(&src)));
522 for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
523 if (dfn->msg_type == msg_type) {
524 DEBUG(10,("message_dispatch: processing message of type %d.\n", msg_type));
525 dfn->fn(msg_type, src,
526 len ? (void *)buf : NULL, len,
533 DEBUG(5,("message_dispatch: warning: no handler registed for "
534 "msg_type %d in pid %u\n",
535 msg_type, (unsigned int)sys_getpid()));
541 /****************************************************************************
542 Register/replace a dispatch function for a particular message type.
543 JRA changed Dec 13 2006. Only one message handler now permitted per type.
544 *NOTE*: Dispatch functions must be able to cope with incoming
545 messages on an *odd* byte boundary.
546 ****************************************************************************/
548 void message_register(int msg_type,
549 void (*fn)(int msg_type, struct server_id pid,
550 void *buf, size_t len,
554 struct dispatch_fns *dfn;
556 for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
557 if (dfn->msg_type == msg_type) {
563 dfn = SMB_MALLOC_P(struct dispatch_fns);
569 dfn->msg_type = msg_type;
571 dfn->private_data = private_data;
573 DLIST_ADD(dispatch_fns, dfn);
577 DEBUG(0,("message_register: Not enough memory. malloc failed!\n"));
581 /****************************************************************************
582 De-register the function for a particular message type.
583 ****************************************************************************/
585 void message_deregister(int msg_type)
587 struct dispatch_fns *dfn, *next;
589 for (dfn = dispatch_fns; dfn; dfn = next) {
591 if (dfn->msg_type == msg_type) {
592 DLIST_REMOVE(dispatch_fns, dfn);
608 /****************************************************************************
609 Send one of the messages for the broadcast.
610 ****************************************************************************/
612 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
614 struct connections_data crec;
615 struct msg_all *msg_all = (struct msg_all *)state;
618 if (dbuf.dsize != sizeof(crec))
621 memcpy(&crec, dbuf.dptr, sizeof(crec));
626 /* Don't send if the receiver hasn't registered an interest. */
628 if(!(crec.bcast_msg_flags & msg_all->msg_flag))
631 /* If the msg send fails because the pid was not found (i.e. smbd died),
632 * the msg has already been deleted from the messages.tdb.*/
634 status = message_send_pid(crec.pid, msg_all->msg_type,
635 msg_all->buf, msg_all->len,
636 msg_all->duplicates);
638 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
640 /* If the pid was not found delete the entry from connections.tdb */
642 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
643 procid_str_static(&crec.pid), crec.cnum, crec.servicename));
644 tdb_delete(the_tdb, kbuf);
651 * Send a message to all smbd processes.
653 * It isn't very efficient, but should be OK for the sorts of
654 * applications that use it. When we need efficient broadcast we can add
657 * @param n_sent Set to the number of messages sent. This should be
658 * equal to the number of processes, but be careful for races.
660 * @retval True for success.
662 BOOL message_send_all(struct messaging_context *msg_ctx,
664 const void *buf, size_t len,
665 BOOL duplicates_allowed,
668 struct msg_all msg_all;
670 msg_all.msg_type = msg_type;
672 msg_all.msg_flag = FLAG_MSG_GENERAL;
673 else if (msg_type > 1000 && msg_type < 2000)
674 msg_all.msg_flag = FLAG_MSG_NMBD;
675 else if (msg_type > 2000 && msg_type < 2100)
676 msg_all.msg_flag = FLAG_MSG_PRINT_NOTIFY;
677 else if (msg_type > 2100 && msg_type < 3000)
678 msg_all.msg_flag = FLAG_MSG_PRINT_GENERAL;
679 else if (msg_type > 3000 && msg_type < 4000)
680 msg_all.msg_flag = FLAG_MSG_SMBD;
686 msg_all.duplicates = duplicates_allowed;
689 connections_traverse(traverse_fn, &msg_all);
691 *n_sent = msg_all.n_sent;
696 * Block and unblock receiving of messages. Allows removal of race conditions
697 * when doing a fork and changing message disposition.
700 void message_block(void)
702 BlockSignals(True, SIGUSR1);
705 void message_unblock(void)
707 BlockSignals(False, SIGUSR1);
711 * Samba4 API wrapper around the Samba3 implementation. Yes, I know, we could
712 * import the whole Samba4 thing, but I want notify.c from Samba4 in first.
715 struct messaging_callback {
716 struct messaging_callback *prev, *next;
718 void (*fn)(struct messaging_context *msg, void *private_data,
720 struct server_id server_id, DATA_BLOB *data);
724 struct messaging_context {
726 struct messaging_callback *callbacks;
729 static int messaging_context_destructor(struct messaging_context *ctx)
731 struct messaging_callback *cb;
733 for (cb = ctx->callbacks; cb; cb = cb->next) {
735 * We unconditionally remove all instances of our callback
736 * from the tdb basis.
738 message_deregister(cb->msg_type);
743 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
744 struct server_id server_id,
745 struct event_context *ev)
747 struct messaging_context *ctx;
749 if (!(ctx = TALLOC_ZERO_P(mem_ctx, struct messaging_context))) {
754 talloc_set_destructor(ctx, messaging_context_destructor);
756 if (!message_init(ctx)) {
757 DEBUG(0, ("message_init failed: %s\n", strerror(errno)));
764 static void messaging_callback(int msg_type, struct server_id pid,
765 void *buf, size_t len, void *private_data)
767 struct messaging_context *ctx = talloc_get_type_abort(
768 private_data, struct messaging_context);
769 struct messaging_callback *cb, *next;
771 for (cb = ctx->callbacks; cb; cb = next) {
773 * Allow a callback to remove itself
777 if (msg_type == cb->msg_type) {
780 blob.data = (uint8 *)buf;
783 cb->fn(ctx, cb->private_data, msg_type, pid, &blob);
789 * Register a dispatch function for a particular message type. Allow multiple
792 NTSTATUS messaging_register(struct messaging_context *ctx, void *private_data,
794 void (*fn)(struct messaging_context *msg,
797 struct server_id server_id,
800 struct messaging_callback *cb;
802 if (!(cb = talloc(ctx, struct messaging_callback))) {
803 return NT_STATUS_NO_MEMORY;
806 cb->msg_type = msg_type;
808 cb->private_data = private_data;
810 DLIST_ADD(ctx->callbacks, cb);
811 message_register(msg_type, messaging_callback, ctx);
816 De-register the function for a particular message type.
818 void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
821 struct messaging_callback *cb, *next;
823 for (cb = ctx->callbacks; cb; cb = next) {
825 if ((cb->msg_type == msg_type)
826 && (cb->private_data == private_data)) {
827 DLIST_REMOVE(ctx->callbacks, cb);
834 Send a message to a particular server
836 NTSTATUS messaging_send(struct messaging_context *msg_ctx,
837 struct server_id server,
838 uint32_t msg_type, const DATA_BLOB *data)
840 return message_send_pid_internal(server, msg_type, data->data,
841 data->length, True, 0);
844 NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
845 struct server_id server, uint32_t msg_type,
846 const uint8 *buf, size_t len)
848 DATA_BLOB blob = data_blob_const(buf, len);
849 return messaging_send(msg_ctx, server, msg_type, &blob);
852 NTSTATUS messaging_send_buf_with_timeout(struct messaging_context *msg_ctx,
853 struct server_id server,
855 const uint8 *buf, size_t len,
858 return message_send_pid_internal(server, msg_type, buf, len,