| 1 |
|
| /* otel_id.h - trace and span ids. |
| 2 |
|
| * |
| 3 |
|
| * A trace id is 16 bytes, a span id 8, and both must be random enough that |
| 4 |
|
| * two processes that have never met do not collide. getentropy(2) where it |
| 5 |
|
| * exists, a cached /dev/urandom descriptor where it does not. |
| 6 |
|
| * |
| 7 |
|
| * The descriptor is cached because opening /dev/urandom per span would be a |
| 8 |
|
| * syscall and a file-table entry per request; it is reopened after a fork, |
| 9 |
|
| * because a child inheriting the parent's descriptor is fine but a child |
| 10 |
|
| * inheriting a BUFFER would not be - which is exactly why nothing here |
| 11 |
|
| * buffers. |
| 12 |
|
| * |
| 13 |
|
| * ALL-ZERO IS NOT A VALID ID. The spec says so, and it matters in both |
| 14 |
|
| * directions: never generate one, and treat one arriving in a header as |
| 15 |
|
| * absent rather than as a parent. A generator that can return all-zero turns |
| 16 |
|
| * "no parent" into "a parent nobody has", which is worse than either. |
| 17 |
|
| */ |
| 18 |
|
| |
| 19 |
|
| #ifndef OTEL_ID_H |
| 20 |
|
| #define OTEL_ID_H |
| 21 |
|
| |
| 22 |
|
| #include <fcntl.h> |
| 23 |
|
| #ifndef _WIN32 |
| 24 |
|
| # include <unistd.h> |
| 25 |
|
| #endif |
| 26 |
|
| #ifdef OTEL_GETENTROPY_SYS_RANDOM |
| 27 |
|
| # include <sys/random.h> |
| 28 |
|
| #endif |
| 29 |
|
| |
| 30 |
|
| static int OTEL_URANDOM_FD = -1; |
| 31 |
|
| static pid_t OTEL_URANDOM_PID = 0; |
| 32 |
|
| |
| 33 |
|
| /* Fill n bytes. Returns 1 on success, 0 when no source could be reached. */ |
| 34 |
10626 |
| static int otel_random_bytes(unsigned char *out, size_t n) { |
| 35 |
|
| #ifdef OTEL_HAVE_GETENTROPY |
| 36 |
|
| /* getentropy caps at 256 bytes a call; an id is 16, so one call always. */ |
| 37 |
10626 |
▶ | if (n <= 256 && getentropy(out, n) == 0) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n <= 256 && getentropy(out, n) == 0) return 1; |
10626 |
0 |
10626 |
| if (n <= 256 && getentropy(out, n) == 0) return 1; |
10626 |
0 |
10626 |
|
| 38 |
|
| #endif |
| 39 |
|
| #ifndef _WIN32 |
| 40 |
|
| { |
| 41 |
0 |
| pid_t me = getpid(); |
| 42 |
0 |
| size_t got = 0; |
| 43 |
|
| /* Reopen after a fork. The descriptor itself would survive, but a |
| 44 |
|
| * worker that inherited the parent's is a worker sharing a source |
| 45 |
|
| * with every sibling, and this is the one place in the SDK where |
| 46 |
|
| * sharing state across a fork is a correctness question rather than |
| 47 |
|
| * a performance one. */ |
| 48 |
0 |
▶ | if (OTEL_URANDOM_FD >= 0 && OTEL_URANDOM_PID != me) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (OTEL_URANDOM_FD >= 0 && OTEL_URANDOM_PID != me) { |
0 |
0 |
0 |
| if (OTEL_URANDOM_FD >= 0 && OTEL_URANDOM_PID != me) { |
0 |
0 |
0 |
|
| 49 |
0 |
| close(OTEL_URANDOM_FD); |
| 50 |
0 |
| OTEL_URANDOM_FD = -1; |
| 51 |
|
| } |
| 52 |
0 |
▶ | if (OTEL_URANDOM_FD < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (OTEL_URANDOM_FD < 0) { |
0 |
0 |
0 |
|
| 53 |
0 |
| OTEL_URANDOM_FD = open("/dev/urandom", O_RDONLY); |
| 54 |
0 |
| OTEL_URANDOM_PID = me; |
| 55 |
|
| } |
| 56 |
0 |
▶ | if (OTEL_URANDOM_FD >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (OTEL_URANDOM_FD >= 0) { |
0 |
0 |
0 |
|
| 57 |
0 |
▶ | while (got < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (got < n) { |
0 |
0 |
0 |
|
| 58 |
0 |
| ssize_t r = read(OTEL_URANDOM_FD, out + got, n - got); |
| 59 |
0 |
▶ | if (r <= 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r <= 0) break; |
0 |
0 |
0 |
|
| 60 |
0 |
| got += (size_t)r; |
| 61 |
|
| } |
| 62 |
0 |
▶ | if (got == n) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (got == n) return 1; |
0 |
0 |
0 |
|
| 63 |
|
| } |
| 64 |
|
| } |
| 65 |
|
| #endif |
| 66 |
0 |
| return 0; |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
11217 |
| static int otel_all_zero(const unsigned char *p, size_t n) { |
| 70 |
|
| size_t i; |
| 71 |
12043 |
▶ | for (i = 0; i < n; i++) if (p[i]) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < n; i++) if (p[i]) return 0; |
11212 |
826 |
12038 |
| for (i = 0; i < n; i++) if (p[i]) return 0; |
12038 |
5 |
12043 |
|
| 72 |
5 |
| return 1; |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
|
| /* Generate an id, retrying the vanishingly unlikely all-zero draw. The retry |
| 76 |
|
| * is cheap and the alternative is a span that claims a parent nobody has. */ |
| 77 |
10626 |
| static int otel_gen_id(unsigned char *out, size_t n) { |
| 78 |
|
| int tries; |
| 79 |
10626 |
▶ | for (tries = 0; tries < 4; tries++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (tries = 0; tries < 4; tries++) { |
10626 |
0 |
10626 |
|
| 80 |
10626 |
▶ | if (!otel_random_bytes(out, n)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!otel_random_bytes(out, n)) return 0; |
0 |
10626 |
10626 |
|
| 81 |
10626 |
▶ | if (!otel_all_zero(out, n)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!otel_all_zero(out, n)) return 1; |
10626 |
0 |
10626 |
|
| 82 |
|
| } |
| 83 |
0 |
| return 0; |
| 84 |
|
| } |
| 85 |
|
| |
| 86 |
|
| /* Parse `want` bytes of lowercase or uppercase hex. Returns 1 on success. |
| 87 |
|
| * Rejects an all-zero result, which is how an invalid id in an inbound header |
| 88 |
|
| * becomes "no parent" rather than a parent that cannot exist. */ |
| 89 |
538 |
| static int otel_hex_to_bytes(const char *s, STRLEN len, unsigned char *out, |
| 90 |
|
| STRLEN want) { |
| 91 |
|
| STRLEN i; |
| 92 |
538 |
▶ | if (len != want * 2) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len != want * 2) return 0; |
2 |
536 |
538 |
|
| 93 |
6960 |
▶ | for (i = 0; i < want; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < want; i++) { |
6425 |
535 |
6960 |
|
| 94 |
|
| int hi, lo, j, d[2]; |
| 95 |
19273 |
▶ | for (j = 0; j < 2; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < 2; j++) { |
12849 |
6424 |
19273 |
|
| 96 |
12849 |
| char c = s[i * 2 + j]; |
| 97 |
12849 |
▶ | d[j] = (c >= '0' && c <= '9') ? c - '0' |
Branch: 1 branch
| Branch | True | False | Total |
| d[j] = (c >= '0' && c <= '9') ? c - '0' |
2424 |
10425 |
12849 |
|
| 98 |
36123 |
▶ | : (c >= 'a' && c <= 'f') ? c - 'a' + 10 |
Branch: 2 branches
| Branch | True | False | Total |
| : (c >= 'a' && c <= 'f') ? c - 'a' + 10 |
12849 |
0 |
12849 |
| : (c >= 'a' && c <= 'f') ? c - 'a' + 10 |
10424 |
1 |
10425 |
|
| 99 |
20850 |
▶ | : (c >= 'A' && c <= 'F') ? c - 'A' + 10 : -1; |
Branch: 3 branches
| Branch | True | False | Total |
| : (c >= 'A' && c <= 'F') ? c - 'A' + 10 : -1; |
10425 |
0 |
10425 |
| : (c >= 'A' && c <= 'F') ? c - 'A' + 10 : -1; |
1 |
0 |
1 |
| : (c >= 'A' && c <= 'F') ? c - 'A' + 10 : -1; |
0 |
1 |
1 |
|
| 100 |
12849 |
▶ | if (d[j] < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (d[j] < 0) return 0; |
1 |
12848 |
12849 |
|
| 101 |
|
| } |
| 102 |
6424 |
| hi = d[0]; lo = d[1]; |
| 103 |
6424 |
| out[i] = (unsigned char)((hi << 4) | lo); |
| 104 |
|
| } |
| 105 |
535 |
| return !otel_all_zero(out, want); |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
630 |
| static void otel_bytes_to_hex(const unsigned char *in, STRLEN n, char *out) { |
| 109 |
|
| static const char H[] = "0123456789abcdef"; |
| 110 |
|
| STRLEN i; |
| 111 |
8470 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
7840 |
630 |
8470 |
|
| 112 |
7840 |
| out[i * 2] = H[in[i] >> 4]; |
| 113 |
7840 |
| out[i * 2 + 1] = H[in[i] & 0xf]; |
| 114 |
|
| } |
| 115 |
630 |
| } |
| 116 |
|
| |
| 117 |
|
| #endif /* OTEL_ID_H */ |