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 <stdio.h> 13 #include <stdlib.h> 14 #include <sys/types.h> 15 #include <syslog.h> 16 #include <unistd.h> 17 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 22 #if defined(USE_EXIT_ON_DIE) 23 #define do_abort() exit(1) 24 #else 25 #define do_abort() abort() 26 #endif 27 28 /* clang-format off */ 29 #define die(_msg, ...) do { \ 30 do_log(LOG_ERR, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__); \ 31 do_abort(); \ 32 } while (0) 33 34 #define pdie(_msg, ...) \ 35 die(_msg ": %m", ## __VA_ARGS__) 36 37 #define warn(_msg, ...) \ 38 do_log(LOG_WARNING, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__) 39 40 #define pwarn(_msg, ...) \ 41 warn(_msg ": %m", ## __VA_ARGS__) 42 43 #define info(_msg, ...) \ 44 do_log(LOG_INFO, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__) 45 46 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 47 /* clang-format on */ 48 49 extern const char *log_syscalls[]; 50 extern const size_t log_syscalls_len; 51 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 63 static inline int is_android(void) 64 { 65 #if defined(__ANDROID__) 66 return 1; 67 #else 68 return 0; 69 #endif 70 } 71 72 void __asan_init(void) __attribute__((weak)); 73 74 static inline int running_with_asan(void) 75 { 76 return &__asan_init != 0; 77 } 78 79 int lookup_syscall(const char *name); 80 const char *lookup_syscall_name(int nr); 81 82 long int parse_constant(char *constant_str, char **endptr); 83 int parse_size(size_t *size, const char *sizespec); 84 85 char *strip(char *s); 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 */ 98 char *tokenize(char **stringp, const char *delim); 99 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 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 131 #ifdef __cplusplus 132 }; /* extern "C" */ 133 #endif 134 135 #endif /* _UTIL_H_ */ 136