blob: 26ce50d1e65742bbf32a08ed1e141557cdd68372 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * OS specific functions for UNIX/POSIX systems
3 * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
4 *
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;
44 struct dl_list list;
45 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{
54 if (sec)
55 sleep(sec);
56 if (usec)
57 usleep(usec);
58}
59
60
61int os_get_time(struct os_time *t)
62{
63 int res;
64 struct timeval tv;
65 res = gettimeofday(&tv, NULL);
66 t->sec = tv.tv_sec;
67 t->usec = tv.tv_usec;
68 return res;
69}
70
71
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -080072int os_get_reltime(struct os_reltime *t)
73{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080074#ifndef __MACH__
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -080075#if defined(CLOCK_BOOTTIME)
76 static clockid_t clock_id = CLOCK_BOOTTIME;
77#elif defined(CLOCK_MONOTONIC)
78 static clockid_t clock_id = CLOCK_MONOTONIC;
79#else
80 static clockid_t clock_id = CLOCK_REALTIME;
81#endif
82 struct timespec ts;
83 int res;
84
Paul Stewart092955c2017-02-06 09:13:09 -080085 if (TEST_FAIL())
86 return -1;
87
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -080088 while (1) {
89 res = clock_gettime(clock_id, &ts);
90 if (res == 0) {
91 t->sec = ts.tv_sec;
92 t->usec = ts.tv_nsec / 1000;
93 return 0;
94 }
95 switch (clock_id) {
96#ifdef CLOCK_BOOTTIME
97 case CLOCK_BOOTTIME:
98 clock_id = CLOCK_MONOTONIC;
99 break;
100#endif
101#ifdef CLOCK_MONOTONIC
102 case CLOCK_MONOTONIC:
103 clock_id = CLOCK_REALTIME;
104 break;
105#endif
106 case CLOCK_REALTIME:
107 return -1;
108 }
109 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800110#else /* __MACH__ */
111 uint64_t abstime, nano;
112 static mach_timebase_info_data_t info = { 0, 0 };
113
114 if (!info.denom) {
115 if (mach_timebase_info(&info) != KERN_SUCCESS)
116 return -1;
117 }
118
119 abstime = mach_absolute_time();
120 nano = (abstime * info.numer) / info.denom;
121
122 t->sec = nano / NSEC_PER_SEC;
123 t->usec = (nano - (((uint64_t) t->sec) * NSEC_PER_SEC)) / NSEC_PER_USEC;
124
125 return 0;
126#endif /* __MACH__ */
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800127}
128
129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130int os_mktime(int year, int month, int day, int hour, int min, int sec,
131 os_time_t *t)
132{
133 struct tm tm, *tm1;
134 time_t t_local, t1, t2;
135 os_time_t tz_offset;
136
137 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
138 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
139 sec > 60)
140 return -1;
141
142 memset(&tm, 0, sizeof(tm));
143 tm.tm_year = year - 1900;
144 tm.tm_mon = month - 1;
145 tm.tm_mday = day;
146 tm.tm_hour = hour;
147 tm.tm_min = min;
148 tm.tm_sec = sec;
149
150 t_local = mktime(&tm);
151
152 /* figure out offset to UTC */
153 tm1 = localtime(&t_local);
154 if (tm1) {
155 t1 = mktime(tm1);
156 tm1 = gmtime(&t_local);
157 if (tm1) {
158 t2 = mktime(tm1);
159 tz_offset = t2 - t1;
160 } else
161 tz_offset = 0;
162 } else
163 tz_offset = 0;
164
165 *t = (os_time_t) t_local - tz_offset;
166 return 0;
167}
168
169
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800170int os_gmtime(os_time_t t, struct os_tm *tm)
171{
172 struct tm *tm2;
173 time_t t2 = t;
174
175 tm2 = gmtime(&t2);
176 if (tm2 == NULL)
177 return -1;
178 tm->sec = tm2->tm_sec;
179 tm->min = tm2->tm_min;
180 tm->hour = tm2->tm_hour;
181 tm->day = tm2->tm_mday;
182 tm->month = tm2->tm_mon + 1;
183 tm->year = tm2->tm_year + 1900;
184 return 0;
185}
186
187
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188#ifdef __APPLE__
189#include <fcntl.h>
190static int os_daemon(int nochdir, int noclose)
191{
192 int devnull;
193
194 if (chdir("/") < 0)
195 return -1;
196
197 devnull = open("/dev/null", O_RDWR);
198 if (devnull < 0)
199 return -1;
200
201 if (dup2(devnull, STDIN_FILENO) < 0) {
202 close(devnull);
203 return -1;
204 }
205
206 if (dup2(devnull, STDOUT_FILENO) < 0) {
207 close(devnull);
208 return -1;
209 }
210
211 if (dup2(devnull, STDERR_FILENO) < 0) {
212 close(devnull);
213 return -1;
214 }
215
216 return 0;
217}
218#else /* __APPLE__ */
219#define os_daemon daemon
220#endif /* __APPLE__ */
221
222
223int os_daemonize(const char *pid_file)
224{
225#if defined(__uClinux__) || defined(__sun__)
226 return -1;
227#else /* defined(__uClinux__) || defined(__sun__) */
228 if (os_daemon(0, 0)) {
229 perror("daemon");
230 return -1;
231 }
232
233 if (pid_file) {
234 FILE *f = fopen(pid_file, "w");
235 if (f) {
236 fprintf(f, "%u\n", getpid());
237 fclose(f);
238 }
239 }
240
241 return -0;
242#endif /* defined(__uClinux__) || defined(__sun__) */
243}
244
245
246void os_daemonize_terminate(const char *pid_file)
247{
248 if (pid_file)
249 unlink(pid_file);
250}
251
252
253int os_get_random(unsigned char *buf, size_t len)
254{
255 FILE *f;
256 size_t rc;
257
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800258 if (TEST_FAIL())
259 return -1;
260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 f = fopen("/dev/urandom", "rb");
262 if (f == NULL) {
263 printf("Could not open /dev/urandom.\n");
264 return -1;
265 }
266
267 rc = fread(buf, 1, len, f);
268 fclose(f);
269
270 return rc != len ? -1 : 0;
271}
272
273
274unsigned long os_random(void)
275{
276 return random();
277}
278
279
280char * os_rel2abs_path(const char *rel_path)
281{
282 char *buf = NULL, *cwd, *ret;
283 size_t len = 128, cwd_len, rel_len, ret_len;
284 int last_errno;
285
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700286 if (!rel_path)
287 return NULL;
288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 if (rel_path[0] == '/')
290 return os_strdup(rel_path);
291
292 for (;;) {
293 buf = os_malloc(len);
294 if (buf == NULL)
295 return NULL;
296 cwd = getcwd(buf, len);
297 if (cwd == NULL) {
298 last_errno = errno;
299 os_free(buf);
300 if (last_errno != ERANGE)
301 return NULL;
302 len *= 2;
303 if (len > 2000)
304 return NULL;
305 } else {
306 buf[len - 1] = '\0';
307 break;
308 }
309 }
310
311 cwd_len = os_strlen(cwd);
312 rel_len = os_strlen(rel_path);
313 ret_len = cwd_len + 1 + rel_len + 1;
314 ret = os_malloc(ret_len);
315 if (ret) {
316 os_memcpy(ret, cwd, cwd_len);
317 ret[cwd_len] = '/';
318 os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
319 ret[ret_len - 1] = '\0';
320 }
321 os_free(buf);
322 return ret;
323}
324
325
326int os_program_init(void)
327{
328#ifdef ANDROID
Yifan Honge0317902017-07-20 14:15:22 -0700329 struct __user_cap_header_struct header;
330 struct __user_cap_data_struct cap;
331 struct group *grp = getgrnam("wifi");
332 gid_t gid_wifi = grp ? grp->gr_gid : 0;
333 struct passwd *pwd = getpwnam("wifi");
334 uid_t uid_wifi = pwd ? pwd->pw_uid : 0;
335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336 /*
337 * We ignore errors here since errors are normal if we
338 * are already running as non-root.
339 */
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800340#ifdef ANDROID_SETGROUPS_OVERRIDE
341 gid_t groups[] = { ANDROID_SETGROUPS_OVERRIDE };
Yifan Honge0317902017-07-20 14:15:22 -0700342
343 if (!gid_wifi || !uid_wifi) return -1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800344#else /* ANDROID_SETGROUPS_OVERRIDE */
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000345 gid_t groups[4];
346 int group_idx = 0;
Yifan Honge0317902017-07-20 14:15:22 -0700347
348 if (!gid_wifi || !uid_wifi) return -1;
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000349 groups[group_idx] = gid_wifi;
Yifan Honge0317902017-07-20 14:15:22 -0700350
351 grp = getgrnam("inet");
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000352 groups[++group_idx] = grp ? grp->gr_gid : 0;
353 if (!groups[group_idx]) return -1;
Yifan Honge0317902017-07-20 14:15:22 -0700354
355 grp = getgrnam("keystore");
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000356 groups[++group_idx] = grp ? grp->gr_gid : 0;
357 if (!groups[group_idx]) return -1;
358
359 grp = getgrnam("log");
360 groups[++group_idx] = grp ? grp->gr_gid : 0;
361 if (!groups[group_idx]) group_idx--;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800362#endif /* ANDROID_SETGROUPS_OVERRIDE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363
Pavel Grafov4d8552e2018-02-06 11:28:29 +0000364 setgroups(group_idx + 1, groups);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365
366 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
367
Yifan Honge0317902017-07-20 14:15:22 -0700368 setgid(gid_wifi);
369 setuid(uid_wifi);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370
371 header.version = _LINUX_CAPABILITY_VERSION;
372 header.pid = 0;
373 cap.effective = cap.permitted =
374 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW);
375 cap.inheritable = 0;
376 capset(&header, &cap);
377#endif /* ANDROID */
378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700379 return 0;
380}
381
382
383void os_program_deinit(void)
384{
385#ifdef WPA_TRACE
386 struct os_alloc_trace *a;
387 unsigned long total = 0;
388 dl_list_for_each(a, &alloc_list, struct os_alloc_trace, list) {
389 total += a->len;
390 if (a->magic != ALLOC_MAGIC) {
391 wpa_printf(MSG_INFO, "MEMLEAK[%p]: invalid magic 0x%x "
392 "len %lu",
393 a, a->magic, (unsigned long) a->len);
394 continue;
395 }
396 wpa_printf(MSG_INFO, "MEMLEAK[%p]: len %lu",
397 a, (unsigned long) a->len);
398 wpa_trace_dump("memleak", a);
399 }
400 if (total)
401 wpa_printf(MSG_INFO, "MEMLEAK: total %lu bytes",
402 (unsigned long) total);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800403 wpa_trace_deinit();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404#endif /* WPA_TRACE */
405}
406
407
408int os_setenv(const char *name, const char *value, int overwrite)
409{
410 return setenv(name, value, overwrite);
411}
412
413
414int os_unsetenv(const char *name)
415{
416#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) || \
417 defined(__OpenBSD__)
418 unsetenv(name);
419 return 0;
420#else
421 return unsetenv(name);
422#endif
423}
424
425
426char * os_readfile(const char *name, size_t *len)
427{
428 FILE *f;
429 char *buf;
430 long pos;
431
432 f = fopen(name, "rb");
433 if (f == NULL)
434 return NULL;
435
436 if (fseek(f, 0, SEEK_END) < 0 || (pos = ftell(f)) < 0) {
437 fclose(f);
438 return NULL;
439 }
440 *len = pos;
441 if (fseek(f, 0, SEEK_SET) < 0) {
442 fclose(f);
443 return NULL;
444 }
445
446 buf = os_malloc(*len);
447 if (buf == NULL) {
448 fclose(f);
449 return NULL;
450 }
451
452 if (fread(buf, 1, *len, f) != *len) {
453 fclose(f);
454 os_free(buf);
455 return NULL;
456 }
457
458 fclose(f);
459
460 return buf;
461}
462
463
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700464int os_file_exists(const char *fname)
465{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700466 return access(fname, F_OK) == 0;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700467}
468
469
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800470int os_fdatasync(FILE *stream)
Mitchell Wills447c7ff2015-08-24 17:24:30 -0700471{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800472 if (!fflush(stream)) {
473#ifdef __linux__
474 return fdatasync(fileno(stream));
475#else /* !__linux__ */
476#ifdef F_FULLFSYNC
477 /* OS X does not implement fdatasync(). */
478 return fcntl(fileno(stream), F_FULLFSYNC);
479#else /* F_FULLFSYNC */
Mitchell Wills447c7ff2015-08-24 17:24:30 -0700480 return fsync(fileno(stream));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800481#endif /* F_FULLFSYNC */
482#endif /* __linux__ */
483 }
484
Mitchell Wills447c7ff2015-08-24 17:24:30 -0700485 return -1;
486}
487
488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700489#ifndef WPA_TRACE
490void * os_zalloc(size_t size)
491{
492 return calloc(1, size);
493}
494#endif /* WPA_TRACE */
495
496
497size_t os_strlcpy(char *dest, const char *src, size_t siz)
498{
499 const char *s = src;
500 size_t left = siz;
501
502 if (left) {
503 /* Copy string up to the maximum size of the dest buffer */
504 while (--left != 0) {
505 if ((*dest++ = *s++) == '\0')
506 break;
507 }
508 }
509
510 if (left == 0) {
511 /* Not enough room for the string; force NUL-termination */
512 if (siz != 0)
513 *dest = '\0';
514 while (*s++)
515 ; /* determine total src string length */
516 }
517
518 return s - src - 1;
519}
520
521
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700522int os_memcmp_const(const void *a, const void *b, size_t len)
523{
524 const u8 *aa = a;
525 const u8 *bb = b;
526 size_t i;
527 u8 res;
528
529 for (res = 0, i = 0; i < len; i++)
530 res |= aa[i] ^ bb[i];
531
532 return res;
533}
534
535
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700536void * os_memdup(const void *src, size_t len)
537{
538 void *r = os_malloc(len);
539
540 if (r)
541 os_memcpy(r, src, len);
542 return r;
543}
544
545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546#ifdef WPA_TRACE
547
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800548#if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS)
549char wpa_trace_fail_func[256] = { 0 };
550unsigned int wpa_trace_fail_after;
551
552static int testing_fail_alloc(void)
553{
554 const char *func[WPA_TRACE_LEN];
555 size_t i, res, len;
556 char *pos, *next;
557 int match;
558
559 if (!wpa_trace_fail_after)
560 return 0;
561
562 res = wpa_trace_calling_func(func, WPA_TRACE_LEN);
563 i = 0;
564 if (i < res && os_strcmp(func[i], __func__) == 0)
565 i++;
566 if (i < res && os_strcmp(func[i], "os_malloc") == 0)
567 i++;
568 if (i < res && os_strcmp(func[i], "os_zalloc") == 0)
569 i++;
570 if (i < res && os_strcmp(func[i], "os_calloc") == 0)
571 i++;
572 if (i < res && os_strcmp(func[i], "os_realloc") == 0)
573 i++;
574 if (i < res && os_strcmp(func[i], "os_realloc_array") == 0)
575 i++;
576 if (i < res && os_strcmp(func[i], "os_strdup") == 0)
577 i++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700578 if (i < res && os_strcmp(func[i], "os_memdup") == 0)
579 i++;
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800580
581 pos = wpa_trace_fail_func;
582
583 match = 0;
584 while (i < res) {
585 int allow_skip = 1;
586 int maybe = 0;
587
588 if (*pos == '=') {
589 allow_skip = 0;
590 pos++;
591 } else if (*pos == '?') {
592 maybe = 1;
593 pos++;
594 }
595 next = os_strchr(pos, ';');
596 if (next)
597 len = next - pos;
598 else
599 len = os_strlen(pos);
600 if (os_memcmp(pos, func[i], len) != 0) {
601 if (maybe && next) {
602 pos = next + 1;
603 continue;
604 }
605 if (allow_skip) {
606 i++;
607 continue;
608 }
609 return 0;
610 }
611 if (!next) {
612 match = 1;
613 break;
614 }
615 pos = next + 1;
616 i++;
617 }
618 if (!match)
619 return 0;
620
621 wpa_trace_fail_after--;
622 if (wpa_trace_fail_after == 0) {
623 wpa_printf(MSG_INFO, "TESTING: fail allocation at %s",
624 wpa_trace_fail_func);
625 for (i = 0; i < res; i++)
626 wpa_printf(MSG_INFO, "backtrace[%d] = %s",
627 (int) i, func[i]);
628 return 1;
629 }
630
631 return 0;
632}
633
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800634
635char wpa_trace_test_fail_func[256] = { 0 };
636unsigned int wpa_trace_test_fail_after;
637
638int testing_test_fail(void)
639{
640 const char *func[WPA_TRACE_LEN];
641 size_t i, res, len;
642 char *pos, *next;
643 int match;
644
645 if (!wpa_trace_test_fail_after)
646 return 0;
647
648 res = wpa_trace_calling_func(func, WPA_TRACE_LEN);
649 i = 0;
650 if (i < res && os_strcmp(func[i], __func__) == 0)
651 i++;
652
653 pos = wpa_trace_test_fail_func;
654
655 match = 0;
656 while (i < res) {
657 int allow_skip = 1;
658 int maybe = 0;
659
660 if (*pos == '=') {
661 allow_skip = 0;
662 pos++;
663 } else if (*pos == '?') {
664 maybe = 1;
665 pos++;
666 }
667 next = os_strchr(pos, ';');
668 if (next)
669 len = next - pos;
670 else
671 len = os_strlen(pos);
672 if (os_memcmp(pos, func[i], len) != 0) {
673 if (maybe && next) {
674 pos = next + 1;
675 continue;
676 }
677 if (allow_skip) {
678 i++;
679 continue;
680 }
681 return 0;
682 }
683 if (!next) {
684 match = 1;
685 break;
686 }
687 pos = next + 1;
688 i++;
689 }
690 if (!match)
691 return 0;
692
693 wpa_trace_test_fail_after--;
694 if (wpa_trace_test_fail_after == 0) {
695 wpa_printf(MSG_INFO, "TESTING: fail at %s",
696 wpa_trace_test_fail_func);
697 for (i = 0; i < res; i++)
698 wpa_printf(MSG_INFO, "backtrace[%d] = %s",
699 (int) i, func[i]);
700 return 1;
701 }
702
703 return 0;
704}
705
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800706#else
707
708static inline int testing_fail_alloc(void)
709{
710 return 0;
711}
712#endif
713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714void * os_malloc(size_t size)
715{
716 struct os_alloc_trace *a;
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800717
718 if (testing_fail_alloc())
719 return NULL;
720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721 a = malloc(sizeof(*a) + size);
722 if (a == NULL)
723 return NULL;
724 a->magic = ALLOC_MAGIC;
725 dl_list_add(&alloc_list, &a->list);
726 a->len = size;
727 wpa_trace_record(a);
728 return a + 1;
729}
730
731
732void * os_realloc(void *ptr, size_t size)
733{
734 struct os_alloc_trace *a;
735 size_t copy_len;
736 void *n;
737
738 if (ptr == NULL)
739 return os_malloc(size);
740
741 a = (struct os_alloc_trace *) ptr - 1;
742 if (a->magic != ALLOC_MAGIC) {
743 wpa_printf(MSG_INFO, "REALLOC[%p]: invalid magic 0x%x%s",
744 a, a->magic,
745 a->magic == FREED_MAGIC ? " (already freed)" : "");
746 wpa_trace_show("Invalid os_realloc() call");
747 abort();
748 }
749 n = os_malloc(size);
750 if (n == NULL)
751 return NULL;
752 copy_len = a->len;
753 if (copy_len > size)
754 copy_len = size;
755 os_memcpy(n, a + 1, copy_len);
756 os_free(ptr);
757 return n;
758}
759
760
761void os_free(void *ptr)
762{
763 struct os_alloc_trace *a;
764
765 if (ptr == NULL)
766 return;
767 a = (struct os_alloc_trace *) ptr - 1;
768 if (a->magic != ALLOC_MAGIC) {
769 wpa_printf(MSG_INFO, "FREE[%p]: invalid magic 0x%x%s",
770 a, a->magic,
771 a->magic == FREED_MAGIC ? " (already freed)" : "");
772 wpa_trace_show("Invalid os_free() call");
773 abort();
774 }
775 dl_list_del(&a->list);
776 a->magic = FREED_MAGIC;
777
778 wpa_trace_check_ref(ptr);
779 free(a);
780}
781
782
783void * os_zalloc(size_t size)
784{
785 void *ptr = os_malloc(size);
786 if (ptr)
787 os_memset(ptr, 0, size);
788 return ptr;
789}
790
791
792char * os_strdup(const char *s)
793{
794 size_t len;
795 char *d;
796 len = os_strlen(s);
797 d = os_malloc(len + 1);
798 if (d == NULL)
799 return NULL;
800 os_memcpy(d, s, len);
801 d[len] = '\0';
802 return d;
803}
804
805#endif /* WPA_TRACE */
Jouni Malinen772e12c2014-10-07 10:29:35 -0700806
807
808int os_exec(const char *program, const char *arg, int wait_completion)
809{
810 pid_t pid;
811 int pid_status;
812
813 pid = fork();
814 if (pid < 0) {
815 perror("fork");
816 return -1;
817 }
818
819 if (pid == 0) {
820 /* run the external command in the child process */
821 const int MAX_ARG = 30;
822 char *_program, *_arg, *pos;
823 char *argv[MAX_ARG + 1];
824 int i;
825
826 _program = os_strdup(program);
827 _arg = os_strdup(arg);
828
829 argv[0] = _program;
830
831 i = 1;
832 pos = _arg;
833 while (i < MAX_ARG && pos && *pos) {
834 while (*pos == ' ')
835 pos++;
836 if (*pos == '\0')
837 break;
838 argv[i++] = pos;
839 pos = os_strchr(pos, ' ');
840 if (pos)
841 *pos++ = '\0';
842 }
843 argv[i] = NULL;
844
845 execv(program, argv);
846 perror("execv");
847 os_free(_program);
848 os_free(_arg);
849 exit(0);
850 return -1;
851 }
852
853 if (wait_completion) {
854 /* wait for the child process to complete in the parent */
855 waitpid(pid, &pid_status, 0);
856 }
857
858 return 0;
859}