Skip to contentSkip to conversation

tunegfs2: Use O_EXCL when opening the device rw

PATCH New Andrew Price 1 participant 1 message Nov 4, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tunegfs2: Use O_EXCL when opening the device rw

Use O_RDONLY when listing and O_RDWR|O_EXCL when modifying.

Also don't print the tunegfs2 version with -l output, improve open() error reporting and add some more basic tunegfs2 tests.

---
 gfs2/tune/main.c | 12 ++++++------
 tests/tune.at    | 30 ++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 6 deletions(-)
gfs2/tune/main.c +6 -6
index fe137803..f72bdc3c 100644
@@ -65,6 +65,7 @@ static void version(void)
 int main(int argc, char **argv)
 {
 	int c, status;
+	int flags = O_RDWR | O_EXCL;
 
 	memset(tfs, 0, sizeof(struct tunegfs2));
 	while((c = getopt(argc, argv, "hL:U:lo:Vr:")) != -1) {
@@ -82,6 +83,7 @@ int main(int argc, char **argv)
 			break;
 		case 'l':
 			tfs->opt_list = 1;
+			flags = O_RDONLY;
 			break;
 		case 'o':
 			parse_mount_options(optarg);
@@ -113,11 +115,11 @@ int main(int argc, char **argv)
 	}
 
 	tfs->devicename = argv[optind];
-	tfs->fd = open(tfs->devicename, O_RDWR); 
+	tfs->fd = open(tfs->devicename, flags);
 
 	if (tfs->fd < 0) {
-		fprintf(stderr, _("Unable to open device %s\n"),
-				tfs->devicename);
+		fprintf(stderr, _("Unable to open device %s: %s\n"),
+				tfs->devicename, strerror(errno));
 		return EX_IOERR;
 	}
 
@@ -160,10 +162,8 @@ int main(int argc, char **argv)
 			goto out;
 	}
 
-	if (tfs->opt_list) {
-		version();
+	if (tfs->opt_list)
 		print_super(tfs);
-	}
 
 	close(tfs->fd);
 out:
tests/tune.at +30 -0
index b0158034..43ce34ce 100644
@@ -15,3 +15,33 @@ AT_CHECK([$GFS_MKFS -p lock_nolock -o format=1801 $GFS_TGT], 0, [ignore], [ignor
 AT_CHECK([tunegfs2 -r 1802 $GFS_TGT], 0, [ignore], [ignore])
 AT_CHECK([fsck.gfs2 -n $GFS_TGT], 0, [ignore], [ignore])
 AT_CLEANUP
+
+AT_SETUP([Handle bad fs])
+AT_KEYWORDS(tunegfs2 tune)
+AT_CHECK([tunegfs2 -l /dev/null], 74, [ignore], [Device does not contain a GFS or GFS2 file system
+])
+AT_CHECK([tunegfs2 -l /dev/zero], 74, [ignore], [Device does not contain a GFS or GFS2 file system
+])
+AT_CLEANUP
+
+AT_SETUP([Expected output])
+AT_KEYWORDS(tunegfs2 tune)
+AT_CHECK([echo -n "File system volume name: cluster:fsname
+File system UUID: e90815fc-087b-460c-b8a5-8edc8a2ed691
+File system magic number: 0x1161970
+File system format version: 1802
+Block size: 4096
+Block shift: 12
+Root inode: 16709
+Master inode: 16437
+Lock protocol: lock_nolock
+Lock table: cluster:fsname
+" > expout], 0, [ignore], [ignore])
+# Create the fs without alignment so that we can predict the root inode address
+AT_CHECK([$GFS_MKFS -p lock_nolock -t cluster:fsname -o align=0 $GFS_TGT], 0, [ignore], [ignore])
+# Set a known UUID to ease comparison
+AT_CHECK([tunegfs2 -U e90815fc-087b-460c-b8a5-8edc8a2ed691 $GFS_TGT], 0, [ignore], [ignore])
+# Check tunegfs2 set the UUID correctly with gfs2_edit
+AT_CHECK([gfs2_edit -p sb field sb_uuid $GFS_TGT | tr -d '\n'], 0, [e90815fc-087b-460c-b8a5-8edc8a2ed691], [ignore])
+AT_CHECK([tunegfs2 -l $GFS_TGT], 0, [expout], [stderr])
+AT_CLEANUP
Signature · 1 line
2.31.1