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