diff mbox series

[RFC,1/3] btrfs-progs: move '--background' fork()s to do_balance()

Message ID 20200408131508.GB19101@jkm (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: balance --bg invocation | expand

Commit Message

Petr Janecek April 8, 2020, 1:15 p.m. UTC
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 mbox series

Patch

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);
 }