Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 1 | /* 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 Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 6 | #include "util.h" |
| 7 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 8 | #include <ctype.h> |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 9 | #include <errno.h> |
Jorge Lucangeli Obes | 0745937 | 2016-10-19 15:33:31 -0400 | [diff] [blame] | 10 | #include <limits.h> |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 11 | #include <stdarg.h> |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 12 | #include <stdint.h> |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 13 | #include <stdio.h> |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 14 | #include <string.h> |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 15 | |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 16 | #include "libconstants.h" |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 17 | #include "libsyscalls.h" |
| 18 | |
Mike Frysinger | fccb4c9 | 2013-10-19 02:42:07 -0400 | [diff] [blame] | 19 | /* |
| 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 Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 23 | * #include <syslog.h> |
Mike Frysinger | fccb4c9 | 2013-10-19 02:42:07 -0400 | [diff] [blame] | 24 | * 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 Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 33 | #if defined(__x86_64__) |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 34 | #if defined(__ANDROID__) |
| 35 | const char *log_syscalls[] = {"socket", "connect", "fcntl", "writev"}; |
Alex Deymo | 7c6899c | 2016-01-27 18:24:19 -0800 | [diff] [blame] | 36 | #else |
Mike Frysinger | 0956086 | 2017-09-27 01:13:15 -0400 | [diff] [blame] | 37 | const char *log_syscalls[] = {"socket", "connect", "sendto"}; |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 38 | #endif |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 39 | #elif defined(__i386__) |
Jeff Vander Stoep | 1482f20 | 2016-01-07 11:21:31 -0800 | [diff] [blame] | 40 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 41 | const char *log_syscalls[] = {"socketcall", "writev", "fcntl64", |
| 42 | "clock_gettime"}; |
Jeff Vander Stoep | 1482f20 | 2016-01-07 11:21:31 -0800 | [diff] [blame] | 43 | #else |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 44 | const char *log_syscalls[] = {"socketcall", "time"}; |
Jeff Vander Stoep | 1482f20 | 2016-01-07 11:21:31 -0800 | [diff] [blame] | 45 | #endif |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 46 | #elif defined(__arm__) |
Jeff Vander Stoep | d38f399 | 2015-12-02 10:54:51 -0800 | [diff] [blame] | 47 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 48 | const char *log_syscalls[] = {"clock_gettime", "connect", "fcntl64", "socket", |
| 49 | "writev"}; |
Jeff Vander Stoep | d38f399 | 2015-12-02 10:54:51 -0800 | [diff] [blame] | 50 | #else |
Mike Frysinger | 0956086 | 2017-09-27 01:13:15 -0400 | [diff] [blame] | 51 | const char *log_syscalls[] = {"socket", "connect", "gettimeofday", "send"}; |
Jeff Vander Stoep | d38f399 | 2015-12-02 10:54:51 -0800 | [diff] [blame] | 52 | #endif |
| 53 | #elif defined(__aarch64__) |
| 54 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 55 | const char *log_syscalls[] = {"connect", "fcntl", "sendto", "socket", "writev"}; |
Jeff Vander Stoep | d38f399 | 2015-12-02 10:54:51 -0800 | [diff] [blame] | 56 | #else |
Mike Frysinger | 0956086 | 2017-09-27 01:13:15 -0400 | [diff] [blame] | 57 | const char *log_syscalls[] = {"socket", "connect", "send"}; |
Jeff Vander Stoep | d38f399 | 2015-12-02 10:54:51 -0800 | [diff] [blame] | 58 | #endif |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 59 | #elif defined(__powerpc__) || defined(__ia64__) || defined(__hppa__) || \ |
| 60 | defined(__sparc__) || defined(__mips__) |
Mike Frysinger | 0956086 | 2017-09-27 01:13:15 -0400 | [diff] [blame] | 61 | const char *log_syscalls[] = {"socket", "connect", "send"}; |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 62 | #else |
| 63 | #error "Unsupported platform" |
| 64 | #endif |
| 65 | |
Mike Frysinger | 404d2bb | 2017-01-17 19:29:00 -0500 | [diff] [blame] | 66 | const size_t log_syscalls_len = ARRAY_SIZE(log_syscalls); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 67 | |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 68 | /* clang-format off */ |
| 69 | static 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 | |
| 83 | void 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 Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 103 | int 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 | |
| 112 | const 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 Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 121 | long int parse_single_constant(char *constant_str, char **endptr) |
| 122 | { |
| 123 | const struct constant_entry *entry = constant_table; |
Jorge Lucangeli Obes | 0745937 | 2016-10-19 15:33:31 -0400 | [diff] [blame] | 124 | long int res = 0; |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 125 | 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 Obes | 0745937 | 2016-10-19 15:33:31 -0400 | [diff] [blame] | 134 | 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 Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 164 | long int parse_constant(char *constant_str, char **endptr) |
| 165 | { |
Luis Hector Chavez | 2122455 | 2015-06-27 18:10:39 +0000 | [diff] [blame] | 166 | 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 Obes | fd6f8e3 | 2016-10-12 11:19:28 -0400 | [diff] [blame] | 174 | * defined in libconstants.gen.c or a number parsed with strtol(3). |
Luis Hector Chavez | 2122455 | 2015-06-27 18:10:39 +0000 | [diff] [blame] | 175 | * |
| 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án | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 194 | /* |
| 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 | */ |
| 202 | int 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 Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 244 | char *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 Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 255 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 256 | char *tokenize(char **stringp, const char *delim) |
| 257 | { |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 258 | 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 Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 305 | |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 306 | char *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 | |
| 319 | void *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 | |
| 329 | char *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 Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 337 | |
| 338 | void 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 | } |