OSDN Git Service

perf config: Add perf_home_perfconfig function
authorJiri Olsa <jolsa@kernel.org>
Sat, 2 Jan 2021 22:04:23 +0000 (23:04 +0100)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 20 Jan 2021 17:34:20 +0000 (14:34 -0300)
Factor out the perf_home_perfconfig, that looks for .perfconfig in home
directory including check for PERF_CONFIG_NOGLOBAL and for proper
permission.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexei Budankov <abudankov@huawei.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Michael Petlan <mpetlan@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/20210102220441.794923-5-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/config.c
tools/perf/util/config.h

index 222cb2e..34fe80c 100644 (file)
@@ -531,6 +531,56 @@ static int perf_config_global(void)
        return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
 }
 
+static char *home_perfconfig(void)
+{
+       const char *home = NULL;
+       char *config;
+       struct stat st;
+
+       home = getenv("HOME");
+
+       /*
+        * Skip reading user config if:
+        *   - there is no place to read it from (HOME)
+        *   - we are asked not to (PERF_CONFIG_NOGLOBAL=1)
+        */
+       if (!home || !*home || !perf_config_global())
+               return NULL;
+
+       config = strdup(mkpath("%s/.perfconfig", home));
+       if (config == NULL) {
+               pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.", home);
+               return NULL;
+       }
+
+       if (stat(config, &st) < 0)
+               goto out_free;
+
+       if (st.st_uid && (st.st_uid != geteuid())) {
+               pr_warning("File %s not owned by current user or root, ignoring it.", config);
+               goto out_free;
+       }
+
+       if (st.st_size)
+               return config;
+
+out_free:
+       free(config);
+       return NULL;
+}
+
+const char *perf_home_perfconfig(void)
+{
+       static const char *config;
+       static bool failed;
+
+       config = failed ? NULL : home_perfconfig();
+       if (!config)
+               failed = true;
+
+       return config;
+}
+
 static struct perf_config_section *find_section(struct list_head *sections,
                                                const char *section_name)
 {
@@ -676,9 +726,6 @@ int perf_config_set__collect(struct perf_config_set *set, const char *file_name,
 static int perf_config_set__init(struct perf_config_set *set)
 {
        int ret = -1;
-       const char *home = NULL;
-       char *user_config;
-       struct stat st;
 
        /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
        if (config_exclusive_filename)
@@ -687,41 +734,11 @@ static int perf_config_set__init(struct perf_config_set *set)
                if (perf_config_from_file(collect_config, perf_etc_perfconfig(), set) < 0)
                        goto out;
        }
-
-       home = getenv("HOME");
-
-       /*
-        * Skip reading user config if:
-        *   - there is no place to read it from (HOME)
-        *   - we are asked not to (PERF_CONFIG_NOGLOBAL=1)
-        */
-       if (!home || !*home || !perf_config_global())
-               return 0;
-
-       user_config = strdup(mkpath("%s/.perfconfig", home));
-       if (user_config == NULL) {
-               pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.", home);
-               goto out;
-       }
-
-       if (stat(user_config, &st) < 0) {
-               if (errno == ENOENT)
-                       ret = 0;
-               goto out_free;
-       }
-
-       ret = 0;
-
-       if (st.st_uid && (st.st_uid != geteuid())) {
-               pr_warning("File %s not owned by current user or root, ignoring it.", user_config);
-               goto out_free;
+       if (perf_config_global() && perf_home_perfconfig()) {
+               if (perf_config_from_file(collect_config, perf_home_perfconfig(), set) < 0)
+                       goto out;
        }
 
-       if (st.st_size)
-               ret = perf_config_from_file(collect_config, user_config, set);
-
-out_free:
-       free(user_config);
 out:
        return ret;
 }
index ee5a242..d6c4f80 100644 (file)
@@ -37,6 +37,7 @@ int perf_config_u64(u64 *dest, const char *, const char *);
 int perf_config_bool(const char *, const char *);
 int config_error_nonbool(const char *);
 const char *perf_etc_perfconfig(void);
+const char *perf_home_perfconfig(void);
 
 struct perf_config_set *perf_config_set__new(void);
 struct perf_config_set *perf_config_set__load_file(const char *file);