]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/channel.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 namespace pdns::channel
28 Notifier::Notifier(FDWrapper
&& descriptor
) :
29 d_fd(std::move(descriptor
))
33 bool Notifier::notify() const
37 auto sent
= write(d_fd
.getHandle(), &data
, sizeof(data
));
39 throw std::runtime_error("Unable to write to channel notifier pipe: remote end has been closed");
41 if (sent
!= sizeof(data
)) {
45 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
48 throw std::runtime_error("Unable to write to channel notifier pipe: " + stringerror());
54 Waiter::Waiter(FDWrapper
&& descriptor
, bool throwOnEOF
) :
55 d_fd(std::move(descriptor
)), d_throwOnEOF(throwOnEOF
)
64 got
= read(d_fd
.getHandle(), &data
, sizeof(data
));
70 throw std::runtime_error("EOF while clearing channel notifier pipe");
76 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
79 throw std::runtime_error("Error while clearing channel notifier pipe: " + stringerror());
84 int Waiter::getDescriptor() const
86 return d_fd
.getHandle();
89 std::pair
<Notifier
, Waiter
> createNotificationQueue(bool nonBlocking
, size_t pipeBufferSize
, bool throwOnEOF
)
91 std::array
<int, 2> fds
= {-1, -1};
92 if (pipe(fds
.data()) < 0) {
93 throw std::runtime_error("Error creating notification channel pipe: " + stringerror());
96 FDWrapper
sender(fds
[1]);
97 FDWrapper
receiver(fds
[0]);
99 if (nonBlocking
&& !setNonBlocking(receiver
.getHandle())) {
101 throw std::runtime_error("Error making notification channel pipe non-blocking: " + stringerror(err
));
104 if (nonBlocking
&& !setNonBlocking(sender
.getHandle())) {
106 throw std::runtime_error("Error making notification channel pipe non-blocking: " + stringerror(err
));
109 if (pipeBufferSize
> 0 && getPipeBufferSize(receiver
.getHandle()) < pipeBufferSize
) {
110 setPipeBufferSize(receiver
.getHandle(), pipeBufferSize
);
113 return {Notifier(std::move(sender
)), Waiter(std::move(receiver
), throwOnEOF
)};