fix systemd open pid file failed

(cherry picked from commit adf6c3fd8a22b3ac4a64dadb38c5d2ec1710f66d)
This commit is contained in:
lihaoxiang 2022-11-30 22:24:06 -05:00 committed by openeuler-sync-bot
parent 397f4c84ef
commit 888f41d3da
2 changed files with 110 additions and 1 deletions

View File

@ -0,0 +1,105 @@
From 09b60c7787dfd31c17bfc2c2560bc561b141c207 Mon Sep 17 00:00:00 2001
From: lihaoxiang <lihaoxiang9@huawei.com>
Date: Thu, 1 Dec 2022 09:32:09 +0800
Subject: [PATCH] quota-nld: fix open PID file failed when systemd read it
Running quota_nld by systemd might cause the problem that systemd
couldn't open the PID file generated by quota_nld. In fact, the PID
file hasn't existed yet because it originates from the child process
of quota_nld which is a daemon process. As the main process exit,
systemd try to access the PID file but the daemon hadn't create it
that time.
In this situation, we move the procedure of creating PID file into the
parent process to ensure the PID file must existed when quota_nld exit.
After that, the above problem would never occur again.
Signed-off-by: lihaoxiang <lihaoxiang9@huawei.com>
---
quota_nld.c | 43 +++++++++++++++++++++++++++++++++----------
1 file changed, 33 insertions(+), 10 deletions(-)
diff --git a/quota_nld.c b/quota_nld.c
index 72d99a9..ae90bd0 100644
--- a/quota_nld.c
+++ b/quota_nld.c
@@ -413,7 +413,7 @@ static char *build_pid_file_name(void)
}
/* Store daemon's PID to file */
-static int store_pid(void)
+static int store_pid(pid_t pid)
{
FILE *pid_file;
char *pid_name;
@@ -429,7 +429,7 @@ static int store_pid(void)
free(pid_name);
return -1;
}
- if (fprintf(pid_file, "%jd\n", (intmax_t)getpid()) < 0) {
+ if (fprintf(pid_file, "%jd\n", pid) < 0) {
errstr(_("Could not write daemon's PID into '%s'.\n"),
pid_name);
fclose(pid_file);
@@ -460,7 +460,7 @@ static void remove_pid(int signal)
}
/* Store daemon's PID into file and register its removal on SIGTERM */
-static void use_pid_file(void)
+static void use_pid_file(pid_t pid)
{
struct sigaction term_action;
@@ -468,8 +468,35 @@ static void use_pid_file(void)
term_action.sa_flags = 0;
if (sigaction(SIGTERM, &term_action, NULL))
errstr(_("Could not register PID file removal on SIGTERM.\n"));
- if (store_pid())
- errstr(_("Could not store my PID %jd.\n"), (intmax_t )getpid());
+ if (store_pid(pid))
+ errstr(_("Could not store my PID %jd.\n"), pid);
+}
+
+static void fork_daemon()
+{
+ pid_t pid = fork();
+ if (pid < 0) {
+ errstr(_("Failed to daemonize: fork error with %s\n"), strerror(errno));
+ exit(1);
+ } else if (pid != 0) {
+ use_pid_file(pid);
+ exit(0);
+ }
+
+ if (setsid() == -1) {
+ errstr(_("Failed to daemonize: setsid error with %s\n"), strerror(errno));
+ exit(1);
+ }
+ if (chdir("/"))
+ errstr(_("Failed to chdir in daemonize \n"));
+ int fd = open("/dev/null", O_RDWR, 0);
+ if (fd >= 0) {
+ (void)dup2(fd, STDIN_FILENO);
+ (void)dup2(fd, STDOUT_FILENO);
+ (void)dup2(fd, STDERR_FILENO);
+
+ (void)close(fd);
+ }
}
int main(int argc, char **argv)
@@ -485,11 +512,7 @@ int main(int argc, char **argv)
dhandle = init_dbus();
if (!(flags & FL_NODAEMON)) {
use_syslog();
- if (daemon(0, 0)) {
- errstr(_("Failed to daemonize: %s\n"), strerror(errno));
- exit(1);
- };
- use_pid_file();
+ fork_daemon();
}
run(nsock);
return 0;
--
2.37.0.windows.1

View File

@ -1,7 +1,7 @@
Name: quota
Version: 4.05
Epoch: 1
Release: 1
Release: 2
Summary: Linux Diskquota system as part of the Linux kernel
License: BSD and GPLv2 and GPLv2+ and LGPLv2+
URL: http://sourceforge.net/projects/linuxquota/
@ -14,6 +14,7 @@ Source4: rpc-rquotad.sysconfig
Patch9000: 9000-Limit-number-of-comparison-characters-to-4.patch
Patch9001: 9001-Limit-maximum-of-RPC-port.patch
Patch9002: 9002-quota-nld-fix-open-PID-file-failed-when-systemd-read.patch
BuildRequires: autoconf, automake, coreutils, rpcgen, systemd
BuildRequires: e2fsprogs-devel, gettext-devel, openldap-devel
@ -122,6 +123,9 @@ make check
%{_mandir}/man*/*
%changelog
* Thu Dec 1 2022 lihaoxiang <lihaoxiang9@huawei.com> - 1:4.05-2
- fix systemd read pid file failed
* Sat Jan 11 2020 renxudong <renxudong1@huawei.com> - 1:4.05-1
- Upgrade to 4.05