[LTP] [PATCH v3] save_restore: Introduce new struct field for flags

Cyril Hrubis chrubis@suse.cz
Mon Nov 14 14:25:33 CET 2022


Hi!
> diff --git a/doc/c-test-api.txt b/doc/c-test-api.txt
> index 8844d9f2f..fa7e2d3b0 100644
> --- a/doc/c-test-api.txt
> +++ b/doc/c-test-api.txt
> @@ -1605,35 +1605,33 @@ LTP library can be instructed to save and restore value of specified
>  field 'save_restore'. It is a NULL-terminated array of struct
>  'tst_path_val' where each tst_path_val.path represents a file, whose
>  value is saved at the beginning and restored at the end of the test.
> -If non-NULL value is passed it is written to the respective file at
> -the beginning of the test. Only the first line of a specified file
> -is saved and restored.
> -
> -Pathnames can be optionally prefixed to specify how strictly (during
> -'store') are handled errors:
> -
> -* (no prefix) - test ends with 'TCONF', if file doesn't exist
> -* '?'         - test prints info message and continues,
> -                if file doesn't exist or open/read fails
> -* '!'         - test ends with 'TBROK', if file doesn't exist
> +If non-NULL string is passed in tst_path_val.val, it is written
> +to the respective file at the beginning of the test. Only the first line
> +of a specified file is saved and restored.
> +
> +By default, the test will end with TCONF if the file is read-only or
> +does not exist. If the optional write of new value fails, the test will end
> +with 'TBROK'. This behavior can be changed using tst_path_val.flags:

I'm not sure if it's a good idea to have default without any constants
passed. When I look at the file_restore records in the tests it all
makes sense until I reach a part where 0 is passed to the flags. I think
that it would make sense to make everything as self describing as
possible.

Would you consider adding TST_SR_TCONF_MISSING and TST_SR_TCONF_RO?

> +* 'TST_SR_FAIL_MISSING' – End test with 'TBROK' if the file does not exist

This FAIL part in this name is quite misleading since the test ends with
TBROK. I would say that it would be much more clear if it was named
TST_SR_TBROK_MISSING.

> +* 'TST_SR_SKIP_MISSING' – Continue without saving the file if it does not exist
> +* 'TST_SR_FAIL_RO' – End test with 'TBROK' if the file is read-only
> +* 'TST_SR_SKIP_RO' – Continue without saving the file if it is read-only
> +* 'TST_SR_IGNORE_ERR' – Ignore errors when writing new value into the file
> +* 'TST_SR_REQUIRED' – Equivalent to 'TST_SR_FAIL_MISSING | TST_SR_FAIL_RO'
> +* 'TST_SR_COND_ACCESS' – Equivalent to 'TST_SR_SKIP_MISSING | TST_SR_SKIP_RO'
>  
>  'restore' is always strict and will TWARN if it encounters any error.
>  
>  [source,c]
>  -------------------------------------------------------------------------------
> -static void setup(void)
> -{
> -	FILE_PRINTF("/proc/sys/kernel/core_pattern", "/mypath");
> -	SAFE_TRY_FILE_PRINTF("/proc/sys/user/max_user_namespaces", "%d", 10);
> -}
> -
>  static struct tst_test test = {
>  	...
>  	.setup = setup,
>  	.save_restore = (const struct tst_path_val[]) {
> -		{"/proc/sys/kernel/core_pattern", NULL},
> -		{"?/proc/sys/user/max_user_namespaces", NULL},
> -		{"!/sys/kernel/mm/ksm/run", "1"},
> +		{"/proc/sys/kernel/core_pattern", NULL, 0},
> +		{"/proc/sys/user/max_user_namespaces", NULL, TST_SR_COND_ACCESS},
> +		{"/sys/kernel/mm/ksm/run", "1", TST_SR_REQUIRED},
>  		{}
>  	},
>  };
> diff --git a/include/tst_sys_conf.h b/include/tst_sys_conf.h
> index b7bbe36fc..b52b8913e 100644
> --- a/include/tst_sys_conf.h
> +++ b/include/tst_sys_conf.h
> @@ -5,14 +5,23 @@
>  #ifndef TST_SYS_CONF_H__
>  #define TST_SYS_CONF_H__
>  
> +#define TST_SR_FAIL_MISSING 0x1
> +#define TST_SR_SKIP_MISSING 0x2
> +#define TST_SR_FAIL_RO 0x4
> +#define TST_SR_SKIP_RO 0x8
> +#define TST_SR_IGNORE_ERR 0x10
> +
> +#define TST_SR_REQUIRED (TST_SR_FAIL_MISSING | TST_SR_FAIL_RO)
> +#define TST_SR_COND_ACCESS (TST_SR_SKIP_MISSING | TST_SR_SKIP_RO)
> +
>  struct tst_path_val {
>          const char *path;
>          const char *val;
> +	unsigned int flags;
>  };
>  
> -int tst_sys_conf_save_str(const char *path, const char *value);
> -int tst_sys_conf_save(const char *path);
> -void tst_sys_conf_set(const char *path, const char *value);
> +void tst_sys_conf_save_str(const char *path, const char *value);
> +int tst_sys_conf_save(const struct tst_path_val *conf);
>  void tst_sys_conf_restore(int verbose);
>  void tst_sys_conf_dump(void);
>  
> diff --git a/lib/tst_sys_conf.c b/lib/tst_sys_conf.c
> index 003698825..7aa0f46bd 100644
> --- a/lib/tst_sys_conf.c
> +++ b/lib/tst_sys_conf.c
> @@ -20,6 +20,14 @@ struct tst_sys_conf {
>  
>  static struct tst_sys_conf *save_restore_data;
>  
> +static void print_error(int info_only, const char *err, const char *path)
> +{
> +	if (info_only)
> +		tst_res(TINFO, err, path);
> +	else
> +		tst_brk(TBROK | TERRNO, err, path);
> +}

Can we please either make this a macro or pass a __LINE__ so that the
line number in the error message is correct?

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list