[Asterisk-code-review] asterisk.c: Prevent duplicate Asterisk processes from starting. (asterisk[master])

N A asteriskteam at digium.com
Sun Dec 4 09:01:33 CST 2022


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/19655 )


Change subject: asterisk.c: Prevent duplicate Asterisk processes from starting.
......................................................................

asterisk.c: Prevent duplicate Asterisk processes from starting.

During startup, there is a period of time between when the daemon
is started and when Asterisk starts accepting socket connections
for remote consoles, during which if another attempt is made to
start the Asterisk daemon, it will succeed. This will lead to
multiple Asterisk processes running concurrently, causing all
sorts of issues.

A check is now added before starting the daemon to check if
if the PID in the PID file is currently running, and if so,
to abort to avoid starting a duplicate Asterisk process.

ASTERISK-30339 #close

Change-Id: I38d8d75567524ffd6b1779aa24e7f2bd6951fbb3
---
M main/asterisk.c
1 file changed, 57 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/55/19655/1

diff --git a/main/asterisk.c b/main/asterisk.c
index dea849f..21da28c 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -1696,6 +1696,35 @@
 		return 1;
 }
 
+static int ast_is_starting(void)
+{
+	/* Don't use kill since that only works if Asterisk was started as the same user. */
+	struct stat st;
+	FILE *f;
+	long file_pid = 0;
+	char procpath[PATH_MAX];
+
+	/* Get current value from PID file */
+	f = fopen(ast_config_AST_PID, "r");
+	if (!f) {
+		return 0; /* PID file doesn't exist? No way to tell. */
+	}
+	fscanf(f, "%ld", &file_pid);
+	fclose(f);
+	if (!file_pid) {
+		return 0;
+	}
+
+	/* Check if such a process is running */
+	snprintf(procpath, sizeof(procpath), "/proc/%ld", file_pid);
+	if (stat(procpath, &st) == -1 && errno == ENOENT) {
+		/* Process doesn't exist */
+		return 0;
+	}
+	return 1;
+}
+
+
 /*! \brief Urgent handler
  *
  * Called by soft_hangup to interrupt the poll, read, or other
@@ -4021,6 +4050,12 @@
 		exit(1);
 	}
 
+	if (ast_is_starting()) {
+		fprintf(stderr, "Asterisk is currently starting.  Use 'asterisk -r' to connect momentarily.\n");
+		printf("%s", term_quit());
+		exit(1);
+	}
+
 #ifdef HAVE_CAP
 	child_cap = cap_from_text("cap_net_admin-eip");
 #endif

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/19655
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I38d8d75567524ffd6b1779aa24e7f2bd6951fbb3
Gerrit-Change-Number: 19655
Gerrit-PatchSet: 1
Gerrit-Owner: N A <asterisk at phreaknet.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20221204/f540995a/attachment.html>


More information about the asterisk-code-review mailing list