2 #include "credential.h"
3 #include "string-list.h"
4 #include "parse-options.h"
5 #include "unix-socket.h"
6 #include "run-command.h"
11 static int send_request(const char *socket, const struct strbuf *out)
14 int fd = unix_stream_connect(socket);
19 if (write_in_full(fd, out->buf, out->len) < 0)
20 die_errno("unable to write to cache daemon");
21 shutdown(fd, SHUT_WR);
27 r = read_in_full(fd, in, sizeof(in));
31 die_errno("read error from cache daemon");
32 write_or_die(1, in, r);
38 static void spawn_daemon(const char *socket)
40 struct child_process daemon = CHILD_PROCESS_INIT;
41 const char *argv[] = { NULL, NULL, NULL };
45 argv[0] = "git-credential-cache--daemon";
51 if (start_command(&daemon))
52 die_errno("unable to start cache daemon");
53 r = read_in_full(daemon.out, buf, sizeof(buf));
55 die_errno("unable to read result code from cache daemon");
56 if (r != 3 || memcmp(buf, "ok\n", 3))
57 die("cache daemon did not start: %.*s", r, buf);
61 static void do_cache(const char *socket, const char *action, int timeout,
64 struct strbuf buf = STRBUF_INIT;
66 strbuf_addf(&buf, "action=%s\n", action);
67 strbuf_addf(&buf, "timeout=%d\n", timeout);
68 if (flags & FLAG_RELAY) {
69 if (strbuf_read(&buf, 0, 0) < 0)
70 die_errno("unable to relay credential");
73 if (send_request(socket, &buf) < 0) {
74 if (errno != ENOENT && errno != ECONNREFUSED)
75 die_errno("unable to connect to cache daemon");
76 if (flags & FLAG_SPAWN) {
78 if (send_request(socket, &buf) < 0)
79 die_errno("unable to connect to cache daemon");
85 int main(int argc, const char **argv)
87 char *socket_path = NULL;
90 const char * const usage[] = {
91 "git credential-cache [<options>] <action>",
94 struct option options[] = {
95 OPT_INTEGER(0, "timeout", &timeout,
96 "number of seconds to cache credentials"),
97 OPT_STRING(0, "socket", &socket_path, "path",
98 "path of cache-daemon socket"),
102 argc = parse_options(argc, argv, NULL, options, usage, 0);
104 usage_with_options(usage, options);
108 socket_path = expand_user_path("~/.git-credential-cache/socket");
110 die("unable to find a suitable socket path; use --socket");
112 if (!strcmp(op, "exit"))
113 do_cache(socket_path, op, timeout, 0);
114 else if (!strcmp(op, "get") || !strcmp(op, "erase"))
115 do_cache(socket_path, op, timeout, FLAG_RELAY);
116 else if (!strcmp(op, "store"))
117 do_cache(socket_path, op, timeout, FLAG_RELAY|FLAG_SPAWN);
119 ; /* ignore unknown operation */