3 #include "trace2/tr2_dst.h"
4 #include "trace2/tr2_sid.h"
5 #include "trace2/tr2_sysenv.h"
8 * How many attempts we will make at creating an automatically-named trace file.
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
)
34 tr2env_dst_debug
= atoi(env_value
) > 0;
37 return tr2env_dst_debug
;
40 void tr2_dst_trace_disable(struct tr2_dst
*dst
)
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
;
69 struct strbuf path
= STRBUF_INIT
, sentinel_path
= STRBUF_INIT
;
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
) {
82 strbuf_addstr(&path
, tgt_prefix
);
83 if (!is_dir_sep(path
.buf
[path
.len
- 1])) {
84 strbuf_addch(&path
, '/');
88 strbuf_addbuf(&sentinel_path
, &path
);
89 strbuf_addstr(&sentinel_path
, DISCARD_SENTINEL_NAME
);
90 if (!stat(sentinel_path
.buf
, &statbuf
)) {
95 /* check file count */
96 dirp
= opendir(path
.buf
);
97 while (file_count
< tr2env_max_files
&& dirp
&& readdir(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);
110 strbuf_release(&path
);
111 strbuf_release(&sentinel_path
);
115 static int tr2_dst_try_auto_path(struct tr2_dst
*dst
, const char *tgt_prefix
)
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
, '/');
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);
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
),
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
),
162 tr2_dst_trace_disable(dst
);
163 strbuf_release(&path
);
167 strbuf_release(&path
);
170 dst
->initialized
= 1;
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);
179 if (tr2_dst_want_warning())
180 warning("trace2: could not open '%s' for '%s' tracing: %s",
182 tr2_sysenv_display_name(dst
->sysenv_var
),
185 tr2_dst_trace_disable(dst
);
191 dst
->initialized
= 1;
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
)
204 struct sockaddr_un sa
;
206 fd
= socket(AF_UNIX
, sock_type
, 0);
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
;
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;
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",
258 tr2_sysenv_display_name(dst
->sysenv_var
));
260 tr2_dst_trace_disable(dst
);
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
);
274 if (uds_try
& TR2_DST_UDS_TRY_STREAM
) {
275 if (!tr2_dst_try_uds_connect(path
, SOCK_STREAM
, &fd
))
277 if (errno
!= EPROTOTYPE
)
280 if (uds_try
& TR2_DST_UDS_TRY_DGRAM
) {
281 if (!tr2_dst_try_uds_connect(path
, SOCK_DGRAM
, &fd
))
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
),
291 tr2_dst_trace_disable(dst
);
297 dst
->initialized
= 1;
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
)
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")) {
328 if (!strcmp(tgt_value
, "1") || !strcasecmp(tgt_value
, "true")) {
329 dst
->fd
= STDERR_FILENO
;
333 if (strlen(tgt_value
) == 1 && isdigit(*tgt_value
)) {
334 dst
->fd
= atoi(tgt_value
);
338 if (is_absolute_path(tgt_value
)) {
339 if (is_directory(tgt_value
))
340 return tr2_dst_try_auto_path(dst
, tgt_value
);
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
);
350 /* Always warn about malformed values. */
351 tr2_dst_malformed_warning(dst
, tgt_value
);
352 tr2_dst_trace_disable(dst
);
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
);
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
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
);
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
),