From patchwork Sun Dec 18 02:59:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?0L3QsNCx?= X-Patchwork-Id: 13075962 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40B39C4167B for ; Sun, 18 Dec 2022 02:59:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229625AbiLRC7s (ORCPT ); Sat, 17 Dec 2022 21:59:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230052AbiLRC7o (ORCPT ); Sat, 17 Dec 2022 21:59:44 -0500 Received: from tarta.nabijaczleweli.xyz (unknown [139.28.40.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B3F0CF012 for ; Sat, 17 Dec 2022 18:59:38 -0800 (PST) Received: from tarta.nabijaczleweli.xyz (unknown [192.168.1.250]) by tarta.nabijaczleweli.xyz (Postfix) with ESMTPSA id C8AB1B00; Sun, 18 Dec 2022 03:59:35 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nabijaczleweli.xyz; s=202211; t=1671332375; bh=of+AmGp7ddEP64mi0oGVyY9wIpcmENb8uiszZZ/7NjY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=PXExfDmLlCgE0b7a9pMslh9DbGbXqdBZfdm0qPpwzTTZ8cWw4o/yO5AD3jwqKMICV 1hvN1tqk1r5VImb4Np7zLV9DcsZQoiOcGHnE6/uLNWedobCP+29FFO1ZQXmh+Mjui2 C0kF1Go8pwujb+ZXG0l8prb9BBYkheryniyUdRWcyxX5rncDwdQMOm9cIzGsTMp0Uw aKC+ormUIEqM2D5Z1fKZqRPizod4i/DvhVhpJBNyRskbJxM79pI9APn13oXnHxWcdD Y0D57b2CfMH5y9347w2hzgwjA8FTUPB7pvMjfptinvhxlk38ZvL8fNfM4K/nJGfc8W TXYmxNxD/vHVQ== Date: Sun, 18 Dec 2022 03:59:34 +0100 From: =?utf-8?b?0L3QsNCx?= To: Herbert Xu Cc: Jilles Tjoelker , dash@vger.kernel.org Subject: [PATCH v2] redir: savefd: use F_DUPFD_CLOEXEC instead of F_DUPFD+F_SETFD, if available Message-ID: <20221218025934.hgg6zzq4pwvja7t4@tarta.nabijaczleweli.xyz> References: <20221217144420.GA3954@stack.nl> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20220429 Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org This saves a syscall on every source file open, &c.; F_DUPFD_CLOEXEC is a mandatory part of POSIX since Issue 7 (Austin Group Interpretation 1003.1-2001 #171). --- autoconf bits adapted almost-verbatim from the st_mtim check above configure.ac | 13 +++++++++++++ src/redir.c | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/configure.ac b/configure.ac index 52aa429..b870a59 100644 --- a/configure.ac +++ b/configure.ac @@ -177,6 +177,19 @@ if test "$have_st_mtim" = "yes"; then [Define if your `struct stat' has `st_mtim']) fi +dnl F_DUPFD_CLOEXEC is a mandatory part of POSIX since Issue 7 +AC_MSG_CHECKING(for F_DUPFD_CLOEXEC) +AC_COMPILE_IFELSE( +[AC_LANG_PROGRAM([#include +#include ], +[return fcntl(0, F_DUPFD_CLOEXEC, 0)])], +have_dupfd_cloexec=yes, have_dupfd_cloexec=no) +AC_MSG_RESULT($have_dupfd_cloexec) +if test "$have_dupfd_cloexec" = "yes"; then + AC_DEFINE([HAVE_F_DUPFD_CLOEXEC], [1], + [Define if your system supports F_DUPFD_CLOEXEC]) +fi + AC_ARG_WITH(libedit, AS_HELP_STRING(--with-libedit, [Compile with libedit support])) use_libedit= if test "$with_libedit" = "yes"; then diff --git a/src/redir.c b/src/redir.c index 5a5835c..c923bfa 100644 --- a/src/redir.c +++ b/src/redir.c @@ -446,14 +446,21 @@ savefd(int from, int ofd) int newfd; int err; +#if HAVE_F_DUPFD_CLOEXEC + newfd = fcntl(from, F_DUPFD_CLOEXEC, 10); +#else newfd = fcntl(from, F_DUPFD, 10); +#endif + err = newfd < 0 ? errno : 0; if (err != EBADF) { close(ofd); if (err) sh_error("%d: %s", from, strerror(err)); +#if !HAVE_F_DUPFD_CLOEXEC else fcntl(newfd, F_SETFD, FD_CLOEXEC); +#endif } return newfd;