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
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, pid_t pid, void *buf, size_t len);
72 /****************************************************************************
73 Notifications come in as signals.
74 ****************************************************************************/
76 static void sig_usr1(void)
82 /****************************************************************************
83 A useful function for testing the message system.
84 ****************************************************************************/
86 static void ping_message(int msg_type, pid_t src, void *buf, size_t len)
88 const char *msg = buf ? buf : "none";
89 DEBUG(1,("INFO: Received PING message from PID %u [%s]\n",(unsigned int)src, msg));
90 message_send_pid(src, MSG_PONG, buf, len, True);
93 /****************************************************************************
94 Initialise the messaging functions.
95 ****************************************************************************/
97 BOOL message_init(void)
101 if (tdb) return True;
103 mem_ctx = talloc_init("message_init");
105 DEBUG(0,("ERROR: No memory to initialise messages database\n"));
108 tdb = tdb_open_log(lock_path(mem_ctx, "messages.tdb"),
109 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
110 O_RDWR|O_CREAT,0600);
111 talloc_destroy(mem_ctx);
114 DEBUG(0,("ERROR: Failed to initialise messages database\n"));
118 CatchSignal(SIGUSR1, SIGNAL_CAST sig_usr1);
120 message_register(MSG_PING, ping_message);
125 /*******************************************************************
126 Form a static tdb key from a pid.
127 ******************************************************************/
129 static TDB_DATA message_key_pid(pid_t pid)
134 slprintf(key, sizeof(key)-1, "PID/%d", (int)pid);
136 kbuf.dptr = (char *)key;
137 kbuf.dsize = strlen(key)+1;
141 /****************************************************************************
142 Notify a process that it has a message. If the process doesn't exist
143 then delete its record in the database.
144 ****************************************************************************/
146 static BOOL message_notify(pid_t pid)
149 * Doing kill with a non-positive pid causes messages to be
150 * sent to places we don't want.
155 if (kill(pid, SIGUSR1) == -1) {
156 if (errno == ESRCH) {
157 DEBUG(2,("pid %d doesn't exist - deleting messages record\n", (int)pid));
158 tdb_delete(tdb, message_key_pid(pid));
160 DEBUG(2,("message to process %d failed - %s\n", (int)pid, strerror(errno)));
167 /****************************************************************************
168 Send a message to a particular pid.
169 ****************************************************************************/
171 static BOOL message_send_pid_internal(pid_t pid, int msg_type, const void *buf, size_t len,
172 BOOL duplicates_allowed, unsigned int timeout)
177 struct message_rec rec;
179 struct message_rec prec;
182 * Doing kill with a non-positive pid causes messages to be
183 * sent to places we don't want.
188 rec.msg_version = MESSAGE_VERSION;
189 rec.msg_type = msg_type;
194 kbuf = message_key_pid(pid);
196 dbuf.dptr = (void *)malloc(len + sizeof(rec));
200 memcpy(dbuf.dptr, &rec, sizeof(rec));
202 memcpy((void *)((char*)dbuf.dptr+sizeof(rec)), buf, len);
204 dbuf.dsize = len + sizeof(rec);
206 if (duplicates_allowed) {
208 /* If duplicates are allowed we can just append the message and return. */
210 /* lock the record for the destination */
212 if (tdb_chainlock_with_timeout(tdb, kbuf, timeout) == -1) {
213 DEBUG(0,("message_send_pid_internal: failed to get chainlock with timeout %ul.\n", timeout));
217 if (tdb_chainlock(tdb, kbuf) == -1) {
218 DEBUG(0,("message_send_pid_internal: failed to get chainlock.\n"));
222 tdb_append(tdb, kbuf, dbuf);
223 tdb_chainunlock(tdb, kbuf);
225 SAFE_FREE(dbuf.dptr);
226 errno = 0; /* paranoia */
227 return message_notify(pid);
230 /* lock the record for the destination */
232 if (tdb_chainlock_with_timeout(tdb, kbuf, timeout) == -1) {
233 DEBUG(0,("message_send_pid_internal: failed to get chainlock with timeout %ul.\n", timeout));
237 if (tdb_chainlock(tdb, kbuf) == -1) {
238 DEBUG(0,("message_send_pid_internal: failed to get chainlock.\n"));
243 old_dbuf = tdb_fetch(tdb, kbuf);
245 if (!old_dbuf.dptr) {
246 /* its a new record */
248 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
249 tdb_chainunlock(tdb, kbuf);
251 SAFE_FREE(dbuf.dptr);
252 errno = 0; /* paranoia */
253 return message_notify(pid);
256 /* Not a new record. Check for duplicates. */
258 for(ptr = (char *)old_dbuf.dptr; ptr < old_dbuf.dptr + old_dbuf.dsize; ) {
260 * First check if the message header matches, then, if it's a non-zero
261 * sized message, check if the data matches. If so it's a duplicate and
262 * we can discard it. JRA.
265 if (!memcmp(ptr, &rec, sizeof(rec))) {
266 if (!len || (len && !memcmp( ptr + sizeof(rec), buf, len))) {
267 tdb_chainunlock(tdb, kbuf);
268 DEBUG(10,("message_send_pid_internal: discarding duplicate message.\n"));
269 SAFE_FREE(dbuf.dptr);
270 SAFE_FREE(old_dbuf.dptr);
274 memcpy(&prec, ptr, sizeof(prec));
275 ptr += sizeof(rec) + prec.len;
278 /* we're adding to an existing entry */
280 tdb_append(tdb, kbuf, dbuf);
281 tdb_chainunlock(tdb, kbuf);
283 SAFE_FREE(old_dbuf.dptr);
284 SAFE_FREE(dbuf.dptr);
286 errno = 0; /* paranoia */
287 return message_notify(pid);
290 /****************************************************************************
291 Send a message to a particular pid - no timeout.
292 ****************************************************************************/
294 BOOL message_send_pid(pid_t pid, int msg_type, const void *buf, size_t len, BOOL duplicates_allowed)
296 return message_send_pid_internal(pid, msg_type, buf, len, duplicates_allowed, 0);
299 /****************************************************************************
300 Send a message to a particular pid, with timeout in seconds.
301 ****************************************************************************/
303 BOOL message_send_pid_with_timeout(pid_t pid, int msg_type, const void *buf, size_t len,
304 BOOL duplicates_allowed, unsigned int timeout)
306 return message_send_pid_internal(pid, msg_type, buf, len, duplicates_allowed, timeout);
309 /****************************************************************************
310 Retrieve all messages for the current process.
311 ****************************************************************************/
313 static BOOL retrieve_all_messages(char **msgs_buf, size_t *total_len)
319 ZERO_STRUCT(null_dbuf);
324 kbuf = message_key_pid(getpid());
326 tdb_chainlock(tdb, kbuf);
327 dbuf = tdb_fetch(tdb, kbuf);
329 * Replace with an empty record to keep the allocated
332 tdb_store(tdb, kbuf, null_dbuf, TDB_REPLACE);
333 tdb_chainunlock(tdb, kbuf);
335 if (dbuf.dptr == NULL || dbuf.dsize == 0) {
336 SAFE_FREE(dbuf.dptr);
340 *msgs_buf = dbuf.dptr;
341 *total_len = dbuf.dsize;
346 /****************************************************************************
347 Parse out the next message for the current process.
348 ****************************************************************************/
350 static BOOL message_recv(char *msgs_buf, size_t total_len, int *msg_type, pid_t *src, char **buf, size_t *len)
352 struct message_rec rec;
353 char *ret_buf = *buf;
358 if (total_len - (ret_buf - msgs_buf) < sizeof(rec))
361 memcpy(&rec, ret_buf, sizeof(rec));
362 ret_buf += sizeof(rec);
364 if (rec.msg_version != MESSAGE_VERSION) {
365 DEBUG(0,("message version %d received (expected %d)\n", rec.msg_version, MESSAGE_VERSION));
370 if (total_len - (ret_buf - msgs_buf) < rec.len)
375 *msg_type = rec.msg_type;
382 /****************************************************************************
383 Receive and dispatch any messages pending for this process.
384 Notice that all dispatch handlers for a particular msg_type get called,
385 so you can register multiple handlers for a message.
386 *NOTE*: Dispatch functions must be able to cope with incoming
387 messages on an *odd* byte boundary.
388 ****************************************************************************/
390 void message_dispatch(void)
396 size_t len, total_len;
397 struct dispatch_fns *dfn;
400 if (!received_signal)
403 DEBUG(10,("message_dispatch: received_signal = %d\n", received_signal));
407 if (!retrieve_all_messages(&msgs_buf, &total_len))
410 for (buf = msgs_buf; message_recv(msgs_buf, total_len, &msg_type, &src, &buf, &len); buf += len) {
411 DEBUG(10,("message_dispatch: received msg_type=%d src_pid=%u\n",
412 msg_type, (unsigned int) src));
414 for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
415 if (dfn->msg_type == msg_type) {
416 DEBUG(10,("message_dispatch: processing message of type %d.\n", msg_type));
417 dfn->fn(msg_type, src, len ? (void *)buf : NULL, len);
422 DEBUG(5,("message_dispatch: warning: no handlers registed for "
423 "msg_type %d in pid %u\n",
424 msg_type, (unsigned int)getpid()));
430 /****************************************************************************
431 Register a dispatch function for a particular message type.
432 *NOTE*: Dispatch functions must be able to cope with incoming
433 messages on an *odd* byte boundary.
434 ****************************************************************************/
436 void message_register(int msg_type,
437 void (*fn)(int msg_type, pid_t pid, void *buf, size_t len))
439 struct dispatch_fns *dfn;
441 dfn = (struct dispatch_fns *)malloc(sizeof(*dfn));
447 dfn->msg_type = msg_type;
450 DLIST_ADD(dispatch_fns, dfn);
454 DEBUG(0,("message_register: Not enough memory. malloc failed!\n"));
458 /****************************************************************************
459 De-register the function for a particular message type.
460 ****************************************************************************/
462 void message_deregister(int msg_type)
464 struct dispatch_fns *dfn, *next;
466 for (dfn = dispatch_fns; dfn; dfn = next) {
468 if (dfn->msg_type == msg_type) {
469 DLIST_REMOVE(dispatch_fns, dfn);
484 /****************************************************************************
485 Send one of the messages for the broadcast.
486 ****************************************************************************/
488 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
490 struct connections_data crec;
491 struct msg_all *msg_all = (struct msg_all *)state;
493 if (dbuf.dsize != sizeof(crec))
496 memcpy(&crec, dbuf.dptr, sizeof(crec));
501 /* Don't send if the receiver hasn't registered an interest. */
503 if(!(crec.bcast_msg_flags & msg_all->msg_flag))
506 /* If the msg send fails because the pid was not found (i.e. smbd died),
507 * the msg has already been deleted from the messages.tdb.*/
509 if (!message_send_pid(crec.pid, msg_all->msg_type,
510 msg_all->buf, msg_all->len,
511 msg_all->duplicates)) {
513 /* If the pid was not found delete the entry from connections.tdb */
515 if (errno == ESRCH) {
516 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
517 (unsigned int)crec.pid, crec.cnum, crec.name));
518 tdb_delete(the_tdb, kbuf);
526 * Send a message to all smbd processes.
528 * It isn't very efficient, but should be OK for the sorts of
529 * applications that use it. When we need efficient broadcast we can add
532 * @param n_sent Set to the number of messages sent. This should be
533 * equal to the number of processes, but be careful for races.
535 * @retval True for success.
537 BOOL message_send_all(TDB_CONTEXT *conn_tdb, int msg_type,
538 const void *buf, size_t len,
539 BOOL duplicates_allowed,
542 struct msg_all msg_all;
544 msg_all.msg_type = msg_type;
546 msg_all.msg_flag = FLAG_MSG_GENERAL;
547 else if (msg_type > 1000 && msg_type < 2000)
548 msg_all.msg_flag = FLAG_MSG_NMBD;
549 else if (msg_type > 2000 && msg_type < 3000)
550 msg_all.msg_flag = FLAG_MSG_PRINTING;
551 else if (msg_type > 3000 && msg_type < 4000)
552 msg_all.msg_flag = FLAG_MSG_SMBD;
558 msg_all.duplicates = duplicates_allowed;
561 tdb_traverse(conn_tdb, traverse_fn, &msg_all);
563 *n_sent = msg_all.n_sent;