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