3 #include "parse-options.h"
6 #include "write-or-die.h"
8 #ifndef NO_UNIX_SOCKETS
10 #include "unix-socket.h"
11 #include "run-command.h"
13 #define FLAG_SPAWN 0x1
14 #define FLAG_RELAY 0x2
16 #ifdef GIT_WINDOWS_NATIVE
18 static int connection_closed(int error
)
20 return (error
== EINVAL
);
23 static int connection_fatally_broken(int error
)
25 return (error
!= ENOENT
) && (error
!= ENETDOWN
);
30 static int connection_closed(int error
)
32 return (error
== ECONNRESET
);
35 static int connection_fatally_broken(int error
)
37 return (error
!= ENOENT
) && (error
!= ECONNREFUSED
);
42 static int send_request(const char *socket
, const struct strbuf
*out
)
45 int fd
= unix_stream_connect(socket
, 0);
50 if (write_in_full(fd
, out
->buf
, out
->len
) < 0)
51 die_errno("unable to write to cache daemon");
52 shutdown(fd
, SHUT_WR
);
58 r
= read_in_full(fd
, in
, sizeof(in
));
59 if (r
== 0 || (r
< 0 && connection_closed(errno
)))
62 die_errno("read error from cache daemon");
63 write_or_die(1, in
, r
);
70 static void spawn_daemon(const char *socket
)
72 struct child_process daemon
= CHILD_PROCESS_INIT
;
76 strvec_pushl(&daemon
.args
,
77 "credential-cache--daemon", socket
,
83 if (start_command(&daemon
))
84 die_errno("unable to start cache daemon");
85 r
= read_in_full(daemon
.out
, buf
, sizeof(buf
));
87 die_errno("unable to read result code from cache daemon");
88 if (r
!= 3 || memcmp(buf
, "ok\n", 3))
89 die("cache daemon did not start: %.*s", r
, buf
);
93 static void do_cache(const char *socket
, const char *action
, int timeout
,
96 struct strbuf buf
= STRBUF_INIT
;
98 strbuf_addf(&buf
, "action=%s\n", action
);
99 strbuf_addf(&buf
, "timeout=%d\n", timeout
);
100 if (flags
& FLAG_RELAY
) {
101 if (strbuf_read(&buf
, 0, 0) < 0)
102 die_errno("unable to relay credential");
105 if (send_request(socket
, &buf
) < 0) {
106 if (connection_fatally_broken(errno
))
107 die_errno("unable to connect to cache daemon");
108 if (flags
& FLAG_SPAWN
) {
109 spawn_daemon(socket
);
110 if (send_request(socket
, &buf
) < 0)
111 die_errno("unable to connect to cache daemon");
114 strbuf_release(&buf
);
117 static char *get_socket_path(void)
120 char *old_dir
, *socket
;
121 old_dir
= interpolate_path("~/.git-credential-cache", 0);
122 if (old_dir
&& !stat(old_dir
, &sb
) && S_ISDIR(sb
.st_mode
))
123 socket
= xstrfmt("%s/socket", old_dir
);
125 socket
= xdg_cache_home("credential/socket");
130 int cmd_credential_cache(int argc
, const char **argv
, const char *prefix
)
132 char *socket_path
= NULL
;
135 const char * const usage
[] = {
136 "git credential-cache [<options>] <action>",
139 struct option options
[] = {
140 OPT_INTEGER(0, "timeout", &timeout
,
141 "number of seconds to cache credentials"),
142 OPT_STRING(0, "socket", &socket_path
, "path",
143 "path of cache-daemon socket"),
147 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
149 usage_with_options(usage
, options
);
153 socket_path
= get_socket_path();
155 die("unable to find a suitable socket path; use --socket");
157 if (!strcmp(op
, "exit"))
158 do_cache(socket_path
, op
, timeout
, 0);
159 else if (!strcmp(op
, "get") || !strcmp(op
, "erase"))
160 do_cache(socket_path
, op
, timeout
, FLAG_RELAY
);
161 else if (!strcmp(op
, "store"))
162 do_cache(socket_path
, op
, timeout
, FLAG_RELAY
|FLAG_SPAWN
);
164 ; /* ignore unknown operation */
171 int cmd_credential_cache(int argc
, const char **argv
, const char *prefix
)
173 const char * const usage
[] = {
174 "git credential-cache [options] <action>",
176 "credential-cache is disabled in this build of Git",
179 struct option options
[] = { OPT_END() };
181 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
182 die(_("credential-cache unavailable; no unix socket support"));
185 #endif /* NO_UNIX_SOCKETS */