Skip to contentSkip to conversation

rework daemonize() of cleanerd

PATCH Applied Hitoshi Mitake 3 participants 12 messages Jan 5, 2014 – Jan 7, 2014 9 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover rework daemonize() of cleanerd

Current daemonize() function of cleanerd has a problem. It cannot ensure that the caller (cleanerd) doesn't become a session leader.

This patchset solves the above problem. In addition, the -n option of cleanerd is removed because it is obsolete. The description of the option in a man page is also removed.

Hitoshi Mitake (4):
cleanerd: ignore nofork option
cleanerd: call _exit(2) twice for ensuring not being a session leader
mount: don't pass -n option to cleanerd
man: remove a description of -n option

 lib/cleaner_exec.c       |  4 +---
 man/nilfs_cleanerd.8     |  4 ----
 sbin/cleanerd/cleanerd.c | 36 ++++++++++++++++++++----------------
 3 files changed, 21 insertions(+), 23 deletions(-)
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): 1388937165-32692-1-git-send-email-mitake.hitoshi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

PATCH v2 1/4 cleanerd: ignore nofork option

The nofork option aims to reduce call of fork(), but it is not effective. This patch lets cleanerd ignore the option simply even if it is passed.

---
 sbin/cleanerd/cleanerd.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)
sbin/cleanerd/cleanerd.c +12 -16
index e1f6a04..0b5bb70 100644
@@ -113,7 +113,7 @@ do {						\
 const static struct option long_option[] = {
 	{"conffile", required_argument, NULL, 'c'},
 	{"help", no_argument, NULL, 'h'},
-	/* internal option for mount.nilfs2 only */
+	/* nofork option is obsolete. It does nothing even if passed */
 	{"nofork", no_argument, NULL, 'n'},
 	{"protection-period", required_argument, NULL, 'p'},
 	{"version", no_argument, NULL, 'V'},
@@ -691,20 +691,18 @@ static int oom_adjust(void)
 #define DEVNULL	"/dev/null"
 #define ROOTDIR	"/"
 
-static int daemonize(int nochdir, int noclose, int nofork)
+static int daemonize(int nochdir, int noclose)
 {
 	pid_t pid;
 
-	if (!nofork) {
-		pid = fork();
-		if (pid < 0)
-			return -1;
-		else if (pid != 0)
-			/* parent */
-			_exit(0);
-	}
+	pid = fork();
+	if (pid < 0)
+		return -1;
+	else if (pid != 0)
+		/* parent */
+		_exit(0);
 
-	/* child or nofork */
+	/* child */
 	if (setsid() < 0)
 		return -1;
 
@@ -1491,7 +1489,7 @@ int main(int argc, char *argv[])
 	char canonical[PATH_MAX + 2];
 	const char *dev, *dir;
 	char *endptr;
-	int status, nofork, c;
+	int status, c;
 #ifdef _GNU_SOURCE
 	int option_index;
 #endif	/* _GNU_SOURCE */
@@ -1499,7 +1497,6 @@ int main(int argc, char *argv[])
 	progname = (strrchr(argv[0], '/') != NULL) ?
 		strrchr(argv[0], '/') + 1 : argv[0];
 	conffile = NILFS_CLEANERD_CONFFILE;
-	nofork = 0;
 	status = 0;
 	protection_period = ULONG_MAX;
 	dev = NULL;
@@ -1520,8 +1517,7 @@ int main(int argc, char *argv[])
 			nilfs_cleanerd_usage(progname);
 			exit(0);
 		case 'n':
-			/* internal option for mount.nilfs2 only */
-			nofork = 1;
+			/* ignore nofork option, do nothing */
 			break;
 		case 'p':
 			protection_period = strtoul(optarg, &endptr, 10);
@@ -1568,7 +1564,7 @@ int main(int argc, char *argv[])
 		}
 	}
 
-	if (daemonize(0, 0, nofork) < 0) {
+	if (daemonize(0, 0) < 0) {
 		fprintf(stderr, "%s: %s\n", progname, strerror(errno));
 		exit(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): 1388937165-32692-2-git-send-email-mitake.hitoshi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#4 lore raw

Re: [PATCH nilfs-utils v2 1/4] cleanerd: ignore nofork option

Quoted 89 lines · Hitoshi Mitake

The nofork option aims to reduce call of fork(), but it is not effective. This patch lets cleanerd ignore the option simply even if it is passed.

---
 sbin/cleanerd/cleanerd.c | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)
sbin/cleanerd/cleanerd.c +12 -16
index e1f6a04..0b5bb70 100644
@@ -113,7 +113,7 @@ do {						\
 const static struct option long_option[] = {
 	{"conffile", required_argument, NULL, 'c'},
 	{"help", no_argument, NULL, 'h'},
-	/* internal option for mount.nilfs2 only */
+	/* nofork option is obsolete. It does nothing even if passed */
 	{"nofork", no_argument, NULL, 'n'},
 	{"protection-period", required_argument, NULL, 'p'},
 	{"version", no_argument, NULL, 'V'},
@@ -691,20 +691,18 @@ static int oom_adjust(void)
 #define DEVNULL	"/dev/null"
 #define ROOTDIR	"/"
 
-static int daemonize(int nochdir, int noclose, int nofork)
+static int daemonize(int nochdir, int noclose)
 {
 	pid_t pid;
 
-	if (!nofork) {
-		pid = fork();
-		if (pid < 0)
-			return -1;
-		else if (pid != 0)
-			/* parent */
-			_exit(0);
-	}
+	pid = fork();
+	if (pid < 0)
+		return -1;
+	else if (pid != 0)
+		/* parent */
+		_exit(0);
 
-	/* child or nofork */
+	/* child */
 	if (setsid() < 0)
 		return -1;
 
@@ -1491,7 +1489,7 @@ int main(int argc, char *argv[])
 	char canonical[PATH_MAX + 2];
 	const char *dev, *dir;
 	char *endptr;
-	int status, nofork, c;
+	int status, c;
 #ifdef _GNU_SOURCE
 	int option_index;
 #endif	/* _GNU_SOURCE */
@@ -1499,7 +1497,6 @@ int main(int argc, char *argv[])
 	progname = (strrchr(argv[0], '/') != NULL) ?
 		strrchr(argv[0], '/') + 1 : argv[0];
 	conffile = NILFS_CLEANERD_CONFFILE;
-	nofork = 0;
 	status = 0;
 	protection_period = ULONG_MAX;
 	dev = NULL;
@@ -1520,8 +1517,7 @@ int main(int argc, char *argv[])
 			nilfs_cleanerd_usage(progname);
 			exit(0);
 		case 'n':
-			/* internal option for mount.nilfs2 only */
-			nofork = 1;
+			/* ignore nofork option, do nothing */
 			break;
 		case 'p':
 			protection_period = strtoul(optarg, &endptr, 10);
@@ -1568,7 +1564,7 @@ int main(int argc, char *argv[])
 		}
 	}
 
-	if (daemonize(0, 0, nofork) < 0) {
+	if (daemonize(0, 0) < 0) {
 		fprintf(stderr, "%s: %s\n", progname, strerror(errno));
 		exit(1);
 	}
Signature · 1 line
1.8.1.2

Looks good to me. Applied.

Thank a lot.

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

#5 lore raw

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

In reply to a message not in the archive

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 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 | 8 ++++++++
 1 file changed, 8 insertions(+)
sbin/cleanerd/cleanerd.c +8 -0
index 0b5bb70..86dfcf7 100644
@@ -708,6 +708,14 @@ static int daemonize(int nochdir, int noclose)
 
 	/* umask(0); */
 
+	/* for ensuring I'm not a session leader */
+	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): 1388937165-32692-3-git-send-email-mitake.hitoshi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#7 lore raw

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

Quoted 33 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 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 | 8 ++++++++
 1 file changed, 8 insertions(+)
sbin/cleanerd/cleanerd.c +8 -0
index 0b5bb70..86dfcf7 100644
@@ -708,6 +708,14 @@ static int daemonize(int nochdir, int noclose)
 
 	/* umask(0); */
 
+	/* for ensuring I'm not a session leader */
+	pid = fork();
+	if (pid < 0)
+		return -1;
+	else if (pid != 0)
+		/* parent */
+		_exit(0);
+
 	if (!nochdir && (chdir(ROOTDIR) < 0))
 		return -1;
 
Signature · 1 line
1.8.1.2

Looks good.

Applied, thank you.

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): 20140106.022042.112826181.konishi.ryusuke-Zyj7fXuS5i5L9jVzuh4AOg@public.gmane.org

#9 lore raw

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

Quoted 32 lines · Ryusuke Konishi
Quoted 31 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 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 | 8 ++++++++
 1 file changed, 8 insertions(+)
sbin/cleanerd/cleanerd.c +8 -0
index 0b5bb70..86dfcf7 100644
@@ -708,6 +708,14 @@ static int daemonize(int nochdir, int noclose)
 
 	/* umask(0); */
 
+	/* for ensuring I'm not a session leader */
+	pid = fork();
+	if (pid < 0)
+		return -1;
+	else if (pid != 0)
+		/* parent */
+		_exit(0);
+
 	if (!nochdir && (chdir(ROOTDIR) < 0))
 		return -1;
 
Quoted 8 lines
Quoted 2 lines
Signature · 1 line
1.8.1.2

Looks good.

Applied, thank you.

Ryusuke Konishi

Oh no, I found this series has a critical issue after I applied them.

The current umount.nilfs2 (and mount.nilfs2 -o remount,nogc) uses pid of cleanerd to shutdown it, and this series breaks the logic since the fork of cleanerd changes its pid. That was the true reason why nofork option is required.

We need to fix it by reverting the series or other means.

Regards,
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): CAFPMYnH8LTD9ctZCSht5NZv6j1TZMfpCZh2pHyLO4wN-5dRDdw@mail.gmail.com

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

#12 lore raw

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

At Sun, 5 Jan 2014 22:44:41 +0400,

Quoted 10 lines · Сергей Александров

[1 <text/plain; KOI8-R (quoted-printable)>]
Hello,
May be it makes sense to use pid files in /var/run folder with special names like nilfs_cleanerd_<dev>.pid (nilfs_cleanerd_sda1.pid for instance) for this purpose? But it looks a little bit over engineered and may also have some issues while mounting from initrd.

Regards,
Aleksandrov Sergey

OKay, I'll try to solve the problem with a way of pidfile or other methods. If we can't find a suitable way, let's revert the series.

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): 20140106.032857.373562363.konishi.ryusuke-Zyj7fXuS5i5L9jVzuh4AOg@public.gmane.org

#14 lore raw

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

Quoted 11 lines · Ryusuke Konishi

Oh no, I found this series has a critical issue after I applied them.

The current umount.nilfs2 (and mount.nilfs2 -o remount,nogc) uses pid of cleanerd to shutdown it, and this series breaks the logic since the fork of cleanerd changes its pid. That was the true reason why nofork option is required.

We need to fix it by reverting the series or other means.

Regards,
Ryusuke Konishi

From what I've learned of writing daemons, good practice is:

* Allow an option for writing a PID file, but don't by default
(location is often distro-specific)
* To daemonize, double-fork, chdir("/"), and then redirect stdin,
stdout, stderr to /dev/null
* Allow users to request messages to syslog, since stdout is lost.
* If you want to capture the PID, have an option to print it on
stderr before redirecting to /dev/null. The caller can read a pipe to find the final PID of the daemon.
* Allow the option to run the program in the foreground (i.e. do not
daemonize at all) for debugging or in case you want to run the daemon from a monitoring program which wait()s on it. In this case do not close stdout, so that debugging messages may be seen on the terminal, or the messages can be piped to a logger.

Until now, I had thought "-n" was the "no-daemonize" option, which I thought was strange for mount to use.

-Mike
--
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

#15 lore raw

PATCH v2 3/4 mount: don't pass -n option to cleanerd

In reply to a message not in the archive

This patch lets mount.nilfs2 not pass the -n option to cleanerd, because it is obsoleted.

---
 lib/cleaner_exec.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
lib/cleaner_exec.c +1 -3
index 299451c..edc55e3 100644
@@ -75,7 +75,6 @@
 #define WAIT_CLEANERD_RETRY_TIMEOUT	8	/* in seconds */
 
 static const char cleanerd[] = "/sbin/" NILFS_CLEANERD_NAME;
-static const char cleanerd_nofork_opt[] = "-n";
 static const char cleanerd_protperiod_opt[] = "-p";
 
 static void default_logger(int priority, const char *fmt, ...)
@@ -118,7 +117,7 @@ static inline int process_is_alive(pid_t pid)
 int nilfs_launch_cleanerd(const char *device, const char *mntdir,
 			  unsigned long protperiod, pid_t *ppid)
 {
-	const char *dargs[7];
+	const char *dargs[6];
 	struct stat statbuf;
 	sigset_t sigs;
 	int i = 0;
@@ -146,7 +145,6 @@ int nilfs_launch_cleanerd(const char *device, const char *mntdir,
 			exit(1);
 		}
 		dargs[i++] = cleanerd;
-		dargs[i++] = cleanerd_nofork_opt;
 		if (protperiod != ULONG_MAX) {
 			dargs[i++] = cleanerd_protperiod_opt;
 			snprintf(buf, sizeof(buf), "%lu", protperiod);
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): 1388937165-32692-4-git-send-email-mitake.hitoshi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#17 lore raw

Re: [PATCH nilfs-utils v2 3/4] mount: don't pass -n option to cleanerd

Quoted 39 lines · Hitoshi Mitake

This patch lets mount.nilfs2 not pass the -n option to cleanerd, because it is obsoleted.

---
 lib/cleaner_exec.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
lib/cleaner_exec.c +1 -3
index 299451c..edc55e3 100644
@@ -75,7 +75,6 @@
 #define WAIT_CLEANERD_RETRY_TIMEOUT	8	/* in seconds */
 
 static const char cleanerd[] = "/sbin/" NILFS_CLEANERD_NAME;
-static const char cleanerd_nofork_opt[] = "-n";
 static const char cleanerd_protperiod_opt[] = "-p";
 
 static void default_logger(int priority, const char *fmt, ...)
@@ -118,7 +117,7 @@ static inline int process_is_alive(pid_t pid)
 int nilfs_launch_cleanerd(const char *device, const char *mntdir,
 			  unsigned long protperiod, pid_t *ppid)
 {
-	const char *dargs[7];
+	const char *dargs[6];
 	struct stat statbuf;
 	sigset_t sigs;
 	int i = 0;
@@ -146,7 +145,6 @@ int nilfs_launch_cleanerd(const char *device, const char *mntdir,
 			exit(1);
 		}
 		dargs[i++] = cleanerd;
-		dargs[i++] = cleanerd_nofork_opt;
 		if (protperiod != ULONG_MAX) {
 			dargs[i++] = cleanerd_protperiod_opt;
 			snprintf(buf, sizeof(buf), "%lu", protperiod);
Signature · 1 line
1.8.1.2

Looks good, applied.

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

#18 lore raw

PATCH v2 4/4 man: remove a description of -n option

In reply to a message not in the archive
---
 man/nilfs_cleanerd.8 | 4 ----
 1 file changed, 4 deletions(-)
man/nilfs_cleanerd.8 +0 -4
index ad6d188..46caef2 100644
@@ -35,10 +35,6 @@ Display help message and exit.
 \fB\-c \fIfile\fR, \fB\-\-conf\fR=\fIfile\fR
 Specify configuration file.
 .TP
-\fB\-n\fR, \fB\-\-nofork\fR
-invoke cleanerd without forking process. This option is intended for
-internal use from \fBmount.nilfs2\fP(8).
-.TP
 \fB\-p \fIinterval\fR, \fB\-\-protection-period\fR=\fIinterval\fR
 Override protection period with the specified number of seconds.
 .SH SIGNALS
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): 1388937165-32692-5-git-send-email-mitake.hitoshi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#20 lore raw

Re: [PATCH nilfs-utils v2 4/4] man: remove a description of -n option

Quoted 22 lines · Hitoshi Mitake
---
 man/nilfs_cleanerd.8 | 4 ----
 1 file changed, 4 deletions(-)
man/nilfs_cleanerd.8 +0 -4
index ad6d188..46caef2 100644
@@ -35,10 +35,6 @@ Display help message and exit.
 \fB\-c \fIfile\fR, \fB\-\-conf\fR=\fIfile\fR
 Specify configuration file.
 .TP
-\fB\-n\fR, \fB\-\-nofork\fR
-invoke cleanerd without forking process. This option is intended for
-internal use from \fBmount.nilfs2\fP(8).
-.TP
 \fB\-p \fIinterval\fR, \fB\-\-protection-period\fR=\fIinterval\fR
 Override protection period with the specified number of seconds.
 .SH SIGNALS
Signature · 1 line
1.8.1.2

Applied, thank you.

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