Skip to contentSkip to conversation

nfs-blkmapd: PID file read by systemd failed

PATCH Superseded zhanchengbin 2 participants 4 messages Oct 18, 2022 – Oct 28, 2022

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 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.

---
V1->V2:
2.27.0 -> 2.33.0

 utils/blkmapd/device-discovery.c | 48 +++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 16 deletions(-)
utils/blkmapd/device-discovery.c +32 -16
index 49935c2e..dcced5c3 100644
@@ -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);
Signature · 1 line
2.33.0
#1 lore raw

ping...

Quoted 86 lines · zhanchengbin

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.

---
V1->V2:
 2.27.0 -> 2.33.0

  utils/blkmapd/device-discovery.c | 48 +++++++++++++++++++++-----------
  1 file changed, 32 insertions(+), 16 deletions(-)
utils/blkmapd/device-discovery.c +32 -16
index 49935c2e..dcced5c3 100644
@@ -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 lore raw
In reply to zhanchengbin

Hello,

This still does not apply.. at all.. but
did take a look.

Quoted 24 lines · zhanchengbin

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.

---
V1->V2:
 2.27.0 -> 2.33.0

  utils/blkmapd/device-discovery.c | 48 +++++++++++++++++++++-----------
  1 file changed, 32 insertions(+), 16 deletions(-)
utils/blkmapd/device-discovery.c +0 -0
index 49935c2e..dcced5c3 100644

utils/blkmapd/device-discovery.c int main(int argc, char **argv) · line 512

Quoted 8 lines
utils/blkmapd/device-discovery.c +3 -2
@@ -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");

use BL_LOG_ERR("fork error: %s\n", strerror(errno)) or something similar which tells why the fork failed.

utils/blkmapd/device-discovery.c int main(int argc, char **argv) · line 517

Quoted 5 lines
utils/blkmapd/device-discovery.c +4 -0
              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);

Same thing here... it is good you are showing the failed file but you are not showing my it failed.

Quoted 21 lines
utils/blkmapd/device-discovery.c +14 -4
+                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);

utils/blkmapd/device-discovery.c int main(int argc, char **argv) · line 542

Quoted 10 lines
utils/blkmapd/device-discovery.c +8 -1
-            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);

not clear this is necessary but it is
probably the safest since that's what
daemon() does.

steved.

Quoted 18 lines
utils/blkmapd/device-discovery.c +3 -9
-        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);

#3 lore raw
Quoted 25 lines · Steve Dickson

Hello,

This still does not apply.. at all.. but
did take a look.

Quoted 19 lines · zhanchengbin

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.

---
V1->V2:
  2.27.0 -> 2.33.0

  utils/blkmapd/device-discovery.c | 48 +++++++++++++++++++++-----------

  1 file changed, 32 insertions(+), 16 deletions(-)
Quoted 15 lines

utils/blkmapd/device-discovery.c int main(int argc, char **argv) · line 512

Quoted 13 lines
utils/blkmapd/device-discovery.c +3 -2
index 49935c2e..dcced5c3 100644
@@ -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");

use BL_LOG_ERR("fork error: %s\n", strerror(errno)) or something similar which tells why the fork failed.

I'm considering if openlog will have problems after fork, I'll try to fix this and push the patch for the next version, thx.

Quoted 42 lines

utils/blkmapd/device-discovery.c int main(int argc, char **argv)

Quoted 7 lines
utils/blkmapd/device-discovery.c +4 -0
               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);

Same thing here... it is good you are showing the failed file but you are not showing my it failed.

Quoted 22 lines
utils/blkmapd/device-discovery.c +14 -4
+                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);

utils/blkmapd/device-discovery.c int main(int argc, char **argv) · line 542

Quoted 10 lines
utils/blkmapd/device-discovery.c +8 -1
-            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);
Quoted 3 lines

not clear this is necessary but it is
probably the safest since that's what
daemon() does.I think it's necessary,because similar processing is in the daemon

function.

-zhanchengbin

Quoted 22 lines

steved.

Quoted 17 lines
utils/blkmapd/device-discovery.c +3 -9
-        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);

      }

Quoted 1 line

      signal(SIGINT, sig_die);

.