lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1443944110-6414-3-git-send-email-treeze.taeung@gmail.com>
Date:	Sun,  4 Oct 2015 16:35:05 +0900
From:	Taeung Song <treeze.taeung@...il.com>
To:	Arnaldo Carvalho de Melo <acme@...nel.org>
Cc:	linux-kernel@...r.kernel.org, jolsa@...hat.com,
	namhyung@...nel.org, Ingo Molnar <mingo@...hat.com>,
	Taeung Song <treeze.taeung@...il.com>
Subject: [PATCH v7 2/7] perf config: Add '--system' and '--user' options to select which config file is used

Which config file is used is decided in only perf_config().
And a perf-config command depend on perf_config() to list
config variables with values. So add '--system' and '--user'
options to select which config file to be used without perf_config().

The file-options '--system' means $(sysconfdir)/perfconfig and
'--user' means $HOME/.perfconfig. If file-option isn't used,
both system and user config file is read
but user config have priority. The syntax examples are like below

    perf config [<file-option>] [options]

    a specific config file.
    # perf config --user | --system
    or
    both user and system config file.
    # perf config

In addition, use List data structure which has config info.
Because perf_config() isn't used any more and directly collect
config info by perf_config_from_file() with a specific config
file path. Whichever config file or both are used, list is required
to keep and handle config variables and values.

Signed-off-by: Taeung Song <treeze.taeung@...il.com>
---
 tools/perf/Documentation/perf-config.txt |  14 ++-
 tools/perf/builtin-config.c              | 163 +++++++++++++++++++++++++++++--
 tools/perf/util/cache.h                  |  15 +++
 tools/perf/util/config.c                 |   4 +-
 4 files changed, 187 insertions(+), 9 deletions(-)

diff --git a/tools/perf/Documentation/perf-config.txt b/tools/perf/Documentation/perf-config.txt
index e8013b3..a42d409 100644
--- a/tools/perf/Documentation/perf-config.txt
+++ b/tools/perf/Documentation/perf-config.txt
@@ -8,7 +8,7 @@ perf-config - Get and set variables in a configuration file.
 SYNOPSIS
 --------
 [verse]
-'perf config' -l | --list
+'perf config' [<file-option>] -l | --list
 
 DESCRIPTION
 -----------
@@ -21,6 +21,14 @@ OPTIONS
 --list::
 	Show current config variables, name and value, for all sections.
 
+--user::
+	For writing and reading options: write to user
+	'$HOME/.perfconfig' file or read it.
+
+--system::
+	For writing and reading options: write to system-wide
+	'$(sysconfdir)/perfconfig' or read it.
+
 CONFIGURATION FILE
 ------------------
 
@@ -33,6 +41,10 @@ store a system-wide default configuration.
 The variables are divided into sections. In each section, the variables
 that are composed of a name and value.
 
+When reading or writing, the values are read from the system and user
+configuration files by default, and options '--system' and '--user'
+can be used to tell the command to read from or write to only that location.
+
 Syntax
 ~~~~~~
 
diff --git a/tools/perf/builtin-config.c b/tools/perf/builtin-config.c
index 30b1500..92be3eb 100644
--- a/tools/perf/builtin-config.c
+++ b/tools/perf/builtin-config.c
@@ -13,8 +13,10 @@
 #include "util/util.h"
 #include "util/debug.h"
 
+static bool use_system_config, use_user_config;
+
 static const char * const config_usage[] = {
-	"perf config [options]",
+	"perf config [<file-option>] [options]",
 	NULL
 };
 
@@ -23,19 +25,150 @@ enum actions {
 } actions;
 
 static struct option config_options[] = {
+	OPT_GROUP("Config file location"),
+	OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
+	OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"),
 	OPT_GROUP("Action"),
 	OPT_SET_UINT('l', "list", &actions,
 		     "show current config variables", ACTION_LIST),
 	OPT_END()
 };
+static int show_config(struct list_head *sections)
+{
+	struct config_section *section;
+	struct config_element *element;
+
+	list_for_each_entry(section, sections, list) {
+		list_for_each_entry(element, &section->element_head, list) {
+			printf("%s.%s=%s\n", section->name,
+				       element->name, element->value);
+		}
+	}
+
+	return 0;
+}
 
-static int show_config(const char *key, const char *value,
-		       void *cb __maybe_unused)
+static struct config_section *find_section(struct list_head *sections,
+					   const char *section_name)
 {
+	struct config_section *section;
+
+	list_for_each_entry(section, sections, list)
+		if (!strcmp(section->name, section_name))
+			return section;
+
+	return NULL;
+}
+
+static struct config_element *find_element(const char *name,
+					   struct config_section *section)
+{
+	struct config_element *element;
+
+	list_for_each_entry(element, &section->element_head, list)
+		if (!strcmp(element->name, name))
+			return element;
+
+	return NULL;
+}
+
+static void find_config(struct list_head *sections,
+			struct config_section **section,
+			struct config_element **element,
+			const char *section_name, const char *name)
+{
+	*section = find_section(sections, section_name);
+
+	if (*section != NULL)
+		*element = find_element(name, *section);
+	else
+		*element = NULL;
+}
+
+static struct config_section *init_section(const char *section_name)
+{
+	struct config_section *section;
+
+	section = zalloc(sizeof(*section));
+	if (!section)
+		return NULL;
+
+	INIT_LIST_HEAD(&section->element_head);
+	section->name = strdup(section_name);
+	if (!section->name) {
+		pr_err("%s: strdup failed\n", __func__);
+		free(section);
+		return NULL;
+	}
+
+	return section;
+}
+
+static int add_element(struct list_head *head,
+			      const char *name, const char *value)
+{
+	struct config_element *element;
+
+	element = zalloc(sizeof(*element));
+	if (!element)
+		return -1;
+
+	element->name = strdup(name);
+	if (!element->name) {
+		pr_err("%s: strdup failed\n", __func__);
+		free(element);
+		return -1;
+	}
 	if (value)
-		printf("%s=%s\n", key, value);
+		element->value = (char *)value;
 	else
-		printf("%s\n", key);
+		element->value = NULL;
+
+	list_add_tail(&element->list, head);
+	return 0;
+}
+
+static int collect_current_config(const char *var, const char *value,
+				  void *spec_sections __maybe_unused)
+{
+	struct config_section *section = NULL;
+	struct config_element *element = NULL;
+	struct list_head *sections = (struct list_head *)spec_sections;
+	char *key = strdup(var);
+	char *section_name, *name;
+
+	if (!key) {
+		pr_err("%s: strdup failed\n", __func__);
+		return -1;
+	}
+
+	section_name = strsep(&key, ".");
+	name = strsep(&key, ".");
+	free(key);
+	if (name == NULL)
+		return -1;
+
+	find_config(sections, &section, &element, section_name, name);
+
+	if (!section) {
+		section = init_section(section_name);
+		if (!section)
+			return -1;
+		list_add_tail(&section->list, sections);
+	}
+
+	value = strdup(value);
+	if (!value) {
+		pr_err("%s: strdup failed\n", __func__);
+		return -1;
+	}
+
+	if (!element)
+		return add_element(&section->element_head, name, value);
+	else {
+		free(element->value);
+		element->value = (char *)value;
+	}
 
 	return 0;
 }
@@ -43,10 +176,28 @@ static int show_config(const char *key, const char *value,
 int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
 {
 	int ret = 0;
+	struct list_head sections;
+	const char *system_config = perf_etc_perfconfig();
+	char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
 
 	argc = parse_options(argc, argv, config_options, config_usage,
 			     PARSE_OPT_STOP_AT_NON_OPTION);
 
+	if (use_system_config && use_user_config) {
+		pr_err("Error: only one config file at a time\n");
+		parse_options_usage(config_usage, config_options, "user", 0);
+		parse_options_usage(NULL, config_options, "system", 0);
+		return -1;
+	}
+
+	INIT_LIST_HEAD(&sections);
+
+	if (use_system_config || (!use_system_config && !use_user_config))
+		perf_config_from_file(collect_current_config, system_config, &sections);
+
+	if (use_user_config || (!use_system_config && !use_user_config))
+		perf_config_from_file(collect_current_config, user_config, &sections);
+
 	switch (actions) {
 	case ACTION_LIST:
 	default:
@@ -55,7 +206,7 @@ int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
 			parse_options_usage(config_usage, config_options, "l", 1);
 			return -1;
 		} else
-			ret = perf_config(show_config, NULL);
+			ret = show_config(&sections);
 	}
 
 	return ret;
diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h
index c861373..712e82b 100644
--- a/tools/perf/util/cache.h
+++ b/tools/perf/util/cache.h
@@ -1,6 +1,7 @@
 #ifndef __PERF_CACHE_H
 #define __PERF_CACHE_H
 
+#include <linux/list.h>
 #include <stdbool.h>
 #include "util.h"
 #include "strbuf.h"
@@ -19,14 +20,28 @@
 #define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR"
 #define PERF_TRACEFS_ENVIRONMENT "PERF_TRACEFS_DIR"
 
+struct config_element {
+	char *name;
+	char *value;
+	struct list_head list;
+};
+
+struct config_section {
+	char *name;
+	struct list_head element_head;
+	struct list_head list;
+};
+
 typedef int (*config_fn_t)(const char *, const char *, void *);
 extern int perf_default_config(const char *, const char *, void *);
 extern int perf_config(config_fn_t fn, void *);
+extern int perf_config_from_file(config_fn_t fn, const char *filename, void *data);
 extern int perf_config_int(const char *, const char *);
 extern u64 perf_config_u64(const char *, const char *);
 extern int perf_config_bool(const char *, const char *);
 extern int config_error_nonbool(const char *);
 extern const char *perf_config_dirname(const char *, const char *);
+extern const char *perf_etc_perfconfig(void);
 
 /* pager.c */
 extern void setup_pager(void);
diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
index 2e452ac..23fa8c5 100644
--- a/tools/perf/util/config.c
+++ b/tools/perf/util/config.c
@@ -416,7 +416,7 @@ int perf_default_config(const char *var, const char *value,
 	return 0;
 }
 
-static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
+int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
 {
 	int ret;
 	FILE *f = fopen(filename, "r");
@@ -434,7 +434,7 @@ static int perf_config_from_file(config_fn_t fn, const char *filename, void *dat
 	return ret;
 }
 
-static const char *perf_etc_perfconfig(void)
+const char *perf_etc_perfconfig(void)
 {
 	static const char *system_wide;
 	if (!system_wide)
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ