* [PATCH] nfs-blkmapd: PID file read by systemd failed
@ 2022-10-11 9:33 zhanchengbin
2022-10-17 3:41 ` zhanchengbin
2022-10-17 19:12 ` Steve Dickson
0 siblings, 2 replies; 3+ messages in thread
From: zhanchengbin @ 2022-10-11 9:33 UTC (permalink / raw)
To: steved; +Cc: linux-nfs, liuzhiqiang26, linfeilong
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 <zhanchengbin1@huawei.com>
Reviewed-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
---
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 2736ac89..4d97ac72 100644
--- a/utils/blkmapd/device-discovery.c
+++ b/utils/blkmapd/device-discovery.c
@@ -507,28 +507,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");
+ pid_t pid = fork();
+ if (pid < 0) {
+ fprintf(stderr, "fork error\n");
exit(1);
+ } else if (pid != 0) {
+ pidfd = open(PID_FILE, O_WRONLY | O_CREAT, 0644);
+ if (pidfd < 0) {
+ fprintf(stderr, "Create pid file %s failed\n", PID_FILE);
+ exit(1);
+ }
+
+ if (lockf(pidfd, F_TLOCK, 0) < 0) {
+ fprintf(stderr, "Already running; Exiting!");
+ close(pidfd);
+ exit(1);
+ }
+ if (ftruncate(pidfd, 0) < 0)
+ fprintf(stderr, "ftruncate on %s failed: m\n", PID_FILE);
+ sprintf(pidbuf, "%d\n", pid);
+ if (write(pidfd, pidbuf, strlen(pidbuf)) != (ssize_t)strlen(pidbuf))
+ fprintf(stderr, "write on %s failed: m\n", PID_FILE);
+ exit(0);
}
- 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);
+ (void)setsid();
+ if (chdir("/")) {
+ fprintf(stderr, "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);
- if (lockf(pidfd, F_TLOCK, 0) < 0) {
- BL_LOG_ERR("Already running; Exiting!");
- close(pidfd);
- exit(1);
+ (void)close(fd);
}
- 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);
+
+ openlog("blkmapd", LOG_PID, 0);
}
signal(SIGINT, sig_die);
--
2.27.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] nfs-blkmapd: PID file read by systemd failed
2022-10-11 9:33 [PATCH] nfs-blkmapd: PID file read by systemd failed zhanchengbin
@ 2022-10-17 3:41 ` zhanchengbin
2022-10-17 19:12 ` Steve Dickson
1 sibling, 0 replies; 3+ messages in thread
From: zhanchengbin @ 2022-10-17 3:41 UTC (permalink / raw)
To: steved; +Cc: linux-nfs, liuzhiqiang26, linfeilong
friendly ping, thanks.
On 2022/10/11 17:33, zhanchengbin wrote:
> 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 <zhanchengbin1@huawei.com>
> Reviewed-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> ---
> 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 2736ac89..4d97ac72 100644
> --- a/utils/blkmapd/device-discovery.c
> +++ b/utils/blkmapd/device-discovery.c
> @@ -507,28 +507,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");
> + pid_t pid = fork();
> + if (pid < 0) {
> + fprintf(stderr, "fork error\n");
> exit(1);
> + } else if (pid != 0) {
> + pidfd = open(PID_FILE, O_WRONLY | O_CREAT, 0644);
> + if (pidfd < 0) {
> + fprintf(stderr, "Create pid file %s failed\n", PID_FILE);
> + exit(1);
> + }
> +
> + if (lockf(pidfd, F_TLOCK, 0) < 0) {
> + fprintf(stderr, "Already running; Exiting!");
> + close(pidfd);
> + exit(1);
> + }
> + if (ftruncate(pidfd, 0) < 0)
> + fprintf(stderr, "ftruncate on %s failed: m\n", PID_FILE);
> + sprintf(pidbuf, "%d\n", pid);
> + if (write(pidfd, pidbuf, strlen(pidbuf)) !=
> (ssize_t)strlen(pidbuf))
> + fprintf(stderr, "write on %s failed: m\n", PID_FILE);
> + exit(0);
> }
>
> - 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);
> + (void)setsid();
> + if (chdir("/")) {
> + fprintf(stderr, "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);
>
> - if (lockf(pidfd, F_TLOCK, 0) < 0) {
> - BL_LOG_ERR("Already running; Exiting!");
> - close(pidfd);
> - exit(1);
> + (void)close(fd);
> }
> - 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);
> +
> + openlog("blkmapd", LOG_PID, 0);
> }
>
> signal(SIGINT, sig_die);
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] nfs-blkmapd: PID file read by systemd failed
2022-10-11 9:33 [PATCH] nfs-blkmapd: PID file read by systemd failed zhanchengbin
2022-10-17 3:41 ` zhanchengbin
@ 2022-10-17 19:12 ` Steve Dickson
1 sibling, 0 replies; 3+ messages in thread
From: Steve Dickson @ 2022-10-17 19:12 UTC (permalink / raw)
To: zhanchengbin; +Cc: linux-nfs, liuzhiqiang26, linfeilong
Hey!
Thanks for the ping!! Bakeathon sucked up a couples weeks!
On 10/11/22 5:33 AM, zhanchengbin wrote:
> 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.
I understand what you are trying to do... but unfortunately
this patch does not apply to the latest release.... So
I'm a bit concern on the amount to testing was done.
Please repost a patch that applies cleanly.
steved.
>
> Signed-off-by: zhanchengbin <zhanchengbin1@huawei.com>
> Reviewed-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> ---
> 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 2736ac89..4d97ac72 100644
> --- a/utils/blkmapd/device-discovery.c
> +++ b/utils/blkmapd/device-discovery.c
> @@ -507,28 +507,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");
> + pid_t pid = fork();
> + if (pid < 0) {
> + fprintf(stderr, "fork error\n");
> exit(1);
> + } else if (pid != 0) {
> + pidfd = open(PID_FILE, O_WRONLY | O_CREAT, 0644);
> + if (pidfd < 0) {
> + fprintf(stderr, "Create pid file %s failed\n", PID_FILE);
> + exit(1);
> + }
> +
> + if (lockf(pidfd, F_TLOCK, 0) < 0) {
> + fprintf(stderr, "Already running; Exiting!");
> + close(pidfd);
> + exit(1);
> + }
> + if (ftruncate(pidfd, 0) < 0)
> + fprintf(stderr, "ftruncate on %s failed: m\n", PID_FILE);
> + sprintf(pidbuf, "%d\n", pid);
> + if (write(pidfd, pidbuf, strlen(pidbuf)) !=
> (ssize_t)strlen(pidbuf))
> + fprintf(stderr, "write on %s failed: m\n", PID_FILE);
> + exit(0);
> }
>
> - 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);
> + (void)setsid();
> + if (chdir("/")) {
> + fprintf(stderr, "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);
>
> - if (lockf(pidfd, F_TLOCK, 0) < 0) {
> - BL_LOG_ERR("Already running; Exiting!");
> - close(pidfd);
> - exit(1);
> + (void)close(fd);
> }
> - 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);
> +
> + openlog("blkmapd", LOG_PID, 0);
> }
>
> signal(SIGINT, sig_die);
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-10-17 19:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-11 9:33 [PATCH] nfs-blkmapd: PID file read by systemd failed zhanchengbin
2022-10-17 3:41 ` zhanchengbin
2022-10-17 19:12 ` Steve Dickson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).