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