#include "pdu_sender.h"
#include <errno.h>
+#include <poll.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
static int
send_response(int fd, uint8_t pdu_type, unsigned char *data, size_t data_len)
{
+ struct pollfd pfd;
int error;
pr_op_debug("Sending %s to client.", pdutype2str(pdu_type));
+ pfd.fd = fd;
+ pfd.events = POLLOUT;
+
+ do {
+ pfd.revents = 0;
+ error = poll(&pfd, 1, -1);
+ if (error < 0)
+ return pr_op_err("poll() error: %d", error);
+ if (error == 0)
+ return pr_op_err("poll() returned 0, even though there's no timeout.");
+ if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL))
+ return pr_op_err("poll() returned revents %u.", pfd.revents);
+ } while (!(pfd.revents & POLLOUT));
+
error = write(fd, data, data_len);
if (error < 0)
return pr_op_errno(errno, "Error sending %s to client.",
return 0;
}
+/*
+ * By the way: man 2 poll says
+ *
+ * > The operation of poll() and ppoll() is not affected by the O_NONBLOCK flag.
+ *
+ * Which appears to be untrue. If I remove this function, both client and server
+ * hang forever, apparently after the TCP handshake.
+ */
static int
set_nonblock(int fd)
{