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]

Re: non-blocking accept() can hang.


2013/2/8 marco atzeri <marco.atzeri@gmail.com>:

> you can also test by yourself
> http://cygwin.com/snapshots/

I see.  I re-tested after replacing /bin/cygwin1.dll.

Unfortunately, the program still block.
(Good news is that ^C is effective.
I don't need to kill the process using Windows task manager.)

% uname -mrsv
CYGWIN_NT-5.1 1.7.18s(0.263/5/3) 20130131 05:33:37 i686
% cat tst-nonblocking.c
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/un.h>

void set_nonblock(int fd)
{
  int ret;
  ret = fcntl(fd, F_GETFL);
  if (ret == -1) { perror("fcntl(F_GETFL)"); exit(EXIT_FAILURE); }
  ret |= O_NONBLOCK;
  ret = fcntl(fd, F_SETFL, ret);
  if (ret == -1) { perror("fcntl(F_SETFL)"); exit(EXIT_FAILURE); }
}

int main(int argc, char *argv[])
{
  int s, c, ret;
  struct sockaddr_un addr;
  socklen_t addrlen;

  unlink("socketfile");

  addrlen = sizeof(addr);
  memset(&addr, '\0', addrlen);
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, "socketfile");

  s = socket(AF_UNIX, SOCK_STREAM, 0);
  if (s == -1) { perror("socket(server)"); exit(EXIT_FAILURE); }

  set_nonblock(s);

  ret = bind(s, (struct sockaddr *)&addr, addrlen);
  if (ret == -1) { perror("bind"); exit(EXIT_FAILURE); }

  ret = listen(s, SOMAXCONN);
  if (ret == -1) { perror("listen"); exit(EXIT_FAILURE); }

  c = socket(AF_UNIX, SOCK_STREAM, 0);
  if (c == -1) { perror("socket(client)"); exit(EXIT_FAILURE); }

  set_nonblock(c);

  ret = connect(c, (struct sockaddr *)&addr, addrlen);
  if (ret == -1 && errno == EINPROGRESS) { perror("connect"); }
  else if (ret == -1) { perror("connect"); exit(EXIT_FAILURE); }

  ret = accept(s, NULL, 0);
  if (ret == -1) { perror("accept"); exit(EXIT_FAILURE); }

  return EXIT_SUCCESS;
}

% gcc -Wall tst-nonblocking.c -o tst-nonblocking
% ./tst-nonblocking
connect: Operation now in progress
(^C is effective)

zsh: interrupt  ./tst-nonblocking
-- 
Tanaka Akira

Attachment: cygcheck.out
Description: Binary data

--
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]