From owner-svn-src-head@freebsd.org Thu Nov 24 14:50:22 2016 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 8EFCEC53A72; Thu, 24 Nov 2016 14:50:22 +0000 (UTC) (envelope-from des@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 mx1.freebsd.org (Postfix) with ESMTPS id 5E87AAD3; Thu, 24 Nov 2016 14:50:22 +0000 (UTC) (envelope-from des@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id uAOEoLvu079216; Thu, 24 Nov 2016 14:50:21 GMT (envelope-from des@FreeBSD.org) Received: (from des@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id uAOEoLA5079215; Thu, 24 Nov 2016 14:50:21 GMT (envelope-from des@FreeBSD.org) Message-Id: <201611241450.uAOEoLA5079215@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: des set sender to des@FreeBSD.org using -f From: =?UTF-8?Q?Dag-Erling_Sm=c3=b8rgrav?= Date: Thu, 24 Nov 2016 14:50:21 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r309109 - head/lib/libutil X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Nov 2016 14:50:22 -0000 Author: des Date: Thu Nov 24 14:50:21 2016 New Revision: 309109 URL: https://svnweb.freebsd.org/changeset/base/309109 Log: Add a warning against modifying this code without understanding it, and an example of how not to make it more portable. I've had this lying around uncommitted since 2009... Modified: head/lib/libutil/flopen.c Modified: head/lib/libutil/flopen.c ============================================================================== --- head/lib/libutil/flopen.c Thu Nov 24 14:48:46 2016 (r309108) +++ head/lib/libutil/flopen.c Thu Nov 24 14:50:21 2016 (r309109) @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2007 Dag-Erling Coïdan Smørgrav + * Copyright (c) 2007-2009 Dag-Erling Coïdan Smørgrav * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -37,6 +37,14 @@ __FBSDID("$FreeBSD$"); #include +/* + * Reliably open and lock a file. + * + * DO NOT, UNDER PAIN OF DEATH, modify this code without first reading the + * revision history and discussing your changes with . + * Don't be fooled by the code's apparent simplicity; there would be no + * need for this function if it was as easy to get right as you think. + */ int flopen(const char *path, int flags, ...) { @@ -100,6 +108,14 @@ flopen(const char *path, int flags, ...) errno = serrno; return (-1); } +#ifdef DONT_EVEN_THINK_ABOUT_IT + if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) { + serrno = errno; + (void)close(fd); + errno = serrno; + return (-1); + } +#endif return (fd); } }