builtin-config.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * builtin-config.c
  4. *
  5. * Copyright (C) 2015, Taeung Song <treeze.taeung@gmail.com>
  6. *
  7. */
  8. #include "builtin.h"
  9. #include "util/cache.h"
  10. #include <subcmd/parse-options.h>
  11. #include "util/debug.h"
  12. #include "util/config.h"
  13. #include <linux/string.h>
  14. #include <limits.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. static bool use_system_config, use_user_config;
  18. static const char * const config_usage[] = {
  19. "perf config [<file-option>] [options] [section.name[=value] ...]",
  20. NULL
  21. };
  22. enum actions {
  23. ACTION_LIST = 1
  24. } actions;
  25. static struct option config_options[] = {
  26. OPT_SET_UINT('l', "list", &actions,
  27. "show current config variables", ACTION_LIST),
  28. OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
  29. OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"),
  30. OPT_END()
  31. };
  32. static int set_config(struct perf_config_set *set, const char *file_name)
  33. {
  34. struct perf_config_section *section = NULL;
  35. struct perf_config_item *item = NULL;
  36. const char *first_line = "# this file is auto-generated.";
  37. FILE *fp;
  38. if (set == NULL)
  39. return -1;
  40. fp = fopen(file_name, "w");
  41. if (!fp)
  42. return -1;
  43. fprintf(fp, "%s\n", first_line);
  44. /* overwrite configvariables */
  45. perf_config_items__for_each_entry(&set->sections, section) {
  46. if (!use_system_config && section->from_system_config)
  47. continue;
  48. fprintf(fp, "[%s]\n", section->name);
  49. perf_config_items__for_each_entry(&section->items, item) {
  50. if (!use_system_config && item->from_system_config)
  51. continue;
  52. if (item->value)
  53. fprintf(fp, "\t%s = %s\n",
  54. item->name, item->value);
  55. }
  56. }
  57. fclose(fp);
  58. return 0;
  59. }
  60. static int show_spec_config(struct perf_config_set *set, const char *var)
  61. {
  62. struct perf_config_section *section;
  63. struct perf_config_item *item;
  64. if (set == NULL)
  65. return -1;
  66. perf_config_items__for_each_entry(&set->sections, section) {
  67. if (!strstarts(var, section->name))
  68. continue;
  69. perf_config_items__for_each_entry(&section->items, item) {
  70. const char *name = var + strlen(section->name) + 1;
  71. if (strcmp(name, item->name) == 0) {
  72. char *value = item->value;
  73. if (value) {
  74. printf("%s=%s\n", var, value);
  75. return 0;
  76. }
  77. }
  78. }
  79. }
  80. return 0;
  81. }
  82. static int show_config(struct perf_config_set *set)
  83. {
  84. struct perf_config_section *section;
  85. struct perf_config_item *item;
  86. if (set == NULL)
  87. return -1;
  88. perf_config_set__for_each_entry(set, section, item) {
  89. char *value = item->value;
  90. if (value)
  91. printf("%s.%s=%s\n", section->name,
  92. item->name, value);
  93. }
  94. return 0;
  95. }
  96. static int parse_config_arg(char *arg, char **var, char **value)
  97. {
  98. const char *last_dot = strchr(arg, '.');
  99. /*
  100. * Since "var" actually contains the section name and the real
  101. * config variable name separated by a dot, we have to know where the dot is.
  102. */
  103. if (last_dot == NULL || last_dot == arg) {
  104. pr_err("The config variable does not contain a section name: %s\n", arg);
  105. return -1;
  106. }
  107. if (!last_dot[1]) {
  108. pr_err("The config variable does not contain a variable name: %s\n", arg);
  109. return -1;
  110. }
  111. *value = strchr(arg, '=');
  112. if (*value == NULL)
  113. *var = arg;
  114. else if (!strcmp(*value, "=")) {
  115. pr_err("The config variable does not contain a value: %s\n", arg);
  116. return -1;
  117. } else {
  118. *value = *value + 1; /* excluding a first character '=' */
  119. *var = strsep(&arg, "=");
  120. if (*var[0] == '\0') {
  121. pr_err("invalid config variable: %s\n", arg);
  122. return -1;
  123. }
  124. }
  125. return 0;
  126. }
  127. int cmd_config(int argc, const char **argv)
  128. {
  129. int i, ret = -1;
  130. struct perf_config_set *set;
  131. char path[PATH_MAX];
  132. char *user_config = mkpath(path, sizeof(path), "%s/.perfconfig", getenv("HOME"));
  133. const char *config_filename;
  134. bool changed = false;
  135. argc = parse_options(argc, argv, config_options, config_usage,
  136. PARSE_OPT_STOP_AT_NON_OPTION);
  137. if (use_system_config && use_user_config) {
  138. pr_err("Error: only one config file at a time\n");
  139. parse_options_usage(config_usage, config_options, "user", 0);
  140. parse_options_usage(NULL, config_options, "system", 0);
  141. return -1;
  142. }
  143. if (use_system_config)
  144. config_exclusive_filename = perf_etc_perfconfig();
  145. else if (use_user_config)
  146. config_exclusive_filename = user_config;
  147. if (!config_exclusive_filename)
  148. config_filename = user_config;
  149. else
  150. config_filename = config_exclusive_filename;
  151. /*
  152. * At only 'config' sub-command, individually use the config set
  153. * because of reinitializing with options config file location.
  154. */
  155. set = perf_config_set__new();
  156. if (!set)
  157. goto out_err;
  158. switch (actions) {
  159. case ACTION_LIST:
  160. if (argc) {
  161. pr_err("Error: takes no arguments\n");
  162. parse_options_usage(config_usage, config_options, "l", 1);
  163. } else {
  164. do_action_list:
  165. if (show_config(set) < 0) {
  166. pr_err("Nothing configured, "
  167. "please check your %s \n", config_filename);
  168. goto out_err;
  169. }
  170. }
  171. break;
  172. default:
  173. if (!argc)
  174. goto do_action_list;
  175. for (i = 0; argv[i]; i++) {
  176. char *var, *value;
  177. char *arg = strdup(argv[i]);
  178. if (!arg) {
  179. pr_err("%s: strdup failed\n", __func__);
  180. goto out_err;
  181. }
  182. if (parse_config_arg(arg, &var, &value) < 0) {
  183. free(arg);
  184. goto out_err;
  185. }
  186. if (value == NULL) {
  187. if (show_spec_config(set, var) < 0) {
  188. pr_err("%s is not configured: %s\n",
  189. var, config_filename);
  190. free(arg);
  191. goto out_err;
  192. }
  193. } else {
  194. if (perf_config_set__collect(set, config_filename,
  195. var, value) < 0) {
  196. pr_err("Failed to add '%s=%s'\n",
  197. var, value);
  198. free(arg);
  199. goto out_err;
  200. }
  201. changed = true;
  202. }
  203. free(arg);
  204. }
  205. if (!changed)
  206. break;
  207. if (set_config(set, config_filename) < 0) {
  208. pr_err("Failed to set the configs on %s\n",
  209. config_filename);
  210. goto out_err;
  211. }
  212. }
  213. ret = 0;
  214. out_err:
  215. perf_config_set__delete(set);
  216. return ret;
  217. }