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