[LTP] [PATCH 2/4] lib: enhance .save_restore to support set expected value
Li Wang
liwang@redhat.com
Tue Mar 8 08:37:07 CET 2022
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
(/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.
+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);
+
+ 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);
+ pvl++;
}
}
--
2.31.1
More information about the ltp
mailing list