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