Merge branch 'ja/worktree-orphan-fix'
[git.git] / builtin / credential-cache.c
blob43b9d0e5b16ba5da555a3b16ce53b88dff5dbb4c
1 #include "builtin.h"
2 #include "gettext.h"
3 #include "parse-options.h"
4 #include "path.h"
5 #include "strbuf.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);
30 #else
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);
42 #endif
44 static int send_request(const char *socket, const struct strbuf *out)
46 int got_data = 0;
47 int fd = unix_stream_connect(socket, 0);
49 if (fd < 0)
50 return -1;
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);
56 while (1) {
57 char in[1024];
58 int r;
60 r = read_in_full(fd, in, sizeof(in));
61 if (r == 0 || (r < 0 && connection_closed(errno)))
62 break;
63 if (r < 0)
64 die_errno("read error from cache daemon");
65 write_or_die(1, in, r);
66 got_data = 1;
68 close(fd);
69 return got_data;
72 static void spawn_daemon(const char *socket)
74 struct child_process daemon = CHILD_PROCESS_INIT;
75 char buf[128];
76 int r;
78 strvec_pushl(&daemon.args,
79 "credential-cache--daemon", socket,
80 NULL);
81 daemon.git_cmd = 1;
82 daemon.no_stdin = 1;
83 daemon.out = -1;
85 if (start_command(&daemon))
86 die_errno("unable to start cache daemon");
87 r = read_in_full(daemon.out, buf, sizeof(buf));
88 if (r < 0)
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);
92 close(daemon.out);
95 static void do_cache(const char *socket, const char *action, int timeout,
96 int flags)
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)
121 struct stat sb;
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);
126 else
127 socket = xdg_cache_home("credential/socket");
128 free(old_dir);
129 return socket;
132 int cmd_credential_cache(int argc, const char **argv, const char *prefix)
134 char *socket_path = NULL;
135 int timeout = 900;
136 const char *op;
137 const char * const usage[] = {
138 "git credential-cache [<options>] <action>",
139 NULL
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"),
146 OPT_END()
149 argc = parse_options(argc, argv, prefix, options, usage, 0);
150 if (!argc)
151 usage_with_options(usage, options);
152 op = argv[0];
154 if (!socket_path)
155 socket_path = get_socket_path();
156 if (!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);
165 else
166 ; /* ignore unknown operation */
168 return 0;
171 #else
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",
179 NULL
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 */