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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 @defgroup messages Internal messaging framework
29 @brief Module for internal messaging between Samba daemons.
31 The idea is that if a part of Samba wants to do communication with
32 another Samba process then it will do a message_register() of a
33 dispatch function, and use message_send_pid() to send messages to
36 The dispatch function is given the pid of the sender, and it can
37 use that to reply by message_send_pid(). See ping_message() for a
40 @caution Dispatch functions must be able to cope with incoming
41 messages on an *odd* byte boundary.
43 This system doesn't have any inherent size limitations but is not
44 very efficient for large messages or when messages are sent in very
51 /* the locking database handle */
52 static TDB_CONTEXT *tdb;
53 static int received_signal;
55 /* change the message version with any incompatible changes in the protocol */
56 #define MESSAGE_VERSION 1
61 struct server_id dest;
66 /* we have a linked list of dispatch handlers */
67 static struct dispatch_fns {
68 struct dispatch_fns *next, *prev;
70 void (*fn)(int msg_type, struct server_id pid, void *buf, size_t len,
75 static void message_register(int msg_type,
76 void (*fn)(int msg_type, struct server_id pid,
77 void *buf, size_t len,
81 /****************************************************************************
83 ****************************************************************************/
85 void gfree_messages(void)
87 struct dispatch_fns *dfn, *next;
89 /* delete the dispatch_fns list */
93 DLIST_REMOVE(dispatch_fns, dfn);
99 /****************************************************************************
100 Notifications come in as signals.
101 ****************************************************************************/
103 static void sig_usr1(void)
106 sys_select_signal(SIGUSR1);
109 static NTSTATUS message_send_pid(struct server_id pid, int msg_type,
110 const void *buf, size_t len);
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);
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();
216 * Something has gone wrong
219 if (errno == ESRCH) {
220 DEBUG(2,("pid %d doesn't exist - deleting messages record\n",
222 tdb_delete(tdb, message_key_pid(procid));
225 * INVALID_HANDLE is the closest I can think of -- vl
227 return NT_STATUS_INVALID_HANDLE;
230 DEBUG(2,("message to process %d failed - %s\n", (int)pid,
234 * No call to map_nt_error_from_unix -- don't want to link in
235 * errormap.o into lots of utils.
238 if (errno == EINVAL) return NT_STATUS_INVALID_PARAMETER;
239 if (errno == EPERM) return NT_STATUS_ACCESS_DENIED;
240 return NT_STATUS_UNSUCCESSFUL;
243 /****************************************************************************
244 Send a message to a particular pid.
245 ****************************************************************************/
247 static NTSTATUS message_send_pid(struct server_id pid, int msg_type,
248 const void *buf, size_t len)
251 struct message_rec rec;
254 /* NULL pointer means implicit length zero. */
256 SMB_ASSERT(len == 0);
260 * Doing kill with a non-positive pid causes messages to be
261 * sent to places we don't want.
264 SMB_ASSERT(procid_to_pid(&pid) > 0);
266 rec.msg_version = MESSAGE_VERSION;
267 rec.msg_type = msg_type;
269 rec.src = procid_self();
270 rec.len = buf ? len : 0;
272 dbuf.dptr = (uint8 *)SMB_MALLOC(len + sizeof(rec));
274 return NT_STATUS_NO_MEMORY;
277 memcpy(dbuf.dptr, &rec, sizeof(rec));
279 memcpy((void *)((char*)dbuf.dptr+sizeof(rec)), buf, len);
281 dbuf.dsize = len + sizeof(rec);
283 ret = tdb_append(tdb, message_key_pid(pid), dbuf);
285 SAFE_FREE(dbuf.dptr);
288 return NT_STATUS_INTERNAL_ERROR;
291 errno = 0; /* paranoia */
292 return message_notify(pid);
295 /****************************************************************************
296 Count the messages pending for a particular pid. Expensive....
297 ****************************************************************************/
299 unsigned int messages_pending_for_pid(struct server_id pid)
303 unsigned int message_count = 0;
305 dbuf = tdb_fetch(tdb, message_key_pid(pid));
306 if (dbuf.dptr == NULL || dbuf.dsize == 0) {
307 SAFE_FREE(dbuf.dptr);
311 for (buf = dbuf.dptr; dbuf.dsize > sizeof(struct message_rec);) {
312 struct message_rec rec;
313 memcpy(&rec, buf, sizeof(rec));
314 buf += (sizeof(rec) + rec.len);
315 dbuf.dsize -= (sizeof(rec) + rec.len);
319 SAFE_FREE(dbuf.dptr);
320 return message_count;
323 /****************************************************************************
324 Retrieve all messages for the current process.
325 ****************************************************************************/
327 static BOOL retrieve_all_messages(char **msgs_buf, size_t *total_len)
333 ZERO_STRUCT(null_dbuf);
338 kbuf = message_key_pid(procid_self());
340 if (tdb_chainlock(tdb, kbuf) == -1)
343 dbuf = tdb_fetch(tdb, kbuf);
345 * Replace with an empty record to keep the allocated
348 tdb_store(tdb, kbuf, null_dbuf, TDB_REPLACE);
349 tdb_chainunlock(tdb, kbuf);
351 if (dbuf.dptr == NULL || dbuf.dsize == 0) {
352 SAFE_FREE(dbuf.dptr);
356 *msgs_buf = (char *)dbuf.dptr;
357 *total_len = dbuf.dsize;
362 /****************************************************************************
363 Parse out the next message for the current process.
364 ****************************************************************************/
366 static BOOL message_recv(char *msgs_buf, size_t total_len, int *msg_type,
367 struct server_id *src, char **buf, size_t *len)
369 struct message_rec rec;
370 char *ret_buf = *buf;
375 if (total_len - (ret_buf - msgs_buf) < sizeof(rec))
378 memcpy(&rec, ret_buf, sizeof(rec));
379 ret_buf += sizeof(rec);
381 if (rec.msg_version != MESSAGE_VERSION) {
382 DEBUG(0,("message version %d received (expected %d)\n",
383 rec.msg_version, MESSAGE_VERSION));
388 if (total_len - (ret_buf - msgs_buf) < rec.len)
393 *msg_type = rec.msg_type;
400 /****************************************************************************
401 Receive and dispatch any messages pending for this process.
402 JRA changed Dec 13 2006. Only one message handler now permitted per type.
403 *NOTE*: Dispatch functions must be able to cope with incoming
404 messages on an *odd* byte boundary.
405 ****************************************************************************/
407 void message_dispatch(void)
410 struct server_id src;
413 size_t len, total_len;
416 if (!received_signal)
419 DEBUG(10, ("message_dispatch: received_signal = %d\n",
424 if (!retrieve_all_messages(&msgs_buf, &total_len))
428 message_recv(msgs_buf, total_len, &msg_type, &src, &buf, &len);
430 struct dispatch_fns *dfn;
432 DEBUG(10,("message_dispatch: received msg_type=%d "
433 "src_pid=%u\n", msg_type,
434 (unsigned int) procid_to_pid(&src)));
437 for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
438 if (dfn->msg_type == msg_type) {
439 DEBUG(10,("message_dispatch: processing "
440 "message of type %d.\n", msg_type));
441 dfn->fn(msg_type, src,
442 len ? (void *)buf : NULL, len,
449 DEBUG(5,("message_dispatch: warning: no handler "
450 "registed for msg_type %d in pid %u\n",
451 msg_type, (unsigned int)sys_getpid()));
457 /****************************************************************************
458 Register/replace a dispatch function for a particular message type.
459 JRA changed Dec 13 2006. Only one message handler now permitted per type.
460 *NOTE*: Dispatch functions must be able to cope with incoming
461 messages on an *odd* byte boundary.
462 ****************************************************************************/
464 static void message_register(int msg_type,
465 void (*fn)(int msg_type, struct server_id pid,
466 void *buf, size_t len,
470 struct dispatch_fns *dfn;
472 for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
473 if (dfn->msg_type == msg_type) {
479 if (!(dfn = SMB_MALLOC_P(struct dispatch_fns))) {
480 DEBUG(0,("message_register: Not enough memory. malloc "
487 dfn->msg_type = msg_type;
489 dfn->private_data = private_data;
491 DLIST_ADD(dispatch_fns, dfn);
494 /****************************************************************************
495 De-register the function for a particular message type.
496 ****************************************************************************/
498 static void message_deregister(int msg_type)
500 struct dispatch_fns *dfn, *next;
502 for (dfn = dispatch_fns; dfn; dfn = next) {
504 if (dfn->msg_type == msg_type) {
505 DLIST_REMOVE(dispatch_fns, dfn);
521 /****************************************************************************
522 Send one of the messages for the broadcast.
523 ****************************************************************************/
525 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
528 struct connections_data crec;
529 struct msg_all *msg_all = (struct msg_all *)state;
532 if (dbuf.dsize != sizeof(crec))
535 memcpy(&crec, dbuf.dptr, sizeof(crec));
540 /* Don't send if the receiver hasn't registered an interest. */
542 if(!(crec.bcast_msg_flags & msg_all->msg_flag))
545 /* If the msg send fails because the pid was not found (i.e. smbd died),
546 * the msg has already been deleted from the messages.tdb.*/
548 status = message_send_pid(crec.pid, msg_all->msg_type,
549 msg_all->buf, msg_all->len);
551 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
553 /* If the pid was not found delete the entry from
556 DEBUG(2,("pid %s doesn't exist - deleting connections "
557 "%d [%s]\n", procid_str_static(&crec.pid), crec.cnum,
559 tdb_delete(the_tdb, kbuf);
566 * Send a message to all smbd processes.
568 * It isn't very efficient, but should be OK for the sorts of
569 * applications that use it. When we need efficient broadcast we can add
572 * @param n_sent Set to the number of messages sent. This should be
573 * equal to the number of processes, but be careful for races.
575 * @retval True for success.
577 BOOL message_send_all(struct messaging_context *msg_ctx,
579 const void *buf, size_t len,
580 BOOL duplicates_allowed,
583 struct msg_all msg_all;
585 msg_all.msg_type = msg_type;
587 msg_all.msg_flag = FLAG_MSG_GENERAL;
588 else if (msg_type > 1000 && msg_type < 2000)
589 msg_all.msg_flag = FLAG_MSG_NMBD;
590 else if (msg_type > 2000 && msg_type < 2100)
591 msg_all.msg_flag = FLAG_MSG_PRINT_NOTIFY;
592 else if (msg_type > 2100 && msg_type < 3000)
593 msg_all.msg_flag = FLAG_MSG_PRINT_GENERAL;
594 else if (msg_type > 3000 && msg_type < 4000)
595 msg_all.msg_flag = FLAG_MSG_SMBD;
601 msg_all.duplicates = duplicates_allowed;
604 connections_traverse(traverse_fn, &msg_all);
606 *n_sent = msg_all.n_sent;
611 * Block and unblock receiving of messages. Allows removal of race conditions
612 * when doing a fork and changing message disposition.
615 void message_block(void)
617 BlockSignals(True, SIGUSR1);
620 void message_unblock(void)
622 BlockSignals(False, SIGUSR1);
626 * Samba4 API wrapper around the Samba3 implementation. Yes, I know, we could
627 * import the whole Samba4 thing, but I want notify.c from Samba4 in first.
630 struct messaging_callback {
631 struct messaging_callback *prev, *next;
633 void (*fn)(struct messaging_context *msg, void *private_data,
635 struct server_id server_id, DATA_BLOB *data);
639 struct messaging_context {
641 struct event_context *event_ctx;
642 struct messaging_callback *callbacks;
645 static int messaging_context_destructor(struct messaging_context *ctx)
647 struct messaging_callback *cb;
649 for (cb = ctx->callbacks; cb; cb = cb->next) {
651 * We unconditionally remove all instances of our callback
652 * from the tdb basis.
654 message_deregister(cb->msg_type);
659 struct event_context *messaging_event_context(struct messaging_context *msg_ctx)
661 return msg_ctx->event_ctx;
664 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
665 struct server_id server_id,
666 struct event_context *ev)
668 struct messaging_context *ctx;
670 if (!(ctx = TALLOC_ZERO_P(mem_ctx, struct messaging_context))) {
676 talloc_set_destructor(ctx, messaging_context_destructor);
678 if (!message_init(ctx)) {
679 DEBUG(0, ("message_init failed: %s\n", strerror(errno)));
686 static void messaging_callback(int msg_type, struct server_id pid,
687 void *buf, size_t len, void *private_data)
689 struct messaging_context *ctx = talloc_get_type_abort(
690 private_data, struct messaging_context);
691 struct messaging_callback *cb, *next;
693 for (cb = ctx->callbacks; cb; cb = next) {
695 * Allow a callback to remove itself
699 if (msg_type == cb->msg_type) {
702 blob.data = (uint8 *)buf;
705 cb->fn(ctx, cb->private_data, msg_type, pid, &blob);
711 * Register a dispatch function for a particular message type. Allow multiple
714 NTSTATUS messaging_register(struct messaging_context *ctx, void *private_data,
716 void (*fn)(struct messaging_context *msg,
719 struct server_id server_id,
722 struct messaging_callback *cb;
724 if (!(cb = talloc(ctx, struct messaging_callback))) {
725 return NT_STATUS_NO_MEMORY;
728 cb->msg_type = msg_type;
730 cb->private_data = private_data;
732 DLIST_ADD(ctx->callbacks, cb);
733 message_register(msg_type, messaging_callback, ctx);
738 De-register the function for a particular message type.
740 void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
743 struct messaging_callback *cb, *next;
745 for (cb = ctx->callbacks; cb; cb = next) {
747 if ((cb->msg_type == msg_type)
748 && (cb->private_data == private_data)) {
749 DLIST_REMOVE(ctx->callbacks, cb);
756 Send a message to a particular server
758 NTSTATUS messaging_send(struct messaging_context *msg_ctx,
759 struct server_id server,
760 uint32_t msg_type, const DATA_BLOB *data)
762 return message_send_pid(server, msg_type, data->data, data->length);
765 NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
766 struct server_id server, uint32_t msg_type,
767 const uint8 *buf, size_t len)
769 DATA_BLOB blob = data_blob_const(buf, len);
770 return messaging_send(msg_ctx, server, msg_type, &blob);