Debian Codegraph @

src/posix_runner_platform.c

raw symbols SCIP
1 #ifndef _GNU_SOURCE
2 #define _GNU_SOURCE
3 #endif
4
5 #ifndef __ANDROID__
6 #include "config.h"
7 #endif // #ifdef __ANDROID__
8 #include "runner.h"
9 #include "cgreen/internal/runner_platform.h"
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <unistd.h>
13 #include <sys/wait.h>
14 #include <signal.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <cgreen/internal/cgreen_time.h>
18
19 #ifdef __ANDROID__
20 #include "cgreen/internal/android_headers/androidcompat.h"
21 #endif // #ifdef __ANDROID__
22
23
24 typedef void (*sighandler_t)(int);
25
26 static int in_child_process(void);
27 static int wait_for_child_process(void);
28 static void stop(void);
29 static void ignore_ctrl_c(void);
30 static void allow_ctrl_c(void);
31
32
33 void run_test_in_its_own_process(TestSuite *suite, CgreenTest *test, TestReporter *reporter) {
34 uint32_t test_starting_milliseconds = cgreen_time_get_current_milliseconds();
35
36 (*reporter->start_test)(reporter, test->name);
37 if (test->skip) {
38 send_reporter_skipped_notification(reporter);
39 (*reporter->finish_test)(reporter, test->filename, test->line, NULL);
40 } else if (in_child_process()) {
41 run_the_test_code(suite, test, reporter);
42 send_reporter_completion_notification(reporter);
43 stop();
44 } else {
45 const int status = wait_for_child_process();
46 reporter->duration = cgreen_time_duration_in_milliseconds(test_starting_milliseconds,
47 cgreen_time_get_current_milliseconds());
48 if (WIFSIGNALED(status)) {
49 /* a C++ exception generates SIGABRT. Only print our special message for other signals. */
50 const int sig = WTERMSIG(status);
51 if (sig != SIGABRT) {
52 char buf[128];
53 snprintf(buf, sizeof(buf), "Test terminated with signal: %s", (const char *)strsignal(sig));
54 (*reporter->finish_test)(reporter, test->filename, test->line, buf);
55 return;
56 }
57 }
58
59 (*reporter->finish_test)(reporter, test->filename, test->line, NULL);
60 }
61 }
62
63 static int in_child_process(void) {
64 fflush(NULL); /* Flush all buffers before forking */
65 pid_t child = fork();
66 if (child < 0) {
67 die("Could not fork process\n");
68 }
69 return ! child;
70 }
71
72 static int wait_for_child_process(void) {
73 int status = 0;
74 ignore_ctrl_c();
75 wait(&status);
76 allow_ctrl_c();
77 return status;
78 }
79
80 void die_in(unsigned int seconds) {
81 sighandler_t signal_result = signal(SIGALRM, (sighandler_t)&stop);
82 if (SIG_ERR == signal_result) {
83 fprintf(stderr, "could not set alarm signal handler\n");
84 return;
85 }
86
87 alarm(seconds);
88 }
89
90 void run_specified_test_if_child(TestSuite *suite, TestReporter *reporter){
91 //not needed for systems that support fork()
92 (void)suite;
93 (void)reporter;
94 }
95
96 static void stop(void) {
97 #ifdef CGREEN_INTERNAL_WITH_GCOV
98 if (1)
99 #else
100 if (getenv("CGREEN_CHILD_EXIT_WITH__EXIT") == NULL)
101 #endif
102 exit(EXIT_SUCCESS);
103 else
104 _exit(EXIT_SUCCESS);
105 }
106
107 static void ignore_ctrl_c(void) {
108 signal(SIGINT, SIG_IGN);
109 }
110
111 static void allow_ctrl_c(void) {
112 signal(SIGINT, SIG_DFL);
113 }
114
115 /* vim: set ts=4 sw=4 et cindent: */