From owner-freebsd-questions@FreeBSD.ORG Mon Sep 10 12:41:37 2012 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 3390A106566C for ; Mon, 10 Sep 2012 12:41:37 +0000 (UTC) (envelope-from freebsd-questions@m.gmane.org) Received: from plane.gmane.org (plane.gmane.org [80.91.229.3]) by mx1.freebsd.org (Postfix) with ESMTP id 21CD98FC14 for ; Mon, 10 Sep 2012 12:41:35 +0000 (UTC) Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1TB3J2-0000Zy-VU for freebsd-questions@freebsd.org; Mon, 10 Sep 2012 14:41:37 +0200 Received: from 79-139-19-75.prenet.pl ([79.139.19.75]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 10 Sep 2012 14:41:36 +0200 Received: from jb.1234abcd by 79-139-19-75.prenet.pl with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 10 Sep 2012 14:41:36 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: freebsd-questions@freebsd.org From: jb Date: Mon, 10 Sep 2012 12:41:19 +0000 (UTC) Lines: 47 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: sea.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 79.139.19.75 (Mozilla/5.0 (X11; FreeBSD i386; rv:15.0) Gecko/20100101 Firefox/15.0.1) Subject: Re: How to detect unconnected AF_UNIX sockets X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 10 Sep 2012 12:41:37 -0000 Sam Varshavchik courier-mta.com> writes: > > I'm porting existing code from Linux where a connect() to an AF_UNIX socket > that exists, but does not have a listener, fails with ECONNREFUSED. This is > quite agreeable with the comparable scenario in AF_INET, with a connection > attempt to a port without a listener on it. So the same code handles both > situations, immediately reporting an error, and without caring much about > the type of the socket. > > But on FreeBSD, according to truss, it seems that a connect() to an AF_UNIX > socket without a listener still succeeds. A subsequent write() also > succeeds, and read() blocks. > > The fall-out is quite unfortunate, and I was hoping for a way to detect that > my allegedly connected socket is lying to me, and it's not really connected > to anything. How would I go about doing that? I tried using the > LOCAL_CONNWAIT option as documented in unix(4), but that does not appear to > make any difference, in this situation. > > It seems to work here: $ uname -a ... FreeBSD 9.1-RC1 #0 ... $ ls -al /tmp/server srwxr-xr-x 1 jb wheel 0 Sep 10 14:22 /tmp/server $ ps auxww |grep -i server-af_unix $ ./client-af_unix client af_unix: connect() errno = 61 connect() failed: No such file or directory $ grep 61 /usr/include/errno.h #define ECONNREFUSED 61 /* Connection refused */ $ Client code: ... rc = connect(sd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr)); if (rc < 0) { printf("client af_unix: connect() errno = %d\n", errno); perror("connect() failed"); ... jb