trace2: make SIDs more unique
[git] / trace2 / tr2_dst.c
1 #include "cache.h"
2 #include "trace2/tr2_dst.h"
3 #include "trace2/tr2_sysenv.h"
4
5 /*
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.
11  */
12 static int tr2_dst_want_warning(void)
13 {
14         static int tr2env_dst_debug = -1;
15
16         if (tr2env_dst_debug == -1) {
17                 const char *env_value = tr2_sysenv_get(TR2_SYSENV_DST_DEBUG);
18                 if (!env_value || !*env_value)
19                         tr2env_dst_debug = 0;
20                 else
21                         tr2env_dst_debug = atoi(env_value) > 0;
22         }
23
24         return tr2env_dst_debug;
25 }
26
27 void tr2_dst_trace_disable(struct tr2_dst *dst)
28 {
29         if (dst->need_close)
30                 close(dst->fd);
31         dst->fd = 0;
32         dst->initialized = 1;
33         dst->need_close = 0;
34 }
35
36 static int tr2_dst_try_path(struct tr2_dst *dst, const char *tgt_value)
37 {
38         int fd = open(tgt_value, O_WRONLY | O_APPEND | O_CREAT, 0666);
39         if (fd == -1) {
40                 if (tr2_dst_want_warning())
41                         warning("trace2: could not open '%s' for '%s' tracing: %s",
42                                 tgt_value,
43                                 tr2_sysenv_display_name(dst->sysenv_var),
44                                 strerror(errno));
45
46                 tr2_dst_trace_disable(dst);
47                 return 0;
48         }
49
50         dst->fd = fd;
51         dst->need_close = 1;
52         dst->initialized = 1;
53
54         return dst->fd;
55 }
56
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:"
61
62 static int tr2_dst_try_uds_connect(const char *path, int sock_type, int *out_fd)
63 {
64         int fd;
65         struct sockaddr_un sa;
66
67         fd = socket(AF_UNIX, sock_type, 0);
68         if (fd == -1)
69                 return errno;
70
71         sa.sun_family = AF_UNIX;
72         strlcpy(sa.sun_path, path, sizeof(sa.sun_path));
73
74         if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
75                 int e = errno;
76                 close(fd);
77                 return e;
78         }
79
80         *out_fd = fd;
81         return 0;
82 }
83
84 #define TR2_DST_UDS_TRY_STREAM (1 << 0)
85 #define TR2_DST_UDS_TRY_DGRAM  (1 << 1)
86
87 static int tr2_dst_try_unix_domain_socket(struct tr2_dst *dst,
88                                           const char *tgt_value)
89 {
90         unsigned int uds_try = 0;
91         int fd;
92         int e;
93         const char *path = NULL;
94
95         /*
96          * Allow "af_unix:[<type>:]<absolute_path>"
97          *
98          * Trace2 always writes complete individual messages (without
99          * chunking), so we can talk to either DGRAM or STREAM type sockets.
100          *
101          * Allow the user to explicitly request the socket type.
102          *
103          * If they omit the socket type, try one and then the other.
104          */
105
106         if (skip_prefix(tgt_value, PREFIX_AF_UNIX_STREAM, &path))
107                 uds_try |= TR2_DST_UDS_TRY_STREAM;
108
109         else if (skip_prefix(tgt_value, PREFIX_AF_UNIX_DGRAM, &path))
110                 uds_try |= TR2_DST_UDS_TRY_DGRAM;
111
112         else if (skip_prefix(tgt_value, PREFIX_AF_UNIX, &path))
113                 uds_try |= TR2_DST_UDS_TRY_STREAM | TR2_DST_UDS_TRY_DGRAM;
114
115         if (!path || !*path) {
116                 if (tr2_dst_want_warning())
117                         warning("trace2: invalid AF_UNIX value '%s' for '%s' tracing",
118                                 tgt_value,
119                                 tr2_sysenv_display_name(dst->sysenv_var));
120
121                 tr2_dst_trace_disable(dst);
122                 return 0;
123         }
124
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));
130
131                 tr2_dst_trace_disable(dst);
132                 return 0;
133         }
134
135         if (uds_try & TR2_DST_UDS_TRY_STREAM) {
136                 e = tr2_dst_try_uds_connect(path, SOCK_STREAM, &fd);
137                 if (!e)
138                         goto connected;
139                 if (e != EPROTOTYPE)
140                         goto error;
141         }
142         if (uds_try & TR2_DST_UDS_TRY_DGRAM) {
143                 e = tr2_dst_try_uds_connect(path, SOCK_DGRAM, &fd);
144                 if (!e)
145                         goto connected;
146         }
147
148 error:
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),
152                         strerror(e));
153
154         tr2_dst_trace_disable(dst);
155         return 0;
156
157 connected:
158         dst->fd = fd;
159         dst->need_close = 1;
160         dst->initialized = 1;
161
162         return dst->fd;
163 }
164 #endif
165
166 static void tr2_dst_malformed_warning(struct tr2_dst *dst,
167                                       const char *tgt_value)
168 {
169         struct strbuf buf = STRBUF_INIT;
170
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);
174
175         strbuf_release(&buf);
176 }
177
178 int tr2_dst_get_trace_fd(struct tr2_dst *dst)
179 {
180         const char *tgt_value;
181
182         /* don't open twice */
183         if (dst->initialized)
184                 return dst->fd;
185
186         dst->initialized = 1;
187
188         tgt_value = tr2_sysenv_get(dst->sysenv_var);
189
190         if (!tgt_value || !strcmp(tgt_value, "") || !strcmp(tgt_value, "0") ||
191             !strcasecmp(tgt_value, "false")) {
192                 dst->fd = 0;
193                 return dst->fd;
194         }
195
196         if (!strcmp(tgt_value, "1") || !strcasecmp(tgt_value, "true")) {
197                 dst->fd = STDERR_FILENO;
198                 return dst->fd;
199         }
200
201         if (strlen(tgt_value) == 1 && isdigit(*tgt_value)) {
202                 dst->fd = atoi(tgt_value);
203                 return dst->fd;
204         }
205
206         if (is_absolute_path(tgt_value))
207                 return tr2_dst_try_path(dst, tgt_value);
208
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);
212 #endif
213
214         /* Always warn about malformed values. */
215         tr2_dst_malformed_warning(dst, tgt_value);
216         tr2_dst_trace_disable(dst);
217         return 0;
218 }
219
220 int tr2_dst_trace_want(struct tr2_dst *dst)
221 {
222         return !!tr2_dst_get_trace_fd(dst);
223 }
224
225 void tr2_dst_write_line(struct tr2_dst *dst, struct strbuf *buf_line)
226 {
227         int fd = tr2_dst_get_trace_fd(dst);
228
229         strbuf_complete_line(buf_line); /* ensure final NL on buffer */
230
231         /*
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.
239          *
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
242          * a short-write.
243          *
244          * If we get an IO error, just close the trace dst.
245          */
246         if (write(fd, buf_line->buf, buf_line->len) >= 0)
247                 return;
248
249         if (tr2_dst_want_warning())
250                 warning("unable to write trace to '%s': %s",
251                         tr2_sysenv_display_name(dst->sysenv_var),
252                         strerror(errno));
253         tr2_dst_trace_disable(dst);
254 }