r22846: Chunk one to replace message_send_pid with messaging_send: Deep inside
[sfrench/samba-autobuild/.git] / source / lib / messages.c
index 7d0da4b158bc6912ad690d619ca5bd8e27dc44c7..7805f3c6d9ac77f37359545491c91a95a0636295 100644 (file)
@@ -3,6 +3,7 @@
    Samba internal messaging functions
    Copyright (C) Andrew Tridgell 2000
    Copyright (C) 2001 by Martin Pool
+   Copyright (C) 2002 by Jeremy Allison
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 */
 
 /**
-   @defgroups messages Internal messaging framework
-   @{
-   @file messages.c
-
-   This module is used for internal messaging between Samba daemons. 
+  @defgroup messages Internal messaging framework
+  @{
+  @file messages.c
+  
+  @brief  Module for internal messaging between Samba daemons. 
 
    The idea is that if a part of Samba wants to do communication with
    another Samba process then it will do a message_register() of a
@@ -35,6 +36,9 @@
    use that to reply by message_send_pid().  See ping_message() for a
    simple example.
 
+   @caution Dispatch functions must be able to cope with incoming
+   messages on an *odd* byte boundary.
+
    This system doesn't have any inherent size limitations but is not
    very efficient for large messages or when messages are sent in very
    quick succession.
@@ -53,8 +57,8 @@ static int received_signal;
 struct message_rec {
        int msg_version;
        int msg_type;
-       pid_t dest;
-       pid_t src;
+       struct server_id dest;
+       struct server_id src;
        size_t len;
 };
 
@@ -62,38 +66,51 @@ struct message_rec {
 static struct dispatch_fns {
        struct dispatch_fns *next, *prev;
        int msg_type;
-       void (*fn)(int msg_type, pid_t pid, void *buf, size_t len);
+       void (*fn)(int msg_type, struct server_id pid, void *buf, size_t len,
+                  void *private_data);
+       void *private_data;
 } *dispatch_fns;
 
 /****************************************************************************
Notifications come in as signals.
Free global objects.
 ****************************************************************************/
 
-static void sig_usr1(void)
+void gfree_messages(void)
 {
-       received_signal = 1;
-       sys_select_signal();
+       struct dispatch_fns *dfn, *next;
+
+       /* delete the dispatch_fns list */
+       dfn = dispatch_fns;
+       while( dfn ) {
+               next = dfn->next;
+               DLIST_REMOVE(dispatch_fns, dfn);
+               SAFE_FREE(dfn);
+               dfn = next;
+       }
 }
 
 /****************************************************************************
A useful function for testing the message system.
Notifications come in as signals.
 ****************************************************************************/
 
-void ping_message(int msg_type, pid_t src, void *buf, size_t len)
+static void sig_usr1(void)
 {
-       char *msg = buf ? buf : "none";
-       DEBUG(1,("INFO: Received PING message from PID %u [%s]\n",(unsigned int)src, msg));
-       message_send_pid(src, MSG_PONG, buf, len, True);
+       received_signal = 1;
+       sys_select_signal(SIGUSR1);
 }
 
 /****************************************************************************
Return current debug level.
A useful function for testing the message system.
 ****************************************************************************/
 
-void debuglevel_message(int msg_type, pid_t src, void *buf, size_t len)
+static void ping_message(int msg_type, struct server_id src,
+                        void *buf, size_t len, void *private_data)
 {
-       DEBUG(1,("INFO: Received REQ_DEBUGLEVEL message from PID %u\n",(unsigned int)src));
-       message_send_pid(src, MSG_DEBUGLEVEL, DEBUGLEVEL_CLASS, sizeof(DEBUGLEVEL_CLASS), True);
+       const char *msg = buf ? (const char *)buf : "none";
+
+       DEBUG(1,("INFO: Received PING message from PID %s [%s]\n",
+                procid_str_static(&src), msg));
+       message_send_pid(src, MSG_PONG, buf, len, True);
 }
 
 /****************************************************************************
@@ -102,7 +119,10 @@ void debuglevel_message(int msg_type, pid_t src, void *buf, size_t len)
 
 BOOL message_init(void)
 {
-       if (tdb) return True;
+       sec_init();
+
+       if (tdb)
+               return True;
 
        tdb = tdb_open_log(lock_path("messages.tdb"), 
                       0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
@@ -113,10 +133,17 @@ BOOL message_init(void)
                return False;
        }
 
+       /* Activate the per-hashchain freelist */
+       tdb_set_max_dead(tdb, 5);
+
        CatchSignal(SIGUSR1, SIGNAL_CAST sig_usr1);
 
-       message_register(MSG_PING, ping_message);
-       message_register(MSG_REQ_DEBUGLEVEL, debuglevel_message);
+       message_register(MSG_PING, ping_message, NULL);
+
+       /* Register some debugging related messages */
+
+       register_msg_pool_usage();
+       register_dmalloc_msgs();
 
        return True;
 }
@@ -125,14 +152,14 @@ BOOL message_init(void)
  Form a static tdb key from a pid.
 ******************************************************************/
 
-static TDB_DATA message_key_pid(pid_t pid)
+static TDB_DATA message_key_pid(struct server_id pid)
 {
        static char key[20];
        TDB_DATA kbuf;
 
-       slprintf(key, sizeof(key)-1, "PID/%d", (int)pid);
+       slprintf(key, sizeof(key)-1, "PID/%s", procid_str_static(&pid));
        
-       kbuf.dptr = (char *)key;
+       kbuf.dptr = (uint8 *)key;
        kbuf.dsize = strlen(key)+1;
        return kbuf;
 }
@@ -142,222 +169,404 @@ static TDB_DATA message_key_pid(pid_t pid)
  then delete its record in the database.
 ****************************************************************************/
 
-static BOOL message_notify(pid_t pid)
+static NTSTATUS message_notify(struct server_id procid)
 {
-       /* Doing kill with a non-positive pid causes messages to be
-        * sent to places we don't want. */
+       pid_t pid = procid.pid;
+       int ret;
+       uid_t euid = geteuid();
+
+       /*
+        * Doing kill with a non-positive pid causes messages to be
+        * sent to places we don't want.
+        */
+
        SMB_ASSERT(pid > 0);
-       if (kill(pid, SIGUSR1) == -1) {
+
+       if (euid != 0) {
+               /* If we're not root become so to send the message. */
+               save_re_uid();
+               set_effective_uid(0);
+       }
+
+       ret = kill(pid, SIGUSR1);
+
+       if (euid != 0) {
+               /* Go back to who we were. */
+               int saved_errno = errno;
+               restore_re_uid_fromroot();
+               errno = saved_errno;
+       }
+
+       if (ret == -1) {
                if (errno == ESRCH) {
-                       DEBUG(2,("pid %d doesn't exist - deleting messages record\n", (int)pid));
-                       tdb_delete(tdb, message_key_pid(pid));
-               } else {
-                       DEBUG(2,("message to process %d failed - %s\n", (int)pid, strerror(errno)));
+                       DEBUG(2,("pid %d doesn't exist - deleting messages record\n",
+                                (int)pid));
+                       tdb_delete(tdb, message_key_pid(procid));
+
+                       /*
+                        * INVALID_HANDLE is the closest I can think of -- vl
+                        */
+                       return NT_STATUS_INVALID_HANDLE;
                }
-               return False;
+
+               DEBUG(2,("message to process %d failed - %s\n", (int)pid,
+                        strerror(errno)));
+
+               /*
+                * No call to map_nt_error_from_unix -- don't want to link in
+                * errormap.o into lots of utils.
+                */
+
+               if (errno == EINVAL) return NT_STATUS_INVALID_PARAMETER;
+               if (errno == EPERM)  return NT_STATUS_ACCESS_DENIED;
+               return NT_STATUS_UNSUCCESSFUL;
        }
-       return True;
+
+       return NT_STATUS_OK;
 }
 
 /****************************************************************************
  Send a message to a particular pid.
 ****************************************************************************/
 
-BOOL message_send_pid(pid_t pid, int msg_type, const void *buf, size_t len,
-                     BOOL duplicates_allowed)
+static NTSTATUS message_send_pid_internal(struct server_id pid, int msg_type,
+                                         const void *buf, size_t len,
+                                         BOOL duplicates_allowed,
+                                         unsigned int timeout)
 {
        TDB_DATA kbuf;
        TDB_DATA dbuf;
+       TDB_DATA old_dbuf;
        struct message_rec rec;
-       void *p;
+       uint8 *ptr;
+       struct message_rec prec;
+
+       /* NULL pointer means implicit length zero. */
+       if (!buf) {
+               SMB_ASSERT(len == 0);
+       }
+
+       /*
+        * Doing kill with a non-positive pid causes messages to be
+        * sent to places we don't want.
+        */
+
+       SMB_ASSERT(procid_to_pid(&pid) > 0);
 
        rec.msg_version = MESSAGE_VERSION;
        rec.msg_type = msg_type;
        rec.dest = pid;
-       rec.src = sys_getpid();
-       rec.len = len;
-
-       /* Doing kill with a non-positive pid causes messages to be
-        * sent to places we don't want. */
-       SMB_ASSERT(pid > 0);
+       rec.src = procid_self();
+       rec.len = buf ? len : 0;
 
        kbuf = message_key_pid(pid);
 
+       dbuf.dptr = (uint8 *)SMB_MALLOC(len + sizeof(rec));
+       if (!dbuf.dptr) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       memcpy(dbuf.dptr, &rec, sizeof(rec));
+       if (len > 0 && buf)
+               memcpy((void *)((char*)dbuf.dptr+sizeof(rec)), buf, len);
+
+       dbuf.dsize = len + sizeof(rec);
+
+       if (duplicates_allowed) {
+
+               /* If duplicates are allowed we can just append the message and return. */
+
+               /* lock the record for the destination */
+               if (timeout) {
+                       if (tdb_chainlock_with_timeout(tdb, kbuf, timeout) == -1) {
+                               DEBUG(0,("message_send_pid_internal: failed to get "
+                                        "chainlock with timeout %ul.\n", timeout));
+                               return NT_STATUS_IO_TIMEOUT;
+                       }
+               } else {
+                       if (tdb_chainlock(tdb, kbuf) == -1) {
+                               DEBUG(0,("message_send_pid_internal: failed to get "
+                                        "chainlock.\n"));
+                               return NT_STATUS_LOCK_NOT_GRANTED;
+                       }
+               }       
+               tdb_append(tdb, kbuf, dbuf);
+               tdb_chainunlock(tdb, kbuf);
+
+               SAFE_FREE(dbuf.dptr);
+               errno = 0;                    /* paranoia */
+               return message_notify(pid);
+       }
+
        /* lock the record for the destination */
-       tdb_chainlock(tdb, kbuf);
+       if (timeout) {
+               if (tdb_chainlock_with_timeout(tdb, kbuf, timeout) == -1) {
+                       DEBUG(0,("message_send_pid_internal: failed to get chainlock "
+                                "with timeout %ul.\n", timeout));
+                       return NT_STATUS_IO_TIMEOUT;
+               }
+       } else {
+               if (tdb_chainlock(tdb, kbuf) == -1) {
+                       DEBUG(0,("message_send_pid_internal: failed to get "
+                                "chainlock.\n"));
+                       return NT_STATUS_LOCK_NOT_GRANTED;
+               }
+       }       
 
-       dbuf = tdb_fetch(tdb, kbuf);
+       old_dbuf = tdb_fetch(tdb, kbuf);
 
-       if (!dbuf.dptr) {
+       if (!old_dbuf.dptr) {
                /* its a new record */
-               p = (void *)malloc(len + sizeof(rec));
-               if (!p) goto failed;
 
-               memcpy(p, &rec, sizeof(rec));
-               if (len > 0) memcpy((void *)((char*)p+sizeof(rec)), buf, len);
-
-               dbuf.dptr = p;
-               dbuf.dsize = len + sizeof(rec);
                tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
-               SAFE_FREE(p);
-               goto ok;
-       }
+               tdb_chainunlock(tdb, kbuf);
 
-       if (!duplicates_allowed) {
-               char *ptr;
-               struct message_rec prec;
-               
-               for(ptr = (char *)dbuf.dptr; ptr < dbuf.dptr + dbuf.dsize; ) {
-                       /*
-                        * First check if the message header matches, then, if it's a non-zero
-                        * sized message, check if the data matches. If so it's a duplicate and
-                        * we can discard it. JRA.
-                        */
+               SAFE_FREE(dbuf.dptr);
+               errno = 0;                    /* paranoia */
+               return message_notify(pid);
+       }
 
-                       if (!memcmp(ptr, &rec, sizeof(rec))) {
-                               if (!len || (len && !memcmp( ptr + sizeof(rec), buf, len))) {
-                                       DEBUG(10,("message_send_pid: discarding duplicate message.\n"));
-                                       SAFE_FREE(dbuf.dptr);
-                                       tdb_chainunlock(tdb, kbuf);
-                                       return True;
-                               }
+       /* Not a new record. Check for duplicates. */
+
+       for(ptr = old_dbuf.dptr; ptr < old_dbuf.dptr + old_dbuf.dsize; ) {
+               /*
+                * First check if the message header matches, then, if it's a non-zero
+                * sized message, check if the data matches. If so it's a duplicate and
+                * we can discard it. JRA.
+                */
+
+               if (!memcmp(ptr, &rec, sizeof(rec))) {
+                       if (!len || (len && !memcmp( ptr + sizeof(rec), buf, len))) {
+                               tdb_chainunlock(tdb, kbuf);
+                               DEBUG(10,("message_send_pid_internal: discarding "
+                                         "duplicate message.\n"));
+                               SAFE_FREE(dbuf.dptr);
+                               SAFE_FREE(old_dbuf.dptr);
+                               return NT_STATUS_OK;
                        }
-                       memcpy(&prec, ptr, sizeof(prec));
-                       ptr += sizeof(rec) + prec.len;
                }
+               memcpy(&prec, ptr, sizeof(prec));
+               ptr += sizeof(rec) + prec.len;
        }
 
        /* we're adding to an existing entry */
-       p = (void *)malloc(dbuf.dsize + len + sizeof(rec));
-       if (!p) goto failed;
 
-       memcpy(p, dbuf.dptr, dbuf.dsize);
-       memcpy((void *)((char*)p+dbuf.dsize), &rec, sizeof(rec));
-       if (len > 0) memcpy((void *)((char*)p+dbuf.dsize+sizeof(rec)), buf, len);
+       tdb_append(tdb, kbuf, dbuf);
+       tdb_chainunlock(tdb, kbuf);
 
-       SAFE_FREE(dbuf.dptr);
-       dbuf.dptr = p;
-       dbuf.dsize += len + sizeof(rec);
-       tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
+       SAFE_FREE(old_dbuf.dptr);
        SAFE_FREE(dbuf.dptr);
 
- ok:
-       tdb_chainunlock(tdb, kbuf);
        errno = 0;                    /* paranoia */
        return message_notify(pid);
+}
 
- failed:
-       tdb_chainunlock(tdb, kbuf);
-       errno = 0;                    /* paranoia */
-       return False;
+/****************************************************************************
+ Send a message to a particular pid - no timeout.
+****************************************************************************/
+
+NTSTATUS message_send_pid(struct server_id pid, int msg_type, const void *buf,
+                         size_t len, BOOL duplicates_allowed)
+{
+       return message_send_pid_internal(pid, msg_type, buf, len,
+                                        duplicates_allowed, 0);
 }
 
 /****************************************************************************
Retrieve the next message for the current process.
Send a message to a particular pid, with timeout in seconds.
 ****************************************************************************/
 
-static BOOL message_recv(int *msg_type, pid_t *src, void **buf, size_t *len)
+NTSTATUS message_send_pid_with_timeout(struct server_id pid, int msg_type,
+                                      const void *buf, size_t len,
+                                      BOOL duplicates_allowed, unsigned int timeout)
+{
+       return message_send_pid_internal(pid, msg_type, buf, len, duplicates_allowed,
+                                        timeout);
+}
+
+/****************************************************************************
+ Count the messages pending for a particular pid. Expensive....
+****************************************************************************/
+
+unsigned int messages_pending_for_pid(struct server_id pid)
 {
        TDB_DATA kbuf;
        TDB_DATA dbuf;
-       struct message_rec rec;
+       uint8 *buf;
+       unsigned int message_count = 0;
 
-       kbuf = message_key_pid(sys_getpid());
+       kbuf = message_key_pid(pid);
 
-       tdb_chainlock(tdb, kbuf);
-       
        dbuf = tdb_fetch(tdb, kbuf);
-       if (dbuf.dptr == NULL || dbuf.dsize == 0) goto failed;
+       if (dbuf.dptr == NULL || dbuf.dsize == 0) {
+               SAFE_FREE(dbuf.dptr);
+               return 0;
+       }
+
+       for (buf = dbuf.dptr; dbuf.dsize > sizeof(struct message_rec);) {
+               struct message_rec rec;
+               memcpy(&rec, buf, sizeof(rec));
+               buf += (sizeof(rec) + rec.len);
+               dbuf.dsize -= (sizeof(rec) + rec.len);
+               message_count++;
+       }
+
+       SAFE_FREE(dbuf.dptr);
+       return message_count;
+}
 
-       memcpy(&rec, dbuf.dptr, sizeof(rec));
+/****************************************************************************
+ Retrieve all messages for the current process.
+****************************************************************************/
+
+static BOOL retrieve_all_messages(char **msgs_buf, size_t *total_len)
+{
+       TDB_DATA kbuf;
+       TDB_DATA dbuf;
+       TDB_DATA null_dbuf;
+
+       ZERO_STRUCT(null_dbuf);
+
+       *msgs_buf = NULL;
+       *total_len = 0;
+
+       kbuf = message_key_pid(pid_to_procid(sys_getpid()));
+
+       if (tdb_chainlock(tdb, kbuf) == -1)
+               return False;
+
+       dbuf = tdb_fetch(tdb, kbuf);
+       /*
+        * Replace with an empty record to keep the allocated
+        * space in the tdb.
+        */
+       tdb_store(tdb, kbuf, null_dbuf, TDB_REPLACE);
+       tdb_chainunlock(tdb, kbuf);
+
+       if (dbuf.dptr == NULL || dbuf.dsize == 0) {
+               SAFE_FREE(dbuf.dptr);
+               return False;
+       }
+
+       *msgs_buf = (char *)dbuf.dptr;
+       *total_len = dbuf.dsize;
+
+       return True;
+}
+
+/****************************************************************************
+ Parse out the next message for the current process.
+****************************************************************************/
+
+static BOOL message_recv(char *msgs_buf, size_t total_len, int *msg_type,
+                        struct server_id *src, char **buf, size_t *len)
+{
+       struct message_rec rec;
+       char *ret_buf = *buf;
+
+       *buf = NULL;
+       *len = 0;
+
+       if (total_len - (ret_buf - msgs_buf) < sizeof(rec))
+               return False;
+
+       memcpy(&rec, ret_buf, sizeof(rec));
+       ret_buf += sizeof(rec);
 
        if (rec.msg_version != MESSAGE_VERSION) {
                DEBUG(0,("message version %d received (expected %d)\n", rec.msg_version, MESSAGE_VERSION));
-               goto failed;
+               return False;
        }
 
        if (rec.len > 0) {
-               (*buf) = (void *)malloc(rec.len);
-               if (!(*buf)) goto failed;
-
-               memcpy(*buf, dbuf.dptr+sizeof(rec), rec.len);
-       } else {
-               *buf = NULL;
+               if (total_len - (ret_buf - msgs_buf) < rec.len)
+                       return False;
        }
 
        *len = rec.len;
        *msg_type = rec.msg_type;
        *src = rec.src;
+       *buf = ret_buf;
 
-       if (dbuf.dsize - (sizeof(rec)+rec.len) > 0)
-               memmove(dbuf.dptr, dbuf.dptr+sizeof(rec)+rec.len, dbuf.dsize - (sizeof(rec)+rec.len));
-       dbuf.dsize -= sizeof(rec)+rec.len;
-
-       if (dbuf.dsize == 0)
-               tdb_delete(tdb, kbuf);
-       else
-               tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
-
-       SAFE_FREE(dbuf.dptr);
-       tdb_chainunlock(tdb, kbuf);
        return True;
-
- failed:
-       tdb_chainunlock(tdb, kbuf);
-       return False;
 }
 
 /****************************************************************************
  Receive and dispatch any messages pending for this process.
- Notice that all dispatch handlers for a particular msg_type get called,
- so you can register multiple handlers for a message.
+ JRA changed Dec 13 2006. Only one message handler now permitted per type.
+ *NOTE*: Dispatch functions must be able to cope with incoming
+ messages on an *odd* byte boundary.
 ****************************************************************************/
 
 void message_dispatch(void)
 {
        int msg_type;
-       pid_t src;
-       void *buf;
-       size_t len;
-       struct dispatch_fns *dfn;
+       struct server_id src;
+       char *buf;
+       char *msgs_buf;
+       size_t len, total_len;
        int n_handled;
 
-       if (!received_signal) return;
+       if (!received_signal)
+               return;
 
        DEBUG(10,("message_dispatch: received_signal = %d\n", received_signal));
 
        received_signal = 0;
 
-       while (message_recv(&msg_type, &src, &buf, &len)) {
-               DEBUG(10,("message_dispatch: received msg_type=%d src_pid=%d\n",
-                         msg_type, (int) src));
+       if (!retrieve_all_messages(&msgs_buf, &total_len))
+               return;
+
+       for (buf = msgs_buf; message_recv(msgs_buf, total_len, &msg_type, &src, &buf, &len); buf += len) {
+               struct dispatch_fns *dfn;
+
+               DEBUG(10,("message_dispatch: received msg_type=%d "
+                         "src_pid=%u\n", msg_type,
+                         (unsigned int) procid_to_pid(&src)));
+
                n_handled = 0;
                for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
                        if (dfn->msg_type == msg_type) {
                                DEBUG(10,("message_dispatch: processing message of type %d.\n", msg_type));
-                               dfn->fn(msg_type, src, buf, len);
+                               dfn->fn(msg_type, src,
+                                       len ? (void *)buf : NULL, len,
+                                       dfn->private_data);
                                n_handled++;
+                               break;
                        }
                }
                if (!n_handled) {
-                       DEBUG(5,("message_dispatch: warning: no handlers registed for "
-                                "msg_type %d in pid%d\n",
-                                msg_type, sys_getpid()));
+                       DEBUG(5,("message_dispatch: warning: no handler registed for "
+                                "msg_type %d in pid %u\n",
+                                msg_type, (unsigned int)sys_getpid()));
                }
-               SAFE_FREE(buf);
        }
+       SAFE_FREE(msgs_buf);
 }
 
 /****************************************************************************
- Register a dispatch function for a particular message type.
+ Register/replace a dispatch function for a particular message type.
+ JRA changed Dec 13 2006. Only one message handler now permitted per type.
+ *NOTE*: Dispatch functions must be able to cope with incoming
+ messages on an *odd* byte boundary.
 ****************************************************************************/
 
 void message_register(int msg_type, 
-                     void (*fn)(int msg_type, pid_t pid, void *buf, size_t len))
+                     void (*fn)(int msg_type, struct server_id pid,
+                                void *buf, size_t len,
+                                void *private_data),
+                     void *private_data)
 {
        struct dispatch_fns *dfn;
 
-       dfn = (struct dispatch_fns *)malloc(sizeof(*dfn));
+       for (dfn = dispatch_fns; dfn; dfn = dfn->next) {
+               if (dfn->msg_type == msg_type) {
+                       dfn->fn = fn;
+                       return;
+               }
+       }
+
+       dfn = SMB_MALLOC_P(struct dispatch_fns);
 
        if (dfn != NULL) {
 
@@ -365,6 +574,7 @@ void message_register(int msg_type,
 
                dfn->msg_type = msg_type;
                dfn->fn = fn;
+               dfn->private_data = private_data;
 
                DLIST_ADD(dispatch_fns, dfn);
        }
@@ -387,16 +597,18 @@ void message_deregister(int msg_type)
                if (dfn->msg_type == msg_type) {
                        DLIST_REMOVE(dispatch_fns, dfn);
                        SAFE_FREE(dfn);
+                       return;
                }
        }       
 }
 
 struct msg_all {
        int msg_type;
+       uint32 msg_flag;
        const void *buf;
        size_t len;
        BOOL duplicates;
-       int             n_sent;
+       int n_sent;
 };
 
 /****************************************************************************
@@ -407,6 +619,7 @@ static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void
 {
        struct connections_data crec;
        struct msg_all *msg_all = (struct msg_all *)state;
+       NTSTATUS status;
 
        if (dbuf.dsize != sizeof(crec))
                return 0;
@@ -416,18 +629,25 @@ static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void
        if (crec.cnum != -1)
                return 0;
 
-       /* if the msg send fails because the pid was not found (i.e. smbd died), 
+       /* Don't send if the receiver hasn't registered an interest. */
+
+       if(!(crec.bcast_msg_flags & msg_all->msg_flag))
+               return 0;
+
+       /* If the msg send fails because the pid was not found (i.e. smbd died), 
         * the msg has already been deleted from the messages.tdb.*/
-       if (!message_send_pid(crec.pid, msg_all->msg_type,
-                             msg_all->buf, msg_all->len,
-                             msg_all->duplicates)) {
+
+       status = message_send_pid(crec.pid, msg_all->msg_type,
+                                 msg_all->buf, msg_all->len,
+                                 msg_all->duplicates);
+
+       if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
                
-               /* if the pid was not found delete the entry from connections.tdb */
-               if (errno == ESRCH) {
-                       DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
-                                       (unsigned int)crec.pid, crec.cnum, crec.name));
-                       tdb_delete(the_tdb, kbuf);
-               }
+               /* If the pid was not found delete the entry from connections.tdb */
+
+               DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
+                        procid_str_static(&crec.pid), crec.cnum, crec.servicename));
+               tdb_delete(the_tdb, kbuf);
        }
        msg_all->n_sent++;
        return 0;
@@ -443,9 +663,9 @@ static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void
  * @param n_sent Set to the number of messages sent.  This should be
  * equal to the number of processes, but be careful for races.
  *
- * @return True for success.
+ * @retval True for success.
  **/
-BOOL message_send_all(TDB_CONTEXT *conn_tdb, int msg_type,
+BOOL message_send_all(int msg_type,
                      const void *buf, size_t len,
                      BOOL duplicates_allowed,
                      int *n_sent)
@@ -453,78 +673,171 @@ BOOL message_send_all(TDB_CONTEXT *conn_tdb, int msg_type,
        struct msg_all msg_all;
 
        msg_all.msg_type = msg_type;
+       if (msg_type < 1000)
+               msg_all.msg_flag = FLAG_MSG_GENERAL;
+       else if (msg_type > 1000 && msg_type < 2000)
+               msg_all.msg_flag = FLAG_MSG_NMBD;
+       else if (msg_type > 2000 && msg_type < 2100)
+               msg_all.msg_flag = FLAG_MSG_PRINT_NOTIFY;
+       else if (msg_type > 2100 && msg_type < 3000)
+               msg_all.msg_flag = FLAG_MSG_PRINT_GENERAL;
+       else if (msg_type > 3000 && msg_type < 4000)
+               msg_all.msg_flag = FLAG_MSG_SMBD;
+       else
+               return False;
+
        msg_all.buf = buf;
        msg_all.len = len;
        msg_all.duplicates = duplicates_allowed;
        msg_all.n_sent = 0;
 
-       tdb_traverse(conn_tdb, traverse_fn, &msg_all);
+       connections_traverse(traverse_fn, &msg_all);
        if (n_sent)
                *n_sent = msg_all.n_sent;
        return True;
 }
 
-static VOLATILE sig_atomic_t gotalarm;
+/*
+ * Block and unblock receiving of messages. Allows removal of race conditions
+ * when doing a fork and changing message disposition.
+ */
 
-/***************************************************************
- Signal function to tell us we timed out.
-****************************************************************/
+void message_block(void)
+{
+       BlockSignals(True, SIGUSR1);
+}
 
-static void gotalarm_sig(void)
+void message_unblock(void)
 {
-       gotalarm = 1;
+       BlockSignals(False, SIGUSR1);
 }
 
-/**
- * Lock the messaging tdb based on a string - this is used as a primitive
- * form of mutex between smbd instances. 
- *
- * @param name A string identifying the name of the mutex.
+/*
+ * Samba4 API wrapper around the Samba3 implementation. Yes, I know, we could
+ * import the whole Samba4 thing, but I want notify.c from Samba4 in first.
  */
 
-BOOL message_named_mutex(char *name, unsigned int timeout)
-{
-       TDB_DATA key;
-       int ret;
-
-       if (!message_init())
-               return False;
+struct messaging_callback {
+       struct messaging_callback *prev, *next;
+       uint32 msg_type;
+       void (*fn)(struct messaging_context *msg, void *private_data, 
+                  uint32_t msg_type, 
+                  struct server_id server_id, DATA_BLOB *data);
+       void *private_data;
+};
 
-       key.dptr = name;
-       key.dsize = strlen(name)+1;
+struct messaging_context {
+       struct server_id id;
+       struct messaging_callback *callbacks;
+};
 
-       if (timeout) {
-               gotalarm = 0;
-               CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
-               alarm(timeout);
+static int messaging_context_destructor(struct messaging_context *ctx)
+{
+       struct messaging_callback *cb;
+
+       for (cb = ctx->callbacks; cb; cb = cb->next) {
+               /*
+                * We unconditionally remove all instances of our callback
+                * from the tdb basis.
+                */
+               message_deregister(cb->msg_type);
        }
+       return 0;
+}
 
-       ret = tdb_chainlock(tdb, key);
+struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, 
+                                        struct server_id server_id, 
+                                        struct event_context *ev)
+{
+       struct messaging_context *ctx;
 
-       if (timeout) {
-               alarm(0);
-               CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
-               if (gotalarm)
-                       return False;
+       if (!(ctx = TALLOC_ZERO_P(mem_ctx, struct messaging_context))) {
+               return NULL;
        }
 
-       return (ret == 0);
+       ctx->id = server_id;
+       talloc_set_destructor(ctx, messaging_context_destructor);
+       return ctx;
 }
 
-/**
- * Unlock a named mutex.
- *
- * @param name A string identifying the name of the mutex.
- */
+static void messaging_callback(int msg_type, struct server_id pid,
+                              void *buf, size_t len, void *private_data)
+{
+       struct messaging_context *ctx = talloc_get_type_abort(
+               private_data, struct messaging_context);
+       struct messaging_callback *cb, *next;
+
+       for (cb = ctx->callbacks; cb; cb = next) {
+               /*
+                * Allow a callback to remove itself
+                */
+               next = cb->next;
+
+               if (msg_type == cb->msg_type) {
+                       DATA_BLOB blob;
 
-void message_named_mutex_release(char *name)
+                       blob.data = (uint8 *)buf;
+                       blob.length = len;
+
+                       cb->fn(ctx, cb->private_data, msg_type, pid, &blob);
+               }
+       }
+}
+
+/*
+ * Register a dispatch function for a particular message type. Allow multiple
+ * registrants
+*/
+NTSTATUS messaging_register(struct messaging_context *ctx, void *private_data,
+                           uint32_t msg_type,
+                           void (*fn)(struct messaging_context *msg,
+                                      void *private_data, 
+                                      uint32_t msg_type, 
+                                      struct server_id server_id,
+                                      DATA_BLOB *data))
 {
-       TDB_DATA key;
+       struct messaging_callback *cb;
 
-       key.dptr = name;
-       key.dsize = strlen(name)+1;
+       if (!(cb = talloc(ctx, struct messaging_callback))) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       cb->msg_type = msg_type;
+       cb->fn = fn;
+       cb->private_data = private_data;
+
+       DLIST_ADD(ctx->callbacks, cb);
+       message_register(msg_type, messaging_callback, ctx);
+       return NT_STATUS_OK;
+}
+
+/*
+  De-register the function for a particular message type.
+*/
+void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
+                         void *private_data)
+{
+       struct messaging_callback *cb, *next;
+
+       for (cb = ctx->callbacks; cb; cb = next) {
+               next = cb->next;
+               if ((cb->msg_type == msg_type)
+                   && (cb->private_data == private_data)) {
+                       DLIST_REMOVE(ctx->callbacks, cb);
+                       TALLOC_FREE(cb);
+               }
+       }
+}
 
-       tdb_chainunlock(tdb, key);
+/*
+  Send a message to a particular server
+*/
+NTSTATUS messaging_send(struct messaging_context *msg,
+                       struct server_id server, 
+                       uint32_t msg_type, const DATA_BLOB *data)
+{
+       return message_send_pid_internal(server, msg_type, data->data,
+                                        data->length, True, 0);
 }
 
 /** @} **/