Yuyi Wang via Cygwin wrote:
Below is a simple unix socket testing code. It creates a unix socket server and
a client to connect to it immediately. It works on Linux and macOS, but hangs on
cygwin. bind + listen work well, but seems that the connect method never 
returns.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>

#define SOCKET_PATH "/tmp/example_socket"

int main() {
     int server_fd, client_fd;
     struct sockaddr_un server_addr;
     socklen_t client_len;

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

     memset(&server_addr, 0, sizeof(server_addr));
     server_addr.sun_family = AF_UNIX;
     strncpy(server_addr.sun_path, SOCKET_PATH, sizeof(server_addr.sun_path) - 
1);

     if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) 
== -1) {
         perror("bind");
         exit(EXIT_FAILURE);
     }

     if (listen(server_fd, 1) == -1) {
         perror("listen");
         exit(EXIT_FAILURE);
     }

     printf("Server is listening on %s\n", SOCKET_PATH);

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

     if (connect(client_fd, (struct sockaddr*)&server_addr, 
sizeof(server_addr)) == -1) {
         perror("connect");
         exit(EXIT_FAILURE);
     }

     printf("Client connected to server\n");

     close(client_fd);
     close(server_fd);
     unlink(SOCKET_PATH);

     return 0;
}

May be this issue which also affected postfix:
https://sourceware.org/legacy-ml/cygwin/2014-08/msg00420.html

Try:
setsockopt(sd, SOL_SOCKET, SO_PEERCRED, NULL, 0);

available since:
https://cygwin.com/git/?p=newlib-cygwin.git;a=commit;h=697b9afe0
and may still work (I don't know).

--
Regards,
Christian


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

Reply via email to