Non-compliant FreeBSD behavior?
M. Warner Losh
imp at bsdimp.com
Fri Apr 11 12:44:31 PDT 2003
A number of places I've seen says that the BUGGY section should be
identical to the else part. That is, setting O_NONBLOCK via fcntl
should result in the same behavior as setting it using FIONBIO.
However, I've observed that for both threaded and unthreaded programs
the BUGGY part doesn't work, while the other part does. Is there
something subtle I'm missing? What do the standards have to say about
this?
Warner
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
int fd;
struct in_addr addr;
struct sockaddr_in sa;
int flags;
fd = socket(PF_INET, SOCK_STREAM, 0);
inet_aton("1.2.3.4", &addr);
sa.sin_len = sizeof(sa);
sa.sin_family = AF_INET;
sa.sin_port = htons(1234);
memcpy(&sa.sin_addr, &addr, sizeof(addr));
#if BUGGY
flags = fcntl(fd, F_GETFL, NULL);
flags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, &flags) == -1)
err(1, "fcntl");
#else
flags = 1;
if (ioctl(fd, FIONBIO, &flags))
err(1, "ioctl");
#endif
if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)))
err(1, "connect");
exit(0);
}
More information about the freebsd-standards
mailing list