Date: Fri, 5 Jul 2013 22:58:56 +0200 From: Jilles Tjoelker <jilles@stack.nl> To: Pawel Jakub Dawidek <pjd@FreeBSD.org> Cc: arch@FreeBSD.org Subject: Re: General purpose library for name/value pairs. Message-ID: <20130705205856.GA19346@stack.nl> In-Reply-To: <20130704215329.GG1402@garage.freebsd.pl> References: <20130704215329.GG1402@garage.freebsd.pl>
next in thread | previous in thread | raw e-mail | index | archive | help
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: > nvlist_t *nvl; > int fd; > > fd = open("/etc/passwd", O_RDONLY); > if (fd < 0) > err(1, "open(/etc/passwd) failed"); > > nvl = 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); > 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. 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. (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.) 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. -- Jilles Tjoelker
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20130705205856.GA19346>