aboutsummaryrefslogtreecommitdiff
path: root/credential-cache--daemon.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2014-09-14 03:35:06 -0400
committerJunio C Hamano <gitster@pobox.com>2014-09-16 11:11:58 -0700
commitf5e3c0b9d050ebdaf96d3910b01b01695e3ea1a2 (patch)
tree25ba70a24be840428e49b1ff9fcd53b69dba48ac /credential-cache--daemon.c
parent96db324a73fdada6fbe7b63221986f8f18cc63b0 (diff)
downloadgit-f5e3c0b9d050ebdaf96d3910b01b01695e3ea1a2.tar.gz
git-f5e3c0b9d050ebdaf96d3910b01b01695e3ea1a2.tar.xz
credential-cache: close stderr in daemon process
If the stderr of "git credential-cache" is redirected to a pipe, the reader on the other end of a pipe may be surprised that the pipe remains open long after the process exits. This happens because we may auto-spawn a daemon which is long-lived, and which keeps stderr open. We can solve this by redirecting the daemon's stderr to /dev/null once we are ready to go into our event loop. We would not want to do so before then, because we may want to report errors about the setup (e.g., failure to establish the listening socket). This does mean that we will not report errors we encounter for specific clients. That's acceptable, as such errors should be rare (e.g., clients sending buggy requests). However, we also provide an escape hatch: if you want to see these later messages, you can provide the "--debug" option to keep stderr open. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'credential-cache--daemon.c')
-rw-r--r--credential-cache--daemon.c25
1 files changed, 21 insertions, 4 deletions
diff --git a/credential-cache--daemon.c b/credential-cache--daemon.c
index 3b370ca5e..c2f00498f 100644
--- a/credential-cache--daemon.c
+++ b/credential-cache--daemon.c
@@ -2,6 +2,7 @@
#include "credential.h"
#include "unix-socket.h"
#include "sigchain.h"
+#include "parse-options.h"
static const char *socket_path;
@@ -201,7 +202,7 @@ static int serve_cache_loop(int fd)
return 1;
}
-static void serve_cache(const char *socket_path)
+static void serve_cache(const char *socket_path, int debug)
{
int fd;
@@ -211,6 +212,10 @@ static void serve_cache(const char *socket_path)
printf("ok\n");
fclose(stdout);
+ if (!debug) {
+ if (!freopen("/dev/null", "w", stderr))
+ die_errno("unable to point stderr to /dev/null");
+ }
while (serve_cache_loop(fd))
; /* nothing */
@@ -252,16 +257,28 @@ static void check_socket_directory(const char *path)
int main(int argc, const char **argv)
{
- socket_path = argv[1];
+ static const char *usage[] = {
+ "git-credential-cache--daemon [opts] <socket_path>",
+ NULL
+ };
+ int debug = 0;
+ const struct option options[] = {
+ OPT_BOOL(0, "debug", &debug,
+ N_("print debugging messages to stderr")),
+ OPT_END()
+ };
+
+ argc = parse_options(argc, argv, NULL, options, usage, 0);
+ socket_path = argv[0];
if (!socket_path)
- die("usage: git-credential-cache--daemon <socket_path>");
+ usage_with_options(usage, options);
check_socket_directory(socket_path);
atexit(cleanup_socket);
sigchain_push_common(cleanup_socket_on_signal);
- serve_cache(socket_path);
+ serve_cache(socket_path, debug);
return 0;
}