Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf config: Add perf_home_perfconfig function

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>

authored by

Jiri Olsa and committed by
Arnaldo Carvalho de Melo
f5f03e19 bcbd79d1

+54 -36
+53 -36
tools/perf/util/config.c
··· 531 531 return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0); 532 532 } 533 533 534 + static char *home_perfconfig(void) 535 + { 536 + const char *home = NULL; 537 + char *config; 538 + struct stat st; 539 + 540 + home = getenv("HOME"); 541 + 542 + /* 543 + * Skip reading user config if: 544 + * - there is no place to read it from (HOME) 545 + * - we are asked not to (PERF_CONFIG_NOGLOBAL=1) 546 + */ 547 + if (!home || !*home || !perf_config_global()) 548 + return NULL; 549 + 550 + config = strdup(mkpath("%s/.perfconfig", home)); 551 + if (config == NULL) { 552 + pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.", home); 553 + return NULL; 554 + } 555 + 556 + if (stat(config, &st) < 0) 557 + goto out_free; 558 + 559 + if (st.st_uid && (st.st_uid != geteuid())) { 560 + pr_warning("File %s not owned by current user or root, ignoring it.", config); 561 + goto out_free; 562 + } 563 + 564 + if (st.st_size) 565 + return config; 566 + 567 + out_free: 568 + free(config); 569 + return NULL; 570 + } 571 + 572 + const char *perf_home_perfconfig(void) 573 + { 574 + static const char *config; 575 + static bool failed; 576 + 577 + config = failed ? NULL : home_perfconfig(); 578 + if (!config) 579 + failed = true; 580 + 581 + return config; 582 + } 583 + 534 584 static struct perf_config_section *find_section(struct list_head *sections, 535 585 const char *section_name) 536 586 { ··· 726 676 static int perf_config_set__init(struct perf_config_set *set) 727 677 { 728 678 int ret = -1; 729 - const char *home = NULL; 730 - char *user_config; 731 - struct stat st; 732 679 733 680 /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */ 734 681 if (config_exclusive_filename) ··· 734 687 if (perf_config_from_file(collect_config, perf_etc_perfconfig(), set) < 0) 735 688 goto out; 736 689 } 737 - 738 - home = getenv("HOME"); 739 - 740 - /* 741 - * Skip reading user config if: 742 - * - there is no place to read it from (HOME) 743 - * - we are asked not to (PERF_CONFIG_NOGLOBAL=1) 744 - */ 745 - if (!home || !*home || !perf_config_global()) 746 - return 0; 747 - 748 - user_config = strdup(mkpath("%s/.perfconfig", home)); 749 - if (user_config == NULL) { 750 - pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.", home); 751 - goto out; 690 + if (perf_config_global() && perf_home_perfconfig()) { 691 + if (perf_config_from_file(collect_config, perf_home_perfconfig(), set) < 0) 692 + goto out; 752 693 } 753 694 754 - if (stat(user_config, &st) < 0) { 755 - if (errno == ENOENT) 756 - ret = 0; 757 - goto out_free; 758 - } 759 - 760 - ret = 0; 761 - 762 - if (st.st_uid && (st.st_uid != geteuid())) { 763 - pr_warning("File %s not owned by current user or root, ignoring it.", user_config); 764 - goto out_free; 765 - } 766 - 767 - if (st.st_size) 768 - ret = perf_config_from_file(collect_config, user_config, set); 769 - 770 - out_free: 771 - free(user_config); 772 695 out: 773 696 return ret; 774 697 }
+1
tools/perf/util/config.h
··· 37 37 int perf_config_bool(const char *, const char *); 38 38 int config_error_nonbool(const char *); 39 39 const char *perf_etc_perfconfig(void); 40 + const char *perf_home_perfconfig(void); 40 41 41 42 struct perf_config_set *perf_config_set__new(void); 42 43 struct perf_config_set *perf_config_set__load_file(const char *file);