From: Cyril Hrubis <chrubis@suse.cz>
To: Li Wang <liwang@redhat.com>
Cc: Bruno Goncalves <bgoncalv@redhat.com>, ltp@lists.linux.it
Subject: Re: [LTP] [PATCH 2/4] lib: enhance .save_restore to support set expected value
Date: Tue, 8 Mar 2022 14:02:10 +0100 [thread overview]
Message-ID: <YidT0piJ3+e9FvhE@yuki> (raw)
In-Reply-To: <20220308073709.4125677-3-liwang@redhat.com>
Hi!
> This extends that .save_restore support set new expected value after
> saving the knob's original, which also avoids additionally checking
> before using the file at other places.
>
> And, export function tst_sys_conf_set() can be singly used for setting
> new value of knob in the whole LTP.
>
> Reported-by: Bruno Goncalves <bgoncalv@redhat.com>
> Suggested-by: Cyril Hrubis <chrubis@suse.cz>
> Signed-off-by: Li Wang <liwang@redhat.com>
> Cc: Jan Stancek <jstancek@redhat.com>
> ---
> doc/c-test-api.txt | 16 +++++++++-------
> include/tst_sys_conf.h | 6 ++++++
> include/tst_test.h | 2 +-
> lib/tst_sys_conf.c | 13 +++++++++++++
> lib/tst_test.c | 9 +++++----
> 5 files changed, 34 insertions(+), 12 deletions(-)
>
> diff --git a/doc/c-test-api.txt b/doc/c-test-api.txt
> index 28383ccee..3e167a1d3 100644
> --- a/doc/c-test-api.txt
> +++ b/doc/c-test-api.txt
> @@ -1559,12 +1559,14 @@ itself is not available on the system.
> 1.27 Saving & restoring /proc|sys values
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> -LTP library can be instructed to save and restore value of specified
> +LTP library can be instructed to save and restore the value of specified
^
This shouldn't be
here as we do not
talk about
specific proc/sys
files but about
any proc/sys file
> (/proc|sys) files. This is achieved by initialized tst_test struct
> -field 'save_restore'. It is a 'NULL' terminated array of strings where
> -each string represents a file, whose value is saved at the beginning
> -and restored at the end of the test. Only first line of a specified
> -file is saved and restored.
> +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 pass a value to tst_path_val.value that will be set to the knob
> +correspondingly, NULL means do nothing else but save the original value.
"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:
> @@ -1578,8 +1580,8 @@ Pathnames can be optionally prefixed to specify how strictly (during
>
> [source,c]
> -------------------------------------------------------------------------------
> -static const char *save_restore[] = {
> - "/proc/sys/kernel/core_pattern",
> +static const struct tst_path_val save_restore[] = {
> + {"/proc/sys/kernel/core_pattern", NULL},
> NULL,
> };
>
> diff --git a/include/tst_sys_conf.h b/include/tst_sys_conf.h
> index 507a552e8..b7bbe36fc 100644
> --- a/include/tst_sys_conf.h
> +++ b/include/tst_sys_conf.h
> @@ -5,8 +5,14 @@
> #ifndef TST_SYS_CONF_H__
> #define TST_SYS_CONF_H__
>
> +struct tst_path_val {
> + const char *path;
> + const char *val;
> +};
> +
> 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_restore(int verbose);
> void tst_sys_conf_dump(void);
>
> diff --git a/include/tst_test.h b/include/tst_test.h
> index 816fab4dd..e514efa76 100644
> --- a/include/tst_test.h
> +++ b/include/tst_test.h
> @@ -259,7 +259,7 @@ struct tst_test {
> * NULL terminated array of (/proc, /sys) files to save
> * before setup and restore after cleanup
> */
> - const char * const *save_restore;
> + const struct tst_path_val const *save_restore;
>
> /*
> * NULL terminated array of kernel config options required for the
> diff --git a/lib/tst_sys_conf.c b/lib/tst_sys_conf.c
> index d7118f15f..f9460d228 100644
> --- a/lib/tst_sys_conf.c
> +++ b/lib/tst_sys_conf.c
> @@ -96,6 +96,19 @@ int tst_sys_conf_save(const char *path)
> return tst_sys_conf_save_str(path, line);
> }
>
> +void tst_sys_conf_set(const char *path, const char *value)
> +{
> + char flag = path[0];
> + if (flag == '?' || flag == '!')
> + path++;
> +
> + if (access(path, F_OK) != 0)
> + tst_brk(TBROK | TERRNO, " The path %s is not exist", path);
^ ^
| does
useless space?
Also do we really have to check for the file existence here? The
SAFE_FILE_PRINTF() will TBROK if the fopen() fails anyways.
> + if (value)
> + SAFE_FILE_PRINTF(path, "%s", value);
> +}
> +
> void tst_sys_conf_restore(int verbose)
> {
> struct tst_sys_conf *i;
> diff --git a/lib/tst_test.c b/lib/tst_test.c
> index 9e745c537..fe2e2bb6c 100644
> --- a/lib/tst_test.c
> +++ b/lib/tst_test.c
> @@ -1105,11 +1105,12 @@ static void do_setup(int argc, char *argv[])
> tst_tmpdir();
>
> if (tst_test->save_restore) {
> - const char * const *name = tst_test->save_restore;
> + const struct tst_path_val const *pvl = tst_test->save_restore;
>
> - while (*name) {
> - tst_sys_conf_save(*name);
> - name++;
> + while (pvl->path) {
> + if (!tst_sys_conf_save(pvl->path))
> + tst_sys_conf_set(pvl->path, pvl->val);
Maybe it would be cleaner if we added tst_sys_conf_save_set() function
instead of tst_sys_conf_set() that would do both, saved the value and
set new one if non-NULL.
> + pvl++;
> }
> }
>
> --
> 2.31.1
>
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2022-03-08 13:00 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-08 7:37 [LTP] [PATCH 0/4] enhance .save_restore to support set value Li Wang
2022-03-08 7:37 ` [LTP] [PATCH 1/4] lib: move struct tst_sys_conf to internal Li Wang
2022-03-08 12:25 ` Cyril Hrubis
2022-03-08 7:37 ` [LTP] [PATCH 2/4] lib: enhance .save_restore to support set expected value Li Wang
2022-03-08 13:02 ` Cyril Hrubis [this message]
2022-03-09 2:27 ` Li Wang
2022-03-08 7:37 ` [LTP] [PATCH 3/4] testcase: switch to the new .save_restore Li Wang
2022-03-08 13:05 ` Cyril Hrubis
2022-03-08 7:37 ` [LTP] [PATCH 4/4] ksm: cleanup work and make use of .save_restore Li Wang
2022-03-08 13:20 ` Cyril Hrubis
2022-03-09 3:04 ` [LTP] [PATCH v2 1/3] lib: move struct tst_sys_conf to internal Li Wang
2022-03-09 3:04 ` [LTP] [PATCH v2 2/3] lib: enhance .save_restore to support set expected value Li Wang
2022-03-09 10:00 ` Cyril Hrubis
2022-03-10 1:26 ` Li Wang
2022-03-10 7:43 ` xuyang2018.jy
2022-03-10 7:53 ` Li Wang
2022-03-10 8:01 ` xuyang2018.jy
2022-03-09 3:04 ` [LTP] [PATCH v2 3/3] ksm: cleanup work and make use of .save_restore Li Wang
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=YidT0piJ3+e9FvhE@yuki \
--to=chrubis@suse.cz \
--cc=bgoncalv@redhat.com \
--cc=liwang@redhat.com \
--cc=ltp@lists.linux.it \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox