Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 1 | /* util.h |
| 2 | * Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | * |
| 6 | * Logging and other utility functions. |
| 7 | */ |
| 8 | |
| 9 | #ifndef _UTIL_H_ |
| 10 | #define _UTIL_H_ |
| 11 | |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 12 | #include <stdio.h> |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 13 | #include <stdlib.h> |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 14 | #include <sys/types.h> |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 15 | #include <syslog.h> |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 16 | #include <unistd.h> |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 17 | |
Jorge Lucangeli Obes | a67bd6a | 2016-08-19 15:33:48 -0400 | [diff] [blame] | 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
Luis Hector Chavez | daa0371 | 2017-09-06 08:10:33 -0700 | [diff] [blame] | 22 | #if defined(USE_EXIT_ON_DIE) |
| 23 | #define do_abort() exit(1) |
| 24 | #else |
| 25 | #define do_abort() abort() |
| 26 | #endif |
| 27 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 28 | /* clang-format off */ |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 29 | #define die(_msg, ...) do { \ |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 30 | do_log(LOG_ERR, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__); \ |
Luis Hector Chavez | daa0371 | 2017-09-06 08:10:33 -0700 | [diff] [blame] | 31 | do_abort(); \ |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 32 | } while (0) |
| 33 | |
| 34 | #define pdie(_msg, ...) \ |
Mike Frysinger | b5d7b9f | 2015-01-09 03:50:15 -0500 | [diff] [blame] | 35 | die(_msg ": %m", ## __VA_ARGS__) |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 36 | |
| 37 | #define warn(_msg, ...) \ |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 38 | do_log(LOG_WARNING, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__) |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 39 | |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 40 | #define pwarn(_msg, ...) \ |
| 41 | warn(_msg ": %m", ## __VA_ARGS__) |
| 42 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 43 | #define info(_msg, ...) \ |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 44 | do_log(LOG_INFO, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__) |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 45 | |
Mike Frysinger | 404d2bb | 2017-01-17 19:29:00 -0500 | [diff] [blame] | 46 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 47 | /* clang-format on */ |
Mike Frysinger | 404d2bb | 2017-01-17 19:29:00 -0500 | [diff] [blame] | 48 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 49 | extern const char *log_syscalls[]; |
| 50 | extern const size_t log_syscalls_len; |
| 51 | |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 52 | enum logging_system_t { |
| 53 | /* Log to syslog. This is the default. */ |
| 54 | LOG_TO_SYSLOG = 0, |
| 55 | |
| 56 | /* Log to a file descriptor. */ |
| 57 | LOG_TO_FD, |
| 58 | }; |
| 59 | |
| 60 | extern void do_log(int priority, const char *format, ...) |
| 61 | __attribute__((__format__(__printf__, 2, 3))); |
| 62 | |
Mike Frysinger | 8c01327 | 2017-09-06 19:26:46 -0400 | [diff] [blame] | 63 | static inline int is_android(void) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 64 | { |
Jorge Lucangeli Obes | 4b276a6 | 2016-01-07 14:31:33 -0800 | [diff] [blame] | 65 | #if defined(__ANDROID__) |
| 66 | return 1; |
| 67 | #else |
| 68 | return 0; |
| 69 | #endif |
| 70 | } |
| 71 | |
Evgenii Stepanov | 97ba2b2 | 2016-09-15 17:09:38 -0700 | [diff] [blame] | 72 | void __asan_init(void) __attribute__((weak)); |
Jorge Lucangeli Obes | 9e35c09 | 2016-04-11 13:30:08 -0700 | [diff] [blame] | 73 | |
Mike Frysinger | 8c01327 | 2017-09-06 19:26:46 -0400 | [diff] [blame] | 74 | static inline int running_with_asan(void) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 75 | { |
Evgenii Stepanov | 97ba2b2 | 2016-09-15 17:09:38 -0700 | [diff] [blame] | 76 | return &__asan_init != 0; |
Jorge Lucangeli Obes | 2413f37 | 2016-04-06 18:43:10 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 79 | int lookup_syscall(const char *name); |
| 80 | const char *lookup_syscall_name(int nr); |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 81 | |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 82 | long int parse_constant(char *constant_str, char **endptr); |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 83 | int parse_size(size_t *size, const char *sizespec); |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 84 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 85 | char *strip(char *s); |
Mike Frysinger | b4c7e77 | 2018-01-17 17:40:15 -0500 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * tokenize: locate the next token in @stringp using the @delim |
| 89 | * @stringp A pointer to the string to scan for tokens |
| 90 | * @delim The delimiter to split by |
| 91 | * |
| 92 | * Note that, unlike strtok, @delim is not a set of characters, but the full |
| 93 | * delimiter. e.g. "a,;b,;c" with a delim of ",;" will yield ["a","b","c"]. |
| 94 | * |
| 95 | * Note that, unlike strtok, this may return an empty token. e.g. "a,,b" with |
| 96 | * strtok will yield ["a","b"], but this will yield ["a","","b"]. |
| 97 | */ |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 98 | char *tokenize(char **stringp, const char *delim); |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 99 | |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 100 | char *path_join(const char *external_path, const char *internal_path); |
| 101 | |
| 102 | /* |
| 103 | * consumebytes: consumes @length bytes from a buffer @buf of length @buflength |
| 104 | * @length Number of bytes to consume |
| 105 | * @buf Buffer to consume from |
| 106 | * @buflength Size of @buf |
| 107 | * |
| 108 | * Returns a pointer to the base of the bytes, or NULL for errors. |
| 109 | */ |
| 110 | void *consumebytes(size_t length, char **buf, size_t *buflength); |
| 111 | |
| 112 | /* |
| 113 | * consumestr: consumes a C string from a buffer @buf of length @length |
| 114 | * @buf Buffer to consume |
| 115 | * @length Length of buffer |
| 116 | * |
| 117 | * Returns a pointer to the base of the string, or NULL for errors. |
| 118 | */ |
| 119 | char *consumestr(char **buf, size_t *buflength); |
| 120 | |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 121 | /* |
| 122 | * init_logging: initializes the module-wide logging. |
| 123 | * @logger The logging system to use. |
| 124 | * @fd The file descriptor to log into. Ignored unless |
| 125 | * @logger = LOG_TO_FD. |
| 126 | * @min_priority The minimum priority to display. Corresponds to syslog's |
| 127 | priority parameter. Ignored unless @logger = LOG_TO_FD. |
| 128 | */ |
| 129 | void init_logging(enum logging_system_t logger, int fd, int min_priority); |
| 130 | |
Jorge Lucangeli Obes | a67bd6a | 2016-08-19 15:33:48 -0400 | [diff] [blame] | 131 | #ifdef __cplusplus |
| 132 | }; /* extern "C" */ |
| 133 | #endif |
| 134 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 135 | #endif /* _UTIL_H_ */ |