blob: 14c028ad1800509a69c5bce80bcc2632d924e3d5 [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>
Luis Hector Chavez114a9302017-09-05 20:36:58 -070011#include <stdarg.h>
Martin Pelikánab9eb442017-01-25 11:53:58 +110012#include <stdint.h>
Luis Hector Chavez40b25742013-09-22 19:44:06 -070013#include <stdio.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070014#include <string.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070015
Luis Hector Chavez40b25742013-09-22 19:44:06 -070016#include "libconstants.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070017#include "libsyscalls.h"
18
Mike Frysingerfccb4c92013-10-19 02:42:07 -040019/*
20 * These are syscalls used by the syslog() C library call. You can find them
21 * by running a simple test program. See below for x86_64 behavior:
22 * $ cat test.c
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080023 * #include <syslog.h>
Mike Frysingerfccb4c92013-10-19 02:42:07 -040024 * main() { syslog(0, "foo"); }
25 * $ gcc test.c -static
26 * $ strace ./a.out
27 * ...
28 * socket(PF_FILE, SOCK_DGRAM|SOCK_CLOEXEC, 0) = 3 <- look for socket connection
29 * connect(...) <- important
30 * sendto(...) <- important
31 * exit_group(0) <- finish!
32 */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070033#if defined(__x86_64__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080034#if defined(__ANDROID__)
35const char *log_syscalls[] = {"socket", "connect", "fcntl", "writev"};
Alex Deymo7c6899c2016-01-27 18:24:19 -080036#else
Mike Frysinger09560862017-09-27 01:13:15 -040037const char *log_syscalls[] = {"socket", "connect", "sendto"};
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080038#endif
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070039#elif defined(__i386__)
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080040#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080041const char *log_syscalls[] = {"socketcall", "writev", "fcntl64",
42 "clock_gettime"};
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080043#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080044const char *log_syscalls[] = {"socketcall", "time"};
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080045#endif
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070046#elif defined(__arm__)
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080047#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080048const char *log_syscalls[] = {"clock_gettime", "connect", "fcntl64", "socket",
49 "writev"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080050#else
Mike Frysinger09560862017-09-27 01:13:15 -040051const char *log_syscalls[] = {"socket", "connect", "gettimeofday", "send"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080052#endif
53#elif defined(__aarch64__)
54#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080055const char *log_syscalls[] = {"connect", "fcntl", "sendto", "socket", "writev"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080056#else
Mike Frysinger09560862017-09-27 01:13:15 -040057const char *log_syscalls[] = {"socket", "connect", "send"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080058#endif
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080059#elif defined(__powerpc__) || defined(__ia64__) || defined(__hppa__) || \
60 defined(__sparc__) || defined(__mips__)
Mike Frysinger09560862017-09-27 01:13:15 -040061const char *log_syscalls[] = {"socket", "connect", "send"};
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070062#else
63#error "Unsupported platform"
64#endif
65
Mike Frysinger404d2bb2017-01-17 19:29:00 -050066const size_t log_syscalls_len = ARRAY_SIZE(log_syscalls);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070067
Luis Hector Chavez114a9302017-09-05 20:36:58 -070068/* clang-format off */
69static struct logging_config_t {
70 /* The logging system to use. The default is syslog. */
71 enum logging_system_t logger;
72
73 /* File descriptor to log to. Only used when logger is LOG_TO_FD. */
74 int fd;
75
76 /* Minimum priority to log. Only used when logger is LOG_TO_FD. */
77 int min_priority;
78} logging_config = {
79 .logger = LOG_TO_SYSLOG,
80};
81/* clang-format on */
82
83void do_log(int priority, const char *format, ...)
84{
85 if (logging_config.logger == LOG_TO_SYSLOG) {
86 va_list args;
87 va_start(args, format);
88 vsyslog(priority, format, args);
89 va_end(args);
90 return;
91 }
92
93 if (logging_config.min_priority < priority)
94 return;
95
96 va_list args;
97 va_start(args, format);
98 vdprintf(logging_config.fd, format, args);
99 va_end(args);
100 dprintf(logging_config.fd, "\n");
101}
102
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -0700103int lookup_syscall(const char *name)
104{
105 const struct syscall_entry *entry = syscall_table;
106 for (; entry->name && entry->nr >= 0; ++entry)
107 if (!strcmp(entry->name, name))
108 return entry->nr;
109 return -1;
110}
111
112const char *lookup_syscall_name(int nr)
113{
114 const struct syscall_entry *entry = syscall_table;
115 for (; entry->name && entry->nr >= 0; ++entry)
116 if (entry->nr == nr)
117 return entry->name;
118 return NULL;
119}
120
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400121long int parse_single_constant(char *constant_str, char **endptr)
122{
123 const struct constant_entry *entry = constant_table;
Jorge Lucangeli Obes07459372016-10-19 15:33:31 -0400124 long int res = 0;
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400125 for (; entry->name; ++entry) {
126 if (!strcmp(entry->name, constant_str)) {
127 if (endptr)
128 *endptr = constant_str + strlen(constant_str);
129
130 return entry->value;
131 }
132 }
133
Jorge Lucangeli Obes07459372016-10-19 15:33:31 -0400134 errno = 0;
135 res = strtol(constant_str, endptr, 0);
136 if (errno == ERANGE) {
137 if (res == LONG_MAX) {
138 /* See if the constant fits in an unsigned long int. */
139 errno = 0;
140 res = strtoul(constant_str, endptr, 0);
141 if (errno == ERANGE) {
142 /*
143 * On unsigned overflow, use the same convention
144 * as when strtol(3) finds no digits: set
145 * |*endptr| to |constant_str| and return 0.
146 */
147 warn("unsigned overflow: '%s'", constant_str);
148 *endptr = constant_str;
149 res = 0;
150 }
151 } else if (res == LONG_MIN) {
152 /*
153 * Same for signed underflow: set |*endptr| to
154 * |constant_str| and return 0.
155 */
156 warn("signed underflow: '%s'", constant_str);
157 *endptr = constant_str;
158 res = 0;
159 }
160 }
161 return res;
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400162}
163
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700164long int parse_constant(char *constant_str, char **endptr)
165{
Luis Hector Chavez21224552015-06-27 18:10:39 +0000166 long int value = 0;
167 char *group, *lastpos = constant_str;
168 char *original_constant_str = constant_str;
169
170 /*
171 * Try to parse constants separated by pipes. Note that since
172 * |constant_str| is an atom, there can be no spaces between the
173 * constant and the pipe. Constants can be either a named constant
Jorge Lucangeli Obesfd6f8e32016-10-12 11:19:28 -0400174 * defined in libconstants.gen.c or a number parsed with strtol(3).
Luis Hector Chavez21224552015-06-27 18:10:39 +0000175 *
176 * If there is an error parsing any of the constants, the whole process
177 * fails.
178 */
179 while ((group = tokenize(&constant_str, "|")) != NULL) {
180 char *end = group;
181 value |= parse_single_constant(group, &end);
182 if (end == group) {
183 lastpos = original_constant_str;
184 value = 0;
185 break;
186 }
187 lastpos = end;
188 }
189 if (endptr)
190 *endptr = lastpos;
191 return value;
192}
193
Martin Pelikánab9eb442017-01-25 11:53:58 +1100194/*
195 * parse_size, specified as a string with a decimal number in bytes,
196 * possibly with one 1-character suffix like "10K" or "6G".
197 * Assumes both pointers are non-NULL.
198 *
199 * Returns 0 on success, negative errno on failure.
200 * Only writes to result on success.
201 */
202int parse_size(size_t *result, const char *sizespec)
203{
204 const char prefixes[] = "KMGTPE";
205 size_t i, multiplier = 1, nsize, size = 0;
206 unsigned long long parsed;
207 const size_t len = strlen(sizespec);
208 char *end;
209
210 if (len == 0 || sizespec[0] == '-')
211 return -EINVAL;
212
213 for (i = 0; i < sizeof(prefixes); ++i) {
214 if (sizespec[len - 1] == prefixes[i]) {
215#if __WORDSIZE == 32
216 if (i >= 3)
217 return -ERANGE;
218#endif
219 multiplier = 1024;
220 while (i-- > 0)
221 multiplier *= 1024;
222 break;
223 }
224 }
225
226 /* We only need size_t but strtoul(3) is too small on IL32P64. */
227 parsed = strtoull(sizespec, &end, 10);
228 if (parsed == ULLONG_MAX)
229 return -errno;
230 if (parsed >= SIZE_MAX)
231 return -ERANGE;
232 if ((multiplier != 1 && end != sizespec + len - 1) ||
233 (multiplier == 1 && end != sizespec + len))
234 return -EINVAL;
235 size = (size_t)parsed;
236
237 nsize = size * multiplier;
238 if (nsize / multiplier != size)
239 return -ERANGE;
240 *result = nsize;
241 return 0;
242}
243
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -0700244char *strip(char *s)
245{
246 char *end;
247 while (*s && isblank(*s))
248 s++;
249 end = s + strlen(s) - 1;
250 while (end >= s && *end && (isblank(*end) || *end == '\n'))
251 end--;
252 *(end + 1) = '\0';
253 return s;
254}
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800255
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700256char *tokenize(char **stringp, const char *delim)
257{
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800258 char *ret = NULL;
259
260 /* If the string is NULL or empty, there are no tokens to be found. */
261 if (stringp == NULL || *stringp == NULL || **stringp == '\0')
262 return NULL;
263
264 /*
265 * If the delimiter is NULL or empty,
266 * the full string makes up the only token.
267 */
268 if (delim == NULL || *delim == '\0') {
269 ret = *stringp;
270 *stringp = NULL;
271 return ret;
272 }
273
274 char *found;
275 while (**stringp != '\0') {
276 found = strstr(*stringp, delim);
277
278 if (!found) {
279 /*
280 * The delimiter was not found, so the full string
281 * makes up the only token, and we're done.
282 */
283 ret = *stringp;
284 *stringp = NULL;
285 break;
286 }
287
288 if (found != *stringp) {
289 /* There's a non-empty token before the delimiter. */
290 *found = '\0';
291 ret = *stringp;
292 *stringp = found + strlen(delim);
293 break;
294 }
295
296 /*
297 * The delimiter was found at the start of the string,
298 * skip it and keep looking for a non-empty token.
299 */
300 *stringp += strlen(delim);
301 }
302
303 return ret;
304}
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400305
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400306char *path_join(const char *external_path, const char *internal_path)
307{
308 char *path;
309 size_t pathlen;
310
311 /* One extra char for '/' and one for '\0', hence + 2. */
312 pathlen = strlen(external_path) + strlen(internal_path) + 2;
313 path = malloc(pathlen);
314 snprintf(path, pathlen, "%s/%s", external_path, internal_path);
315
316 return path;
317}
318
319void *consumebytes(size_t length, char **buf, size_t *buflength)
320{
321 char *p = *buf;
322 if (length > *buflength)
323 return NULL;
324 *buf += length;
325 *buflength -= length;
326 return p;
327}
328
329char *consumestr(char **buf, size_t *buflength)
330{
331 size_t len = strnlen(*buf, *buflength);
332 if (len == *buflength)
333 /* There's no null-terminator. */
334 return NULL;
335 return consumebytes(len + 1, buf, buflength);
336}
Luis Hector Chavez114a9302017-09-05 20:36:58 -0700337
338void init_logging(enum logging_system_t logger, int fd, int min_priority)
339{
340 logging_config.logger = logger;
341 logging_config.fd = fd;
342 logging_config.min_priority = min_priority;
343}