]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
2225c0b9 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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 "util.h" | |
ed2232fc | 19 | #include <ctype.h> |
064af421 | 20 | #include <errno.h> |
711e0157 | 21 | #include <limits.h> |
5fcbed74 | 22 | #include <pthread.h> |
064af421 | 23 | #include <stdarg.h> |
711e0157 | 24 | #include <stdint.h> |
064af421 BP |
25 | #include <stdio.h> |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
fee0c963 | 28 | #include <sys/stat.h> |
daf03c53 | 29 | #include <unistd.h> |
ed2232fc | 30 | #include "bitmap.h" |
ddc4f8e2 | 31 | #include "byte-order.h" |
064af421 | 32 | #include "coverage.h" |
275eebb9 | 33 | #include "ovs-rcu.h" |
728a8b14 | 34 | #include "ovs-thread.h" |
64559798 | 35 | #include "socket-util.h" |
ff1d2c16 | 36 | #include "timeval.h" |
e6211adc | 37 | #include "openvswitch/vlog.h" |
1805ee4b EM |
38 | #ifdef HAVE_PTHREAD_SET_NAME_NP |
39 | #include <pthread_np.h> | |
40 | #endif | |
4d8f04b3 BP |
41 | #ifdef _WIN32 |
42 | #include <shlwapi.h> | |
43 | #endif | |
daf03c53 | 44 | |
d98e6007 | 45 | VLOG_DEFINE_THIS_MODULE(util); |
5136ce49 | 46 | |
ff1d2c16 BB |
47 | #ifdef __linux__ |
48 | #define LINUX 1 | |
49 | #include <asm/param.h> | |
50 | #else | |
51 | #define LINUX 0 | |
52 | #endif | |
53 | ||
d76f09ea BP |
54 | COVERAGE_DEFINE(util_xalloc); |
55 | ||
781dee08 | 56 | /* argv[0] without directory names. */ |
91e12f0d | 57 | char *program_name; |
781dee08 | 58 | |
bc9fb3a9 BP |
59 | /* Name for the currently running thread or process, for log messages, process |
60 | * listings, and debuggers. */ | |
61 | DEFINE_PER_THREAD_MALLOCED_DATA(char *, subprogram_name); | |
781dee08 BP |
62 | |
63 | /* --version option output. */ | |
55d5bb44 | 64 | static char *program_version; |
064af421 | 65 | |
da05d1c8 IM |
66 | /* 'true' if mlockall() succeeded. */ |
67 | static bool is_memory_locked = false; | |
68 | ||
315ea327 | 69 | /* Buffer used by ovs_strerror() and ovs_format_message(). */ |
2ba4f163 BP |
70 | DEFINE_STATIC_PER_THREAD_DATA(struct { char s[128]; }, |
71 | strerror_buffer, | |
72 | { "" }); | |
5fcbed74 | 73 | |
ac01d085 GS |
74 | static char *xreadlink(const char *filename); |
75 | ||
4749f73d BP |
76 | void |
77 | ovs_assert_failure(const char *where, const char *function, | |
78 | const char *condition) | |
79 | { | |
80 | /* Prevent an infinite loop (or stack overflow) in case VLOG_ABORT happens | |
81 | * to trigger an assertion failure of its own. */ | |
82 | static int reentry = 0; | |
83 | ||
84 | switch (reentry++) { | |
85 | case 0: | |
86 | VLOG_ABORT("%s: assertion %s failed in %s()", | |
87 | where, condition, function); | |
428b2edd | 88 | OVS_NOT_REACHED(); |
4749f73d BP |
89 | |
90 | case 1: | |
91 | fprintf(stderr, "%s: assertion %s failed in %s()", | |
92 | where, condition, function); | |
93 | abort(); | |
94 | ||
95 | default: | |
96 | abort(); | |
97 | } | |
98 | } | |
99 | ||
da05d1c8 IM |
100 | void |
101 | set_memory_locked(void) | |
102 | { | |
103 | is_memory_locked = true; | |
104 | } | |
105 | ||
106 | bool | |
107 | memory_locked(void) | |
108 | { | |
109 | return is_memory_locked; | |
110 | } | |
111 | ||
064af421 | 112 | void |
d295e8e9 | 113 | out_of_memory(void) |
064af421 | 114 | { |
c1c8308a | 115 | ovs_abort(0, "virtual memory exhausted"); |
064af421 BP |
116 | } |
117 | ||
118 | void * | |
d295e8e9 | 119 | xcalloc(size_t count, size_t size) |
064af421 BP |
120 | { |
121 | void *p = count && size ? calloc(count, size) : malloc(1); | |
122 | COVERAGE_INC(util_xalloc); | |
123 | if (p == NULL) { | |
124 | out_of_memory(); | |
125 | } | |
126 | return p; | |
127 | } | |
128 | ||
ec6fde61 BP |
129 | void * |
130 | xzalloc(size_t size) | |
131 | { | |
132 | return xcalloc(1, size); | |
133 | } | |
134 | ||
064af421 | 135 | void * |
d295e8e9 | 136 | xmalloc(size_t size) |
064af421 BP |
137 | { |
138 | void *p = malloc(size ? size : 1); | |
139 | COVERAGE_INC(util_xalloc); | |
140 | if (p == NULL) { | |
141 | out_of_memory(); | |
142 | } | |
143 | return p; | |
144 | } | |
145 | ||
146 | void * | |
d295e8e9 | 147 | xrealloc(void *p, size_t size) |
064af421 BP |
148 | { |
149 | p = realloc(p, size ? size : 1); | |
150 | COVERAGE_INC(util_xalloc); | |
151 | if (p == NULL) { | |
152 | out_of_memory(); | |
153 | } | |
154 | return p; | |
155 | } | |
156 | ||
157 | void * | |
158 | xmemdup(const void *p_, size_t size) | |
159 | { | |
160 | void *p = xmalloc(size); | |
316d0932 | 161 | nullable_memcpy(p, p_, size); |
064af421 BP |
162 | return p; |
163 | } | |
164 | ||
165 | char * | |
166 | xmemdup0(const char *p_, size_t length) | |
167 | { | |
168 | char *p = xmalloc(length + 1); | |
169 | memcpy(p, p_, length); | |
170 | p[length] = '\0'; | |
171 | return p; | |
172 | } | |
173 | ||
174 | char * | |
d295e8e9 | 175 | xstrdup(const char *s) |
064af421 BP |
176 | { |
177 | return xmemdup0(s, strlen(s)); | |
178 | } | |
179 | ||
2225c0b9 BP |
180 | char * MALLOC_LIKE |
181 | nullable_xstrdup(const char *s) | |
182 | { | |
183 | return s ? xstrdup(s) : NULL; | |
184 | } | |
185 | ||
aacf18c3 IM |
186 | bool |
187 | nullable_string_is_equal(const char *a, const char *b) | |
188 | { | |
189 | return a ? b && !strcmp(a, b) : !b; | |
190 | } | |
191 | ||
064af421 BP |
192 | char * |
193 | xvasprintf(const char *format, va_list args) | |
194 | { | |
195 | va_list args2; | |
196 | size_t needed; | |
197 | char *s; | |
198 | ||
199 | va_copy(args2, args); | |
200 | needed = vsnprintf(NULL, 0, format, args); | |
201 | ||
202 | s = xmalloc(needed + 1); | |
203 | ||
204 | vsnprintf(s, needed + 1, format, args2); | |
205 | va_end(args2); | |
206 | ||
207 | return s; | |
208 | } | |
209 | ||
210 | void * | |
211 | x2nrealloc(void *p, size_t *n, size_t s) | |
212 | { | |
213 | *n = *n == 0 ? 1 : 2 * *n; | |
214 | return xrealloc(p, *n * s); | |
215 | } | |
216 | ||
0de1b425 WT |
217 | /* Allocates and returns 'size' bytes of memory aligned to 'alignment' bytes. |
218 | * 'alignment' must be a power of two and a multiple of sizeof(void *). | |
2fec66db | 219 | * |
0de1b425 | 220 | * Use free_size_align() to free the returned memory block. */ |
2fec66db | 221 | void * |
0de1b425 | 222 | xmalloc_size_align(size_t size, size_t alignment) |
2fec66db | 223 | { |
3dc62a6c YT |
224 | #ifdef HAVE_POSIX_MEMALIGN |
225 | void *p; | |
226 | int error; | |
227 | ||
228 | COVERAGE_INC(util_xalloc); | |
0de1b425 | 229 | error = posix_memalign(&p, alignment, size ? size : 1); |
3dc62a6c YT |
230 | if (error != 0) { |
231 | out_of_memory(); | |
232 | } | |
233 | return p; | |
234 | #else | |
2fec66db BP |
235 | /* Allocate room for: |
236 | * | |
0de1b425 WT |
237 | * - Header padding: Up to alignment - 1 bytes, to allow the |
238 | * pointer 'q' to be aligned exactly sizeof(void *) bytes before the | |
239 | * beginning of the alignment. | |
2fec66db | 240 | * |
ee3ac719 BP |
241 | * - Pointer: A pointer to the start of the header padding, to allow us |
242 | * to free() the block later. | |
2fec66db | 243 | * |
ee3ac719 BP |
244 | * - User data: 'size' bytes. |
245 | * | |
0de1b425 | 246 | * - Trailer padding: Enough to bring the user data up to a alignment |
ee3ac719 BP |
247 | * multiple. |
248 | * | |
249 | * +---------------+---------+------------------------+---------+ | |
250 | * | header | pointer | user data | trailer | | |
251 | * +---------------+---------+------------------------+---------+ | |
252 | * ^ ^ ^ | |
253 | * | | | | |
254 | * p q r | |
255 | * | |
256 | */ | |
0de1b425 WT |
257 | void *p, *r, **q; |
258 | bool runt; | |
259 | ||
260 | if (!IS_POW2(alignment) || (alignment % sizeof(void *) != 0)) { | |
261 | ovs_abort(0, "Invalid alignment"); | |
262 | } | |
263 | ||
264 | p = xmalloc((alignment - 1) | |
265 | + sizeof(void *) | |
266 | + ROUND_UP(size, alignment)); | |
267 | ||
268 | runt = PAD_SIZE((uintptr_t) p, alignment) < sizeof(void *); | |
269 | /* When the padding size < sizeof(void*), we don't have enough room for | |
270 | * pointer 'q'. As a reuslt, need to move 'r' to the next alignment. | |
271 | * So ROUND_UP when xmalloc above, and ROUND_UP again when calculate 'r' | |
272 | * below. | |
273 | */ | |
274 | r = (void *) ROUND_UP((uintptr_t) p + (runt ? alignment : 0), alignment); | |
275 | q = (void **) r - 1; | |
ee3ac719 | 276 | *q = p; |
0de1b425 | 277 | |
ee3ac719 | 278 | return r; |
3dc62a6c | 279 | #endif |
2fec66db BP |
280 | } |
281 | ||
0de1b425 WT |
282 | void |
283 | free_size_align(void *p) | |
284 | { | |
285 | #ifdef HAVE_POSIX_MEMALIGN | |
286 | free(p); | |
287 | #else | |
288 | if (p) { | |
289 | void **q = (void **) p - 1; | |
290 | free(*q); | |
291 | } | |
292 | #endif | |
293 | } | |
294 | ||
295 | /* Allocates and returns 'size' bytes of memory aligned to a cache line and in | |
296 | * dedicated cache lines. That is, the memory block returned will not share a | |
297 | * cache line with other data, avoiding "false sharing". | |
298 | * | |
299 | * Use free_cacheline() to free the returned memory block. */ | |
300 | void * | |
301 | xmalloc_cacheline(size_t size) | |
302 | { | |
303 | return xmalloc_size_align(size, CACHE_LINE_SIZE); | |
304 | } | |
305 | ||
2fec66db BP |
306 | /* Like xmalloc_cacheline() but clears the allocated memory to all zero |
307 | * bytes. */ | |
308 | void * | |
309 | xzalloc_cacheline(size_t size) | |
310 | { | |
311 | void *p = xmalloc_cacheline(size); | |
312 | memset(p, 0, size); | |
313 | return p; | |
314 | } | |
315 | ||
316 | /* Frees a memory block allocated with xmalloc_cacheline() or | |
317 | * xzalloc_cacheline(). */ | |
318 | void | |
319 | free_cacheline(void *p) | |
320 | { | |
0de1b425 WT |
321 | free_size_align(p); |
322 | } | |
323 | ||
324 | void * | |
325 | xmalloc_pagealign(size_t size) | |
326 | { | |
327 | return xmalloc_size_align(size, get_page_size()); | |
328 | } | |
329 | ||
330 | void | |
331 | free_pagealign(void *p) | |
332 | { | |
333 | free_size_align(p); | |
2fec66db BP |
334 | } |
335 | ||
064af421 BP |
336 | char * |
337 | xasprintf(const char *format, ...) | |
338 | { | |
339 | va_list args; | |
340 | char *s; | |
341 | ||
342 | va_start(args, format); | |
343 | s = xvasprintf(format, args); | |
344 | va_end(args); | |
345 | ||
346 | return s; | |
347 | } | |
348 | ||
e868fb3d BP |
349 | /* Similar to strlcpy() from OpenBSD, but it never reads more than 'size - 1' |
350 | * bytes from 'src' and doesn't return anything. */ | |
064af421 BP |
351 | void |
352 | ovs_strlcpy(char *dst, const char *src, size_t size) | |
353 | { | |
354 | if (size > 0) { | |
e868fb3d BP |
355 | size_t len = strnlen(src, size - 1); |
356 | memcpy(dst, src, len); | |
357 | dst[len] = '\0'; | |
064af421 BP |
358 | } |
359 | } | |
360 | ||
71d7c22f BP |
361 | /* Copies 'src' to 'dst'. Reads no more than 'size - 1' bytes from 'src'. |
362 | * Always null-terminates 'dst' (if 'size' is nonzero), and writes a zero byte | |
363 | * to every otherwise unused byte in 'dst'. | |
364 | * | |
365 | * Except for performance, the following call: | |
366 | * ovs_strzcpy(dst, src, size); | |
367 | * is equivalent to these two calls: | |
368 | * memset(dst, '\0', size); | |
369 | * ovs_strlcpy(dst, src, size); | |
370 | * | |
371 | * (Thus, ovs_strzcpy() is similar to strncpy() without some of the pitfalls.) | |
372 | */ | |
373 | void | |
374 | ovs_strzcpy(char *dst, const char *src, size_t size) | |
375 | { | |
376 | if (size > 0) { | |
377 | size_t len = strnlen(src, size - 1); | |
378 | memcpy(dst, src, len); | |
379 | memset(dst + len, '\0', size - len); | |
380 | } | |
381 | } | |
382 | ||
971f4b39 MW |
383 | /* |
384 | * Returns true if 'str' ends with given 'suffix'. | |
385 | */ | |
386 | int | |
387 | string_ends_with(const char *str, const char *suffix) | |
388 | { | |
389 | int str_len = strlen(str); | |
390 | int suffix_len = strlen(suffix); | |
391 | ||
392 | return (str_len >= suffix_len) && | |
393 | (0 == strcmp(str + (str_len - suffix_len), suffix)); | |
394 | } | |
395 | ||
c1c8308a BP |
396 | /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is |
397 | * nonzero, then it is formatted with ovs_retval_to_string() and appended to | |
398 | * the message inside parentheses. Then, terminates with abort(). | |
399 | * | |
400 | * This function is preferred to ovs_fatal() in a situation where it would make | |
401 | * sense for a monitoring process to restart the daemon. | |
402 | * | |
403 | * 'format' should not end with a new-line, because this function will add one | |
404 | * itself. */ | |
405 | void | |
406 | ovs_abort(int err_no, const char *format, ...) | |
407 | { | |
408 | va_list args; | |
409 | ||
410 | va_start(args, format); | |
d41d4b71 BP |
411 | ovs_abort_valist(err_no, format, args); |
412 | } | |
c1c8308a | 413 | |
d41d4b71 BP |
414 | /* Same as ovs_abort() except that the arguments are supplied as a va_list. */ |
415 | void | |
416 | ovs_abort_valist(int err_no, const char *format, va_list args) | |
417 | { | |
418 | ovs_error_valist(err_no, format, args); | |
c1c8308a BP |
419 | abort(); |
420 | } | |
421 | ||
422 | /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is | |
423 | * nonzero, then it is formatted with ovs_retval_to_string() and appended to | |
424 | * the message inside parentheses. Then, terminates with EXIT_FAILURE. | |
425 | * | |
426 | * 'format' should not end with a new-line, because this function will add one | |
427 | * itself. */ | |
064af421 BP |
428 | void |
429 | ovs_fatal(int err_no, const char *format, ...) | |
430 | { | |
431 | va_list args; | |
432 | ||
064af421 | 433 | va_start(args, format); |
fcaddd4d BP |
434 | ovs_fatal_valist(err_no, format, args); |
435 | } | |
064af421 | 436 | |
fcaddd4d BP |
437 | /* Same as ovs_fatal() except that the arguments are supplied as a va_list. */ |
438 | void | |
439 | ovs_fatal_valist(int err_no, const char *format, va_list args) | |
440 | { | |
441 | ovs_error_valist(err_no, format, args); | |
064af421 BP |
442 | exit(EXIT_FAILURE); |
443 | } | |
444 | ||
c1c8308a BP |
445 | /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is |
446 | * nonzero, then it is formatted with ovs_retval_to_string() and appended to | |
447 | * the message inside parentheses. | |
448 | * | |
449 | * 'format' should not end with a new-line, because this function will add one | |
450 | * itself. */ | |
064af421 BP |
451 | void |
452 | ovs_error(int err_no, const char *format, ...) | |
453 | { | |
064af421 BP |
454 | va_list args; |
455 | ||
064af421 | 456 | va_start(args, format); |
c1c8308a | 457 | ovs_error_valist(err_no, format, args); |
064af421 | 458 | va_end(args); |
c1c8308a BP |
459 | } |
460 | ||
461 | /* Same as ovs_error() except that the arguments are supplied as a va_list. */ | |
462 | void | |
463 | ovs_error_valist(int err_no, const char *format, va_list args) | |
464 | { | |
bc9fb3a9 | 465 | const char *subprogram_name = get_subprogram_name(); |
c1c8308a BP |
466 | int save_errno = errno; |
467 | ||
781dee08 BP |
468 | if (subprogram_name[0]) { |
469 | fprintf(stderr, "%s(%s): ", program_name, subprogram_name); | |
470 | } else { | |
471 | fprintf(stderr, "%s: ", program_name); | |
472 | } | |
473 | ||
c1c8308a | 474 | vfprintf(stderr, format, args); |
0fec26b0 | 475 | if (err_no != 0) { |
c18ea70d | 476 | fprintf(stderr, " (%s)", ovs_retval_to_string(err_no)); |
0fec26b0 | 477 | } |
064af421 BP |
478 | putc('\n', stderr); |
479 | ||
480 | errno = save_errno; | |
481 | } | |
482 | ||
c18ea70d AE |
483 | /* Many OVS functions return an int which is one of: |
484 | * - 0: no error yet | |
485 | * - >0: errno value | |
486 | * - EOF: end of file (not necessarily an error; depends on the function called) | |
487 | * | |
488 | * Returns the appropriate human-readable string. The caller must copy the | |
489 | * string if it wants to hold onto it, as the storage may be overwritten on | |
490 | * subsequent function calls. | |
491 | */ | |
492 | const char * | |
493 | ovs_retval_to_string(int retval) | |
494 | { | |
5fcbed74 BP |
495 | return (!retval ? "" |
496 | : retval == EOF ? "End of file" | |
497 | : ovs_strerror(retval)); | |
498 | } | |
c18ea70d | 499 | |
b26f46a4 GS |
500 | /* This function returns the string describing the error number in 'error' |
501 | * for POSIX platforms. For Windows, this function can be used for C library | |
502 | * calls. For socket calls that are also used in Windows, use sock_strerror() | |
503 | * instead. For WINAPI calls, look at ovs_lasterror_to_string(). */ | |
5fcbed74 BP |
504 | const char * |
505 | ovs_strerror(int error) | |
506 | { | |
507 | enum { BUFSIZE = sizeof strerror_buffer_get()->s }; | |
508 | int save_errno; | |
509 | char *buffer; | |
510 | char *s; | |
511 | ||
11b7f938 YT |
512 | if (error == 0) { |
513 | /* | |
514 | * strerror(0) varies among platforms: | |
515 | * | |
516 | * Success | |
517 | * No error | |
518 | * Undefined error: 0 | |
519 | * | |
520 | * We want to provide a consistent result here because | |
521 | * our testsuite has test cases which strictly matches | |
522 | * log messages containing this string. | |
523 | */ | |
524 | return "Success"; | |
525 | } | |
526 | ||
5fcbed74 BP |
527 | save_errno = errno; |
528 | buffer = strerror_buffer_get()->s; | |
529 | ||
530 | #if STRERROR_R_CHAR_P | |
531 | /* GNU style strerror_r() might return an immutable static string, or it | |
532 | * might write and return 'buffer', but in either case we can pass the | |
533 | * returned string directly to the caller. */ | |
534 | s = strerror_r(error, buffer, BUFSIZE); | |
535 | #else /* strerror_r() returns an int. */ | |
536 | s = buffer; | |
537 | if (strerror_r(error, buffer, BUFSIZE)) { | |
538 | /* strerror_r() is only allowed to fail on ERANGE (because the buffer | |
539 | * is too short). We don't check the actual failure reason because | |
540 | * POSIX requires strerror_r() to return the error but old glibc | |
541 | * (before 2.13) returns -1 and sets errno. */ | |
5865a8af | 542 | snprintf(buffer, BUFSIZE, "Unknown error %d", error); |
c18ea70d | 543 | } |
5fcbed74 BP |
544 | #endif |
545 | ||
546 | errno = save_errno; | |
547 | ||
548 | return s; | |
c18ea70d AE |
549 | } |
550 | ||
55d5bb44 JP |
551 | /* Sets global "program_name" and "program_version" variables. Should |
552 | * be called at the beginning of main() with "argv[0]" as the argument | |
553 | * to 'argv0'. | |
554 | * | |
e385ef55 EJ |
555 | * 'version' should contain the version of the caller's program. If 'version' |
556 | * is the same as the VERSION #define, the caller is assumed to be part of Open | |
557 | * vSwitch. Otherwise, it is assumed to be an external program linking against | |
558 | * the Open vSwitch libraries. | |
559 | * | |
55d5bb44 JP |
560 | */ |
561 | void | |
fa54d373 | 562 | ovs_set_program_name(const char *argv0, const char *version) |
064af421 | 563 | { |
ed596d3a | 564 | char *basename; |
91e12f0d | 565 | #ifdef _WIN32 |
ed596d3a | 566 | size_t max_len = strlen(argv0) + 1; |
fda546bd | 567 | |
144ccc02 | 568 | SetErrorMode(GetErrorMode() | SEM_NOGPFAULTERRORBOX); |
54b32d6f AGS |
569 | #if _MSC_VER < 1900 |
570 | /* This function is deprecated from 1900 (Visual Studio 2015) */ | |
c9f80f7f | 571 | _set_output_format(_TWO_DIGIT_EXPONENT); |
54b32d6f | 572 | #endif |
144ccc02 | 573 | |
ed596d3a GS |
574 | basename = xmalloc(max_len); |
575 | _splitpath_s(argv0, NULL, 0, NULL, 0, basename, max_len, NULL, 0); | |
ed596d3a | 576 | #else |
064af421 | 577 | const char *slash = strrchr(argv0, '/'); |
91e12f0d | 578 | basename = xstrdup(slash ? slash + 1 : argv0); |
ed596d3a | 579 | #endif |
55d5bb44 | 580 | |
91e12f0d AA |
581 | assert_single_threaded(); |
582 | free(program_name); | |
7f2f24e3 SM |
583 | /* Remove libtool prefix, if it is there */ |
584 | if (strncmp(basename, "lt-", 3) == 0) { | |
585 | char *tmp_name = basename; | |
586 | basename = xstrdup(basename + 3); | |
587 | free(tmp_name); | |
588 | } | |
91e12f0d | 589 | program_name = basename; |
e385ef55 | 590 | |
7f2f24e3 | 591 | free(program_version); |
e385ef55 | 592 | if (!strcmp(version, VERSION)) { |
fa54d373 MC |
593 | program_version = xasprintf("%s (Open vSwitch) "VERSION"\n", |
594 | program_name); | |
e385ef55 EJ |
595 | } else { |
596 | program_version = xasprintf("%s %s\n" | |
fa54d373 MC |
597 | "Open vSwitch Library "VERSION"\n", |
598 | program_name, version); | |
e385ef55 | 599 | } |
55d5bb44 JP |
600 | } |
601 | ||
bc9fb3a9 BP |
602 | /* Returns the name of the currently running thread or process. */ |
603 | const char * | |
604 | get_subprogram_name(void) | |
605 | { | |
606 | const char *name = subprogram_name_get(); | |
607 | return name ? name : ""; | |
608 | } | |
609 | ||
40e7cf56 BP |
610 | /* Sets 'subprogram_name' as the name of the currently running thread or |
611 | * process. (This appears in log messages and may also be visible in system | |
612 | * process listings and debuggers.) */ | |
bc9fb3a9 | 613 | void |
40e7cf56 | 614 | set_subprogram_name(const char *subprogram_name) |
bc9fb3a9 | 615 | { |
40e7cf56 | 616 | char *pname = xstrdup(subprogram_name ? subprogram_name : program_name); |
d710edc4 BP |
617 | free(subprogram_name_set(pname)); |
618 | ||
8a8cd0ac | 619 | #if HAVE_GLIBC_PTHREAD_SETNAME_NP |
e584f6a8 | 620 | pthread_setname_np(pthread_self(), pname); |
8a8cd0ac | 621 | #elif HAVE_NETBSD_PTHREAD_SETNAME_NP |
e584f6a8 | 622 | pthread_setname_np(pthread_self(), "%s", pname); |
0f13e650 | 623 | #elif HAVE_PTHREAD_SET_NAME_NP |
e584f6a8 | 624 | pthread_set_name_np(pthread_self(), pname); |
0f13e650 | 625 | #endif |
bc9fb3a9 BP |
626 | } |
627 | ||
ff1d2c16 BB |
628 | unsigned int |
629 | get_page_size(void) | |
630 | { | |
631 | static unsigned int cached; | |
632 | ||
633 | if (!cached) { | |
634 | #ifndef _WIN32 | |
635 | long int value = sysconf(_SC_PAGESIZE); | |
636 | #else | |
637 | long int value; | |
638 | SYSTEM_INFO sysinfo; | |
639 | GetSystemInfo(&sysinfo); | |
640 | value = sysinfo.dwPageSize; | |
641 | #endif | |
642 | if (value >= 0) { | |
643 | cached = value; | |
644 | } | |
645 | } | |
646 | ||
647 | return cached; | |
648 | } | |
649 | ||
650 | /* Returns the time at which the system booted, as the number of milliseconds | |
651 | * since the epoch, or 0 if the time of boot cannot be determined. */ | |
652 | long long int | |
653 | get_boot_time(void) | |
654 | { | |
655 | static long long int cache_expiration = LLONG_MIN; | |
656 | static long long int boot_time; | |
657 | ||
658 | ovs_assert(LINUX); | |
659 | ||
660 | if (time_msec() >= cache_expiration) { | |
661 | static const char stat_file[] = "/proc/stat"; | |
662 | char line[128]; | |
663 | FILE *stream; | |
664 | ||
665 | cache_expiration = time_msec() + 5 * 1000; | |
666 | ||
667 | stream = fopen(stat_file, "r"); | |
668 | if (!stream) { | |
669 | VLOG_ERR_ONCE("%s: open failed (%s)", | |
670 | stat_file, ovs_strerror(errno)); | |
671 | return boot_time; | |
672 | } | |
673 | ||
674 | while (fgets(line, sizeof line, stream)) { | |
675 | long long int btime; | |
676 | if (ovs_scan(line, "btime %lld", &btime)) { | |
677 | boot_time = btime * 1000; | |
678 | goto done; | |
679 | } | |
680 | } | |
681 | VLOG_ERR_ONCE("%s: btime not found", stat_file); | |
682 | done: | |
683 | fclose(stream); | |
684 | } | |
685 | return boot_time; | |
686 | } | |
687 | ||
9551e80b IM |
688 | /* This is a wrapper for setting timeout in control utils. |
689 | * The value of OVS_CTL_TIMEOUT environment variable will be used by | |
690 | * default if 'secs' is not specified. */ | |
691 | void | |
692 | ctl_timeout_setup(unsigned int secs) | |
693 | { | |
694 | if (!secs) { | |
695 | char *env = getenv("OVS_CTL_TIMEOUT"); | |
696 | ||
697 | if (env && env[0]) { | |
698 | str_to_uint(env, 10, &secs); | |
699 | } | |
700 | } | |
701 | if (secs) { | |
702 | time_alarm(secs); | |
703 | } | |
704 | } | |
705 | ||
55d5bb44 JP |
706 | /* Returns a pointer to a string describing the program version. The |
707 | * caller must not modify or free the returned string. | |
b53055f4 | 708 | */ |
55d5bb44 | 709 | const char * |
b0248b2a | 710 | ovs_get_program_version(void) |
55d5bb44 JP |
711 | { |
712 | return program_version; | |
064af421 BP |
713 | } |
714 | ||
b0248b2a TG |
715 | /* Returns a pointer to a string describing the program name. The |
716 | * caller must not modify or free the returned string. | |
717 | */ | |
718 | const char * | |
719 | ovs_get_program_name(void) | |
720 | { | |
721 | return program_name; | |
722 | } | |
723 | ||
064af421 BP |
724 | /* Print the version information for the program. */ |
725 | void | |
55d5bb44 | 726 | ovs_print_version(uint8_t min_ofp, uint8_t max_ofp) |
064af421 | 727 | { |
55d5bb44 | 728 | printf("%s", program_version); |
064af421 BP |
729 | if (min_ofp || max_ofp) { |
730 | printf("OpenFlow versions %#x:%#x\n", min_ofp, max_ofp); | |
731 | } | |
732 | } | |
733 | ||
734 | /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per | |
735 | * line. Numeric offsets are also included, starting at 'ofs' for the first | |
736 | * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters | |
737 | * are also rendered alongside. */ | |
738 | void | |
739 | ovs_hex_dump(FILE *stream, const void *buf_, size_t size, | |
740 | uintptr_t ofs, bool ascii) | |
741 | { | |
172cc6c1 BP |
742 | const uint8_t *buf = buf_; |
743 | const size_t per_line = 16; /* Maximum bytes per line. */ | |
744 | ||
745 | while (size > 0) { | |
746 | size_t i; | |
747 | ||
748 | /* Number of bytes on this line. */ | |
749 | size_t start = ofs % per_line; | |
750 | size_t end = per_line; | |
751 | if (end - start > size) { | |
752 | end = start + size; | |
753 | } | |
754 | size_t n = end - start; | |
755 | ||
756 | /* Print line. */ | |
757 | fprintf(stream, "%08"PRIxMAX" ", | |
758 | (uintmax_t) ROUND_DOWN(ofs, per_line)); | |
759 | for (i = 0; i < start; i++) { | |
064af421 | 760 | fprintf(stream, " "); |
172cc6c1 BP |
761 | } |
762 | for (; i < end; i++) { | |
763 | fprintf(stream, "%c%02x", | |
764 | i == per_line / 2 ? '-' : ' ', buf[i - start]); | |
765 | } | |
766 | if (ascii) { | |
064af421 | 767 | fprintf(stream, " "); |
172cc6c1 BP |
768 | for (; i < per_line; i++) { |
769 | fprintf(stream, " "); | |
770 | } | |
771 | fprintf(stream, "|"); | |
772 | for (i = 0; i < start; i++) { | |
773 | fprintf(stream, " "); | |
774 | } | |
775 | for (; i < end; i++) { | |
776 | int c = buf[i - start]; | |
777 | putc(c >= 32 && c < 127 ? c : '.', stream); | |
778 | } | |
779 | for (; i < per_line; i++) { | |
780 | fprintf(stream, " "); | |
781 | } | |
782 | fprintf(stream, "|"); | |
064af421 | 783 | } |
172cc6c1 | 784 | fprintf(stream, "\n"); |
064af421 | 785 | |
172cc6c1 BP |
786 | ofs += n; |
787 | buf += n; | |
788 | size -= n; | |
064af421 BP |
789 | } |
790 | } | |
791 | ||
792 | bool | |
793 | str_to_int(const char *s, int base, int *i) | |
794 | { | |
795 | long long ll; | |
796 | bool ok = str_to_llong(s, base, &ll); | |
65dca9eb IM |
797 | |
798 | if (!ok || ll < INT_MIN || ll > INT_MAX) { | |
799 | *i = 0; | |
800 | return false; | |
801 | } | |
064af421 | 802 | *i = ll; |
65dca9eb | 803 | return true; |
064af421 BP |
804 | } |
805 | ||
806 | bool | |
807 | str_to_long(const char *s, int base, long *li) | |
808 | { | |
809 | long long ll; | |
810 | bool ok = str_to_llong(s, base, &ll); | |
65dca9eb IM |
811 | |
812 | if (!ok || ll < LONG_MIN || ll > LONG_MAX) { | |
813 | *li = 0; | |
814 | return false; | |
815 | } | |
064af421 | 816 | *li = ll; |
65dca9eb | 817 | return true; |
064af421 BP |
818 | } |
819 | ||
820 | bool | |
821 | str_to_llong(const char *s, int base, long long *x) | |
822 | { | |
064af421 | 823 | char *tail; |
1ab39058 LR |
824 | bool ok = str_to_llong_with_tail(s, &tail, base, x); |
825 | if (*tail != '\0') { | |
826 | *x = 0; | |
827 | return false; | |
828 | } | |
829 | return ok; | |
830 | } | |
831 | ||
832 | bool | |
833 | str_to_llong_with_tail(const char *s, char **tail, int base, long long *x) | |
834 | { | |
835 | int save_errno = errno; | |
064af421 | 836 | errno = 0; |
1ab39058 LR |
837 | *x = strtoll(s, tail, base); |
838 | if (errno == EINVAL || errno == ERANGE || *tail == s) { | |
064af421 BP |
839 | errno = save_errno; |
840 | *x = 0; | |
841 | return false; | |
842 | } else { | |
843 | errno = save_errno; | |
844 | return true; | |
845 | } | |
846 | } | |
847 | ||
4c21aa06 ZK |
848 | bool |
849 | str_to_uint(const char *s, int base, unsigned int *u) | |
850 | { | |
851 | long long ll; | |
852 | bool ok = str_to_llong(s, base, &ll); | |
853 | if (!ok || ll < 0 || ll > UINT_MAX) { | |
a42023ee AZ |
854 | *u = 0; |
855 | return false; | |
4c21aa06 | 856 | } else { |
a42023ee AZ |
857 | *u = ll; |
858 | return true; | |
4c21aa06 ZK |
859 | } |
860 | } | |
861 | ||
c24b3458 IM |
862 | bool |
863 | str_to_ullong(const char *s, int base, unsigned long long *x) | |
864 | { | |
865 | int save_errno = errno; | |
866 | char *tail; | |
867 | ||
868 | errno = 0; | |
869 | *x = strtoull(s, &tail, base); | |
870 | if (errno == EINVAL || errno == ERANGE || tail == s || *tail != '\0') { | |
871 | errno = save_errno; | |
872 | *x = 0; | |
873 | return false; | |
874 | } else { | |
875 | errno = save_errno; | |
876 | return true; | |
877 | } | |
878 | } | |
879 | ||
1ab39058 LR |
880 | bool |
881 | str_to_llong_range(const char *s, int base, long long *begin, | |
882 | long long *end) | |
883 | { | |
884 | char *tail; | |
885 | if (str_to_llong_with_tail(s, &tail, base, begin) | |
886 | && *tail == '-' | |
887 | && str_to_llong(tail + 1, base, end)) { | |
888 | return true; | |
889 | } | |
890 | *begin = 0; | |
891 | *end = 0; | |
892 | return false; | |
893 | } | |
894 | ||
f38b84ea BP |
895 | /* Converts floating-point string 's' into a double. If successful, stores |
896 | * the double in '*d' and returns true; on failure, stores 0 in '*d' and | |
897 | * returns false. | |
898 | * | |
899 | * Underflow (e.g. "1e-9999") is not considered an error, but overflow | |
900 | * (e.g. "1e9999)" is. */ | |
901 | bool | |
902 | str_to_double(const char *s, double *d) | |
903 | { | |
904 | int save_errno = errno; | |
905 | char *tail; | |
906 | errno = 0; | |
907 | *d = strtod(s, &tail); | |
908 | if (errno == EINVAL || (errno == ERANGE && *d != 0) | |
909 | || tail == s || *tail != '\0') { | |
910 | errno = save_errno; | |
911 | *d = 0; | |
912 | return false; | |
913 | } else { | |
914 | errno = save_errno; | |
915 | return true; | |
916 | } | |
917 | } | |
918 | ||
919 | /* Returns the value of 'c' as a hexadecimal digit. */ | |
920 | int | |
b4e1bfd7 BP |
921 | hexit_value(unsigned char c) |
922 | { | |
923 | static const signed char tbl[UCHAR_MAX + 1] = { | |
924 | #define TBL(x) \ | |
925 | ( x >= '0' && x <= '9' ? x - '0' \ | |
926 | : x >= 'a' && x <= 'f' ? x - 'a' + 0xa \ | |
927 | : x >= 'A' && x <= 'F' ? x - 'A' + 0xa \ | |
928 | : -1) | |
929 | #define TBL0(x) TBL(x), TBL((x) + 1), TBL((x) + 2), TBL((x) + 3) | |
930 | #define TBL1(x) TBL0(x), TBL0((x) + 4), TBL0((x) + 8), TBL0((x) + 12) | |
931 | #define TBL2(x) TBL1(x), TBL1((x) + 16), TBL1((x) + 32), TBL1((x) + 48) | |
932 | TBL2(0), TBL2(64), TBL2(128), TBL2(192) | |
933 | }; | |
934 | ||
935 | return tbl[c]; | |
f38b84ea | 936 | } |
29d4af60 | 937 | |
bf971267 | 938 | /* Returns the integer value of the 'n' hexadecimal digits starting at 's', or |
0429d959 BP |
939 | * UINTMAX_MAX if one of those "digits" is not really a hex digit. Sets '*ok' |
940 | * to true if the conversion succeeds or to false if a non-hex digit is | |
941 | * detected. */ | |
942 | uintmax_t | |
bf971267 BP |
943 | hexits_value(const char *s, size_t n, bool *ok) |
944 | { | |
0429d959 | 945 | uintmax_t value; |
bf971267 BP |
946 | size_t i; |
947 | ||
948 | value = 0; | |
949 | for (i = 0; i < n; i++) { | |
950 | int hexit = hexit_value(s[i]); | |
951 | if (hexit < 0) { | |
0429d959 BP |
952 | *ok = false; |
953 | return UINTMAX_MAX; | |
bf971267 BP |
954 | } |
955 | value = (value << 4) + hexit; | |
956 | } | |
0429d959 | 957 | *ok = true; |
bf971267 BP |
958 | return value; |
959 | } | |
960 | ||
e7ae59f9 JG |
961 | /* Parses the string in 's' as an integer in either hex or decimal format and |
962 | * puts the result right justified in the array 'valuep' that is 'field_width' | |
963 | * big. If the string is in hex format, the value may be arbitrarily large; | |
964 | * integers are limited to 64-bit values. (The rationale is that decimal is | |
965 | * likely to represent a number and 64 bits is a reasonable maximum whereas | |
966 | * hex could either be a number or a byte string.) | |
967 | * | |
968 | * On return 'tail' points to the first character in the string that was | |
969 | * not parsed as part of the value. ERANGE is returned if the value is too | |
970 | * large to fit in the given field. */ | |
971 | int | |
972 | parse_int_string(const char *s, uint8_t *valuep, int field_width, char **tail) | |
973 | { | |
974 | unsigned long long int integer; | |
975 | int i; | |
976 | ||
977 | if (!strncmp(s, "0x", 2) || !strncmp(s, "0X", 2)) { | |
978 | uint8_t *hexit_str; | |
979 | int len = 0; | |
980 | int val_idx; | |
981 | int err = 0; | |
982 | ||
983 | s += 2; | |
984 | hexit_str = xmalloc(field_width * 2); | |
985 | ||
986 | for (;;) { | |
987 | uint8_t hexit; | |
988 | bool ok; | |
989 | ||
990 | s += strspn(s, " \t\r\n"); | |
991 | hexit = hexits_value(s, 1, &ok); | |
992 | if (!ok) { | |
993 | *tail = CONST_CAST(char *, s); | |
994 | break; | |
995 | } | |
996 | ||
997 | if (hexit != 0 || len) { | |
998 | if (DIV_ROUND_UP(len + 1, 2) > field_width) { | |
999 | err = ERANGE; | |
1000 | goto free; | |
1001 | } | |
1002 | ||
1003 | hexit_str[len] = hexit; | |
1004 | len++; | |
1005 | } | |
1006 | s++; | |
1007 | } | |
1008 | ||
1009 | val_idx = field_width; | |
1010 | for (i = len - 1; i >= 0; i -= 2) { | |
1011 | val_idx--; | |
1012 | valuep[val_idx] = hexit_str[i]; | |
1013 | if (i > 0) { | |
1014 | valuep[val_idx] += hexit_str[i - 1] << 4; | |
1015 | } | |
1016 | } | |
1017 | ||
1018 | memset(valuep, 0, val_idx); | |
1019 | ||
1020 | free: | |
1021 | free(hexit_str); | |
a42023ee | 1022 | return err; |
e7ae59f9 JG |
1023 | } |
1024 | ||
1025 | errno = 0; | |
1026 | integer = strtoull(s, tail, 0); | |
46d3a76a BP |
1027 | if (errno || s == *tail) { |
1028 | return errno ? errno : EINVAL; | |
e7ae59f9 JG |
1029 | } |
1030 | ||
1031 | for (i = field_width - 1; i >= 0; i--) { | |
1032 | valuep[i] = integer; | |
1033 | integer >>= 8; | |
1034 | } | |
1035 | if (integer) { | |
1036 | return ERANGE; | |
1037 | } | |
1038 | ||
1039 | return 0; | |
1040 | } | |
1041 | ||
daf03c53 BP |
1042 | /* Returns the current working directory as a malloc()'d string, or a null |
1043 | * pointer if the current working directory cannot be determined. */ | |
1044 | char * | |
1045 | get_cwd(void) | |
1046 | { | |
1047 | long int path_max; | |
1048 | size_t size; | |
1049 | ||
1050 | /* Get maximum path length or at least a reasonable estimate. */ | |
661c32dc | 1051 | #ifndef _WIN32 |
daf03c53 | 1052 | path_max = pathconf(".", _PC_PATH_MAX); |
661c32dc GS |
1053 | #else |
1054 | path_max = MAX_PATH; | |
1055 | #endif | |
daf03c53 BP |
1056 | size = (path_max < 0 ? 1024 |
1057 | : path_max > 10240 ? 10240 | |
1058 | : path_max); | |
1059 | ||
1060 | /* Get current working directory. */ | |
1061 | for (;;) { | |
1062 | char *buf = xmalloc(size); | |
1063 | if (getcwd(buf, size)) { | |
1064 | return xrealloc(buf, strlen(buf) + 1); | |
1065 | } else { | |
1066 | int error = errno; | |
1067 | free(buf); | |
1068 | if (error != ERANGE) { | |
10a89ef0 | 1069 | VLOG_WARN("getcwd failed (%s)", ovs_strerror(error)); |
daf03c53 BP |
1070 | return NULL; |
1071 | } | |
1072 | size *= 2; | |
1073 | } | |
1074 | } | |
1075 | } | |
1076 | ||
e1aff6f9 BP |
1077 | static char * |
1078 | all_slashes_name(const char *s) | |
1079 | { | |
1080 | return xstrdup(s[0] == '/' && s[1] == '/' && s[2] != '/' ? "//" | |
1081 | : s[0] == '/' ? "/" | |
1082 | : "."); | |
1083 | } | |
1084 | ||
3c1150ce | 1085 | #ifndef _WIN32 |
29d4af60 BP |
1086 | /* Returns the directory name portion of 'file_name' as a malloc()'d string, |
1087 | * similar to the POSIX dirname() function but thread-safe. */ | |
1088 | char * | |
1089 | dir_name(const char *file_name) | |
1090 | { | |
1091 | size_t len = strlen(file_name); | |
1092 | while (len > 0 && file_name[len - 1] == '/') { | |
1093 | len--; | |
1094 | } | |
1095 | while (len > 0 && file_name[len - 1] != '/') { | |
1096 | len--; | |
1097 | } | |
1098 | while (len > 0 && file_name[len - 1] == '/') { | |
1099 | len--; | |
1100 | } | |
e1aff6f9 BP |
1101 | return len ? xmemdup0(file_name, len) : all_slashes_name(file_name); |
1102 | } | |
1103 | ||
1104 | /* Returns the file name portion of 'file_name' as a malloc()'d string, | |
1105 | * similar to the POSIX basename() function but thread-safe. */ | |
1106 | char * | |
1107 | base_name(const char *file_name) | |
1108 | { | |
1109 | size_t end, start; | |
1110 | ||
1111 | end = strlen(file_name); | |
1112 | while (end > 0 && file_name[end - 1] == '/') { | |
1113 | end--; | |
1114 | } | |
1115 | ||
1116 | if (!end) { | |
1117 | return all_slashes_name(file_name); | |
29d4af60 | 1118 | } |
e1aff6f9 BP |
1119 | |
1120 | start = end; | |
1121 | while (start > 0 && file_name[start - 1] != '/') { | |
1122 | start--; | |
1123 | } | |
1124 | ||
1125 | return xmemdup0(file_name + start, end - start); | |
29d4af60 | 1126 | } |
3c1150ce | 1127 | #endif /* _WIN32 */ |
18b9283b | 1128 | |
4d8f04b3 BP |
1129 | bool |
1130 | is_file_name_absolute(const char *fn) | |
1131 | { | |
1132 | #ifdef _WIN32 | |
1133 | /* Use platform specific API */ | |
1134 | return !PathIsRelative(fn); | |
1135 | #else | |
1136 | /* An absolute path begins with /. */ | |
1137 | return fn[0] == '/'; | |
1138 | #endif | |
1139 | } | |
1140 | ||
1141 | /* If 'file_name' is absolute, returns a copy of 'file_name'. Otherwise, | |
daf03c53 BP |
1142 | * returns an absolute path to 'file_name' considering it relative to 'dir', |
1143 | * which itself must be absolute. 'dir' may be null or the empty string, in | |
1144 | * which case the current working directory is used. | |
1145 | * | |
1146 | * Returns a null pointer if 'dir' is null and getcwd() fails. */ | |
1147 | char * | |
1148 | abs_file_name(const char *dir, const char *file_name) | |
1149 | { | |
4d8f04b3 BP |
1150 | /* If it's already absolute, return a copy. */ |
1151 | if (is_file_name_absolute(file_name)) { | |
ee8749ca | 1152 | return xstrdup(file_name); |
4d8f04b3 BP |
1153 | } |
1154 | ||
1155 | /* If a base dir was supplied, use it. We assume, without checking, that | |
1156 | * the base dir is absolute.*/ | |
1157 | if (dir && dir[0]) { | |
daf03c53 BP |
1158 | char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/"; |
1159 | return xasprintf("%s%s%s", dir, separator, file_name); | |
daf03c53 | 1160 | } |
4d8f04b3 BP |
1161 | |
1162 | #if _WIN32 | |
1163 | /* It's a little complicated to make an absolute path on Windows because a | |
1164 | * relative path might still specify a drive letter. The OS has a function | |
1165 | * to do the job for us, so use it. */ | |
1166 | char abs_path[MAX_PATH]; | |
1167 | DWORD n = GetFullPathName(file_name, sizeof abs_path, abs_path, NULL); | |
1168 | return n > 0 && n <= sizeof abs_path ? xmemdup0(abs_path, n) : NULL; | |
1169 | #else | |
1170 | /* Outside Windows, do the job ourselves. */ | |
1171 | char *cwd = get_cwd(); | |
1172 | if (!cwd) { | |
1173 | return NULL; | |
1174 | } | |
1175 | char *abs_name = xasprintf("%s/%s", cwd, file_name); | |
1176 | free(cwd); | |
1177 | return abs_name; | |
1178 | #endif | |
daf03c53 BP |
1179 | } |
1180 | ||
fee0c963 BP |
1181 | /* Like readlink(), but returns the link name as a null-terminated string in |
1182 | * allocated memory that the caller must eventually free (with free()). | |
1183 | * Returns NULL on error, in which case errno is set appropriately. */ | |
ac01d085 | 1184 | static char * |
fee0c963 BP |
1185 | xreadlink(const char *filename) |
1186 | { | |
f984769f AS |
1187 | #ifdef _WIN32 |
1188 | errno = ENOENT; | |
1189 | return NULL; | |
1190 | #else | |
fee0c963 BP |
1191 | size_t size; |
1192 | ||
1193 | for (size = 64; ; size *= 2) { | |
1194 | char *buf = xmalloc(size); | |
1195 | ssize_t retval = readlink(filename, buf, size); | |
1196 | int error = errno; | |
1197 | ||
1198 | if (retval >= 0 && retval < size) { | |
1199 | buf[retval] = '\0'; | |
1200 | return buf; | |
1201 | } | |
1202 | ||
1203 | free(buf); | |
1204 | if (retval < 0) { | |
1205 | errno = error; | |
1206 | return NULL; | |
1207 | } | |
1208 | } | |
f984769f | 1209 | #endif |
fee0c963 BP |
1210 | } |
1211 | ||
1212 | /* Returns a version of 'filename' with symlinks in the final component | |
1213 | * dereferenced. This differs from realpath() in that: | |
1214 | * | |
1215 | * - 'filename' need not exist. | |
1216 | * | |
1217 | * - If 'filename' does exist as a symlink, its referent need not exist. | |
1218 | * | |
1219 | * - Only symlinks in the final component of 'filename' are dereferenced. | |
1220 | * | |
e9f56e84 GS |
1221 | * For Windows platform, this function returns a string that has the same |
1222 | * value as the passed string. | |
1223 | * | |
fee0c963 BP |
1224 | * The caller must eventually free the returned string (with free()). */ |
1225 | char * | |
1226 | follow_symlinks(const char *filename) | |
1227 | { | |
e9f56e84 | 1228 | #ifndef _WIN32 |
fee0c963 BP |
1229 | struct stat s; |
1230 | char *fn; | |
1231 | int i; | |
1232 | ||
1233 | fn = xstrdup(filename); | |
1234 | for (i = 0; i < 10; i++) { | |
1235 | char *linkname; | |
1236 | char *next_fn; | |
1237 | ||
1238 | if (lstat(fn, &s) != 0 || !S_ISLNK(s.st_mode)) { | |
1239 | return fn; | |
1240 | } | |
1241 | ||
1242 | linkname = xreadlink(fn); | |
1243 | if (!linkname) { | |
10a89ef0 BP |
1244 | VLOG_WARN("%s: readlink failed (%s)", |
1245 | filename, ovs_strerror(errno)); | |
fee0c963 BP |
1246 | return fn; |
1247 | } | |
1248 | ||
1249 | if (linkname[0] == '/') { | |
1250 | /* Target of symlink is absolute so use it raw. */ | |
1251 | next_fn = linkname; | |
1252 | } else { | |
1253 | /* Target of symlink is relative so add to 'fn''s directory. */ | |
1254 | char *dir = dir_name(fn); | |
1255 | ||
1256 | if (!strcmp(dir, ".")) { | |
1257 | next_fn = linkname; | |
1258 | } else { | |
1259 | char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/"; | |
1260 | next_fn = xasprintf("%s%s%s", dir, separator, linkname); | |
1261 | free(linkname); | |
1262 | } | |
1263 | ||
1264 | free(dir); | |
1265 | } | |
1266 | ||
1267 | free(fn); | |
1268 | fn = next_fn; | |
1269 | } | |
1270 | ||
1271 | VLOG_WARN("%s: too many levels of symlinks", filename); | |
1272 | free(fn); | |
e9f56e84 | 1273 | #endif |
fee0c963 BP |
1274 | return xstrdup(filename); |
1275 | } | |
daf03c53 | 1276 | |
18b9283b | 1277 | /* Pass a value to this function if it is marked with |
d295e8e9 JP |
1278 | * __attribute__((warn_unused_result)) and you genuinely want to ignore |
1279 | * its return value. (Note that every scalar type can be implicitly | |
18b9283b | 1280 | * converted to bool.) */ |
c69ee87c | 1281 | void ignore(bool x OVS_UNUSED) { } |
44b4d050 BP |
1282 | |
1283 | /* Returns an appropriate delimiter for inserting just before the 0-based item | |
1284 | * 'index' in a list that has 'total' items in it. */ | |
1285 | const char * | |
1286 | english_list_delimiter(size_t index, size_t total) | |
1287 | { | |
1288 | return (index == 0 ? "" | |
1289 | : index < total - 1 ? ", " | |
1290 | : total > 2 ? ", and " | |
1291 | : " and "); | |
1292 | } | |
711e0157 | 1293 | |
0ee140fb | 1294 | /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */ |
25f45143 | 1295 | #if __GNUC__ >= 4 || _MSC_VER |
0ee140fb | 1296 | /* Defined inline in util.h. */ |
aad29cd1 | 1297 | #else |
8c947903 | 1298 | /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */ |
481da12c | 1299 | int |
d43d314e | 1300 | raw_ctz(uint64_t n) |
0ee140fb | 1301 | { |
d43d314e BP |
1302 | uint64_t k; |
1303 | int count = 63; | |
aad29cd1 BP |
1304 | |
1305 | #define CTZ_STEP(X) \ | |
0ee140fb BP |
1306 | k = n << (X); \ |
1307 | if (k) { \ | |
1308 | count -= X; \ | |
1309 | n = k; \ | |
1310 | } | |
d43d314e | 1311 | CTZ_STEP(32); |
0ee140fb BP |
1312 | CTZ_STEP(16); |
1313 | CTZ_STEP(8); | |
1314 | CTZ_STEP(4); | |
1315 | CTZ_STEP(2); | |
1316 | CTZ_STEP(1); | |
aad29cd1 BP |
1317 | #undef CTZ_STEP |
1318 | ||
0ee140fb | 1319 | return count; |
aad29cd1 | 1320 | } |
8c947903 JR |
1321 | |
1322 | /* Returns the number of leading 0-bits in 'n'. Undefined if 'n' == 0. */ | |
1323 | int | |
1324 | raw_clz64(uint64_t n) | |
1325 | { | |
1326 | uint64_t k; | |
1327 | int count = 63; | |
1328 | ||
1329 | #define CLZ_STEP(X) \ | |
1330 | k = n >> (X); \ | |
1331 | if (k) { \ | |
1332 | count -= X; \ | |
1333 | n = k; \ | |
1334 | } | |
1335 | CLZ_STEP(32); | |
1336 | CLZ_STEP(16); | |
1337 | CLZ_STEP(8); | |
1338 | CLZ_STEP(4); | |
1339 | CLZ_STEP(2); | |
1340 | CLZ_STEP(1); | |
1341 | #undef CLZ_STEP | |
1342 | ||
1343 | return count; | |
1344 | } | |
0ee140fb | 1345 | #endif |
75a75043 | 1346 | |
381657b3 | 1347 | #if NEED_COUNT_1BITS_8 |
a656cb77 BP |
1348 | #define INIT1(X) \ |
1349 | ((((X) & (1 << 0)) != 0) + \ | |
1350 | (((X) & (1 << 1)) != 0) + \ | |
1351 | (((X) & (1 << 2)) != 0) + \ | |
1352 | (((X) & (1 << 3)) != 0) + \ | |
1353 | (((X) & (1 << 4)) != 0) + \ | |
1354 | (((X) & (1 << 5)) != 0) + \ | |
1355 | (((X) & (1 << 6)) != 0) + \ | |
1356 | (((X) & (1 << 7)) != 0)) | |
1357 | #define INIT2(X) INIT1(X), INIT1((X) + 1) | |
1358 | #define INIT4(X) INIT2(X), INIT2((X) + 2) | |
1359 | #define INIT8(X) INIT4(X), INIT4((X) + 4) | |
1360 | #define INIT16(X) INIT8(X), INIT8((X) + 8) | |
1361 | #define INIT32(X) INIT16(X), INIT16((X) + 16) | |
1362 | #define INIT64(X) INIT32(X), INIT32((X) + 32) | |
1363 | ||
c3cc4d2d JR |
1364 | const uint8_t count_1bits_8[256] = { |
1365 | INIT64(0), INIT64(64), INIT64(128), INIT64(192) | |
1366 | }; | |
1367 | #endif | |
2a4ca27c | 1368 | |
df1a62cb | 1369 | /* Returns true if the 'n' bytes starting at 'p' are 'byte'. */ |
75a75043 | 1370 | bool |
df1a62cb | 1371 | is_all_byte(const void *p_, size_t n, uint8_t byte) |
75a75043 | 1372 | { |
53cb9c3e | 1373 | const uint8_t *p = p_; |
75a75043 BP |
1374 | size_t i; |
1375 | ||
1376 | for (i = 0; i < n; i++) { | |
df1a62cb | 1377 | if (p[i] != byte) { |
75a75043 BP |
1378 | return false; |
1379 | } | |
1380 | } | |
1381 | return true; | |
1382 | } | |
1383 | ||
df1a62cb | 1384 | /* Returns true if the 'n' bytes starting at 'p' are zeros. */ |
75a75043 | 1385 | bool |
df1a62cb | 1386 | is_all_zeros(const void *p, size_t n) |
75a75043 | 1387 | { |
df1a62cb BP |
1388 | return is_all_byte(p, n, 0); |
1389 | } | |
75a75043 | 1390 | |
df1a62cb BP |
1391 | /* Returns true if the 'n' bytes starting at 'p' are 0xff. */ |
1392 | bool | |
1393 | is_all_ones(const void *p, size_t n) | |
1394 | { | |
1395 | return is_all_byte(p, n, 0xff); | |
75a75043 BP |
1396 | } |
1397 | ||
ddc4f8e2 BP |
1398 | /* Copies 'n_bits' bits starting from bit 'src_ofs' in 'src' to the 'n_bits' |
1399 | * starting from bit 'dst_ofs' in 'dst'. 'src' is 'src_len' bytes long and | |
1400 | * 'dst' is 'dst_len' bytes long. | |
1401 | * | |
1402 | * If you consider all of 'src' to be a single unsigned integer in network byte | |
1403 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1404 | * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1405 | * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len - | |
1406 | * 2], and so on. Similarly for 'dst'. | |
1407 | * | |
1408 | * Required invariants: | |
1409 | * src_ofs + n_bits <= src_len * 8 | |
1410 | * dst_ofs + n_bits <= dst_len * 8 | |
1411 | * 'src' and 'dst' must not overlap. | |
1412 | */ | |
1413 | void | |
1414 | bitwise_copy(const void *src_, unsigned int src_len, unsigned int src_ofs, | |
1415 | void *dst_, unsigned int dst_len, unsigned int dst_ofs, | |
1416 | unsigned int n_bits) | |
1417 | { | |
1418 | const uint8_t *src = src_; | |
1419 | uint8_t *dst = dst_; | |
1420 | ||
1421 | src += src_len - (src_ofs / 8 + 1); | |
1422 | src_ofs %= 8; | |
1423 | ||
1424 | dst += dst_len - (dst_ofs / 8 + 1); | |
1425 | dst_ofs %= 8; | |
1426 | ||
1427 | if (src_ofs == 0 && dst_ofs == 0) { | |
1428 | unsigned int n_bytes = n_bits / 8; | |
1429 | if (n_bytes) { | |
1430 | dst -= n_bytes - 1; | |
1431 | src -= n_bytes - 1; | |
1432 | memcpy(dst, src, n_bytes); | |
1433 | ||
1434 | n_bits %= 8; | |
1435 | src--; | |
1436 | dst--; | |
1437 | } | |
1438 | if (n_bits) { | |
1439 | uint8_t mask = (1 << n_bits) - 1; | |
1440 | *dst = (*dst & ~mask) | (*src & mask); | |
1441 | } | |
1442 | } else { | |
1443 | while (n_bits > 0) { | |
1444 | unsigned int max_copy = 8 - MAX(src_ofs, dst_ofs); | |
1445 | unsigned int chunk = MIN(n_bits, max_copy); | |
1446 | uint8_t mask = ((1 << chunk) - 1) << dst_ofs; | |
1447 | ||
1448 | *dst &= ~mask; | |
1449 | *dst |= ((*src >> src_ofs) << dst_ofs) & mask; | |
1450 | ||
1451 | src_ofs += chunk; | |
1452 | if (src_ofs == 8) { | |
1453 | src--; | |
1454 | src_ofs = 0; | |
1455 | } | |
1456 | dst_ofs += chunk; | |
1457 | if (dst_ofs == 8) { | |
1458 | dst--; | |
1459 | dst_ofs = 0; | |
1460 | } | |
1461 | n_bits -= chunk; | |
1462 | } | |
1463 | } | |
1464 | } | |
1465 | ||
6cc7ea5e BP |
1466 | /* Zeros the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'. 'dst' is |
1467 | * 'dst_len' bytes long. | |
1468 | * | |
1469 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1470 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1471 | * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1472 | * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len - | |
1473 | * 2], and so on. | |
1474 | * | |
1475 | * Required invariant: | |
1476 | * dst_ofs + n_bits <= dst_len * 8 | |
1477 | */ | |
1478 | void | |
1479 | bitwise_zero(void *dst_, unsigned int dst_len, unsigned dst_ofs, | |
1480 | unsigned int n_bits) | |
1481 | { | |
1482 | uint8_t *dst = dst_; | |
1483 | ||
1484 | if (!n_bits) { | |
1485 | return; | |
1486 | } | |
1487 | ||
1488 | dst += dst_len - (dst_ofs / 8 + 1); | |
1489 | dst_ofs %= 8; | |
1490 | ||
1491 | if (dst_ofs) { | |
1492 | unsigned int chunk = MIN(n_bits, 8 - dst_ofs); | |
1493 | ||
1494 | *dst &= ~(((1 << chunk) - 1) << dst_ofs); | |
1495 | ||
1496 | n_bits -= chunk; | |
1497 | if (!n_bits) { | |
1498 | return; | |
1499 | } | |
1500 | ||
1501 | dst--; | |
1502 | } | |
1503 | ||
1504 | while (n_bits >= 8) { | |
1505 | *dst-- = 0; | |
1506 | n_bits -= 8; | |
1507 | } | |
1508 | ||
1509 | if (n_bits) { | |
1510 | *dst &= ~((1 << n_bits) - 1); | |
1511 | } | |
1512 | } | |
1513 | ||
c2dd4932 BP |
1514 | /* Sets to 1 all of the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'. |
1515 | * 'dst' is 'dst_len' bytes long. | |
1516 | * | |
1517 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1518 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1519 | * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1520 | * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len - | |
1521 | * 2], and so on. | |
1522 | * | |
1523 | * Required invariant: | |
1524 | * dst_ofs + n_bits <= dst_len * 8 | |
1525 | */ | |
1526 | void | |
1527 | bitwise_one(void *dst_, unsigned int dst_len, unsigned dst_ofs, | |
1528 | unsigned int n_bits) | |
1529 | { | |
1530 | uint8_t *dst = dst_; | |
1531 | ||
1532 | if (!n_bits) { | |
1533 | return; | |
1534 | } | |
1535 | ||
1536 | dst += dst_len - (dst_ofs / 8 + 1); | |
1537 | dst_ofs %= 8; | |
1538 | ||
1539 | if (dst_ofs) { | |
1540 | unsigned int chunk = MIN(n_bits, 8 - dst_ofs); | |
1541 | ||
1542 | *dst |= ((1 << chunk) - 1) << dst_ofs; | |
1543 | ||
1544 | n_bits -= chunk; | |
1545 | if (!n_bits) { | |
1546 | return; | |
1547 | } | |
1548 | ||
1549 | dst--; | |
1550 | } | |
1551 | ||
1552 | while (n_bits >= 8) { | |
1553 | *dst-- = 0xff; | |
1554 | n_bits -= 8; | |
1555 | } | |
1556 | ||
1557 | if (n_bits) { | |
1558 | *dst |= (1 << n_bits) - 1; | |
1559 | } | |
1560 | } | |
1561 | ||
79a010aa BP |
1562 | /* Scans the 'n_bits' bits starting from bit 'dst_ofs' in 'dst' for 1-bits. |
1563 | * Returns false if any 1-bits are found, otherwise true. 'dst' is 'dst_len' | |
1564 | * bytes long. | |
1565 | * | |
1566 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1567 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1568 | * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1569 | * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len - | |
1570 | * 2], and so on. | |
1571 | * | |
1572 | * Required invariant: | |
1573 | * dst_ofs + n_bits <= dst_len * 8 | |
1574 | */ | |
1575 | bool | |
1576 | bitwise_is_all_zeros(const void *p_, unsigned int len, unsigned int ofs, | |
1577 | unsigned int n_bits) | |
1578 | { | |
1579 | const uint8_t *p = p_; | |
1580 | ||
1581 | if (!n_bits) { | |
1582 | return true; | |
1583 | } | |
1584 | ||
1585 | p += len - (ofs / 8 + 1); | |
1586 | ofs %= 8; | |
1587 | ||
1588 | if (ofs) { | |
1589 | unsigned int chunk = MIN(n_bits, 8 - ofs); | |
1590 | ||
1591 | if (*p & (((1 << chunk) - 1) << ofs)) { | |
1592 | return false; | |
1593 | } | |
1594 | ||
1595 | n_bits -= chunk; | |
1596 | if (!n_bits) { | |
1597 | return true; | |
1598 | } | |
1599 | ||
1600 | p--; | |
1601 | } | |
1602 | ||
1603 | while (n_bits >= 8) { | |
1604 | if (*p) { | |
1605 | return false; | |
1606 | } | |
1607 | n_bits -= 8; | |
1608 | p--; | |
1609 | } | |
1610 | ||
1611 | if (n_bits && *p & ((1 << n_bits) - 1)) { | |
1612 | return false; | |
1613 | } | |
1614 | ||
1615 | return true; | |
1616 | } | |
1617 | ||
95a92d5a BP |
1618 | /* Scans the bits in 'p' that have bit offsets 'start' (inclusive) through |
1619 | * 'end' (exclusive) for the first bit with value 'target'. If one is found, | |
1620 | * returns its offset, otherwise 'end'. 'p' is 'len' bytes long. | |
099c06e3 BP |
1621 | * |
1622 | * If you consider all of 'p' to be a single unsigned integer in network byte | |
1623 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1624 | * with value 1 in p[len - 1], bit 1 is the bit with value 2, bit 2 is the bit | |
1625 | * with value 4, ..., bit 8 is the bit with value 1 in p[len - 2], and so on. | |
1626 | * | |
1627 | * Required invariant: | |
1628 | * start <= end | |
1629 | */ | |
1630 | unsigned int | |
95a92d5a | 1631 | bitwise_scan(const void *p, unsigned int len, bool target, unsigned int start, |
099c06e3 BP |
1632 | unsigned int end) |
1633 | { | |
099c06e3 BP |
1634 | unsigned int ofs; |
1635 | ||
1636 | for (ofs = start; ofs < end; ofs++) { | |
95a92d5a | 1637 | if (bitwise_get_bit(p, len, ofs) == target) { |
099c06e3 BP |
1638 | break; |
1639 | } | |
1640 | } | |
1641 | return ofs; | |
1642 | } | |
1643 | ||
95a92d5a BP |
1644 | /* Scans the bits in 'p' that have bit offsets 'start' (inclusive) through |
1645 | * 'end' (exclusive) for the first bit with value 'target', in reverse order. | |
1646 | * If one is found, returns its offset, otherwise 'end'. 'p' is 'len' bytes | |
1647 | * long. | |
1648 | * | |
1649 | * If you consider all of 'p' to be a single unsigned integer in network byte | |
1650 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1651 | * with value 1 in p[len - 1], bit 1 is the bit with value 2, bit 2 is the bit | |
1652 | * with value 4, ..., bit 8 is the bit with value 1 in p[len - 2], and so on. | |
1653 | * | |
1654 | * To scan an entire bit array in reverse order, specify start == len * 8 - 1 | |
1655 | * and end == -1, in which case the return value is nonnegative if successful | |
1656 | * and -1 if no 'target' match is found. | |
1657 | * | |
1658 | * Required invariant: | |
1659 | * start >= end | |
1660 | */ | |
1661 | int | |
1662 | bitwise_rscan(const void *p, unsigned int len, bool target, int start, int end) | |
1663 | { | |
76adea87 HZ |
1664 | const uint8_t *s = p; |
1665 | int start_byte = len - (start / 8 + 1); | |
1666 | int end_byte = len - (end / 8 + 1); | |
1667 | int ofs_byte; | |
95a92d5a | 1668 | int ofs; |
76adea87 | 1669 | uint8_t the_byte; |
95a92d5a | 1670 | |
76adea87 HZ |
1671 | /* Find the target in the start_byte from starting offset */ |
1672 | ofs_byte = start_byte; | |
1673 | the_byte = s[ofs_byte]; | |
1674 | for (ofs = start % 8; ofs >= 0; ofs--) { | |
1675 | if (((the_byte & (1u << ofs)) != 0) == target) { | |
95a92d5a BP |
1676 | break; |
1677 | } | |
1678 | } | |
76adea87 HZ |
1679 | if (ofs < 0) { |
1680 | /* Target not found in start byte, continue searching byte by byte */ | |
1681 | for (ofs_byte = start_byte + 1; ofs_byte <= end_byte; ofs_byte++) { | |
1682 | if ((target && s[ofs_byte]) | |
1683 | || (!target && (s[ofs_byte] != 0xff))) { | |
1684 | break; | |
1685 | } | |
1686 | } | |
1687 | if (ofs_byte > end_byte) { | |
1688 | return end; | |
1689 | } | |
1690 | the_byte = s[ofs_byte]; | |
1691 | /* Target is in the_byte, find it bit by bit */ | |
1692 | for (ofs = 7; ofs >= 0; ofs--) { | |
1693 | if (((the_byte & (1u << ofs)) != 0) == target) { | |
1694 | break; | |
1695 | } | |
1696 | } | |
1697 | } | |
1698 | int ret = (len - ofs_byte) * 8 - (8 - ofs); | |
1699 | if (ret < end) { | |
1700 | return end; | |
1701 | } | |
1702 | return ret; | |
95a92d5a | 1703 | } |
099c06e3 | 1704 | |
ddc4f8e2 BP |
1705 | /* Copies the 'n_bits' low-order bits of 'value' into the 'n_bits' bits |
1706 | * starting at bit 'dst_ofs' in 'dst', which is 'dst_len' bytes long. | |
1707 | * | |
1708 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1709 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1710 | * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1711 | * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len - | |
1712 | * 2], and so on. | |
1713 | * | |
1714 | * Required invariants: | |
1715 | * dst_ofs + n_bits <= dst_len * 8 | |
1716 | * n_bits <= 64 | |
1717 | */ | |
1718 | void | |
1719 | bitwise_put(uint64_t value, | |
1720 | void *dst, unsigned int dst_len, unsigned int dst_ofs, | |
1721 | unsigned int n_bits) | |
1722 | { | |
1723 | ovs_be64 n_value = htonll(value); | |
1724 | bitwise_copy(&n_value, sizeof n_value, 0, | |
1725 | dst, dst_len, dst_ofs, | |
1726 | n_bits); | |
1727 | } | |
1728 | ||
1729 | /* Returns the value of the 'n_bits' bits starting at bit 'src_ofs' in 'src', | |
1730 | * which is 'src_len' bytes long. | |
1731 | * | |
1732 | * If you consider all of 'src' to be a single unsigned integer in network byte | |
1733 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1734 | * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1735 | * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len - | |
1736 | * 2], and so on. | |
1737 | * | |
1738 | * Required invariants: | |
1739 | * src_ofs + n_bits <= src_len * 8 | |
1740 | * n_bits <= 64 | |
1741 | */ | |
1742 | uint64_t | |
1743 | bitwise_get(const void *src, unsigned int src_len, | |
1744 | unsigned int src_ofs, unsigned int n_bits) | |
1745 | { | |
1746 | ovs_be64 value = htonll(0); | |
1747 | ||
1748 | bitwise_copy(src, src_len, src_ofs, | |
1749 | &value, sizeof value, 0, | |
1750 | n_bits); | |
1751 | return ntohll(value); | |
1752 | } | |
95a92d5a BP |
1753 | |
1754 | /* Returns the value of the bit with offset 'ofs' in 'src', which is 'len' | |
1755 | * bytes long. | |
1756 | * | |
1757 | * If you consider all of 'src' to be a single unsigned integer in network byte | |
1758 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1759 | * with value 1 in src[len - 1], bit 1 is the bit with value 2, bit 2 is the | |
1760 | * bit with value 4, ..., bit 8 is the bit with value 1 in src[len - 2], and so | |
1761 | * on. | |
1762 | * | |
1763 | * Required invariants: | |
1764 | * ofs < len * 8 | |
1765 | */ | |
1766 | bool | |
1767 | bitwise_get_bit(const void *src_, unsigned int len, unsigned int ofs) | |
1768 | { | |
1769 | const uint8_t *src = src_; | |
1770 | ||
1771 | return (src[len - (ofs / 8 + 1)] & (1u << (ofs % 8))) != 0; | |
1772 | } | |
1773 | ||
1774 | /* Sets the bit with offset 'ofs' in 'dst', which is 'len' bytes long, to 0. | |
1775 | * | |
1776 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1777 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1778 | * with value 1 in dst[len - 1], bit 1 is the bit with value 2, bit 2 is the | |
1779 | * bit with value 4, ..., bit 8 is the bit with value 1 in dst[len - 2], and so | |
1780 | * on. | |
1781 | * | |
1782 | * Required invariants: | |
1783 | * ofs < len * 8 | |
1784 | */ | |
1785 | void | |
1786 | bitwise_put0(void *dst_, unsigned int len, unsigned int ofs) | |
1787 | { | |
1788 | uint8_t *dst = dst_; | |
1789 | ||
1790 | dst[len - (ofs / 8 + 1)] &= ~(1u << (ofs % 8)); | |
1791 | } | |
1792 | ||
1793 | /* Sets the bit with offset 'ofs' in 'dst', which is 'len' bytes long, to 1. | |
1794 | * | |
1795 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1796 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1797 | * with value 1 in dst[len - 1], bit 1 is the bit with value 2, bit 2 is the | |
1798 | * bit with value 4, ..., bit 8 is the bit with value 1 in dst[len - 2], and so | |
1799 | * on. | |
1800 | * | |
1801 | * Required invariants: | |
1802 | * ofs < len * 8 | |
1803 | */ | |
1804 | void | |
1805 | bitwise_put1(void *dst_, unsigned int len, unsigned int ofs) | |
1806 | { | |
1807 | uint8_t *dst = dst_; | |
1808 | ||
1809 | dst[len - (ofs / 8 + 1)] |= 1u << (ofs % 8); | |
1810 | } | |
1811 | ||
1812 | /* Sets the bit with offset 'ofs' in 'dst', which is 'len' bytes long, to 'b'. | |
1813 | * | |
1814 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1815 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1816 | * with value 1 in dst[len - 1], bit 1 is the bit with value 2, bit 2 is the | |
1817 | * bit with value 4, ..., bit 8 is the bit with value 1 in dst[len - 2], and so | |
1818 | * on. | |
1819 | * | |
1820 | * Required invariants: | |
1821 | * ofs < len * 8 | |
1822 | */ | |
1823 | void | |
1824 | bitwise_put_bit(void *dst, unsigned int len, unsigned int ofs, bool b) | |
1825 | { | |
1826 | if (b) { | |
1827 | bitwise_put1(dst, len, ofs); | |
1828 | } else { | |
1829 | bitwise_put0(dst, len, ofs); | |
1830 | } | |
1831 | } | |
1832 | ||
1833 | /* Flips the bit with offset 'ofs' in 'dst', which is 'len' bytes long. | |
1834 | * | |
1835 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1836 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1837 | * with value 1 in dst[len - 1], bit 1 is the bit with value 2, bit 2 is the | |
1838 | * bit with value 4, ..., bit 8 is the bit with value 1 in dst[len - 2], and so | |
1839 | * on. | |
1840 | * | |
1841 | * Required invariants: | |
1842 | * ofs < len * 8 | |
1843 | */ | |
1844 | void | |
1845 | bitwise_toggle_bit(void *dst_, unsigned int len, unsigned int ofs) | |
1846 | { | |
1847 | uint8_t *dst = dst_; | |
1848 | ||
1849 | dst[len - (ofs / 8 + 1)] ^= 1u << (ofs % 8); | |
1850 | } | |
ed2232fc BP |
1851 | \f |
1852 | /* ovs_scan */ | |
1853 | ||
1854 | struct scan_spec { | |
1855 | unsigned int width; | |
1856 | enum { | |
1857 | SCAN_DISCARD, | |
1858 | SCAN_CHAR, | |
1859 | SCAN_SHORT, | |
1860 | SCAN_INT, | |
1861 | SCAN_LONG, | |
1862 | SCAN_LLONG, | |
1863 | SCAN_INTMAX_T, | |
1864 | SCAN_PTRDIFF_T, | |
1865 | SCAN_SIZE_T | |
1866 | } type; | |
1867 | }; | |
1868 | ||
1869 | static const char * | |
1870 | skip_spaces(const char *s) | |
1871 | { | |
1872 | while (isspace((unsigned char) *s)) { | |
1873 | s++; | |
1874 | } | |
1875 | return s; | |
1876 | } | |
1877 | ||
1878 | static const char * | |
1879 | scan_int(const char *s, const struct scan_spec *spec, int base, va_list *args) | |
1880 | { | |
1881 | const char *start = s; | |
1882 | uintmax_t value; | |
1883 | bool negative; | |
1884 | int n_digits; | |
1885 | ||
1886 | negative = *s == '-'; | |
1887 | s += *s == '-' || *s == '+'; | |
1888 | ||
1889 | if ((!base || base == 16) && *s == '0' && (s[1] == 'x' || s[1] == 'X')) { | |
1890 | base = 16; | |
1891 | s += 2; | |
1892 | } else if (!base) { | |
1893 | base = *s == '0' ? 8 : 10; | |
1894 | } | |
1895 | ||
1896 | if (s - start >= spec->width) { | |
1897 | return NULL; | |
1898 | } | |
1899 | ||
1900 | value = 0; | |
1901 | n_digits = 0; | |
1902 | while (s - start < spec->width) { | |
1903 | int digit = hexit_value(*s); | |
1904 | ||
1905 | if (digit < 0 || digit >= base) { | |
1906 | break; | |
1907 | } | |
1908 | value = value * base + digit; | |
1909 | n_digits++; | |
1910 | s++; | |
1911 | } | |
1912 | if (!n_digits) { | |
1913 | return NULL; | |
1914 | } | |
1915 | ||
1916 | if (negative) { | |
1917 | value = -value; | |
1918 | } | |
1919 | ||
1920 | switch (spec->type) { | |
1921 | case SCAN_DISCARD: | |
1922 | break; | |
1923 | case SCAN_CHAR: | |
1924 | *va_arg(*args, char *) = value; | |
1925 | break; | |
1926 | case SCAN_SHORT: | |
1927 | *va_arg(*args, short int *) = value; | |
1928 | break; | |
1929 | case SCAN_INT: | |
1930 | *va_arg(*args, int *) = value; | |
1931 | break; | |
1932 | case SCAN_LONG: | |
1933 | *va_arg(*args, long int *) = value; | |
1934 | break; | |
1935 | case SCAN_LLONG: | |
1936 | *va_arg(*args, long long int *) = value; | |
1937 | break; | |
1938 | case SCAN_INTMAX_T: | |
1939 | *va_arg(*args, intmax_t *) = value; | |
1940 | break; | |
1941 | case SCAN_PTRDIFF_T: | |
1942 | *va_arg(*args, ptrdiff_t *) = value; | |
1943 | break; | |
1944 | case SCAN_SIZE_T: | |
1945 | *va_arg(*args, size_t *) = value; | |
1946 | break; | |
1947 | } | |
1948 | return s; | |
1949 | } | |
1950 | ||
1951 | static const char * | |
1952 | skip_digits(const char *s) | |
1953 | { | |
1954 | while (*s >= '0' && *s <= '9') { | |
1955 | s++; | |
1956 | } | |
1957 | return s; | |
1958 | } | |
1959 | ||
1960 | static const char * | |
1961 | scan_float(const char *s, const struct scan_spec *spec, va_list *args) | |
1962 | { | |
1963 | const char *start = s; | |
1964 | long double value; | |
1965 | char *tail; | |
1966 | char *copy; | |
1967 | bool ok; | |
1968 | ||
1969 | s += *s == '+' || *s == '-'; | |
1970 | s = skip_digits(s); | |
1971 | if (*s == '.') { | |
1972 | s = skip_digits(s + 1); | |
1973 | } | |
1974 | if (*s == 'e' || *s == 'E') { | |
1975 | s++; | |
1976 | s += *s == '+' || *s == '-'; | |
1977 | s = skip_digits(s); | |
1978 | } | |
1979 | ||
1980 | if (s - start > spec->width) { | |
1981 | s = start + spec->width; | |
1982 | } | |
1983 | ||
1984 | copy = xmemdup0(start, s - start); | |
1985 | value = strtold(copy, &tail); | |
1986 | ok = *tail == '\0'; | |
1987 | free(copy); | |
1988 | if (!ok) { | |
1989 | return NULL; | |
1990 | } | |
1991 | ||
1992 | switch (spec->type) { | |
1993 | case SCAN_DISCARD: | |
1994 | break; | |
1995 | case SCAN_INT: | |
1996 | *va_arg(*args, float *) = value; | |
1997 | break; | |
1998 | case SCAN_LONG: | |
1999 | *va_arg(*args, double *) = value; | |
2000 | break; | |
2001 | case SCAN_LLONG: | |
2002 | *va_arg(*args, long double *) = value; | |
2003 | break; | |
2004 | ||
2005 | case SCAN_CHAR: | |
2006 | case SCAN_SHORT: | |
2007 | case SCAN_INTMAX_T: | |
2008 | case SCAN_PTRDIFF_T: | |
2009 | case SCAN_SIZE_T: | |
428b2edd | 2010 | OVS_NOT_REACHED(); |
ed2232fc BP |
2011 | } |
2012 | return s; | |
2013 | } | |
2014 | ||
2015 | static void | |
2016 | scan_output_string(const struct scan_spec *spec, | |
2017 | const char *s, size_t n, | |
2018 | va_list *args) | |
2019 | { | |
2020 | if (spec->type != SCAN_DISCARD) { | |
2021 | char *out = va_arg(*args, char *); | |
2022 | memcpy(out, s, n); | |
2023 | out[n] = '\0'; | |
2024 | } | |
2025 | } | |
2026 | ||
2027 | static const char * | |
2028 | scan_string(const char *s, const struct scan_spec *spec, va_list *args) | |
2029 | { | |
2030 | size_t n; | |
2031 | ||
2032 | for (n = 0; n < spec->width; n++) { | |
2033 | if (!s[n] || isspace((unsigned char) s[n])) { | |
2034 | break; | |
2035 | } | |
2036 | } | |
2037 | if (!n) { | |
2038 | return NULL; | |
2039 | } | |
2040 | ||
2041 | scan_output_string(spec, s, n, args); | |
2042 | return s + n; | |
2043 | } | |
2044 | ||
2045 | static const char * | |
2046 | parse_scanset(const char *p_, unsigned long *set, bool *complemented) | |
2047 | { | |
2048 | const uint8_t *p = (const uint8_t *) p_; | |
2049 | ||
2050 | *complemented = *p == '^'; | |
2051 | p += *complemented; | |
2052 | ||
2053 | if (*p == ']') { | |
2054 | bitmap_set1(set, ']'); | |
2055 | p++; | |
2056 | } | |
2057 | ||
2058 | while (*p && *p != ']') { | |
2059 | if (p[1] == '-' && p[2] != ']' && p[2] > *p) { | |
2060 | bitmap_set_multiple(set, *p, p[2] - *p + 1, true); | |
2061 | p += 3; | |
2062 | } else { | |
2063 | bitmap_set1(set, *p++); | |
2064 | } | |
2065 | } | |
2066 | if (*p == ']') { | |
2067 | p++; | |
2068 | } | |
2069 | return (const char *) p; | |
2070 | } | |
2071 | ||
2072 | static const char * | |
2073 | scan_set(const char *s, const struct scan_spec *spec, const char **pp, | |
2074 | va_list *args) | |
2075 | { | |
2076 | unsigned long set[BITMAP_N_LONGS(UCHAR_MAX + 1)]; | |
2077 | bool complemented; | |
2078 | unsigned int n; | |
2079 | ||
2080 | /* Parse the scan set. */ | |
2081 | memset(set, 0, sizeof set); | |
2082 | *pp = parse_scanset(*pp, set, &complemented); | |
2083 | ||
2084 | /* Parse the data. */ | |
2085 | n = 0; | |
2086 | while (s[n] | |
2087 | && bitmap_is_set(set, (unsigned char) s[n]) == !complemented | |
2088 | && n < spec->width) { | |
2089 | n++; | |
2090 | } | |
2091 | if (!n) { | |
2092 | return NULL; | |
2093 | } | |
2094 | scan_output_string(spec, s, n, args); | |
2095 | return s + n; | |
2096 | } | |
2097 | ||
2098 | static const char * | |
2099 | scan_chars(const char *s, const struct scan_spec *spec, va_list *args) | |
2100 | { | |
31499678 | 2101 | unsigned int n = spec->width == UINT_MAX ? 1 : spec->width; |
ed2232fc BP |
2102 | |
2103 | if (strlen(s) < n) { | |
2104 | return NULL; | |
2105 | } | |
2106 | if (spec->type != SCAN_DISCARD) { | |
2107 | memcpy(va_arg(*args, char *), s, n); | |
2108 | } | |
2109 | return s + n; | |
2110 | } | |
2111 | ||
f071cbba PS |
2112 | static bool |
2113 | ovs_scan__(const char *s, int *n, const char *format, va_list *args) | |
ed2232fc BP |
2114 | { |
2115 | const char *const start = s; | |
2116 | bool ok = false; | |
2117 | const char *p; | |
ed2232fc | 2118 | |
037821cf | 2119 | p = format; |
ed2232fc BP |
2120 | while (*p != '\0') { |
2121 | struct scan_spec spec; | |
2122 | unsigned char c = *p++; | |
2123 | bool discard; | |
2124 | ||
2125 | if (isspace(c)) { | |
2126 | s = skip_spaces(s); | |
2127 | continue; | |
2128 | } else if (c != '%') { | |
2129 | if (*s != c) { | |
2130 | goto exit; | |
2131 | } | |
2132 | s++; | |
2133 | continue; | |
2134 | } else if (*p == '%') { | |
2135 | if (*s++ != '%') { | |
2136 | goto exit; | |
2137 | } | |
2138 | p++; | |
2139 | continue; | |
2140 | } | |
2141 | ||
2142 | /* Parse '*' flag. */ | |
2143 | discard = *p == '*'; | |
2144 | p += discard; | |
2145 | ||
2146 | /* Parse field width. */ | |
2147 | spec.width = 0; | |
2148 | while (*p >= '0' && *p <= '9') { | |
2149 | spec.width = spec.width * 10 + (*p++ - '0'); | |
2150 | } | |
2151 | if (spec.width == 0) { | |
2152 | spec.width = UINT_MAX; | |
2153 | } | |
2154 | ||
2155 | /* Parse type modifier. */ | |
2156 | switch (*p) { | |
2157 | case 'h': | |
2158 | if (p[1] == 'h') { | |
2159 | spec.type = SCAN_CHAR; | |
2160 | p += 2; | |
2161 | } else { | |
2162 | spec.type = SCAN_SHORT; | |
2163 | p++; | |
2164 | } | |
2165 | break; | |
2166 | ||
2167 | case 'j': | |
2168 | spec.type = SCAN_INTMAX_T; | |
2169 | p++; | |
2170 | break; | |
2171 | ||
2172 | case 'l': | |
2173 | if (p[1] == 'l') { | |
2174 | spec.type = SCAN_LLONG; | |
2175 | p += 2; | |
2176 | } else { | |
2177 | spec.type = SCAN_LONG; | |
2178 | p++; | |
2179 | } | |
2180 | break; | |
2181 | ||
2182 | case 'L': | |
2183 | case 'q': | |
2184 | spec.type = SCAN_LLONG; | |
2185 | p++; | |
2186 | break; | |
2187 | ||
2188 | case 't': | |
2189 | spec.type = SCAN_PTRDIFF_T; | |
2190 | p++; | |
2191 | break; | |
2192 | ||
2193 | case 'z': | |
2194 | spec.type = SCAN_SIZE_T; | |
2195 | p++; | |
2196 | break; | |
2197 | ||
2198 | default: | |
2199 | spec.type = SCAN_INT; | |
2200 | break; | |
2201 | } | |
2202 | ||
2203 | if (discard) { | |
2204 | spec.type = SCAN_DISCARD; | |
2205 | } | |
2206 | ||
2207 | c = *p++; | |
2208 | if (c != 'c' && c != 'n' && c != '[') { | |
2209 | s = skip_spaces(s); | |
2210 | } | |
2211 | switch (c) { | |
2212 | case 'd': | |
f071cbba | 2213 | s = scan_int(s, &spec, 10, args); |
ed2232fc BP |
2214 | break; |
2215 | ||
2216 | case 'i': | |
f071cbba | 2217 | s = scan_int(s, &spec, 0, args); |
ed2232fc BP |
2218 | break; |
2219 | ||
2220 | case 'o': | |
f071cbba | 2221 | s = scan_int(s, &spec, 8, args); |
ed2232fc BP |
2222 | break; |
2223 | ||
2224 | case 'u': | |
f071cbba | 2225 | s = scan_int(s, &spec, 10, args); |
ed2232fc BP |
2226 | break; |
2227 | ||
2228 | case 'x': | |
2229 | case 'X': | |
f071cbba | 2230 | s = scan_int(s, &spec, 16, args); |
ed2232fc BP |
2231 | break; |
2232 | ||
2233 | case 'e': | |
2234 | case 'f': | |
2235 | case 'g': | |
2236 | case 'E': | |
2237 | case 'G': | |
f071cbba | 2238 | s = scan_float(s, &spec, args); |
ed2232fc BP |
2239 | break; |
2240 | ||
2241 | case 's': | |
f071cbba | 2242 | s = scan_string(s, &spec, args); |
ed2232fc BP |
2243 | break; |
2244 | ||
2245 | case '[': | |
f071cbba | 2246 | s = scan_set(s, &spec, &p, args); |
ed2232fc BP |
2247 | break; |
2248 | ||
2249 | case 'c': | |
f071cbba | 2250 | s = scan_chars(s, &spec, args); |
ed2232fc BP |
2251 | break; |
2252 | ||
2253 | case 'n': | |
2254 | if (spec.type != SCAN_DISCARD) { | |
f071cbba | 2255 | *va_arg(*args, int *) = s - start; |
ed2232fc BP |
2256 | } |
2257 | break; | |
2258 | } | |
2259 | ||
2260 | if (!s) { | |
2261 | goto exit; | |
2262 | } | |
2263 | } | |
f071cbba PS |
2264 | if (n) { |
2265 | *n = s - start; | |
2266 | } | |
ed2232fc | 2267 | |
f071cbba | 2268 | ok = true; |
ed2232fc | 2269 | exit: |
ed2232fc BP |
2270 | return ok; |
2271 | } | |
2272 | ||
f071cbba PS |
2273 | /* This is an implementation of the standard sscanf() function, with the |
2274 | * following exceptions: | |
2275 | * | |
2276 | * - It returns true if the entire format was successfully scanned and | |
2277 | * converted, false if any conversion failed. | |
2278 | * | |
2279 | * - The standard doesn't define sscanf() behavior when an out-of-range value | |
2280 | * is scanned, e.g. if a "%"PRIi8 conversion scans "-1" or "0x1ff". Some | |
2281 | * implementations consider this an error and stop scanning. This | |
2282 | * implementation never considers an out-of-range value an error; instead, | |
2283 | * it stores the least-significant bits of the converted value in the | |
2284 | * destination, e.g. the value 255 for both examples earlier. | |
2285 | * | |
2286 | * - Only single-byte characters are supported, that is, the 'l' modifier | |
2287 | * on %s, %[, and %c is not supported. The GNU extension 'a' modifier is | |
2288 | * also not supported. | |
2289 | * | |
2290 | * - %p is not supported. | |
2291 | */ | |
2292 | bool | |
2293 | ovs_scan(const char *s, const char *format, ...) | |
2294 | { | |
2295 | va_list args; | |
2296 | bool res; | |
2297 | ||
2298 | va_start(args, format); | |
2299 | res = ovs_scan__(s, NULL, format, &args); | |
2300 | va_end(args); | |
2301 | return res; | |
2302 | } | |
2303 | ||
2304 | /* | |
938a73da BP |
2305 | * This function is similar to ovs_scan(), with an extra parameter `n` added to |
2306 | * return the number of scanned characters. | |
f071cbba PS |
2307 | */ |
2308 | bool | |
2309 | ovs_scan_len(const char *s, int *n, const char *format, ...) | |
2310 | { | |
2311 | va_list args; | |
2312 | bool success; | |
2313 | int n1; | |
2314 | ||
2315 | va_start(args, format); | |
2316 | success = ovs_scan__(s + *n, &n1, format, &args); | |
2317 | va_end(args); | |
2318 | if (success) { | |
2319 | *n = *n + n1; | |
2320 | } | |
2321 | return success; | |
2322 | } | |
2323 | ||
5fd2f418 | 2324 | void |
275eebb9 PS |
2325 | xsleep(unsigned int seconds) |
2326 | { | |
275eebb9 | 2327 | ovsrcu_quiesce_start(); |
5fd2f418 GS |
2328 | #ifdef _WIN32 |
2329 | Sleep(seconds * 1000); | |
2330 | #else | |
2331 | sleep(seconds); | |
2332 | #endif | |
275eebb9 | 2333 | ovsrcu_quiesce_end(); |
275eebb9 PS |
2334 | } |
2335 | ||
ca3cc1aa BB |
2336 | /* High resolution sleep. */ |
2337 | void | |
2338 | xnanosleep(uint64_t nanoseconds) | |
2339 | { | |
2340 | ovsrcu_quiesce_start(); | |
2341 | #ifndef _WIN32 | |
2342 | int retval; | |
2343 | struct timespec ts_sleep; | |
2344 | nsec_to_timespec(nanoseconds, &ts_sleep); | |
2345 | ||
2346 | int error = 0; | |
2347 | do { | |
2348 | retval = nanosleep(&ts_sleep, NULL); | |
2349 | error = retval < 0 ? errno : 0; | |
2350 | } while (error == EINTR); | |
2351 | #else | |
2352 | HANDLE timer = CreateWaitableTimer(NULL, FALSE, NULL); | |
2353 | if (timer) { | |
2354 | LARGE_INTEGER duetime; | |
2355 | duetime.QuadPart = -nanoseconds; | |
2356 | if (SetWaitableTimer(timer, &duetime, 0, NULL, NULL, FALSE)) { | |
2357 | WaitForSingleObject(timer, INFINITE); | |
2358 | } else { | |
2359 | VLOG_ERR_ONCE("SetWaitableTimer Failed (%s)", | |
2360 | ovs_lasterror_to_string()); | |
2361 | } | |
2362 | CloseHandle(timer); | |
2363 | } else { | |
2364 | VLOG_ERR_ONCE("CreateWaitableTimer Failed (%s)", | |
2365 | ovs_lasterror_to_string()); | |
2366 | } | |
2367 | #endif | |
2368 | ovsrcu_quiesce_end(); | |
2369 | } | |
2370 | ||
20174b74 QM |
2371 | /* Determine whether standard output is a tty or not. This is useful to decide |
2372 | * whether to use color output or not when --color option for utilities is set | |
2373 | * to `auto`. | |
2374 | */ | |
2375 | bool | |
2376 | is_stdout_a_tty(void) | |
2377 | { | |
2378 | char const *t = getenv("TERM"); | |
2379 | return (isatty(STDOUT_FILENO) && t && strcmp(t, "dumb") != 0); | |
2380 | } | |
2381 | ||
06f14c92 GS |
2382 | #ifdef _WIN32 |
2383 | \f | |
06f14c92 | 2384 | char * |
315ea327 | 2385 | ovs_format_message(int error) |
06f14c92 | 2386 | { |
315ea327 GS |
2387 | enum { BUFSIZE = sizeof strerror_buffer_get()->s }; |
2388 | char *buffer = strerror_buffer_get()->s; | |
2389 | ||
11b7f938 YT |
2390 | if (error == 0) { |
2391 | /* See ovs_strerror */ | |
2392 | return "Success"; | |
2393 | } | |
2394 | ||
315ea327 GS |
2395 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
2396 | NULL, error, 0, buffer, BUFSIZE, NULL); | |
06f14c92 GS |
2397 | return buffer; |
2398 | } | |
315ea327 GS |
2399 | |
2400 | /* Returns a null-terminated string that explains the last error. | |
2401 | * Use this function to get the error string for WINAPI calls. */ | |
2402 | char * | |
2403 | ovs_lasterror_to_string(void) | |
2404 | { | |
2405 | return ovs_format_message(GetLastError()); | |
2406 | } | |
daa04db8 GS |
2407 | |
2408 | int | |
2409 | ftruncate(int fd, off_t length) | |
2410 | { | |
2411 | int error; | |
2412 | ||
2413 | error = _chsize_s(fd, length); | |
2414 | if (error) { | |
2415 | return -1; | |
2416 | } | |
2417 | return 0; | |
2418 | } | |
64559798 GS |
2419 | |
2420 | OVS_CONSTRUCTOR(winsock_start) { | |
2421 | WSADATA wsaData; | |
2422 | int error; | |
2423 | ||
2424 | error = WSAStartup(MAKEWORD(2, 2), &wsaData); | |
2425 | if (error != 0) { | |
2426 | VLOG_FATAL("WSAStartup failed: %s", sock_strerror(sock_errno())); | |
2427 | } | |
2428 | } | |
06f14c92 | 2429 | #endif |