trace2 / tr2_dst.con commit Merge branch 'jh/trace2-sid-fix' (5b2d1c0)
   1#include "cache.h"
   2#include "trace2/tr2_dst.h"
   3#include "trace2/tr2_sid.h"
   4#include "trace2/tr2_sysenv.h"
   5
   6/*
   7 * How many attempts we will make at creating an automatically-named trace file.
   8 */
   9#define MAX_AUTO_ATTEMPTS 10
  10
  11static int tr2_dst_want_warning(void)
  12{
  13        static int tr2env_dst_debug = -1;
  14
  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;
  21        }
  22
  23        return tr2env_dst_debug;
  24}
  25
  26void tr2_dst_trace_disable(struct tr2_dst *dst)
  27{
  28        if (dst->need_close)
  29                close(dst->fd);
  30        dst->fd = 0;
  31        dst->initialized = 1;
  32        dst->need_close = 0;
  33}
  34
  35static int tr2_dst_try_auto_path(struct tr2_dst *dst, const char *tgt_prefix)
  36{
  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;
  42
  43        last_slash = strrchr(sid, '/');
  44        if (last_slash)
  45                sid = last_slash + 1;
  46
  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;
  52
  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);
  57                }
  58
  59                fd = open(path.buf, O_WRONLY | O_CREAT | O_EXCL, 0666);
  60                if (fd != -1)
  61                        break;
  62        }
  63
  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));
  70
  71                tr2_dst_trace_disable(dst);
  72                strbuf_release(&path);
  73                return 0;
  74        }
  75
  76        strbuf_release(&path);
  77
  78        dst->fd = fd;
  79        dst->need_close = 1;
  80        dst->initialized = 1;
  81
  82        return dst->fd;
  83}
  84
  85static int tr2_dst_try_path(struct tr2_dst *dst, const char *tgt_value)
  86{
  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));
  94
  95                tr2_dst_trace_disable(dst);
  96                return 0;
  97        }
  98
  99        dst->fd = fd;
 100        dst->need_close = 1;
 101        dst->initialized = 1;
 102
 103        return dst->fd;
 104}
 105
 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:"
 110
 111static int tr2_dst_try_uds_connect(const char *path, int sock_type, int *out_fd)
 112{
 113        int fd;
 114        struct sockaddr_un sa;
 115
 116        fd = socket(AF_UNIX, sock_type, 0);
 117        if (fd == -1)
 118                return errno;
 119
 120        sa.sun_family = AF_UNIX;
 121        strlcpy(sa.sun_path, path, sizeof(sa.sun_path));
 122
 123        if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
 124                int e = errno;
 125                close(fd);
 126                return e;
 127        }
 128
 129        *out_fd = fd;
 130        return 0;
 131}
 132
 133#define TR2_DST_UDS_TRY_STREAM (1 << 0)
 134#define TR2_DST_UDS_TRY_DGRAM  (1 << 1)
 135
 136static int tr2_dst_try_unix_domain_socket(struct tr2_dst *dst,
 137                                          const char *tgt_value)
 138{
 139        unsigned int uds_try = 0;
 140        int fd;
 141        int e;
 142        const char *path = NULL;
 143
 144        /*
 145         * Allow "af_unix:[<type>:]<absolute_path>"
 146         *
 147         * Trace2 always writes complete individual messages (without
 148         * chunking), so we can talk to either DGRAM or STREAM type sockets.
 149         *
 150         * Allow the user to explicitly request the socket type.
 151         *
 152         * If they omit the socket type, try one and then the other.
 153         */
 154
 155        if (skip_prefix(tgt_value, PREFIX_AF_UNIX_STREAM, &path))
 156                uds_try |= TR2_DST_UDS_TRY_STREAM;
 157
 158        else if (skip_prefix(tgt_value, PREFIX_AF_UNIX_DGRAM, &path))
 159                uds_try |= TR2_DST_UDS_TRY_DGRAM;
 160
 161        else if (skip_prefix(tgt_value, PREFIX_AF_UNIX, &path))
 162                uds_try |= TR2_DST_UDS_TRY_STREAM | TR2_DST_UDS_TRY_DGRAM;
 163
 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));
 169
 170                tr2_dst_trace_disable(dst);
 171                return 0;
 172        }
 173
 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));
 179
 180                tr2_dst_trace_disable(dst);
 181                return 0;
 182        }
 183
 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;
 190        }
 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;
 195        }
 196
 197error:
 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));
 202
 203        tr2_dst_trace_disable(dst);
 204        return 0;
 205
 206connected:
 207        dst->fd = fd;
 208        dst->need_close = 1;
 209        dst->initialized = 1;
 210
 211        return dst->fd;
 212}
 213#endif
 214
 215static void tr2_dst_malformed_warning(struct tr2_dst *dst,
 216                                      const char *tgt_value)
 217{
 218        struct strbuf buf = STRBUF_INIT;
 219
 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);
 223
 224        strbuf_release(&buf);
 225}
 226
 227int tr2_dst_get_trace_fd(struct tr2_dst *dst)
 228{
 229        const char *tgt_value;
 230
 231        /* don't open twice */
 232        if (dst->initialized)
 233                return dst->fd;
 234
 235        dst->initialized = 1;
 236
 237        tgt_value = tr2_sysenv_get(dst->sysenv_var);
 238
 239        if (!tgt_value || !strcmp(tgt_value, "") || !strcmp(tgt_value, "0") ||
 240            !strcasecmp(tgt_value, "false")) {
 241                dst->fd = 0;
 242                return dst->fd;
 243        }
 244
 245        if (!strcmp(tgt_value, "1") || !strcasecmp(tgt_value, "true")) {
 246                dst->fd = STDERR_FILENO;
 247                return dst->fd;
 248        }
 249
 250        if (strlen(tgt_value) == 1 && isdigit(*tgt_value)) {
 251                dst->fd = atoi(tgt_value);
 252                return dst->fd;
 253        }
 254
 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);
 260        }
 261
 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
 266
 267        /* Always warn about malformed values. */
 268        tr2_dst_malformed_warning(dst, tgt_value);
 269        tr2_dst_trace_disable(dst);
 270        return 0;
 271}
 272
 273int tr2_dst_trace_want(struct tr2_dst *dst)
 274{
 275        return !!tr2_dst_get_trace_fd(dst);
 276}
 277
 278void tr2_dst_write_line(struct tr2_dst *dst, struct strbuf *buf_line)
 279{
 280        int fd = tr2_dst_get_trace_fd(dst);
 281
 282        strbuf_complete_line(buf_line); /* ensure final NL on buffer */
 283
 284        /*
 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.
 292         *
 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.
 296         *
 297         * If we get an IO error, just close the trace dst.
 298         */
 299        if (write(fd, buf_line->buf, buf_line->len) >= 0)
 300                return;
 301
 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);
 307}