blob: 2605a3fb2e5e9838c4490b8b0d7075811a9e5073 [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
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04006#include "util.h"
7
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -07008#include <ctype.h>
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04009#include <errno.h>
Jorge Lucangeli Obes07459372016-10-19 15:33:31 -040010#include <limits.h>
Martin Pelikánab9eb442017-01-25 11:53:58 +110011#include <stdint.h>
Luis Hector Chavez40b25742013-09-22 19:44:06 -070012#include <stdio.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070013#include <string.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070014
Luis Hector Chavez40b25742013-09-22 19:44:06 -070015#include "libconstants.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070016#include "libsyscalls.h"
17
Mike Frysingerfccb4c92013-10-19 02:42:07 -040018/*
19 * These are syscalls used by the syslog() C library call. You can find them
20 * by running a simple test program. See below for x86_64 behavior:
21 * $ cat test.c
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080022 * #include <syslog.h>
Mike Frysingerfccb4c92013-10-19 02:42:07 -040023 * main() { syslog(0, "foo"); }
24 * $ gcc test.c -static
25 * $ strace ./a.out
26 * ...
27 * socket(PF_FILE, SOCK_DGRAM|SOCK_CLOEXEC, 0) = 3 <- look for socket connection
28 * connect(...) <- important
29 * sendto(...) <- important
30 * exit_group(0) <- finish!
31 */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070032#if defined(__x86_64__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080033#if defined(__ANDROID__)
34const char *log_syscalls[] = {"socket", "connect", "fcntl", "writev"};
Alex Deymo7c6899c2016-01-27 18:24:19 -080035#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080036const char *log_syscalls[] = {"connect", "sendto"};
37#endif
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070038#elif defined(__i386__)
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080039#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080040const char *log_syscalls[] = {"socketcall", "writev", "fcntl64",
41 "clock_gettime"};
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080042#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080043const char *log_syscalls[] = {"socketcall", "time"};
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080044#endif
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070045#elif defined(__arm__)
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080046#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080047const char *log_syscalls[] = {"clock_gettime", "connect", "fcntl64", "socket",
48 "writev"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080049#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080050const char *log_syscalls[] = {"connect", "gettimeofday", "send"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080051#endif
52#elif defined(__aarch64__)
53#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080054const char *log_syscalls[] = {"connect", "fcntl", "sendto", "socket", "writev"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080055#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080056const char *log_syscalls[] = {"connect", "send"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080057#endif
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080058#elif defined(__powerpc__) || defined(__ia64__) || defined(__hppa__) || \
59 defined(__sparc__) || defined(__mips__)
60const char *log_syscalls[] = {"connect", "send"};
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070061#else
62#error "Unsupported platform"
63#endif
64
Mike Frysinger404d2bb2017-01-17 19:29:00 -050065const size_t log_syscalls_len = ARRAY_SIZE(log_syscalls);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070066
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070067int lookup_syscall(const char *name)
68{
69 const struct syscall_entry *entry = syscall_table;
70 for (; entry->name && entry->nr >= 0; ++entry)
71 if (!strcmp(entry->name, name))
72 return entry->nr;
73 return -1;
74}
75
76const char *lookup_syscall_name(int nr)
77{
78 const struct syscall_entry *entry = syscall_table;
79 for (; entry->name && entry->nr >= 0; ++entry)
80 if (entry->nr == nr)
81 return entry->name;
82 return NULL;
83}
84
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040085long int parse_single_constant(char *constant_str, char **endptr)
86{
87 const struct constant_entry *entry = constant_table;
Jorge Lucangeli Obes07459372016-10-19 15:33:31 -040088 long int res = 0;
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040089 for (; entry->name; ++entry) {
90 if (!strcmp(entry->name, constant_str)) {
91 if (endptr)
92 *endptr = constant_str + strlen(constant_str);
93
94 return entry->value;
95 }
96 }
97
Jorge Lucangeli Obes07459372016-10-19 15:33:31 -040098 errno = 0;
99 res = strtol(constant_str, endptr, 0);
100 if (errno == ERANGE) {
101 if (res == LONG_MAX) {
102 /* See if the constant fits in an unsigned long int. */
103 errno = 0;
104 res = strtoul(constant_str, endptr, 0);
105 if (errno == ERANGE) {
106 /*
107 * On unsigned overflow, use the same convention
108 * as when strtol(3) finds no digits: set
109 * |*endptr| to |constant_str| and return 0.
110 */
111 warn("unsigned overflow: '%s'", constant_str);
112 *endptr = constant_str;
113 res = 0;
114 }
115 } else if (res == LONG_MIN) {
116 /*
117 * Same for signed underflow: set |*endptr| to
118 * |constant_str| and return 0.
119 */
120 warn("signed underflow: '%s'", constant_str);
121 *endptr = constant_str;
122 res = 0;
123 }
124 }
125 return res;
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400126}
127
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700128long int parse_constant(char *constant_str, char **endptr)
129{
Luis Hector Chavez21224552015-06-27 18:10:39 +0000130 long int value = 0;
131 char *group, *lastpos = constant_str;
132 char *original_constant_str = constant_str;
133
134 /*
135 * Try to parse constants separated by pipes. Note that since
136 * |constant_str| is an atom, there can be no spaces between the
137 * constant and the pipe. Constants can be either a named constant
Jorge Lucangeli Obesfd6f8e32016-10-12 11:19:28 -0400138 * defined in libconstants.gen.c or a number parsed with strtol(3).
Luis Hector Chavez21224552015-06-27 18:10:39 +0000139 *
140 * If there is an error parsing any of the constants, the whole process
141 * fails.
142 */
143 while ((group = tokenize(&constant_str, "|")) != NULL) {
144 char *end = group;
145 value |= parse_single_constant(group, &end);
146 if (end == group) {
147 lastpos = original_constant_str;
148 value = 0;
149 break;
150 }
151 lastpos = end;
152 }
153 if (endptr)
154 *endptr = lastpos;
155 return value;
156}
157
Martin Pelikánab9eb442017-01-25 11:53:58 +1100158/*
159 * parse_size, specified as a string with a decimal number in bytes,
160 * possibly with one 1-character suffix like "10K" or "6G".
161 * Assumes both pointers are non-NULL.
162 *
163 * Returns 0 on success, negative errno on failure.
164 * Only writes to result on success.
165 */
166int parse_size(size_t *result, const char *sizespec)
167{
168 const char prefixes[] = "KMGTPE";
169 size_t i, multiplier = 1, nsize, size = 0;
170 unsigned long long parsed;
171 const size_t len = strlen(sizespec);
172 char *end;
173
174 if (len == 0 || sizespec[0] == '-')
175 return -EINVAL;
176
177 for (i = 0; i < sizeof(prefixes); ++i) {
178 if (sizespec[len - 1] == prefixes[i]) {
179#if __WORDSIZE == 32
180 if (i >= 3)
181 return -ERANGE;
182#endif
183 multiplier = 1024;
184 while (i-- > 0)
185 multiplier *= 1024;
186 break;
187 }
188 }
189
190 /* We only need size_t but strtoul(3) is too small on IL32P64. */
191 parsed = strtoull(sizespec, &end, 10);
192 if (parsed == ULLONG_MAX)
193 return -errno;
194 if (parsed >= SIZE_MAX)
195 return -ERANGE;
196 if ((multiplier != 1 && end != sizespec + len - 1) ||
197 (multiplier == 1 && end != sizespec + len))
198 return -EINVAL;
199 size = (size_t)parsed;
200
201 nsize = size * multiplier;
202 if (nsize / multiplier != size)
203 return -ERANGE;
204 *result = nsize;
205 return 0;
206}
207
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -0700208char *strip(char *s)
209{
210 char *end;
211 while (*s && isblank(*s))
212 s++;
213 end = s + strlen(s) - 1;
214 while (end >= s && *end && (isblank(*end) || *end == '\n'))
215 end--;
216 *(end + 1) = '\0';
217 return s;
218}
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800219
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700220char *tokenize(char **stringp, const char *delim)
221{
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800222 char *ret = NULL;
223
224 /* If the string is NULL or empty, there are no tokens to be found. */
225 if (stringp == NULL || *stringp == NULL || **stringp == '\0')
226 return NULL;
227
228 /*
229 * If the delimiter is NULL or empty,
230 * the full string makes up the only token.
231 */
232 if (delim == NULL || *delim == '\0') {
233 ret = *stringp;
234 *stringp = NULL;
235 return ret;
236 }
237
238 char *found;
239 while (**stringp != '\0') {
240 found = strstr(*stringp, delim);
241
242 if (!found) {
243 /*
244 * The delimiter was not found, so the full string
245 * makes up the only token, and we're done.
246 */
247 ret = *stringp;
248 *stringp = NULL;
249 break;
250 }
251
252 if (found != *stringp) {
253 /* There's a non-empty token before the delimiter. */
254 *found = '\0';
255 ret = *stringp;
256 *stringp = found + strlen(delim);
257 break;
258 }
259
260 /*
261 * The delimiter was found at the start of the string,
262 * skip it and keep looking for a non-empty token.
263 */
264 *stringp += strlen(delim);
265 }
266
267 return ret;
268}
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400269
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400270char *path_join(const char *external_path, const char *internal_path)
271{
272 char *path;
273 size_t pathlen;
274
275 /* One extra char for '/' and one for '\0', hence + 2. */
276 pathlen = strlen(external_path) + strlen(internal_path) + 2;
277 path = malloc(pathlen);
278 snprintf(path, pathlen, "%s/%s", external_path, internal_path);
279
280 return path;
281}
282
283void *consumebytes(size_t length, char **buf, size_t *buflength)
284{
285 char *p = *buf;
286 if (length > *buflength)
287 return NULL;
288 *buf += length;
289 *buflength -= length;
290 return p;
291}
292
293char *consumestr(char **buf, size_t *buflength)
294{
295 size_t len = strnlen(*buf, *buflength);
296 if (len == *buflength)
297 /* There's no null-terminator. */
298 return NULL;
299 return consumebytes(len + 1, buf, buflength);
300}