1 /* Copyright (c) 2008, 2009 Nicira Networks
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * In addition, as a special exception, Nicira Networks gives permission
17 * to link the code of its release of vswitchd with the OpenSSL project's
18 * "OpenSSL" library (or with modified versions of it that use the same
19 * license as the "OpenSSL" library), and distribute the linked
20 * executables. You must obey the GNU General Public License in all
21 * respects for all of the code used other than "OpenSSL". If you modify
22 * this file, you may extend this exception to your version of the file,
23 * but you are not obligated to do so. If you do not wish to do so,
24 * delete this exception statement from your version.
37 #include "command-line.h"
42 #define THIS_MODULE VLM_cfg_mod
45 /* Configuration when we first read the configuration file. */
46 static struct svec orig_cfg = SVEC_EMPTY_INITIALIZER;
49 usage(char *prog_name, int exit_code)
51 printf("Usage: %s --config-file=FILE ACTIONS\n"
53 " -T, --timeout=MS wait at most MS milliseconds for lock\n"
54 " -F, --config-file=FILE use configuration FILE\n"
56 " -a, --add=ENTRY add ENTRY\n"
57 " -d, --del-entry=ENTRY delete ENTRY\n"
58 " -D, --del-section=KEY delete section matching KEY\n"
59 " --del-match=PATTERN delete entries matching shell PATTERN\n"
60 " -q, --query=KEY return all entries matching KEY\n"
61 " -c, --log-changes log changes up to this point\n"
63 " -h, --help display this help message\n"
64 " -V, --version display version information\n",
70 open_config(char *config_file, int timeout)
74 error = cfg_set_file(config_file);
76 ovs_fatal(error, "failed to add configuration file \"%s\"",
80 error = cfg_lock(NULL, timeout);
82 ovs_fatal(error, "could not lock configuration file\n");
85 cfg_get_all(&orig_cfg);
95 cfg_get_all_strings(&vals, "%s", key);
97 for (i=0; i<vals.n; i++) {
98 printf("%s\n", vals.names[i]);
105 struct svec new_cfg, removed, added;
109 cfg_get_all(&new_cfg);
110 svec_diff(&orig_cfg, &new_cfg, &removed, NULL, &added);
111 if (removed.n || added.n) {
112 VLOG_INFO("configuration changes:");
113 for (i = 0; i < removed.n; i++) {
114 VLOG_INFO("-%s", removed.names[i]);
116 for (i = 0; i < added.n; i++) {
117 VLOG_INFO("+%s", added.names[i]);
120 VLOG_INFO("configuration unchanged");
122 svec_destroy(&added);
123 svec_destroy(&removed);
124 svec_swap(&new_cfg, &orig_cfg);
125 svec_destroy(&new_cfg);
128 int main(int argc, char *argv[])
131 OPT_DEL_MATCH = UCHAR_MAX + 1,
133 static const struct option long_options[] = {
134 {"config-file", required_argument, 0, 'F'},
135 {"timeout", required_argument, 0, 'T'},
136 {"add", required_argument, 0, 'a'},
137 {"del-entry", required_argument, 0, 'd'},
138 {"del-section", required_argument, 0, 'D'},
139 {"del-match", required_argument, 0, OPT_DEL_MATCH},
140 {"query", required_argument, 0, 'q'},
141 {"changes", no_argument, 0, 'c'},
142 {"verbose", optional_argument, 0, 'v'},
143 {"help", no_argument, 0, 'h'},
144 {"version", no_argument, 0, 'V'},
148 bool config_set = false;
149 int timeout = INT_MAX;
151 set_program_name(argv[0]);
155 short_options = long_options_to_short_options(long_options);
159 option = getopt_long(argc, argv, short_options, long_options, NULL);
164 if ((option > UCHAR_MAX || !strchr("FhVv?", option))
165 && config_set == false) {
166 ovs_fatal(0, "no config file specified (use --help for help)");
172 ovs_fatal(0, "--timeout or -T must be specified "
173 "before --file or -F");
175 timeout = atoi(optarg);
179 open_config(optarg, timeout);
184 cfg_add_entry("%s", optarg);
188 cfg_del_entry("%s", optarg);
192 cfg_del_section("%s", optarg);
196 cfg_del_match("%s", optarg);
208 usage(argv[0], EXIT_SUCCESS);
212 OVS_PRINT_VERSION(0, 0);
216 vlog_set_verbosity(optarg);
228 if (optind != argc) {
229 ovs_fatal(0, "non-option arguments not accepted "
230 "(use --help for help)");
233 if (cfg_is_dirty()) {