Changeset 1743 for zzuf/trunk/src/zzuf.c
- Timestamp:
- Feb 9, 2007, 2:33:02 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
zzuf/trunk/src/zzuf.c
r1742 r1743 91 91 92 92 static void loop_stdin(struct opts *); 93 static int run_process( char const *, char *[]);93 static int run_process(struct opts *, int[][2]); 94 94 95 95 static void spawn_children(struct opts *); … … 328 328 if(myoptind >= argc) 329 329 { 330 if(opts->verbose) 331 { 332 finfo(stderr, opts, opts->seed); 333 fprintf(stderr, "reading from stdin\n"); 334 } 335 330 336 if(opts->endseed != opts->seed + 1) 331 337 { … … 530 536 static void spawn_children(struct opts *opts) 531 537 { 532 static int const files[] = { DEBUG_FILENO, STDERR_FILENO, STDOUT_FILENO };533 char buf[64];534 538 int fd[3][2]; 535 539 int64_t now = _zz_time(); … … 570 574 } 571 575 572 #if defined HAVE_FORK 573 /* Fork and launch child */ 574 pid = fork(); 575 if(pid < -1) 576 { 577 perror("fork"); 576 pid = run_process(opts, fd); 577 if(pid < 0) 578 578 return; 579 }580 #else581 pid = 0;582 #endif583 584 if(pid == 0)585 {586 #if defined HAVE_SETRLIMIT587 if(opts->maxmem >= 0)588 {589 struct rlimit rlim;590 rlim.rlim_cur = opts->maxmem * 1000000;591 rlim.rlim_max = opts->maxmem * 1000000;592 setrlimit(ZZUF_RLIMIT_CONST, &rlim);593 }594 #endif595 596 #if defined HAVE_FORK597 /* We loop in reverse order so that files[0] is done last,598 * just in case one of the other dup2()ed fds had the value */599 for(j = 3; j--; )600 {601 close(fd[j][0]);602 if(fd[j][1] != files[j])603 {604 dup2(fd[j][1], files[j]);605 close(fd[j][1]);606 }607 }608 #endif609 610 /* Set environment variables */611 sprintf(buf, "%i", opts->seed);612 setenv("ZZUF_SEED", buf, 1);613 sprintf(buf, "%g", opts->minratio);614 setenv("ZZUF_MINRATIO", buf, 1);615 sprintf(buf, "%g", opts->maxratio);616 setenv("ZZUF_MAXRATIO", buf, 1);617 618 #if defined HAVE_FORK619 if(run_process(opts->oldargv[0], opts->newargv) < 0)620 exit(EXIT_FAILURE);621 exit(EXIT_SUCCESS);622 #else623 if(run_process(opts->oldargv[0], opts->newargv) < 0)624 return;625 #endif626 }627 579 628 580 /* We’re the parent, acknowledge spawn */ … … 881 833 #endif 882 834 883 static int run_process(char const *zzuf_exe, char *argv[]) 884 { 835 static int run_process(struct opts *opts, int fd[][2]) 836 { 837 char buf[64]; 885 838 #if defined HAVE_FORK 839 static int const files[] = { DEBUG_FILENO, STDERR_FILENO, STDOUT_FILENO }; 886 840 char *libpath, *tmp; 887 int ret, len = strlen(zzuf_exe);841 int pid, j, len = strlen(opts->oldargv[0]); 888 842 # if defined __APPLE__ 889 843 # define FILENAME "libzzuf.dylib" … … 900 854 # define PRELOAD "LD_PRELOAD" 901 855 # endif 902 856 #elif HAVE_WINDOWS_H 857 PROCESS_INFORMATION pinfo; 858 STARTUPINFO sinfo; 859 HANDLE pid; 860 void *epaddr; 861 #endif 862 int ret; 863 864 #if defined HAVE_FORK 865 /* Fork and launch child */ 866 pid = fork(); 867 if(pid < -1) 868 perror("fork"); 869 if(pid != 0) 870 return pid; 871 872 /* We loop in reverse order so that files[0] is done last, 873 * just in case one of the other dup2()ed fds had the value */ 874 for(j = 3; j--; ) 875 { 876 close(fd[j][0]); 877 if(fd[j][1] != files[j]) 878 { 879 dup2(fd[j][1], files[j]); 880 close(fd[j][1]); 881 } 882 } 883 #endif 884 885 #if defined HAVE_SETRLIMIT 886 if(opts->maxmem >= 0) 887 { 888 struct rlimit rlim; 889 rlim.rlim_cur = opts->maxmem * 1000000; 890 rlim.rlim_max = opts->maxmem * 1000000; 891 setrlimit(ZZUF_RLIMIT_CONST, &rlim); 892 } 893 #endif 894 895 /* Set environment variables */ 896 sprintf(buf, "%i", opts->seed); 897 setenv("ZZUF_SEED", buf, 1); 898 sprintf(buf, "%g", opts->minratio); 899 setenv("ZZUF_MINRATIO", buf, 1); 900 sprintf(buf, "%g", opts->maxratio); 901 setenv("ZZUF_MAXRATIO", buf, 1); 902 903 #if defined HAVE_FORK 903 904 libpath = malloc(len + strlen("/.libs/" FILENAME EXTRAINFO) + 1); 904 strcpy(libpath, zzuf_exe);905 strcpy(libpath, opts->oldargv[0]); 905 906 906 907 tmp = strrchr(libpath, '/'); … … 915 916 free(libpath); 916 917 917 if(execvp(argv[0], argv)) 918 { 919 perror(argv[0]); 920 return -1; 921 } 922 918 if(execvp(opts->newargv[0], opts->newargv)) 919 { 920 perror(opts->newargv[0]); 921 exit(EXIT_FAILURE); 922 } 923 924 exit(EXIT_SUCCESS); 925 /* no return */ 923 926 return 0; 924 927 #elif HAVE_WINDOWS_H 925 PROCESS_INFORMATION pinfo; 926 STARTUPINFO sinfo; 927 void *epaddr; 928 int ret; 928 pid = GetCurrentProcess(); 929 929 930 930 /* Get entry point */ 931 epaddr = get_entry( argv[0]);931 epaddr = get_entry(opts->newargv[0]); 932 932 if(!epaddr) 933 933 return -1; … … 935 935 memset(&sinfo, 0, sizeof(sinfo)); 936 936 sinfo.cb = sizeof(sinfo); 937 ret = CreateProcess(NULL, argv[0], NULL, NULL, FALSE, 937 DuplicateHandle(pid, (HANDLE)_get_osfhandle(fd[j][0]), pid, 938 /* FIXME */ &sinfo.hStdInput, 0, TRUE, DUPLICATE_SAME_ACCESS); 939 DuplicateHandle(pid, (HANDLE)_get_osfhandle(fd[j][1]), pid, 940 &sinfo.hStdError, 0, TRUE, DUPLICATE_SAME_ACCESS); 941 DuplicateHandle(pid, (HANDLE)_get_osfhandle(fd[j][2]), pid, 942 &sinfo.hStdOutput, 0, TRUE, DUPLICATE_SAME_ACCESS); 943 ret = CreateProcess(NULL, opts->newargv[0], NULL, NULL, FALSE, 938 944 CREATE_SUSPENDED, NULL, NULL, &sinfo, &pinfo); 939 945 if(!ret) … … 955 961 } 956 962 957 return 0; 958 #else 959 return -1; 963 return (long int)pinfo.hProcess; 960 964 #endif 961 965 }
Note: See TracChangeset
for help on using the changeset viewer.