-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathqutil.c
More file actions
379 lines (317 loc) · 6.43 KB
/
Copy pathqutil.c
File metadata and controls
379 lines (317 loc) · 6.43 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// SPDX-License-Identifier: Apache-2.0
/* Copyright (c) 2024-2026 Elastic NV */
#include <sys/resource.h>
#include <ctype.h> /* is_digit(3) */
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <bpf/libbpf.h>
#include "quark.h"
ssize_t
qread(int fd, void *buf, size_t count)
{
ssize_t n;
again:
n = read(fd, buf, count);
if (n == -1) {
if (errno == EINTR)
goto again;
return (-1);
}
return (n);
}
int
qwrite(int fd, const void *buf, size_t count)
{
ssize_t n;
const char *p;
for (p = buf; count != 0; p += n, count -= n) {
again:
n = write(fd, p, count);
if (n == -1) {
if (errno == EINTR)
goto again;
return (-1);
} else if (n == 0)
return (errno = EPIPE, -1);
}
return (0);
}
/*
* Safer readlinkat(2), guarantees termination and returns the number of bytes
* placed in buf (excluding NUL) so caller can check truncation, like strlcpy().
* If the return value is >= bufsiz, truncation occurred.
*/
ssize_t
qreadlinkat(int dfd, const char *pathname, char *buf, size_t bufsiz)
{
ssize_t n;
if (bufsiz < 2)
return (errno = EINVAL, -1);
if ((n = readlinkat(dfd, pathname, buf, bufsiz - 1)) == -1)
return (-1);
buf[n] = 0;
if ((size_t)n == (bufsiz - 1))
n++;
return (n);
}
/*
* Like closefrom but can keep one fd open
*/
int
qclosefrom(int lowfd, int keep)
{
int i;
long highfd;
highfd = sysconf(_SC_OPEN_MAX);
if (highfd < 0 || highfd > INT_MAX)
highfd = INT_MAX;
for (i = lowfd; i < (int)highfd; i++) {
if (i != keep)
(void)close(i);
}
return (0);
}
int
isnumber(const char *s)
{
for (; *s != 0; s++) {
if (!isdigit(*s))
return (0);
}
return (1);
}
/*
* Reads a "single-lined" file. Returns size of the line excluding NUL and
* excluding \n, guarantees termination on >= 0, truncates silently.
*/
ssize_t
readlineat(int dfd, const char *pathname, char *buf, size_t bufsiz)
{
int fd;
ssize_t n;
fd = openat(dfd, pathname, O_RDONLY);
if (fd == -1)
return (-1);
n = qread(fd, buf, bufsiz);
close(fd);
if (n == -1)
return (-1);
else if (n == 0) {
buf[0] = 0;
return (0);
}
buf[n - 1] = 0;
return (n - 1);
}
/*
* Like a strtoull but with proper detection.
*/
int
strtou64(u64 *dst, const char *v, int base)
{
char *p;
u64 u;
errno = 0;
u = strtoull(v, &p, base);
if (*p != 0 || (u == ULLONG_MAX && errno != 0))
return (-1);
*dst = u;
return (0);
}
char *
find_line(FILE *f, const char *needle)
{
char *line, *found;
size_t line_len;
ssize_t n;
long old_pos;
old_pos = ftell(f);
if (old_pos == -1)
return (NULL);
if (fseek(f, 0, SEEK_SET) == -1)
return (NULL);
line = NULL;
line_len = 0;
found = NULL;
while ((n = getline(&line, &line_len, f)) != -1) {
if (line[n - 1] == '\n')
line[n - 1] = 0;
if (strncmp(line, needle, strlen(needle)))
continue;
found = strdup(line);
break;
}
free(line);
(void)fseek(f, old_pos, SEEK_SET);
return (found);
}
char *
find_line_p(const char *path, const char *needle)
{
FILE *f;
char *line;
if ((f = fopen(path, "r")) == NULL)
return (NULL);
line = find_line(f, needle);
fclose(f);
return (line);
}
/*
* Returns file content with a NUL appended, doesn't do stat so it works on
* /proc, if total is non-null, size of read bytes is filled, not including the
* appended NUL.
*/
char *
load_file_nostat(int fd, size_t *total)
{
ssize_t n, bufsize, copied;
char *buf, *nbuf;
buf = NULL;
bufsize = 0;
copied = 0;
for (; ;) {
if (bufsize - copied == 0) {
bufsize = bufsize == 0 ? 4096 : bufsize * 2;
/* Grow with one extra for NUL */
nbuf = realloc(buf, bufsize + 1);
if (nbuf == NULL) {
free(buf);
return (NULL);
}
buf = nbuf;
}
n = qread(fd, buf + copied, bufsize - copied);
if (n == -1) {
free(buf);
return (NULL);
} else if (n == 0) {
/* We allocate an extra byte to guarantee NUL space */
buf[copied] = 0;
break;
}
copied += n;
}
/* Signal an empty file with NULL */
if (copied == 0) {
free(buf);
buf = NULL;
}
if (total != NULL)
*total = copied;
return (buf);
}
char *
load_file_path_nostat(const char *path, size_t *total)
{
int fd;
char *buf;
if ((fd = open(path, O_RDONLY)) == -1)
return (NULL);
buf = load_file_nostat(fd, total);
close(fd);
return (buf);
}
int
ipv6_supported(void)
{
int fd;
int enabled = 0;
if ((fd = socket(AF_INET6, SOCK_STREAM, 0)) != -1) {
enabled = 1;
close(fd);
}
return (enabled);
}
void
qlog_func(int pri, int do_errno, const char *func, int lineno,
const char *fmt, ...)
{
va_list ap;
char *nfmt;
int saved_errno;
if (pri > quark_verbose)
return;
if (do_errno)
saved_errno = errno;
va_start(ap, fmt);
/* best effort in out of mem situations */
if (asprintf(&nfmt, "%s:%d: %s", func, lineno, fmt) == -1) {
fprintf(stderr, "%s: %s:%d: ",
program_invocation_short_name, func, lineno);
vfprintf(stderr, fmt, ap);
if (do_errno)
fprintf(stderr, ": %s", strerror(saved_errno));
fprintf(stderr, "\n");
} else {
if (do_errno) {
errno = saved_errno;
vwarn(nfmt, ap);
} else
vwarnx(nfmt, ap);
free(nfmt);
}
va_end(ap);
}
const char *
safe_basename(const char *path)
{
const char *p;
p = strrchr(path, '/');
if (p != NULL && p[1] != 0)
return (p + 1);
return (NULL);
}
u64
fetch_boottime(void)
{
char *line;
const char *errstr, *needle;
u64 btime;
needle = "btime ";
line = find_line_p("/proc/stat", needle);
if (line == NULL)
return (0);
btime = strtonum(line + strlen(needle), 1, LLONG_MAX, &errstr);
free(line);
if (errstr != NULL)
qwarnx("can't parse btime: %s", errstr);
return (btime * NS_PER_S);
}
/*
* Map libbpf logs into quark_verbose.
* fmt has a newline, we have to prepend program_invocatin_short_name, so we
* can't use vwarn, as it prepends itself and adds a newline.
*/
static int
libbpf_print_fn(enum libbpf_print_level level, const char *fmt, va_list ap)
{
int pri;
char *nfmt;
if (level == LIBBPF_WARN || level == LIBBPF_INFO)
pri = QUARK_VL_WARN;
else if (level == LIBBPF_DEBUG)
pri = QUARK_VL_DEBUG;
else
pri = QUARK_VL_WARN; /* fallback in case they add something new */
if (pri > quark_verbose)
return (0);
/* best effort in out of mem situations */
if (asprintf(&nfmt, "%s: %s", program_invocation_short_name, fmt) == -1)
vfprintf(stderr, fmt, ap);
else {
vfprintf(stderr, nfmt, ap);
free(nfmt);
}
return (0);
}
void
setup_libbpf_logs(void)
{
libbpf_set_print(libbpf_print_fn);
}