* [RFC PATCH 1/3] btrfs-progs: move '--background' fork()s to do_balance()
@ 2020-04-08 13:15 Petr Janecek
0 siblings, 0 replies; only message in thread
From: Petr Janecek @ 2020-04-08 13:15 UTC (permalink / raw)
To: linux-btrfs
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
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2020-04-08 13:15 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-08 13:15 [RFC PATCH 1/3] btrfs-progs: move '--background' fork()s to do_balance() Petr Janecek
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).