All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <darrick.wong@oracle.com>
To: Eric Sandeen <sandeen@sandeen.net>
Cc: linux-xfs@vger.kernel.org
Subject: Re: [PATCH 6/7] mkfs: consolidate and streamline config opening function
Date: Tue, 12 Jun 2018 15:21:16 -0700	[thread overview]
Message-ID: <20180612222116.GR22045@magnolia> (raw)
In-Reply-To: <1528831883-21879-7-git-send-email-sandeen@sandeen.net>

On Tue, Jun 12, 2018 at 02:31:22PM -0500, Eric Sandeen wrote:
> There was a lot of duplication between open_cli_config and open_config_file
> which was unnecessary.  Collapse all the logic into one function.
> 
> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
> Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
> ---
>  mkfs/config.c   | 148 +++++++++++++++++++++-----------------------------------
>  mkfs/xfs_mkfs.c |   1 -
>  2 files changed, 56 insertions(+), 93 deletions(-)
> 
> diff --git a/mkfs/config.c b/mkfs/config.c
> index 940a055..173ab9a 100644
> --- a/mkfs/config.c
> +++ b/mkfs/config.c
> @@ -523,60 +523,6 @@ config_stat_check(
>  	return 0;
>  }
>  
> -/*
> - * If the file is not found -1 is returned and errno set. Otherwise
> - * the file descriptor is returned.
> - */
> -int
> -open_cli_config(
> -	int		dirfd,
> -	const char	*cli_config_file,
> -	char		**fpath)
> -{
> -	int		fd = -1, len, ret;
> -	struct stat	st;
> -
> -	fd = openat(AT_FDCWD, cli_config_file, O_NOFOLLOW, O_RDONLY);
> -	if (fd < 0) {
> -		len = snprintf(*fpath, PATH_MAX, "%s/%s", MKFS_XFS_CONF_DIR,
> -			       cli_config_file);
> -		/* Indicates truncation */
> -		if (len >= PATH_MAX) {
> -			errno = ENAMETOOLONG;
> -			goto out;
> -		}
> -
> -		fd = openat(dirfd, cli_config_file, O_NOFOLLOW, O_RDONLY);
> -		if (fd < 0)
> -			goto out;
> -
> -		ret = fstat(fd, &st);
> -		if (ret != 0)
> -			goto err_out_close;
> -
> -		ret = config_stat_check(&st);
> -		if (ret != 0)
> -			goto err_out_close;
> -
> -		goto out;
> -	}
> -
> -	memcpy(*fpath, cli_config_file, strlen(cli_config_file));
> -
> -	ret = fstat(fd, &st);
> -	if (ret != 0)
> -		goto err_out_close;
> -
> -	ret = config_stat_check(&st);
> -	if (ret != 0)
> -		goto err_out_close;
> -out:
> -	return fd;
> -err_out_close:
> -	close(fd);
> -	return -1;
> -}
> -
>  #ifndef O_PATH
>  #if defined __alpha__
>  #define O_PATH		040000000
> @@ -589,13 +535,26 @@ err_out_close:
>  #endif
>  #endif /* O_PATH */
>  
> +/*
> + * Try to open a config file, either cli-specified, or default.
> + *
> + * If specified on commandline, search relative to pwd or absolute path.
> + * If not specified or if above fails, try either cli-spec'd file or "default"
> + * in MKFS_XFS_CONF_DIR.
> + *
> + * If any config file is successfully opened, dft->type is set to reflect the
> + * source.
> + *
> + * If a cli-specified file is not found -1 is returned and errno set. Otherwise
> + * the file descriptor is returned.
> + */
>  int
>  open_config_file(
> -	const char			*cli_config_file,
> +	const char			*config_file,
>  	struct mkfs_default_params	*dft,
> -	char				**fpath)	/* path where config is found */
> +	char				**fpath)	/* path where we found config */

Move the comment to the pre-function comment and who is expected to get
rid of it?

"If a config file is found, its path is returned in **fpath.  This
memory must be free()'d by the caller."

Otherwise seems fine so far...

--D

>  {
> -	int				dirfd, fd = -1, len, ret;
> +	int				dirfd = -1, fd = -1, len, ret = 0;
>  	struct stat			st;
>  
>  	*fpath = malloc(PATH_MAX);
> @@ -604,57 +563,62 @@ open_config_file(
>  
>  	memset(*fpath, 0, PATH_MAX);
>  
> -	dirfd = open(MKFS_XFS_CONF_DIR, O_PATH|O_NOFOLLOW|O_DIRECTORY);
> -
> -	if (cli_config_file) {
> -		if (strlen(cli_config_file) > PATH_MAX) {
> +	/* first try relative to pwd or absolute path to cli configfile */
> +	if (config_file) {
> +		dft->type = DEFAULTS_CLI_CONFIG;
> +		if (strlen(config_file) > PATH_MAX) {
>  			errno = ENAMETOOLONG;
>  			goto out;
>  		}
> -		fd = open_cli_config(dirfd, cli_config_file, fpath);
> -		goto out;
> +		memcpy(*fpath, config_file, strlen(config_file));
> +		fd = openat(AT_FDCWD, config_file, O_NOFOLLOW, O_RDONLY);
>  	}
>  
> -	fd = openat(dirfd, "default", O_NOFOLLOW, O_RDONLY);
> -	if (fd < 0)
> -		goto out;
> -
> -	dft->type = DEFAULTS_CONFIG;
> -
> -	len = snprintf(*fpath, PATH_MAX, "%s/%s", MKFS_XFS_CONF_DIR, "default");
> -	/* Indicates truncation */
> -	if (len >= PATH_MAX) {
> -		errno = ENAMETOOLONG;
> -		goto err_out_close;
> +	/* on failure search for cli config or default file in sysconfdir */
> +	if (fd < 0) {
> +		if (!config_file)
> +			config_file = "default";

Use a symbolic constant instead of "default" here (and elsewhere)?

> +		len = snprintf(*fpath, PATH_MAX, "%s/%s", MKFS_XFS_CONF_DIR,
> +				config_file);
> +		/* Indicates truncation */
> +		if (len >= PATH_MAX) {
> +			errno = ENAMETOOLONG;
> +			goto out;
> +		}
> +		dirfd = open(MKFS_XFS_CONF_DIR, O_PATH|O_NOFOLLOW|O_DIRECTORY);
> +		if (dirfd < 0)
> +			goto out;
> +		fd = openat(dirfd, config_file, O_NOFOLLOW, O_RDONLY);
> +		if (fd < 0)
> +			goto out;
> +		if (!strcmp(config_file, "default"))
> +			dft->type = DEFAULTS_CONFIG;
>  	}
>  
>  	ret = fstat(fd, &st);
>  	if (ret != 0)
> -		goto err_out_close;
> -
> +		goto out;
>  	ret = config_stat_check(&st);
>  	if (ret != 0)
> -		goto err_out_close;
> -
> +		goto out;
> +	
>  out:
> -	if (fd < 0) {
> -		if (dft->type != DEFAULTS_BUILTIN) {
> -			fprintf(stderr,
> +	/* stat check is always fatal; missing is fatal only if cli-specified */
> +	if (ret ||
> +	    (fd < 0 && dft->type == DEFAULTS_CLI_CONFIG)) {
> +		fprintf(stderr,
>  _("Unable to open %s config file: %s : %s\n"),
> -				default_type_str(dft->type), *fpath,
> -				strerror(errno));
> -			free(*fpath);
> -			exit(1);
> -		}
> +			default_type_str(dft->type), *fpath,
> +			strerror(errno));
> +		free(*fpath);
> +		exit(1);
>  	}
> +
> +	if (ret && fd >= 0)
> +		close(fd);
>  	if (dirfd >= 0)
>  		close(dirfd);
> -	return fd;
> -
> -err_out_close:
> -	close(fd);
> -	fd = -1;
> -	goto out;
> +	return ret ? ret : fd;
>  }
>  
>  /*
> diff --git a/mkfs/xfs_mkfs.c b/mkfs/xfs_mkfs.c
> index be3094b..acd98f6 100644
> --- a/mkfs/xfs_mkfs.c
> +++ b/mkfs/xfs_mkfs.c
> @@ -3785,7 +3785,6 @@ _("respecification of configuration not allowed\n"));
>  				exit(1);
>  			}
>  			cli_config_file = optarg;
> -			dft.type = DEFAULTS_CLI_CONFIG;
>  			break;
>  		default:
>  			continue;
> -- 
> 1.8.3.1
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2018-06-12 22:21 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-12 19:31 [PATCH 0/7] mkfs config support cleanups Eric Sandeen
2018-06-12 19:31 ` [PATCH 1/7] mkfs: document the config file option Eric Sandeen
2018-06-12 22:13   ` Darrick J. Wong
2018-06-12 19:31 ` [PATCH 2/7] mkfs: move mkfs.xfs.8 to an .in file Eric Sandeen
2018-06-12 19:45   ` Eric Sandeen
2018-06-12 22:10     ` Darrick J. Wong
2018-06-12 23:15       ` Eric Sandeen
2018-06-12 19:31 ` [PATCH 3/7] mkfs: parameterize sysconfdir in mkfs.xfs.8 Eric Sandeen
2018-06-12 22:09   ` Darrick J. Wong
2018-06-12 19:31 ` [PATCH 4/7] mkfs: tidy up whitespace in mkfs/config.c Eric Sandeen
2018-06-12 22:09   ` Darrick J. Wong
2018-06-12 19:31 ` [PATCH 5/7] mkfs: properly fix TOCTOU open/stat race in config file handling Eric Sandeen
2018-06-12 22:14   ` Darrick J. Wong
2018-06-12 19:31 ` [PATCH 6/7] mkfs: consolidate and streamline config opening function Eric Sandeen
2018-06-12 22:21   ` Darrick J. Wong [this message]
2018-06-13  0:01     ` Darrick J. Wong
2018-06-12 19:31 ` [PATCH 7/7] mkfs: remove gotos in parse_defaults_file Eric Sandeen
2018-06-12 22:23   ` Darrick J. Wong
2018-06-12 22:27 ` [PATCH 0/7] mkfs config support cleanups Darrick J. Wong
2018-06-12 23:17   ` Eric Sandeen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180612222116.GR22045@magnolia \
    --to=darrick.wong@oracle.com \
    --cc=linux-xfs@vger.kernel.org \
    --cc=sandeen@sandeen.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.