All of lore.kernel.org
 help / color / mirror / Atom feed
From: larsxschneider@gmail.com
To: git@vger.kernel.org
Cc: Johannes.Schindelin@gmx.de, e@80x24.org, jnareb@gmail.com,
	gitster@pobox.com, Lars Schneider <larsxschneider@gmail.com>
Subject: [PATCH v2 2/2] read-cache: make sure file handles are not inherited by child processes
Date: Mon, 24 Oct 2016 20:03:00 +0200	[thread overview]
Message-ID: <20161024180300.52359-3-larsxschneider@gmail.com> (raw)
In-Reply-To: <20161024180300.52359-1-larsxschneider@gmail.com>

From: Lars Schneider <larsxschneider@gmail.com>

This fixes "convert: add filter.<driver>.process option" (edcc8581) on
Windows.

Consider the case of a file that requires filtering and is present in
branch A but not in branch B. If A is the current HEAD and we checkout B
then the following happens:

1. ce_compare_data() opens the file
2.   index_fd() detects that the file requires to run a clean filter and
     calls index_stream_convert_blob()
4.     index_stream_convert_blob() calls convert_to_git_filter_fd()
5.       convert_to_git_filter_fd() calls apply_filter() which creates a
         new long running filter process (in case it is the first file
         of this kind to be filtered)
6.       The new filter process inherits all file handles. This is the
         default on Linux/OSX and is explicitly defined in the
         `CreateProcessW` call in `mingw.c` on Windows.
7. ce_compare_data() closes the file
8. Git unlinks the file as it is not present in B

The unlink operation does not work on Windows because the filter process
has still an open handle to the file. On Linux/OSX the unlink operation
succeeds but the file descriptors still leak into the child process.

Fix this problem by opening files in read-cache with the CLOEXEC flag to
ensure that the file descriptor does not remain open in a newly spawned
process similar to 05d1ed61.

Signed-off-by: Lars Schneider <larsxschneider@gmail.com>
---
 read-cache.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/read-cache.c b/read-cache.c
index 38d67fa..b594865 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -156,7 +156,11 @@ void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
 static int ce_compare_data(const struct cache_entry *ce, struct stat *st)
 {
 	int match = -1;
-	int fd = open(ce->name, O_RDONLY);
+	int fd = open(ce->name, O_RDONLY | O_CLOEXEC);
+
+	if (O_CLOEXEC && fd < 0 && errno == EINVAL)
+		/* Try again w/o O_CLOEXEC: the kernel might not support it */
+		fd = open(ce->name, O_RDONLY);
 
 	if (fd >= 0) {
 		unsigned char sha1[20];
-- 
2.10.0


  parent reply	other threads:[~2016-10-24 18:03 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-24 18:02 [PATCH v2 0/2] Use CLOEXEC to avoid fd leaks larsxschneider
2016-10-24 18:02 ` [PATCH v2 1/2] sha1_file: open window into packfiles with CLOEXEC larsxschneider
2016-10-25 10:27   ` Johannes Schindelin
2016-10-25 16:58     ` Junio C Hamano
2016-10-24 18:03 ` larsxschneider [this message]
2016-10-24 18:39   ` [PATCH v2 2/2] read-cache: make sure file handles are not inherited by child processes Eric Wong
2016-10-24 19:53     ` Junio C Hamano
2016-10-25 10:33       ` Johannes Schindelin
2016-10-25 17:02         ` Junio C Hamano
2016-10-24 19:22   ` Johannes Sixt
2016-10-24 19:53     ` Lars Schneider
2016-10-25 21:39       ` Johannes Sixt
2016-10-24 18:23 ` [PATCH v2 0/2] Use CLOEXEC to avoid fd leaks Junio C Hamano
2016-10-25 11:27 ` Johannes Schindelin
2016-10-25 18:16   ` [PATCH v3 0/3] quick reroll of Lars's git_open() w/ O_CLOEXEC Junio C Hamano
2016-10-25 18:16     ` [PATCH v3 1/3] sha1_file: rename git_open_noatime() to git_open() Junio C Hamano
2016-10-25 18:16     ` [PATCH v3 2/3] sha1_file: open window into packfiles with O_CLOEXEC Junio C Hamano
2016-10-26  4:25       ` Jeff King
2016-10-26 16:23         ` Junio C Hamano
2016-10-26 16:47           ` Jeff King
2016-10-26 17:52             ` Junio C Hamano
2016-10-26 20:17               ` Jeff King
2016-10-26 21:15                 ` Junio C Hamano
2016-10-27 10:24                   ` Jeff King
2016-10-27 21:49                     ` Junio C Hamano
2016-10-27 22:38                     ` Linus Torvalds
2016-10-27 22:56                       ` Junio C Hamano
2016-10-27 23:09                         ` Linus Torvalds
2016-10-27 23:19                           ` Linus Torvalds
2016-10-27 23:36                             ` Junio C Hamano
2016-10-27 23:44                               ` Linus Torvalds
2016-10-28  1:08                                 ` Junio C Hamano
2016-10-28  2:37                                   ` Junio C Hamano
2016-10-28  5:51                                     ` Eric Wong
2016-10-28 11:11                                     ` Johannes Schindelin
2016-10-28 16:13                                       ` Linus Torvalds
2016-10-28 16:48                                         ` Junio C Hamano
2016-10-28 17:38                                           ` Linus Torvalds
2016-10-28 17:47                                             ` Junio C Hamano
2016-10-29  1:26                                             ` Junio C Hamano
2016-10-29  8:25                                               ` Johannes Schindelin
2016-10-29 17:06                                                 ` Linus Torvalds
2016-10-31 17:37                                                   ` Junio C Hamano
2016-10-31 13:56                                         ` Jeff King
2016-10-31 17:55                                           ` Junio C Hamano
2016-10-31 18:05                                             ` Jeff King
2016-10-28 13:32                                     ` Junio C Hamano
2016-10-28 13:33                                       ` Junio C Hamano
2016-10-28  7:51                       ` Jeff King
2016-10-25 18:16     ` [PATCH v3 3/3] read-cache: make sure file handles are not inherited by child processes Junio C Hamano
2016-10-25 21:33       ` Eric Wong
2016-10-25 22:54         ` Junio C Hamano
2016-10-25 21:48     ` [PATCH v3 0/3] quick reroll of Lars's git_open() w/ O_CLOEXEC Lars Schneider
2016-10-25 22:56       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161024180300.52359-3-larsxschneider@gmail.com \
    --to=larsxschneider@gmail.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=e@80x24.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jnareb@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.