diff mbox series

[v3] nfs-blkmapd: PID file read by systemd failed

Message ID c0bd54a6-ff27-ccf7-39b4-a823996bd5af@huawei.com (mailing list archive)
State New, archived
Headers show
Series [v3] nfs-blkmapd: PID file read by systemd failed | expand

Commit Message

zhanchengbin Nov. 8, 2022, 3:41 a.m. UTC
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>
---
V2->V3:
  Replace "fprintf(stderr" with BL_LOG_ERR.

  utils/blkmapd/device-discovery.c | 48 +++++++++++++++++++++-----------
  1 file changed, 32 insertions(+), 16 deletions(-)

Comments

Steve Dickson Nov. 8, 2022, 7:59 p.m. UTC | #1
Hey!

Again thank you make those changes but once again
nothing applies cleanly... I think your email client
is reformatting the patch. Please try this:

$ git clone git://linux-nfs.org/~steved/nfs-utils
$ cd nfs-utils
$ edit utils/blkmapd/device-discovery.c
$ git commit -s -a
$ git format-patch -o /tmp/ -1
$ git send-email $DRYRUN  --suppress-cc=all --suppress-from \
    --no-chain-reply-to \
    --from "zhanchengbin <zhanchengbin1@huawei.com>" \
    --to "Linux NFS Mailing list <linux-nfs@vger.kernel.org>"

If DRYRUN=--dry-run will test the mailing but not send it.

steved.

On 11/7/22 10:41 PM, 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>
> ---
> V2->V3:
>   Replace "fprintf(stderr" with BL_LOG_ERR.
> 
>   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 bd890598..a565fdbd 100644
> --- a/utils/blkmapd/device-discovery.c
> +++ b/utils/blkmapd/device-discovery.c
> @@ -498,28 +498,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) {
> +            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);
>           }
> 
> -        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("/")) {
> +            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);
> 
> -        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);
zhanchengbin Nov. 14, 2022, 1:59 a.m. UTC | #2
I don't know why it's going wrong, but I always use the latest
code and then git format-patch.
Thanks.

  -zhanchengbin.

On 2022/11/9 3:59, Steve Dickson wrote:
> Hey!
> 
> Again thank you make those changes but once again
> nothing applies cleanly... I think your email client
> is reformatting the patch. Please try this:
> 
> $ git clone git://linux-nfs.org/~steved/nfs-utils
> $ cd nfs-utils
> $ edit utils/blkmapd/device-discovery.c
> $ git commit -s -a
> $ git format-patch -o /tmp/ -1
> $ git send-email $DRYRUN  --suppress-cc=all --suppress-from \
>     --no-chain-reply-to \
>     --from "zhanchengbin <zhanchengbin1@huawei.com>" \
>     --to "Linux NFS Mailing list <linux-nfs@vger.kernel.org>"
> 
> If DRYRUN=--dry-run will test the mailing but not send it.
> 
> steved.
> 
> On 11/7/22 10:41 PM, 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>
>> ---
>> V2->V3:
>>   Replace "fprintf(stderr" with BL_LOG_ERR.
>>
>>   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 bd890598..a565fdbd 100644
>> --- a/utils/blkmapd/device-discovery.c
>> +++ b/utils/blkmapd/device-discovery.c
>> @@ -498,28 +498,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) {
>> +            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);
>>           }
>>
>> -        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("/")) {
>> +            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);
>>
>> -        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);
> 
> .
diff mbox series

Patch

diff --git a/utils/blkmapd/device-discovery.c 
b/utils/blkmapd/device-discovery.c
index bd890598..a565fdbd 100644
--- a/utils/blkmapd/device-discovery.c
+++ b/utils/blkmapd/device-discovery.c
@@ -498,28 +498,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) {
+			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);
  		}

-		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("/")) {
+			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);

-		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);