From owner-svn-src-all@freebsd.org Tue Sep 3 14:07:40 2019 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id E201FDD07D; Tue, 3 Sep 2019 14:06:51 +0000 (UTC) (envelope-from yuripv@freebsd.org) Received: from freefall.freebsd.org (freefall.freebsd.org [96.47.72.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "freefall.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 46N7zv1wHnz4PyQ; Tue, 3 Sep 2019 14:06:51 +0000 (UTC) (envelope-from yuripv@freebsd.org) Received: by freefall.freebsd.org (Postfix, from userid 1452) id 3D1AD1AC8C; Tue, 3 Sep 2019 14:06:19 +0000 (UTC) X-Original-To: yuripv@localmail.freebsd.org Delivered-To: yuripv@localmail.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [96.47.72.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client CN "mx1.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by freefall.freebsd.org (Postfix) with ESMTPS id 6AFE3265; Mon, 15 Apr 2019 03:31:06 +0000 (UTC) (envelope-from owner-src-committers@freebsd.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2610:1c1:1:6074::16:84]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "freefall.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 3938B8D88C; Mon, 15 Apr 2019 03:31:06 +0000 (UTC) (envelope-from owner-src-committers@freebsd.org) Received: by freefall.freebsd.org (Postfix, from userid 538) id 1D679264; Mon, 15 Apr 2019 03:31:06 +0000 (UTC) Delivered-To: src-committers@localmail.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [96.47.72.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client CN "mx1.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by freefall.freebsd.org (Postfix) with ESMTPS id 78D68262 for ; Mon, 15 Apr 2019 03:31:03 +0000 (UTC) (envelope-from oshogbo@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 3D0538D882; Mon, 15 Apr 2019 03:31:03 +0000 (UTC) (envelope-from oshogbo@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 0FF4921EA; Mon, 15 Apr 2019 03:31:03 +0000 (UTC) (envelope-from oshogbo@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x3F3V2tb026412; Mon, 15 Apr 2019 03:31:02 GMT (envelope-from oshogbo@FreeBSD.org) Received: (from oshogbo@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x3F3V2kv026411; Mon, 15 Apr 2019 03:31:02 GMT (envelope-from oshogbo@FreeBSD.org) Message-Id: <201904150331.x3F3V2kv026411@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: oshogbo set sender to oshogbo@FreeBSD.org using -f From: Mariusz Zaborski To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r346218 - head/lib/libnv X-SVN-Group: head X-SVN-Commit-Author: oshogbo X-SVN-Commit-Paths: head/lib/libnv X-SVN-Commit-Revision: 346218 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk X-Loop: FreeBSD.org Sender: owner-src-committers@freebsd.org X-Rspamd-Queue-Id: 3938B8D88C X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.94 / 15.00]; local_wl_from(0.00)[freebsd.org]; NEURAL_HAM_MEDIUM(-1.00)[-0.999,0]; NEURAL_HAM_SHORT(-0.94)[-0.943,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US]; NEURAL_HAM_LONG(-1.00)[-1.000,0] Status: O X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Date: Tue, 03 Sep 2019 14:07:40 -0000 X-Original-Date: Mon, 15 Apr 2019 03:31:02 +0000 (UTC) X-List-Received-Date: Tue, 03 Sep 2019 14:07:40 -0000 Author: oshogbo Date: Mon Apr 15 03:31:02 2019 New Revision: 346218 URL: https://svnweb.freebsd.org/changeset/base/346218 Log: libnv: add support for nvlist_send()/nvlist_recv() on Linux This may be useful for cross build in the feature. Submitted by: Mindaugas Rasiukevicius MFC after: 2 weeks Modified: head/lib/libnv/msgio.c Modified: head/lib/libnv/msgio.c ============================================================================== --- head/lib/libnv/msgio.c Mon Apr 15 01:27:15 2019 (r346217) +++ head/lib/libnv/msgio.c Mon Apr 15 03:31:02 2019 (r346218) @@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include @@ -58,7 +59,12 @@ __FBSDID("$FreeBSD$"); #define PJDLOG_ABORT(...) abort() #endif +#ifdef __linux__ +/* Linux: arbitrary size, but must be lower than SCM_MAX_FD. */ +#define PKG_MAX_SIZE ((64U - 1) * CMSG_SPACE(sizeof(int))) +#else #define PKG_MAX_SIZE (MCLBYTES / CMSG_SPACE(sizeof(int)) - 1) +#endif static int msghdr_add_fd(struct cmsghdr *cmsg, int fd) @@ -74,31 +80,6 @@ msghdr_add_fd(struct cmsghdr *cmsg, int fd) return (0); } -static int -msghdr_get_fd(struct cmsghdr *cmsg) -{ - int fd; - - if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET || - cmsg->cmsg_type != SCM_RIGHTS || - cmsg->cmsg_len != CMSG_LEN(sizeof(fd))) { - errno = EINVAL; - return (-1); - } - - bcopy(CMSG_DATA(cmsg), &fd, sizeof(fd)); -#ifndef MSG_CMSG_CLOEXEC - /* - * If the MSG_CMSG_CLOEXEC flag is not available we cannot set the - * close-on-exec flag atomically, but we still want to set it for - * consistency. - */ - (void) fcntl(fd, F_SETFD, FD_CLOEXEC); -#endif - - return (fd); -} - static void fd_wait(int fd, bool doread) { @@ -157,14 +138,7 @@ msg_send(int sock, const struct msghdr *msg) return (0); } -/* - * MacOS/Linux do not define struct cmsgcred but we need to bootstrap libnv - * when building on non-FreeBSD systems. Since they are not used during - * bootstrap we can just omit these two functions there. - */ -#ifndef __FreeBSD__ -#warning "cred_send() not supported on non-FreeBSD systems" -#else +#ifdef __FreeBSD__ int cred_send(int sock) { @@ -326,29 +300,53 @@ fd_package_recv(int sock, int *fds, size_t nfds) if (msg_recv(sock, &msg) == -1) goto end; - for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL; - i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) { - fds[i] = msghdr_get_fd(cmsg); - if (fds[i] < 0) + i = 0; + cmsg = CMSG_FIRSTHDR(&msg); + while (cmsg && i < nfds) { + unsigned int n; + + if (cmsg->cmsg_level != SOL_SOCKET || + cmsg->cmsg_type != SCM_RIGHTS) { + errno = EINVAL; break; + } + n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); + if (i + n > nfds) { + errno = EINVAL; + break; + } + bcopy(CMSG_DATA(cmsg), fds + i, sizeof(int) * n); + cmsg = CMSG_NXTHDR(&msg, cmsg); + i += n; } if (cmsg != NULL || i < nfds) { - int fd; + unsigned int last; /* * We need to close all received descriptors, even if we have * different control message (eg. SCM_CREDS) in between. */ - for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; - cmsg = CMSG_NXTHDR(&msg, cmsg)) { - fd = msghdr_get_fd(cmsg); - if (fd >= 0) - close(fd); + last = i; + for (i = 0; i < last; i++) { + if (fds[i] >= 0) { + close(fds[i]); + } } errno = EINVAL; goto end; } + +#ifndef MSG_CMSG_CLOEXEC + /* + * If the MSG_CMSG_CLOEXEC flag is not available we cannot set the + * close-on-exec flag atomically, but we still want to set it for + * consistency. + */ + for (i = 0; i < nfds; i++) { + (void) fcntl(fds[i], F_SETFD, FD_CLOEXEC); + } +#endif ret = 0; end: