Thread View: news.software.nntp
1 messages
1 total messages
Started by Christopher J. Portman
Sat, 08 Aug 1998 00:00
HPUX11 Bug: accept()/listen() and O_NONBLOCK
Author: Christopher J. Portman
Date: Sat, 08 Aug 1998 00:00
Date: Sat, 08 Aug 1998 00:00
137 lines
4982 bytes
4982 bytes
There are conditions under which some applications are designed to
initialize their listen sockets to be nonblocking, and wish to later
reconfigure accepted sockets as blocking. A common implementation of this is
a server process, selecting on many descriptors and/or sockets, including
a listen socket, which then forks children and desire to read from the socket
in blocking mode.
A reproducable bug exists in HPUX 11, whereby the latter is not possible.
Accepted sockets which have inherited O_NONBLOCK from a "parent" descriptor
can not have O_NONBLOCK removed.
This was first encountered while tracing down problems with Diablo
(http://www.backplane.com/diablo), a backbone news transit system. Diablo's
author, Matt Dillion, confirmed the existance of the bug and almost
immediately provided a solution. Matt passed along his findings
to the INN team (http://www.isc.org/inn.html), who were experiencing
similar symptoms.
A ticket has been opened with HP's response centre.
/****************************************************************************
Demonstration of nonstandard socket blocking in HPUX 11's accept/listen
implementation.
Author:
Christopher J. Portman (chris@-unix.org)
Developed and tested on:
% uname -a
HP-UX random1 B.11.00 A 9000/820 2008824405 two-user license
Compilation instructions:
(1) Save the following code segment as 'block-bug.c'
(2) 'cc -o block-bug block-bug.c'
(3) './block-bug'
(4) In another window, initiate a connection to any address on the
machine running 'block-bug', to port 10000.
Purpose:
The code below will demonstrate that, after accepting a socket
(newsock_fd) from a listen()/accept()'ed socket (listen_fd) that has
been fcntl()'ed nonblocking, the accepted socket (newsock_fd) can not
be placed back into blocking mode.
****************************************************************************/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define LISTEN_PORT 10000
#define BUFFER_SIZE 128
int main(int argc, char **argv)
{
char buffer[BUFFER_SIZE];
int listen_fd = -1, newsock_fd = -1, bytesread = -1, addr_len = -1;
struct sockaddr_in listen_sin, newsock_sin;
fd_set fdset;
memset(&listen_sin, 0, sizeof(listen_sin));
/* Create socket */
if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
/* Set socket options */
listen_sin.sin_port = htons(LISTEN_PORT);
listen_sin.sin_family = AF_INET;
/* Bind address and port to socket. */
if (bind(listen_fd, (struct sockaddr *) &listen_sin,
sizeof(listen_sin)) < 0) {
perror("bind");
exit(1);
}
/* Set accept socket to nonblocking. */
fcntl(listen_fd, F_SETFL, O_NONBLOCK);
/* Activate queue for incoming connections. */
if (listen(listen_fd, 10) < 0) {
perror("listen");
exit(1);
}
printf(">>> Waiting for connection on port %d\n", LISTEN_PORT);
/* Very basic select() */
FD_ZERO(&fdset);
while (!FD_ISSET(listen_fd, &fdset)) {
FD_ZERO(&fdset);
FD_SET(listen_fd, &fdset);
if (select(10, &fdset, NULL, NULL, NULL) < 0) {
perror("select");
exit(1);
}
}
/* There's activity on the socket. Accept the connection. */
if ((newsock_fd = accept(listen_fd, (struct sockaddr *) &newsock_sin,
&addr_len)) < 0) {
perror("accept");
exit(1);
}
/*
* The newly accepted socket (newsock_fd) inherits some options of its
* "parent" descriptor (listen_fd), notably O_NONBLOCK.
*/
/* Attempt a read() - socket configured O_NONBLOCK. */
printf("\n>>> As socket is O_NONBLOCK, read() should return -1\n");
printf(">>> About to read()\n");
bytesread = read(newsock_fd, &buffer, sizeof(buffer));
printf("*** Read %d bytes (%s)\n", bytesread,
bytesread == -1 ? strerror(errno) : "no error");
/* Attempt a read() - socket reconfigured without O_NONBLOCK. */
fcntl(newsock_fd, F_SETFL, 0);
printf("\n>>> Socket SHOULD now be blocking.\n");
printf(">>> About to read().\n");
bytesread = read(newsock_fd, &buffer, sizeof(buffer));
printf("*** Read %d bytes (%s)\n", bytesread,
bytesread == -1 ? strerror(errno) : "no error");
return (0);
}
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads