format-patch: replace erroneous and condition
[git.git] / trace2 / tr2_dst.c
blob5dda0ca1cdb5d09a8cdfb27dd45d4436f3e8aa6f
1 #include "cache.h"
2 #include "trace2/tr2_dst.h"
3 #include "trace2/tr2_sid.h"
4 #include "trace2/tr2_sysenv.h"
6 /*
7 * How many attempts we will make at creating an automatically-named trace file.
8 */
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)
18 tr2env_dst_debug = 0;
19 else
20 tr2env_dst_debug = atoi(env_value) > 0;
23 return tr2env_dst_debug;
26 void tr2_dst_trace_disable(struct tr2_dst *dst)
28 if (dst->need_close)
29 close(dst->fd);
30 dst->fd = 0;
31 dst->initialized = 1;
32 dst->need_close = 0;
35 static int tr2_dst_try_auto_path(struct tr2_dst *dst, const char *tgt_prefix)
37 int fd;
38 const char *last_slash, *sid = tr2_sid_get();
39 struct strbuf path = STRBUF_INIT;
40 size_t base_path_len;
41 unsigned attempt_count;
43 last_slash = strrchr(sid, '/');
44 if (last_slash)
45 sid = last_slash + 1;
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);
60 if (fd != -1)
61 break;
64 if (fd == -1) {
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),
69 strerror(errno));
71 tr2_dst_trace_disable(dst);
72 strbuf_release(&path);
73 return 0;
76 strbuf_release(&path);
78 dst->fd = fd;
79 dst->need_close = 1;
80 dst->initialized = 1;
82 return dst->fd;
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);
88 if (fd == -1) {
89 if (tr2_dst_want_warning())
90 warning("trace2: could not open '%s' for '%s' tracing: %s",
91 tgt_value,
92 tr2_sysenv_display_name(dst->sysenv_var),
93 strerror(errno));
95 tr2_dst_trace_disable(dst);
96 return 0;
99 dst->fd = fd;
100 dst->need_close = 1;
101 dst->initialized = 1;
103 return dst->fd;
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)
113 int fd;
114 struct sockaddr_un sa;
116 fd = socket(AF_UNIX, sock_type, 0);
117 if (fd == -1)
118 return errno;
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) {
124 int e = errno;
125 close(fd);
126 return e;
129 *out_fd = fd;
130 return 0;
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;
140 int fd;
141 int e;
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",
167 tgt_value,
168 tr2_sysenv_display_name(dst->sysenv_var));
170 tr2_dst_trace_disable(dst);
171 return 0;
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);
181 return 0;
184 if (uds_try & TR2_DST_UDS_TRY_STREAM) {
185 e = tr2_dst_try_uds_connect(path, SOCK_STREAM, &fd);
186 if (!e)
187 goto connected;
188 if (e != EPROTOTYPE)
189 goto error;
191 if (uds_try & TR2_DST_UDS_TRY_DGRAM) {
192 e = tr2_dst_try_uds_connect(path, SOCK_DGRAM, &fd);
193 if (!e)
194 goto connected;
197 error:
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),
201 strerror(e));
203 tr2_dst_trace_disable(dst);
204 return 0;
206 connected:
207 dst->fd = fd;
208 dst->need_close = 1;
209 dst->initialized = 1;
211 return dst->fd;
213 #endif
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)
233 return dst->fd;
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")) {
241 dst->fd = 0;
242 return dst->fd;
245 if (!strcmp(tgt_value, "1") || !strcasecmp(tgt_value, "true")) {
246 dst->fd = STDERR_FILENO;
247 return dst->fd;
250 if (strlen(tgt_value) == 1 && isdigit(*tgt_value)) {
251 dst->fd = atoi(tgt_value);
252 return dst->fd;
255 if (is_absolute_path(tgt_value)) {
256 if (is_directory(tgt_value))
257 return tr2_dst_try_auto_path(dst, tgt_value);
258 else
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);
265 #endif
267 /* Always warn about malformed values. */
268 tr2_dst_malformed_warning(dst, tgt_value);
269 tr2_dst_trace_disable(dst);
270 return 0;
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
295 * a short-write.
297 * If we get an IO error, just close the trace dst.
299 if (write(fd, buf_line->buf, buf_line->len) >= 0)
300 return;
302 if (tr2_dst_want_warning())
303 warning("unable to write trace to '%s': %s",
304 tr2_sysenv_display_name(dst->sysenv_var),
305 strerror(errno));
306 tr2_dst_trace_disable(dst);