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 * path, const 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("IFEXITED : %d, ", WIFEXITED(wstatus)); |
---|
58 | printf("EXITSTATUS : %d, ", WEXITSTATUS(wstatus)); |
---|
59 | printf("IFSIGNALED : %d, ", WIFSIGNALED(wstatus)); |
---|
60 | printf("TERMSIG : %d, ", WTERMSIG(wstatus)); |
---|
61 | printf("COREDUMP : %d, ", WCOREDUMP(wstatus)); |
---|
62 | printf("IFSTOPPED : %d, ", WIFSTOPPED(wstatus)); |
---|
63 | printf("STOPSIG : %d, ", WSTOPSIG(wstatus)); |
---|
64 | printf("IFCONTINUED: %d", WIFCONTINUED(wstatus)); |
---|
65 | printf("\n"); |
---|
66 | printf("\n"); |
---|
67 | } |
---|
68 | |
---|
69 | static void check_main(const char * path) { |
---|
70 | if(getenv("CFATEST_FORK_EXEC_TEXT")) return; |
---|
71 | |
---|
72 | const char * env[] = { "CFATEST_FORK_EXEC_TEXT=1", (char*)0 }; |
---|
73 | exit( true_main(path, env) ); |
---|
74 | } |
---|