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 <sys/types.h>
14 #include <syslog.h>
15 #include <unistd.h>
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 #define die(_msg, ...) do { \
22 syslog(LOG_ERR, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__); \
23 abort(); \
24 } while (0)
25
26 #define pdie(_msg, ...) \
27 die(_msg ": %m", ## __VA_ARGS__)
28
29 #define warn(_msg, ...) \
30 syslog(LOG_WARNING, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__)
31
32 #define pwarn(_msg, ...) \
33 warn(_msg ": %m", ## __VA_ARGS__)
34
35 #define info(_msg, ...) \
36 syslog(LOG_INFO, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__)
37
38 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
39
40 extern const char *log_syscalls[];
41 extern const size_t log_syscalls_len;
42
is_android()43 static inline int is_android() {
44 #if defined(__ANDROID__)
45 return 1;
46 #else
47 return 0;
48 #endif
49 }
50
51 void __asan_init(void) __attribute__((weak));
52
running_with_asan()53 static inline int running_with_asan() {
54 return &__asan_init != 0;
55 }
56
57 int lookup_syscall(const char *name);
58 const char *lookup_syscall_name(int nr);
59
60 long int parse_constant(char *constant_str, char **endptr);
61 int parse_size(size_t *size, const char *sizespec);
62
63 char *strip(char *s);
64 char *tokenize(char **stringp, const char *delim);
65
66 char *path_join(const char *external_path, const char *internal_path);
67 int write_proc_file(pid_t pid, const char *content, const char *basename);
68 int write_pid_to_path(pid_t pid, const char *path);
69
70 /*
71 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
72 * @length Number of bytes to consume
73 * @buf Buffer to consume from
74 * @buflength Size of @buf
75 *
76 * Returns a pointer to the base of the bytes, or NULL for errors.
77 */
78 void *consumebytes(size_t length, char **buf, size_t *buflength);
79
80 /*
81 * consumestr: consumes a C string from a buffer @buf of length @length
82 * @buf Buffer to consume
83 * @length Length of buffer
84 *
85 * Returns a pointer to the base of the string, or NULL for errors.
86 */
87 char *consumestr(char **buf, size_t *buflength);
88
89 #ifdef __cplusplus
90 }; /* extern "C" */
91 #endif
92
93 #endif /* _UTIL_H_ */
94