2 #include "trace2/tr2_dst.h"
3 #include "trace2/tr2_sysenv.h"
6 * If a Trace2 target cannot be opened for writing, we should issue a
7 * warning to stderr, but this is very annoying if the target is a pipe
8 * or socket and beyond the user's control -- especially since every
9 * git command (and sub-command) will print the message. So we silently
10 * eat these warnings and just discard the trace data.
12 static int tr2_dst_want_warning(void)
14 static int tr2env_dst_debug = -1;
16 if (tr2env_dst_debug == -1) {
17 const char *env_value = tr2_sysenv_get(TR2_SYSENV_DST_DEBUG);
18 if (!env_value || !*env_value)
21 tr2env_dst_debug = atoi(env_value) > 0;
24 return tr2env_dst_debug;
27 void tr2_dst_trace_disable(struct tr2_dst *dst)
36 static int tr2_dst_try_path(struct tr2_dst *dst, const char *tgt_value)
38 int fd = open(tgt_value, O_WRONLY | O_APPEND | O_CREAT, 0666);
40 if (tr2_dst_want_warning())
41 warning("trace2: could not open '%s' for '%s' tracing: %s",
43 tr2_sysenv_display_name(dst->sysenv_var),
46 tr2_dst_trace_disable(dst);
57 #ifndef NO_UNIX_SOCKETS
58 #define PREFIX_AF_UNIX "af_unix:"
59 #define PREFIX_AF_UNIX_STREAM "af_unix:stream:"
60 #define PREFIX_AF_UNIX_DGRAM "af_unix:dgram:"
62 static int tr2_dst_try_uds_connect(const char *path, int sock_type, int *out_fd)
65 struct sockaddr_un sa;
67 fd = socket(AF_UNIX, sock_type, 0);
71 sa.sun_family = AF_UNIX;
72 strlcpy(sa.sun_path, path, sizeof(sa.sun_path));
74 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
84 #define TR2_DST_UDS_TRY_STREAM (1 << 0)
85 #define TR2_DST_UDS_TRY_DGRAM (1 << 1)
87 static int tr2_dst_try_unix_domain_socket(struct tr2_dst *dst,
88 const char *tgt_value)
90 unsigned int uds_try = 0;
93 const char *path = NULL;
96 * Allow "af_unix:[<type>:]<absolute_path>"
98 * Trace2 always writes complete individual messages (without
99 * chunking), so we can talk to either DGRAM or STREAM type sockets.
101 * Allow the user to explicitly request the socket type.
103 * If they omit the socket type, try one and then the other.
106 if (skip_prefix(tgt_value, PREFIX_AF_UNIX_STREAM, &path))
107 uds_try |= TR2_DST_UDS_TRY_STREAM;
109 else if (skip_prefix(tgt_value, PREFIX_AF_UNIX_DGRAM, &path))
110 uds_try |= TR2_DST_UDS_TRY_DGRAM;
112 else if (skip_prefix(tgt_value, PREFIX_AF_UNIX, &path))
113 uds_try |= TR2_DST_UDS_TRY_STREAM | TR2_DST_UDS_TRY_DGRAM;
115 if (!path || !*path) {
116 if (tr2_dst_want_warning())
117 warning("trace2: invalid AF_UNIX value '%s' for '%s' tracing",
119 tr2_sysenv_display_name(dst->sysenv_var));
121 tr2_dst_trace_disable(dst);
125 if (!is_absolute_path(path) ||
126 strlen(path) >= sizeof(((struct sockaddr_un *)0)->sun_path)) {
127 if (tr2_dst_want_warning())
128 warning("trace2: invalid AF_UNIX path '%s' for '%s' tracing",
129 path, tr2_sysenv_display_name(dst->sysenv_var));
131 tr2_dst_trace_disable(dst);
135 if (uds_try & TR2_DST_UDS_TRY_STREAM) {
136 e = tr2_dst_try_uds_connect(path, SOCK_STREAM, &fd);
142 if (uds_try & TR2_DST_UDS_TRY_DGRAM) {
143 e = tr2_dst_try_uds_connect(path, SOCK_DGRAM, &fd);
149 if (tr2_dst_want_warning())
150 warning("trace2: could not connect to socket '%s' for '%s' tracing: %s",
151 path, tr2_sysenv_display_name(dst->sysenv_var),
154 tr2_dst_trace_disable(dst);
160 dst->initialized = 1;
166 static void tr2_dst_malformed_warning(struct tr2_dst *dst,
167 const char *tgt_value)
169 struct strbuf buf = STRBUF_INIT;
171 strbuf_addf(&buf, "trace2: unknown value for '%s': '%s'",
172 tr2_sysenv_display_name(dst->sysenv_var), tgt_value);
173 warning("%s", buf.buf);
175 strbuf_release(&buf);
178 int tr2_dst_get_trace_fd(struct tr2_dst *dst)
180 const char *tgt_value;
182 /* don't open twice */
183 if (dst->initialized)
186 dst->initialized = 1;
188 tgt_value = tr2_sysenv_get(dst->sysenv_var);
190 if (!tgt_value || !strcmp(tgt_value, "") || !strcmp(tgt_value, "0") ||
191 !strcasecmp(tgt_value, "false")) {
196 if (!strcmp(tgt_value, "1") || !strcasecmp(tgt_value, "true")) {
197 dst->fd = STDERR_FILENO;
201 if (strlen(tgt_value) == 1 && isdigit(*tgt_value)) {
202 dst->fd = atoi(tgt_value);
206 if (is_absolute_path(tgt_value))
207 return tr2_dst_try_path(dst, tgt_value);
209 #ifndef NO_UNIX_SOCKETS
210 if (starts_with(tgt_value, PREFIX_AF_UNIX))
211 return tr2_dst_try_unix_domain_socket(dst, tgt_value);
214 /* Always warn about malformed values. */
215 tr2_dst_malformed_warning(dst, tgt_value);
216 tr2_dst_trace_disable(dst);
220 int tr2_dst_trace_want(struct tr2_dst *dst)
222 return !!tr2_dst_get_trace_fd(dst);
225 void tr2_dst_write_line(struct tr2_dst *dst, struct strbuf *buf_line)
227 int fd = tr2_dst_get_trace_fd(dst);
229 strbuf_complete_line(buf_line); /* ensure final NL on buffer */
232 * We do not use write_in_full() because we do not want
233 * a short-write to try again. We are using O_APPEND mode
234 * files and the kernel handles the atomic seek+write. If
235 * another thread or git process is concurrently writing to
236 * this fd or file, our remainder-write may not be contiguous
237 * with our initial write of this message. And that will
238 * confuse readers. So just don't bother.
240 * It is assumed that TRACE2 messages are short enough that
241 * the system can write them in 1 attempt and we won't see
244 * If we get an IO error, just close the trace dst.
246 if (write(fd, buf_line->buf, buf_line->len) >= 0)
249 if (tr2_dst_want_warning())
250 warning("unable to write trace to '%s': %s",
251 tr2_sysenv_display_name(dst->sysenv_var),
253 tr2_dst_trace_disable(dst);