[a659b31] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // fork+exec.cfa -- Check that we can use fork+exec to test parameters. |
---|
| 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Wed Sep 27 10:44:38 2022 |
---|
| 11 | // Last Modified By : |
---|
| 12 | // Last Modified On : |
---|
| 13 | // Update Count : |
---|
| 14 | // |
---|
| 15 | #include <stdlib.h> |
---|
| 16 | #include <stdio.h> |
---|
| 17 | #include <string.h> |
---|
| 18 | |
---|
| 19 | #include <errno.h> |
---|
| 20 | #include <signal.h> |
---|
| 21 | |
---|
| 22 | extern "C" { |
---|
| 23 | #include <sys/types.h> |
---|
| 24 | #include <sys/wait.h> |
---|
| 25 | #include <unistd.h> |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | int true_main(const char * exec); |
---|
| 29 | |
---|
| 30 | int main(int argc, char * argv[]) { |
---|
| 31 | if(!getenv("CFATEST_FORK_EXEC_TEXT")) return true_main(argv[0]); |
---|
| 32 | |
---|
| 33 | printf("arguments are:\n"); |
---|
| 34 | if(argc == 1) printf(" None\n"); |
---|
| 35 | for(int i = 1; i < argc; i++) { |
---|
| 36 | printf(" '%s'\n", argv[i]); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | printf("Success!\n"); |
---|
| 40 | fflush(stdout); |
---|
| 41 | return 0; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | int do_wait(pid_t pid) { |
---|
| 45 | int wstatus; |
---|
| 46 | int options = 0; |
---|
| 47 | pid_t ret = waitpid(pid, &wstatus, options); |
---|
| 48 | fflush(stdout); |
---|
| 49 | if(ret < 0) { |
---|
| 50 | fprintf(stderr, "Fork returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 51 | exit(1); |
---|
| 52 | } |
---|
| 53 | return wstatus; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | pid_t strict_fork(void) { |
---|
| 57 | fflush(stdout); |
---|
| 58 | pid_t ret = fork(); |
---|
| 59 | if(ret < 0) { |
---|
| 60 | fprintf(stderr, "Fork returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 61 | exit(1); |
---|
| 62 | } |
---|
| 63 | return ret; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | void print_status(int wstatus) { |
---|
| 67 | printf("Child status:\n"); |
---|
| 68 | printf(" WIFEXITED : %d\n", WIFEXITED(wstatus)); |
---|
| 69 | printf(" WEXITSTATUS : %d\n", WEXITSTATUS(wstatus)); |
---|
| 70 | printf(" WIFSIGNALED : %d\n", WIFSIGNALED(wstatus)); |
---|
| 71 | printf(" WTERMSIG : %d\n", WTERMSIG(wstatus)); |
---|
| 72 | printf(" WCOREDUMP : %d\n", WCOREDUMP(wstatus)); |
---|
| 73 | printf(" WIFSTOPPED : %d\n", WIFSTOPPED(wstatus)); |
---|
| 74 | printf(" WSTOPSIG : %d\n", WSTOPSIG(wstatus)); |
---|
| 75 | printf(" WIFCONTINUED: %d\n", WIFCONTINUED(wstatus)); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | int true_main(const char * path) { |
---|
| 79 | char * env[] = { "CFATEST_FORK_EXEC_TEXT=1", 0p }; |
---|
| 80 | |
---|
| 81 | printf("no arg:\n"); |
---|
| 82 | if(pid_t child = strict_fork(); child == 0) { |
---|
| 83 | int ret = execle(path, path, (const char*)0p, env); |
---|
| 84 | if(ret < 0) { |
---|
| 85 | fprintf(stderr, "Execl 1 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 86 | exit(1); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | else { |
---|
| 90 | int status = do_wait(child); |
---|
| 91 | print_status(status); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | printf("1 arg:\n"); |
---|
| 95 | if(pid_t child = strict_fork(); child == 0) { |
---|
| 96 | int ret = execle(path, path, "Hello World!", (const char*)0p, env); |
---|
| 97 | if(ret < 0) { |
---|
| 98 | fprintf(stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 99 | exit(1); |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | else { |
---|
| 103 | int status = do_wait(child); |
---|
| 104 | print_status(status); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | printf("5 arg:\n"); |
---|
| 108 | if(pid_t child = strict_fork(); child == 0) { |
---|
| 109 | int ret = execle(path, path, "Hi,", "my", "name", "is", "Fred", (const char*)0p, env); |
---|
| 110 | if(ret < 0) { |
---|
| 111 | fprintf(stderr, "Execl 3 returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 112 | exit(1); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | else { |
---|
| 116 | int status = do_wait(child); |
---|
| 117 | print_status(status); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | printf("All Done!\n"); |
---|
| 121 | |
---|
| 122 | return 0; |
---|
| 123 | } |
---|