linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Petr Janecek <janecek@ucw.cz>
To: linux-btrfs@vger.kernel.org
Subject: [RFC PATCH 1/3] btrfs-progs: move '--background' fork()s to do_balance()
Date: Wed, 8 Apr 2020 15:15:09 +0200	[thread overview]
Message-ID: <20200408131508.GB19101@jkm> (raw)

Move the '--background' forking to happen after btrfs_open_dir(),
so that the possible 'wrong path' error message can be seen the user.
---
 cmds/balance.c | 78 ++++++++++++++++++++++++++------------------------
 1 file changed, 40 insertions(+), 38 deletions(-)

diff --git a/cmds/balance.c b/cmds/balance.c
index 5392a604..fada2ab3 100644
--- a/cmds/balance.c
+++ b/cmds/balance.c
@@ -426,8 +426,9 @@ static int do_balance_v1(int fd)
 }
 
 enum {
-	BALANCE_START_FILTERS = 1 << 0,
-	BALANCE_START_NOWARN  = 1 << 1
+	BALANCE_START_FILTERS    = 1 << 0,
+	BALANCE_START_NOWARN     = 1 << 1,
+	BALANCE_START_BACKGROUND = 1 << 2
 };
 
 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
@@ -436,11 +437,47 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
 	int fd;
 	int ret;
 	DIR *dirstream = NULL;
+	int i __attribute__((unused));
 
 	fd = btrfs_open_dir(path, &dirstream, 1);
 	if (fd < 0)
 		return 1;
 
+	if (flags & BALANCE_START_BACKGROUND) {
+		switch (fork()) {
+		case (-1):
+			error("unable to fork to run balance in background");
+			return 1;
+		case (0):
+			setsid();
+			switch(fork()) {
+			case (-1):
+				error(
+				"unable to fork to run balance in background");
+				exit(1);
+			case (0):
+				/*
+				 * Read the return value to silence compiler
+				 * warning. Change to / should succeed and
+				 * we're not in a security-sensitive context.
+				 */
+				i = chdir("/");
+				close(0);
+				close(1);
+				close(2);
+				open("/dev/null", O_RDONLY);
+				open("/dev/null", O_WRONLY);
+				open("/dev/null", O_WRONLY);
+				break;
+			default:
+				exit(0);
+			}
+			break;
+		default:
+			exit(0);
+		}
+	}
+
 	ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
 	if (ret < 0) {
 		/*
@@ -510,7 +547,6 @@ static int cmd_balance_start(const struct cmd_struct *cmd,
 						&args.meta, NULL };
 	int force = 0;
 	int verbose = 0;
-	int background = 0;
 	unsigned start_flags = 0;
 	int i;
 
@@ -570,7 +606,7 @@ static int cmd_balance_start(const struct cmd_struct *cmd,
 			start_flags |= BALANCE_START_NOWARN;
 			break;
 		case GETOPT_VAL_BACKGROUND:
-			background = 1;
+			start_flags |= BALANCE_START_BACKGROUND;
 			break;
 		default:
 			usage_unknown_option(cmd, argv);
@@ -642,40 +678,6 @@ static int cmd_balance_start(const struct cmd_struct *cmd,
 		args.flags |= BTRFS_BALANCE_FORCE;
 	if (verbose)
 		dump_ioctl_balance_args(&args);
-	if (background) {
-		switch (fork()) {
-		case (-1):
-			error("unable to fork to run balance in background");
-			return 1;
-		case (0):
-			setsid();
-			switch(fork()) {
-			case (-1):
-				error(
-				"unable to fork to run balance in background");
-				exit(1);
-			case (0):
-				/*
-				 * Read the return value to silence compiler
-				 * warning. Change to / should succeed and
-				 * we're not in a security-sensitive context.
-				 */
-				i = chdir("/");
-				close(0);
-				close(1);
-				close(2);
-				open("/dev/null", O_RDONLY);
-				open("/dev/null", O_WRONLY);
-				open("/dev/null", O_WRONLY);
-				break;
-			default:
-				exit(0);
-			}
-			break;
-		default:
-			exit(0);
-		}
-	}
 
 	return do_balance(argv[optind], &args, start_flags);
 }
-- 
2.26.0


                 reply	other threads:[~2020-04-08 13:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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

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

  git send-email \
    --in-reply-to=20200408131508.GB19101@jkm \
    --to=janecek@ucw.cz \
    --cc=linux-btrfs@vger.kernel.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.
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 NNTP newsgroup(s).