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