3 #include "parse-options.h"
5 #include "write-or-die.h"
7 #ifndef NO_UNIX_SOCKETS
9 #include "credential.h"
10 #include "string-list.h"
11 #include "unix-socket.h"
12 #include "run-command.h"
14 #define FLAG_SPAWN 0x1
15 #define FLAG_RELAY 0x2
17 #ifdef GIT_WINDOWS_NATIVE
19 static int connection_closed(int error
)
21 return (error
== EINVAL
);
24 static int connection_fatally_broken(int error
)
26 return (error
!= ENOENT
) && (error
!= ENETDOWN
);
31 static int connection_closed(int error
)
33 return (error
== ECONNRESET
);
36 static int connection_fatally_broken(int error
)
38 return (error
!= ENOENT
) && (error
!= ECONNREFUSED
);
43 static int send_request(const char *socket
, const struct strbuf
*out
)
46 int fd
= unix_stream_connect(socket
, 0);
51 if (write_in_full(fd
, out
->buf
, out
->len
) < 0)
52 die_errno("unable to write to cache daemon");
53 shutdown(fd
, SHUT_WR
);
59 r
= read_in_full(fd
, in
, sizeof(in
));
60 if (r
== 0 || (r
< 0 && connection_closed(errno
)))
63 die_errno("read error from cache daemon");
64 write_or_die(1, in
, r
);
71 static void spawn_daemon(const char *socket
)
73 struct child_process daemon
= CHILD_PROCESS_INIT
;
77 strvec_pushl(&daemon
.args
,
78 "credential-cache--daemon", socket
,
84 if (start_command(&daemon
))
85 die_errno("unable to start cache daemon");
86 r
= read_in_full(daemon
.out
, buf
, sizeof(buf
));
88 die_errno("unable to read result code from cache daemon");
89 if (r
!= 3 || memcmp(buf
, "ok\n", 3))
90 die("cache daemon did not start: %.*s", r
, buf
);
94 static void do_cache(const char *socket
, const char *action
, int timeout
,
97 struct strbuf buf
= STRBUF_INIT
;
99 strbuf_addf(&buf
, "action=%s\n", action
);
100 strbuf_addf(&buf
, "timeout=%d\n", timeout
);
101 if (flags
& FLAG_RELAY
) {
102 if (strbuf_read(&buf
, 0, 0) < 0)
103 die_errno("unable to relay credential");
106 if (send_request(socket
, &buf
) < 0) {
107 if (connection_fatally_broken(errno
))
108 die_errno("unable to connect to cache daemon");
109 if (flags
& FLAG_SPAWN
) {
110 spawn_daemon(socket
);
111 if (send_request(socket
, &buf
) < 0)
112 die_errno("unable to connect to cache daemon");
115 strbuf_release(&buf
);
118 static char *get_socket_path(void)
121 char *old_dir
, *socket
;
122 old_dir
= interpolate_path("~/.git-credential-cache", 0);
123 if (old_dir
&& !stat(old_dir
, &sb
) && S_ISDIR(sb
.st_mode
))
124 socket
= xstrfmt("%s/socket", old_dir
);
126 socket
= xdg_cache_home("credential/socket");
131 int cmd_credential_cache(int argc
, const char **argv
, const char *prefix
)
133 char *socket_path
= NULL
;
136 const char * const usage
[] = {
137 "git credential-cache [<options>] <action>",
140 struct option options
[] = {
141 OPT_INTEGER(0, "timeout", &timeout
,
142 "number of seconds to cache credentials"),
143 OPT_STRING(0, "socket", &socket_path
, "path",
144 "path of cache-daemon socket"),
148 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
150 usage_with_options(usage
, options
);
154 socket_path
= get_socket_path();
156 die("unable to find a suitable socket path; use --socket");
158 if (!strcmp(op
, "exit"))
159 do_cache(socket_path
, op
, timeout
, 0);
160 else if (!strcmp(op
, "get") || !strcmp(op
, "erase"))
161 do_cache(socket_path
, op
, timeout
, FLAG_RELAY
);
162 else if (!strcmp(op
, "store"))
163 do_cache(socket_path
, op
, timeout
, FLAG_RELAY
|FLAG_SPAWN
);
165 ; /* ignore unknown operation */
172 int cmd_credential_cache(int argc
, const char **argv
, const char *prefix
)
174 const char * const usage
[] = {
175 "git credential-cache [options] <action>",
177 "credential-cache is disabled in this build of Git",
180 struct option options
[] = { OPT_END() };
182 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
183 die(_("credential-cache unavailable; no unix socket support"));
186 #endif /* NO_UNIX_SOCKETS */