[68456fa] | 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.hfa -- tools for using fork + exec |
---|
| 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Thu Oct 06 14:02:46 2022 |
---|
| 11 | // Last Modified By : |
---|
| 12 | // Last Modified On : |
---|
| 13 | // Update Count : |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #include <stdarg.h> // va_start, va_end |
---|
| 17 | #include <stdio.h> |
---|
| 18 | #include <stdlib.h> |
---|
| 19 | #include <string.h> |
---|
| 20 | |
---|
| 21 | #include <errno.h> |
---|
| 22 | #include <signal.h> |
---|
| 23 | |
---|
| 24 | extern "C" { |
---|
| 25 | #include <sys/types.h> |
---|
| 26 | #include <sys/wait.h> |
---|
| 27 | #include <unistd.h> |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | static int true_main(const char * exec, char * env[]); |
---|
| 31 | |
---|
| 32 | static int do_wait(pid_t pid) { |
---|
| 33 | int wstatus; |
---|
| 34 | int options = 0; |
---|
| 35 | pid_t ret = waitpid(pid, &wstatus, options); |
---|
| 36 | fflush(stdout); |
---|
| 37 | if(ret < 0) { |
---|
| 38 | fprintf(stderr, "Fork returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 39 | exit(1); |
---|
| 40 | } |
---|
| 41 | return wstatus; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | static pid_t strict_fork(void) { |
---|
| 45 | fflush(stdout); |
---|
| 46 | pid_t ret = fork(); |
---|
| 47 | if(ret < 0) { |
---|
| 48 | fprintf(stderr, "Fork returned with error: %d '%s'\n", errno, strerror(errno)); |
---|
| 49 | exit(1); |
---|
| 50 | } |
---|
| 51 | if(ret == 0) dup2(1, 2); |
---|
| 52 | return ret; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | static void print_status(int wstatus) { |
---|
| 56 | printf("Child status:\n"); |
---|
| 57 | printf(" WIFEXITED : %d\n", WIFEXITED(wstatus)); |
---|
| 58 | printf(" WEXITSTATUS : %d\n", WEXITSTATUS(wstatus)); |
---|
| 59 | printf(" WIFSIGNALED : %d\n", WIFSIGNALED(wstatus)); |
---|
| 60 | printf(" WTERMSIG : %d\n", WTERMSIG(wstatus)); |
---|
| 61 | printf(" WCOREDUMP : %d\n", WCOREDUMP(wstatus)); |
---|
| 62 | printf(" WIFSTOPPED : %d\n", WIFSTOPPED(wstatus)); |
---|
| 63 | printf(" WSTOPSIG : %d\n", WSTOPSIG(wstatus)); |
---|
| 64 | printf(" WIFCONTINUED: %d\n", WIFCONTINUED(wstatus)); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | static void check_main(const char * path) { |
---|
| 68 | if(getenv("CFATEST_FORK_EXEC_TEXT")) return; |
---|
| 69 | |
---|
| 70 | char * env[] = { "CFATEST_FORK_EXEC_TEXT=1", 0p }; |
---|
| 71 | exit( true_main(path, env) ); |
---|
| 72 | } |
---|