3 #include "parse-options.h"
6 #include "write-or-die.h"
8 #ifndef NO_UNIX_SOCKETS
10 #include "credential.h"
11 #include "string-list.h"
12 #include "unix-socket.h"
13 #include "run-command.h"
15 #define FLAG_SPAWN 0x1
16 #define FLAG_RELAY 0x2
18 #ifdef GIT_WINDOWS_NATIVE
20 static int connection_closed(int error
)
22 return (error
== EINVAL
);
25 static int connection_fatally_broken(int error
)
27 return (error
!= ENOENT
) && (error
!= ENETDOWN
);
32 static int connection_closed(int error
)
34 return (error
== ECONNRESET
);
37 static int connection_fatally_broken(int error
)
39 return (error
!= ENOENT
) && (error
!= ECONNREFUSED
);
44 static int send_request(const char *socket
, const struct strbuf
*out
)
47 int fd
= unix_stream_connect(socket
, 0);
52 if (write_in_full(fd
, out
->buf
, out
->len
) < 0)
53 die_errno("unable to write to cache daemon");
54 shutdown(fd
, SHUT_WR
);
60 r
= read_in_full(fd
, in
, sizeof(in
));
61 if (r
== 0 || (r
< 0 && connection_closed(errno
)))
64 die_errno("read error from cache daemon");
65 write_or_die(1, in
, r
);
72 static void spawn_daemon(const char *socket
)
74 struct child_process daemon
= CHILD_PROCESS_INIT
;
78 strvec_pushl(&daemon
.args
,
79 "credential-cache--daemon", socket
,
85 if (start_command(&daemon
))
86 die_errno("unable to start cache daemon");
87 r
= read_in_full(daemon
.out
, buf
, sizeof(buf
));
89 die_errno("unable to read result code from cache daemon");
90 if (r
!= 3 || memcmp(buf
, "ok\n", 3))
91 die("cache daemon did not start: %.*s", r
, buf
);
95 static void do_cache(const char *socket
, const char *action
, int timeout
,
98 struct strbuf buf
= STRBUF_INIT
;
100 strbuf_addf(&buf
, "action=%s\n", action
);
101 strbuf_addf(&buf
, "timeout=%d\n", timeout
);
102 if (flags
& FLAG_RELAY
) {
103 if (strbuf_read(&buf
, 0, 0) < 0)
104 die_errno("unable to relay credential");
107 if (send_request(socket
, &buf
) < 0) {
108 if (connection_fatally_broken(errno
))
109 die_errno("unable to connect to cache daemon");
110 if (flags
& FLAG_SPAWN
) {
111 spawn_daemon(socket
);
112 if (send_request(socket
, &buf
) < 0)
113 die_errno("unable to connect to cache daemon");
116 strbuf_release(&buf
);
119 static char *get_socket_path(void)
122 char *old_dir
, *socket
;
123 old_dir
= interpolate_path("~/.git-credential-cache", 0);
124 if (old_dir
&& !stat(old_dir
, &sb
) && S_ISDIR(sb
.st_mode
))
125 socket
= xstrfmt("%s/socket", old_dir
);
127 socket
= xdg_cache_home("credential/socket");
132 int cmd_credential_cache(int argc
, const char **argv
, const char *prefix
)
134 char *socket_path
= NULL
;
137 const char * const usage
[] = {
138 "git credential-cache [<options>] <action>",
141 struct option options
[] = {
142 OPT_INTEGER(0, "timeout", &timeout
,
143 "number of seconds to cache credentials"),
144 OPT_STRING(0, "socket", &socket_path
, "path",
145 "path of cache-daemon socket"),
149 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
151 usage_with_options(usage
, options
);
155 socket_path
= get_socket_path();
157 die("unable to find a suitable socket path; use --socket");
159 if (!strcmp(op
, "exit"))
160 do_cache(socket_path
, op
, timeout
, 0);
161 else if (!strcmp(op
, "get") || !strcmp(op
, "erase"))
162 do_cache(socket_path
, op
, timeout
, FLAG_RELAY
);
163 else if (!strcmp(op
, "store"))
164 do_cache(socket_path
, op
, timeout
, FLAG_RELAY
|FLAG_SPAWN
);
166 ; /* ignore unknown operation */
173 int cmd_credential_cache(int argc
, const char **argv
, const char *prefix
)
175 const char * const usage
[] = {
176 "git credential-cache [options] <action>",
178 "credential-cache is disabled in this build of Git",
181 struct option options
[] = { OPT_END() };
183 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
184 die(_("credential-cache unavailable; no unix socket support"));
187 #endif /* NO_UNIX_SOCKETS */