2009-05-13 23:27:00 +00:00
|
|
|
/* $Id: buffer-poll.c,v 1.11 2009-05-13 23:27:00 nicm Exp $ */
|
2007-07-09 19:04:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2007-11-30 11:08:35 +00:00
|
|
|
/* Set up pollfd for buffers. */
|
|
|
|
void
|
|
|
|
buffer_set(
|
|
|
|
struct pollfd *pfd, int fd, unused struct buffer *in, struct buffer *out)
|
|
|
|
{
|
|
|
|
pfd->fd = fd;
|
|
|
|
pfd->events = POLLIN;
|
|
|
|
if (BUFFER_USED(out) > 0)
|
|
|
|
pfd->events |= POLLOUT;
|
|
|
|
}
|
|
|
|
|
2007-07-09 19:04:12 +00:00
|
|
|
/* Fill buffers from socket based on poll results. */
|
|
|
|
int
|
|
|
|
buffer_poll(struct pollfd *pfd, struct buffer *in, struct buffer *out)
|
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
|
2008-09-09 22:16:37 +00:00
|
|
|
#if 0
|
2008-07-01 20:35:16 +00:00
|
|
|
log_debug("buffer_poll (%ld): fd=%d, revents=%d; out=%zu in=%zu",
|
|
|
|
(long) getpid(),
|
2008-06-18 20:11:25 +00:00
|
|
|
pfd->fd, pfd->revents, BUFFER_USED(out), BUFFER_USED(in));
|
2008-09-09 22:16:37 +00:00
|
|
|
#endif
|
2008-05-31 20:04:15 +00:00
|
|
|
|
2009-05-13 23:27:00 +00:00
|
|
|
#ifdef HAVE_POLL
|
2007-07-09 19:04:12 +00:00
|
|
|
if (pfd->revents & (POLLERR|POLLNVAL|POLLHUP))
|
|
|
|
return (-1);
|
2008-08-28 17:45:30 +00:00
|
|
|
#endif
|
2007-07-09 19:04:12 +00:00
|
|
|
if (pfd->revents & POLLIN) {
|
|
|
|
buffer_ensure(in, BUFSIZ);
|
|
|
|
n = read(pfd->fd, BUFFER_IN(in), BUFFER_FREE(in));
|
2008-09-09 22:16:37 +00:00
|
|
|
#if 0
|
2008-05-31 20:04:15 +00:00
|
|
|
log_debug("buffer_poll: fd=%d, read=%zd", pfd->fd, n);
|
2008-09-09 22:16:37 +00:00
|
|
|
#endif
|
2007-07-09 19:04:12 +00:00
|
|
|
if (n == 0)
|
|
|
|
return (-1);
|
|
|
|
if (n == -1) {
|
|
|
|
if (errno != EINTR && errno != EAGAIN)
|
|
|
|
return (-1);
|
2007-11-07 19:41:17 +00:00
|
|
|
} else
|
|
|
|
buffer_add(in, n);
|
2007-07-09 19:04:12 +00:00
|
|
|
}
|
|
|
|
if (BUFFER_USED(out) > 0 && pfd->revents & POLLOUT) {
|
2008-06-18 22:21:51 +00:00
|
|
|
n = write(pfd->fd, BUFFER_OUT(out), BUFFER_USED(out));
|
2008-09-09 22:16:37 +00:00
|
|
|
#if 0
|
2008-05-31 20:04:15 +00:00
|
|
|
log_debug("buffer_poll: fd=%d, write=%zd", pfd->fd, n);
|
2008-09-09 22:16:37 +00:00
|
|
|
#endif
|
2007-07-09 19:04:12 +00:00
|
|
|
if (n == -1) {
|
|
|
|
if (errno != EINTR && errno != EAGAIN)
|
|
|
|
return (-1);
|
2007-11-07 19:41:17 +00:00
|
|
|
} else
|
|
|
|
buffer_remove(out, n);
|
2007-07-09 19:04:12 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
2007-11-27 19:23:34 +00:00
|
|
|
|
|
|
|
/* Flush buffer output to socket. */
|
|
|
|
void
|
|
|
|
buffer_flush(int fd, struct buffer *in, struct buffer *out)
|
|
|
|
{
|
|
|
|
struct pollfd pfd;
|
|
|
|
|
|
|
|
while (BUFFER_USED(out) > 0) {
|
2007-11-30 11:08:35 +00:00
|
|
|
buffer_set(&pfd, fd, in, out);
|
2007-11-27 19:23:34 +00:00
|
|
|
|
|
|
|
if (poll(&pfd, 1, INFTIM) == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
continue;
|
|
|
|
fatal("poll failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer_poll(&pfd, in, out) != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|