s3: messages: Implement cleanup of dead records.
authorJeremy Allison <jra@samba.org>
Wed, 2 Apr 2014 23:45:25 +0000 (16:45 -0700)
committerJeremy Allison <jra@samba.org>
Thu, 3 Apr 2014 04:22:13 +0000 (06:22 +0200)
When a smbd process dies, pending messages.tdb records for this process
might not get cleaned up. Implement a cleanup for dead records that is
triggered after a smbd dies uncleanly; the records for that PID are
deleted.

Based on a patchset from Christof Schmitt <cs@samba.org>.

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Christof Schmitt <cs@samba.org>
source3/include/messages.h
source3/lib/messages.c
source3/lib/messages_local.c
source3/smbd/server.c

index d7a28538cddb871dafbb6fbbcf3eb0bc9240a520..47c5f7a2d9d510d27f3d0f1a333dfee6f2296531 100644 (file)
@@ -97,6 +97,9 @@ NTSTATUS messaging_tdb_init(struct messaging_context *msg_ctx,
 
 bool messaging_tdb_parent_init(TALLOC_CTX *mem_ctx);
 
+NTSTATUS messaging_tdb_cleanup(struct messaging_context *msg_ctx,
+                       struct server_id pid);
+
 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
                              TALLOC_CTX *mem_ctx,
                              struct messaging_backend **presult);
@@ -143,6 +146,9 @@ struct tevent_req *messaging_read_send(TALLOC_CTX *mem_ctx,
 int messaging_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                        struct messaging_rec **presult);
 
+void messaging_cleanup_server(struct messaging_context *msg_ctx,
+                               struct server_id pid);
+
 #include "librpc/gen_ndr/ndr_messaging.h"
 
 #endif
index 96b6b88a808e76c85fc35b395346c0e1496b8e48..4ff933dc6e7a02c44d685b7615f9fc3915d591d7 100644 (file)
@@ -567,4 +567,21 @@ void messaging_dispatch_rec(struct messaging_context *msg_ctx,
        return;
 }
 
+/*
+  Call when a process has terminated abnormally.
+*/
+void messaging_cleanup_server(struct messaging_context *msg_ctx,
+                               struct server_id server)
+{
+       if (server_id_is_disconnected(&server)) {
+               return;
+       }
+
+       if (!procid_is_local(&server)) {
+               return;
+       }
+
+       (void)messaging_tdb_cleanup(msg_ctx, server);
+
+}
 /** @} **/
index 1fe89c3bfaacf013a80ff16d938a012a1c264ba7..d535df1be27d70552ba8365a3fc9de70f0e41a23 100644 (file)
@@ -45,6 +45,7 @@
 #include "includes.h"
 #include "system/filesys.h"
 #include "messages.h"
+#include "serverid.h"
 #include "lib/tdb_wrap/tdb_wrap.h"
 #include "lib/param/param.h"
 
@@ -221,6 +222,43 @@ static TDB_DATA message_key_pid(TALLOC_CTX *mem_ctx, struct server_id pid)
        return kbuf;
 }
 
+/*******************************************************************
+ Called when a process has terminated abnormally. Remove all messages
+ pending for it.
+******************************************************************/
+
+NTSTATUS messaging_tdb_cleanup(struct messaging_context *msg_ctx,
+                               struct server_id pid)
+{
+       struct messaging_tdb_context *ctx = talloc_get_type(
+                                       msg_ctx->local->private_data,
+                                       struct messaging_tdb_context);
+       struct tdb_wrap *tdb = ctx->tdb;
+       TDB_DATA key;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       key = message_key_pid(frame, pid);
+       /*
+        * We have to lock the key to avoid
+        * races in case the server_id was
+        * re-used and is active (a remote
+        * possibility, true). We only
+        * clean up the database if we
+        * know server_id doesn't exist
+        * while checked under the chainlock.
+        */
+       if (tdb_chainlock(tdb->tdb, key) != 0) {
+               TALLOC_FREE(frame);
+               return NT_STATUS_LOCK_NOT_GRANTED;
+       }
+       if (!serverid_exists(&pid)) {
+               (void)tdb_delete(tdb->tdb, key);
+       }
+       tdb_chainunlock(tdb->tdb, key);
+       TALLOC_FREE(frame);
+       return NT_STATUS_OK;
+}
+
 /*
   Fetch the messaging array for a process
  */
index 29e688d3f01fe4f2498256bc49ed6587d2034322..bc9d293b494aa28dbfa6ee4c99d9dad3fdfbc221 100644 (file)
@@ -481,6 +481,13 @@ static void remove_child_pid(struct smbd_parent_context *parent,
                                                parent);
                        DEBUG(1,("Scheduled cleanup of brl and lock database after unclean shutdown\n"));
                }
+
+               /*
+                * Ensure we flush any stored messages
+                * queued for the child process that
+                * terminated uncleanly.
+                */
+               messaging_cleanup_server(parent->msg_ctx, child_id);
        }
 
        if (!serverid_deregister(child_id)) {