/src/git/trace2/tr2_dst.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "git-compat-util.h" |
2 | | #include "abspath.h" |
3 | | #include "sigchain.h" |
4 | | #include "strbuf.h" |
5 | | #include "trace2/tr2_dst.h" |
6 | | #include "trace2/tr2_sid.h" |
7 | | #include "trace2/tr2_sysenv.h" |
8 | | |
9 | | /* |
10 | | * How many attempts we will make at creating an automatically-named trace file. |
11 | | */ |
12 | 0 | #define MAX_AUTO_ATTEMPTS 10 |
13 | | |
14 | | /* |
15 | | * Sentinel file used to detect when we should discard new traces to avoid |
16 | | * writing too many trace files to a directory. |
17 | | */ |
18 | 0 | #define DISCARD_SENTINEL_NAME "git-trace2-discard" |
19 | | |
20 | | /* |
21 | | * When set to zero, disables directory file count checks. Otherwise, controls |
22 | | * how many files we can write to a directory before entering discard mode. |
23 | | * This can be overridden via the TR2_SYSENV_MAX_FILES setting. |
24 | | */ |
25 | | static int tr2env_max_files = 0; |
26 | | |
27 | | static int tr2_dst_want_warning(void) |
28 | 0 | { |
29 | 0 | static int tr2env_dst_debug = -1; |
30 | |
|
31 | 0 | if (tr2env_dst_debug == -1) { |
32 | 0 | const char *env_value = tr2_sysenv_get(TR2_SYSENV_DST_DEBUG); |
33 | 0 | if (!env_value || !*env_value) |
34 | 0 | tr2env_dst_debug = 0; |
35 | 0 | else |
36 | 0 | tr2env_dst_debug = atoi(env_value) > 0; |
37 | 0 | } |
38 | |
|
39 | 0 | return tr2env_dst_debug; |
40 | 0 | } |
41 | | |
42 | | void tr2_dst_trace_disable(struct tr2_dst *dst) |
43 | 0 | { |
44 | 0 | if (dst->need_close) |
45 | 0 | close(dst->fd); |
46 | 0 | dst->fd = 0; |
47 | 0 | dst->initialized = 1; |
48 | 0 | dst->need_close = 0; |
49 | 0 | } |
50 | | |
51 | | /* |
52 | | * Check to make sure we're not overloading the target directory with too many |
53 | | * files. First get the threshold (if present) from the config or envvar. If |
54 | | * it's zero or unset, disable this check. Next check for the presence of a |
55 | | * sentinel file, then check file count. |
56 | | * |
57 | | * Returns 0 if tracing should proceed as normal. Returns 1 if the sentinel file |
58 | | * already exists, which means tracing should be disabled. Returns -1 if there |
59 | | * are too many files but there was no sentinel file, which means we have |
60 | | * created and should write traces to the sentinel file. |
61 | | * |
62 | | * We expect that some trace processing system is gradually collecting files |
63 | | * from the target directory; after it removes the sentinel file we'll start |
64 | | * writing traces again. |
65 | | */ |
66 | | static int tr2_dst_too_many_files(struct tr2_dst *dst, const char *tgt_prefix) |
67 | 0 | { |
68 | 0 | int file_count = 0, max_files = 0, ret = 0; |
69 | 0 | const char *max_files_var; |
70 | 0 | DIR *dirp; |
71 | 0 | struct strbuf path = STRBUF_INIT, sentinel_path = STRBUF_INIT; |
72 | 0 | struct stat statbuf; |
73 | | |
74 | | /* Get the config or envvar and decide if we should continue this check */ |
75 | 0 | max_files_var = tr2_sysenv_get(TR2_SYSENV_MAX_FILES); |
76 | 0 | if (max_files_var && *max_files_var && ((max_files = atoi(max_files_var)) >= 0)) |
77 | 0 | tr2env_max_files = max_files; |
78 | |
|
79 | 0 | if (!tr2env_max_files) { |
80 | 0 | ret = 0; |
81 | 0 | goto cleanup; |
82 | 0 | } |
83 | | |
84 | 0 | strbuf_addstr(&path, tgt_prefix); |
85 | 0 | if (!is_dir_sep(path.buf[path.len - 1])) { |
86 | 0 | strbuf_addch(&path, '/'); |
87 | 0 | } |
88 | | |
89 | | /* check sentinel */ |
90 | 0 | strbuf_addbuf(&sentinel_path, &path); |
91 | 0 | strbuf_addstr(&sentinel_path, DISCARD_SENTINEL_NAME); |
92 | 0 | if (!stat(sentinel_path.buf, &statbuf)) { |
93 | 0 | ret = 1; |
94 | 0 | goto cleanup; |
95 | 0 | } |
96 | | |
97 | | /* check file count */ |
98 | 0 | dirp = opendir(path.buf); |
99 | 0 | while (file_count < tr2env_max_files && dirp && readdir(dirp)) |
100 | 0 | file_count++; |
101 | 0 | if (dirp) |
102 | 0 | closedir(dirp); |
103 | |
|
104 | 0 | if (file_count >= tr2env_max_files) { |
105 | 0 | dst->too_many_files = 1; |
106 | 0 | dst->fd = open(sentinel_path.buf, O_WRONLY | O_CREAT | O_EXCL, 0666); |
107 | 0 | ret = -1; |
108 | 0 | goto cleanup; |
109 | 0 | } |
110 | | |
111 | 0 | cleanup: |
112 | 0 | strbuf_release(&path); |
113 | 0 | strbuf_release(&sentinel_path); |
114 | 0 | return ret; |
115 | 0 | } |
116 | | |
117 | | static int tr2_dst_try_auto_path(struct tr2_dst *dst, const char *tgt_prefix) |
118 | 0 | { |
119 | 0 | int too_many_files; |
120 | 0 | const char *last_slash, *sid = tr2_sid_get(); |
121 | 0 | struct strbuf path = STRBUF_INIT; |
122 | 0 | size_t base_path_len; |
123 | 0 | unsigned attempt_count; |
124 | |
|
125 | 0 | last_slash = strrchr(sid, '/'); |
126 | 0 | if (last_slash) |
127 | 0 | sid = last_slash + 1; |
128 | |
|
129 | 0 | strbuf_addstr(&path, tgt_prefix); |
130 | 0 | if (!is_dir_sep(path.buf[path.len - 1])) |
131 | 0 | strbuf_addch(&path, '/'); |
132 | 0 | strbuf_addstr(&path, sid); |
133 | 0 | base_path_len = path.len; |
134 | |
|
135 | 0 | too_many_files = tr2_dst_too_many_files(dst, tgt_prefix); |
136 | 0 | if (!too_many_files) { |
137 | 0 | for (attempt_count = 0; attempt_count < MAX_AUTO_ATTEMPTS; attempt_count++) { |
138 | 0 | if (attempt_count > 0) { |
139 | 0 | strbuf_setlen(&path, base_path_len); |
140 | 0 | strbuf_addf(&path, ".%d", attempt_count); |
141 | 0 | } |
142 | |
|
143 | 0 | dst->fd = open(path.buf, O_WRONLY | O_CREAT | O_EXCL, 0666); |
144 | 0 | if (dst->fd != -1) |
145 | 0 | break; |
146 | 0 | } |
147 | 0 | } else if (too_many_files == 1) { |
148 | 0 | strbuf_release(&path); |
149 | 0 | if (tr2_dst_want_warning()) |
150 | 0 | warning("trace2: not opening %s trace file due to too " |
151 | 0 | "many files in target directory %s", |
152 | 0 | tr2_sysenv_display_name(dst->sysenv_var), |
153 | 0 | tgt_prefix); |
154 | 0 | return 0; |
155 | 0 | } |
156 | | |
157 | 0 | if (dst->fd == -1) { |
158 | 0 | if (tr2_dst_want_warning()) |
159 | 0 | warning("trace2: could not open '%.*s' for '%s' tracing: %s", |
160 | 0 | (int) base_path_len, path.buf, |
161 | 0 | tr2_sysenv_display_name(dst->sysenv_var), |
162 | 0 | strerror(errno)); |
163 | |
|
164 | 0 | tr2_dst_trace_disable(dst); |
165 | 0 | strbuf_release(&path); |
166 | 0 | return 0; |
167 | 0 | } |
168 | | |
169 | 0 | strbuf_release(&path); |
170 | |
|
171 | 0 | dst->need_close = 1; |
172 | 0 | dst->initialized = 1; |
173 | |
|
174 | 0 | return dst->fd; |
175 | 0 | } |
176 | | |
177 | | static int tr2_dst_try_path(struct tr2_dst *dst, const char *tgt_value) |
178 | 0 | { |
179 | 0 | int fd = open(tgt_value, O_WRONLY | O_APPEND | O_CREAT, 0666); |
180 | 0 | if (fd == -1) { |
181 | 0 | if (tr2_dst_want_warning()) |
182 | 0 | warning("trace2: could not open '%s' for '%s' tracing: %s", |
183 | 0 | tgt_value, |
184 | 0 | tr2_sysenv_display_name(dst->sysenv_var), |
185 | 0 | strerror(errno)); |
186 | |
|
187 | 0 | tr2_dst_trace_disable(dst); |
188 | 0 | return 0; |
189 | 0 | } |
190 | | |
191 | 0 | dst->fd = fd; |
192 | 0 | dst->need_close = 1; |
193 | 0 | dst->initialized = 1; |
194 | |
|
195 | 0 | return dst->fd; |
196 | 0 | } |
197 | | |
198 | | #ifndef NO_UNIX_SOCKETS |
199 | 0 | #define PREFIX_AF_UNIX "af_unix:" |
200 | 0 | #define PREFIX_AF_UNIX_STREAM "af_unix:stream:" |
201 | 0 | #define PREFIX_AF_UNIX_DGRAM "af_unix:dgram:" |
202 | | |
203 | | static int tr2_dst_try_uds_connect(const char *path, int sock_type, int *out_fd) |
204 | 0 | { |
205 | 0 | int fd; |
206 | 0 | struct sockaddr_un sa; |
207 | |
|
208 | 0 | fd = socket(AF_UNIX, sock_type, 0); |
209 | 0 | if (fd == -1) |
210 | 0 | return -1; |
211 | | |
212 | 0 | sa.sun_family = AF_UNIX; |
213 | 0 | strlcpy(sa.sun_path, path, sizeof(sa.sun_path)); |
214 | |
|
215 | 0 | if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) { |
216 | 0 | int saved_errno = errno; |
217 | 0 | close(fd); |
218 | 0 | errno = saved_errno; |
219 | 0 | return -1; |
220 | 0 | } |
221 | | |
222 | 0 | *out_fd = fd; |
223 | 0 | return 0; |
224 | 0 | } |
225 | | |
226 | 0 | #define TR2_DST_UDS_TRY_STREAM (1 << 0) |
227 | 0 | #define TR2_DST_UDS_TRY_DGRAM (1 << 1) |
228 | | |
229 | | static int tr2_dst_try_unix_domain_socket(struct tr2_dst *dst, |
230 | | const char *tgt_value) |
231 | 0 | { |
232 | 0 | unsigned int uds_try = 0; |
233 | 0 | int fd; |
234 | 0 | const char *path = NULL; |
235 | | |
236 | | /* |
237 | | * Allow "af_unix:[<type>:]<absolute_path>" |
238 | | * |
239 | | * Trace2 always writes complete individual messages (without |
240 | | * chunking), so we can talk to either DGRAM or STREAM type sockets. |
241 | | * |
242 | | * Allow the user to explicitly request the socket type. |
243 | | * |
244 | | * If they omit the socket type, try one and then the other. |
245 | | */ |
246 | |
|
247 | 0 | if (skip_prefix(tgt_value, PREFIX_AF_UNIX_STREAM, &path)) |
248 | 0 | uds_try |= TR2_DST_UDS_TRY_STREAM; |
249 | | |
250 | 0 | else if (skip_prefix(tgt_value, PREFIX_AF_UNIX_DGRAM, &path)) |
251 | 0 | uds_try |= TR2_DST_UDS_TRY_DGRAM; |
252 | | |
253 | 0 | else if (skip_prefix(tgt_value, PREFIX_AF_UNIX, &path)) |
254 | 0 | uds_try |= TR2_DST_UDS_TRY_STREAM | TR2_DST_UDS_TRY_DGRAM; |
255 | |
|
256 | 0 | if (!path || !*path) { |
257 | 0 | if (tr2_dst_want_warning()) |
258 | 0 | warning("trace2: invalid AF_UNIX value '%s' for '%s' tracing", |
259 | 0 | tgt_value, |
260 | 0 | tr2_sysenv_display_name(dst->sysenv_var)); |
261 | |
|
262 | 0 | tr2_dst_trace_disable(dst); |
263 | 0 | return 0; |
264 | 0 | } |
265 | | |
266 | 0 | if (!is_absolute_path(path) || |
267 | 0 | strlen(path) >= sizeof(((struct sockaddr_un *)0)->sun_path)) { |
268 | 0 | if (tr2_dst_want_warning()) |
269 | 0 | warning("trace2: invalid AF_UNIX path '%s' for '%s' tracing", |
270 | 0 | path, tr2_sysenv_display_name(dst->sysenv_var)); |
271 | |
|
272 | 0 | tr2_dst_trace_disable(dst); |
273 | 0 | return 0; |
274 | 0 | } |
275 | | |
276 | 0 | if (uds_try & TR2_DST_UDS_TRY_STREAM) { |
277 | 0 | if (!tr2_dst_try_uds_connect(path, SOCK_STREAM, &fd)) |
278 | 0 | goto connected; |
279 | 0 | if (errno != EPROTOTYPE) |
280 | 0 | goto error; |
281 | 0 | } |
282 | 0 | if (uds_try & TR2_DST_UDS_TRY_DGRAM) { |
283 | 0 | if (!tr2_dst_try_uds_connect(path, SOCK_DGRAM, &fd)) |
284 | 0 | goto connected; |
285 | 0 | } |
286 | | |
287 | 0 | error: |
288 | 0 | if (tr2_dst_want_warning()) |
289 | 0 | warning("trace2: could not connect to socket '%s' for '%s' tracing: %s", |
290 | 0 | path, tr2_sysenv_display_name(dst->sysenv_var), |
291 | 0 | strerror(errno)); |
292 | |
|
293 | 0 | tr2_dst_trace_disable(dst); |
294 | 0 | return 0; |
295 | | |
296 | 0 | connected: |
297 | 0 | dst->fd = fd; |
298 | 0 | dst->need_close = 1; |
299 | 0 | dst->initialized = 1; |
300 | |
|
301 | 0 | return dst->fd; |
302 | 0 | } |
303 | | #endif |
304 | | |
305 | | static void tr2_dst_malformed_warning(struct tr2_dst *dst, |
306 | | const char *tgt_value) |
307 | 0 | { |
308 | 0 | warning("trace2: unknown value for '%s': '%s'", |
309 | 0 | tr2_sysenv_display_name(dst->sysenv_var), tgt_value); |
310 | 0 | } |
311 | | |
312 | | int tr2_dst_get_trace_fd(struct tr2_dst *dst) |
313 | 0 | { |
314 | 0 | const char *tgt_value; |
315 | | |
316 | | /* don't open twice */ |
317 | 0 | if (dst->initialized) |
318 | 0 | return dst->fd; |
319 | | |
320 | 0 | dst->initialized = 1; |
321 | |
|
322 | 0 | tgt_value = tr2_sysenv_get(dst->sysenv_var); |
323 | |
|
324 | 0 | if (!tgt_value || !strcmp(tgt_value, "") || !strcmp(tgt_value, "0") || |
325 | 0 | !strcasecmp(tgt_value, "false")) { |
326 | 0 | dst->fd = 0; |
327 | 0 | return dst->fd; |
328 | 0 | } |
329 | | |
330 | 0 | if (!strcmp(tgt_value, "1") || !strcasecmp(tgt_value, "true")) { |
331 | 0 | dst->fd = STDERR_FILENO; |
332 | 0 | return dst->fd; |
333 | 0 | } |
334 | | |
335 | 0 | if (strlen(tgt_value) == 1 && isdigit(*tgt_value)) { |
336 | 0 | dst->fd = atoi(tgt_value); |
337 | 0 | return dst->fd; |
338 | 0 | } |
339 | | |
340 | 0 | if (is_absolute_path(tgt_value)) { |
341 | 0 | if (is_directory(tgt_value)) |
342 | 0 | return tr2_dst_try_auto_path(dst, tgt_value); |
343 | 0 | else |
344 | 0 | return tr2_dst_try_path(dst, tgt_value); |
345 | 0 | } |
346 | | |
347 | 0 | #ifndef NO_UNIX_SOCKETS |
348 | 0 | if (starts_with(tgt_value, PREFIX_AF_UNIX)) |
349 | 0 | return tr2_dst_try_unix_domain_socket(dst, tgt_value); |
350 | 0 | #endif |
351 | | |
352 | | /* Always warn about malformed values. */ |
353 | 0 | tr2_dst_malformed_warning(dst, tgt_value); |
354 | 0 | tr2_dst_trace_disable(dst); |
355 | 0 | return 0; |
356 | 0 | } |
357 | | |
358 | | int tr2_dst_trace_want(struct tr2_dst *dst) |
359 | 0 | { |
360 | 0 | return !!tr2_dst_get_trace_fd(dst); |
361 | 0 | } |
362 | | |
363 | | void tr2_dst_write_line(struct tr2_dst *dst, struct strbuf *buf_line) |
364 | 0 | { |
365 | 0 | int fd = tr2_dst_get_trace_fd(dst); |
366 | 0 | ssize_t bytes; |
367 | |
|
368 | 0 | strbuf_complete_line(buf_line); /* ensure final NL on buffer */ |
369 | | |
370 | | /* |
371 | | * We do not use write_in_full() because we do not want |
372 | | * a short-write to try again. We are using O_APPEND mode |
373 | | * files and the kernel handles the atomic seek+write. If |
374 | | * another thread or git process is concurrently writing to |
375 | | * this fd or file, our remainder-write may not be contiguous |
376 | | * with our initial write of this message. And that will |
377 | | * confuse readers. So just don't bother. |
378 | | * |
379 | | * It is assumed that TRACE2 messages are short enough that |
380 | | * the system can write them in 1 attempt and we won't see |
381 | | * a short-write. |
382 | | * |
383 | | * If we get an IO error, just close the trace dst. |
384 | | */ |
385 | 0 | sigchain_push(SIGPIPE, SIG_IGN); |
386 | 0 | bytes = write(fd, buf_line->buf, buf_line->len); |
387 | 0 | sigchain_pop(SIGPIPE); |
388 | 0 | if (bytes >= 0) |
389 | 0 | return; |
390 | | |
391 | 0 | tr2_dst_trace_disable(dst); |
392 | 0 | if (tr2_dst_want_warning()) |
393 | 0 | warning("unable to write trace to '%s': %s", |
394 | 0 | tr2_sysenv_display_name(dst->sysenv_var), |
395 | 0 | strerror(errno)); |
396 | 0 | } |