blob: e721df27d1dd3f334875e0c260a382d4c266471b [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * OS specific functions for UNIX/POSIX systems
Hai Shalom021b0b52019-04-10 11:17:58 -07003 * Copyright (c) 2005-2019, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080011#include <time.h>
Jouni Malinen772e12c2014-10-07 10:29:35 -070012#include <sys/wait.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#ifdef ANDROID
Yifan Honge0317902017-07-20 14:15:22 -070015#include <grp.h>
16#include <pwd.h>
Nick Kralevicha562b192013-02-28 13:55:41 -080017#include <sys/capability.h>
Elliott Hughes0f4fce12014-07-18 17:45:49 -070018#include <sys/prctl.h>
Yifan Honge0317902017-07-20 14:15:22 -070019#include <sys/types.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#endif /* ANDROID */
21
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080022#ifdef __MACH__
23#include <CoreServices/CoreServices.h>
24#include <mach/mach.h>
25#include <mach/mach_time.h>
26#endif /* __MACH__ */
27
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "os.h"
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070029#include "common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030
31#ifdef WPA_TRACE
32
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "wpa_debug.h"
34#include "trace.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080035#include "list.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036
Dmitry Shmidt7f656022015-02-25 14:36:37 -080037static struct dl_list alloc_list = DL_LIST_HEAD_INIT(alloc_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038
39#define ALLOC_MAGIC 0xa84ef1b2
40#define FREED_MAGIC 0x67fd487a
41
42struct os_alloc_trace {
43 unsigned int magic;
Hai Shalom60840252021-02-19 19:02:11 -080044 struct dl_list list __attribute__((aligned(16)));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045 size_t len;
46 WPA_TRACE_INFO
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080047} __attribute__((aligned(16)));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048
49#endif /* WPA_TRACE */
50
51
52void os_sleep(os_time_t sec, os_time_t usec)
53{
Hai Shalomc3565922019-10-28 11:58:20 -070054#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L)
55 const struct timespec req = { sec, usec * 1000 };
56
57 nanosleep(&req, NULL);
58#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059 if (sec)
60 sleep(sec);
61 if (usec)
62 usleep(usec);
Hai Shalomc3565922019-10-28 11:58:20 -070063#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064}
65
66
67int os_get_time(struct os_time *t)
68{
69 int res;
70 struct timeval tv;
71 res = gettimeofday(&tv, NULL);
72 t->sec = tv.tv_sec;
73 t->usec = tv.tv_usec;
74 return res;
75}
76
77
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -080078int os_get_reltime(struct os_reltime *t)
79{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080080#ifndef __MACH__
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -080081#if defined(CLOCK_BOOTTIME)
82 static clockid_t clock_id = CLOCK_BOOTTIME;
83#elif defined(CLOCK_MONOTONIC)
84 static clockid_t clock_id = CLOCK_MONOTONIC;
85#else
86 static clockid_t clock_id = CLOCK_REALTIME;
87#endif
88 struct timespec ts;
89 int res;
90
Paul Stewart092955c2017-02-06 09:13:09 -080091 if (TEST_FAIL())
92 return -1;
93
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -080094 while (1) {
95 res = clock_gettime(clock_id, &ts);
96 if (res == 0) {
97 t->sec = ts.tv_sec;
98 t->usec = ts.tv_nsec / 1000;
99 return 0;
100 }
101 switch (clock_id) {
102#ifdef CLOCK_BOOTTIME
103 case CLOCK_BOOTTIME:
104 clock_id = CLOCK_MONOTONIC;
105 break;
106#endif
107#ifdef CLOCK_MONOTONIC
108 case CLOCK_MONOTONIC:
109 clock_id = CLOCK_REALTIME;
110 break;
111#endif
112 case CLOCK_REALTIME:
113 return -1;
114 }
115 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800116#else /* __MACH__ */
117 uint64_t abstime, nano;
118 static mach_timebase_info_data_t info = { 0, 0 };
119
120 if (!info.denom) {
121 if (mach_timebase_info(&info) != KERN_SUCCESS)
122 return -1;
123 }
124
125 abstime = mach_absolute_time();
126 nano = (abstime * info.numer) / info.denom;
127
128 t->sec = nano / NSEC_PER_SEC;
129 t->usec = (nano - (((uint64_t) t->sec) * NSEC_PER_SEC)) / NSEC_PER_USEC;
130
131 return 0;
132#endif /* __MACH__ */
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800133}
134
135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136int os_mktime(int year, int month, int day, int hour, int min, int sec,
137 os_time_t *t)
138{
139 struct tm tm, *tm1;
140 time_t t_local, t1, t2;
141 os_time_t tz_offset;
142
143 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
144 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
145 sec > 60)
146 return -1;
147
148 memset(&tm, 0, sizeof(tm));
149 tm.tm_year = year - 1900;
150 tm.tm_mon = month - 1;
151 tm.tm_mday = day;
152 tm.tm_hour = hour;
153 tm.tm_min = min;
154 tm.tm_sec = sec;
155
156 t_local = mktime(&tm);
157
158 /* figure out offset to UTC */
159 tm1 = localtime(&t_local);
160 if (tm1) {
161 t1 = mktime(tm1);
162 tm1 = gmtime(&t_local);
163 if (tm1) {
164 t2 = mktime(tm1);
165 tz_offset = t2 - t1;
166 } else
167 tz_offset = 0;
168 } else
169 tz_offset = 0;
170
171 *t = (os_time_t) t_local - tz_offset;
172 return 0;
173}
174
175
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800176int os_gmtime(os_time_t t, struct os_tm *tm)
177{
178 struct tm *tm2;
179 time_t t2 = t;
180
181 tm2 = gmtime(&t2);
182 if (tm2 == NULL)
183 return -1;
184 tm->sec = tm2->tm_sec;
185 tm->min = tm2->tm_min;
186 tm->hour = tm2->tm_hour;
187 tm->day = tm2->tm_mday;
188 tm->month = tm2->tm_mon + 1;
189 tm->year = tm2->tm_year + 1900;
190 return 0;
191}
192
193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194#ifdef __APPLE__
195#include <fcntl.h>
196static int os_daemon(int nochdir, int noclose)
197{
198 int devnull;
199
200 if (chdir("/") < 0)
201 return -1;
202
203 devnull = open("/dev/null", O_RDWR);
204 if (devnull < 0)
205 return -1;
206
207 if (dup2(devnull, STDIN_FILENO) < 0) {
208 close(devnull);
209 return -1;
210 }
211
212 if (dup2(devnull, STDOUT_FILENO) < 0) {
213 close(devnull);
214 return -1;
215 }
216
217 if (dup2(devnull, STDERR_FILENO) < 0) {
218 close(devnull);
219 return -1;
220 }
221
222 return 0;
223}
224#else /* __APPLE__ */
225#define os_daemon daemon
226#endif /* __APPLE__ */
227
228
229int os_daemonize(const char *pid_file)
230{
231#if defined(__uClinux__) || defined(__sun__)
232 return -1;
233#else /* defined(__uClinux__) || defined(__sun__) */
234 if (os_daemon(0, 0)) {
235 perror("daemon");
236 return -1;
237 }
238
239 if (pid_file) {
240 FILE *f = fopen(pid_file, "w");
241 if (f) {
242 fprintf(f, "%u\n", getpid());
243 fclose(f);
244 }
245 }
246
247 return -0;
248#endif /* defined(__uClinux__) || defined(__sun__) */
249}
250
251
252void os_daemonize_terminate(const char *pid_file)
253{
254 if (pid_file)
255 unlink(pid_file);
256}
257
258
259int os_get_random(unsigned char *buf, size_t len)
260{
Hai Shalom74f70d42019-02-11 14:42:39 -0800261#ifdef TEST_FUZZ
262 size_t i;
263
264 for (i = 0; i < len; i++)
265 buf[i] = i & 0xff;
266 return 0;
267#else /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268 FILE *f;
269 size_t rc;
270
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800271 if (TEST_FAIL())
272 return -1;
273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274 f = fopen("/dev/urandom", "rb");
275 if (f == NULL) {
276 printf("Could not open /dev/urandom.\n");
277 return -1;
278 }
279
280 rc = fread(buf, 1, len, f);
281 fclose(f);
282
283 return rc != len ? -1 : 0;
Hai Shalom74f70d42019-02-11 14:42:39 -0800284#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285}
286
287
288unsigned long os_random(void)
289{
290 return random();
291}
292
293
294char * os_rel2abs_path(const char *rel_path)
295{
296 char *buf = NULL, *cwd, *ret;
297 size_t len = 128, cwd_len, rel_len, ret_len;
298 int last_errno;
299
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700300 if (!rel_path)
301 return NULL;
302
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 if (rel_path[0] == '/')
304 return os_strdup(rel_path);
305
306 for (;;) {
307 buf = os_malloc(len);
308 if (buf == NULL)
309 return NULL;
310 cwd = getcwd(buf, len);
311 if (cwd == NULL) {
312 last_errno = errno;
313 os_free(buf);
314 if (last_errno != ERANGE)
315 return NULL;
316 len *= 2;
317 if (len > 2000)
318 return NULL;
319 } else {
320 buf[len - 1] = '\0';
321 break;
322 }
323 }
324
325 cwd_len = os_strlen(cwd);
326 rel_len = os_strlen(rel_path);
327 ret_len = cwd_len + 1 + rel_len + 1;
328 ret = os_malloc(ret_len);
329 if (ret) {
330 os_memcpy(ret, cwd, cwd_len);
331 ret[cwd_len] = '/';
332 os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
333 ret[ret_len - 1] = '\0';
334 }
335 os_free(buf);
336 return ret;
337}
338
339
340int os_program_init(void)
341{
Hai Shalomfdcde762020-04-02 11:19:20 -0700342 unsigned int seed;
343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344#ifdef ANDROID
Yifan Honge0317902017-07-20 14:15:22 -0700345 struct __user_cap_header_struct header;
346 struct __user_cap_data_struct cap;
347 struct group *grp = getgrnam("wifi");
348 gid_t gid_wifi = grp ? grp->gr_gid : 0;
349 struct passwd *pwd = getpwnam("wifi");
350 uid_t uid_wifi = pwd ? pwd->pw_uid : 0;
351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 /*
353 * We ignore errors here since errors are normal if we
354 * are already running as non-root.
355 */
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800356#ifdef ANDROID_SETGROUPS_OVERRIDE
357 gid_t groups[] = { ANDROID_SETGROUPS_OVERRIDE };
Yifan Honge0317902017-07-20 14:15:22 -0700358
359 if (!gid_wifi || !uid_wifi) return -1;
Pavel Grafov7b28f302018-02-14 14:29:41 +0000360 setgroups(ARRAY_SIZE(groups), groups);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800361#else /* ANDROID_SETGROUPS_OVERRIDE */
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000362 gid_t groups[4];
363 int group_idx = 0;
Yifan Honge0317902017-07-20 14:15:22 -0700364
365 if (!gid_wifi || !uid_wifi) return -1;
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000366 groups[group_idx] = gid_wifi;
Yifan Honge0317902017-07-20 14:15:22 -0700367
368 grp = getgrnam("inet");
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000369 groups[++group_idx] = grp ? grp->gr_gid : 0;
370 if (!groups[group_idx]) return -1;
Yifan Honge0317902017-07-20 14:15:22 -0700371
372 grp = getgrnam("keystore");
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000373 groups[++group_idx] = grp ? grp->gr_gid : 0;
374 if (!groups[group_idx]) return -1;
375
376 grp = getgrnam("log");
377 groups[++group_idx] = grp ? grp->gr_gid : 0;
378 if (!groups[group_idx]) group_idx--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700379
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000380 setgroups(group_idx + 1, groups);
Pavel Grafov7b28f302018-02-14 14:29:41 +0000381#endif /* ANDROID_SETGROUPS_OVERRIDE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382
383 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
384
Yifan Honge0317902017-07-20 14:15:22 -0700385 setgid(gid_wifi);
386 setuid(uid_wifi);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387
388 header.version = _LINUX_CAPABILITY_VERSION;
389 header.pid = 0;
390 cap.effective = cap.permitted =
391 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW);
392 cap.inheritable = 0;
393 capset(&header, &cap);
394#endif /* ANDROID */
395
Hai Shalomfdcde762020-04-02 11:19:20 -0700396 if (os_get_random((unsigned char *) &seed, sizeof(seed)) == 0)
397 srandom(seed);
398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 return 0;
400}
401
402
403void os_program_deinit(void)
404{
405#ifdef WPA_TRACE
406 struct os_alloc_trace *a;
407 unsigned long total = 0;
408 dl_list_for_each(a, &alloc_list, struct os_alloc_trace, list) {
409 total += a->len;
410 if (a->magic != ALLOC_MAGIC) {
411 wpa_printf(MSG_INFO, "MEMLEAK[%p]: invalid magic 0x%x "
412 "len %lu",
413 a, a->magic, (unsigned long) a->len);
414 continue;
415 }
416 wpa_printf(MSG_INFO, "MEMLEAK[%p]: len %lu",
417 a, (unsigned long) a->len);
418 wpa_trace_dump("memleak", a);
419 }
420 if (total)
421 wpa_printf(MSG_INFO, "MEMLEAK: total %lu bytes",
422 (unsigned long) total);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800423 wpa_trace_deinit();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424#endif /* WPA_TRACE */
425}
426
427
428int os_setenv(const char *name, const char *value, int overwrite)
429{
430 return setenv(name, value, overwrite);
431}
432
433
434int os_unsetenv(const char *name)
435{
436#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) || \
437 defined(__OpenBSD__)
438 unsetenv(name);
439 return 0;
440#else
441 return unsetenv(name);
442#endif
443}
444
445
446char * os_readfile(const char *name, size_t *len)
447{
448 FILE *f;
449 char *buf;
450 long pos;
451
452 f = fopen(name, "rb");
453 if (f == NULL)
454 return NULL;
455
456 if (fseek(f, 0, SEEK_END) < 0 || (pos = ftell(f)) < 0) {
457 fclose(f);
458 return NULL;
459 }
460 *len = pos;
461 if (fseek(f, 0, SEEK_SET) < 0) {
462 fclose(f);
463 return NULL;
464 }
465
466 buf = os_malloc(*len);
467 if (buf == NULL) {
468 fclose(f);
469 return NULL;
470 }
471
472 if (fread(buf, 1, *len, f) != *len) {
473 fclose(f);
474 os_free(buf);
475 return NULL;
476 }
477
478 fclose(f);
479
480 return buf;
481}
482
483
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700484int os_file_exists(const char *fname)
485{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700486 return access(fname, F_OK) == 0;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700487}
488
489
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800490int os_fdatasync(FILE *stream)
Mitchell Wills447c7ff2015-08-24 17:24:30 -0700491{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800492 if (!fflush(stream)) {
Hai Shaloma20dcd72022-02-04 13:43:00 -0800493#if defined __FreeBSD__ || defined __linux__
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800494 return fdatasync(fileno(stream));
Hai Shaloma20dcd72022-02-04 13:43:00 -0800495#else /* !__linux__ && !__FreeBSD__ */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800496#ifdef F_FULLFSYNC
497 /* OS X does not implement fdatasync(). */
498 return fcntl(fileno(stream), F_FULLFSYNC);
499#else /* F_FULLFSYNC */
Mitchell Wills447c7ff2015-08-24 17:24:30 -0700500 return fsync(fileno(stream));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800501#endif /* F_FULLFSYNC */
502#endif /* __linux__ */
503 }
504
Mitchell Wills447c7ff2015-08-24 17:24:30 -0700505 return -1;
506}
507
508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509#ifndef WPA_TRACE
510void * os_zalloc(size_t size)
511{
512 return calloc(1, size);
513}
514#endif /* WPA_TRACE */
515
516
517size_t os_strlcpy(char *dest, const char *src, size_t siz)
518{
519 const char *s = src;
520 size_t left = siz;
521
522 if (left) {
523 /* Copy string up to the maximum size of the dest buffer */
524 while (--left != 0) {
525 if ((*dest++ = *s++) == '\0')
526 break;
527 }
528 }
529
530 if (left == 0) {
531 /* Not enough room for the string; force NUL-termination */
532 if (siz != 0)
533 *dest = '\0';
534 while (*s++)
535 ; /* determine total src string length */
536 }
537
538 return s - src - 1;
539}
540
541
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700542int os_memcmp_const(const void *a, const void *b, size_t len)
543{
544 const u8 *aa = a;
545 const u8 *bb = b;
546 size_t i;
547 u8 res;
548
549 for (res = 0, i = 0; i < len; i++)
550 res |= aa[i] ^ bb[i];
551
552 return res;
553}
554
555
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700556void * os_memdup(const void *src, size_t len)
557{
558 void *r = os_malloc(len);
559
Hai Shalom021b0b52019-04-10 11:17:58 -0700560 if (r && src)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700561 os_memcpy(r, src, len);
562 return r;
563}
564
565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700566#ifdef WPA_TRACE
567
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800568#if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS)
569char wpa_trace_fail_func[256] = { 0 };
570unsigned int wpa_trace_fail_after;
571
572static int testing_fail_alloc(void)
573{
574 const char *func[WPA_TRACE_LEN];
575 size_t i, res, len;
576 char *pos, *next;
577 int match;
578
579 if (!wpa_trace_fail_after)
580 return 0;
581
582 res = wpa_trace_calling_func(func, WPA_TRACE_LEN);
583 i = 0;
584 if (i < res && os_strcmp(func[i], __func__) == 0)
585 i++;
586 if (i < res && os_strcmp(func[i], "os_malloc") == 0)
587 i++;
588 if (i < res && os_strcmp(func[i], "os_zalloc") == 0)
589 i++;
590 if (i < res && os_strcmp(func[i], "os_calloc") == 0)
591 i++;
592 if (i < res && os_strcmp(func[i], "os_realloc") == 0)
593 i++;
594 if (i < res && os_strcmp(func[i], "os_realloc_array") == 0)
595 i++;
596 if (i < res && os_strcmp(func[i], "os_strdup") == 0)
597 i++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700598 if (i < res && os_strcmp(func[i], "os_memdup") == 0)
599 i++;
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800600
601 pos = wpa_trace_fail_func;
602
603 match = 0;
604 while (i < res) {
605 int allow_skip = 1;
606 int maybe = 0;
607
608 if (*pos == '=') {
609 allow_skip = 0;
610 pos++;
611 } else if (*pos == '?') {
612 maybe = 1;
613 pos++;
614 }
615 next = os_strchr(pos, ';');
616 if (next)
617 len = next - pos;
618 else
619 len = os_strlen(pos);
620 if (os_memcmp(pos, func[i], len) != 0) {
621 if (maybe && next) {
622 pos = next + 1;
623 continue;
624 }
625 if (allow_skip) {
626 i++;
627 continue;
628 }
629 return 0;
630 }
631 if (!next) {
632 match = 1;
633 break;
634 }
635 pos = next + 1;
636 i++;
637 }
638 if (!match)
639 return 0;
640
641 wpa_trace_fail_after--;
642 if (wpa_trace_fail_after == 0) {
643 wpa_printf(MSG_INFO, "TESTING: fail allocation at %s",
644 wpa_trace_fail_func);
645 for (i = 0; i < res; i++)
646 wpa_printf(MSG_INFO, "backtrace[%d] = %s",
647 (int) i, func[i]);
648 return 1;
649 }
650
651 return 0;
652}
653
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800654
655char wpa_trace_test_fail_func[256] = { 0 };
656unsigned int wpa_trace_test_fail_after;
657
658int testing_test_fail(void)
659{
660 const char *func[WPA_TRACE_LEN];
661 size_t i, res, len;
662 char *pos, *next;
663 int match;
664
665 if (!wpa_trace_test_fail_after)
666 return 0;
667
668 res = wpa_trace_calling_func(func, WPA_TRACE_LEN);
669 i = 0;
670 if (i < res && os_strcmp(func[i], __func__) == 0)
671 i++;
672
673 pos = wpa_trace_test_fail_func;
674
675 match = 0;
676 while (i < res) {
677 int allow_skip = 1;
678 int maybe = 0;
679
680 if (*pos == '=') {
681 allow_skip = 0;
682 pos++;
683 } else if (*pos == '?') {
684 maybe = 1;
685 pos++;
686 }
687 next = os_strchr(pos, ';');
688 if (next)
689 len = next - pos;
690 else
691 len = os_strlen(pos);
692 if (os_memcmp(pos, func[i], len) != 0) {
693 if (maybe && next) {
694 pos = next + 1;
695 continue;
696 }
697 if (allow_skip) {
698 i++;
699 continue;
700 }
701 return 0;
702 }
703 if (!next) {
704 match = 1;
705 break;
706 }
707 pos = next + 1;
708 i++;
709 }
710 if (!match)
711 return 0;
712
713 wpa_trace_test_fail_after--;
714 if (wpa_trace_test_fail_after == 0) {
715 wpa_printf(MSG_INFO, "TESTING: fail at %s",
716 wpa_trace_test_fail_func);
717 for (i = 0; i < res; i++)
718 wpa_printf(MSG_INFO, "backtrace[%d] = %s",
719 (int) i, func[i]);
720 return 1;
721 }
722
723 return 0;
724}
725
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800726#else
727
728static inline int testing_fail_alloc(void)
729{
730 return 0;
731}
732#endif
733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734void * os_malloc(size_t size)
735{
736 struct os_alloc_trace *a;
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800737
738 if (testing_fail_alloc())
739 return NULL;
740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741 a = malloc(sizeof(*a) + size);
742 if (a == NULL)
743 return NULL;
744 a->magic = ALLOC_MAGIC;
745 dl_list_add(&alloc_list, &a->list);
746 a->len = size;
747 wpa_trace_record(a);
748 return a + 1;
749}
750
751
752void * os_realloc(void *ptr, size_t size)
753{
754 struct os_alloc_trace *a;
755 size_t copy_len;
756 void *n;
757
758 if (ptr == NULL)
759 return os_malloc(size);
760
761 a = (struct os_alloc_trace *) ptr - 1;
762 if (a->magic != ALLOC_MAGIC) {
763 wpa_printf(MSG_INFO, "REALLOC[%p]: invalid magic 0x%x%s",
764 a, a->magic,
765 a->magic == FREED_MAGIC ? " (already freed)" : "");
766 wpa_trace_show("Invalid os_realloc() call");
767 abort();
768 }
769 n = os_malloc(size);
770 if (n == NULL)
771 return NULL;
772 copy_len = a->len;
773 if (copy_len > size)
774 copy_len = size;
775 os_memcpy(n, a + 1, copy_len);
776 os_free(ptr);
777 return n;
778}
779
780
781void os_free(void *ptr)
782{
783 struct os_alloc_trace *a;
784
785 if (ptr == NULL)
786 return;
787 a = (struct os_alloc_trace *) ptr - 1;
788 if (a->magic != ALLOC_MAGIC) {
789 wpa_printf(MSG_INFO, "FREE[%p]: invalid magic 0x%x%s",
790 a, a->magic,
791 a->magic == FREED_MAGIC ? " (already freed)" : "");
792 wpa_trace_show("Invalid os_free() call");
793 abort();
794 }
795 dl_list_del(&a->list);
796 a->magic = FREED_MAGIC;
797
798 wpa_trace_check_ref(ptr);
799 free(a);
800}
801
802
803void * os_zalloc(size_t size)
804{
805 void *ptr = os_malloc(size);
806 if (ptr)
807 os_memset(ptr, 0, size);
808 return ptr;
809}
810
811
812char * os_strdup(const char *s)
813{
814 size_t len;
815 char *d;
816 len = os_strlen(s);
817 d = os_malloc(len + 1);
818 if (d == NULL)
819 return NULL;
820 os_memcpy(d, s, len);
821 d[len] = '\0';
822 return d;
823}
824
825#endif /* WPA_TRACE */
Jouni Malinen772e12c2014-10-07 10:29:35 -0700826
827
828int os_exec(const char *program, const char *arg, int wait_completion)
829{
830 pid_t pid;
831 int pid_status;
832
833 pid = fork();
834 if (pid < 0) {
835 perror("fork");
836 return -1;
837 }
838
839 if (pid == 0) {
840 /* run the external command in the child process */
841 const int MAX_ARG = 30;
842 char *_program, *_arg, *pos;
843 char *argv[MAX_ARG + 1];
844 int i;
845
846 _program = os_strdup(program);
847 _arg = os_strdup(arg);
848
849 argv[0] = _program;
850
851 i = 1;
852 pos = _arg;
853 while (i < MAX_ARG && pos && *pos) {
854 while (*pos == ' ')
855 pos++;
856 if (*pos == '\0')
857 break;
858 argv[i++] = pos;
859 pos = os_strchr(pos, ' ');
860 if (pos)
861 *pos++ = '\0';
862 }
863 argv[i] = NULL;
864
865 execv(program, argv);
866 perror("execv");
867 os_free(_program);
868 os_free(_arg);
869 exit(0);
870 return -1;
871 }
872
873 if (wait_completion) {
874 /* wait for the child process to complete in the parent */
875 waitpid(pid, &pid_status, 0);
876 }
877
878 return 0;
879}