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