Skip to contentSkip to conversation

btrfs-progs: Fix wrong optind re-initialization to allow mixed option and non-option

PATCH In review Qu Wenruo 4 participants 6 messages Jun 20, 2018 – Jul 3, 2018 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH v2 1/2 btrfs-progs: Fix wrong optind re-initialization to allow mixed option and non-option

In function handle_global_options(), we reset @optind to 1. However according to man page of getopt(3) NOTES section, if we need to rescan options later, @optind should be reset to 0 to initialize the internal variables correctly.

This explains the reason why in cmd_check(), getopt_long() doesn't handle the following command correctly:
"btrfs check /dev/data/btrfs --check-data-csum"

While mkfs.btrfs handles mixed non-option and option correctly:
"mkfs.btrfs -f /dev/data/disk1 --data raid1 /dev/data/disk2"

Cc Paul Jones <[email protected]>
Cc Hugo Mills <[email protected]>
Fixes 010ceab56e06 ("btrfs-progs: rework option parser to use getopt for global options")

---
changelog:
v2:
Instead of resetting @optind at handle_global_options(), reset @optind before each later getopt() call. Since there are cases uses @optind and expects it to be starting from 1.

---
 cmds-balance.c            | 2 ++
 cmds-device.c             | 3 +++
 cmds-fi-du.c              | 1 +
 cmds-fi-usage.c           | 1 +
 cmds-filesystem.c         | 2 ++
 cmds-inspect-dump-tree.c  | 1 +
 cmds-inspect-tree-stats.c | 1 +
 cmds-inspect.c            | 3 +++
 cmds-qgroup.c             | 3 +++
 cmds-quota.c              | 1 +
 cmds-receive.c            | 1 +
 cmds-replace.c            | 3 +++
 cmds-rescue.c             | 2 ++
 cmds-restore.c            | 1 +
 cmds-send.c               | 1 +
 cmds-subvolume.c          | 6 ++++++
 16 files changed, 32 insertions(+)
cmds-balance.c +2 -0
index 0c91bdf13ada..6cc26c358f95 100644
@@ -528,6 +528,7 @@ static int cmd_balance_start(int argc, char **argv)
 
 	memset(&args, 0, sizeof(args));
 
+	optind = 0;
 	while (1) {
 		enum { GETOPT_VAL_FULL_BALANCE = 256,
 			GETOPT_VAL_BACKGROUND = 257 };
@@ -831,6 +832,7 @@ static int cmd_balance_status(int argc, char **argv)
 	int verbose = 0;
 	int ret;
 
+	optind = 0;
 	while (1) {
 		int opt;
 		static const struct option longopts[] = {
cmds-device.c +3 -0
index 86459d1b9564..cd689b5017c8 100644
@@ -57,6 +57,7 @@ static int cmd_device_add(int argc, char **argv)
 	int force = 0;
 	int last_dev;
 
+	optind = 0;
 	while (1) {
 		int c;
 		static const struct option long_options[] = {
@@ -267,6 +268,7 @@ static int cmd_device_scan(int argc, char **argv)
 	int all = 0;
 	int ret = 0;
 
+	optind = 0;
 	while (1) {
 		int c;
 		static const struct option long_options[] = {
@@ -403,6 +405,7 @@ static int cmd_device_stats(int argc, char **argv)
 	__u64 flags = 0;
 	DIR *dirstream = NULL;
 
+	optind = 0;
 	while (1) {
 		int c;
 		static const struct option long_options[] = {
cmds-fi-du.c +1 -0
index 7e6bb7f6a570..4e639f6dc231 100644
@@ -565,6 +565,7 @@ int cmd_filesystem_du(int argc, char **argv)
 
 	unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
 
+	optind = 0;
 	while (1) {
 		static const struct option long_options[] = {
 			{ "summarize", no_argument, NULL, 's'},
cmds-fi-usage.c +1 -0
index de7ad668ba2d..cbb3cd191b3d 100644
@@ -975,6 +975,7 @@ int cmd_filesystem_usage(int argc, char **argv)
 
 	unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
 
+	optind = 0;
 	while (1) {
 		int c;
 
cmds-filesystem.c +2 -0
index 30a50bf55e38..06c8311bdfe3 100644
@@ -685,6 +685,7 @@ static int cmd_filesystem_show(int argc, char **argv)
 
 	unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
 
+	optind = 0;
 	while (1) {
 		int c;
 		static const struct option long_options[] = {
@@ -924,6 +925,7 @@ static int cmd_filesystem_defrag(int argc, char **argv)
 	defrag_global_errors = 0;
 	defrag_global_verbose = 0;
 	defrag_global_errors = 0;
+	optind = 0;
 	while(1) {
 		int c = getopt(argc, argv, "vrc::fs:l:t:");
 		if (c < 0)
cmds-inspect-dump-tree.c +1 -0
index 92a2a45b267e..c8acd55a0c3a 100644
@@ -235,6 +235,7 @@ int cmd_inspect_dump_tree(int argc, char **argv)
 	 * tree blocks as possible.
 	 */
 	open_ctree_flags = OPEN_CTREE_PARTIAL | OPEN_CTREE_NO_BLOCK_GROUPS;
+	optind = 0;
 	while (1) {
 		int c;
 		enum { GETOPT_VAL_FOLLOW = 256 };
cmds-inspect-tree-stats.c +1 -0
index eced0db9f840..dfa34c52ff5f 100644
@@ -434,6 +434,7 @@ int cmd_inspect_tree_stats(int argc, char **argv)
 	int opt;
 	int ret = 0;
 
+	optind = 0;
 	while ((opt = getopt(argc, argv, "vb")) != -1) {
 		switch (opt) {
 		case 'v':
cmds-inspect.c +3 -0
index afd7fe48df5c..2fc50c1a2d90 100644
@@ -94,6 +94,7 @@ static int cmd_inspect_inode_resolve(int argc, char **argv)
 	int ret;
 	DIR *dirstream = NULL;
 
+	optind = 0;
 	while (1) {
 		int c = getopt(argc, argv, "v");
 		if (c < 0)
@@ -148,6 +149,7 @@ static int cmd_inspect_logical_resolve(int argc, char **argv)
 	char *path_ptr;
 	DIR *dirstream = NULL;
 
+	optind = 0;
 	while (1) {
 		int c = getopt(argc, argv, "Pvs:");
 		if (c < 0)
@@ -591,6 +593,7 @@ static int cmd_inspect_min_dev_size(int argc, char **argv)
 	DIR *dirstream = NULL;
 	u64 devid = 1;
 
+	optind = 0;
 	while (1) {
 		int c;
 		enum { GETOPT_VAL_DEVID = 256 };
cmds-qgroup.c +3 -0
index 93206900693d..b928edc7c408 100644
@@ -46,6 +46,7 @@ static int _cmd_qgroup_assign(int assign, int argc, char **argv,
 	DIR *dirstream = NULL;
 
 	if (assign) {
+		optind = 0;
 		while (1) {
 			enum { GETOPT_VAL_RESCAN = 256, GETOPT_VAL_NO_RESCAN };
 			static const struct option long_options[] = {
@@ -310,6 +311,7 @@ static int cmd_qgroup_show(int argc, char **argv)
 
 	unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
 
+	optind = 0;
 	while (1) {
 		int c;
 		enum {
@@ -429,6 +431,7 @@ static int cmd_qgroup_limit(int argc, char **argv)
 	DIR *dirstream = NULL;
 	enum btrfs_util_error err;
 
+	optind = 0;
 	while (1) {
 		int c = getopt(argc, argv, "ce");
 		if (c < 0)
cmds-quota.c +1 -0
index 745889d12523..c9ea9c0f36ec 100644
@@ -119,6 +119,7 @@ static int cmd_quota_rescan(int argc, char **argv)
 	DIR *dirstream = NULL;
 	int wait_for_completion = 0;
 
+	optind = 0;
 	while (1) {
 		int c = getopt(argc, argv, "sw");
 		if (c < 0)
cmds-receive.c +1 -0
index 68123a31cc5c..34d51ef3f301 100644
@@ -1267,6 +1267,7 @@ int cmd_receive(int argc, char **argv)
 	realmnt[0] = 0;
 	fromfile[0] = 0;
 
+	optind = 0;
 	while (1) {
 		int c;
 		enum { GETOPT_VAL_DUMP = 257 };
cmds-replace.c +3 -0
index 032a44fcda3c..1fa802845f0e 100644
@@ -134,6 +134,7 @@ static int cmd_replace_start(int argc, char **argv)
 	u64 srcdev_size;
 	u64 dstdev_size;
 
+	optind = 0;
 	while ((c = getopt(argc, argv, "Brf")) != -1) {
 		switch (c) {
 		case 'B':
@@ -333,6 +334,7 @@ static int cmd_replace_status(int argc, char **argv)
 	int ret;
 	DIR *dirstream = NULL;
 
+	optind = 0;
 	while ((c = getopt(argc, argv, "1")) != -1) {
 		switch (c) {
 		case '1':
@@ -501,6 +503,7 @@ static int cmd_replace_cancel(int argc, char **argv)
 	char *path;
 	DIR *dirstream = NULL;
 
+	optind = 0;
 	while ((c = getopt(argc, argv, "")) != -1) {
 		switch (c) {
 		case '?':
cmds-rescue.c +2 -0
index c40088ad374e..0a1df5b8088b 100644
@@ -52,6 +52,7 @@ static int cmd_rescue_chunk_recover(int argc, char *argv[])
 	int yes = 0;
 	int verbose = 0;
 
+	optind = 0;
 	while (1) {
 		int c = getopt(argc, argv, "yvh");
 		if (c < 0)
@@ -119,6 +120,7 @@ static int cmd_rescue_super_recover(int argc, char **argv)
 	int yes = 0;
 	char *dname;
 
+	optind = 0;
 	while (1) {
 		int c = getopt(argc, argv, "vy");
 		if (c < 0)
cmds-restore.c +1 -0
index f228acab8276..f124e0f8ef15 100644
@@ -1440,6 +1440,7 @@ int cmd_restore(int argc, char **argv)
 	regex_t match_reg, *mreg = NULL;
 	char reg_err[256];
 
+	optind = 0;
 	while (1) {
 		int opt;
 		enum { GETOPT_VAL_PATH_REGEX = 256 };
cmds-send.c +1 -0
index c5ecdaa11999..16b9f8d2bf0b 100644
@@ -508,6 +508,7 @@ int cmd_send(int argc, char **argv)
 	send.dump_fd = fileno(stdout);
 	outname[0] = 0;
 
+	optind = 0;
 	while (1) {
 		enum { GETOPT_VAL_SEND_NO_DATA = 256 };
 		static const struct option long_options[] = {
cmds-subvolume.c +6 -0
index 45363a5ab3f8..e7a884af1f5d 100644
@@ -102,6 +102,7 @@ static int cmd_subvol_create(int argc, char **argv)
 	struct btrfs_qgroup_inherit *inherit = NULL;
 	DIR	*dirstream = NULL;
 
+	optind = 0;
 	while (1) {
 		int c = getopt(argc, argv, "c:i:");
 		if (c < 0)
@@ -248,6 +249,7 @@ static int cmd_subvol_delete(int argc, char **argv)
 	enum { COMMIT_AFTER = 1, COMMIT_EACH = 2 };
 	enum btrfs_util_error err;
 
+	optind = 0;
 	while (1) {
 		int c;
 		static const struct option long_options[] = {
@@ -466,6 +468,7 @@ static int cmd_subvol_list(int argc, char **argv)
 	filter_set = btrfs_list_alloc_filter_set();
 	comparer_set = btrfs_list_alloc_comparer_set();
 
+	optind = 0;
 	while(1) {
 		int c;
 		static const struct option long_options[] = {
@@ -636,6 +639,7 @@ static int cmd_subvol_snapshot(int argc, char **argv)
 	DIR *dirstream1 = NULL, *dirstream2 = NULL;
 
 	memset(&args, 0, sizeof(args));
+	optind = 0;
 	while (1) {
 		int c = getopt(argc, argv, "c:i:r");
 		if (c < 0)
@@ -933,6 +937,7 @@ static int cmd_subvol_show(int argc, char **argv)
 	char *subvol_path = NULL;
 	enum btrfs_util_error err;
 
+	optind = 0;
 	while (1) {
 		int c;
 		static const struct option long_options[] = {
@@ -1132,6 +1137,7 @@ static int cmd_subvol_sync(int argc, char **argv)
 	int sleep_interval = 1;
 	enum btrfs_util_error err;
 
+	optind = 0;
 	while (1) {
 		int c = getopt(argc, argv, "s:");
 
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 2/2 btrfs-progs: misc-tests: Fix 029 test cases for sudo test environment

Test misc/029 only works if the test case is executed as root, while for sudo usage, it doesn't work as initial mkdir and final cleanup doesn't use $SUDO_HELPER.

Add "run_check $SUDO_HELPER" for such cases to allow it works under sudo usage.

---
 tests/misc-tests/029-send-p-different-mountpoints/test.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/misc-tests/029-send-p-different-mountpoints/test.sh +3 -3
index 0b42b772e3d7..90465e1d08fd 100755
@@ -14,7 +14,7 @@ prepare_test_dev
 SUBVOL_MNT="$TEST_MNT/subvol"
 TOPLEVEL_MNT="$TEST_MNT/toplevel"
 TEST_MNT="$TOPLEVEL_MNT"
-mkdir -p "$TOPLEVEL_MNT" "$SUBVOL_MNT"
+run_check $SUDO_HELPER mkdir -p "$TOPLEVEL_MNT" "$SUBVOL_MNT"
 
 run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f "$TEST_DEV"
 run_check_mount_test_dev
@@ -47,5 +47,5 @@ run_mustfail_stdout "send -p on 2 mount points" \
 run_check_umount_test_dev "$SUBVOL_MNT"
 run_check_umount_test_dev "$TOPLEVEL_MNT"
 
-rmdir "$SUBVOL_MNT"
-rmdir "$TOPLEVEL_MNT"
+run_check $SUDO_HELPER rmdir "$SUBVOL_MNT"
+run_check $SUDO_HELPER rmdir "$TOPLEVEL_MNT"
Signature · 1 line
2.17.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] btrfs-progs: misc-tests: Fix 029 test cases for sudo test environment

Quoted 8 lines · Qu Wenruo

Test misc/029 only works if the test case is executed as root, while for sudo usage, it doesn't work as initial mkdir and final cleanup doesn't use $SUDO_HELPER.

Add "run_check $SUDO_HELPER" for such cases to allow it works under sudo usage.

Although this seems unrelated to the first patch. I'd advise in this case to send them as separate patches or if you are going to batch up multiple fixes add a cover letter saying "just a bunch of random fixes to xxx" :)

Quoted 26 lines
---
 tests/misc-tests/029-send-p-different-mountpoints/test.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/misc-tests/029-send-p-different-mountpoints/test.sh +3 -3
index 0b42b772e3d7..90465e1d08fd 100755
@@ -14,7 +14,7 @@ prepare_test_dev
 SUBVOL_MNT="$TEST_MNT/subvol"
 TOPLEVEL_MNT="$TEST_MNT/toplevel"
 TEST_MNT="$TOPLEVEL_MNT"
-mkdir -p "$TOPLEVEL_MNT" "$SUBVOL_MNT"
+run_check $SUDO_HELPER mkdir -p "$TOPLEVEL_MNT" "$SUBVOL_MNT"
 
 run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f "$TEST_DEV"
 run_check_mount_test_dev
@@ -47,5 +47,5 @@ run_mustfail_stdout "send -p on 2 mount points" \
 run_check_umount_test_dev "$SUBVOL_MNT"
 run_check_umount_test_dev "$TOPLEVEL_MNT"
 
-rmdir "$SUBVOL_MNT"
-rmdir "$TOPLEVEL_MNT"
+run_check $SUDO_HELPER rmdir "$SUBVOL_MNT"
+run_check $SUDO_HELPER rmdir "$TOPLEVEL_MNT"
#3 lore raw
In reply to Qu Wenruo
Quoted 16 lines · Qu Wenruo

In function handle_global_options(), we reset @optind to 1. However according to man page of getopt(3) NOTES section, if we need to rescan options later, @optind should be reset to 0 to initialize the internal variables correctly.

This explains the reason why in cmd_check(), getopt_long() doesn't handle the following command correctly:
"btrfs check /dev/data/btrfs --check-data-csum"

While mkfs.btrfs handles mixed non-option and option correctly:
"mkfs.btrfs -f /dev/data/disk1 --data raid1 /dev/data/disk2"

Cc Paul Jones <[email protected]>
Cc Hugo Mills <[email protected]>
Fixes 010ceab56e06 ("btrfs-progs: rework option parser to use getopt for global options")
Quoted 6 lines

---
changelog:
v2:
Instead of resetting @optind at handle_global_options(), reset @optind before each later getopt() call. Since there are cases uses @optind and expects it to be starting from 1.

So it's not possible to set it once before the callbacks are called in btrfs.c:main() due to the exceptions that need it to be set to 1. Ok, can you please send a separate patch that makes the optind = 1 explicit? Even it might be redundant in the context, it's a way to document the expected behaviour of getopt and also we would not have to think if the optind setting is missing or not before the getopts. Thanks.

#4 lore raw
Quoted 24 lines · David Sterba
Quoted 16 lines · Qu Wenruo

In function handle_global_options(), we reset @optind to 1. However according to man page of getopt(3) NOTES section, if we need to rescan options later, @optind should be reset to 0 to initialize the internal variables correctly.

This explains the reason why in cmd_check(), getopt_long() doesn't handle the following command correctly:
"btrfs check /dev/data/btrfs --check-data-csum"

While mkfs.btrfs handles mixed non-option and option correctly:
"mkfs.btrfs -f /dev/data/disk1 --data raid1 /dev/data/disk2"

Cc Paul Jones <[email protected]>
Cc Hugo Mills <[email protected]>
Fixes 010ceab56e06 ("btrfs-progs: rework option parser to use getopt for global options")
Quoted 6 lines

---
changelog:
v2:
Instead of resetting @optind at handle_global_options(), reset @optind before each later getopt() call. Since there are cases uses @optind and expects it to be starting from 1.

Quoted 3 lines

So it's not possible to set it once before the callbacks are called in btrfs.c:main() due to the exceptions that need it to be set to 1. Ok, can you please send a separate patch that makes the optind = 1 explicit?

It's already done in handle_global_options(), so every subcommand is getting @optind set to 1 already.

Thanks,
Qu

Quoted 8 lines

Even it might be redundant in the context, it's a way to document the expected behaviour of getopt and also we would not have to think if the optind setting is missing or not before the getopts. Thanks. --
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
In reply to Qu Wenruo
Quoted 16 lines · Qu Wenruo

In function handle_global_options(), we reset @optind to 1. However according to man page of getopt(3) NOTES section, if we need to rescan options later, @optind should be reset to 0 to initialize the internal variables correctly.

This explains the reason why in cmd_check(), getopt_long() doesn't handle the following command correctly:
"btrfs check /dev/data/btrfs --check-data-csum"

While mkfs.btrfs handles mixed non-option and option correctly:
"mkfs.btrfs -f /dev/data/disk1 --data raid1 /dev/data/disk2"

Cc Paul Jones <[email protected]>
Cc Hugo Mills <[email protected]>
Fixes 010ceab56e06 ("btrfs-progs: rework option parser to use getopt for global options")
Quoted 6 lines

---
changelog:
v2:
Instead of resetting @optind at handle_global_options(), reset @optind before each later getopt() call. Since there are cases uses @optind and expects it to be starting from 1.

1 and 2 applied, thanks.