2 #include "parse-options.h"
4 #ifndef NO_UNIX_SOCKETS
6 #include "credential.h"
7 #include "string-list.h"
8 #include "unix-socket.h"
9 #include "run-command.h"
11 #define FLAG_SPAWN 0x1
12 #define FLAG_RELAY 0x2
14 static int send_request(const char *socket
, const struct strbuf
*out
)
17 int fd
= unix_stream_connect(socket
, 0);
22 if (write_in_full(fd
, out
->buf
, out
->len
) < 0)
23 die_errno("unable to write to cache daemon");
24 shutdown(fd
, SHUT_WR
);
30 r
= read_in_full(fd
, in
, sizeof(in
));
31 if (r
== 0 || (r
< 0 && errno
== ECONNRESET
))
34 die_errno("read error from cache daemon");
35 write_or_die(1, in
, r
);
42 static void spawn_daemon(const char *socket
)
44 struct child_process daemon
= CHILD_PROCESS_INIT
;
48 strvec_pushl(&daemon
.args
,
49 "credential-cache--daemon", socket
,
55 if (start_command(&daemon
))
56 die_errno("unable to start cache daemon");
57 r
= read_in_full(daemon
.out
, buf
, sizeof(buf
));
59 die_errno("unable to read result code from cache daemon");
60 if (r
!= 3 || memcmp(buf
, "ok\n", 3))
61 die("cache daemon did not start: %.*s", r
, buf
);
65 static void do_cache(const char *socket
, const char *action
, int timeout
,
68 struct strbuf buf
= STRBUF_INIT
;
70 strbuf_addf(&buf
, "action=%s\n", action
);
71 strbuf_addf(&buf
, "timeout=%d\n", timeout
);
72 if (flags
& FLAG_RELAY
) {
73 if (strbuf_read(&buf
, 0, 0) < 0)
74 die_errno("unable to relay credential");
77 if (send_request(socket
, &buf
) < 0) {
78 if (errno
!= ENOENT
&& errno
!= ECONNREFUSED
)
79 die_errno("unable to connect to cache daemon");
80 if (flags
& FLAG_SPAWN
) {
82 if (send_request(socket
, &buf
) < 0)
83 die_errno("unable to connect to cache daemon");
89 static char *get_socket_path(void)
92 char *old_dir
, *socket
;
93 old_dir
= expand_user_path("~/.git-credential-cache", 0);
94 if (old_dir
&& !stat(old_dir
, &sb
) && S_ISDIR(sb
.st_mode
))
95 socket
= xstrfmt("%s/socket", old_dir
);
97 socket
= xdg_cache_home("credential/socket");
102 int cmd_credential_cache(int argc
, const char **argv
, const char *prefix
)
104 char *socket_path
= NULL
;
107 const char * const usage
[] = {
108 "git credential-cache [<options>] <action>",
111 struct option options
[] = {
112 OPT_INTEGER(0, "timeout", &timeout
,
113 "number of seconds to cache credentials"),
114 OPT_STRING(0, "socket", &socket_path
, "path",
115 "path of cache-daemon socket"),
119 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
121 usage_with_options(usage
, options
);
125 socket_path
= get_socket_path();
127 die("unable to find a suitable socket path; use --socket");
129 if (!strcmp(op
, "exit"))
130 do_cache(socket_path
, op
, timeout
, 0);
131 else if (!strcmp(op
, "get") || !strcmp(op
, "erase"))
132 do_cache(socket_path
, op
, timeout
, FLAG_RELAY
);
133 else if (!strcmp(op
, "store"))
134 do_cache(socket_path
, op
, timeout
, FLAG_RELAY
|FLAG_SPAWN
);
136 ; /* ignore unknown operation */
143 int cmd_credential_cache(int argc
, const char **argv
, const char *prefix
)
145 const char * const usage
[] = {
146 "git credential-cache [options] <action>",
148 "credential-cache is disabled in this build of Git",
151 struct option options
[] = { OPT_END() };
153 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
154 die(_("credential-cache unavailable; no unix socket support"));
157 #endif /* NO_UNIX_SOCKETS */