httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nathan J Kurz <n...@tripod.tripod.com>
Subject Re: [PATCH] PR#502: timeout problems
Date Fri, 02 May 1997 23:17:22 GMT
Dean gave some patches to the timoute handling in rfc1413.c:
> 
> The rfc1413 code did some bogosity with alarm().  This patch hopefully
> works around the bogosity in such a way to get a working 1.2.  We've
> mumbled in the past that we need proper timeout code to handle multiple
> outstanding timeouts... that'll have to wait until post-1.2. 
> 

Maybe it would be better to avoid having timeouts here altogether, and
use select() as a timeout instead?  I've been using this for a TCP
authentication scheme, and it's been working fine.

I don't use identd, so I don't want to patch it, but it should be
simple to substitute write_with_timeout() and read_with_timeout() for
the write() and read() calls.  Then you could just strip out all the
alarm handling and not muck around with resetting it.  

Then again maybe keeping the alarms for 1.2 would be good.  Although
there were a lot of things being changed in Dean's fix, so maybe this
would be cleaner.

nate@tripod.com
http://www.tripod.com

------------------------------------------------------------------------

ssize_t write_with_timeout(int sock_fd, const char *buffer,
                           size_t length, int seconds)
{
  fd_set write_fds;
  struct timeval timelimit;
  int    number_writeable;
  ssize_t bytes_written;

  /* check to see if the socket is valid */
  if (sock_fd < 0) return -1;

  /* set the timeout to the number of seconds given */
  timelimit.tv_sec = seconds;
  timelimit.tv_usec = 0;

  /* see if the socket is writeable, waiting as long as timeout if necessary */
  FD_ZERO(&write_fds);
  FD_SET(sock_fd, &write_fds);
  number_writeable = select(sock_fd + 1, NULL, &write_fds, NULL, &timelimit);

  /* write the string to the socket if the socket is writeable */
  if (number_writeable == 1) {
    do { 
      bytes_written = write(sock_fd, buffer, length); 
    } while ( (bytes_written == -1) && (errno == EINTR) );
  }
  else {
    bytes_written = -1;
  }

  return bytes_written;
}


ssize_t read_with_timeout(int sock_fd, char *buffer, 
                          size_t length, int seconds)
{
  fd_set read_fds;
  struct timeval timelimit;
  int    bytes_read;
  int number_readable;

  /* check to see if the socket is valid */
  if (sock_fd < 0) return -1;

  /* set the timeout to the number of seconds given */
  timelimit.tv_sec = seconds;
  timelimit.tv_usec = 0;

  /* waiting as long as timeout until the socket is readable */
  FD_ZERO(&read_fds);
  FD_SET(sock_fd, &read_fds);
  number_readable = select(sock_fd + 1, &read_fds, NULL, NULL, &timelimit);

  /* if the socket is readable read from the socket into the buffer  */
  /* note: buffer must be preallocated to at least length + 1 */
  /* note: it is assumed that only one read is necessary to read all data */
  if (number_readable == 1) {
    do { 
      bytes_read = read(sock_fd, buffer, length); 
    } while ( (bytes_read == -1) && (errno == EINTR));
    buffer[bytes_read + 1] = '\0';
  }
  else {
    bytes_read = -1;
  }

  return bytes_read;
}





Mime
View raw message