diff mbox

[1/2] btrfs-progs: add necessary close(fd) in mkfs

Message ID 1503045166-18612-1-git-send-email-gujx@cn.fujitsu.com (mailing list archive)
State New, archived
Headers show

Commit Message

Gu Jinxiang Aug. 18, 2017, 8:32 a.m. UTC
Add some close(fd) when error occures in mkfs.
And add close(fd) when end use it.

Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
---
 mkfs/main.c | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Nikolay Borisov Aug. 18, 2017, 8:39 a.m. UTC | #1
On 18.08.2017 11:32, Gu Jinxiang wrote:
> Add some close(fd) when error occures in mkfs.
> And add close(fd) when end use it.

When a process exits all of its fds (and all other runtime resources
tracked by the kernel) are freed, so we don't leak fds if that's your
worry. Admittedly, it's good practice to manage the lifetime of fds
correctly in a program, so I don't have any strong preferences either ways.


> 
> Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
> ---
>  mkfs/main.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/mkfs/main.c b/mkfs/main.c
> index 2b109a5..ec82565 100644
> --- a/mkfs/main.c
> +++ b/mkfs/main.c
> @@ -1694,6 +1694,7 @@ int main(int argc, char **argv)
>  					file,
>  					(unsigned long long)block_count,
>  					(unsigned long long)dev_block_count);
> +			close(fd);
>  			exit(1);
>  		}
>  	} else {
> @@ -1711,6 +1712,7 @@ int main(int argc, char **argv)
>  		ret = zero_output_file(fd, block_count);
>  		if (ret) {
>  			error("unable to zero the output file");
> +			close(fd);
>  			exit(1);
>  		}
>  		/* our "device" is the new image file */
> @@ -1721,6 +1723,7 @@ int main(int argc, char **argv)
>  	if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
>  		error("device is too small to make filesystem, must be at least %llu",
>  				(unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE);
> +		close(fd);
>  		exit(1);
>  	}
>  
> @@ -1740,6 +1743,7 @@ int main(int argc, char **argv)
>  	ret = make_btrfs(fd, &mkfs_cfg);
>  	if (ret) {
>  		error("error during mkfs: %s", strerror(-ret));
> +		close(fd);
>  		exit(1);
>  	}
>  
> @@ -1750,6 +1754,7 @@ int main(int argc, char **argv)
>  		close(fd);
>  		exit(1);
>  	}
> +	close(fd);
>  	root = fs_info->fs_root;
>  	fs_info->alloc_start = alloc_start;
>  
> @@ -1827,6 +1832,7 @@ int main(int argc, char **argv)
>  					sectorsize, sectorsize, sectorsize);
>  		if (ret) {
>  			error("unable to add %s to filesystem: %d", file, ret);
> +			close(fd);
>  			goto out;
>  		}
>  		if (verbose >= 2) {
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
David Sterba Aug. 21, 2017, 5:20 p.m. UTC | #2
On Fri, Aug 18, 2017 at 01:32:45AM -0700, Gu Jinxiang wrote:
> Add some close(fd) when error occures in mkfs.
> And add close(fd) when end use it.

Can you please rework it so all the in-place exists are gotos to a
common exit block that does the close(fd) cleanup? Thanks.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/mkfs/main.c b/mkfs/main.c
index 2b109a5..ec82565 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -1694,6 +1694,7 @@  int main(int argc, char **argv)
 					file,
 					(unsigned long long)block_count,
 					(unsigned long long)dev_block_count);
+			close(fd);
 			exit(1);
 		}
 	} else {
@@ -1711,6 +1712,7 @@  int main(int argc, char **argv)
 		ret = zero_output_file(fd, block_count);
 		if (ret) {
 			error("unable to zero the output file");
+			close(fd);
 			exit(1);
 		}
 		/* our "device" is the new image file */
@@ -1721,6 +1723,7 @@  int main(int argc, char **argv)
 	if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
 		error("device is too small to make filesystem, must be at least %llu",
 				(unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE);
+		close(fd);
 		exit(1);
 	}
 
@@ -1740,6 +1743,7 @@  int main(int argc, char **argv)
 	ret = make_btrfs(fd, &mkfs_cfg);
 	if (ret) {
 		error("error during mkfs: %s", strerror(-ret));
+		close(fd);
 		exit(1);
 	}
 
@@ -1750,6 +1754,7 @@  int main(int argc, char **argv)
 		close(fd);
 		exit(1);
 	}
+	close(fd);
 	root = fs_info->fs_root;
 	fs_info->alloc_start = alloc_start;
 
@@ -1827,6 +1832,7 @@  int main(int argc, char **argv)
 					sectorsize, sectorsize, sectorsize);
 		if (ret) {
 			error("unable to add %s to filesystem: %d", file, ret);
+			close(fd);
 			goto out;
 		}
 		if (verbose >= 2) {