[PATCH v8 07/19] index-helper: add --detach
- From
David Turner <dturner@twopensource.com>
- Date
- May 5, 2016, 21:46 UTC
- Message-ID
- <1462484831-13643-8-git-send-email-dturner@twopensource.com>
- In-Reply-To
- <1462484831-13643-1-git-send-email-dturner@twopensource.com>
From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
We detach after creating and opening the socket, because otherwise we might return control to the shell before index-helper is ready to accept commands. This might lead to flaky tests.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: David Turner <dturner@twopensource.com> --- Documentation/git-index-helper.txt | 3 +++ index-helper.c | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/Documentation/git-index-helper.txt b/Documentation/git-index-helper.txt index 9c2f5eb..e144752 100644 --- a/Documentation/git-index-helper.txt +++ b/Documentation/git-index-helper.txt @@ -34,6 +34,9 @@ OPTIONS for reading an index, but because it will happen in the background, it's not noticable. `--strict` is enabled by default. +--detach:: + Detach from the shell. + NOTES ----- diff --git a/index-helper.c b/index-helper.c index 21fb431..7df7a97 100644 --- a/index-helper.c +++ b/index-helper.c @@ -17,7 +17,7 @@ struct shm { static struct shm shm_index; static struct shm shm_base_index; -static int to_verify = 1; +static int daemonized, to_verify = 1; static void log_warning(const char *warning, ...) { @@ -59,6 +59,8 @@ static void cleanup_shm(void) static void cleanup(void) { + if (daemonized) + return; unlink(git_path("index-helper.sock")); cleanup_shm(); } @@ -311,7 +313,7 @@ static const char * const usage_text[] = { int main(int argc, char **argv) { const char *prefix; - int idle_in_seconds = 600; + int idle_in_seconds = 600, detach = 0; int fd; struct strbuf socket_path = STRBUF_INIT; struct option options[] = { @@ -319,6 +321,7 @@ int main(int argc, char **argv) N_("exit if not used after some seconds")), OPT_BOOL(0, "strict", &to_verify, "verify shared memory after creating"), + OPT_BOOL(0, "detach", &detach, "detach the process"), OPT_END() }; @@ -342,6 +345,14 @@ int main(int argc, char **argv) if (fd < 0) die_errno(_("could not set up index-helper socket")); + if (!freopen("/dev/null", "w", stderr)) + die_errno("failed to redirect stderr to /dev/null"); + + if (!freopen("/dev/null", "w", stdout)) + die_errno("failed to redirect stdout to /dev/null"); + + if (detach && daemonize(&daemonized)) + die_errno(_("unable to detach")); loop(fd, idle_in_seconds); close(fd);
-- 2.4.2.767.g62658d5-twtrsrc