From debbugs-submit-bounces@debbugs.gnu.org Tue May 01 11:27:30 2018 Received: (at submit) by debbugs.gnu.org; 1 May 2018 15:27:30 +0000 Received: from localhost ([127.0.0.1]:45798 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fDXBh-0005U5-Hb for submit@debbugs.gnu.org; Tue, 01 May 2018 11:27:30 -0400 Received: from eggs.gnu.org ([208.118.235.92]:45647) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fDSgf-00051j-IQ for submit@debbugs.gnu.org; Tue, 01 May 2018 06:39:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fDSgZ-0006Fq-B4 for submit@debbugs.gnu.org; Tue, 01 May 2018 06:39:04 -0400 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50,T_DKIM_INVALID autolearn=disabled version=3.3.2 Received: from lists.gnu.org ([2001:4830:134:3::11]:38443) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fDSgZ-0006Fj-80 for submit@debbugs.gnu.org; Tue, 01 May 2018 06:39:03 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36616) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fDSgX-0004jA-Vn for bug-coreutils@gnu.org; Tue, 01 May 2018 06:39:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fDSgU-0006E9-PR for bug-coreutils@gnu.org; Tue, 01 May 2018 06:39:01 -0400 Received: from mail-pg0-x229.google.com ([2607:f8b0:400e:c05::229]:44272) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fDSgU-0006DZ-Ia for bug-coreutils@gnu.org; Tue, 01 May 2018 06:38:58 -0400 Received: by mail-pg0-x229.google.com with SMTP id 82-v6so8124665pge.11 for ; Tue, 01 May 2018 03:38:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toojays-net.20150623.gappssmtp.com; s=20150623; h=to:from:subject:message-id:date:user-agent:mime-version :content-transfer-encoding:content-language; bh=YcdOOjtvXAwdHwAu3cIAS9xjVZbty+smrgyZK/Jx4TE=; b=ZRj0eO3M4uqiMJmUh3aDJj8uuuEKM7eo77aM101yt7tZZHdsl/cLjka4o+iHNksg/x 75T0VDIK4vCW372k30ct38tdUILnv4CW4IjyqK6uNCF0LSlEQ7z6EXaNiDTmiM6CetcD bGIk+5PXjed6eMdnuom/nUBfOFV/BsFjIreiv2b7IZRiGI2rVT5149Hw8M90gs/aCTcg Dt9pyXuT5obIdknkSyTYlQIVLSce1Z6uuAkg+ZpsLecwNtd034mYmfK70MdkVvbUgf2s nc2zxN0QGfRtN/1ffEl6Vr5ZaaFV9xBe5qqmLzwmYn1jPzTf9florHCXk0mL357FS4rZ aD+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-transfer-encoding:content-language; bh=YcdOOjtvXAwdHwAu3cIAS9xjVZbty+smrgyZK/Jx4TE=; b=rZHZPpPWnytMMhbuQNviN+9HNY7BeVORQ4xWQJ+w2xi2CIgYBvoWtKW2qcDe5hTqQC UYPGIXddODDrruh1BIhdoM26F4TNIHZXAmnceJBBs5p8l0XBOmm3IozTTPXlI4040HEQ F71L45ybbCmC2ChAg7kXjJgO2WfQ5H5NDmUA37ryEyn5cw6UeCqF2woBaA1gcP/YxtVP ioCkZEOXGa0si1ijj00x0x0e62ILbU7eswRYrvQTWGKu+Z5N0EkwZobWJAUERpW/6gvo MM17KmJpRS76aI0yNwGaKOG0m0DqjuS5DFZztDb6T59iwEaXJ8kYP0sp+FqIdSK0Tas9 NeLQ== X-Gm-Message-State: ALQs6tAB5UsUogG1v6ayNRHfzLSHOUEvJbix254KP96WSYyq2+tsXUDB K8zuYebzpGqxHGTwn0NuAuqz65QTkCI= X-Google-Smtp-Source: AB8JxZqvCafZpSHp+mO/d0v4q+s6hh9zLEuGIngLzhJRp8+n3IGQV/kjqK6MxZw/wVAmz9g39WSguA== X-Received: by 10.98.155.141 with SMTP id e13mr15285489pfk.157.1525171136623; Tue, 01 May 2018 03:38:56 -0700 (PDT) Received: from [192.168.1.108] ([45.124.201.30]) by smtp.gmail.com with ESMTPSA id k13sm21271813pfj.186.2018.05.01.03.38.53 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 May 2018 03:38:55 -0700 (PDT) To: bug-coreutils@gnu.org From: John Steele Scott Subject: touch unnecessarily calls dup2 Message-ID: <7c66c637-fd04-ff02-0c49-b0ba6df20422@toojays.net> Date: Tue, 1 May 2018 20:08:50 +0930 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Language: en-AU X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2001:4830:134:3::11 X-Spam-Score: -5.0 (-----) X-Debbugs-Envelope-To: submit X-Mailman-Approved-At: Tue, 01 May 2018 11:27:26 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -6.0 (------) >From https://stackoverflow.com/questions/40446555/why-does-touch-call-the-dup2-syscall jscott@citra:/tmp$ touch --version | head -1 touch (GNU coreutils) 8.25 jscott@citra:/tmp$ strace -ttt touch foo 2>&1 | tail -9 1525170579.952032 open("foo", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3 1525170579.952080 dup2(3, 0)            = 0 1525170579.952119 close(3)              = 0 1525170579.952156 utimensat(0, NULL, NULL, 0) = 0 1525170579.952209 close(0)              = 0 1525170579.952257 close(1)              = 0 1525170579.952294 close(2)              = 0 1525170579.952333 exit_group(0)         = ? 1525170579.952450 +++ exited with 0 +++ My analysis from that discussion:     It's a historical artifact.     The open()+dup2() pattern comes from the fd_reopen() function, which is used     by several of the programs in the coreutils code base.     Prior to coreutils commit e373bb1, fd_reopen() did not do open()+dup2(), but     closed the desired file descriptor before opening a new one. This was the     case when touch started using this function at coreutils commit 478bd89. Per     that commit message, the intent was to reduce the number of file descriptors     touch would have open. In the scheme of things the extra call to dup2() is not a big deal. We've lived with it for 10 years. On my laptop it would take 25,000 invocations of touch before those unnecessary dup2() calls add up to a second. But touch is called a lot, on systems all over the world. So maybe you guys care about this? Cheers, John From debbugs-submit-bounces@debbugs.gnu.org Mon Oct 29 23:17:24 2018 Received: (at 31332) by debbugs.gnu.org; 30 Oct 2018 03:17:24 +0000 Received: from localhost ([127.0.0.1]:52770 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gHKWx-0004YJ-O5 for submit@debbugs.gnu.org; Mon, 29 Oct 2018 23:17:24 -0400 Received: from mail-pl1-f169.google.com ([209.85.214.169]:39916) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gHKWv-0004TC-Nb; Mon, 29 Oct 2018 23:17:22 -0400 Received: by mail-pl1-f169.google.com with SMTP id b5-v6so4178780pla.6; Mon, 29 Oct 2018 20:17:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=NMxitYyeeFmmMZ5s4/JXrObzwk6fXO5b7xYMYAMPgpI=; b=T4XHkuOgfxRl9lHvsDOcWO38TMTzdSZSP+ZuPghqX23lh4BpSe/wQpShtQXNpDm75u 4/mukitUJPirME6MIKfuW8I+MX+k6mz3FCtfr9h2pDKVM0ZoyCL8ANO6TDTbPSeQpuZi ulVV5S6Xw+8ihApjB31ysYIqsIIgAcZx1lBsCQ1YTKeUwUv3ei/+cmBRtyQlk5IWK8bs XPgG7dza9viCDnyUSBV3mr9NpmzTI/dBhp24dmhfLz1CzRnDSgn6DPpFwJKMg/ITseNB 7drFfi5l7xonkx/UeUSVC5EF+Ne+JRhL3CjL9k7Kj3XGzhlkQnHgueNt2zaAK1sSO7P+ +Y3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=NMxitYyeeFmmMZ5s4/JXrObzwk6fXO5b7xYMYAMPgpI=; b=scdUnlTBO/qbyQncWkWdDaVb5SPRsIp/Us8++Oz4jmfEQGSJo5/PID4AXZ9H3Yv1TA iLxVrbkHs1e8Kw7nA1dJNwyn6J7C1S4eV1+MbjHAqBNkAdTAOj3RWQ9PjpX6fPf7aIZF JBZhqX3hKfnj4YVnHurgPwTyxwZzRm5ZLlBt8vyEyqseJYBiFuy1+E343wJByhE3XaHl pKpORY5D5wnKtEytwZuMhJdIURL47uSn6xZ6u3ZO7oyhMFuss49ugQS75c7XvEWENrIL 7ZvTu5kN3OFnziCpEYk4YFniAtxxoHxV1K0l4mWzPCTPxMRzZiWsg3pVo0akiqjfBXM6 7ZMQ== X-Gm-Message-State: AGRZ1gKWLo8GUFULR5Ym++jj3f/geLqRTc3Vh0jR4/rn67NkB/sb96if ttcaaEq4y80FcPdT6x3V5D9aoIRVFr8= X-Google-Smtp-Source: AJdET5ddSU8/OhXdsjo0wssQNx+QeVF2sdUYOfRkRSG+oMB85xNGICrqREScP0fEWObYqV4YKPWAFA== X-Received: by 2002:a17:902:8b8a:: with SMTP id ay10-v6mr16869330plb.130.1540869435111; Mon, 29 Oct 2018 20:17:15 -0700 (PDT) Received: from tomato.housegordon.com (moose.housegordon.com. [184.68.105.38]) by smtp.googlemail.com with ESMTPSA id p14-v6sm27486831pgn.45.2018.10.29.20.17.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 29 Oct 2018 20:17:13 -0700 (PDT) Subject: Re: bug#31332: touch unnecessarily calls dup2 To: John Steele Scott , 31332@debbugs.gnu.org References: <7c66c637-fd04-ff02-0c49-b0ba6df20422@toojays.net> From: Assaf Gordon Message-ID: Date: Mon, 29 Oct 2018 21:17:07 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <7c66c637-fd04-ff02-0c49-b0ba6df20422@toojays.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 31332 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) tags 31332 notabug close 31332 stop On 2018-05-01 4:38 a.m., John Steele Scott wrote: > From https://stackoverflow.com/questions/40446555/why-does-touch-call-the-dup2-syscall > > jscott@citra:/tmp$ touch --version | head -1 > touch (GNU coreutils) 8.25 > jscott@citra:/tmp$ strace -ttt touch foo 2>&1 | tail -9 > 1525170579.952032 open("foo", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3 > 1525170579.952080 dup2(3, 0)            = 0 > 1525170579.952119 close(3)              = 0 > 1525170579.952156 utimensat(0, NULL, NULL, 0) = 0 > 1525170579.952209 close(0)              = 0 > 1525170579.952257 close(1)              = 0 > 1525170579.952294 close(2)              = 0 > 1525170579.952333 exit_group(0)         = ? > 1525170579.952450 +++ exited with 0 +++ > > My analysis from that discussion: > >     It's a historical artifact. > >     The open()+dup2() pattern comes from the fd_reopen() function, which is used >     by several of the programs in the coreutils code base. > Not exactly an "artifact" - but an indented operation. The goal of "fd_reopen" is not just to open a file, but to ensure the returned file descriptor (an "int") has a specific value. For example, standard input (STDIN) is typically file descriptor value 0. calling fd_reopen first opens the file (the kernel returns file descriptor 3), then it ensures file descriptor 0 is associated with the same file (and then, calling "close(3)" gets rid of the other file descriptor). Checking how fd_reopen is used in coreutils, it is almost always used to ensure STDIN/STDOUT point to the desired file(s): ==== $ git grep fd_reopen src/csplit.c: if (! STREQ (name, "-") && fd_reopen (STDIN_FILENO, name, O_RDONLY, 0) < 0) src/dd.c:/* Restart on EINTR from fd_reopen(). */ src/dd.c:ifd_reopen (int desired_fd, char const *file, int flag, mode_t mode) src/dd.c: ret = fd_reopen (desired_fd, file, flag, mode); src/dd.c: if (ifd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0) src/dd.c: || ifd_reopen (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0) src/dd.c: && (ifd_reopen (STDOUT_FILENO, output_file, O_WRONLY | opts, perms) src/nohup.c: if (fd_reopen (STDIN_FILENO, "/dev/null", O_WRONLY, 0) < 0) src/nohup.c: ? fd_reopen (STDOUT_FILENO, file, flags, mode) src/nohup.c: ? fd_reopen (STDOUT_FILENO, in_home, flags, mode) src/split.c: && fd_reopen (STDIN_FILENO, infile, O_RDONLY, 0) < 0) src/stty.c: if (fd_reopen (STDIN_FILENO, device_name, O_RDONLY | O_NONBLOCK, 0) < 0) src/touch.c: fd = fd_reopen (STDIN_FILENO, file, === This means the rest of the program can just operate on STDIN (or STDOUT). As such, I'm closing this item. Discussion can continue by replying to this thread. -assaf From debbugs-submit-bounces@debbugs.gnu.org Tue Oct 30 00:19:08 2018 Received: (at 31332) by debbugs.gnu.org; 30 Oct 2018 04:19:08 +0000 Received: from localhost ([127.0.0.1]:52975 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gHLUh-0006Wi-Q2 for submit@debbugs.gnu.org; Tue, 30 Oct 2018 00:19:08 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:40539) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gHLJW-00049e-8g for 31332@debbugs.gnu.org; Tue, 30 Oct 2018 00:07:34 -0400 Received: by mail-pf1-f194.google.com with SMTP id g21-v6so5111776pfi.7 for <31332@debbugs.gnu.org>; Mon, 29 Oct 2018 21:07:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toojays-net.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=pmjeO+PFUrKrPNkyGiI0KGfIv/ovnzwjaB8W3QnZ/4M=; b=NJuQKLCov7Pwk4vbVwW5HyGH5RnNTgDzT86glMcWawkKOuJgnib38OCGqYIpTjlg0d eo56tjtf3g/PzGbR2E4cfnkpxOYUxZz80RKdtCvnfjB+NGFEjz4RdNd4nSYmN5c+eKeZ 4PMtekYL3I7nz+9Y6wXeF4MfU00Kk4rB0ZYZoQESZHoDBPFCr2E+7VjcGrl6ZqbEYBBQ RsksoStga3+DqqoY1k/1/iUCzCPMtfNwPAaydzhP+D+M5y3Ad6+q+AH65F/ejOFRlMWF 8Cl0KPl51IX6HxkMmwX7tfHZAYj3moE0sKp0COi2jDZxHljjZA4xJ/aZc5SfU6F3HOS6 wXbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=pmjeO+PFUrKrPNkyGiI0KGfIv/ovnzwjaB8W3QnZ/4M=; b=YLQoHmfRHhGJUNwB1EyVVxgkFoKV8raDYd6UFeLSShdb7LlKL2pD7jNAHZbeG/T3T/ UEp8AiZZYuZ/6OH/P17nrP33nXfx6B0GrKtA3wPKwai8B+TsO76VTmHBWELG4ZoZ7iUf Me4SMxoo6fEWA01oiWnq0mej1Dw/1XO4oYeaHR4G+qyTA3kyTdcu78Ks9sKjQ73WVQnB 0IKs54K0yqE6RsQVtz5nj62JK9yMDnBKqVg8h5N7Rlzw1iZTssTR0lX3ZZOTmadXVu3e U5e5hGFWTd0Y4/MVf9P0mJ2kh+bnOQTuUMJ3iLDR5/ci5ZpPp5cJ1ecBl6eeDfWPHHjN vVqw== X-Gm-Message-State: AGRZ1gKv14BqCVboYP1hb7qUvupzZ3HPa5Dp3jVg53YarA7KyJioJM/U VG6av/BOg82fKSFJvE8eWOdfHJjzcZg= X-Google-Smtp-Source: AJdET5cKFDQiSVWsSSEgqwO8M93Ib84BLpa2RZqMTrKEYyI+Cu35gYIIss6tstXxaEv1pGdRe7jtFQ== X-Received: by 2002:a62:89d7:: with SMTP id n84-v6mr1257192pfk.255.1540872447927; Mon, 29 Oct 2018 21:07:27 -0700 (PDT) Received: from [10.161.88.111] ([146.174.200.100]) by smtp.gmail.com with ESMTPSA id q128-v6sm7041372pfb.160.2018.10.29.21.07.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 29 Oct 2018 21:07:26 -0700 (PDT) Subject: Re: bug#31332: touch unnecessarily calls dup2 To: Assaf Gordon , 31332@debbugs.gnu.org References: <7c66c637-fd04-ff02-0c49-b0ba6df20422@toojays.net> From: John Steele Scott Message-ID: <764d2d89-1454-b063-7e41-054475c6dac7@toojays.net> Date: Tue, 30 Oct 2018 14:37:23 +1030 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Language: en-AU X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 31332 X-Mailman-Approved-At: Tue, 30 Oct 2018 00:19:06 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) Hi Assif, Thanks for your reply. On 30/10/18 1:47 pm, Assaf Gordon wrote: > tags 31332 notabug > close 31332 > stop > > On 2018-05-01 4:38 a.m., John Steele Scott wrote: >>  From https://stackoverflow.com/questions/40446555/why-does-touch-call-the-dup2-syscall >> >> jscott@citra:/tmp$ touch --version | head -1 >> touch (GNU coreutils) 8.25 >> jscott@citra:/tmp$ strace -ttt touch foo 2>&1 | tail -9 >> 1525170579.952032 open("foo", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3 >> 1525170579.952080 dup2(3, 0)            = 0 >> 1525170579.952119 close(3)              = 0 >> 1525170579.952156 utimensat(0, NULL, NULL, 0) = 0 >> 1525170579.952209 close(0)              = 0 >> 1525170579.952257 close(1)              = 0 >> 1525170579.952294 close(2)              = 0 >> 1525170579.952333 exit_group(0)         = ? >> 1525170579.952450 +++ exited with 0 +++ >> >> My analysis from that discussion: >> >>      It's a historical artifact. >> >>      The open()+dup2() pattern comes from the fd_reopen() function, which is used >>      by several of the programs in the coreutils code base. >> > > Not exactly an "artifact" - but an indented operation. > I called it a historical artefact because the call to dup2() did not occur when touch started calling fd_reopen(). Prior to http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=commitdiff;h=e373bb19 fd_reopen() initially did "close(desired_fd); fd = open(...)" which would always do the right thing if stdin was the desired fd. No call to dup2(). > The goal of "fd_reopen" is not just to open a file, > but to ensure the returned file descriptor (an "int") has a specific > value. > > For example, standard input (STDIN) is typically file descriptor value 0. calling fd_reopen first opens the file (the kernel returns file descriptor 3), then it ensures file descriptor 0 is associated with the same file (and then, calling "close(3)" gets rid of the other file descriptor). > > Checking how fd_reopen is used in coreutils, it is almost always used > to ensure STDIN/STDOUT point to the desired file(s): > ==== > $ git grep fd_reopen > src/csplit.c:  if (! STREQ (name, "-") && fd_reopen (STDIN_FILENO, name, O_RDONLY, 0) < 0) > src/dd.c:/* Restart on EINTR from fd_reopen().  */ > src/dd.c:ifd_reopen (int desired_fd, char const *file, int flag, mode_t mode) > src/dd.c:      ret = fd_reopen (desired_fd, file, flag, mode); > src/dd.c:      if (ifd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0) > src/dd.c:           || ifd_reopen (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0) > src/dd.c:          && (ifd_reopen (STDOUT_FILENO, output_file, O_WRONLY | opts, perms) > src/nohup.c:      if (fd_reopen (STDIN_FILENO, "/dev/null", O_WRONLY, 0) < 0) > src/nohup.c:                ? fd_reopen (STDOUT_FILENO, file, flags, mode) > src/nohup.c:                        ? fd_reopen (STDOUT_FILENO, in_home, flags, mode) > src/split.c:      && fd_reopen (STDIN_FILENO, infile, O_RDONLY, 0) < 0) > src/stty.c:      if (fd_reopen (STDIN_FILENO, device_name, O_RDONLY | O_NONBLOCK, 0) < 0) > src/touch.c:      fd = fd_reopen (STDIN_FILENO, file, > === > > This means the rest of the program can just operate on STDIN (or STDOUT). I haven't looked at all those cases, but my point was that the call to dup2() adds an extra 40us to each invocation of touch. In the case of touch, it really only cares about the fd being STDOUT_FILENO or !STDOUT_FILENO; it doesn't need to be STDIN_FILENO exactly. With a small amount of work touch could use open() instead of fd_reopen(). The code would be somewhat simpler and it would save 40us of execution time. Or maybe it wouldn't, since there's an extra fd open when the program terminates and we need to wait for the kernel to clean that up anyway? I don't know. Cheers, John From debbugs-submit-bounces@debbugs.gnu.org Tue Oct 30 01:38:45 2018 Received: (at 31332) by debbugs.gnu.org; 30 Oct 2018 05:38:45 +0000 Received: from localhost ([127.0.0.1]:53110 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gHMjl-00009X-3f for submit@debbugs.gnu.org; Tue, 30 Oct 2018 01:38:45 -0400 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:44286) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gHMjj-00009K-79 for 31332@debbugs.gnu.org; Tue, 30 Oct 2018 01:38:43 -0400 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 7A15A160056; Mon, 29 Oct 2018 22:38:37 -0700 (PDT) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 4hgcEgcDhkYX; Mon, 29 Oct 2018 22:38:36 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 8DFE316005E; Mon, 29 Oct 2018 22:38:36 -0700 (PDT) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 4cJ6qKWlq_HN; Mon, 29 Oct 2018 22:38:36 -0700 (PDT) Received: from [192.168.1.9] (cpe-23-242-74-103.socal.res.rr.com [23.242.74.103]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 6642E160056; Mon, 29 Oct 2018 22:38:36 -0700 (PDT) Subject: Re: bug#31332: touch unnecessarily calls dup2 To: John Steele Scott , Assaf Gordon , 31332@debbugs.gnu.org References: <7c66c637-fd04-ff02-0c49-b0ba6df20422@toojays.net> <764d2d89-1454-b063-7e41-054475c6dac7@toojays.net> From: Paul Eggert Organization: UCLA Computer Science Department Message-ID: <0b5b0df4-a28d-99e7-3ade-b97c1cb3ef07@cs.ucla.edu> Date: Mon, 29 Oct 2018 22:38:36 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <764d2d89-1454-b063-7e41-054475c6dac7@toojays.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 31332 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) John Steele Scott wrote: > Prior tohttp://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=commitdiff;h=e373bb19 fd_reopen() initially did "close(desired_fd); fd = open(...)" which would always do the right thing No, as the old code did the wrong thing for "touch /dev/stdin", whereas the current code works. From debbugs-submit-bounces@debbugs.gnu.org Tue Oct 30 02:24:21 2018 Received: (at 31332) by debbugs.gnu.org; 30 Oct 2018 06:24:21 +0000 Received: from localhost ([127.0.0.1]:53133 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gHNRs-0001K0-CQ for submit@debbugs.gnu.org; Tue, 30 Oct 2018 02:24:20 -0400 Received: from mail-pf1-f175.google.com ([209.85.210.175]:38315) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gHNIn-00013m-4g for 31332@debbugs.gnu.org; Tue, 30 Oct 2018 02:14:57 -0400 Received: by mail-pf1-f175.google.com with SMTP id b11-v6so5268624pfi.5 for <31332@debbugs.gnu.org>; Mon, 29 Oct 2018 23:14:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toojays-net.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=6UxZoheMCYc2kNZAFkoCk9pw3acH5071dU7pPKc2AFU=; b=Nn6pdkbRbldyttzZdVnJp7MoyrJAsAKqq3GdgFV99GeIFvj5xofKDw6wDu+0eZrm6W rg60MLREyjj8BiEl5qsUzPgN8vYtiw2gOsIjAqWnPOPFBptrDu81R6j7hUujsGRm3l1F GNGX3oRQXJ7D534rlu9KQ90yAobx7HMc+gIYbgZjdObq0kCkaaMTfEnXlyzXKTbeyDOo /qLGqME9fGKm1kLD3+hHTAfguT6g/Ve86MMcx45xNNjr/G7zJPa6G9NsMvOKouTIFPmc jIAKHYXaAr5WCrKrCesxDRBfhRZWMvGIIcgyreOq0jr7IPS7+ceRqKyBLxpUoyWtFLYD Aj3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=6UxZoheMCYc2kNZAFkoCk9pw3acH5071dU7pPKc2AFU=; b=owuM4l9eF8a+ocBmhpO7vjLUu8HT/yRkOfLAa+Gaqb2wteNWpN3+jekrs66q3f0Lht yR7W57K/UA10SPTCDwoXof5YY+jcsGyuR8LYUj6j8XCBX48ivgnExsN4qfVxEp2Vzr01 Q8mkHQQsaocYReuMA5tilhOoe0Z0dSH2giGbhUeCYXzeZn98Wfms1qbEsUEO3w4b5R0o n7ceg2M08a0CZi1/M8NUgEbtuvrAsOZibm7EzPMB3CFGKk1ggIa8dOpnwXK+LPnY7LY3 p9ij/qC3kHG4sBgE6ZJJKgD5QESBsmp1RUue0QKKwLzmu/S5IkUScJGWW7c6sjUq4L+S uKXA== X-Gm-Message-State: AGRZ1gKWS/4L/X19/qc0Y89dEfJifCXKdb3UbBoH35g7ZezKsLOy0H5O o+FyJk3Mmw8IM8ZpXw681Kz2+btIuyY= X-Google-Smtp-Source: AJdET5fVX4pKGp2Ek85wnz2up+JMLgrb7jG2EJjaMeHsXrJmfUeQUFdwcXS7VkqEIQMT+48ISRSwgg== X-Received: by 2002:a62:9c4a:: with SMTP id f71-v6mr1630300pfe.135.1540880090609; Mon, 29 Oct 2018 23:14:50 -0700 (PDT) Received: from [10.161.88.111] ([146.174.200.100]) by smtp.gmail.com with ESMTPSA id x63-v6sm14253644pfk.14.2018.10.29.23.14.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 29 Oct 2018 23:14:49 -0700 (PDT) Subject: Re: bug#31332: touch unnecessarily calls dup2 To: Paul Eggert , Assaf Gordon , 31332@debbugs.gnu.org References: <7c66c637-fd04-ff02-0c49-b0ba6df20422@toojays.net> <764d2d89-1454-b063-7e41-054475c6dac7@toojays.net> <0b5b0df4-a28d-99e7-3ade-b97c1cb3ef07@cs.ucla.edu> From: John Steele Scott Message-ID: <8255d0af-373c-6e4e-431a-9ef728fe14b5@toojays.net> Date: Tue, 30 Oct 2018 16:44:45 +1030 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <0b5b0df4-a28d-99e7-3ade-b97c1cb3ef07@cs.ucla.edu> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Language: en-AU X-Spam-Score: -0.0 (/) X-Debbugs-Envelope-To: 31332 X-Mailman-Approved-At: Tue, 30 Oct 2018 02:24:19 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) On 30/10/18 4:08 pm, Paul Eggert wrote: > John Steele Scott wrote: >> Prior tohttp://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=commitdiff;h=e373bb19  fd_reopen() initially did "close(desired_fd); fd = open(...)" which would always do the right thing > > No, as the old code did the wrong thing for "touch /dev/stdin", whereas the current code works. I stand corrected. Okay, I see that we don't want to close /dev/stdin at the start of touch. Thanks for making me think of this case. I'd much much obliged if one of you could enlighten me as to why touch needs to treat /dev/stdin as a special case after the file has been opened though. Wouldn't the following work just as well, with one less system call? --- a/src/touch.c +++ b/src/touch.c @@ -132,8 +132,8 @@ touch (const char *file)    else if (! (no_create || no_dereference))      {        /* Try to open FILE, creating it if necessary.  */ -      fd = fd_reopen (STDIN_FILENO, file, -                      O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY, MODE_RW_UGO); +      fd = open (file, +                 O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY, MODE_RW_UGO);          /* Don't save a copy of errno if it's EISDIR, since that would lead           touch to give a bogus diagnostic for e.g., 'touch /' (assuming @@ -166,9 +166,9 @@ touch (const char *file)                       (no_dereference && fd == -1) ? AT_SYMLINK_NOFOLLOW : 0)          == 0);   -  if (fd == STDIN_FILENO) +  if (fd != STDOUT_FILENO)      { -      if (close (STDIN_FILENO) != 0) +      if (close (fd) != 0)          {            error (0, errno, _("failed to close %s"), quoteaf (file));            return false; Cheers, John From debbugs-submit-bounces@debbugs.gnu.org Mon Nov 12 17:23:15 2018 Received: (at 31332) by debbugs.gnu.org; 12 Nov 2018 22:23:15 +0000 Received: from localhost ([127.0.0.1]:48401 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gMKbx-00040z-Su for submit@debbugs.gnu.org; Mon, 12 Nov 2018 17:23:15 -0500 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:47828) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gMKbv-00040l-Ox for 31332@debbugs.gnu.org; Mon, 12 Nov 2018 17:23:12 -0500 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id B56541600EF; Mon, 12 Nov 2018 14:23:05 -0800 (PST) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id SsmMP9WjIZAA; Mon, 12 Nov 2018 14:23:05 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id F3BE31600ED; Mon, 12 Nov 2018 14:23:04 -0800 (PST) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id f1OLTzFMS5oW; Mon, 12 Nov 2018 14:23:04 -0800 (PST) Received: from [192.168.1.9] (cpe-23-242-74-103.socal.res.rr.com [23.242.74.103]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id BEA371600E9; Mon, 12 Nov 2018 14:23:04 -0800 (PST) Subject: Re: bug#31332: touch unnecessarily calls dup2 To: John Steele Scott , Assaf Gordon , 31332@debbugs.gnu.org References: <7c66c637-fd04-ff02-0c49-b0ba6df20422@toojays.net> <764d2d89-1454-b063-7e41-054475c6dac7@toojays.net> <0b5b0df4-a28d-99e7-3ade-b97c1cb3ef07@cs.ucla.edu> <8255d0af-373c-6e4e-431a-9ef728fe14b5@toojays.net> From: Paul Eggert Organization: UCLA Computer Science Department Message-ID: Date: Mon, 12 Nov 2018 14:23:04 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <8255d0af-373c-6e4e-431a-9ef728fe14b5@toojays.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 31332 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) John Steele Scott wrote: > I'd much much obliged if one of you could enlighten me as to why touch = needs to treat /dev/stdin as a special case after the file has been opene= d though. Wouldn't the following work just as well, with one less system = call? >=20 > --- a/src/touch.c > +++ b/src/touch.c > @@ -132,8 +132,8 @@ touch (const char *file) > =C2=A0=C2=A0 else if (! (no_create || no_dereference)) > =C2=A0=C2=A0=C2=A0=C2=A0 { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* Try to open FILE, creating it = if necessary.=C2=A0 */ > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 fd =3D fd_reopen (STDIN_FILENO, file, > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 O_WRONLY | O_CREAT= | O_NONBLOCK | O_NOCTTY, MODE_RW_UGO); > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 fd =3D open (file, > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0 O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY, MODE= _RW_UGO); > =20 > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* Don't save a copy of errno if = it's EISDIR, since that would lead > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 touch to give a= bogus diagnostic for e.g., 'touch /' (assuming > @@ -166,9 +166,9 @@ touch (const char *file) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (no_dereference &&= fd =3D=3D -1) ? AT_SYMLINK_NOFOLLOW : 0) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =3D=3D 0); > =20 > -=C2=A0 if (fd =3D=3D STDIN_FILENO) > +=C2=A0 if (fd !=3D STDOUT_FILENO) > =C2=A0=C2=A0=C2=A0=C2=A0 { > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (close (STDIN_FILENO) !=3D 0) > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (close (fd) !=3D 0) That patch has trouble if 'open' returns 1. The resulting fd is never clo= sed and=20 for some filesystems (NFS, for example) one needs to close the fd to find= out=20 whether the fdutimensat call actually worked. From debbugs-submit-bounces@debbugs.gnu.org Mon Nov 12 17:41:51 2018 Received: (at 31332) by debbugs.gnu.org; 12 Nov 2018 22:41:51 +0000 Received: from localhost ([127.0.0.1]:48412 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gMKtx-0004Rr-UK for submit@debbugs.gnu.org; Mon, 12 Nov 2018 17:41:51 -0500 Received: from mail-pg1-f178.google.com ([209.85.215.178]:36365) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gMKtv-0004Re-Nq for 31332@debbugs.gnu.org; Mon, 12 Nov 2018 17:41:48 -0500 Received: by mail-pg1-f178.google.com with SMTP id z17-v6so4705764pgv.3 for <31332@debbugs.gnu.org>; Mon, 12 Nov 2018 14:41:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toojays-net.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=1cAcwU9//S/5OKHHLBEXgWvaMQC1MW9hD4daF/7EbOg=; b=TiIH5GjhKbRYqkF24psmEx23q2qHXeu18LsvO03b3cRrLYHLeQnOd3WjOmaJ126QVB C/KDkRRxDiKo4Zgt4h09VnCvNh8OZ2xMiqet16064XOqosaP/KOWQvns3Y65KsHlzxo4 TvvT5lIeATtS63DOh8DJAwYBWlV3HX0yvaHuESDnehYWhpT/s5tfA7mHPVOTFISUseI9 KYsVEIQEU1oYaCGa/WB2vKQxymA8fYxVZhP0LH2tLdqQzGap37zWj7Z6JtG3n5q6n+br udndDv1M3GH9nUrHgwv4celuL1IRc+GFuUTFAoyKl9PD4nHqdYnHwAq6rY83OX6TJSgW dwsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=1cAcwU9//S/5OKHHLBEXgWvaMQC1MW9hD4daF/7EbOg=; b=HDQKstfNyqNNrSzyWAg5FOs2XRS4sW3S6ugQVvQy3CBLI8R7Bzzq0KDTLfYcCrUP7z vd3VTtcICmui1SoZ1eMQI71Y0QqzoVHTx5fLej4FTFfDvD+6FitJQFSsll0LZXZrF0l/ hI9uuuvgTfFl693ZIql/6szkndp2YKi1fJWExKqv4Eg1qXQLkuFrQTPFTEnd+teAQlCv uWKqHR6HNcN047L7iL0Dz0NmwuwCtlkb2rE3EIDwJwXtfcIwQaeBU4MibcO0GYdf4Bw8 iaM0Kw668qV5PX5lsrbooAefOOaZ3hsXiCqGW8dP0hS1x/9yBEm0lg3ZobfNjCrVKrs3 RmZw== X-Gm-Message-State: AGRZ1gLabKrLDmIScIl1TJFMS2SLFw8Ev5HUShrx3MTRm8xJhyFtdWLP Yl31moOlmgh2I9K1f2l2UUOfpzyIvZU= X-Google-Smtp-Source: AJdET5cxsf9u0ED2sm9a+8QIfaOJrTi1AoHIwyyz4G4SQ0gPdn+JIXuYGpeq6qqvcidAArDKBscWew== X-Received: by 2002:a63:b4c:: with SMTP id a12mr2485929pgl.131.1542062501372; Mon, 12 Nov 2018 14:41:41 -0800 (PST) Received: from [10.161.88.111] ([146.174.200.100]) by smtp.gmail.com with ESMTPSA id d24-v6sm30641125pfe.40.2018.11.12.14.41.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Nov 2018 14:41:40 -0800 (PST) Subject: Re: bug#31332: touch unnecessarily calls dup2 To: Paul Eggert , Assaf Gordon , 31332@debbugs.gnu.org References: <7c66c637-fd04-ff02-0c49-b0ba6df20422@toojays.net> <764d2d89-1454-b063-7e41-054475c6dac7@toojays.net> <0b5b0df4-a28d-99e7-3ade-b97c1cb3ef07@cs.ucla.edu> <8255d0af-373c-6e4e-431a-9ef728fe14b5@toojays.net> From: John Steele Scott Message-ID: <1675a6d5-bb96-3d5a-7e85-b75ccea7c57b@toojays.net> Date: Tue, 13 Nov 2018 09:11:36 +1030 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Language: en-AU X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 31332 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) On 13/11/18 8:53 am, Paul Eggert wrote: > John Steele Scott wrote: >> I'd much much obliged if one of you could enlighten me as to why touch needs to treat /dev/stdin as a special case after the file has been opened though. Wouldn't the following work just as well, with one less system call? >> >> --- a/src/touch.c >> +++ b/src/touch.c >> @@ -132,8 +132,8 @@ touch (const char *file) >>     else if (! (no_create || no_dereference)) >>       { >>         /* Try to open FILE, creating it if necessary.  */ >> -      fd = fd_reopen (STDIN_FILENO, file, >> -                      O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY, MODE_RW_UGO); >> +      fd = open (file, >> +                 O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY, MODE_RW_UGO); >>           /* Don't save a copy of errno if it's EISDIR, since that would lead >>            touch to give a bogus diagnostic for e.g., 'touch /' (assuming >> @@ -166,9 +166,9 @@ touch (const char *file) >>                        (no_dereference && fd == -1) ? AT_SYMLINK_NOFOLLOW : 0) >>           == 0); >>   -  if (fd == STDIN_FILENO) >> +  if (fd != STDOUT_FILENO) >>       { >> -      if (close (STDIN_FILENO) != 0) >> +      if (close (fd) != 0) > > That patch has trouble if 'open' returns 1. The resulting fd is never closed and for some filesystems (NFS, for example) one needs to close the fd to find out whether the fdutimensat call actually worked. Thanks Paul, So if someone did want to wipe out that dup2 call from GNU touch, they'd need to track separately whether they opened the file or whether they are using a passed-in file descriptor, rather than relying on the value of fd to indicate that. Got it. I'll stop flogging this dead horse now. Thanks both Paul and Assaf for your replies. Cheers, John From unknown Sun Jun 22 11:35:08 2025 Received: (at fakecontrol) by fakecontrolmessage; To: internal_control@debbugs.gnu.org From: Debbugs Internal Request Subject: Internal Control Message-Id: bug archived. Date: Tue, 11 Dec 2018 12:24:04 +0000 User-Agent: Fakemail v42.6.9 # This is a fake control message. # # The action: # bug archived. thanks # This fakemail brought to you by your local debbugs # administrator