1//
2// PipeImpl_POSIX.cpp
3//
4// Library: Foundation
5// Package: Processes
6// Module: PipeImpl
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/PipeImpl_POSIX.h"
16#include "Poco/Exception.h"
17#include <sys/types.h>
18#include <unistd.h>
19#include <errno.h>
20
21
22namespace Poco {
23
24
25PipeImpl::PipeImpl()
26{
27 int fds[2];
28 int rc = pipe(fds);
29 if (rc == 0)
30 {
31 _readfd = fds[0];
32 _writefd = fds[1];
33 }
34 else throw CreateFileException("anonymous pipe");
35}
36
37
38PipeImpl::~PipeImpl()
39{
40 closeRead();
41 closeWrite();
42}
43
44
45int PipeImpl::writeBytes(const void* buffer, int length)
46{
47 poco_assert (_writefd != -1);
48
49 int n;
50 do
51 {
52 n = write(_writefd, buffer, length);
53 }
54 while (n < 0 && errno == EINTR);
55 if (n >= 0)
56 return n;
57 else
58 throw WriteFileException("anonymous pipe");
59}
60
61
62int PipeImpl::readBytes(void* buffer, int length)
63{
64 poco_assert (_readfd != -1);
65
66 int n;
67 do
68 {
69 n = read(_readfd, buffer, length);
70 }
71 while (n < 0 && errno == EINTR);
72 if (n >= 0)
73 return n;
74 else
75 throw ReadFileException("anonymous pipe");
76}
77
78
79PipeImpl::Handle PipeImpl::readHandle() const
80{
81 return _readfd;
82}
83
84
85PipeImpl::Handle PipeImpl::writeHandle() const
86{
87 return _writefd;
88}
89
90
91void PipeImpl::closeRead()
92{
93 if (_readfd != -1)
94 {
95 close(_readfd);
96 _readfd = -1;
97 }
98}
99
100
101void PipeImpl::closeWrite()
102{
103 if (_writefd != -1)
104 {
105 close(_writefd);
106 _writefd = -1;
107 }
108}
109
110
111} // namespace Poco
112