From owner-p4-projects@FreeBSD.ORG Wed Jun 10 11:46:46 2009 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 06CA81065678; Wed, 10 Jun 2009 11:46:46 +0000 (UTC) Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id B40C51065672 for ; Wed, 10 Jun 2009 11:46:45 +0000 (UTC) (envelope-from bb+lists.freebsd.perforce@cyrus.watson.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id A0F558FC22 for ; Wed, 10 Jun 2009 11:46:45 +0000 (UTC) (envelope-from bb+lists.freebsd.perforce@cyrus.watson.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.3/8.14.3) with ESMTP id n5ABkjKr045285 for ; Wed, 10 Jun 2009 11:46:45 GMT (envelope-from bb+lists.freebsd.perforce@cyrus.watson.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.3/8.14.3/Submit) id n5ABkjVt045283 for perforce@freebsd.org; Wed, 10 Jun 2009 11:46:45 GMT (envelope-from bb+lists.freebsd.perforce@cyrus.watson.org) Date: Wed, 10 Jun 2009 11:46:45 GMT Message-Id: <200906101146.n5ABkjVt045283@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to bb+lists.freebsd.perforce@cyrus.watson.org using -f From: Robert Watson To: Perforce Change Reviews Cc: Subject: PERFORCE change 163978 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Jun 2009 11:46:46 -0000 http://perforce.freebsd.org/chv.cgi?CH=163978 Change 163978 by rwatson@rwatson_freebsd_capabilities on 2009/06/10 11:46:38 Assymptotically approach correctness. Affected files ... .. //depot/projects/trustedbsd/capabilities/src/lib/libcapability/libcapability_host.c#3 edit Differences ... ==== //depot/projects/trustedbsd/capabilities/src/lib/libcapability/libcapability_host.c#3 (text+ko) ==== @@ -30,7 +30,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $P4: //depot/projects/trustedbsd/capabilities/src/lib/libcapability/libcapability_host.c#2 $ + * $P4: //depot/projects/trustedbsd/capabilities/src/lib/libcapability/libcapability_host.c#3 $ */ #include @@ -92,15 +92,31 @@ * number. */ static int -lch_installfds(int fd_count, int *fds) +lch_installfds(u_int fd_count, int *fds) { - int i; + u_int i; + int highestfd; + + if (fd_count == 0) + return (0); + + /* + * Identify the highest source file descriptor we care about so that + * when we play the dup2() rearranging game, we don't overwrite any + * we care about. + */ + highestfd = fds[0]; + for (i = 1; i < fd_count; i++) { + if (fds[i] > highestfd) + highestfd = fds[i]; + } + highestfd++; /* * First, move all our descriptors up the range. */ for (i = 0; i < fd_count; i++) { - if (dup2(fds[i], fd_count + i) < 0) + if (dup2(fds[i], highestfd + i) < 0) return (-1); } @@ -108,7 +124,7 @@ * Now put them back. */ for (i = 0; i < fd_count; i++) { - if (dup2(fd_count + i, fds[i]) < 0) + if (dup2(highestfd + i, i) < 0) return (-1); }