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