From 12ea81babb899cd637426afd13740dc9729ca8d3 Mon Sep 17 00:00:00 2001
From: Roman Romanchenko <rromanchenko@sipwise.com>
Date: Mon, 26 Mar 2018 12:47:33 +0300
Subject: [PATCH] TT#34704 Fix sems and sems-pbx to write pid file when running
 with -E option

 - now pid file written with -E as well
---
 core/sems.cpp | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/core/sems.cpp b/core/sems.cpp
index 6f03e5cd..a1814d32 100644
--- a/core/sems.cpp
+++ b/core/sems.cpp
@@ -207,11 +207,11 @@ static bool apply_args(std::map<char,string>& args)
       AmConfig::PlugInPath = it->second;
       break;
 
-#ifndef DISABLE_DAEMON_MODE
     case 'P':
       AmConfig::DaemonPidFile = it->second;
       break;
 
+#ifndef DISABLE_DAEMON_MODE
     case 'u':
       AmConfig::DaemonUid = it->second;
       break;
@@ -535,10 +535,6 @@ int main(int argc, char* argv[])
       DBG("I'm out. pid: %d", main_pid);
       return 0;
     }
-	
-    if(write_pid_file()<0) {
-      goto error;
-    }
 
 #ifdef PROPAGATE_COREDUMP_SETTINGS
     if (have_limit) {
@@ -566,6 +562,10 @@ int main(int argc, char* argv[])
     };
   }
 
+  if(write_pid_file()<0) {
+    goto error;
+  }
+
 #endif /* DISABLE_DAEMON_MODE */
 
   main_pid = getpid();
@@ -661,10 +661,9 @@ int main(int argc, char* argv[])
   async_file_writer::instance()->stop();
   async_file_writer::instance()->join();
 
+  unlink(AmConfig::DaemonPidFile.c_str());
+
 #ifndef DISABLE_DAEMON_MODE
-  if (AmConfig::DaemonMode) {
-    unlink(AmConfig::DaemonPidFile.c_str());
-  }
   if(fd[1]){
      main_pid = -1;
      DBG("send -1 to parent\n");
-- 
2.17.1

