This is the mail archive of the
cygwin
mailing list for the Cygwin project.
bind() behavior inconsistency with Linux and MacOS
- From: Petr Skočík <pskocik at gmail dot com>
- To: cygwin at cygwin dot com
- Date: Wed, 5 Jun 2019 15:06:39 +0200
- Subject: bind() behavior inconsistency with Linux and MacOS
Hi.
I don't know if this is technically a bug, but I've noticed that unlike
on Linux or MacOS, I a cannot bind a unix domain socket in a child
process and then listen on it in the parent.
The bind succeeds but `listen()` in the parent then fails with EINVAL.
(The reason I'd like to `bind` in a different process is so I could
`chdir` to a different directory before `bind`ing and then conceptually
back without having the `chdir` break file operations in different threads.)
Just wanted to let you guys know in case there was an easy fix.
Best regards,
Petr Skocik
____________________
Example code:
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int C, char **V)
{
int sfd; if(0>(sfd=socket(AF_UNIX, SOCK_STREAM, 0),1)) return
perror("socket"),1;
struct sockaddr_un uaddr; uaddr.sun_family = AF_UNIX;
char const *nm = "FOO.sock"; size_t nmlen =
nm[0]?strlen(nm):(1+strlen(&nm[1]));
size_t actlen = nmlen <= sizeof(uaddr.sun_path)-1 ? nmlen :
sizeof(uaddr.sun_path)-1;
memcpy(&uaddr.sun_path[0],nm,actlen);
uaddr.sun_path[actlen]='\0';
size_t socklen = 0 ? sizeof(struct sockaddr_un) : offsetof(struct
sockaddr_un, sun_path) + actlen + 1;
fflush(stdout);
pid_t pid=0;
int fork_eh = atoi(V[1]?V[1]:"1");
if(fork_eh)
if(0>(pid=fork())) return perror("fork"),1;
if(0==pid){
printf("pid=%d binding\n", (int)getpid());
if(0>bind(sfd, (struct sockaddr*)&uaddr, (socklen_t)+socklen))
perror("bind"),_exit(1);
printf("pid=%d bound\n", (int)getpid());
fflush(stdout);
if(fork_eh) _exit(0);
}
wait(0);
if(0>listen(sfd,INT_MAX)) return perror("listen"),1;
printf("pid=%d listening\n", (int)getpid());
}
--
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