-
Notifications
You must be signed in to change notification settings - Fork 2
/
qutil.c
349 lines (296 loc) · 5.77 KB
/
qutil.c
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
// SPDX-License-Identifier: Apache-2.0
/* Copyright (c) 2024 Elastic NV */
#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 "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 strlen(pathname) so
* caller can check truncation, like strlcpy(). Compare to >= 0 if truncation is
* acceptable.
*/
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;
return (strlen(pathname));
}
void
qstr_init(struct qstr *qstr)
{
qstr->p = qstr->small;
}
int
qstr_ensure(struct qstr *qstr, size_t n)
{
if (n > sizeof(qstr->small)) {
qstr->p = malloc(n);
if (qstr->p == NULL)
return (-1);
}
return (0);
}
int
qstr_memcpy(struct qstr *qstr, const void *src, size_t len)
{
if (qstr_ensure(qstr, len) == -1)
return (-1);
memcpy(qstr->p, src, len);
return (0);
}
int
qstr_strcpy(struct qstr *qstr, const char *src)
{
return (qstr_memcpy(qstr, src, strlen(src) + 1));
}
void
qstr_free(struct qstr *qstr)
{
if (qstr->p == qstr->small)
return;
free(qstr->p);
}
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);
rewind(f);
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);
}
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);
}
/* buf_len includes the terminating NUL */
struct args *
args_make(const struct quark_process *qev)
{
struct args *args;
const char *p, *end;
int i, argc;
const char *buf;
size_t buf_len;
buf = qev->cmdline;
buf_len = qev->cmdline_len;
if (buf_len == 0 || buf[buf_len - 1] != 0)
return (NULL);
/* Walk source and count how many arguments */
argc = 0;
end = buf + buf_len;
for (p = buf; p < end; p += strlen(p) + 1)
argc++;
/*
* Allocate with variadic end
*/
if ((args = calloc(1, sizeof(*args) + (argc * sizeof(char *)))) == NULL)
goto fail;
if ((args->buf = malloc(buf_len)) == NULL)
goto fail;
memcpy(args->buf, buf, buf_len);
args->buf_len = buf_len;
/*
* This is a bit paranoic, we recheck what we already know.
*/
i = 0;
end = args->buf + args->buf_len;
for (p = args->buf;
p < end && i < argc && *p != 0;
p += strlen(p) + 1) {
args->argv[i++] = p;
}
args->argc = i;
return (args);
fail:
if (args != NULL)
free(args->buf);
free(args);
return (NULL);
}
void
args_free(struct args *args) {
free(args->buf);
free(args);
}
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);
}