This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

odd socketpair() failure


A while ago, I tested pipe() for EMFILE failures [1]. Well, I repeated those tests for socketpair() [2], and cygwin is once again the odd man out.

[1] http://cygwin.com/ml/cygwin/2011-06/msg00328.html
[2] http://austingroupbugs.net/view.php?id=483

$ cat foo.c
#define _POSIX_C_SOURCE 200811L
#define __EXTENSIONS__ 1
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/socket.h>
int
main (int argc, char **argv)
{
  int last;
  int fd[2] = {-2,-3};
  int err;
  /* Get to an EMFILE condition.  */
  while (1) {
    int fd = open("/dev/null", O_RDONLY);
    if (fd < 0) {
      printf ("after fd %d, open failed with errno %d %s\n",
	      last, errno, strerror(errno));
      break;
    }
    last = fd;
  }
  /* Probe behavior */
  err = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
  if (err)
    printf ("try 1, socketpair returned %d errno %d %s, fds %d %d\n",
	    err, errno, strerror(errno), fd[0], fd[1]);
  else
    printf ("try 1, socketpair succeeded, fds %d %d\n", fd[0], fd[1]);
  if (close(last))
    return 1;
  err = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
  if (err)
    printf ("try 2, socketpair returned %d errno %d %s, fds %d %d\n",
	    err, errno, strerror(errno), fd[0], fd[1]);
  else
    printf ("try 2, socketpair succeeded, fds %d %d\n", fd[0], fd[1]);
  if (close(0))
    return 1;
  err = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
  if (err)
    printf ("try 3, socketpair returned %d errno %d %s, fds %d %d\n",
	    err, errno, strerror(errno), fd[0], fd[1]);
  else
    printf ("try 3, socketpair succeeded, fds %d %d\n", fd[0], fd[1]);
  return 0;
}
$ ./foo
after fd 3199, open failed with errno 24 Too many open files
try 1, socketpair returned -1 errno 24 Too many open files, fds -2 -3
try 2, socketpair returned -1 errno 24 Too many open files, fds -2 -3
try 3, socketpair returned -1 errno 24 Too many open files, fds -2 -3

But on Linux, try 3 succeeds. Something in cygwin is not quite right on try 3 - the program explicitly freed two fd slots (0 and 3199), so it should have plenty of room to create the socketpair without hitting EMFILE.

Disclaimer: I tested on 1.7.9 rather than the latest snapshot; maybe the pipe() fix in the meantime also fixed socketpair()?

--
Eric Blake   eblake@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]