blob: a96c71f4ec0c278056721c375b73f3e5b818e365 [file] [log] [blame]
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -07001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6#include <ctype.h>
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04007#include <errno.h>
8#include <fcntl.h>
Jorge Lucangeli Obes07459372016-10-19 15:33:31 -04009#include <limits.h>
Martin Pelikánab9eb442017-01-25 11:53:58 +110010#include <stdint.h>
Luis Hector Chavez40b25742013-09-22 19:44:06 -070011#include <stdio.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070012#include <string.h>
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -040013#include <sys/stat.h>
14#include <sys/types.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070015
16#include "util.h"
17
Luis Hector Chavez40b25742013-09-22 19:44:06 -070018#include "libconstants.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070019#include "libsyscalls.h"
20
Mike Frysingerfccb4c92013-10-19 02:42:07 -040021/*
22 * These are syscalls used by the syslog() C library call. You can find them
23 * by running a simple test program. See below for x86_64 behavior:
24 * $ cat test.c
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080025 * #include <syslog.h>
Mike Frysingerfccb4c92013-10-19 02:42:07 -040026 * main() { syslog(0, "foo"); }
27 * $ gcc test.c -static
28 * $ strace ./a.out
29 * ...
30 * socket(PF_FILE, SOCK_DGRAM|SOCK_CLOEXEC, 0) = 3 <- look for socket connection
31 * connect(...) <- important
32 * sendto(...) <- important
33 * exit_group(0) <- finish!
34 */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070035#if defined(__x86_64__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080036#if defined(__ANDROID__)
37const char *log_syscalls[] = {"socket", "connect", "fcntl", "writev"};
Alex Deymo7c6899c2016-01-27 18:24:19 -080038#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080039const char *log_syscalls[] = {"connect", "sendto"};
40#endif
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070041#elif defined(__i386__)
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080042#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080043const char *log_syscalls[] = {"socketcall", "writev", "fcntl64",
44 "clock_gettime"};
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080045#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080046const char *log_syscalls[] = {"socketcall", "time"};
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080047#endif
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070048#elif defined(__arm__)
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080049#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080050const char *log_syscalls[] = {"clock_gettime", "connect", "fcntl64", "socket",
51 "writev"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080052#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080053const char *log_syscalls[] = {"connect", "gettimeofday", "send"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080054#endif
55#elif defined(__aarch64__)
56#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080057const char *log_syscalls[] = {"connect", "fcntl", "sendto", "socket", "writev"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080058#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080059const char *log_syscalls[] = {"connect", "send"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080060#endif
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080061#elif defined(__powerpc__) || defined(__ia64__) || defined(__hppa__) || \
62 defined(__sparc__) || defined(__mips__)
63const char *log_syscalls[] = {"connect", "send"};
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070064#else
65#error "Unsupported platform"
66#endif
67
Mike Frysinger404d2bb2017-01-17 19:29:00 -050068const size_t log_syscalls_len = ARRAY_SIZE(log_syscalls);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070069
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070070int lookup_syscall(const char *name)
71{
72 const struct syscall_entry *entry = syscall_table;
73 for (; entry->name && entry->nr >= 0; ++entry)
74 if (!strcmp(entry->name, name))
75 return entry->nr;
76 return -1;
77}
78
79const char *lookup_syscall_name(int nr)
80{
81 const struct syscall_entry *entry = syscall_table;
82 for (; entry->name && entry->nr >= 0; ++entry)
83 if (entry->nr == nr)
84 return entry->name;
85 return NULL;
86}
87
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040088long int parse_single_constant(char *constant_str, char **endptr)
89{
90 const struct constant_entry *entry = constant_table;
Jorge Lucangeli Obes07459372016-10-19 15:33:31 -040091 long int res = 0;
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040092 for (; entry->name; ++entry) {
93 if (!strcmp(entry->name, constant_str)) {
94 if (endptr)
95 *endptr = constant_str + strlen(constant_str);
96
97 return entry->value;
98 }
99 }
100
Jorge Lucangeli Obes07459372016-10-19 15:33:31 -0400101 errno = 0;
102 res = strtol(constant_str, endptr, 0);
103 if (errno == ERANGE) {
104 if (res == LONG_MAX) {
105 /* See if the constant fits in an unsigned long int. */
106 errno = 0;
107 res = strtoul(constant_str, endptr, 0);
108 if (errno == ERANGE) {
109 /*
110 * On unsigned overflow, use the same convention
111 * as when strtol(3) finds no digits: set
112 * |*endptr| to |constant_str| and return 0.
113 */
114 warn("unsigned overflow: '%s'", constant_str);
115 *endptr = constant_str;
116 res = 0;
117 }
118 } else if (res == LONG_MIN) {
119 /*
120 * Same for signed underflow: set |*endptr| to
121 * |constant_str| and return 0.
122 */
123 warn("signed underflow: '%s'", constant_str);
124 *endptr = constant_str;
125 res = 0;
126 }
127 }
128 return res;
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400129}
130
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700131long int parse_constant(char *constant_str, char **endptr)
132{
Luis Hector Chavez21224552015-06-27 18:10:39 +0000133 long int value = 0;
134 char *group, *lastpos = constant_str;
135 char *original_constant_str = constant_str;
136
137 /*
138 * Try to parse constants separated by pipes. Note that since
139 * |constant_str| is an atom, there can be no spaces between the
140 * constant and the pipe. Constants can be either a named constant
Jorge Lucangeli Obesfd6f8e32016-10-12 11:19:28 -0400141 * defined in libconstants.gen.c or a number parsed with strtol(3).
Luis Hector Chavez21224552015-06-27 18:10:39 +0000142 *
143 * If there is an error parsing any of the constants, the whole process
144 * fails.
145 */
146 while ((group = tokenize(&constant_str, "|")) != NULL) {
147 char *end = group;
148 value |= parse_single_constant(group, &end);
149 if (end == group) {
150 lastpos = original_constant_str;
151 value = 0;
152 break;
153 }
154 lastpos = end;
155 }
156 if (endptr)
157 *endptr = lastpos;
158 return value;
159}
160
Martin Pelikánab9eb442017-01-25 11:53:58 +1100161/*
162 * parse_size, specified as a string with a decimal number in bytes,
163 * possibly with one 1-character suffix like "10K" or "6G".
164 * Assumes both pointers are non-NULL.
165 *
166 * Returns 0 on success, negative errno on failure.
167 * Only writes to result on success.
168 */
169int parse_size(size_t *result, const char *sizespec)
170{
171 const char prefixes[] = "KMGTPE";
172 size_t i, multiplier = 1, nsize, size = 0;
173 unsigned long long parsed;
174 const size_t len = strlen(sizespec);
175 char *end;
176
177 if (len == 0 || sizespec[0] == '-')
178 return -EINVAL;
179
180 for (i = 0; i < sizeof(prefixes); ++i) {
181 if (sizespec[len - 1] == prefixes[i]) {
182#if __WORDSIZE == 32
183 if (i >= 3)
184 return -ERANGE;
185#endif
186 multiplier = 1024;
187 while (i-- > 0)
188 multiplier *= 1024;
189 break;
190 }
191 }
192
193 /* We only need size_t but strtoul(3) is too small on IL32P64. */
194 parsed = strtoull(sizespec, &end, 10);
195 if (parsed == ULLONG_MAX)
196 return -errno;
197 if (parsed >= SIZE_MAX)
198 return -ERANGE;
199 if ((multiplier != 1 && end != sizespec + len - 1) ||
200 (multiplier == 1 && end != sizespec + len))
201 return -EINVAL;
202 size = (size_t)parsed;
203
204 nsize = size * multiplier;
205 if (nsize / multiplier != size)
206 return -ERANGE;
207 *result = nsize;
208 return 0;
209}
210
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -0700211char *strip(char *s)
212{
213 char *end;
214 while (*s && isblank(*s))
215 s++;
216 end = s + strlen(s) - 1;
217 while (end >= s && *end && (isblank(*end) || *end == '\n'))
218 end--;
219 *(end + 1) = '\0';
220 return s;
221}
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800222
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700223char *tokenize(char **stringp, const char *delim)
224{
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800225 char *ret = NULL;
226
227 /* If the string is NULL or empty, there are no tokens to be found. */
228 if (stringp == NULL || *stringp == NULL || **stringp == '\0')
229 return NULL;
230
231 /*
232 * If the delimiter is NULL or empty,
233 * the full string makes up the only token.
234 */
235 if (delim == NULL || *delim == '\0') {
236 ret = *stringp;
237 *stringp = NULL;
238 return ret;
239 }
240
241 char *found;
242 while (**stringp != '\0') {
243 found = strstr(*stringp, delim);
244
245 if (!found) {
246 /*
247 * The delimiter was not found, so the full string
248 * makes up the only token, and we're done.
249 */
250 ret = *stringp;
251 *stringp = NULL;
252 break;
253 }
254
255 if (found != *stringp) {
256 /* There's a non-empty token before the delimiter. */
257 *found = '\0';
258 ret = *stringp;
259 *stringp = found + strlen(delim);
260 break;
261 }
262
263 /*
264 * The delimiter was found at the start of the string,
265 * skip it and keep looking for a non-empty token.
266 */
267 *stringp += strlen(delim);
268 }
269
270 return ret;
271}
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400272
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400273char *path_join(const char *external_path, const char *internal_path)
274{
275 char *path;
276 size_t pathlen;
277
278 /* One extra char for '/' and one for '\0', hence + 2. */
279 pathlen = strlen(external_path) + strlen(internal_path) + 2;
280 path = malloc(pathlen);
281 snprintf(path, pathlen, "%s/%s", external_path, internal_path);
282
283 return path;
284}
285
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -0400286int write_proc_file(pid_t pid, const char *content, const char *basename)
287{
288 int fd, ret;
289 size_t sz, len;
290 ssize_t written;
291 char filename[32];
292
293 sz = sizeof(filename);
294 ret = snprintf(filename, sz, "/proc/%d/%s", pid, basename);
295 if (ret < 0 || (size_t)ret >= sz) {
296 warn("failed to generate %s filename", basename);
297 return -1;
298 }
299
300 fd = open(filename, O_WRONLY | O_CLOEXEC);
301 if (fd < 0) {
302 pwarn("failed to open '%s'", filename);
303 return -errno;
304 }
305
306 len = strlen(content);
307 written = write(fd, content, len);
308 if (written < 0) {
309 pwarn("failed to write '%s'", filename);
310 return -1;
311 }
312
313 if ((size_t)written < len) {
314 warn("failed to write %zu bytes to '%s'", len, filename);
315 return -1;
316 }
317 close(fd);
318 return 0;
319}
320
321int write_pid_to_path(pid_t pid, const char *path)
322{
323 FILE *fp = fopen(path, "w");
324
325 if (!fp) {
326 pwarn("failed to open '%s'", path);
327 return -errno;
328 }
329 if (fprintf(fp, "%d\n", (int)pid) < 0) {
330 /* fprintf(3) does not set errno on failure. */
331 warn("fprintf(%s) failed", path);
332 return -1;
333 }
334 if (fclose(fp)) {
335 pwarn("fclose(%s) failed", path);
336 return -errno;
337 }
338
339 return 0;
340}
341
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400342void *consumebytes(size_t length, char **buf, size_t *buflength)
343{
344 char *p = *buf;
345 if (length > *buflength)
346 return NULL;
347 *buf += length;
348 *buflength -= length;
349 return p;
350}
351
352char *consumestr(char **buf, size_t *buflength)
353{
354 size_t len = strnlen(*buf, *buflength);
355 if (len == *buflength)
356 /* There's no null-terminator. */
357 return NULL;
358 return consumebytes(len + 1, buf, buflength);
359}