Hello Samuel,
I haven't had much time to look into this in depth yet but I have adapted my 
initial example s.t. it doesn't need an external program for the main socket 
(please excuse my terrible C code and the formatting).

The socket is first created nonblocking and once the first connection comes in 
it is set to blocking and the connection is handled. As before the first 
connection returns a nonblocking socket.

--8<---------------cut here---------------start------------->8---
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

int
main (int argc, char **argv)
{

  int sock = socket(AF_UNIX, SOCK_STREAM, 0);
  fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK);  
  
  struct sockaddr_un addr;
  memset(&addr, 0, sizeof(addr));
  addr.sun_family= AF_UNIX;
  strncpy(addr.sun_path, "/tmp/sock", sizeof(addr.sun_path) -1);

  bind(sock, (struct sockaddr *) &addr, sizeof(addr));
  listen(sock, 10);

  fd_set fds;
  FD_ZERO(&fds);
  FD_SET(sock, &fds);

  select(sock + 1, &fds, NULL, NULL, NULL); 
  fprintf(stderr, "Setting sock to blocking\n");
  fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) & ~O_NONBLOCK);  
  
  int client;
  char buffer[1024];
  ssize_t num_read;
  
  while (1)
    {
      client = accept(sock, NULL, NULL);
      if (client < 0) return 1;
      fprintf(stderr, "Accepting connection\n");
      if ((fcntl(client, F_GETFL) & O_NONBLOCK) != 0)
{
  fprintf(stderr, "Socket is O_NONBLOCK\n");
}
      num_read = read(client, buffer, sizeof(buffer) -1);
      fprintf(stderr,"Received: %s\n", buffer);
      write(client, buffer, num_read);
      close(client);
    }
  return 0;
}
--8<---------------cut here---------------end--------------->8---


Apr 18, 2025, 14:54 by samuel.thiba...@gnu.org:

>
> No immediate idea, no, it should be investigated, to check what happens
> with the flags in __libc_accept4 on the program side, and newsock->flags
> in S_socket_accept / S_io_set_some_openmodes / S_io_get_openmodes on the
> pfinet side.
>
> Samuel
>


Reply via email to