From owner-freebsd-arch@FreeBSD.ORG Sat Jul 6 20:47:38 2013 Return-Path: Delivered-To: arch@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 3E902E51 for ; Sat, 6 Jul 2013 20:47:38 +0000 (UTC) (envelope-from pawel@dawidek.net) Received: from mail.dawidek.net (garage.dawidek.net [91.121.88.72]) by mx1.freebsd.org (Postfix) with ESMTP id 096FD147B for ; Sat, 6 Jul 2013 20:47:37 +0000 (UTC) Received: from localhost (89-73-195-149.dynamic.chello.pl [89.73.195.149]) by mail.dawidek.net (Postfix) with ESMTPSA id DF33A442; Sat, 6 Jul 2013 22:42:54 +0200 (CEST) Date: Sat, 6 Jul 2013 22:47:58 +0200 From: Pawel Jakub Dawidek To: Jilles Tjoelker Subject: Re: General purpose library for name/value pairs. Message-ID: <20130706204758.GG25842@garage.freebsd.pl> References: <20130704215329.GG1402@garage.freebsd.pl> <20130705205856.GA19346@stack.nl> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="bygAmIonOAIqBxQB" Content-Disposition: inline In-Reply-To: <20130705205856.GA19346@stack.nl> X-OS: FreeBSD 10.0-CURRENT amd64 User-Agent: Mutt/1.5.21 (2010-09-15) Cc: arch@FreeBSD.org X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 06 Jul 2013 20:47:38 -0000 --bygAmIonOAIqBxQB Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Jul 05, 2013 at 10:58:56PM +0200, Jilles Tjoelker wrote: > On Thu, Jul 04, 2013 at 11:53:30PM +0200, Pawel Jakub Dawidek wrote: > > The library allows to send and receive descriptors, of course only over > > UNIX domain sockets: >=20 > > nvlist_t *nvl; > > int fd; > >=20 > > fd =3D open("/etc/passwd", O_RDONLY); > > if (fd < 0) > > err(1, "open(/etc/passwd) failed"); > >=20 > > nvl =3D nvlist_create(0); > > nvlist_add_string(nvl, "filename", "/etc/passwd"); > > nvlist_move_descriptor(nvl, "fd", fd); > > if (nvlist_send(sock, nvl) < 0) > > err(1, "nvlist_send() failed"); > > nvlist_destroy(nvl); >=20 > > Also note that I used nvlist_move_descriptor() function and not > > nvlist_add_descriptor(). The former will allow nvlist to consume the > > given descriptor, so we don't have to close it, the latter will dup(2) > > the given descriptor and then add it to the nvlist. >=20 > The library should use fcntl(fd, F_DUPFD_CLOEXEC, 0) instead of dup(fd) > so it does not pass the fd in case another thread forks. This is > available in sufficiently recent head, stable/9 and stable/8. >=20 > (On the other hand, if the application provides a file descriptor, I > think it is not necessary to set the close-on-exec flag because only the > creator of the file descriptor can do so in a race-free manner.) >=20 > The recvmsg() call should use the MSG_CMSG_CLOEXEC flag for the same > reason. This is currently only available in head. It is probably best to > fcntl(fd, F_SETFD, 1) if MSG_CMSG_CLOEXEC is not available so that > people do not write applications that assume close-on-exec is clear. I fully agree and implemented what you have suggested. Thanks! --=20 Pawel Jakub Dawidek http://www.wheelsystems.com FreeBSD committer http://www.FreeBSD.org Am I Evil? Yes, I Am! http://mobter.com --bygAmIonOAIqBxQB Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.20 (FreeBSD) iEYEARECAAYFAlHYgn4ACgkQForvXbEpPzS39gCcDAOWBYaxdJlMOO7QgJh7sGR7 CSgAnRLBhO/ww9+//jpX45PTuUV8UMHA =J0Ln -----END PGP SIGNATURE----- --bygAmIonOAIqBxQB--