2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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.
27 #define THIS_MODULE VLM_util
30 const char *program_name;
35 ovs_fatal(0, "virtual memory exhausted");
39 xcalloc(size_t count, size_t size)
41 void *p = count && size ? calloc(count, size) : malloc(1);
42 COVERAGE_INC(util_xalloc);
52 return xcalloc(1, size);
58 void *p = malloc(size ? size : 1);
59 COVERAGE_INC(util_xalloc);
67 xrealloc(void *p, size_t size)
69 p = realloc(p, size ? size : 1);
70 COVERAGE_INC(util_xalloc);
78 xmemdup(const void *p_, size_t size)
80 void *p = xmalloc(size);
86 xmemdup0(const char *p_, size_t length)
88 char *p = xmalloc(length + 1);
89 memcpy(p, p_, length);
95 xstrdup(const char *s)
97 return xmemdup0(s, strlen(s));
101 xvasprintf(const char *format, va_list args)
107 va_copy(args2, args);
108 needed = vsnprintf(NULL, 0, format, args);
110 s = xmalloc(needed + 1);
112 vsnprintf(s, needed + 1, format, args2);
119 x2nrealloc(void *p, size_t *n, size_t s)
121 *n = *n == 0 ? 1 : 2 * *n;
122 return xrealloc(p, *n * s);
126 xasprintf(const char *format, ...)
131 va_start(args, format);
132 s = xvasprintf(format, args);
139 ovs_strlcpy(char *dst, const char *src, size_t size)
142 size_t n = strlen(src);
143 size_t n_copy = MIN(n, size - 1);
144 memcpy(dst, src, n_copy);
150 ovs_fatal(int err_no, const char *format, ...)
154 fprintf(stderr, "%s: ", program_name);
155 va_start(args, format);
156 vfprintf(stderr, format, args);
159 fprintf(stderr, " (%s)", strerror(err_no));
166 ovs_error(int err_no, const char *format, ...)
168 int save_errno = errno;
171 fprintf(stderr, "%s: ", program_name);
172 va_start(args, format);
173 vfprintf(stderr, format, args);
176 fprintf(stderr, " (%s)",
177 err_no == EOF ? "end of file" : strerror(err_no));
184 /* Sets program_name based on 'argv0'. Should be called at the beginning of
185 * main(), as "set_program_name(argv[0]);". */
186 void set_program_name(const char *argv0)
188 const char *slash = strrchr(argv0, '/');
189 program_name = slash ? slash + 1 : argv0;
192 /* Print the version information for the program. */
194 ovs_print_version(char *date, char *time,
195 uint8_t min_ofp, uint8_t max_ofp)
197 printf("%s (Open vSwitch) "VERSION BUILDNR"\n", program_name);
198 printf("Compiled %s %s\n", date, time);
199 if (min_ofp || max_ofp) {
200 printf("OpenFlow versions %#x:%#x\n", min_ofp, max_ofp);
204 /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per
205 * line. Numeric offsets are also included, starting at 'ofs' for the first
206 * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
207 * are also rendered alongside. */
209 ovs_hex_dump(FILE *stream, const void *buf_, size_t size,
210 uintptr_t ofs, bool ascii)
212 const uint8_t *buf = buf_;
213 const size_t per_line = 16; /* Maximum bytes per line. */
217 size_t start, end, n;
220 /* Number of bytes on this line. */
221 start = ofs % per_line;
223 if (end - start > size)
228 fprintf(stream, "%08jx ", (uintmax_t) ROUND_DOWN(ofs, per_line));
229 for (i = 0; i < start; i++)
230 fprintf(stream, " ");
232 fprintf(stream, "%02hhx%c",
233 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
236 for (; i < per_line; i++)
237 fprintf(stream, " ");
238 fprintf(stream, "|");
239 for (i = 0; i < start; i++)
240 fprintf(stream, " ");
241 for (; i < end; i++) {
242 int c = buf[i - start];
243 putc(c >= 32 && c < 127 ? c : '.', stream);
245 for (; i < per_line; i++)
246 fprintf(stream, " ");
247 fprintf(stream, "|");
249 fprintf(stream, "\n");
258 str_to_int(const char *s, int base, int *i)
261 bool ok = str_to_llong(s, base, &ll);
267 str_to_long(const char *s, int base, long *li)
270 bool ok = str_to_llong(s, base, &ll);
276 str_to_llong(const char *s, int base, long long *x)
278 int save_errno = errno;
281 *x = strtoll(s, &tail, base);
282 if (errno == EINVAL || errno == ERANGE || tail == s || *tail != '\0') {
293 str_to_uint(const char *s, int base, unsigned int *u)
295 return str_to_int(s, base, (int *) u);
299 str_to_ulong(const char *s, int base, unsigned long *ul)
301 return str_to_long(s, base, (long *) ul);
305 str_to_ullong(const char *s, int base, unsigned long long *ull)
307 return str_to_llong(s, base, (long long *) ull);
310 /* Converts floating-point string 's' into a double. If successful, stores
311 * the double in '*d' and returns true; on failure, stores 0 in '*d' and
314 * Underflow (e.g. "1e-9999") is not considered an error, but overflow
315 * (e.g. "1e9999)" is. */
317 str_to_double(const char *s, double *d)
319 int save_errno = errno;
322 *d = strtod(s, &tail);
323 if (errno == EINVAL || (errno == ERANGE && *d != 0)
324 || tail == s || *tail != '\0') {
334 /* Returns the value of 'c' as a hexadecimal digit. */
339 case '0': case '1': case '2': case '3': case '4':
340 case '5': case '6': case '7': case '8': case '9':
365 /* Returns the current working directory as a malloc()'d string, or a null
366 * pointer if the current working directory cannot be determined. */
373 /* Get maximum path length or at least a reasonable estimate. */
374 path_max = pathconf(".", _PC_PATH_MAX);
375 size = (path_max < 0 ? 1024
376 : path_max > 10240 ? 10240
379 /* Get current working directory. */
381 char *buf = xmalloc(size);
382 if (getcwd(buf, size)) {
383 return xrealloc(buf, strlen(buf) + 1);
387 if (error != ERANGE) {
388 VLOG_WARN("getcwd failed (%s)", strerror(error));
396 /* Returns the directory name portion of 'file_name' as a malloc()'d string,
397 * similar to the POSIX dirname() function but thread-safe. */
399 dir_name(const char *file_name)
401 size_t len = strlen(file_name);
402 while (len > 0 && file_name[len - 1] == '/') {
405 while (len > 0 && file_name[len - 1] != '/') {
408 while (len > 0 && file_name[len - 1] == '/') {
412 return xstrdup((file_name[0] == '/'
413 && file_name[1] == '/'
414 && file_name[2] != '/') ? "//"
415 : file_name[0] == '/' ? "/"
418 return xmemdup0(file_name, len);
422 /* If 'file_name' starts with '/', returns a copy of 'file_name'. Otherwise,
423 * returns an absolute path to 'file_name' considering it relative to 'dir',
424 * which itself must be absolute. 'dir' may be null or the empty string, in
425 * which case the current working directory is used.
427 * Returns a null pointer if 'dir' is null and getcwd() fails. */
429 abs_file_name(const char *dir, const char *file_name)
431 if (file_name[0] == '/') {
432 return xstrdup(file_name);
433 } else if (dir && dir[0]) {
434 char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/";
435 return xasprintf("%s%s%s", dir, separator, file_name);
437 char *cwd = get_cwd();
439 char *abs_name = xasprintf("%s/%s", cwd, file_name);
449 /* Pass a value to this function if it is marked with
450 * __attribute__((warn_unused_result)) and you genuinely want to ignore
451 * its return value. (Note that every scalar type can be implicitly
452 * converted to bool.) */
453 void ignore(bool x OVS_UNUSED) { }