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