cmogstored dev/user discussion/issues/patches/etc
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: cmogstored-public@bogomips.org
Subject: [PATCH 1/2] stdin is always redirected to /dev/null
Date: Wed,  1 Jun 2016 08:28:28 +0000	[thread overview]
Message-ID: <20160601082829.24203-1-e@80x24.org> (raw)
In-Reply-To: <20160601082755.GA23982@dcvr.yhbt.net>

There is no reason for stdin to ever be connected to a terminal,
ensure we have a consistent stdin for iostat processes and the
like.
---
 cmogstored.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/cmogstored.c b/cmogstored.c
index ecfbd6e..5625775 100644
--- a/cmogstored.c
+++ b/cmogstored.c
@@ -152,12 +152,11 @@ static void dup2_null(int oldfd, int newfd, const char *errdest)
 		die_errno("dup2(/dev/null,%s) failed", errdest);
 }
 
-static void daemonize(void)
+static void daemonize(int null_fd)
 {
 	int ready_pipe[2];
 	pid_t pid;
 	ssize_t r;
-	int fd;
 
 	if (pipe(ready_pipe) < 0)
 		die_errno("pipe() failed");
@@ -191,15 +190,9 @@ static void daemonize(void)
 
 	if (chdir("/") < 0)
 		die_errno("chdir(/) failed");
-	fd = open("/dev/null", O_RDWR);
-	if (fd < 0)
-		die_errno("open(/dev/null) failed");
-
-	dup2_null(fd, STDIN_FILENO, "stdin");
-	dup2_null(fd, STDOUT_FILENO, "stdout");
-	dup2_null(fd, STDERR_FILENO, "stderr");
-	mog_close(fd);
 
+	dup2_null(null_fd, STDOUT_FILENO, "stdout");
+	dup2_null(null_fd, STDERR_FILENO, "stderr");
 	do {
 		r = write(ready_pipe[1], &pid, sizeof(pid_t));
 	} while (r < 0 && errno == EINTR);
@@ -239,6 +232,7 @@ MOG_NOINLINE static void setup(int argc, char *argv[])
 {
 	int pid_fd = -1;
 	static struct argp argp = { options, parse_opt, NULL, summary };
+	int null_fd;
 
 	mog_mnt_refresh();
 	argp_parse(&argp, argc, argv, 0, NULL, &mog_cli);
@@ -250,9 +244,16 @@ MOG_NOINLINE static void setup(int argc, char *argv[])
 
 	if (mog_cli.pidfile) pid_fd = mog_pidfile_prepare(mog_cli.pidfile);
 
+	null_fd = open("/dev/null", O_RDWR);
+	if (null_fd < 0)
+		die_errno("open(/dev/null) failed");
+	dup2_null(null_fd, STDIN_FILENO, "stdin");
+
 	/* don't daemonize if we're inheriting FDs, we're already daemonized */
 	if (mog_cli.daemonize && !getenv("CMOGSTORED_FD"))
-		daemonize();
+		daemonize(null_fd);
+
+	mog_close(null_fd);
 
 	if (pid_fd >= 0 && mog_pidfile_commit(pid_fd) < 0)
 		syslog(LOG_ERR,

  reply	other threads:[~2016-06-01  8:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-01  8:27 [PATCH 0/2] vfork safety fixes Eric Wong
2016-06-01  8:28 ` Eric Wong [this message]
2016-06-01  8:28   ` [PATCH 2/2] minor vfork/fork " Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/cmogstored/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160601082829.24203-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=cmogstored-public@bogomips.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/cmogstored.git/

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 read-only IMAP folder(s) and NNTP newsgroup(s).