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 | |
| 12 | #include <stdlib.h> |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 13 | #include <sys/types.h> |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 14 | #include <syslog.h> |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 15 | #include <unistd.h> |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 16 | |
Jorge Lucangeli Obes | a67bd6a | 2016-08-19 15:33:48 -0400 | [diff] [blame] | 17 | #ifdef __cplusplus |
| 18 | extern "C" { |
| 19 | #endif |
| 20 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 21 | /* clang-format off */ |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 22 | #define die(_msg, ...) do { \ |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 23 | syslog(LOG_ERR, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__); \ |
Jorge Lucangeli Obes | 2f61ee4 | 2014-06-16 11:08:18 -0700 | [diff] [blame] | 24 | abort(); \ |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 25 | } while (0) |
| 26 | |
| 27 | #define pdie(_msg, ...) \ |
Mike Frysinger | b5d7b9f | 2015-01-09 03:50:15 -0500 | [diff] [blame] | 28 | die(_msg ": %m", ## __VA_ARGS__) |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 29 | |
| 30 | #define warn(_msg, ...) \ |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 31 | syslog(LOG_WARNING, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__) |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 32 | |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 33 | #define pwarn(_msg, ...) \ |
| 34 | warn(_msg ": %m", ## __VA_ARGS__) |
| 35 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 36 | #define info(_msg, ...) \ |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 37 | syslog(LOG_INFO, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__) |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 38 | |
Mike Frysinger | 404d2bb | 2017-01-17 19:29:00 -0500 | [diff] [blame] | 39 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 40 | /* clang-format on */ |
Mike Frysinger | 404d2bb | 2017-01-17 19:29:00 -0500 | [diff] [blame] | 41 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 42 | extern const char *log_syscalls[]; |
| 43 | extern const size_t log_syscalls_len; |
| 44 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 45 | static inline int is_android() |
| 46 | { |
Jorge Lucangeli Obes | 4b276a6 | 2016-01-07 14:31:33 -0800 | [diff] [blame] | 47 | #if defined(__ANDROID__) |
| 48 | return 1; |
| 49 | #else |
| 50 | return 0; |
| 51 | #endif |
| 52 | } |
| 53 | |
Evgenii Stepanov | 97ba2b2 | 2016-09-15 17:09:38 -0700 | [diff] [blame] | 54 | void __asan_init(void) __attribute__((weak)); |
Jorge Lucangeli Obes | 9e35c09 | 2016-04-11 13:30:08 -0700 | [diff] [blame] | 55 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 56 | static inline int running_with_asan() |
| 57 | { |
Evgenii Stepanov | 97ba2b2 | 2016-09-15 17:09:38 -0700 | [diff] [blame] | 58 | return &__asan_init != 0; |
Jorge Lucangeli Obes | 2413f37 | 2016-04-06 18:43:10 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 61 | int lookup_syscall(const char *name); |
| 62 | const char *lookup_syscall_name(int nr); |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 63 | |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 64 | long int parse_constant(char *constant_str, char **endptr); |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 65 | int parse_size(size_t *size, const char *sizespec); |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 66 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 67 | char *strip(char *s); |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 68 | char *tokenize(char **stringp, const char *delim); |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 69 | |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 70 | char *path_join(const char *external_path, const char *internal_path); |
| 71 | |
| 72 | /* |
| 73 | * consumebytes: consumes @length bytes from a buffer @buf of length @buflength |
| 74 | * @length Number of bytes to consume |
| 75 | * @buf Buffer to consume from |
| 76 | * @buflength Size of @buf |
| 77 | * |
| 78 | * Returns a pointer to the base of the bytes, or NULL for errors. |
| 79 | */ |
| 80 | void *consumebytes(size_t length, char **buf, size_t *buflength); |
| 81 | |
| 82 | /* |
| 83 | * consumestr: consumes a C string from a buffer @buf of length @length |
| 84 | * @buf Buffer to consume |
| 85 | * @length Length of buffer |
| 86 | * |
| 87 | * Returns a pointer to the base of the string, or NULL for errors. |
| 88 | */ |
| 89 | char *consumestr(char **buf, size_t *buflength); |
| 90 | |
Jorge Lucangeli Obes | a67bd6a | 2016-08-19 15:33:48 -0400 | [diff] [blame] | 91 | #ifdef __cplusplus |
| 92 | }; /* extern "C" */ |
| 93 | #endif |
| 94 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 95 | #endif /* _UTIL_H_ */ |