Skip to contentSkip to conversation

trivial improvements of cleanerd

PATCH In review Hitoshi Mitake 2 participants 8 messages Jan 1, 2014 – Jan 5, 2014 6 messages not in the archive NILFS2 filesystem

lore
Share & tools
lore
#0 lore raw

cover trivial improvements of cleanerd

This patchset removes trivial problems in cleanerd. Below is a summary of the problems:
1. current daemonize() of cleanerd doesn't ensure not to being a session leader
2. cleanerd doesn't adjust the OOM killer

Thanks,
Hitoshi

Hitoshi Mitake (2):
cleanerd: call _exit(2) twice for ensuring not being a session leader
cleanerd: adjust the OOM killer

 sbin/cleanerd/cleanerd.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
Signature · 6 lines
1.8.1.2

--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 1388561449-13980-1-git-send-email-mitake.hitoshi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

PATCH 1/2 cleanerd: call _exit(2) twice for ensuring not being a session leader

Current daemonize() function of cleanerd call _exit(2) only once during its process of becoming a daemon process. But in the linux environment, a daemon process should call _exit(2) twice for ensuring not being a session leader. If a process don't do that, unexpected SIGHUP can be sent to the process (though it happens rarely). The signal would be confusing event for cleanerd of nilfs. This patch removes this potential problem.

---
 sbin/cleanerd/cleanerd.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
sbin/cleanerd/cleanerd.c +10 -0
index 26067bd..edfa083 100644
@@ -676,6 +676,16 @@ static int daemonize(int nochdir, int noclose, int nofork)
 
 	/* umask(0); */
 
+	/* for ensuring I'm not a session leader */
+	if (!nofork) {
+		pid = fork();
+		if (pid < 0)
+			return -1;
+		else if (pid != 0)
+			/* parent */
+			_exit(0);
+	}
+
 	if (!nochdir && (chdir(ROOTDIR) < 0))
 		return -1;
 
Signature · 6 lines
1.8.1.2

--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 1388561449-13980-2-git-send-email-mitake.hitoshi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#4 lore raw

Re: [PATCH nilfs-utils 1/2] cleanerd: call _exit(2) twice for ensuring not being a session leader

Quoted 20 lines · Hitoshi Mitake

Current daemonize() function of cleanerd call _exit(2) only once during its process of becoming a daemon process. But in the linux environment, a daemon process should call _exit(2) twice for ensuring not being a session leader. If a process don't do that, unexpected SIGHUP can be sent to the process (though it happens rarely). The signal would be confusing event for cleanerd of nilfs. This patch removes this potential problem.

---
 sbin/cleanerd/cleanerd.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
sbin/cleanerd/cleanerd.c +0 -0
index 26067bd..edfa083 100644
@@ -676,6 +676,16 @@ static int daemonize(int nochdir, int noclose, int nofork)
 
 	/* umask(0); */
 

sbin/cleanerd/cleanerd.c static int daemonize(int nochdir, int noclose, int nofork) · line 688

Quoted 10 lines
sbin/cleanerd/cleanerd.c +10 -0
+	/* for ensuring I'm not a session leader */
+	if (!nofork) {
+		pid = fork();
+		if (pid < 0)
+			return -1;
+		else if (pid != 0)
+			/* parent */
+			_exit(0);
+	}
+

I tried your patch, but the cleaner daemon still was a session leader.

This turned out because nilfs_cleanerd is usually executed by mount.nilfs2 program with the nofork option (-n).

To fix this problem, it looks like the above !nofork check of the second fork() should be removed even though it becomes confusing. In that case, we may need to add some explanation why fork() should be called even if nofork is specified.

Regards,
Ryusuke Konishi

Quoted 10 lines
sbin/cleanerd/cleanerd.c +0 -0
 	if (!nochdir && (chdir(ROOTDIR) < 0))
 		return -1;
 
Signature · 6 lines
1.8.1.2

--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20140101.183018.491730600.konishi.ryusuke-Zyj7fXuS5i5L9jVzuh4AOg@public.gmane.org

#6 lore raw

Re: [PATCH nilfs-utils 1/2] cleanerd: call _exit(2) twice for ensuring not being a session leader

Quoted 21 lines · Ryusuke Konishi
Quoted 20 lines · Hitoshi Mitake

Current daemonize() function of cleanerd call _exit(2) only once during its process of becoming a daemon process. But in the linux environment, a daemon process should call _exit(2) twice for ensuring not being a session leader. If a process don't do that, unexpected SIGHUP can be sent to the process (though it happens rarely). The signal would be confusing event for cleanerd of nilfs. This patch removes this potential problem.

---
 sbin/cleanerd/cleanerd.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
sbin/cleanerd/cleanerd.c +0 -0
index 26067bd..edfa083 100644
@@ -676,6 +676,16 @@ static int daemonize(int nochdir, int noclose, int nofork)
 
      /* umask(0); */
 
Quoted 12 lines

sbin/cleanerd/cleanerd.c static int daemonize(int nochdir, int noclose, int nofork) · line 688

Quoted 10 lines
sbin/cleanerd/cleanerd.c +10 -0
+     /* for ensuring I'm not a session leader */
+     if (!nofork) {
+             pid = fork();
+             if (pid < 0)
+                     return -1;
+             else if (pid != 0)
+                     /* parent */
+                     _exit(0);
+     }
+

I tried your patch, but the cleaner daemon still was a session leader.

Thanks for your review and testing.

Quoted 8 lines

This turned out because nilfs_cleanerd is usually executed by mount.nilfs2 program with the nofork option (-n).

To fix this problem, it looks like the above !nofork check of the second fork() should be removed even though it becomes confusing. In that case, we may need to add some explanation why fork() should be called even if nofork is specified.

For ensuring not being a session leader, fork() should be called twice. Removing the second condition of !nofork is not enough. For this purpose, we need to remove both of the conditions of !nofork.

BTW, what is an intention of "-n" option of cleanerd? I read the code of nilfs_launch_cleanerd() but couldn't understand the reason of this option.

If this option is aiming to reduce calling of fork(), I think this can be eliminated. Calling 3 fork()s (1 in mount.nilfs2, 2 in cleanerd) would be acceptable.

Thanks,
Hitoshi
--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CAE1WaKLvwoTwM0W5Wsv0wcWSzihWLjifNi3HXsPt3XK529tanQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#8 lore raw

Re: [PATCH nilfs-utils 1/2] cleanerd: call _exit(2) twice for ensuring not being a session leader

Hi,

Quoted 42 lines · Hitoshi Mitake
Quoted 21 lines · Ryusuke Konishi
Quoted 20 lines · Hitoshi Mitake

Current daemonize() function of cleanerd call _exit(2) only once during its process of becoming a daemon process. But in the linux environment, a daemon process should call _exit(2) twice for ensuring not being a session leader. If a process don't do that, unexpected SIGHUP can be sent to the process (though it happens rarely). The signal would be confusing event for cleanerd of nilfs. This patch removes this potential problem.

---
 sbin/cleanerd/cleanerd.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
sbin/cleanerd/cleanerd.c +0 -0
index 26067bd..edfa083 100644
@@ -676,6 +676,16 @@ static int daemonize(int nochdir, int noclose, int nofork)
 
      /* umask(0); */
 
Quoted 12 lines

sbin/cleanerd/cleanerd.c static int daemonize(int nochdir, int noclose, int nofork) · line 688

Quoted 10 lines
sbin/cleanerd/cleanerd.c +10 -0
+     /* for ensuring I'm not a session leader */
+     if (!nofork) {
+             pid = fork();
+             if (pid < 0)
+                     return -1;
+             else if (pid != 0)
+                     /* parent */
+                     _exit(0);
+     }
+

I tried your patch, but the cleaner daemon still was a session leader.

Thanks for your review and testing.

Quoted 4 lines

This turned out because nilfs_cleanerd is usually executed by mount.nilfs2 program with the nofork option (-n).

Quoted 8 lines
Quoted 4 lines

To fix this problem, it looks like the above !nofork check of the second fork() should be removed even though it becomes confusing. In that case, we may need to add some explanation why fork() should be called even if nofork is specified.

For ensuring not being a session leader, fork() should be called twice. Removing the second condition of !nofork is not enough. For this purpose, we need to remove both of the conditions of !nofork.

Yes, I supposed here that the caller (the mount helper program) already did a fork() call when -n option is specified.

But, anyway, removing only the latter check of !nofork isn't a good idea. It's a hacky.

Quoted 2 lines

BTW, what is an intention of "-n" option of cleanerd? I read the code of nilfs_launch_cleanerd() but couldn't understand the reason of this option.

This is an option just to avoid fork doubly when mount.nilfs2 already did a fork().

Quoted 3 lines

If this option is aiming to reduce calling of fork(), I think this can be eliminated. Calling 3 fork()s (1 in mount.nilfs2, 2 in cleanerd) would be acceptable.

Okay, accepting 3 forks()s seems reasonable. So, how about changing both programs as follow?

1) Change cleanerd to simply ignore -n option as a historical option (remove the existing !nofork check).
2) Change cleanerd always fork twice to ensure that it will not be a session leader.
3) Change cleaner_exec.c not to add -n option.

Thanks,
Ryusuke Konishi
--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20140104.232806.190364467.konishi.ryusuke-Zyj7fXuS5i5L9jVzuh4AOg@public.gmane.org

#10 lore raw

Re: [PATCH nilfs-utils 1/2] cleanerd: call _exit(2) twice for ensuring not being a session leader

At Sat, 04 Jan 2014 23:28:06 +0900 (JST),

Quoted 70 lines · Ryusuke Konishi

Hi,

Quoted 42 lines · Hitoshi Mitake
Quoted 21 lines · Ryusuke Konishi
Quoted 20 lines · Hitoshi Mitake

Current daemonize() function of cleanerd call _exit(2) only once during its process of becoming a daemon process. But in the linux environment, a daemon process should call _exit(2) twice for ensuring not being a session leader. If a process don't do that, unexpected SIGHUP can be sent to the process (though it happens rarely). The signal would be confusing event for cleanerd of nilfs. This patch removes this potential problem.

---
 sbin/cleanerd/cleanerd.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
sbin/cleanerd/cleanerd.c +0 -0
index 26067bd..edfa083 100644
@@ -676,6 +676,16 @@ static int daemonize(int nochdir, int noclose, int nofork)
 
      /* umask(0); */
 
Quoted 12 lines

sbin/cleanerd/cleanerd.c static int daemonize(int nochdir, int noclose, int nofork) · line 688

Quoted 10 lines
sbin/cleanerd/cleanerd.c +10 -0
+     /* for ensuring I'm not a session leader */
+     if (!nofork) {
+             pid = fork();
+             if (pid < 0)
+                     return -1;
+             else if (pid != 0)
+                     /* parent */
+                     _exit(0);
+     }
+

I tried your patch, but the cleaner daemon still was a session leader.

Thanks for your review and testing.

Quoted 4 lines

This turned out because nilfs_cleanerd is usually executed by mount.nilfs2 program with the nofork option (-n).

Quoted 8 lines
Quoted 4 lines

To fix this problem, it looks like the above !nofork check of the second fork() should be removed even though it becomes confusing. In that case, we may need to add some explanation why fork() should be called even if nofork is specified.

For ensuring not being a session leader, fork() should be called twice. Removing the second condition of !nofork is not enough. For this purpose, we need to remove both of the conditions of !nofork.

Yes, I supposed here that the caller (the mount helper program) already did a fork() call when -n option is specified.

But, anyway, removing only the latter check of !nofork isn't a good idea. It's a hacky.

Quoted 2 lines

BTW, what is an intention of "-n" option of cleanerd? I read the code of nilfs_launch_cleanerd() but couldn't understand the reason of this option.

This is an option just to avoid fork doubly when mount.nilfs2 already did a fork().

Quoted 3 lines

If this option is aiming to reduce calling of fork(), I think this can be eliminated. Calling 3 fork()s (1 in mount.nilfs2, 2 in cleanerd) would be acceptable.

Quoted 8 lines

Okay, accepting 3 forks()s seems reasonable. So, how about changing both programs as follow?

1) Change cleanerd to simply ignore -n option as a historical option (remove the existing !nofork check).
2) Change cleanerd always fork twice to ensure that it will not be a session leader.
3) Change cleaner_exec.c not to add -n option.

I agree with the above 3 policies. I'll send v2 based on them later.

Thanks,
Hitoshi
--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#11 lore raw

PATCH 2/2 cleanerd: adjust the OOM killer

In reply to a message not in the archive

cleanerd is a very important process in a system which uses nilfs. So it should adjust the OOM killer for reducing possibility of the killing as much as possible.

---
 sbin/cleanerd/cleanerd.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
sbin/cleanerd/cleanerd.c +39 -0
index edfa083..3494a9a 100644
@@ -654,6 +654,40 @@ nilfs_cleanerd_select_segments(struct nilfs_cleanerd *cleanerd,
 	return nssegs;
 }
 
+static int oom_adjust(void)
+{
+	int fd, err;
+	const char *path, *score;
+	struct stat st;
+
+	/* Avoid oom-killer */
+	path = "/proc/self/oom_score_adj";
+	score = "-1000\n";
+
+	if (stat(path, &st)) {
+		/* oom_score_adj cannot be used, try oom_adj */
+		path = "/proc/self/oom_adj";
+		score = "-17\n";
+	}
+
+	fd = open(path, O_WRONLY);
+	if (fd < 0) {
+		fprintf(stderr, "can't adjust oom-killer's pardon %s, %m\n",
+			path);
+		return errno;
+	}
+
+	err = write(fd, score, strlen(score));
+	if (err < 0) {
+		fprintf(stderr, "can't adjust oom-killer's pardon %s, %m\n",
+			path);
+		close(fd);
+		return errno;
+	}
+	close(fd);
+	return 0;
+}
+
 #define DEVNULL	"/dev/null"
 #define ROOTDIR	"/"
 
@@ -1549,6 +1583,11 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
+	if (oom_adjust() < 0) {
+		fprintf(stderr, "adjusting the OOM killer falied: %m\n");
+		exit(1);
+	}
+
 	openlog(progname, LOG_PID, LOG_DAEMON);
 	syslog(LOG_INFO, "start");
 
Signature · 6 lines
1.8.1.2

--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 1388561449-13980-3-git-send-email-mitake.hitoshi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#13 lore raw

Re: [PATCH nilfs-utils 2/2] cleanerd: adjust the OOM killer

Quoted 5 lines · Hitoshi Mitake

cleanerd is a very important process in a system which uses nilfs. So it should adjust the OOM killer for reducing possibility of the killing as much as possible.

Looks OK to me.

Applied after I corrected a trivial typo: s/falied/failed/.

Thanks,
Ryusuke Konishi

Quoted 68 lines
---
 sbin/cleanerd/cleanerd.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
sbin/cleanerd/cleanerd.c +39 -0
index edfa083..3494a9a 100644
@@ -654,6 +654,40 @@ nilfs_cleanerd_select_segments(struct nilfs_cleanerd *cleanerd,
 	return nssegs;
 }
 
+static int oom_adjust(void)
+{
+	int fd, err;
+	const char *path, *score;
+	struct stat st;
+
+	/* Avoid oom-killer */
+	path = "/proc/self/oom_score_adj";
+	score = "-1000\n";
+
+	if (stat(path, &st)) {
+		/* oom_score_adj cannot be used, try oom_adj */
+		path = "/proc/self/oom_adj";
+		score = "-17\n";
+	}
+
+	fd = open(path, O_WRONLY);
+	if (fd < 0) {
+		fprintf(stderr, "can't adjust oom-killer's pardon %s, %m\n",
+			path);
+		return errno;
+	}
+
+	err = write(fd, score, strlen(score));
+	if (err < 0) {
+		fprintf(stderr, "can't adjust oom-killer's pardon %s, %m\n",
+			path);
+		close(fd);
+		return errno;
+	}
+	close(fd);
+	return 0;
+}
+
 #define DEVNULL	"/dev/null"
 #define ROOTDIR	"/"
 
@@ -1549,6 +1583,11 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
+	if (oom_adjust() < 0) {
+		fprintf(stderr, "adjusting the OOM killer falied: %m\n");
+		exit(1);
+	}
+
 	openlog(progname, LOG_PID, LOG_DAEMON);
 	syslog(LOG_INFO, "start");
 
Signature · 6 lines
1.8.1.2

--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html