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