2 #include "trace2/tr2_dst.h"
3 #include "trace2/tr2_sid.h"
4 #include "trace2/tr2_sysenv.h"
7 * How many attempts we will make at creating an automatically-named trace file.
9 #define MAX_AUTO_ATTEMPTS 10
11 static int tr2_dst_want_warning(void)
13 static int tr2env_dst_debug
= -1;
15 if (tr2env_dst_debug
== -1) {
16 const char *env_value
= tr2_sysenv_get(TR2_SYSENV_DST_DEBUG
);
17 if (!env_value
|| !*env_value
)
20 tr2env_dst_debug
= atoi(env_value
) > 0;
23 return tr2env_dst_debug
;
26 void tr2_dst_trace_disable(struct tr2_dst
*dst
)
35 static int tr2_dst_try_auto_path(struct tr2_dst
*dst
, const char *tgt_prefix
)
38 const char *last_slash
, *sid
= tr2_sid_get();
39 struct strbuf path
= STRBUF_INIT
;
41 unsigned attempt_count
;
43 last_slash
= strrchr(sid
, '/');
47 strbuf_addstr(&path
, tgt_prefix
);
48 if (!is_dir_sep(path
.buf
[path
.len
- 1]))
49 strbuf_addch(&path
, '/');
50 strbuf_addstr(&path
, sid
);
51 base_path_len
= path
.len
;
53 for (attempt_count
= 0; attempt_count
< MAX_AUTO_ATTEMPTS
; attempt_count
++) {
54 if (attempt_count
> 0) {
55 strbuf_setlen(&path
, base_path_len
);
56 strbuf_addf(&path
, ".%d", attempt_count
);
59 fd
= open(path
.buf
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
65 if (tr2_dst_want_warning())
66 warning("trace2: could not open '%.*s' for '%s' tracing: %s",
67 (int) base_path_len
, path
.buf
,
68 tr2_sysenv_display_name(dst
->sysenv_var
),
71 tr2_dst_trace_disable(dst
);
72 strbuf_release(&path
);
76 strbuf_release(&path
);
85 static int tr2_dst_try_path(struct tr2_dst
*dst
, const char *tgt_value
)
87 int fd
= open(tgt_value
, O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
89 if (tr2_dst_want_warning())
90 warning("trace2: could not open '%s' for '%s' tracing: %s",
92 tr2_sysenv_display_name(dst
->sysenv_var
),
95 tr2_dst_trace_disable(dst
);
101 dst
->initialized
= 1;
106 #ifndef NO_UNIX_SOCKETS
107 #define PREFIX_AF_UNIX "af_unix:"
108 #define PREFIX_AF_UNIX_STREAM "af_unix:stream:"
109 #define PREFIX_AF_UNIX_DGRAM "af_unix:dgram:"
111 static int tr2_dst_try_uds_connect(const char *path
, int sock_type
, int *out_fd
)
114 struct sockaddr_un sa
;
116 fd
= socket(AF_UNIX
, sock_type
, 0);
120 sa
.sun_family
= AF_UNIX
;
121 strlcpy(sa
.sun_path
, path
, sizeof(sa
.sun_path
));
123 if (connect(fd
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1) {
133 #define TR2_DST_UDS_TRY_STREAM (1 << 0)
134 #define TR2_DST_UDS_TRY_DGRAM (1 << 1)
136 static int tr2_dst_try_unix_domain_socket(struct tr2_dst
*dst
,
137 const char *tgt_value
)
139 unsigned int uds_try
= 0;
142 const char *path
= NULL
;
145 * Allow "af_unix:[<type>:]<absolute_path>"
147 * Trace2 always writes complete individual messages (without
148 * chunking), so we can talk to either DGRAM or STREAM type sockets.
150 * Allow the user to explicitly request the socket type.
152 * If they omit the socket type, try one and then the other.
155 if (skip_prefix(tgt_value
, PREFIX_AF_UNIX_STREAM
, &path
))
156 uds_try
|= TR2_DST_UDS_TRY_STREAM
;
158 else if (skip_prefix(tgt_value
, PREFIX_AF_UNIX_DGRAM
, &path
))
159 uds_try
|= TR2_DST_UDS_TRY_DGRAM
;
161 else if (skip_prefix(tgt_value
, PREFIX_AF_UNIX
, &path
))
162 uds_try
|= TR2_DST_UDS_TRY_STREAM
| TR2_DST_UDS_TRY_DGRAM
;
164 if (!path
|| !*path
) {
165 if (tr2_dst_want_warning())
166 warning("trace2: invalid AF_UNIX value '%s' for '%s' tracing",
168 tr2_sysenv_display_name(dst
->sysenv_var
));
170 tr2_dst_trace_disable(dst
);
174 if (!is_absolute_path(path
) ||
175 strlen(path
) >= sizeof(((struct sockaddr_un
*)0)->sun_path
)) {
176 if (tr2_dst_want_warning())
177 warning("trace2: invalid AF_UNIX path '%s' for '%s' tracing",
178 path
, tr2_sysenv_display_name(dst
->sysenv_var
));
180 tr2_dst_trace_disable(dst
);
184 if (uds_try
& TR2_DST_UDS_TRY_STREAM
) {
185 e
= tr2_dst_try_uds_connect(path
, SOCK_STREAM
, &fd
);
191 if (uds_try
& TR2_DST_UDS_TRY_DGRAM
) {
192 e
= tr2_dst_try_uds_connect(path
, SOCK_DGRAM
, &fd
);
198 if (tr2_dst_want_warning())
199 warning("trace2: could not connect to socket '%s' for '%s' tracing: %s",
200 path
, tr2_sysenv_display_name(dst
->sysenv_var
),
203 tr2_dst_trace_disable(dst
);
209 dst
->initialized
= 1;
215 static void tr2_dst_malformed_warning(struct tr2_dst
*dst
,
216 const char *tgt_value
)
218 struct strbuf buf
= STRBUF_INIT
;
220 strbuf_addf(&buf
, "trace2: unknown value for '%s': '%s'",
221 tr2_sysenv_display_name(dst
->sysenv_var
), tgt_value
);
222 warning("%s", buf
.buf
);
224 strbuf_release(&buf
);
227 int tr2_dst_get_trace_fd(struct tr2_dst
*dst
)
229 const char *tgt_value
;
231 /* don't open twice */
232 if (dst
->initialized
)
235 dst
->initialized
= 1;
237 tgt_value
= tr2_sysenv_get(dst
->sysenv_var
);
239 if (!tgt_value
|| !strcmp(tgt_value
, "") || !strcmp(tgt_value
, "0") ||
240 !strcasecmp(tgt_value
, "false")) {
245 if (!strcmp(tgt_value
, "1") || !strcasecmp(tgt_value
, "true")) {
246 dst
->fd
= STDERR_FILENO
;
250 if (strlen(tgt_value
) == 1 && isdigit(*tgt_value
)) {
251 dst
->fd
= atoi(tgt_value
);
255 if (is_absolute_path(tgt_value
)) {
256 if (is_directory(tgt_value
))
257 return tr2_dst_try_auto_path(dst
, tgt_value
);
259 return tr2_dst_try_path(dst
, tgt_value
);
262 #ifndef NO_UNIX_SOCKETS
263 if (starts_with(tgt_value
, PREFIX_AF_UNIX
))
264 return tr2_dst_try_unix_domain_socket(dst
, tgt_value
);
267 /* Always warn about malformed values. */
268 tr2_dst_malformed_warning(dst
, tgt_value
);
269 tr2_dst_trace_disable(dst
);
273 int tr2_dst_trace_want(struct tr2_dst
*dst
)
275 return !!tr2_dst_get_trace_fd(dst
);
278 void tr2_dst_write_line(struct tr2_dst
*dst
, struct strbuf
*buf_line
)
280 int fd
= tr2_dst_get_trace_fd(dst
);
282 strbuf_complete_line(buf_line
); /* ensure final NL on buffer */
285 * We do not use write_in_full() because we do not want
286 * a short-write to try again. We are using O_APPEND mode
287 * files and the kernel handles the atomic seek+write. If
288 * another thread or git process is concurrently writing to
289 * this fd or file, our remainder-write may not be contiguous
290 * with our initial write of this message. And that will
291 * confuse readers. So just don't bother.
293 * It is assumed that TRACE2 messages are short enough that
294 * the system can write them in 1 attempt and we won't see
297 * If we get an IO error, just close the trace dst.
299 if (write(fd
, buf_line
->buf
, buf_line
->len
) >= 0)
302 if (tr2_dst_want_warning())
303 warning("unable to write trace to '%s': %s",
304 tr2_sysenv_display_name(dst
->sysenv_var
),
306 tr2_dst_trace_disable(dst
);