/src/hostap/src/utils/os_unix.c
Line | Count | Source |
1 | | /* |
2 | | * OS specific functions for UNIX/POSIX systems |
3 | | * Copyright (c) 2005-2019, Jouni Malinen <j@w1.fi> |
4 | | * |
5 | | * This software may be distributed under the terms of the BSD license. |
6 | | * See README for more details. |
7 | | */ |
8 | | |
9 | | #include "includes.h" |
10 | | |
11 | | #include <time.h> |
12 | | #include <sys/wait.h> |
13 | | |
14 | | #ifdef ANDROID |
15 | | #include <sys/capability.h> |
16 | | #include <sys/prctl.h> |
17 | | #include <private/android_filesystem_config.h> |
18 | | #endif /* ANDROID */ |
19 | | |
20 | | #ifdef __MACH__ |
21 | | #include <CoreServices/CoreServices.h> |
22 | | #include <mach/mach.h> |
23 | | #include <mach/mach_time.h> |
24 | | #endif /* __MACH__ */ |
25 | | |
26 | | #include "os.h" |
27 | | #include "common.h" |
28 | | |
29 | | #ifdef WPA_TRACE |
30 | | |
31 | | #include "wpa_debug.h" |
32 | | #include "trace.h" |
33 | | #include "list.h" |
34 | | |
35 | | static struct dl_list alloc_list = DL_LIST_HEAD_INIT(alloc_list); |
36 | | |
37 | | #define ALLOC_MAGIC 0xa84ef1b2 |
38 | | #define FREED_MAGIC 0x67fd487a |
39 | | |
40 | | struct os_alloc_trace { |
41 | | unsigned int magic; |
42 | | struct dl_list list __attribute__((aligned(16))); |
43 | | size_t len; |
44 | | WPA_TRACE_INFO |
45 | | } __attribute__((aligned(16))); |
46 | | |
47 | | #endif /* WPA_TRACE */ |
48 | | |
49 | | |
50 | | void os_sleep(os_time_t sec, os_time_t usec) |
51 | 0 | { |
52 | 0 | #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L) |
53 | 0 | const struct timespec req = { sec, usec * 1000 }; |
54 | |
|
55 | 0 | nanosleep(&req, NULL); |
56 | | #else |
57 | | if (sec) |
58 | | sleep(sec); |
59 | | if (usec) |
60 | | usleep(usec); |
61 | | #endif |
62 | 0 | } |
63 | | |
64 | | |
65 | | int os_get_time(struct os_time *t) |
66 | 0 | { |
67 | 0 | int res; |
68 | 0 | struct timeval tv; |
69 | 0 | res = gettimeofday(&tv, NULL); |
70 | 0 | t->sec = tv.tv_sec; |
71 | 0 | t->usec = tv.tv_usec; |
72 | 0 | return res; |
73 | 0 | } |
74 | | |
75 | | |
76 | | int os_get_reltime(struct os_reltime *t) |
77 | 0 | { |
78 | 0 | #ifndef __MACH__ |
79 | 0 | #if defined(CLOCK_BOOTTIME) |
80 | 0 | static clockid_t clock_id = CLOCK_BOOTTIME; |
81 | | #elif defined(CLOCK_MONOTONIC) |
82 | | static clockid_t clock_id = CLOCK_MONOTONIC; |
83 | | #else |
84 | | static clockid_t clock_id = CLOCK_REALTIME; |
85 | | #endif |
86 | 0 | struct timespec ts; |
87 | 0 | int res; |
88 | |
|
89 | 0 | if (TEST_FAIL()) |
90 | 0 | return -1; |
91 | | |
92 | 0 | while (1) { |
93 | 0 | res = clock_gettime(clock_id, &ts); |
94 | 0 | if (res == 0) { |
95 | 0 | t->sec = ts.tv_sec; |
96 | 0 | t->usec = ts.tv_nsec / 1000; |
97 | 0 | return 0; |
98 | 0 | } |
99 | | |
100 | | /* Try to fall back to alternative clockid_t values, if any */ |
101 | | |
102 | 0 | #if defined(CLOCK_BOOTTIME) && defined(CLOCK_MONOTONIC) |
103 | | /* Some systems (e.g., FreeBSD 15.0) define both CLOCK_BOOTTIME |
104 | | * and CLOCK_MONOTONIC to have the same value, so handle that |
105 | | * as a special case. */ |
106 | 0 | if (CLOCK_BOOTTIME == CLOCK_MONOTONIC && |
107 | 0 | clock_id == CLOCK_BOOTTIME) { |
108 | 0 | clock_id = CLOCK_REALTIME; |
109 | 0 | continue; |
110 | 0 | } |
111 | | |
112 | 0 | if (clock_id == CLOCK_BOOTTIME) { |
113 | 0 | clock_id = CLOCK_MONOTONIC; |
114 | 0 | continue; |
115 | 0 | } |
116 | | #elif CLOCK_BOOTTIME |
117 | | /* CLOCK_MONOTONIC is not defined. Unknown whether such cases |
118 | | * exists, but it is safe to fall back to CLOCK_REALTIME. |
119 | | */ |
120 | | if (clock_id == CLOCK_BOOTTIME) { |
121 | | clock_id = CLOCK_REALTIME; |
122 | | continue; |
123 | | } |
124 | | #endif |
125 | | |
126 | 0 | #ifdef CLOCK_MONOTONIC |
127 | 0 | if (clock_id == CLOCK_MONOTONIC) { |
128 | 0 | clock_id = CLOCK_REALTIME; |
129 | 0 | continue; |
130 | 0 | } |
131 | 0 | #endif |
132 | | |
133 | | /* No additional clockid_t values to fall back to */ |
134 | 0 | return -1; |
135 | 0 | } |
136 | | #else /* __MACH__ */ |
137 | | uint64_t abstime, nano; |
138 | | static mach_timebase_info_data_t info = { 0, 0 }; |
139 | | |
140 | | if (!info.denom) { |
141 | | if (mach_timebase_info(&info) != KERN_SUCCESS) |
142 | | return -1; |
143 | | } |
144 | | |
145 | | abstime = mach_absolute_time(); |
146 | | nano = (abstime * info.numer) / info.denom; |
147 | | |
148 | | t->sec = nano / NSEC_PER_SEC; |
149 | | t->usec = (nano - (((uint64_t) t->sec) * NSEC_PER_SEC)) / NSEC_PER_USEC; |
150 | | |
151 | | return 0; |
152 | | #endif /* __MACH__ */ |
153 | 0 | } |
154 | | |
155 | | |
156 | | int os_mktime(int year, int month, int day, int hour, int min, int sec, |
157 | | os_time_t *t) |
158 | 0 | { |
159 | 0 | struct tm tm, *tm1; |
160 | 0 | time_t t_local, t1, t2; |
161 | 0 | os_time_t tz_offset; |
162 | |
|
163 | 0 | if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 || |
164 | 0 | hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 || |
165 | 0 | sec > 60) |
166 | 0 | return -1; |
167 | | |
168 | 0 | memset(&tm, 0, sizeof(tm)); |
169 | 0 | tm.tm_year = year - 1900; |
170 | 0 | tm.tm_mon = month - 1; |
171 | 0 | tm.tm_mday = day; |
172 | 0 | tm.tm_hour = hour; |
173 | 0 | tm.tm_min = min; |
174 | 0 | tm.tm_sec = sec; |
175 | |
|
176 | 0 | t_local = mktime(&tm); |
177 | | |
178 | | /* figure out offset to UTC */ |
179 | 0 | tm1 = localtime(&t_local); |
180 | 0 | if (tm1) { |
181 | 0 | t1 = mktime(tm1); |
182 | 0 | tm1 = gmtime(&t_local); |
183 | 0 | if (tm1) { |
184 | 0 | t2 = mktime(tm1); |
185 | 0 | tz_offset = t2 - t1; |
186 | 0 | } else |
187 | 0 | tz_offset = 0; |
188 | 0 | } else |
189 | 0 | tz_offset = 0; |
190 | |
|
191 | 0 | *t = (os_time_t) t_local - tz_offset; |
192 | 0 | return 0; |
193 | 0 | } |
194 | | |
195 | | |
196 | | int os_gmtime(os_time_t t, struct os_tm *tm) |
197 | 0 | { |
198 | 0 | struct tm *tm2; |
199 | 0 | time_t t2 = t; |
200 | |
|
201 | 0 | tm2 = gmtime(&t2); |
202 | 0 | if (tm2 == NULL) |
203 | 0 | return -1; |
204 | 0 | tm->sec = tm2->tm_sec; |
205 | 0 | tm->min = tm2->tm_min; |
206 | 0 | tm->hour = tm2->tm_hour; |
207 | 0 | tm->day = tm2->tm_mday; |
208 | 0 | tm->month = tm2->tm_mon + 1; |
209 | 0 | tm->year = tm2->tm_year + 1900; |
210 | 0 | return 0; |
211 | 0 | } |
212 | | |
213 | | |
214 | | #ifdef __APPLE__ |
215 | | #include <fcntl.h> |
216 | | static int os_daemon(int nochdir, int noclose) |
217 | | { |
218 | | int devnull; |
219 | | |
220 | | if (chdir("/") < 0) |
221 | | return -1; |
222 | | |
223 | | devnull = open("/dev/null", O_RDWR); |
224 | | if (devnull < 0) |
225 | | return -1; |
226 | | |
227 | | if (dup2(devnull, STDIN_FILENO) < 0) { |
228 | | close(devnull); |
229 | | return -1; |
230 | | } |
231 | | |
232 | | if (dup2(devnull, STDOUT_FILENO) < 0) { |
233 | | close(devnull); |
234 | | return -1; |
235 | | } |
236 | | |
237 | | if (dup2(devnull, STDERR_FILENO) < 0) { |
238 | | close(devnull); |
239 | | return -1; |
240 | | } |
241 | | |
242 | | return 0; |
243 | | } |
244 | | #else /* __APPLE__ */ |
245 | 0 | #define os_daemon daemon |
246 | | #endif /* __APPLE__ */ |
247 | | |
248 | | |
249 | | int os_daemonize(const char *pid_file) |
250 | 0 | { |
251 | | #if defined(__uClinux__) || defined(__sun__) |
252 | | return -1; |
253 | | #else /* defined(__uClinux__) || defined(__sun__) */ |
254 | 0 | if (os_daemon(0, 0)) { |
255 | 0 | perror("daemon"); |
256 | 0 | return -1; |
257 | 0 | } |
258 | | |
259 | 0 | if (pid_file) { |
260 | 0 | FILE *f = fopen(pid_file, "w"); |
261 | 0 | if (f) { |
262 | 0 | fprintf(f, "%u\n", getpid()); |
263 | 0 | fclose(f); |
264 | 0 | } |
265 | 0 | } |
266 | |
|
267 | 0 | return -0; |
268 | 0 | #endif /* defined(__uClinux__) || defined(__sun__) */ |
269 | 0 | } |
270 | | |
271 | | |
272 | | void os_daemonize_terminate(const char *pid_file) |
273 | 0 | { |
274 | 0 | if (pid_file) |
275 | 0 | unlink(pid_file); |
276 | 0 | } |
277 | | |
278 | | |
279 | | int os_get_random(unsigned char *buf, size_t len) |
280 | 1.65k | { |
281 | 1.65k | #ifdef TEST_FUZZ |
282 | 1.65k | size_t i; |
283 | | |
284 | 8.28k | for (i = 0; i < len; i++) |
285 | 6.62k | buf[i] = i & 0xff; |
286 | 1.65k | return 0; |
287 | | #else /* TEST_FUZZ */ |
288 | | FILE *f; |
289 | | size_t rc; |
290 | | |
291 | | if (TEST_FAIL()) |
292 | | return -1; |
293 | | |
294 | | f = fopen("/dev/urandom", "rb"); |
295 | | if (f == NULL) { |
296 | | printf("Could not open /dev/urandom.\n"); |
297 | | return -1; |
298 | | } |
299 | | |
300 | | rc = fread(buf, 1, len, f); |
301 | | fclose(f); |
302 | | |
303 | | return rc != len ? -1 : 0; |
304 | | #endif /* TEST_FUZZ */ |
305 | 1.65k | } |
306 | | |
307 | | |
308 | | unsigned long os_random(void) |
309 | 0 | { |
310 | 0 | return random(); |
311 | 0 | } |
312 | | |
313 | | |
314 | | char * os_rel2abs_path(const char *rel_path) |
315 | 0 | { |
316 | 0 | char *buf = NULL, *cwd, *ret; |
317 | 0 | size_t len = 128, cwd_len, rel_len, ret_len; |
318 | 0 | int last_errno; |
319 | |
|
320 | 0 | if (!rel_path) |
321 | 0 | return NULL; |
322 | | |
323 | 0 | if (rel_path[0] == '/') |
324 | 0 | return os_strdup(rel_path); |
325 | | |
326 | 0 | for (;;) { |
327 | 0 | buf = os_malloc(len); |
328 | 0 | if (buf == NULL) |
329 | 0 | return NULL; |
330 | 0 | cwd = getcwd(buf, len); |
331 | 0 | if (cwd == NULL) { |
332 | 0 | last_errno = errno; |
333 | 0 | os_free(buf); |
334 | 0 | if (last_errno != ERANGE) |
335 | 0 | return NULL; |
336 | 0 | len *= 2; |
337 | 0 | if (len > 2000) |
338 | 0 | return NULL; |
339 | 0 | } else { |
340 | 0 | buf[len - 1] = '\0'; |
341 | 0 | break; |
342 | 0 | } |
343 | 0 | } |
344 | | |
345 | 0 | cwd_len = os_strlen(cwd); |
346 | 0 | rel_len = os_strlen(rel_path); |
347 | 0 | ret_len = cwd_len + 1 + rel_len + 1; |
348 | 0 | ret = os_malloc(ret_len); |
349 | 0 | if (ret) { |
350 | 0 | os_memcpy(ret, cwd, cwd_len); |
351 | 0 | ret[cwd_len] = '/'; |
352 | 0 | os_memcpy(ret + cwd_len + 1, rel_path, rel_len); |
353 | 0 | ret[ret_len - 1] = '\0'; |
354 | 0 | } |
355 | 0 | os_free(buf); |
356 | 0 | return ret; |
357 | 0 | } |
358 | | |
359 | | |
360 | | int os_program_init(void) |
361 | 1.65k | { |
362 | 1.65k | unsigned int seed; |
363 | | |
364 | | #ifdef ANDROID |
365 | | /* |
366 | | * We ignore errors here since errors are normal if we |
367 | | * are already running as non-root. |
368 | | */ |
369 | | #ifdef ANDROID_SETGROUPS_OVERRIDE |
370 | | gid_t groups[] = { ANDROID_SETGROUPS_OVERRIDE }; |
371 | | #else /* ANDROID_SETGROUPS_OVERRIDE */ |
372 | | gid_t groups[] = { AID_INET, AID_WIFI, AID_KEYSTORE }; |
373 | | #endif /* ANDROID_SETGROUPS_OVERRIDE */ |
374 | | struct __user_cap_header_struct header; |
375 | | struct __user_cap_data_struct cap; |
376 | | |
377 | | setgroups(ARRAY_SIZE(groups), groups); |
378 | | |
379 | | prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); |
380 | | |
381 | | setgid(AID_WIFI); |
382 | | setuid(AID_WIFI); |
383 | | |
384 | | header.version = _LINUX_CAPABILITY_VERSION; |
385 | | header.pid = 0; |
386 | | cap.effective = cap.permitted = |
387 | | (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW); |
388 | | cap.inheritable = 0; |
389 | | capset(&header, &cap); |
390 | | #endif /* ANDROID */ |
391 | | |
392 | 1.65k | if (os_get_random((unsigned char *) &seed, sizeof(seed)) == 0) |
393 | 1.65k | srandom(seed); |
394 | | |
395 | 1.65k | return 0; |
396 | 1.65k | } |
397 | | |
398 | | |
399 | | void os_program_deinit(void) |
400 | 1.65k | { |
401 | | #ifdef WPA_TRACE |
402 | | struct os_alloc_trace *a; |
403 | | unsigned long total = 0; |
404 | | dl_list_for_each(a, &alloc_list, struct os_alloc_trace, list) { |
405 | | total += a->len; |
406 | | if (a->magic != ALLOC_MAGIC) { |
407 | | wpa_printf(MSG_INFO, "MEMLEAK[%p]: invalid magic 0x%x " |
408 | | "len %lu", |
409 | | a, a->magic, (unsigned long) a->len); |
410 | | continue; |
411 | | } |
412 | | wpa_printf(MSG_INFO, "MEMLEAK[%p]: len %lu", |
413 | | a, (unsigned long) a->len); |
414 | | wpa_trace_dump("memleak", a); |
415 | | } |
416 | | if (total) |
417 | | wpa_printf(MSG_INFO, "MEMLEAK: total %lu bytes", |
418 | | (unsigned long) total); |
419 | | wpa_trace_deinit(); |
420 | | #endif /* WPA_TRACE */ |
421 | 1.65k | } |
422 | | |
423 | | |
424 | | int os_setenv(const char *name, const char *value, int overwrite) |
425 | 0 | { |
426 | 0 | return setenv(name, value, overwrite); |
427 | 0 | } |
428 | | |
429 | | |
430 | | int os_unsetenv(const char *name) |
431 | 0 | { |
432 | | #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) || \ |
433 | | defined(__OpenBSD__) |
434 | | unsetenv(name); |
435 | | return 0; |
436 | | #else |
437 | 0 | return unsetenv(name); |
438 | 0 | #endif |
439 | 0 | } |
440 | | |
441 | | |
442 | | char * os_readfile(const char *name, size_t *len) |
443 | 0 | { |
444 | 0 | FILE *f; |
445 | 0 | char *buf; |
446 | 0 | long pos; |
447 | |
|
448 | 0 | f = fopen(name, "rb"); |
449 | 0 | if (f == NULL) |
450 | 0 | return NULL; |
451 | | |
452 | 0 | if (fseek(f, 0, SEEK_END) < 0 || (pos = ftell(f)) < 0) { |
453 | 0 | fclose(f); |
454 | 0 | return NULL; |
455 | 0 | } |
456 | 0 | *len = pos; |
457 | 0 | if (fseek(f, 0, SEEK_SET) < 0) { |
458 | 0 | fclose(f); |
459 | 0 | return NULL; |
460 | 0 | } |
461 | | |
462 | 0 | buf = os_malloc(*len); |
463 | 0 | if (buf == NULL) { |
464 | 0 | fclose(f); |
465 | 0 | return NULL; |
466 | 0 | } |
467 | | |
468 | 0 | if (fread(buf, 1, *len, f) != *len) { |
469 | 0 | fclose(f); |
470 | 0 | os_free(buf); |
471 | 0 | return NULL; |
472 | 0 | } |
473 | | |
474 | 0 | fclose(f); |
475 | |
|
476 | 0 | return buf; |
477 | 0 | } |
478 | | |
479 | | |
480 | | int os_file_exists(const char *fname) |
481 | 0 | { |
482 | 0 | return access(fname, F_OK) == 0; |
483 | 0 | } |
484 | | |
485 | | |
486 | | int os_fdatasync(FILE *stream) |
487 | 0 | { |
488 | 0 | if (!fflush(stream)) { |
489 | 0 | #if defined __FreeBSD__ || defined __linux__ |
490 | 0 | return fdatasync(fileno(stream)); |
491 | | #else /* !__linux__ && !__FreeBSD__ */ |
492 | | #ifdef F_FULLFSYNC |
493 | | /* OS X does not implement fdatasync(). */ |
494 | | return fcntl(fileno(stream), F_FULLFSYNC); |
495 | | #else /* F_FULLFSYNC */ |
496 | | return fsync(fileno(stream)); |
497 | | #endif /* F_FULLFSYNC */ |
498 | | #endif /* __linux__ */ |
499 | 0 | } |
500 | | |
501 | 0 | return -1; |
502 | 0 | } |
503 | | |
504 | | |
505 | | #ifndef WPA_TRACE |
506 | | void * os_zalloc(size_t size) |
507 | 3.04k | { |
508 | 3.04k | return calloc(1, size); |
509 | 3.04k | } |
510 | | #endif /* WPA_TRACE */ |
511 | | |
512 | | |
513 | | size_t os_strlcpy(char *dest, const char *src, size_t siz) |
514 | 39 | { |
515 | 39 | const char *s = src; |
516 | 39 | size_t left = siz; |
517 | | |
518 | 39 | if (left) { |
519 | | /* Copy string up to the maximum size of the dest buffer */ |
520 | 331 | while (--left != 0) { |
521 | 331 | if ((*dest++ = *s++) == '\0') |
522 | 39 | break; |
523 | 331 | } |
524 | 39 | } |
525 | | |
526 | 39 | if (left == 0) { |
527 | | /* Not enough room for the string; force NUL-termination */ |
528 | 0 | if (siz != 0) |
529 | 0 | *dest = '\0'; |
530 | 0 | while (*s++) |
531 | 0 | ; /* determine total src string length */ |
532 | 0 | } |
533 | | |
534 | 39 | return s - src - 1; |
535 | 39 | } |
536 | | |
537 | | |
538 | | int os_memcmp_const(const void *a, const void *b, size_t len) |
539 | 0 | { |
540 | 0 | const u8 *aa = a; |
541 | 0 | const u8 *bb = b; |
542 | 0 | size_t i; |
543 | 0 | u8 res; |
544 | |
|
545 | 0 | for (res = 0, i = 0; i < len; i++) |
546 | 0 | res |= aa[i] ^ bb[i]; |
547 | |
|
548 | 0 | return res; |
549 | 0 | } |
550 | | |
551 | | |
552 | | void * os_memdup(const void *src, size_t len) |
553 | 23 | { |
554 | 23 | void *r = os_malloc(len); |
555 | | |
556 | 23 | if (r && src) |
557 | 23 | os_memcpy(r, src, len); |
558 | 23 | return r; |
559 | 23 | } |
560 | | |
561 | | |
562 | | #ifdef WPA_TRACE |
563 | | |
564 | | #if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS) |
565 | | static struct wpa_trace_test_fail { |
566 | | unsigned int fail_after; |
567 | | char pattern[256]; |
568 | | bool uses_tag; |
569 | | } wpa_trace_test_fail[2][5]; |
570 | | |
571 | | int testing_test_fail(const char *tag, bool is_alloc) |
572 | | { |
573 | | const char *ignore_list[] = { |
574 | | "os_malloc", "os_zalloc", "os_calloc", "os_realloc", |
575 | | "os_realloc_array", "os_strdup", "os_memdup" |
576 | | }; |
577 | | const char *func[WPA_TRACE_LEN]; |
578 | | size_t i, j, res, len, idx; |
579 | | char *pos, *next; |
580 | | int match; |
581 | | |
582 | | is_alloc = !!is_alloc; |
583 | | |
584 | | if (wpa_trace_test_fail[is_alloc][0].uses_tag && !tag) |
585 | | return 0; |
586 | | |
587 | | for (idx = 0; idx < ARRAY_SIZE(wpa_trace_test_fail[is_alloc]); idx++) { |
588 | | if (wpa_trace_test_fail[is_alloc][idx].fail_after != 0) |
589 | | break; |
590 | | } |
591 | | if (idx >= ARRAY_SIZE(wpa_trace_test_fail[is_alloc])) |
592 | | return 0; |
593 | | |
594 | | res = wpa_trace_calling_func(func, WPA_TRACE_LEN); |
595 | | i = 0; |
596 | | |
597 | | if (is_alloc) { |
598 | | /* Skip our own stack frame */ |
599 | | i++; |
600 | | |
601 | | /* Skip allocation helpers */ |
602 | | for (j = 0; j < ARRAY_SIZE(ignore_list) && i < res; j++) { |
603 | | if (os_strcmp(func[i], ignore_list[j]) == 0) |
604 | | i++; |
605 | | } |
606 | | } else { |
607 | | /* Not allocation, we might have a tag, if so, replace our |
608 | | * own stack frame with the tag, otherwise skip it. |
609 | | */ |
610 | | if (tag) |
611 | | func[0] = tag; |
612 | | else |
613 | | i++; |
614 | | } |
615 | | |
616 | | pos = wpa_trace_test_fail[is_alloc][idx].pattern; |
617 | | |
618 | | /* The prefixes mean: |
619 | | * - '=': The function needs to be next in the backtrace |
620 | | * - '?': The function is optionally present in the backtrace |
621 | | */ |
622 | | |
623 | | match = 0; |
624 | | while (i < res) { |
625 | | int allow_skip = 1; |
626 | | int maybe = 0; |
627 | | bool prefix = false; |
628 | | |
629 | | if (*pos == '=') { |
630 | | allow_skip = 0; |
631 | | pos++; |
632 | | } else if (*pos == '?') { |
633 | | maybe = 1; |
634 | | pos++; |
635 | | } |
636 | | next = os_strchr(pos, ';'); |
637 | | if (next) |
638 | | len = next - pos; |
639 | | else |
640 | | len = os_strlen(pos); |
641 | | if (len >= 1 && pos[len - 1] == '*') { |
642 | | prefix = true; |
643 | | len -= 1; |
644 | | } |
645 | | if (os_strncmp(pos, func[i], len) != 0 || |
646 | | (!prefix && func[i][len] != '\0')) { |
647 | | if (maybe && next) { |
648 | | pos = next + 1; |
649 | | continue; |
650 | | } |
651 | | if (allow_skip) { |
652 | | i++; |
653 | | continue; |
654 | | } |
655 | | return 0; |
656 | | } |
657 | | if (!next) { |
658 | | match = 1; |
659 | | break; |
660 | | } |
661 | | pos = next + 1; |
662 | | i++; |
663 | | } |
664 | | if (!match) |
665 | | return 0; |
666 | | |
667 | | wpa_trace_test_fail[is_alloc][idx].fail_after--; |
668 | | if (wpa_trace_test_fail[is_alloc][idx].fail_after == 0) { |
669 | | wpa_printf(MSG_INFO, "TESTING: fail at %s", |
670 | | wpa_trace_test_fail[is_alloc][idx].pattern); |
671 | | for (i = 0; i < res; i++) |
672 | | wpa_printf(MSG_INFO, "backtrace[%d] = %s", |
673 | | (int) i, func[i]); |
674 | | return 1; |
675 | | } |
676 | | |
677 | | return 0; |
678 | | } |
679 | | |
680 | | |
681 | | int testing_set_fail_pattern(bool is_alloc, char *patterns) |
682 | | { |
683 | | #ifdef WPA_TRACE_BFD |
684 | | char *token, *context = NULL; |
685 | | size_t idx; |
686 | | |
687 | | is_alloc = !!is_alloc; |
688 | | |
689 | | os_memset(wpa_trace_test_fail[is_alloc], 0, |
690 | | sizeof(wpa_trace_test_fail[is_alloc])); |
691 | | |
692 | | idx = 0; |
693 | | while ((token = str_token(patterns, " \n\r\t", &context)) && |
694 | | idx < ARRAY_SIZE(wpa_trace_test_fail[is_alloc])) { |
695 | | wpa_trace_test_fail[is_alloc][idx].fail_after = atoi(token); |
696 | | token = os_strchr(token, ':'); |
697 | | if (!token) { |
698 | | os_memset(wpa_trace_test_fail[is_alloc], 0, |
699 | | sizeof(wpa_trace_test_fail[is_alloc])); |
700 | | return -1; |
701 | | } |
702 | | |
703 | | if (idx == 0) { |
704 | | const char *p1, *p2; |
705 | | |
706 | | p1 = os_strchr(token, '-'); |
707 | | p2 = os_strchr(token, ';'); |
708 | | if (p1 && p2 && p1 < p2) |
709 | | wpa_trace_test_fail[is_alloc][0].uses_tag = |
710 | | true; |
711 | | } |
712 | | os_strlcpy(wpa_trace_test_fail[is_alloc][idx].pattern, |
713 | | token + 1, |
714 | | sizeof(wpa_trace_test_fail[is_alloc][0].pattern)); |
715 | | idx++; |
716 | | } |
717 | | |
718 | | return 0; |
719 | | #else /* WPA_TRACE_BFD */ |
720 | | return -1; |
721 | | #endif /* WPA_TRACE_BFD */ |
722 | | } |
723 | | |
724 | | |
725 | | int testing_get_fail_pattern(bool is_alloc, char *buf, size_t buflen) |
726 | | { |
727 | | #ifdef WPA_TRACE_BFD |
728 | | size_t idx, ret; |
729 | | char *pos = buf; |
730 | | char *end = buf + buflen; |
731 | | |
732 | | is_alloc = !!is_alloc; |
733 | | |
734 | | for (idx = 0; idx < ARRAY_SIZE(wpa_trace_test_fail[is_alloc]); idx++) { |
735 | | if (wpa_trace_test_fail[is_alloc][idx].pattern[0] == '\0') |
736 | | break; |
737 | | |
738 | | ret = os_snprintf(pos, end - pos, "%s%u:%s", |
739 | | pos == buf ? "" : " ", |
740 | | wpa_trace_test_fail[is_alloc][idx].fail_after, |
741 | | wpa_trace_test_fail[is_alloc][idx].pattern); |
742 | | if (os_snprintf_error(end - pos, ret)) |
743 | | break; |
744 | | pos += ret; |
745 | | } |
746 | | |
747 | | return pos - buf; |
748 | | #else /* WPA_TRACE_BFD */ |
749 | | return -1; |
750 | | #endif /* WPA_TRACE_BFD */ |
751 | | } |
752 | | #endif /* defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS) */ |
753 | | |
754 | | void * os_malloc(size_t size) |
755 | | { |
756 | | struct os_alloc_trace *a; |
757 | | |
758 | | if (testing_test_fail(NULL, true)) |
759 | | return NULL; |
760 | | |
761 | | a = malloc(sizeof(*a) + size); |
762 | | if (a == NULL) |
763 | | return NULL; |
764 | | a->magic = ALLOC_MAGIC; |
765 | | dl_list_add(&alloc_list, &a->list); |
766 | | a->len = size; |
767 | | wpa_trace_record(a); |
768 | | return a + 1; |
769 | | } |
770 | | |
771 | | |
772 | | void * os_realloc(void *ptr, size_t size) |
773 | | { |
774 | | struct os_alloc_trace *a; |
775 | | size_t copy_len; |
776 | | void *n; |
777 | | |
778 | | if (ptr == NULL) |
779 | | return os_malloc(size); |
780 | | |
781 | | a = (struct os_alloc_trace *) ptr - 1; |
782 | | if (a->magic != ALLOC_MAGIC) { |
783 | | wpa_printf(MSG_INFO, "REALLOC[%p]: invalid magic 0x%x%s", |
784 | | a, a->magic, |
785 | | a->magic == FREED_MAGIC ? " (already freed)" : ""); |
786 | | wpa_trace_show("Invalid os_realloc() call"); |
787 | | abort(); |
788 | | } |
789 | | n = os_malloc(size); |
790 | | if (n == NULL) |
791 | | return NULL; |
792 | | copy_len = a->len; |
793 | | if (copy_len > size) |
794 | | copy_len = size; |
795 | | os_memcpy(n, a + 1, copy_len); |
796 | | os_free(ptr); |
797 | | return n; |
798 | | } |
799 | | |
800 | | |
801 | | void os_free(void *ptr) |
802 | | { |
803 | | struct os_alloc_trace *a; |
804 | | |
805 | | if (ptr == NULL) |
806 | | return; |
807 | | a = (struct os_alloc_trace *) ptr - 1; |
808 | | if (a->magic != ALLOC_MAGIC) { |
809 | | wpa_printf(MSG_INFO, "FREE[%p]: invalid magic 0x%x%s", |
810 | | a, a->magic, |
811 | | a->magic == FREED_MAGIC ? " (already freed)" : ""); |
812 | | wpa_trace_show("Invalid os_free() call"); |
813 | | abort(); |
814 | | } |
815 | | dl_list_del(&a->list); |
816 | | a->magic = FREED_MAGIC; |
817 | | |
818 | | wpa_trace_check_ref(ptr); |
819 | | free(a); |
820 | | } |
821 | | |
822 | | |
823 | | void * os_zalloc(size_t size) |
824 | | { |
825 | | void *ptr = os_malloc(size); |
826 | | if (ptr) |
827 | | os_memset(ptr, 0, size); |
828 | | return ptr; |
829 | | } |
830 | | |
831 | | |
832 | | char * os_strdup(const char *s) |
833 | | { |
834 | | size_t len; |
835 | | char *d; |
836 | | len = os_strlen(s); |
837 | | d = os_malloc(len + 1); |
838 | | if (d == NULL) |
839 | | return NULL; |
840 | | os_memcpy(d, s, len); |
841 | | d[len] = '\0'; |
842 | | return d; |
843 | | } |
844 | | |
845 | | #endif /* WPA_TRACE */ |
846 | | |
847 | | |
848 | | int os_exec(const char *program, const char *arg, int wait_completion) |
849 | 0 | { |
850 | 0 | pid_t pid; |
851 | 0 | int pid_status; |
852 | |
|
853 | 0 | pid = fork(); |
854 | 0 | if (pid < 0) { |
855 | 0 | perror("fork"); |
856 | 0 | return -1; |
857 | 0 | } |
858 | | |
859 | 0 | if (pid == 0) { |
860 | | /* run the external command in the child process */ |
861 | 0 | #define MAX_ARG 30 |
862 | 0 | char *_program, *_arg, *pos; |
863 | 0 | char *argv[MAX_ARG + 1]; |
864 | 0 | int i; |
865 | |
|
866 | 0 | _program = os_strdup(program); |
867 | 0 | _arg = os_strdup(arg); |
868 | |
|
869 | 0 | argv[0] = _program; |
870 | |
|
871 | 0 | i = 1; |
872 | 0 | pos = _arg; |
873 | 0 | while (i < MAX_ARG && pos && *pos) { |
874 | 0 | while (*pos == ' ') |
875 | 0 | pos++; |
876 | 0 | if (*pos == '\0') |
877 | 0 | break; |
878 | 0 | argv[i++] = pos; |
879 | 0 | pos = os_strchr(pos, ' '); |
880 | 0 | if (pos) |
881 | 0 | *pos++ = '\0'; |
882 | 0 | } |
883 | 0 | argv[i] = NULL; |
884 | |
|
885 | 0 | execv(program, argv); |
886 | 0 | perror("execv"); |
887 | 0 | os_free(_program); |
888 | 0 | os_free(_arg); |
889 | 0 | exit(0); |
890 | 0 | return -1; |
891 | 0 | } |
892 | | |
893 | 0 | if (wait_completion) { |
894 | | /* wait for the child process to complete in the parent */ |
895 | 0 | waitpid(pid, &pid_status, 0); |
896 | 0 | } |
897 | |
|
898 | 0 | return 0; |
899 | 0 | } |