-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_context.c
More file actions
220 lines (196 loc) · 5.34 KB
/
Copy pathapp_context.c
File metadata and controls
220 lines (196 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "app_context.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
/* Fixed-capacity open-addressed table with linear probing; on overflow the home
* slot is evicted. True LRU isn't needed since process lifetimes far exceed query rates. */
#define CACHE_CAP 1024
struct app_cache {
struct app_meta slots[CACHE_CAP];
int used;
};
struct app_cache *app_cache_new(void)
{
struct app_cache *c = calloc(1, sizeof(*c));
return c;
}
void app_cache_free(struct app_cache *c)
{
free(c);
}
/* Read up to cap-1 bytes into buf, NUL-terminate; byte count (>=0) or -1. */
static ssize_t read_proc_file(const char *path, char *buf, size_t cap)
{
int fd = open(path, O_RDONLY);
if (fd < 0)
return -1;
ssize_t n = read(fd, buf, cap - 1);
close(fd);
if (n < 0)
return -1;
buf[n] = '\0';
return n;
}
static void resolve_exe(__u32 tgid, char *out, size_t cap)
{
char path[64];
snprintf(path, sizeof(path), "/proc/%u/exe", tgid);
ssize_t n = readlink(path, out, cap - 1);
if (n < 0)
n = 0;
out[n] = '\0';
}
/* /proc/<pid>/cmdline is NUL-separated argv; join with spaces. */
static void resolve_cmdline(__u32 tgid, char *out, size_t cap)
{
char path[64];
snprintf(path, sizeof(path), "/proc/%u/cmdline", tgid);
char raw[APP_CMDLINE_MAX];
ssize_t n = read_proc_file(path, raw, sizeof(raw));
if (n <= 0) {
out[0] = '\0';
return;
}
size_t o = 0;
for (ssize_t i = 0; i < n && o + 1 < cap; i++) {
char ch = raw[i];
if (ch == '\0') {
/* interior NUL -> single space; skip runs to avoid doubles */
if (i + 1 < n && raw[i + 1] != '\0' && o > 0)
out[o++] = ' ';
continue;
}
out[o++] = ch;
}
out[o] = '\0';
}
/* Extract a container id from a /proc/<pid>/cgroup line. Runtimes write it as the
* last path segment (e.g. .../docker-<64hex>.scope). Take the final component,
* strip a known runtime prefix and ".scope" suffix, accept only a >=12-char hex id.
* https://man7.org/linux/man-pages/man7/cgroups.7.html */
static int extract_container_id(const char *line, char *out, size_t cap)
{
const char *slash = strrchr(line, '/');
const char *seg = slash ? slash + 1 : line;
char tmp[APP_CONTAINER_MAX];
size_t n = strlen(seg);
while (n > 0 && (seg[n - 1] == '\n' || seg[n - 1] == '\r'))
n--;
if (n == 0 || n >= sizeof(tmp))
return 0;
memcpy(tmp, seg, n);
tmp[n] = '\0';
char *dot = strstr(tmp, ".scope");
if (dot)
*dot = '\0';
static const char *prefixes[] = { "docker-", "crio-", "containerd-",
"libpod-", "cri-containerd-" };
char *id = tmp;
for (size_t i = 0; i < sizeof(prefixes) / sizeof(prefixes[0]); i++) {
size_t pl = strlen(prefixes[i]);
if (strncmp(id, prefixes[i], pl) == 0) {
id += pl;
break;
}
}
size_t idlen = strlen(id);
if (idlen < 12)
return 0;
for (size_t i = 0; i < idlen; i++) {
if (!isxdigit((unsigned char)id[i]))
return 0;
}
if (idlen >= cap)
idlen = cap - 1;
memcpy(out, id, idlen);
out[idlen] = '\0';
return 1;
}
static void resolve_container(__u32 tgid, char *out, size_t cap)
{
out[0] = '\0';
char path[64];
snprintf(path, sizeof(path), "/proc/%u/cgroup", tgid);
char buf[4096];
ssize_t n = read_proc_file(path, buf, sizeof(buf));
if (n <= 0)
return;
/* first line that yields a hex id wins */
char *save = NULL;
for (char *line = strtok_r(buf, "\n", &save); line;
line = strtok_r(NULL, "\n", &save)) {
if (extract_container_id(line, out, cap))
return;
}
}
/* Service name from /proc/<pid>/environ (NUL-separated KEY=VALUE): prefer
* OTEL_SERVICE_NAME, then HOSTNAME (k8s pod name), else comm. */
static void resolve_service(__u32 tgid, const char *comm, char *out, size_t cap)
{
char path[64];
snprintf(path, sizeof(path), "/proc/%u/environ", tgid);
int fd = open(path, O_RDONLY);
char found_host[APP_SERVICE_MAX] = { 0 };
if (fd >= 0) {
char buf[8192];
ssize_t n = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (n > 0) {
buf[n] = '\0';
ssize_t i = 0;
while (i < n) {
const char *kv = &buf[i];
size_t len = strnlen(kv, n - i);
if (strncmp(kv, "OTEL_SERVICE_NAME=", 18) == 0) {
snprintf(out, cap, "%s", kv + 18);
return;
}
if (strncmp(kv, "HOSTNAME=", 9) == 0)
snprintf(found_host, sizeof(found_host), "%s", kv + 9);
i += len + 1;
}
}
}
if (found_host[0]) {
snprintf(out, cap, "%s", found_host);
return;
}
snprintf(out, cap, "%s", comm ? comm : "");
}
static void resolve_meta(struct app_meta *m, __u32 tgid, const char *comm)
{
m->tgid = tgid;
resolve_exe(tgid, m->exe, sizeof(m->exe));
resolve_cmdline(tgid, m->cmdline, sizeof(m->cmdline));
resolve_container(tgid, m->container_id, sizeof(m->container_id));
resolve_service(tgid, comm, m->service_name, sizeof(m->service_name));
m->resolved = 1;
}
const struct app_meta *app_cache_get(struct app_cache *c, __u32 tgid,
const char *comm)
{
__u32 start = tgid % CACHE_CAP;
__u32 free_slot = CACHE_CAP;
for (__u32 probe = 0; probe < CACHE_CAP; probe++) {
__u32 i = (start + probe) % CACHE_CAP;
struct app_meta *m = &c->slots[i];
if (m->resolved && m->tgid == tgid)
return m;
if (!m->resolved) {
free_slot = i;
break;
}
}
/* table full with no match: evict the home slot */
if (free_slot == CACHE_CAP)
free_slot = start;
struct app_meta *m = &c->slots[free_slot];
memset(m, 0, sizeof(*m));
resolve_meta(m, tgid, comm);
if (c->used < CACHE_CAP)
c->used++;
return m;
}