From 63f86cac09d369c68af5425ddba29464778761cc Mon Sep 17 00:00:00 2001
From: Sean Bright <sean.bright@gmail.com>
Date: Tue, 9 Apr 2019 11:10:12 -0400
Subject: [PATCH] app_voicemail: Cleanup stale lock files on module load

If Asterisk crashes while a VM directory is locked, lock files in the VM
spool directory will not get properly cleaned up. We now clear them on
module load.

ASTERISK-20207 #close
Reported by: Steven Wheeler

Change-Id: If40ccd508e2f6e5ade94dde2f0bcef99056d0aaf
---
 apps/app_voicemail.c | 51 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c
index a151f0cb96..0e24731003 100644
--- a/apps/app_voicemail.c
+++ b/apps/app_voicemail.c
@@ -3812,6 +3812,54 @@ static void check_quota(struct vm_state *vms, char *mailbox) {
 
 #endif /* IMAP_STORAGE */
 
+static void cleanup_orphaned_lock_files(const char *base)
+{
+	DIR *dir;
+	struct dirent *e;
+
+	dir = opendir(base);
+	if (!dir) {
+		/* Don't complain about this too loudly */
+		ast_debug(2, "Unable to open `%s': %s\n", base, strerror(errno));
+		return;
+	}
+
+	while ((e = readdir(dir))) {
+		char *fullpath;
+		struct stat s;
+
+		/* Always skip . and .. */
+		if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) {
+			continue;
+		}
+
+		/* Build up the full path (using dynamic memory because PATH_MAX is crap) */
+		if (ast_asprintf(&fullpath, "%s/%s", base, e->d_name) == -1) {
+			break;
+		}
+
+		if (lstat(fullpath, &s) < 0) {
+			ast_free(fullpath);
+			continue;
+		}
+
+		/* This is exposing an implementation detail of ast_lock_path, but it makes
+		 * our life a bit easier */
+		if (!strcmp(e->d_name, ".lock") && S_ISLNK(s.st_mode)) {
+			if (!ast_unlock_path(base)) {
+				ast_log(AST_LOG_NOTICE, "Cleaned up orphaned lock file: %s/.lock\n", base);
+			}
+		} else if (S_ISDIR(s.st_mode)) {
+			/* If it is a directory, let's dive down */
+			cleanup_orphaned_lock_files(fullpath);
+		}
+
+		ast_free(fullpath);
+	}
+
+	closedir(dir);
+}
+
 /*! \brief Lock file path
  * only return failure if ast_lock_path returns 'timeout',
  * not if the path does not exist or any other reason
@@ -15355,6 +15403,9 @@ static int load_module(void)
 	/* compute the location of the voicemail spool directory */
 	snprintf(VM_SPOOL_DIR, sizeof(VM_SPOOL_DIR), "%s/voicemail/", ast_config_AST_SPOOL_DIR);
 
+	/* Now that we have a spool directory, clean up old lock files */
+	cleanup_orphaned_lock_files(VM_SPOOL_DIR);
+
 	if (!(mwi_subscription_tps = ast_taskprocessor_get("app_voicemail", 0))) {
 		ast_log(AST_LOG_WARNING, "failed to reference mwi subscription taskprocessor.  MWI will not work\n");
 	}
-- 
GitLab