Back to home page

Enduro/X

 
 

    


0001 #include "cgreen/internal/cgreen_pipe.h"
0002 #include <errno.h>
0003 #include <fcntl.h>
0004 #include <signal.h>
0005 #include <stdio.h>
0006 #include <unistd.h>
0007 #include <sys/wait.h>
0008 
0009 #ifdef __cplusplus
0010 namespace cgreen {
0011 #endif
0012 
0013 
0014 int cgreen_pipe_open(int pipes[2])
0015 {
0016     int pipe_open_result;
0017     int pipe_nonblock_result;
0018 
0019     pipe_open_result = pipe(pipes);
0020 
0021     if (pipe_open_result != 0) {
0022         return pipe_open_result;
0023     }
0024 
0025     pipe_nonblock_result = fcntl(pipes[1], F_SETFL, O_NONBLOCK); 
0026 
0027     if (pipe_nonblock_result != 0) {
0028         return pipe_open_result;
0029     }
0030 
0031     return 0;
0032 }
0033 
0034 void cgreen_pipe_close(int p)
0035 {
0036     close(p);
0037 }
0038 
0039 ssize_t cgreen_pipe_read(int p, void *buf, size_t count)
0040 {
0041     if (0 != fcntl(p, F_SETFL, O_NONBLOCK)) {
0042         fprintf(stderr, "could not set file status flag on read pipe\n");
0043         return -1;
0044     }
0045 
0046     return read(p, buf, count);
0047 }
0048 
0049 ssize_t cgreen_pipe_write(int p, const void *buf, size_t count)
0050 {
0051     int pipe_write_result = write(p, buf, count);
0052     int status;
0053     if (pipe_write_result < 0) {
0054         if (errno == EWOULDBLOCK) {
0055             fprintf(stderr, "\tCGREEN EXCEPTION: Too many assertions within a single test.\n");
0056         } else if (errno != EPIPE) {
0057             fprintf(stderr, "\tCGREEN EXCEPTION: Error when reporting from test case process to reporter\n");
0058         }
0059         raise(SIGPIPE);
0060         wait(&status); /* Safe-guarding against a signalhandler for SIGPIPE, which
0061                           incidentaly the test case for pipe block need to have... */
0062     }
0063     return pipe_write_result;
0064 }
0065 
0066 
0067 #ifdef __cplusplus
0068 } // namespace cgreen
0069 #endif
0070 
0071 /* vim: set ts=4 sw=4 et cindent: */