<p>N A has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/19655">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">asterisk.c: Prevent duplicate Asterisk processes from starting.<br><br>During startup, there is a period of time between when the daemon<br>is started and when Asterisk starts accepting socket connections<br>for remote consoles, during which if another attempt is made to<br>start the Asterisk daemon, it will succeed. This will lead to<br>multiple Asterisk processes running concurrently, causing all<br>sorts of issues.<br><br>A check is now added before starting the daemon to check if<br>if the PID in the PID file is currently running, and if so,<br>to abort to avoid starting a duplicate Asterisk process.<br><br>ASTERISK-30339 #close<br><br>Change-Id: I38d8d75567524ffd6b1779aa24e7f2bd6951fbb3<br>---<br>M main/asterisk.c<br>1 file changed, 57 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/55/19655/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/main/asterisk.c b/main/asterisk.c</span><br><span>index dea849f..21da28c 100644</span><br><span>--- a/main/asterisk.c</span><br><span>+++ b/main/asterisk.c</span><br><span>@@ -1696,6 +1696,35 @@</span><br><span>          return 1;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static int ast_is_starting(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       /* Don't use kill since that only works if Asterisk was started as the same user. */</span><br><span style="color: hsl(120, 100%, 40%);">+      struct stat st;</span><br><span style="color: hsl(120, 100%, 40%);">+       FILE *f;</span><br><span style="color: hsl(120, 100%, 40%);">+      long file_pid = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+    char procpath[PATH_MAX];</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    /* Get current value from PID file */</span><br><span style="color: hsl(120, 100%, 40%);">+ f = fopen(ast_config_AST_PID, "r");</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!f) {</span><br><span style="color: hsl(120, 100%, 40%);">+             return 0; /* PID file doesn't exist? No way to tell. */</span><br><span style="color: hsl(120, 100%, 40%);">+   }</span><br><span style="color: hsl(120, 100%, 40%);">+     fscanf(f, "%ld", &file_pid);</span><br><span style="color: hsl(120, 100%, 40%);">+    fclose(f);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (!file_pid) {</span><br><span style="color: hsl(120, 100%, 40%);">+              return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Check if such a process is running */</span><br><span style="color: hsl(120, 100%, 40%);">+      snprintf(procpath, sizeof(procpath), "/proc/%ld", file_pid);</span><br><span style="color: hsl(120, 100%, 40%);">+        if (stat(procpath, &st) == -1 && errno == ENOENT) {</span><br><span style="color: hsl(120, 100%, 40%);">+               /* Process doesn't exist */</span><br><span style="color: hsl(120, 100%, 40%);">+               return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*! \brief Urgent handler</span><br><span>  *</span><br><span>  * Called by soft_hangup to interrupt the poll, read, or other</span><br><span>@@ -4021,6 +4050,12 @@</span><br><span>               exit(1);</span><br><span>     }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_is_starting()) {</span><br><span style="color: hsl(120, 100%, 40%);">+              fprintf(stderr, "Asterisk is currently starting.  Use 'asterisk -r' to connect momentarily.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+            printf("%s", term_quit());</span><br><span style="color: hsl(120, 100%, 40%);">+          exit(1);</span><br><span style="color: hsl(120, 100%, 40%);">+      }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> #ifdef HAVE_CAP</span><br><span>       child_cap = cap_from_text("cap_net_admin-eip");</span><br><span> #endif</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/19655">change 19655</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/19655"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I38d8d75567524ffd6b1779aa24e7f2bd6951fbb3 </div>
<div style="display:none"> Gerrit-Change-Number: 19655 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: N A <asterisk@phreaknet.org> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>