From d190af58500a98d5f55c214d9b10c04e8ac3342d Mon Sep 17 00:00:00 2001 From: Zhiqiang Liu Date: Thu, 24 Nov 2022 15:36:51 +0800 Subject: [PATCH] nfs-blkmapd: PID file read by systemd failed fix issue:https://gitee.com/src-openeuler/nfs-utils/issues/I5TQQX Signed-off-by: Zhiqiang Liu (cherry picked from commit e34346fa254b42b7ef173b084dd194882b8bf7c5) --- ...mapd-PID-file-read-by-systemd-failed.patch | 94 +++++++++++++++++++ nfs-utils.spec | 6 +- 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 0007-nfs-blkmapd-PID-file-read-by-systemd-failed.patch diff --git a/0007-nfs-blkmapd-PID-file-read-by-systemd-failed.patch b/0007-nfs-blkmapd-PID-file-read-by-systemd-failed.patch new file mode 100644 index 0000000..f1fea1e --- /dev/null +++ b/0007-nfs-blkmapd-PID-file-read-by-systemd-failed.patch @@ -0,0 +1,94 @@ +From 9565ab64e60f8282967e138f43c6057562dc5c27 Mon Sep 17 00:00:00 2001 +From: zhanchengbin +Date: Sat, 19 Nov 2022 11:50:07 -0500 +Subject: [PATCH] nfs-blkmapd: PID file read by systemd failed + +When started nfs-blkmap.service, the PID file can't be opened, The +cause is that the child process does not create the PID file before +the systemd reads the PID file. + +Adding "ExecStartPost=/bin/sleep 0.1" to +/usr/lib/systemd/system/nfs-blkmap.service will probably solve this +problem, However, there is no guarantee that the above solutions are +effective under high cpu pressure.So replace the daemon function with +the fork function, and put the behavior of creating the PID file in +the parent process to solve the above problems. + +Signed-off-by: zhanchengbin +Signed-off-by: Zhiqiang Liu +Signed-off-by: Steve Dickson +--- + utils/blkmapd/device-discovery.c | 48 ++++++++++++++++++++++++++-------------- + 1 file changed, 32 insertions(+), 16 deletions(-) + +diff --git a/utils/blkmapd/device-discovery.c b/utils/blkmapd/device-discovery.c +index bd89059..a565fdb 100644 +--- a/utils/blkmapd/device-discovery.c ++++ b/utils/blkmapd/device-discovery.c +@@ -504,28 +504,44 @@ int main(int argc, char **argv) + if (fg) { + openlog("blkmapd", LOG_PERROR, 0); + } else { +- if (daemon(0, 0) != 0) { +- fprintf(stderr, "Daemonize failed\n"); +- exit(1); ++ pid_t pid = fork(); ++ if (pid < 0) { ++ BL_LOG_ERR("fork error\n"); ++ exit(1); ++ } else if (pid != 0) { ++ pidfd = open(PID_FILE, O_WRONLY | O_CREAT, 0644); ++ if (pidfd < 0) { ++ BL_LOG_ERR("Create pid file %s failed\n", PID_FILE); ++ exit(1); ++ } ++ ++ if (lockf(pidfd, F_TLOCK, 0) < 0) { ++ BL_LOG_ERR("Already running; Exiting!"); ++ close(pidfd); ++ exit(1); ++ } ++ if (ftruncate(pidfd, 0) < 0) ++ BL_LOG_ERR("ftruncate on %s failed: m\n", PID_FILE); ++ sprintf(pidbuf, "%d\n", pid); ++ if (write(pidfd, pidbuf, strlen(pidbuf)) != (ssize_t)strlen(pidbuf)) ++ BL_LOG_ERR("write on %s failed: m\n", PID_FILE); ++ exit(0); ++ } ++ ++ (void)setsid(); ++ if (chdir("/")) { ++ BL_LOG_ERR("chdir error\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); + } + + openlog("blkmapd", LOG_PID, 0); +- pidfd = open(PID_FILE, O_WRONLY | O_CREAT, 0644); +- if (pidfd < 0) { +- BL_LOG_ERR("Create pid file %s failed\n", PID_FILE); +- exit(1); +- } +- +- if (lockf(pidfd, F_TLOCK, 0) < 0) { +- BL_LOG_ERR("Already running; Exiting!"); +- close(pidfd); +- exit(1); +- } +- if (ftruncate(pidfd, 0) < 0) +- BL_LOG_WARNING("ftruncate on %s failed: m\n", PID_FILE); +- sprintf(pidbuf, "%d\n", getpid()); +- if (write(pidfd, pidbuf, strlen(pidbuf)) != (ssize_t)strlen(pidbuf)) +- BL_LOG_WARNING("write on %s failed: m\n", PID_FILE); + } + + signal(SIGINT, sig_die); +-- +2.27.0 + diff --git a/nfs-utils.spec b/nfs-utils.spec index 48a6a20..342909f 100644 --- a/nfs-utils.spec +++ b/nfs-utils.spec @@ -4,7 +4,7 @@ Name: nfs-utils Version: 2.5.4 -Release: 8 +Release: 9 Epoch: 2 Summary: The Linux NFS userland utility package License: MIT and GPLv2 and GPLv2+ and BSD @@ -19,6 +19,7 @@ Patch3: 0003-idmapd-Fix-error-status-when-nfs-idmapd-exits.patch Patch4: 0004-fix-coredump-in-bl_add_disk.patch Patch5: 0005-Fix-format-overflow-warning.patch Patch6: 0006-nfs-blkmaped-Fix-the-error-status-when-nfs_blkmapd-s.patch +Patch7: 0007-nfs-blkmapd-PID-file-read-by-systemd-failed.patch BuildRequires: libevent-devel,libcap-devel, libtirpc-devel libblkid-devel BuildRequires: krb5-libs >= 1.4 autoconf >= 2.57 openldap-devel >= 2.2 BuildRequires: automake, libtool, gcc, device-mapper-devel @@ -229,6 +230,9 @@ fi %{_mandir}/*/* %changelog +* Thu Nov 24 2022 Zhiqiang Liu - 2:2.5.4-9 +- nfs-blkmapd: PID file read by systemd failed + * Tue Sep 6 2022 zhanchengbin - 2:2.5.4-8 - nfs-blkmapd: Fix the error status when nfs-blkmapd stops -- Gitee