I have a program that creates a TCP socket and listens for incoming connections. After calling accept it uses setsockopt and the SO_RCVTIMEO and SO_SNDTIMEO options so that reads will timeout if no data is received for a while. This works flawlessly unless the program is compiled with -pthread (the program doesn't actually use any pthread_* functions). If compiled with threads then a call to read will block forever. How-To-Repeat: This is an example program that works as expected unless -pthread is passed to the compiler (as expected means read times out after 15 seconds). You can also get this program from http://www.ugh.net.au/~andrew/tmp/setsockopt.c in case it gets mangled in transit. #include <sys/param.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/time.h> #include <netinet/in.h> #include <arpa/inet.h> #include <err.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) { char buffer[1024]; int listening_fd, connection_fd, port, size; struct sockaddr_in local_address, remote_address; struct timeval tv; if (argc != 2) { fprintf(stderr, "usage: %s port\n", getprogname()); exit(1); } else { port = atoi(argv[1]); } /* create socket */ if ((listening_fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { err(1, "socket"); } /* allow us to rebind even if old clients are still hanging around */ size = 1; /* borrow memory for a second */ if (setsockopt(listening_fd, SOL_SOCKET, SO_REUSEADDR, &size, sizeof(size)) != 0) { err(1, "setsockopt"); } /* describe our end of the connection */ bzero(&local_address, sizeof(local_address)); local_address.sin_family = AF_INET; local_address.sin_addr.s_addr = htonl(INADDR_ANY); local_address.sin_port = htons(port); /* bind the socket to the address */ if (bind(listening_fd, (struct sockaddr *)&local_address, sizeof(local_address)) == -1) { err(1, "bind"); } /* make it a listening port */ if (listen(listening_fd, -1) != 0) { err(1, "listen"); } /* accept connections */ for (;;) { size = sizeof(remote_address); if ((connection_fd = accept(listening_fd, (struct sockaddr *)&remote_address, &size)) == -1) { err(1, "accept"); } printf("connected...\n"); /* set up timeouts */ tv.tv_sec = 15L; tv.tv_usec = 0L; if (setsockopt(connection_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) { err(1, "setsockopt"); } if (setsockopt(connection_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) != 0) { err(1, "setsockopt"); } /* do a bit of reading and writing */ #define STRING "hello\n" if (write(connection_fd, STRING, sizeof(STRING)) == -1) { err(1, "write"); } #undef STRING if (read(connection_fd, buffer, sizeof(buffer)) == -1) { warn("read"); goto failure; } else { printf("received %s", buffer); } #define STRING "goodbye\n" if (write(connection_fd, STRING, sizeof(STRING)) == -1) { err(1, "write"); } #undef STRING failure: if (close(connection_fd) == -1) { err(1, "close"); } printf("disconnected...\n"); } return 0; }
i think this problem never get solved since FreeBSD 4.1 - I have the same problem in FreeBSD 4.3 stable, i.e. set timeout has not effects whatsoever if linked with libc_r. Do you guys have any plan to do something about it? thanks. _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com
This PR is duplicated with misc/20861: http://www.FreeBSD.org/cgi/query-pr.cgi?pr=31860 -- Maxim Konovalov, MAcomnet, Internet-Intranet Dept., system engineer phone: +7 (095) 796-9079, mailto: maxim@macomnet.ru
State Changed From-To: open->closed Duplicate of PR misc/20861.