]> git.proxmox.com Git - mirror_ovs.git/blame - lib/command-line.c
Move lib/ofp-errors.h to include/openvswitch directory
[mirror_ovs.git] / lib / command-line.c
CommitLineData
064af421 1/*
2f51a7eb 2 * Copyright (c) 2008, 2009, 2010, 2011, 2013, 2014 Nicira, Inc.
064af421 3 *
a14bc59f
BP
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
064af421 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
064af421
BP
15 */
16
17#include <config.h>
18#include "command-line.h"
19#include <getopt.h>
20#include <limits.h>
675febfa 21#include <stdlib.h>
66fa2c88 22#include "dynamic-string.h"
728a8b14 23#include "ovs-thread.h"
064af421 24#include "util.h"
e6211adc 25#include "openvswitch/vlog.h"
279c9e03
BP
26
27VLOG_DEFINE_THIS_MODULE(command_line);
064af421
BP
28
29/* Given the GNU-style long options in 'options', returns a string that may be
30 * passed to getopt() with the corresponding short options. The caller is
31 * responsible for freeing the string. */
32char *
5f383751 33ovs_cmdl_long_options_to_short_options(const struct option options[])
064af421
BP
34{
35 char short_options[UCHAR_MAX * 3 + 1];
36 char *p = short_options;
d295e8e9 37
064af421
BP
38 for (; options->name; options++) {
39 const struct option *o = options;
40 if (o->flag == NULL && o->val > 0 && o->val <= UCHAR_MAX) {
41 *p++ = o->val;
42 if (o->has_arg == required_argument) {
43 *p++ = ':';
44 } else if (o->has_arg == optional_argument) {
45 *p++ = ':';
46 *p++ = ':';
47 }
48 }
49 }
50 *p = '\0';
d295e8e9 51
064af421
BP
52 return xstrdup(short_options);
53}
54
5f383751 55/* Given the 'struct ovs_cmdl_command' array, prints the usage of all commands. */
451de37e 56void
5f383751 57ovs_cmdl_print_commands(const struct ovs_cmdl_command commands[])
451de37e
AW
58{
59 struct ds ds = DS_EMPTY_INITIALIZER;
60
61 ds_put_cstr(&ds, "The available commands are:\n");
62 for (; commands->name; commands++) {
5f383751 63 const struct ovs_cmdl_command *c = commands;
451de37e
AW
64 ds_put_format(&ds, " %-23s %s\n", c->name, c->usage ? c->usage : "");
65 }
66 printf("%s", ds.string);
67 ds_destroy(&ds);
68}
69
66fa2c88
AW
70/* Given the GNU-style options in 'options', prints all options. */
71void
5f383751 72ovs_cmdl_print_options(const struct option options[])
66fa2c88
AW
73{
74 struct ds ds = DS_EMPTY_INITIALIZER;
75
76 for (; options->name; options++) {
77 const struct option *o = options;
78 const char *arg = o->has_arg == required_argument ? "ARG" : "[ARG]";
79
80 ds_put_format(&ds, "--%s%s%s\n", o->name, o->has_arg ? "=" : "",
81 o->has_arg ? arg : "");
82 if (o->flag == NULL && o->val > 0 && o->val <= UCHAR_MAX) {
83 ds_put_format(&ds, "-%c %s\n", o->val, o->has_arg ? arg : "");
84 }
85 }
86 printf("%s", ds.string);
87 ds_destroy(&ds);
88}
89
675febfa
BP
90/* Runs the command designated by argv[0] within the command table specified by
91 * 'commands', which must be terminated by a command whose 'name' member is a
92 * null pointer.
93 *
94 * Command-line options should be stripped off, so that a typical invocation
1636c761
RB
95 * looks like:
96 * struct ovs_cmdl_context ctx = {
97 * .argc = argc - optind,
98 * .argv = argv + optind,
99 * };
100 * ovs_cmdl_run_command(&ctx, my_commands);
101 * */
675febfa 102void
1636c761 103ovs_cmdl_run_command(struct ovs_cmdl_context *ctx, const struct ovs_cmdl_command commands[])
675febfa 104{
5f383751 105 const struct ovs_cmdl_command *p;
675febfa 106
1636c761 107 if (ctx->argc < 1) {
675febfa
BP
108 ovs_fatal(0, "missing command name; use --help for help");
109 }
110
111 for (p = commands; p->name != NULL; p++) {
1636c761
RB
112 if (!strcmp(p->name, ctx->argv[0])) {
113 int n_arg = ctx->argc - 1;
675febfa 114 if (n_arg < p->min_args) {
279c9e03
BP
115 VLOG_FATAL( "'%s' command requires at least %d arguments",
116 p->name, p->min_args);
675febfa 117 } else if (n_arg > p->max_args) {
279c9e03
BP
118 VLOG_FATAL("'%s' command takes at most %d arguments",
119 p->name, p->max_args);
675febfa 120 } else {
1636c761 121 p->handler(ctx);
675febfa 122 if (ferror(stdout)) {
279c9e03 123 VLOG_FATAL("write to stdout failed");
675febfa
BP
124 }
125 if (ferror(stderr)) {
279c9e03 126 VLOG_FATAL("write to stderr failed");
675febfa
BP
127 }
128 return;
129 }
130 }
131 }
132
1636c761 133 VLOG_FATAL("unknown command '%s'; use --help for help", ctx->argv[0]);
675febfa 134}
40f0707c
BP
135\f
136/* Process title. */
137
2f51a7eb 138#ifdef __linux__
97be1538
EJ
139static struct ovs_mutex proctitle_mutex = OVS_MUTEX_INITIALIZER;
140
141/* Start of command-line arguments in memory. */
142static char *argv_start OVS_GUARDED_BY(proctitle_mutex);
143
144/* Number of bytes of command-line arguments. */
145static size_t argv_size OVS_GUARDED_BY(proctitle_mutex);
146
147/* Saved command-line arguments. */
148static char *saved_proctitle OVS_GUARDED_BY(proctitle_mutex);
40f0707c
BP
149
150/* Prepares the process so that proctitle_set() can later succeed.
151 *
152 * This modifies the argv[] array so that it no longer points into the memory
153 * that it originally does. Later, proctitle_set() might overwrite that
154 * memory. That means that this function should be called before anything else
155 * that accesses the process's argv[] array. Ideally, it should be called
156 * before anything else, period, at the very beginning of program
157 * execution. */
158void
5f383751 159ovs_cmdl_proctitle_init(int argc, char **argv)
40f0707c
BP
160{
161 int i;
162
728a8b14 163 assert_single_threaded();
40f0707c
BP
164 if (!argc || !argv[0]) {
165 /* This situation should never occur, but... */
166 return;
167 }
168
97be1538 169 ovs_mutex_lock(&proctitle_mutex);
40f0707c
BP
170 /* Specialized version of first loop iteration below. */
171 argv_start = argv[0];
172 argv_size = strlen(argv[0]) + 1;
173 argv[0] = xstrdup(argv[0]);
174
175 for (i = 1; i < argc; i++) {
176 size_t size = strlen(argv[i]) + 1;
177
178 /* Add (argv[i], strlen(argv[i])+1) to (argv_start, argv_size). */
179 if (argv[i] + size == argv_start) {
180 /* Arguments grow downward in memory. */
181 argv_start -= size;
182 argv_size += size;
183 } else if (argv[i] == argv_start + argv_size) {
184 /* Arguments grow upward in memory. */
185 argv_size += size;
186 } else {
187 /* Arguments not contiguous. (Is this really Linux?) */
188 }
189
190 /* Copy out the old argument so we can reuse the space. */
191 argv[i] = xstrdup(argv[i]);
192 }
97be1538 193 ovs_mutex_unlock(&proctitle_mutex);
40f0707c
BP
194}
195
d86a6c09
EM
196/* Changes the name of the process, as shown by "ps", to the program name
197 * followed by 'format', which is formatted as if by printf(). */
40f0707c 198void
5f383751 199ovs_cmdl_proctitle_set(const char *format, ...)
40f0707c
BP
200{
201 va_list args;
202 int n;
203
97be1538 204 ovs_mutex_lock(&proctitle_mutex);
40f0707c 205 if (!argv_start || argv_size < 8) {
97be1538 206 goto out;
40f0707c
BP
207 }
208
209 if (!saved_proctitle) {
210 saved_proctitle = xmemdup(argv_start, argv_size);
211 }
212
213 va_start(args, format);
d86a6c09
EM
214 n = snprintf(argv_start, argv_size, "%s: ", program_name);
215 if (n < argv_size) {
216 n += vsnprintf(argv_start + n, argv_size - n, format, args);
217 }
40f0707c
BP
218 if (n >= argv_size) {
219 /* The name is too long, so add an ellipsis at the end. */
220 strcpy(&argv_start[argv_size - 4], "...");
221 } else {
222 /* Fill the extra space with null bytes, so that trailing bytes don't
223 * show up in the command line. */
224 memset(&argv_start[n], '\0', argv_size - n);
225 }
226 va_end(args);
97be1538
EJ
227
228out:
229 ovs_mutex_unlock(&proctitle_mutex);
40f0707c
BP
230}
231
232/* Restores the process's original command line, as seen by "ps". */
233void
5f383751 234ovs_cmdl_proctitle_restore(void)
40f0707c 235{
97be1538 236 ovs_mutex_lock(&proctitle_mutex);
40f0707c
BP
237 if (saved_proctitle) {
238 memcpy(argv_start, saved_proctitle, argv_size);
239 free(saved_proctitle);
240 saved_proctitle = NULL;
241 }
97be1538 242 ovs_mutex_unlock(&proctitle_mutex);
40f0707c 243}
2f51a7eb 244#else /* !__linux__ */
40f0707c
BP
245/* Stubs that don't do anything on non-Linux systems. */
246
247void
5f383751 248ovs_cmdl_proctitle_init(int argc OVS_UNUSED, char **argv OVS_UNUSED)
40f0707c
BP
249{
250}
251
666afb55
YT
252#if !(defined(__FreeBSD__) || defined(__NetBSD__))
253/* On these platforms we #define this to setproctitle. */
40f0707c 254void
5f383751 255ovs_cmdl_proctitle_set(const char *format OVS_UNUSED, ...)
40f0707c
BP
256{
257}
fb301c74 258#endif
40f0707c
BP
259
260void
5f383751 261ovs_cmdl_proctitle_restore(void)
40f0707c
BP
262{
263}
2f51a7eb 264#endif /* !__linux__ */