Subject: Re: [iproute2 04/22] tc/tc_util: Read class names from provided path, /etc/, /usr

From: Petr Machata <[email-masked]>
Date: Thu, 20 Jul 2023 12:10:50 +0200

Gioele Barabucci <[email-masked]> writes:

> Signed-off-by: Gioele Barabucci <[email-masked]>
> ---
>  tc/tc_util.c | 18 +++++++++++++-----
>  1 file changed, 13 insertions(+), 5 deletions(-)
>
> diff --git a/tc/tc_util.c b/tc/tc_util.c
> index ed9efa70..e6235291 100644
> --- a/tc/tc_util.c
> +++ b/tc/tc_util.c
> @@ -28,7 +28,8 @@
>  
>  static struct db_names *cls_names;
>  
> -#define NAMES_DB "/etc/iproute2/tc_cls"
> +#define NAMES_DB_USR "/usr/lib/iproute2/tc_cls"
> +#define NAMES_DB_ETC "/etc/iproute2/tc_cls"

Is there a reason that these don't use CONF_USR_DIR and CONF_ETC_DIR?
I thought maybe the caller uses those and this is just a hardcoded
fallback, but that's not the case.

>  
>  int cls_names_init(char *path)
>  {
> @@ -38,11 +39,18 @@ int cls_names_init(char *path)
>  	if (!cls_names)
>  		return -1;
>  
> -	ret = db_names_load(cls_names, path ?: NAMES_DB);
> -	if (ret == -ENOENT && path) {
> -		fprintf(stderr, "Can't open class names file: %s\n", path);
> -		return -1;
> +	if (path) {
> +		ret = db_names_load(cls_names, path);
> +		if (ret == -ENOENT) {
> +			fprintf(stderr, "Can't open class names file: %s\n", path);
> +			return -1;

The caller always calls cls_uninit(), even for failures, so this
technically does not leak. Not great, but let's keep it as is,
fixing this is clearly out of scope.

> +		}
>  	}
> +
> +	ret = db_names_load(cls_names, NAMES_DB_ETC);
> +	if (ret == -ENOENT)
> +		ret = db_names_load(cls_names, NAMES_DB_USR);
> +
>  	if (ret) {
>  		db_names_free(cls_names);
>  		cls_names = NULL;

Otherwise looking good. In fact IMHO clearer than the old code.