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