1 /*
2     Copyright (c) 2005-2020 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #if __APPLE__
18 
19 #define HARNESS_CUSTOM_MAIN 1
20 #include "harness.h"
21 #include <cstdlib>
22 #include "tbb/task_scheduler_init.h"
23 
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <errno.h>
29 
exec_test(const char * self)30 bool exec_test(const char *self) {
31     int status = 1;
32     pid_t p = fork();
33     if(p < 0) {
34         REPORT("fork error: errno=%d: %s\n", errno, strerror(errno));
35         return true;
36     }
37     else if(p) { // parent
38         if(waitpid(p, &status, 0) != p) {
39             REPORT("wait error: errno=%d: %s\n", errno, strerror(errno));
40             return true;
41         }
42         if(WIFEXITED(status)) {
43             if(!WEXITSTATUS(status)) return false; // ok
44             else REPORT("child has exited with return code 0x%x\n", WEXITSTATUS(status));
45         } else {
46             REPORT("child error 0x%x:%s%s ", status, WIFSIGNALED(status)?" signalled":"",
47                 WIFSTOPPED(status)?" stopped":"");
48             if(WIFSIGNALED(status))
49                 REPORT("%s%s", sys_siglist[WTERMSIG(status)], WCOREDUMP(status)?" core dumped":"");
50             if(WIFSTOPPED(status))
51                 REPORT("with %d stop-code", WSTOPSIG(status));
52             REPORT("\n");
53         }
54     }
55     else { // child
56         // reproduces error much often
57         execl(self, self, "0", NULL);
58         REPORT("exec fails %s: %d: %s\n", self, errno, strerror(errno));
59         exit(2);
60     }
61     return true;
62 }
63 
64 HARNESS_EXPORT
main(int argc,char * argv[])65 int main( int argc, char * argv[] ) {
66     MinThread = 3000;
67     ParseCommandLine( argc, argv );
68     if( MinThread <= 0 ) {
69         tbb::task_scheduler_init init( 2 ); // even number required for an error
70     } else {
71         for(int i = 0; i<MinThread; i++) {
72             if(exec_test(argv[0])) {
73                 REPORT("ERROR: execution fails at %d-th iteration!\n", i);
74                 exit(1);
75             }
76         }
77         REPORT("done\n");
78     }
79 }
80 
81 #else /* !__APPLE__ */
82 
83 #define HARNESS_NO_PARSE_COMMAND_LINE 1
84 #include "harness.h"
85 
TestMain()86 int TestMain () {
87     return Harness::Skipped;
88 }
89 
90 #endif /* !__APPLE__ */
91