2 * Copyright (c) 2008, 2009, 2010, 2011, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "command-line.h"
22 #include "ovs-thread.h"
26 VLOG_DEFINE_THIS_MODULE(command_line
);
28 /* Given the GNU-style long options in 'options', returns a string that may be
29 * passed to getopt() with the corresponding short options. The caller is
30 * responsible for freeing the string. */
32 long_options_to_short_options(const struct option options
[])
34 char short_options
[UCHAR_MAX
* 3 + 1];
35 char *p
= short_options
;
37 for (; options
->name
; options
++) {
38 const struct option
*o
= options
;
39 if (o
->flag
== NULL
&& o
->val
> 0 && o
->val
<= UCHAR_MAX
) {
41 if (o
->has_arg
== required_argument
) {
43 } else if (o
->has_arg
== optional_argument
) {
51 return xstrdup(short_options
);
54 /* Runs the command designated by argv[0] within the command table specified by
55 * 'commands', which must be terminated by a command whose 'name' member is a
58 * Command-line options should be stripped off, so that a typical invocation
59 * looks like "run_command(argc - optind, argv + optind, my_commands);". */
61 run_command(int argc
, char *argv
[], const struct command commands
[])
63 const struct command
*p
;
66 ovs_fatal(0, "missing command name; use --help for help");
69 for (p
= commands
; p
->name
!= NULL
; p
++) {
70 if (!strcmp(p
->name
, argv
[0])) {
72 if (n_arg
< p
->min_args
) {
73 VLOG_FATAL( "'%s' command requires at least %d arguments",
74 p
->name
, p
->min_args
);
75 } else if (n_arg
> p
->max_args
) {
76 VLOG_FATAL("'%s' command takes at most %d arguments",
77 p
->name
, p
->max_args
);
79 p
->handler(argc
, argv
);
81 VLOG_FATAL("write to stdout failed");
84 VLOG_FATAL("write to stderr failed");
91 VLOG_FATAL("unknown command '%s'; use --help for help", argv
[0]);
97 static struct ovs_mutex proctitle_mutex
= OVS_MUTEX_INITIALIZER
;
99 /* Start of command-line arguments in memory. */
100 static char *argv_start
OVS_GUARDED_BY(proctitle_mutex
);
102 /* Number of bytes of command-line arguments. */
103 static size_t argv_size
OVS_GUARDED_BY(proctitle_mutex
);
105 /* Saved command-line arguments. */
106 static char *saved_proctitle
OVS_GUARDED_BY(proctitle_mutex
);
108 /* Prepares the process so that proctitle_set() can later succeed.
110 * This modifies the argv[] array so that it no longer points into the memory
111 * that it originally does. Later, proctitle_set() might overwrite that
112 * memory. That means that this function should be called before anything else
113 * that accesses the process's argv[] array. Ideally, it should be called
114 * before anything else, period, at the very beginning of program
117 proctitle_init(int argc
, char **argv
)
121 assert_single_threaded();
122 if (!argc
|| !argv
[0]) {
123 /* This situation should never occur, but... */
127 ovs_mutex_lock(&proctitle_mutex
);
128 /* Specialized version of first loop iteration below. */
129 argv_start
= argv
[0];
130 argv_size
= strlen(argv
[0]) + 1;
131 argv
[0] = xstrdup(argv
[0]);
133 for (i
= 1; i
< argc
; i
++) {
134 size_t size
= strlen(argv
[i
]) + 1;
136 /* Add (argv[i], strlen(argv[i])+1) to (argv_start, argv_size). */
137 if (argv
[i
] + size
== argv_start
) {
138 /* Arguments grow downward in memory. */
141 } else if (argv
[i
] == argv_start
+ argv_size
) {
142 /* Arguments grow upward in memory. */
145 /* Arguments not contiguous. (Is this really Linux?) */
148 /* Copy out the old argument so we can reuse the space. */
149 argv
[i
] = xstrdup(argv
[i
]);
151 ovs_mutex_unlock(&proctitle_mutex
);
154 /* Changes the name of the process, as shown by "ps", to the program name
155 * followed by 'format', which is formatted as if by printf(). */
157 proctitle_set(const char *format
, ...)
162 ovs_mutex_lock(&proctitle_mutex
);
163 if (!argv_start
|| argv_size
< 8) {
167 if (!saved_proctitle
) {
168 saved_proctitle
= xmemdup(argv_start
, argv_size
);
171 va_start(args
, format
);
172 n
= snprintf(argv_start
, argv_size
, "%s: ", program_name
);
174 n
+= vsnprintf(argv_start
+ n
, argv_size
- n
, format
, args
);
176 if (n
>= argv_size
) {
177 /* The name is too long, so add an ellipsis at the end. */
178 strcpy(&argv_start
[argv_size
- 4], "...");
180 /* Fill the extra space with null bytes, so that trailing bytes don't
181 * show up in the command line. */
182 memset(&argv_start
[n
], '\0', argv_size
- n
);
187 ovs_mutex_unlock(&proctitle_mutex
);
190 /* Restores the process's original command line, as seen by "ps". */
192 proctitle_restore(void)
194 ovs_mutex_lock(&proctitle_mutex
);
195 if (saved_proctitle
) {
196 memcpy(argv_start
, saved_proctitle
, argv_size
);
197 free(saved_proctitle
);
198 saved_proctitle
= NULL
;
200 ovs_mutex_unlock(&proctitle_mutex
);
202 #else /* !__linux__ */
203 /* Stubs that don't do anything on non-Linux systems. */
206 proctitle_init(int argc OVS_UNUSED
, char **argv OVS_UNUSED
)
210 #if !(defined(__FreeBSD__) || defined(__NetBSD__))
211 /* On these platforms we #define this to setproctitle. */
213 proctitle_set(const char *format OVS_UNUSED
, ...)
219 proctitle_restore(void)
222 #endif /* !__linux__ */