Skip to contentSkip to conversation

lib: add helper funcs to save and restore /proc|sys content

RFC PATCH New Jan Stancek 2 participants 3 messages Oct 18, 2018 – Oct 19, 2018 Library code

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 lib: add helper funcs to save and restore /proc|sys content

To avoid adding specially crafted functions for every feature where we need to save/restore some proc/sys config, this patch introduces a struct (linked list) where user pushes files names whose values should be saved. These can be later restored in cleanup or during the test.

Example:
TST_SYS_CONF_INIT(saved_conf); tst_sys_conf_save(&saved_conf, "/proc/sys/kernel/core_pattern"); tst_sys_conf_save(&saved_conf, "/proc/sys/kernel/numa_balancing");

  SAFE_FILE_PRINTF("/proc/sys/kernel/core_pattern", "changed");
  SAFE_FILE_PRINTF("/proc/sys/kernel/numa_balancing", "0");

  tst_sys_conf_restore(&saved_conf, 1);

At the moment this saves/restores one line of a specified file. Saving is non-strict, it only prints info message if file can't be found. Restoring is strict and will TBROK.

---
 include/tst_sys_conf.h      | 28 ++++++++++++++
 lib/newlib_tests/.gitignore |  1 +
 lib/newlib_tests/test19.c   | 60 +++++++++++++++++++++++++++++
 lib/tst_sys_conf.c          | 92 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 181 insertions(+)
 create mode 100644 include/tst_sys_conf.h
 create mode 100644 lib/newlib_tests/test19.c
 create mode 100644 lib/tst_sys_conf.c
include/tst_sys_conf.h new file +28 -0
new file mode 100644
index 000000000000..6c4545822a3e
@@ -0,0 +1,28 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Copyright (c) 2018 Jan Stancek <[email protected]>
+ */
+
+#ifndef TST_SYS_CONF_H__
+#define TST_SYS_CONF_H__
+
+struct tst_sys_conf {
+	char path[PATH_MAX];
+	char value[PATH_MAX];
+	struct tst_sys_conf *prev;
+	struct tst_sys_conf *next;
+};
+
+#define HEAD_INIT(name) { .prev = &(name), .next = &(name) }
+
+#define TST_SYS_CONF_INIT(name) \
+        struct tst_sys_conf name = HEAD_INIT(name)
+
+int tst_sys_conf_save_str(struct tst_sys_conf *c, const char *path, const char *value);
+int tst_sys_conf_save(struct tst_sys_conf *c, const char *path);
+void tst_sys_conf_restore(struct tst_sys_conf *c, int verbose);
+
+void tst_sys_conf_dump(struct tst_sys_conf *c);
+
+#endif
lib/newlib_tests/.gitignore +1 -0
index 76e89e438f55..c702644f0d1c 100644
@@ -20,6 +20,7 @@ tst_res_hexd
 tst_strstatus
 test17
 test18
+test19
 tst_expiration_timer
 test_exec
 test_exec_child
lib/newlib_tests/test19.c new file +60 -0

60 lines not on this page · raw message

lib/tst_sys_conf.c new file +92 -0

92 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#1 lore raw

RFC PATCH 1/1 lib: add helper funcs to save and restore /proc|sys content

Hi!

Quoted 5 lines

To avoid adding specially crafted functions for every feature where we need to save/restore some proc/sys config, this patch introduces a struct (linked list) where user pushes files names whose values should be saved. These can be later restored in cleanup or during the test.

This is a great idea, but I guess that the user API could be even more simplified.

I would go for an NULL terminated array of strings that could be passed in the tst_test structure. Also that way the values would be restored in the test library even after the test has crashed.

Something as:

static const char *save_restore[] = {
"/proc/sys/kernel/core_pattern",
"/proc/sys/kernel/numa_balancing",
NULL,
};

static struct tst_test test = {
	...

	.save_restore = save_restore,
};

What do you think?

Signature · 2 lines
Cyril Hrubis
[email protected]
#2 lore raw

RFC PATCH 1/1 lib: add helper funcs to save and restore /proc|sys content

----- Original Message -----

Quoted 23 lines

Hi!

Quoted 5 lines

To avoid adding specially crafted functions for every feature where we need to save/restore some proc/sys config, this patch introduces a struct (linked list) where user pushes files names whose values should be saved. These can be later restored in cleanup or during the test.

This is a great idea, but I guess that the user API could be even more simplified.

I would go for an NULL terminated array of strings that could be passed in the tst_test structure. Also that way the values would be restored in the test library even after the test has crashed.

Something as:

static const char *save_restore[] = {
"/proc/sys/kernel/core_pattern",
"/proc/sys/kernel/numa_balancing",
NULL,
};

Quoted 7 lines
static struct tst_test test = {
	...

	.save_restore = save_restore,
};

What do you think?

Looks good to me - I agree this makes it easier for user.

I was already thinking about corner cases with function approach, but that would be easy to add if we find need for it later.

I'll post v2 that uses tst_test struct.

Regards,
Jan

Quoted 5 lines

--
Cyril Hrubis
[email protected]