1 #include <assert.h> 2 #include <stdio.h> 3 #include <string.h> 4 #include <sys/types.h> 5 #include <sys/wait.h> 6 #include <unistd.h> 7 8 extern char **environ; 9 10 #define S(...) (fprintf(stdout, __VA_ARGS__),fflush(stdout)) 11 #define FORKEXECWAIT(exec_call) do { \ 12 int status;\ 13 pid_t child = fork(); \ 14 if (child == 0) {exec_call; perror ("exec failed");} \ 15 else if (child == -1) perror ("cannot fork\n"); \ 16 else if (child != wait (&status)) perror ("error waiting child"); \ 17 else S("child exited\n"); \ 18 } while (0) 19 20 void test_allexec (char *exec) 21 { 22 FORKEXECWAIT (execlp(exec, exec, NULL)); 23 FORKEXECWAIT (execlp(exec, exec, "constant_arg1", "constant_arg2", NULL)); 24 FORKEXECWAIT (execve(exec, NULL, environ)); 25 } 26 27 28 /* If a single argument "exec" is given, will execute itself 29 (in bi-arch, a 32 bit and 64 bit variant) via various exec system calls. 30 Note that this test can only be run after the prerequisite have been 31 prepared by allexec_prepare_prereq, which will a.o. make links 32 for the allexec32 and allexec64 executables. On single arch build, 33 these links points to the same executable to ensure this test works 34 everywhere the same. 35 No arguments or more arguments means just print its args. */ 36 int main(int argc, char **argv, char **envp) 37 { 38 if ( (argc == 2) && (strcmp (argv[1], "exec") == 0)) { 39 S("%s will exec ./allexec32\n", argv[0]); 40 test_allexec ("./allexec32"); 41 S("%s will exec ./allexec64\n", argv[0]); 42 test_allexec ("./allexec64"); 43 } else { 44 int i; 45 S("program exec-ed:"); 46 for (i = 0; i < argc; i++) S(" %s", argv[i]); 47 S("\n"); 48 } 49 return 0; 50 } 51