diff mbox series

[17/21] libmultipath: alias.c: factor out read_binding()

Message ID 20230901180235.23980-18-mwilck@suse.com (mailing list archive)
State Not Applicable, archived
Delegated to: christophe varoqui
Headers show
Series multipath-tools: user-friendly names rework | expand

Commit Message

Martin Wilck Sept. 1, 2023, 6:02 p.m. UTC
From: Martin Wilck <mwilck@suse.com>

This way we can test the parsing of input lines from the bindings
file more easily.

Signed-off-by: Martin Wilck <mwilck@suse.com>
---
 libmultipath/alias.c | 58 ++++++++++++++++++++++++++++++--------------
 1 file changed, 40 insertions(+), 18 deletions(-)

Comments

Benjamin Marzinski Sept. 6, 2023, 10:45 p.m. UTC | #1
On Fri, Sep 01, 2023 at 08:02:30PM +0200, mwilck@suse.com wrote:
> From: Martin Wilck <mwilck@suse.com>
> 
> This way we can test the parsing of input lines from the bindings
> file more easily.
> 
Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>
> Signed-off-by: Martin Wilck <mwilck@suse.com>
> ---
>  libmultipath/alias.c | 58 ++++++++++++++++++++++++++++++--------------
>  1 file changed, 40 insertions(+), 18 deletions(-)
> 
> diff --git a/libmultipath/alias.c b/libmultipath/alias.c
> index 9ca5da8..2f9bc82 100644
> --- a/libmultipath/alias.c
> +++ b/libmultipath/alias.c
> @@ -647,6 +647,43 @@ void cleanup_bindings(void)
>  	free_bindings(&global_bindings);
>  }
>  
> +enum {
> +	READ_BINDING_OK,
> +	READ_BINDING_SKIP,
> +};
> +
> +static int read_binding(char *line, unsigned int linenr, char **alias,
> +			char **wwid) {
> +	char *c, *saveptr;
> +
> +	c = strpbrk(line, "#\n\r");
> +	if (c)
> +		*c = '\0';
> +
> +	*alias = strtok_r(line, " \t", &saveptr);
> +	if (!*alias) /* blank line */
> +		return READ_BINDING_SKIP;
> +
> +	*wwid = strtok_r(NULL, " \t", &saveptr);
> +	if (!*wwid) {
> +		condlog(1, "invalid line %u in bindings file, missing WWID",
> +			linenr);
> +		return READ_BINDING_SKIP;
> +	}
> +	if (strlen(*wwid) > WWID_SIZE - 1) {
> +		condlog(3,
> +			"Ignoring too large wwid at %u in bindings file",
> +			linenr);
> +		return READ_BINDING_SKIP;
> +	}
> +	c = strtok_r(NULL, " \t", &saveptr);
> +	if (c)
> +		/* This is non-fatal */
> +		condlog(1, "invalid line %d in bindings file, extra args \"%s\"",
> +			linenr, c);
> +	return READ_BINDING_OK;
> +}
> +
>  static int _check_bindings_file(const struct config *conf, FILE *file,
>  				 Bindings *bindings)
>  {
> @@ -658,27 +695,12 @@ static int _check_bindings_file(const struct config *conf, FILE *file,
>  
>  	pthread_cleanup_push(cleanup_free_ptr, &line);
>  	while ((n = getline(&line, &line_len, file)) >= 0) {
> -		char *c, *alias, *wwid, *saveptr;
> +		char *alias, *wwid;
>  		const char *mpe_wwid;
>  
> -		linenr++;
> -		c = strpbrk(line, "#\n\r");
> -		if (c)
> -			*c = '\0';
> -		alias = strtok_r(line, " \t", &saveptr);
> -		if (!alias) /* blank line */
> +		if (read_binding(line, ++linenr, &alias, &wwid)
> +		    == READ_BINDING_SKIP)
>  			continue;
> -		wwid = strtok_r(NULL, " \t", &saveptr);
> -		if (!wwid) {
> -			condlog(1, "invalid line %d in bindings file, missing WWID",
> -				linenr);
> -			continue;
> -		}
> -		c = strtok_r(NULL, " \t", &saveptr);
> -		if (c)
> -			/* This is non-fatal */
> -			condlog(1, "invalid line %d in bindings file, extra args \"%s\"",
> -				linenr, c);
>  
>  		mpe_wwid = get_mpe_wwid(conf->mptable, alias);
>  		if (mpe_wwid && strcmp(mpe_wwid, wwid)) {
> -- 
> 2.41.0
--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel
diff mbox series

Patch

diff --git a/libmultipath/alias.c b/libmultipath/alias.c
index 9ca5da8..2f9bc82 100644
--- a/libmultipath/alias.c
+++ b/libmultipath/alias.c
@@ -647,6 +647,43 @@  void cleanup_bindings(void)
 	free_bindings(&global_bindings);
 }
 
+enum {
+	READ_BINDING_OK,
+	READ_BINDING_SKIP,
+};
+
+static int read_binding(char *line, unsigned int linenr, char **alias,
+			char **wwid) {
+	char *c, *saveptr;
+
+	c = strpbrk(line, "#\n\r");
+	if (c)
+		*c = '\0';
+
+	*alias = strtok_r(line, " \t", &saveptr);
+	if (!*alias) /* blank line */
+		return READ_BINDING_SKIP;
+
+	*wwid = strtok_r(NULL, " \t", &saveptr);
+	if (!*wwid) {
+		condlog(1, "invalid line %u in bindings file, missing WWID",
+			linenr);
+		return READ_BINDING_SKIP;
+	}
+	if (strlen(*wwid) > WWID_SIZE - 1) {
+		condlog(3,
+			"Ignoring too large wwid at %u in bindings file",
+			linenr);
+		return READ_BINDING_SKIP;
+	}
+	c = strtok_r(NULL, " \t", &saveptr);
+	if (c)
+		/* This is non-fatal */
+		condlog(1, "invalid line %d in bindings file, extra args \"%s\"",
+			linenr, c);
+	return READ_BINDING_OK;
+}
+
 static int _check_bindings_file(const struct config *conf, FILE *file,
 				 Bindings *bindings)
 {
@@ -658,27 +695,12 @@  static int _check_bindings_file(const struct config *conf, FILE *file,
 
 	pthread_cleanup_push(cleanup_free_ptr, &line);
 	while ((n = getline(&line, &line_len, file)) >= 0) {
-		char *c, *alias, *wwid, *saveptr;
+		char *alias, *wwid;
 		const char *mpe_wwid;
 
-		linenr++;
-		c = strpbrk(line, "#\n\r");
-		if (c)
-			*c = '\0';
-		alias = strtok_r(line, " \t", &saveptr);
-		if (!alias) /* blank line */
+		if (read_binding(line, ++linenr, &alias, &wwid)
+		    == READ_BINDING_SKIP)
 			continue;
-		wwid = strtok_r(NULL, " \t", &saveptr);
-		if (!wwid) {
-			condlog(1, "invalid line %d in bindings file, missing WWID",
-				linenr);
-			continue;
-		}
-		c = strtok_r(NULL, " \t", &saveptr);
-		if (c)
-			/* This is non-fatal */
-			condlog(1, "invalid line %d in bindings file, extra args \"%s\"",
-				linenr, c);
 
 		mpe_wwid = get_mpe_wwid(conf->mptable, alias);
 		if (mpe_wwid && strcmp(mpe_wwid, wwid)) {