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 /*
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
55 /* clang-format off */
56 #define die(_msg, ...) \
57 do_fatal_log(LOG_ERR, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__)
58
59 #define pdie(_msg, ...) \
60 die(_msg ": %m", ## __VA_ARGS__)
61
62 #define warn(_msg, ...) \
63 do_log(LOG_WARNING, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__)
64
65 #define pwarn(_msg, ...) \
66 warn(_msg ": %m", ## __VA_ARGS__)
67
68 #define info(_msg, ...) \
69 do_log(LOG_INFO, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__)
70
71 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
72 /* clang-format on */
73
74 extern const char *log_syscalls[];
75 extern const size_t log_syscalls_len;
76
77 enum logging_system_t {
78 /* Log to syslog. This is the default. */
79 LOG_TO_SYSLOG = 0,
80
81 /* Log to a file descriptor. */
82 LOG_TO_FD,
83 };
84
85 /*
86 * Even though this function internally calls abort(2)/exit(2), it is
87 * intentionally not marked with the noreturn attribute. When marked as
88 * noreturn, clang coalesces several of the do_fatal_log() calls in methods that
89 * have a large number of such calls (like minijail_enter()), making it
90 * impossible for breakpad to correctly identify the line where it was called,
91 * making the backtrace somewhat useless.
92 */
93 extern void do_fatal_log(int priority, const char *format, ...)
94 attribute_printf(2, 3);
95
96 extern void do_log(int priority, const char *format, ...)
97 attribute_printf(2, 3);
98
is_android(void)99 static inline int is_android(void)
100 {
101 #if defined(__ANDROID__)
102 return 1;
103 #else
104 return 0;
105 #endif
106 }
107
108 void __asan_init(void) attribute_weak;
109 void __hwasan_init(void) attribute_weak;
110
running_with_asan(void)111 static inline int running_with_asan(void)
112 {
113 return &__asan_init != 0 || &__hwasan_init != 0;
114 }
115
116 int lookup_syscall(const char *name);
117 const char *lookup_syscall_name(int nr);
118
119 long int parse_single_constant(char *constant_str, char **endptr);
120 long int parse_constant(char *constant_str, char **endptr);
121 int parse_size(size_t *size, const char *sizespec);
122
123 char *strip(char *s);
124
125 /*
126 * tokenize: locate the next token in @stringp using the @delim
127 * @stringp A pointer to the string to scan for tokens
128 * @delim The delimiter to split by
129 *
130 * Note that, unlike strtok, @delim is not a set of characters, but the full
131 * delimiter. e.g. "a,;b,;c" with a delim of ",;" will yield ["a","b","c"].
132 *
133 * Note that, unlike strtok, this may return an empty token. e.g. "a,,b" with
134 * strtok will yield ["a","b"], but this will yield ["a","","b"].
135 */
136 char *tokenize(char **stringp, const char *delim);
137
138 char *path_join(const char *external_path, const char *internal_path);
139
140 /*
141 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
142 * @length Number of bytes to consume
143 * @buf Buffer to consume from
144 * @buflength Size of @buf
145 *
146 * Returns a pointer to the base of the bytes, or NULL for errors.
147 */
148 void *consumebytes(size_t length, char **buf, size_t *buflength);
149
150 /*
151 * consumestr: consumes a C string from a buffer @buf of length @length
152 * @buf Buffer to consume
153 * @length Length of buffer
154 *
155 * Returns a pointer to the base of the string, or NULL for errors.
156 */
157 char *consumestr(char **buf, size_t *buflength);
158
159 /*
160 * init_logging: initializes the module-wide logging.
161 * @logger The logging system to use.
162 * @fd The file descriptor to log into. Ignored unless
163 * @logger = LOG_TO_FD.
164 * @min_priority The minimum priority to display. Corresponds to syslog's
165 priority parameter. Ignored unless @logger = LOG_TO_FD.
166 */
167 void init_logging(enum logging_system_t logger, int fd, int min_priority);
168
169 #ifdef __cplusplus
170 }; /* extern "C" */
171 #endif
172
173 #endif /* _UTIL_H_ */
174