Debian Codegraph @

src/posix_cgreen_pipe.c

raw symbols SCIP
1 #include "cgreen/internal/cgreen_pipe.h"
2
3 #include "utils.h"
4
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <sys/wait.h>
11
12 #ifdef __ANDROID__
13 #include "cgreen/internal/android_headers/androidcompat.h"
14 #endif // #ifdef __ANDROID__
15
16 #ifndef O_NONBLOCK
17 # error "Your platform does not support POSIX ASYNC pipe reads. Please report a bug to http://github.com/cgreen-devs/cgreen/issues"
18 /* One way to do it the old way is: ioctl(fd, FIOBIO, &flags); */
19 #endif
20
21
22 int cgreen_pipe_open(int pipes[2])
23 {
24 int pipe_open_result;
25 int pipe_nonblock_result;
26
27 pipe_open_result = pipe(pipes);
28
29 if (pipe_open_result != 0) {
30 return pipe_open_result;
31 }
32
33 pipe_nonblock_result = fcntl(pipes[1], F_SETFL, O_NONBLOCK);
34
35 if (pipe_nonblock_result != 0) {
36 return pipe_open_result;
37 }
38
39 return 0;
40 }
41
42 void cgreen_pipe_close(int p)
43 {
44 close(p);
45 }
46
47 ssize_t cgreen_pipe_read(int p, void *buf, size_t count)
48 {
49 if (0 != fcntl(p, F_SETFL, O_NONBLOCK)) {
50 fprintf(stderr, "could not set file status flag on read pipe\n");
51 return -1;
52 }
53
54 return read(p, buf, count);
55 }
56
57 ssize_t cgreen_pipe_write(int p, const void *buf, size_t count)
58 {
59 int pipe_write_result = write(p, buf, count);
60 int status;
61 if (pipe_write_result < 0) {
62 if (errno == EWOULDBLOCK) {
63 PANIC("Too many assertions within a single test.");
64 } else if (errno != EPIPE) {
65 PANIC("Error when reporting from test case process to reporter.");
66 }
67 raise(SIGPIPE);
68 wait(&status); /* Safe-guarding against a signalhandler for SIGPIPE, which
69 incidentally the test case for pipe block need to have... */
70 }
71 return pipe_write_result;
72 }
73
74 /* vim: set ts=4 sw=4 et cindent: */