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