skylark/framework-create-pidfile-after-os.fork-in-child-proc.patch

63 lines
1.8 KiB
Diff
Raw Permalink Normal View History

From 12d0dd3662a21acdde8e2f0264ed4c8aec6e3138 Mon Sep 17 00:00:00 2001
From: Dongxu Sun <sundongxu3@huawei.com>
Date: Wed, 24 Aug 2022 15:47:09 +0800
Subject: [PATCH] framework: create pidfile after os.fork in child process
To prevent the pidfile from being read by systemd when Skylark
does not initialize the pidfile, move create_pid_file() after
os.fork in child process.
Signed-off-by: Dongxu Sun <sundongxu3@huawei.com>
---
skylark.py | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/skylark.py b/skylark.py
index c281a54..6224f9b 100644
--- a/skylark.py
+++ b/skylark.py
@@ -112,15 +112,16 @@ def create_pid_file():
os.fchmod(fd, stat.S_IRUSR | stat.S_IWUSR)
os.close(fd)
try:
- PID_FILE = open(PID_FILE_NAME, 'w')
+ PID_FILE = open(PID_FILE_NAME, 'a')
except IOError:
LOGGER.error("Failed to open pid file")
- exit(1)
+ sys.exit(1)
try:
fcntl.flock(PID_FILE.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
LOGGER.error("A running service instance already creates the pid file! This service will exit!")
+ PID_FILE.close()
os._exit(0)
process_pid = os.getpid()
@@ -153,9 +154,6 @@ def func_daemon():
@atexit.register
def daemon_exit_func():
LIBVIRT_CONN.close()
- remove_pid_file()
-
- create_pid_file()
LOGGER.info("Try to open libvirtd connection")
try:
@@ -186,7 +184,10 @@ def create_daemon():
LOGGER.error('Fork daemon process failed: %d (%s)' % (error.errno, error.strerror))
os._exit(1)
else:
- if pid:
+ if pid == 0:
+ atexit.register(remove_pid_file)
+ create_pid_file()
+ else:
os._exit(0)
os.chdir('/')
os.umask(0)
--
2.17.1