]>
Commit | Line | Data |
---|---|---|
b5dd96b7 | 1 | #include "builtin.h" |
f394e093 | 2 | #include "gettext.h" |
b5dd96b7 | 3 | #include "parse-options.h" |
d1cbe1e6 | 4 | #include "path.h" |
88e4e183 | 5 | #include "strbuf.h" |
d48be35c | 6 | #include "write-or-die.h" |
b5dd96b7 JK |
7 | |
8 | #ifndef NO_UNIX_SOCKETS | |
9 | ||
e2770979 JK |
10 | #include "unix-socket.h" |
11 | #include "run-command.h" | |
12 | ||
13 | #define FLAG_SPAWN 0x1 | |
14 | #define FLAG_RELAY 0x2 | |
15 | ||
245670cd CMAB |
16 | #ifdef GIT_WINDOWS_NATIVE |
17 | ||
18 | static int connection_closed(int error) | |
19 | { | |
20 | return (error == EINVAL); | |
21 | } | |
22 | ||
23 | static int connection_fatally_broken(int error) | |
24 | { | |
25 | return (error != ENOENT) && (error != ENETDOWN); | |
26 | } | |
27 | ||
28 | #else | |
29 | ||
30 | static int connection_closed(int error) | |
31 | { | |
32 | return (error == ECONNRESET); | |
33 | } | |
34 | ||
35 | static int connection_fatally_broken(int error) | |
36 | { | |
37 | return (error != ENOENT) && (error != ECONNREFUSED); | |
38 | } | |
39 | ||
40 | #endif | |
41 | ||
e2770979 JK |
42 | static int send_request(const char *socket, const struct strbuf *out) |
43 | { | |
44 | int got_data = 0; | |
77e522ca | 45 | int fd = unix_stream_connect(socket, 0); |
e2770979 JK |
46 | |
47 | if (fd < 0) | |
48 | return -1; | |
49 | ||
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); | |
53 | ||
54 | while (1) { | |
55 | char in[1024]; | |
56 | int r; | |
57 | ||
58 | r = read_in_full(fd, in, sizeof(in)); | |
245670cd | 59 | if (r == 0 || (r < 0 && connection_closed(errno))) |
e2770979 JK |
60 | break; |
61 | if (r < 0) | |
62 | die_errno("read error from cache daemon"); | |
63 | write_or_die(1, in, r); | |
64 | got_data = 1; | |
65 | } | |
9c60d9fa | 66 | close(fd); |
e2770979 JK |
67 | return got_data; |
68 | } | |
69 | ||
70 | static void spawn_daemon(const char *socket) | |
71 | { | |
d3180279 | 72 | struct child_process daemon = CHILD_PROCESS_INIT; |
e2770979 JK |
73 | char buf[128]; |
74 | int r; | |
75 | ||
c0e190c1 JH |
76 | strvec_pushl(&daemon.args, |
77 | "credential-cache--daemon", socket, | |
78 | NULL); | |
79 | daemon.git_cmd = 1; | |
e2770979 JK |
80 | daemon.no_stdin = 1; |
81 | daemon.out = -1; | |
82 | ||
83 | if (start_command(&daemon)) | |
84 | die_errno("unable to start cache daemon"); | |
85 | r = read_in_full(daemon.out, buf, sizeof(buf)); | |
86 | if (r < 0) | |
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); | |
90 | close(daemon.out); | |
91 | } | |
92 | ||
93 | static void do_cache(const char *socket, const char *action, int timeout, | |
94 | int flags) | |
95 | { | |
96 | struct strbuf buf = STRBUF_INIT; | |
97 | ||
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"); | |
103 | } | |
104 | ||
8ec6c8d7 | 105 | if (send_request(socket, &buf) < 0) { |
245670cd | 106 | if (connection_fatally_broken(errno)) |
98c2924c | 107 | die_errno("unable to connect to cache daemon"); |
8ec6c8d7 JK |
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"); | |
112 | } | |
e2770979 JK |
113 | } |
114 | strbuf_release(&buf); | |
115 | } | |
116 | ||
60759baa DL |
117 | static char *get_socket_path(void) |
118 | { | |
119 | struct stat sb; | |
120 | char *old_dir, *socket; | |
a03b097d | 121 | old_dir = interpolate_path("~/.git-credential-cache", 0); |
60759baa DL |
122 | if (old_dir && !stat(old_dir, &sb) && S_ISDIR(sb.st_mode)) |
123 | socket = xstrfmt("%s/socket", old_dir); | |
124 | else | |
125 | socket = xdg_cache_home("credential/socket"); | |
126 | free(old_dir); | |
127 | return socket; | |
128 | } | |
129 | ||
b5dd96b7 | 130 | int cmd_credential_cache(int argc, const char **argv, const char *prefix) |
e2770979 JK |
131 | { |
132 | char *socket_path = NULL; | |
133 | int timeout = 900; | |
134 | const char *op; | |
135 | const char * const usage[] = { | |
d96a0313 | 136 | "git credential-cache [<options>] <action>", |
e2770979 JK |
137 | NULL |
138 | }; | |
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"), | |
144 | OPT_END() | |
145 | }; | |
146 | ||
b5dd96b7 | 147 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
e2770979 JK |
148 | if (!argc) |
149 | usage_with_options(usage, options); | |
150 | op = argv[0]; | |
151 | ||
152 | if (!socket_path) | |
60759baa | 153 | socket_path = get_socket_path(); |
e2770979 JK |
154 | if (!socket_path) |
155 | die("unable to find a suitable socket path; use --socket"); | |
156 | ||
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); | |
163 | else | |
164 | ; /* ignore unknown operation */ | |
165 | ||
166 | return 0; | |
167 | } | |
b5dd96b7 JK |
168 | |
169 | #else | |
170 | ||
171 | int cmd_credential_cache(int argc, const char **argv, const char *prefix) | |
172 | { | |
173 | const char * const usage[] = { | |
174 | "git credential-cache [options] <action>", | |
175 | "", | |
176 | "credential-cache is disabled in this build of Git", | |
177 | NULL | |
178 | }; | |
179 | struct option options[] = { OPT_END() }; | |
180 | ||
181 | argc = parse_options(argc, argv, prefix, options, usage, 0); | |
182 | die(_("credential-cache unavailable; no unix socket support")); | |
183 | } | |
184 | ||
185 | #endif /* NO_UNIX_SOCKETS */ |