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));
185 memcpy(p, &rec, sizeof(rec));
186 if (len > 0) memcpy((void *)((char*)p+sizeof(rec)), buf, len);
189 dbuf.dsize = len + sizeof(rec);
190 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
195 if (!duplicates_allowed) {
197 struct message_rec prec;
199 for(ptr = (char *)dbuf.dptr; ptr < dbuf.dptr + dbuf.dsize; ) {
201 * First check if the message header matches, then, if it's a non-zero
202 * sized message, check if the data matches. If so it's a duplicate and
203 * we can discard it. JRA.
206 if (!memcmp(ptr, &rec, sizeof(rec))) {
207 if (!len || (len && !memcmp( ptr + sizeof(rec), buf, len))) {
208 DEBUG(10,("message_send_pid: discarding duplicate message.\n"));
209 SAFE_FREE(dbuf.dptr);
210 tdb_chainunlock(tdb, kbuf);
214 memcpy(&prec, ptr, sizeof(prec));
215 ptr += sizeof(rec) + prec.len;
219 /* we're adding to an existing entry */
220 p = (void *)malloc(dbuf.dsize + len + sizeof(rec));
223 memcpy(p, dbuf.dptr, dbuf.dsize);
224 memcpy((void *)((char*)p+dbuf.dsize), &rec, sizeof(rec));
225 if (len > 0) memcpy((void *)((char*)p+dbuf.dsize+sizeof(rec)), buf, len);
227 SAFE_FREE(dbuf.dptr);
229 dbuf.dsize += len + sizeof(rec);
230 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
231 SAFE_FREE(dbuf.dptr);
234 tdb_chainunlock(tdb, kbuf);
235 errno = 0; /* paranoia */
236 return message_notify(pid);
239 tdb_chainunlock(tdb, kbuf);
240 errno = 0; /* paranoia */
244 /****************************************************************************
245 Retrieve the next message for the current process.
246 ****************************************************************************/
248 static BOOL message_recv(int *msg_type, pid_t *src, void **buf, size_t *len)
252 struct message_rec rec;
254 kbuf = message_key_pid(sys_getpid());
256 tdb_chainlock(tdb, kbuf);
258 dbuf = tdb_fetch(tdb, kbuf);
259 if (dbuf.dptr == NULL || dbuf.dsize == 0) goto failed;
261 memcpy(&rec, dbuf.dptr, sizeof(rec));
263 if (rec.msg_version != MESSAGE_VERSION) {
264 DEBUG(0,("message version %d received (expected %d)\n", rec.msg_version, MESSAGE_VERSION));
269 (*buf) = (void *)malloc(rec.len);
270 if (!(*buf)) goto failed;
272 memcpy(*buf, dbuf.dptr+sizeof(rec), rec.len);
278 *msg_type = rec.msg_type;
281 if (dbuf.dsize - (sizeof(rec)+rec.len) > 0)
282 memmove(dbuf.dptr, dbuf.dptr+sizeof(rec)+rec.len, dbuf.dsize - (sizeof(rec)+rec.len));
283 dbuf.dsize -= sizeof(rec)+rec.len;
286 tdb_delete(tdb, kbuf);
288 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
290 SAFE_FREE(dbuf.dptr);
291 tdb_chainunlock(tdb, kbuf);
295 tdb_chainunlock(tdb, kbuf);
299 /****************************************************************************
300 Receive and dispatch any messages pending for this process.
301 Notice that all dispatch handlers for a particular msg_type get called,
302 so you can register multiple handlers for a message.
303 ****************************************************************************/
305 void message_dispatch(void)
311 struct dispatch_fns *dfn;
314 if (!received_signal) return;
316 DEBUG(10,("message_dispatch: received_signal = %d\n", received_signal));
320 while (message_recv(&msg_type, &src, &buf, &len)) {
321 DEBUG(10,("message_dispatch: received msg_type=%d src_pid=%d\n",
322 msg_type, (int) src));
324 for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
325 if (dfn->msg_type == msg_type) {
326 DEBUG(10,("message_dispatch: processing message of type %d.\n", msg_type));
327 dfn->fn(msg_type, src, buf, len);
332 DEBUG(5,("message_dispatch: warning: no handlers registered for "
333 "msg_type %d in pid%d\n",
334 msg_type, sys_getpid()));
340 /****************************************************************************
341 Register a dispatch function for a particular message type.
342 ****************************************************************************/
344 void message_register(int msg_type,
345 void (*fn)(int msg_type, pid_t pid, void *buf, size_t len))
347 struct dispatch_fns *dfn;
349 dfn = (struct dispatch_fns *)malloc(sizeof(*dfn));
355 dfn->msg_type = msg_type;
358 DLIST_ADD(dispatch_fns, dfn);
362 DEBUG(0,("message_register: Not enough memory. malloc failed!\n"));
366 /****************************************************************************
367 De-register the function for a particular message type.
368 ****************************************************************************/
370 void message_deregister(int msg_type)
372 struct dispatch_fns *dfn, *next;
374 for (dfn = dispatch_fns; dfn; dfn = next) {
376 if (dfn->msg_type == msg_type) {
377 DLIST_REMOVE(dispatch_fns, dfn);
391 /****************************************************************************
392 Send one of the messages for the broadcast.
393 ****************************************************************************/
395 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
397 struct connections_data crec;
398 struct msg_all *msg_all = (struct msg_all *)state;
400 if (dbuf.dsize != sizeof(crec))
403 memcpy(&crec, dbuf.dptr, sizeof(crec));
408 /* if the msg send fails because the pid was not found (i.e. smbd died),
409 * the msg has already been deleted from the messages.tdb.*/
410 if (!message_send_pid(crec.pid, msg_all->msg_type,
411 msg_all->buf, msg_all->len,
412 msg_all->duplicates)) {
414 /* if the pid was not found delete the entry from connections.tdb */
415 if (errno == ESRCH) {
416 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
417 (unsigned int)crec.pid, crec.cnum, crec.name));
418 tdb_delete(the_tdb, kbuf);
426 * Send a message to all smbd processes.
428 * It isn't very efficient, but should be OK for the sorts of
429 * applications that use it. When we need efficient broadcast we can add
432 * @param n_sent Set to the number of messages sent. This should be
433 * equal to the number of processes, but be careful for races.
435 * @return True for success.
437 BOOL message_send_all(TDB_CONTEXT *conn_tdb, int msg_type,
438 const void *buf, size_t len,
439 BOOL duplicates_allowed,
442 struct msg_all msg_all;
444 msg_all.msg_type = msg_type;
447 msg_all.duplicates = duplicates_allowed;
450 tdb_traverse(conn_tdb, traverse_fn, &msg_all);
452 *n_sent = msg_all.n_sent;
456 static VOLATILE sig_atomic_t gotalarm;
458 /***************************************************************
459 Signal function to tell us we timed out.
460 ****************************************************************/
462 static void gotalarm_sig(void)
468 * Lock the messaging tdb based on a string - this is used as a primitive
469 * form of mutex between smbd instances.
471 * @param name A string identifying the name of the mutex.
474 BOOL message_named_mutex(char *name, unsigned int timeout)
483 key.dsize = strlen(name)+1;
487 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
491 ret = tdb_chainlock(tdb, key);
495 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
501 DEBUG(10,("message_named_mutex: got mutex for %s\n", name ));
507 * Unlock a named mutex.
509 * @param name A string identifying the name of the mutex.
512 void message_named_mutex_release(char *name)
517 key.dsize = strlen(name)+1;
519 tdb_chainunlock(tdb, key);
520 DEBUG(10,("message_named_mutex: released mutex for %s\n", name ));