From eb1f022b33bb627b93951cde9faf9331f384c927 Mon Sep 17 00:00:00 2001
From: Roman Tsisyk <roman@tsisyk.com>
Date: Tue, 15 Oct 2013 19:11:03 +0400
Subject: [PATCH] opentar53-sigterm: remove pid file on SIGTERM during the boot
 process

---
 src/tarantool.cc | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/src/tarantool.cc b/src/tarantool.cc
index f24100800e..2343bf967e 100644
--- a/src/tarantool.cc
+++ b/src/tarantool.cc
@@ -443,6 +443,21 @@ sig_fatal_cb(int signo)
 	abort();
 }
 
+/**
+ * This SIGTERM handler is only used before the main event loop started to
+ * cleanup server pid file. The handler is replaced by ev_signal after the boot.
+ * @sa signal_start
+ */
+static void
+sig_term_cb(int signo)
+{
+	(void) signo;
+	/* unlink pidfile. */
+	if (cfg.pid_file != NULL)
+		unlink(cfg.pid_file);
+
+	_exit(EXIT_SUCCESS);
+}
 
 static void
 signal_free(void)
@@ -511,6 +526,12 @@ signal_init(void)
 		exit(EX_OSERR);
 	}
 
+	sa.sa_handler = sig_term_cb;
+	if (sigaction(SIGTERM, &sa, 0) == -1) {
+		say_syserror("sigaction");
+		exit(EX_OSERR);
+	}
+
 	ev_signal_init(&ev_sigs[0], sig_snapshot, SIGUSR1);
 	ev_signal_init(&ev_sigs[1], signal_cb, SIGINT);
 	ev_signal_init(&ev_sigs[2], signal_cb, SIGTERM);
@@ -599,7 +620,6 @@ tarantool_lua_free()
 	tarantool_L = NULL;
 }
 
-
 void
 tarantool_free(void)
 {
@@ -619,8 +639,8 @@ tarantool_free(void)
 		gopt_free(main_opt);
 	free_proc_title(main_argc, main_argv);
 
-	/* unlink pidfile but not in replication process. */
-	if ((cfg.pid_file != NULL) && (master_pid == getpid()))
+	/* unlink pidfile. */
+	if (cfg.pid_file != NULL)
 		unlink(cfg.pid_file);
 	destroy_tarantool_cfg(&cfg);
 
-- 
GitLab