Merge branch 'jk/close-stderr-of-credential-cache-deamon'
authorJunio C Hamano <gitster@pobox.com>
Fri, 26 Sep 2014 21:39:45 +0000 (14:39 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 26 Sep 2014 21:39:45 +0000 (14:39 -0700)
Plug fd leaks.

* jk/close-stderr-of-credential-cache-deamon:
credential-cache: close stderr in daemon process

Documentation/git-credential-cache--daemon.txt
credential-cache--daemon.c
index d15db42d43b9e94628246a30e7c12df41b45b935..7051c6bdf8f542b8fb8c3c4a5a24471355111922 100644 (file)
@@ -8,7 +8,7 @@ git-credential-cache--daemon - Temporarily store user credentials in memory
 SYNOPSIS
 --------
 [verse]
-git credential-cache--daemon <socket>
+git credential-cache--daemon [--debug] <socket>
 
 DESCRIPTION
 -----------
@@ -21,6 +21,10 @@ for `git-credential-cache` clients. Clients may store and retrieve
 credentials. Each credential is held for a timeout specified by the
 client; once no credentials are held, the daemon exits.
 
+If the `--debug` option is specified, the daemon does not close its
+stderr stream, and may output extra diagnostics to it even after it has
+begun listening for clients.
+
 GIT
 ---
 Part of the linkgit:git[1] suite
index 3b370ca5e529c74e17e0f39da5c689ebc4ccfc25..c2f00498f6a171ecde3dd91a5cbdb30d88827495 100644 (file)
@@ -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;
 }