messaging_dgm: Add messaging_dgm_wipe
authorVolker Lendecke <vl@samba.org>
Thu, 10 Apr 2014 20:07:11 +0000 (22:07 +0200)
committerJeremy Allison <jra@samba.org>
Wed, 23 Apr 2014 20:33:09 +0000 (22:33 +0200)
This walks all sockets and wipes the left-overs

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/include/messages.h
source3/lib/messages_dgm.c

index 3e1b664ce1d007846adafbacfe43529d4e06b012..8a818db57b0f6cd5dab4dd501072bec5c80f3164 100644 (file)
@@ -95,6 +95,7 @@ NTSTATUS messaging_dgm_init(struct messaging_context *msg_ctx,
                            TALLOC_CTX *mem_ctx,
                            struct messaging_backend **presult);
 NTSTATUS messaging_dgm_cleanup(struct messaging_context *msg_ctx, pid_t pid);
+NTSTATUS messaging_dgm_wipe(struct messaging_context *msg_ctx);
 
 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
                              TALLOC_CTX *mem_ctx,
index 8327f9d50566c0097cc02b7d376b5073323ed242..354dac36773b305815d3636a53bc566f976471cd 100644 (file)
@@ -407,3 +407,57 @@ NTSTATUS messaging_dgm_cleanup(struct messaging_context *msg_ctx, pid_t pid)
        TALLOC_FREE(lockfile_name);
        return NT_STATUS_OK;
 }
+
+NTSTATUS messaging_dgm_wipe(struct messaging_context *msg_ctx)
+{
+       struct messaging_dgm_context *ctx = talloc_get_type_abort(
+               msg_ctx->local->private_data, struct messaging_dgm_context);
+       char *msgdir_name;
+       DIR *msgdir;
+       struct dirent *dp;
+       pid_t our_pid = getpid();
+
+       /*
+        * We scan the socket directory and not the lock directory. Otherwise
+        * we would race against messaging_dgm_lockfile_create's open(O_CREAT)
+        * and fcntl(SETLK).
+        */
+
+       msgdir_name = talloc_asprintf(talloc_tos(), "%s/msg", ctx->cache_dir);
+       if (msgdir_name == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       msgdir = opendir(msgdir_name);
+       TALLOC_FREE(msgdir_name);
+       if (msgdir == NULL) {
+               return map_nt_error_from_unix(errno);
+       }
+
+       while ((dp = readdir(msgdir)) != NULL) {
+               NTSTATUS status;
+               unsigned long pid;
+
+               pid = strtoul(dp->d_name, NULL, 10);
+               if (pid == 0) {
+                       /*
+                        * . and .. and other malformed entries
+                        */
+                       continue;
+               }
+               if (pid == our_pid) {
+                       /*
+                        * fcntl(F_GETLK) will succeed for ourselves, we hold
+                        * that lock ourselves.
+                        */
+                       continue;
+               }
+
+               status = messaging_dgm_cleanup(msg_ctx, pid);
+               DEBUG(10, ("messaging_dgm_cleanup(%lu) returned %s\n",
+                          pid, nt_errstr(status)));
+       }
+       closedir(msgdir);
+
+       return NT_STATUS_OK;
+}