2 Unix SMB/CIFS implementation.
3 Samba internal messaging functions
4 Copyright (C) Andrew Tridgell 2000
5 Copyright (C) 2001 by Martin Pool
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 @defgroups messages Internal messaging framework
27 This module is used for internal messaging between Samba daemons.
29 The idea is that if a part of Samba wants to do communication with
30 another Samba process then it will do a message_register() of a
31 dispatch function, and use message_send_pid() to send messages to
34 The dispatch function is given the pid of the sender, and it can
35 use that to reply by message_send_pid(). See ping_message() for a
38 This system doesn't have any inherent size limitations but is not
39 very efficient for large messages or when messages are sent in very
46 /* the locking database handle */
47 static TDB_CONTEXT *tdb;
48 static int received_signal;
50 /* change the message version with any incompatible changes in the protocol */
51 #define MESSAGE_VERSION 1
61 /* we have a linked list of dispatch handlers */
62 static struct dispatch_fns {
63 struct dispatch_fns *next, *prev;
65 void (*fn)(int msg_type, pid_t pid, void *buf, size_t len);
68 /****************************************************************************
69 Notifications come in as signals.
70 ****************************************************************************/
72 static void sig_usr1(void)
78 /****************************************************************************
79 A useful function for testing the message system.
80 ****************************************************************************/
82 static void ping_message(int msg_type, pid_t src, void *buf, size_t len)
84 char *msg = buf ? buf : "none";
85 DEBUG(1,("INFO: Received PING message from PID %u [%s]\n",(unsigned int)src, msg));
86 message_send_pid(src, MSG_PONG, buf, len, True);
89 /****************************************************************************
90 Initialise the messaging functions.
91 ****************************************************************************/
93 BOOL message_init(void)
97 tdb = tdb_open_log(lock_path("messages.tdb"),
98 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
102 DEBUG(0,("ERROR: Failed to initialise messages database\n"));
106 CatchSignal(SIGUSR1, SIGNAL_CAST sig_usr1);
108 message_register(MSG_PING, ping_message);
113 /*******************************************************************
114 Form a static tdb key from a pid.
115 ******************************************************************/
117 static TDB_DATA message_key_pid(pid_t pid)
122 slprintf(key, sizeof(key)-1, "PID/%d", (int)pid);
124 kbuf.dptr = (char *)key;
125 kbuf.dsize = strlen(key)+1;
129 /****************************************************************************
130 Notify a process that it has a message. If the process doesn't exist
131 then delete its record in the database.
132 ****************************************************************************/
134 static BOOL message_notify(pid_t pid)
136 /* Doing kill with a non-positive pid causes messages to be
137 * sent to places we don't want. */
139 if (kill(pid, SIGUSR1) == -1) {
140 if (errno == ESRCH) {
141 DEBUG(2,("pid %d doesn't exist - deleting messages record\n", (int)pid));
142 tdb_delete(tdb, message_key_pid(pid));
144 DEBUG(2,("message to process %d failed - %s\n", (int)pid, strerror(errno)));
151 /****************************************************************************
152 Send a message to a particular pid.
153 ****************************************************************************/
155 BOOL message_send_pid(pid_t pid, int msg_type, const void *buf, size_t len,
156 BOOL duplicates_allowed)
160 struct message_rec rec;
163 rec.msg_version = MESSAGE_VERSION;
164 rec.msg_type = msg_type;
166 rec.src = sys_getpid();
169 /* Doing kill with a non-positive pid causes messages to be
170 * sent to places we don't want. */
173 kbuf = message_key_pid(pid);
175 /* lock the record for the destination */
176 tdb_chainlock(tdb, kbuf);
178 dbuf = tdb_fetch(tdb, kbuf);
181 /* its a new record */
182 p = (void *)malloc(len + sizeof(rec));
186 memcpy(p, &rec, sizeof(rec));
188 memcpy((void *)((char*)p+sizeof(rec)), buf, len);
191 dbuf.dsize = len + sizeof(rec);
192 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
197 if (!duplicates_allowed) {
199 struct message_rec prec;
201 for(ptr = (char *)dbuf.dptr; ptr < dbuf.dptr + dbuf.dsize; ) {
203 * First check if the message header matches, then, if it's a non-zero
204 * sized message, check if the data matches. If so it's a duplicate and
205 * we can discard it. JRA.
208 if (!memcmp(ptr, &rec, sizeof(rec))) {
209 if (!len || (len && !memcmp( ptr + sizeof(rec), buf, len))) {
210 DEBUG(10,("message_send_pid: discarding duplicate message.\n"));
211 SAFE_FREE(dbuf.dptr);
212 tdb_chainunlock(tdb, kbuf);
216 memcpy(&prec, ptr, sizeof(prec));
217 ptr += sizeof(rec) + prec.len;
221 /* we're adding to an existing entry */
222 p = (void *)malloc(dbuf.dsize + len + sizeof(rec));
226 memcpy(p, dbuf.dptr, dbuf.dsize);
227 memcpy((void *)((char*)p+dbuf.dsize), &rec, sizeof(rec));
229 memcpy((void *)((char*)p+dbuf.dsize+sizeof(rec)), buf, len);
231 SAFE_FREE(dbuf.dptr);
233 dbuf.dsize += len + sizeof(rec);
234 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
235 SAFE_FREE(dbuf.dptr);
238 tdb_chainunlock(tdb, kbuf);
239 errno = 0; /* paranoia */
240 return message_notify(pid);
243 tdb_chainunlock(tdb, kbuf);
244 errno = 0; /* paranoia */
248 /****************************************************************************
249 Retrieve the next message for the current process.
250 ****************************************************************************/
252 static BOOL message_recv(int *msg_type, pid_t *src, void **buf, size_t *len)
256 struct message_rec rec;
258 kbuf = message_key_pid(sys_getpid());
260 tdb_chainlock(tdb, kbuf);
262 dbuf = tdb_fetch(tdb, kbuf);
263 if (dbuf.dptr == NULL || dbuf.dsize == 0)
266 memcpy(&rec, dbuf.dptr, sizeof(rec));
268 if (rec.msg_version != MESSAGE_VERSION) {
269 DEBUG(0,("message version %d received (expected %d)\n", rec.msg_version, MESSAGE_VERSION));
274 (*buf) = (void *)malloc(rec.len);
278 memcpy(*buf, dbuf.dptr+sizeof(rec), rec.len);
284 *msg_type = rec.msg_type;
287 if (dbuf.dsize - (sizeof(rec)+rec.len) > 0)
288 memmove(dbuf.dptr, dbuf.dptr+sizeof(rec)+rec.len, dbuf.dsize - (sizeof(rec)+rec.len));
289 dbuf.dsize -= sizeof(rec)+rec.len;
292 tdb_delete(tdb, kbuf);
294 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
296 SAFE_FREE(dbuf.dptr);
297 tdb_chainunlock(tdb, kbuf);
301 tdb_chainunlock(tdb, kbuf);
302 SAFE_FREE(dbuf.dptr);
306 /****************************************************************************
307 Receive and dispatch any messages pending for this process.
308 Notice that all dispatch handlers for a particular msg_type get called,
309 so you can register multiple handlers for a message.
310 ****************************************************************************/
312 void message_dispatch(void)
318 struct dispatch_fns *dfn;
321 if (!received_signal) return;
323 DEBUG(10,("message_dispatch: received_signal = %d\n", received_signal));
327 while (message_recv(&msg_type, &src, &buf, &len)) {
328 DEBUG(10,("message_dispatch: received msg_type=%d src_pid=%d\n",
329 msg_type, (int) src));
331 for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
332 if (dfn->msg_type == msg_type) {
333 DEBUG(10,("message_dispatch: processing message of type %d.\n", msg_type));
334 dfn->fn(msg_type, src, buf, len);
339 DEBUG(5,("message_dispatch: warning: no handlers registered for "
340 "msg_type %d in pid %d\n",
341 msg_type, sys_getpid()));
347 /****************************************************************************
348 Register a dispatch function for a particular message type.
349 ****************************************************************************/
351 void message_register(int msg_type,
352 void (*fn)(int msg_type, pid_t pid, void *buf, size_t len))
354 struct dispatch_fns *dfn;
356 dfn = (struct dispatch_fns *)malloc(sizeof(*dfn));
362 dfn->msg_type = msg_type;
365 DLIST_ADD(dispatch_fns, dfn);
369 DEBUG(0,("message_register: Not enough memory. malloc failed!\n"));
373 /****************************************************************************
374 De-register the function for a particular message type.
375 ****************************************************************************/
377 void message_deregister(int msg_type)
379 struct dispatch_fns *dfn, *next;
381 for (dfn = dispatch_fns; dfn; dfn = next) {
383 if (dfn->msg_type == msg_type) {
384 DLIST_REMOVE(dispatch_fns, dfn);
399 /****************************************************************************
400 Send one of the messages for the broadcast.
401 ****************************************************************************/
403 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
405 struct connections_data crec;
406 struct msg_all *msg_all = (struct msg_all *)state;
408 if (dbuf.dsize != sizeof(crec))
411 memcpy(&crec, dbuf.dptr, sizeof(crec));
416 /* Don't send if the receiver hasn't registered an interest. */
418 if(!(crec.bcast_msg_flags & msg_all->msg_flag))
421 /* If the msg send fails because the pid was not found (i.e. smbd died),
422 * the msg has already been deleted from the messages.tdb.*/
424 if (!message_send_pid(crec.pid, msg_all->msg_type,
425 msg_all->buf, msg_all->len,
426 msg_all->duplicates)) {
428 /* If the pid was not found delete the entry from connections.tdb */
430 if (errno == ESRCH) {
431 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
432 (unsigned int)crec.pid, crec.cnum, crec.name));
433 tdb_delete(the_tdb, kbuf);
441 * Send a message to all smbd processes.
443 * It isn't very efficient, but should be OK for the sorts of
444 * applications that use it. When we need efficient broadcast we can add
447 * @param n_sent Set to the number of messages sent. This should be
448 * equal to the number of processes, but be careful for races.
450 * @return True for success.
452 BOOL message_send_all(TDB_CONTEXT *conn_tdb, int msg_type,
453 const void *buf, size_t len,
454 BOOL duplicates_allowed,
457 struct msg_all msg_all;
459 msg_all.msg_type = msg_type;
461 msg_all.msg_flag = FLAG_MSG_GENERAL;
462 else if (msg_type > 1000 && msg_type < 2000)
463 msg_all.msg_flag = FLAG_MSG_NMBD;
464 else if (msg_type > 2000 && msg_type < 3000)
465 msg_all.msg_flag = FLAG_MSG_PRINTING;
466 else if (msg_type > 3000 && msg_type < 4000)
467 msg_all.msg_flag = FLAG_MSG_SMBD;
473 msg_all.duplicates = duplicates_allowed;
476 tdb_traverse(conn_tdb, traverse_fn, &msg_all);
478 *n_sent = msg_all.n_sent;