2 Unix SMB/Netbios implementation.
4 Samba internal messaging functions
5 Copyright (C) Andrew Tridgell 2000
6 Copyright (C) 2001 by Martin Pool
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 @defgroups messages Internal messaging framework
28 This module is used 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 This system doesn't have any inherent size limitations but is not
40 very efficient for large messages or when messages are sent in very
47 /* the locking database handle */
48 static TDB_CONTEXT *tdb;
49 static int received_signal;
51 /* change the message version with any incompatible changes in the protocol */
52 #define MESSAGE_VERSION 1
62 /* we have a linked list of dispatch handlers */
63 static struct dispatch_fns {
64 struct dispatch_fns *next, *prev;
66 void (*fn)(int msg_type, pid_t pid, void *buf, size_t len);
69 /****************************************************************************
70 Notifications come in as signals.
71 ****************************************************************************/
73 static void sig_usr1(void)
79 /****************************************************************************
80 A useful function for testing the message system.
81 ****************************************************************************/
83 void ping_message(int msg_type, pid_t src, void *buf, size_t len)
85 char *msg = buf ? buf : "none";
86 DEBUG(1,("INFO: Received PING message from PID %u [%s]\n",(unsigned int)src, msg));
87 message_send_pid(src, MSG_PONG, buf, len, True);
90 /****************************************************************************
91 Return current debug level.
92 ****************************************************************************/
94 void debuglevel_message(int msg_type, pid_t src, void *buf, size_t len)
96 DEBUG(1,("INFO: Received REQ_DEBUGLEVEL message from PID %u\n",(unsigned int)src));
97 message_send_pid(src, MSG_DEBUGLEVEL, DEBUGLEVEL_CLASS, sizeof(DEBUGLEVEL_CLASS), True);
100 /****************************************************************************
101 Initialise the messaging functions.
102 ****************************************************************************/
104 BOOL message_init(void)
106 if (tdb) return True;
108 tdb = tdb_open_log(lock_path("messages.tdb"),
109 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
110 O_RDWR|O_CREAT,0600);
113 DEBUG(0,("ERROR: Failed to initialise messages database\n"));
117 CatchSignal(SIGUSR1, SIGNAL_CAST sig_usr1);
119 message_register(MSG_PING, ping_message);
120 message_register(MSG_REQ_DEBUGLEVEL, debuglevel_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)
148 if (kill(pid, SIGUSR1) == -1) {
149 if (errno == ESRCH) {
150 DEBUG(2,("pid %d doesn't exist - deleting messages record\n", (int)pid));
151 tdb_delete(tdb, message_key_pid(pid));
153 DEBUG(2,("message to process %d failed - %s\n", (int)pid, strerror(errno)));
160 /****************************************************************************
161 Send a message to a particular pid.
162 ****************************************************************************/
164 BOOL message_send_pid(pid_t pid, int msg_type, const void *buf, size_t len,
165 BOOL duplicates_allowed)
169 struct message_rec rec;
172 rec.msg_version = MESSAGE_VERSION;
173 rec.msg_type = msg_type;
175 rec.src = sys_getpid();
178 kbuf = message_key_pid(pid);
180 /* lock the record for the destination */
181 tdb_chainlock(tdb, kbuf);
183 dbuf = tdb_fetch(tdb, kbuf);
186 /* its a new record */
187 p = (void *)malloc(len + sizeof(rec));
190 memcpy(p, &rec, sizeof(rec));
191 if (len > 0) memcpy((void *)((char*)p+sizeof(rec)), buf, len);
194 dbuf.dsize = len + sizeof(rec);
195 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
200 if (!duplicates_allowed) {
202 struct message_rec prec;
204 for(ptr = (char *)dbuf.dptr; ptr < dbuf.dptr + dbuf.dsize; ) {
206 * First check if the message header matches, then, if it's a non-zero
207 * sized message, check if the data matches. If so it's a duplicate and
208 * we can discard it. JRA.
211 if (!memcmp(ptr, &rec, sizeof(rec))) {
212 if (!len || (len && !memcmp( ptr + sizeof(rec), buf, len))) {
213 DEBUG(10,("message_send_pid: discarding duplicate message.\n"));
214 SAFE_FREE(dbuf.dptr);
215 tdb_chainunlock(tdb, kbuf);
219 memcpy(&prec, ptr, sizeof(prec));
220 ptr += sizeof(rec) + prec.len;
224 /* we're adding to an existing entry */
225 p = (void *)malloc(dbuf.dsize + len + sizeof(rec));
228 memcpy(p, dbuf.dptr, dbuf.dsize);
229 memcpy((void *)((char*)p+dbuf.dsize), &rec, sizeof(rec));
230 if (len > 0) memcpy((void *)((char*)p+dbuf.dsize+sizeof(rec)), buf, len);
232 SAFE_FREE(dbuf.dptr);
234 dbuf.dsize += len + sizeof(rec);
235 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
236 SAFE_FREE(dbuf.dptr);
239 tdb_chainunlock(tdb, kbuf);
240 errno = 0; /* paranoia */
241 return message_notify(pid);
244 tdb_chainunlock(tdb, kbuf);
245 errno = 0; /* paranoia */
249 /****************************************************************************
250 Retrieve the next message for the current process.
251 ****************************************************************************/
253 static BOOL message_recv(int *msg_type, pid_t *src, void **buf, size_t *len)
257 struct message_rec rec;
259 kbuf = message_key_pid(sys_getpid());
261 tdb_chainlock(tdb, kbuf);
263 dbuf = tdb_fetch(tdb, kbuf);
264 if (dbuf.dptr == NULL || dbuf.dsize == 0) goto failed;
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);
275 if (!(*buf)) goto failed;
277 memcpy(*buf, dbuf.dptr+sizeof(rec), rec.len);
283 *msg_type = rec.msg_type;
286 if (dbuf.dsize - (sizeof(rec)+rec.len) > 0)
287 memmove(dbuf.dptr, dbuf.dptr+sizeof(rec)+rec.len, dbuf.dsize - (sizeof(rec)+rec.len));
288 dbuf.dsize -= sizeof(rec)+rec.len;
291 tdb_delete(tdb, kbuf);
293 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
295 SAFE_FREE(dbuf.dptr);
296 tdb_chainunlock(tdb, kbuf);
300 tdb_chainunlock(tdb, kbuf);
304 /****************************************************************************
305 Receive and dispatch any messages pending for this process.
306 Notice that all dispatch handlers for a particular msg_type get called,
307 so you can register multiple handlers for a message.
308 ****************************************************************************/
310 void message_dispatch(void)
316 struct dispatch_fns *dfn;
318 if (!received_signal) return;
320 DEBUG(10,("message_dispatch: received_signal = %d\n", received_signal));
324 while (message_recv(&msg_type, &src, &buf, &len)) {
325 for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
326 if (dfn->msg_type == msg_type) {
327 DEBUG(10,("message_dispatch: processing message of type %d.\n", msg_type));
328 dfn->fn(msg_type, src, buf, len);
335 /****************************************************************************
336 Register a dispatch function for a particular message type.
337 ****************************************************************************/
339 void message_register(int msg_type,
340 void (*fn)(int msg_type, pid_t pid, void *buf, size_t len))
342 struct dispatch_fns *dfn;
344 dfn = (struct dispatch_fns *)malloc(sizeof(*dfn));
350 dfn->msg_type = msg_type;
353 DLIST_ADD(dispatch_fns, dfn);
357 DEBUG(0,("message_register: Not enough memory. malloc failed!\n"));
361 /****************************************************************************
362 De-register the function for a particular message type.
363 ****************************************************************************/
365 void message_deregister(int msg_type)
367 struct dispatch_fns *dfn, *next;
369 for (dfn = dispatch_fns; dfn; dfn = next) {
371 if (dfn->msg_type == msg_type) {
372 DLIST_REMOVE(dispatch_fns, dfn);
386 /****************************************************************************
387 Send one of the messages for the broadcast.
388 ****************************************************************************/
390 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
392 struct connections_data crec;
393 struct msg_all *msg_all = (struct msg_all *)state;
395 if (dbuf.dsize != sizeof(crec))
398 memcpy(&crec, dbuf.dptr, sizeof(crec));
403 /* if the msg send fails because the pid was not found (i.e. smbd died),
404 * the msg has already been deleted from the messages.tdb.*/
405 if (!message_send_pid(crec.pid, msg_all->msg_type,
406 msg_all->buf, msg_all->len,
407 msg_all->duplicates)) {
409 /* if the pid was not found delete the entry from connections.tdb */
410 if (errno == ESRCH) {
411 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
412 (unsigned int)crec.pid, crec.cnum, crec.name));
413 tdb_delete(the_tdb, kbuf);
421 * Send a message to all smbd processes.
423 * It isn't very efficient, but should be OK for the sorts of
424 * applications that use it. When we need efficient broadcast we can add
427 * @param n_sent Set to the number of messages sent. This should be
428 * equal to the number of processes, but be careful for races.
430 * @return True for success.
432 BOOL message_send_all(TDB_CONTEXT *conn_tdb, int msg_type,
433 const void *buf, size_t len,
434 BOOL duplicates_allowed,
437 struct msg_all msg_all;
439 msg_all.msg_type = msg_type;
442 msg_all.duplicates = duplicates_allowed;
445 tdb_traverse(conn_tdb, traverse_fn, &msg_all);
447 *n_sent = msg_all.n_sent;