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 | |
Mike Frysinger | d9ef07c | 2018-05-30 16:51:36 -0400 | [diff] [blame^] | 22 | /* |
| 23 | * Silence compiler warnings for unused variables/functions. |
| 24 | * |
| 25 | * If the definition is actually used, the attribute should be removed, but if |
| 26 | * it's forgotten or left in place, it doesn't cause a problem. |
| 27 | * |
| 28 | * If the definition is actually unused, the compiler is free to remove it from |
| 29 | * the output so as to save size. If you want to make sure the definition is |
| 30 | * kept (e.g. for ABI compatibility), look at the "used" attribute instead. |
| 31 | */ |
| 32 | #define attribute_unused __attribute__((__unused__)) |
| 33 | |
| 34 | /* |
| 35 | * Mark the symbol as "weak" in the ELF output. This provides a fallback symbol |
| 36 | * that may be overriden at link time. See this page for more details: |
| 37 | * https://en.wikipedia.org/wiki/Weak_symbol |
| 38 | */ |
| 39 | #define attribute_weak __attribute__((__weak__)) |
| 40 | |
| 41 | /* |
| 42 | * Mark the function as a printf-style function. |
| 43 | * @format_idx The index in the function argument list where the format string |
| 44 | * is passed (where the first argument is "1"). |
| 45 | * @check_idx The index in the function argument list where the first argument |
| 46 | * used in the format string is passed. |
| 47 | * Some examples: |
| 48 | * foo([1] const char *format, [2] ...): format=1 check=2 |
| 49 | * foo([1] int, [2] const char *format, [3] ...): format=2 check=3 |
| 50 | * foo([1] const char *format, [2] const char *, [3] ...): format=1 check=3 |
| 51 | */ |
| 52 | #define attribute_printf(format_idx, check_idx) \ |
| 53 | __attribute__((__format__(__printf__, format_idx, check_idx))) |
| 54 | |
Luis Hector Chavez | daa0371 | 2017-09-06 08:10:33 -0700 | [diff] [blame] | 55 | #if defined(USE_EXIT_ON_DIE) |
| 56 | #define do_abort() exit(1) |
| 57 | #else |
| 58 | #define do_abort() abort() |
| 59 | #endif |
| 60 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 61 | /* clang-format off */ |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 62 | #define die(_msg, ...) do { \ |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 63 | do_log(LOG_ERR, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__); \ |
Luis Hector Chavez | daa0371 | 2017-09-06 08:10:33 -0700 | [diff] [blame] | 64 | do_abort(); \ |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 65 | } while (0) |
| 66 | |
| 67 | #define pdie(_msg, ...) \ |
Mike Frysinger | b5d7b9f | 2015-01-09 03:50:15 -0500 | [diff] [blame] | 68 | die(_msg ": %m", ## __VA_ARGS__) |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 69 | |
| 70 | #define warn(_msg, ...) \ |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 71 | do_log(LOG_WARNING, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__) |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 72 | |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 73 | #define pwarn(_msg, ...) \ |
| 74 | warn(_msg ": %m", ## __VA_ARGS__) |
| 75 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 76 | #define info(_msg, ...) \ |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 77 | do_log(LOG_INFO, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__) |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 78 | |
Mike Frysinger | 404d2bb | 2017-01-17 19:29:00 -0500 | [diff] [blame] | 79 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 80 | /* clang-format on */ |
Mike Frysinger | 404d2bb | 2017-01-17 19:29:00 -0500 | [diff] [blame] | 81 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 82 | extern const char *log_syscalls[]; |
| 83 | extern const size_t log_syscalls_len; |
| 84 | |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 85 | enum logging_system_t { |
| 86 | /* Log to syslog. This is the default. */ |
| 87 | LOG_TO_SYSLOG = 0, |
| 88 | |
| 89 | /* Log to a file descriptor. */ |
| 90 | LOG_TO_FD, |
| 91 | }; |
| 92 | |
| 93 | extern void do_log(int priority, const char *format, ...) |
Mike Frysinger | d9ef07c | 2018-05-30 16:51:36 -0400 | [diff] [blame^] | 94 | attribute_printf(2, 3); |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 95 | |
Mike Frysinger | 8c01327 | 2017-09-06 19:26:46 -0400 | [diff] [blame] | 96 | static inline int is_android(void) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 97 | { |
Jorge Lucangeli Obes | 4b276a6 | 2016-01-07 14:31:33 -0800 | [diff] [blame] | 98 | #if defined(__ANDROID__) |
| 99 | return 1; |
| 100 | #else |
| 101 | return 0; |
| 102 | #endif |
| 103 | } |
| 104 | |
Mike Frysinger | d9ef07c | 2018-05-30 16:51:36 -0400 | [diff] [blame^] | 105 | void __asan_init(void) attribute_weak; |
Jorge Lucangeli Obes | 9e35c09 | 2016-04-11 13:30:08 -0700 | [diff] [blame] | 106 | |
Mike Frysinger | 8c01327 | 2017-09-06 19:26:46 -0400 | [diff] [blame] | 107 | static inline int running_with_asan(void) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 108 | { |
Evgenii Stepanov | 97ba2b2 | 2016-09-15 17:09:38 -0700 | [diff] [blame] | 109 | return &__asan_init != 0; |
Jorge Lucangeli Obes | 2413f37 | 2016-04-06 18:43:10 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 112 | int lookup_syscall(const char *name); |
| 113 | const char *lookup_syscall_name(int nr); |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 114 | |
Mike Frysinger | e34d7fe | 2018-05-23 04:18:30 -0400 | [diff] [blame] | 115 | long int parse_single_constant(char *constant_str, char **endptr); |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 116 | long int parse_constant(char *constant_str, char **endptr); |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 117 | int parse_size(size_t *size, const char *sizespec); |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 118 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 119 | char *strip(char *s); |
Mike Frysinger | b4c7e77 | 2018-01-17 17:40:15 -0500 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * tokenize: locate the next token in @stringp using the @delim |
| 123 | * @stringp A pointer to the string to scan for tokens |
| 124 | * @delim The delimiter to split by |
| 125 | * |
| 126 | * Note that, unlike strtok, @delim is not a set of characters, but the full |
| 127 | * delimiter. e.g. "a,;b,;c" with a delim of ",;" will yield ["a","b","c"]. |
| 128 | * |
| 129 | * Note that, unlike strtok, this may return an empty token. e.g. "a,,b" with |
| 130 | * strtok will yield ["a","b"], but this will yield ["a","","b"]. |
| 131 | */ |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 132 | char *tokenize(char **stringp, const char *delim); |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 133 | |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 134 | char *path_join(const char *external_path, const char *internal_path); |
| 135 | |
| 136 | /* |
| 137 | * consumebytes: consumes @length bytes from a buffer @buf of length @buflength |
| 138 | * @length Number of bytes to consume |
| 139 | * @buf Buffer to consume from |
| 140 | * @buflength Size of @buf |
| 141 | * |
| 142 | * Returns a pointer to the base of the bytes, or NULL for errors. |
| 143 | */ |
| 144 | void *consumebytes(size_t length, char **buf, size_t *buflength); |
| 145 | |
| 146 | /* |
| 147 | * consumestr: consumes a C string from a buffer @buf of length @length |
| 148 | * @buf Buffer to consume |
| 149 | * @length Length of buffer |
| 150 | * |
| 151 | * Returns a pointer to the base of the string, or NULL for errors. |
| 152 | */ |
| 153 | char *consumestr(char **buf, size_t *buflength); |
| 154 | |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 155 | /* |
| 156 | * init_logging: initializes the module-wide logging. |
| 157 | * @logger The logging system to use. |
| 158 | * @fd The file descriptor to log into. Ignored unless |
| 159 | * @logger = LOG_TO_FD. |
| 160 | * @min_priority The minimum priority to display. Corresponds to syslog's |
| 161 | priority parameter. Ignored unless @logger = LOG_TO_FD. |
| 162 | */ |
| 163 | void init_logging(enum logging_system_t logger, int fd, int min_priority); |
| 164 | |
Jorge Lucangeli Obes | a67bd6a | 2016-08-19 15:33:48 -0400 | [diff] [blame] | 165 | #ifdef __cplusplus |
| 166 | }; /* extern "C" */ |
| 167 | #endif |
| 168 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 169 | #endif /* _UTIL_H_ */ |