blob: 7ff86b8593346c2de56eb9732726c1846bc6abac [file] [log] [blame]
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -07001/* 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
Luis Hector Chavez114a9302017-09-05 20:36:58 -070012#include <stdio.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070013#include <stdlib.h>
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -040014#include <sys/types.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070015#include <syslog.h>
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -040016#include <unistd.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070017
Jorge Lucangeli Obesa67bd6a2016-08-19 15:33:48 -040018#ifdef __cplusplus
19extern "C" {
20#endif
21
Luis Hector Chavezdaa03712017-09-06 08:10:33 -070022#if defined(USE_EXIT_ON_DIE)
23#define do_abort() exit(1)
24#else
25#define do_abort() abort()
26#endif
27
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040028/* clang-format off */
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070029#define die(_msg, ...) do { \
Luis Hector Chavez114a9302017-09-05 20:36:58 -070030 do_log(LOG_ERR, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__); \
Luis Hector Chavezdaa03712017-09-06 08:10:33 -070031 do_abort(); \
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070032} while (0)
33
34#define pdie(_msg, ...) \
Mike Frysingerb5d7b9f2015-01-09 03:50:15 -050035 die(_msg ": %m", ## __VA_ARGS__)
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070036
37#define warn(_msg, ...) \
Luis Hector Chavez114a9302017-09-05 20:36:58 -070038 do_log(LOG_WARNING, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__)
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070039
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -040040#define pwarn(_msg, ...) \
41 warn(_msg ": %m", ## __VA_ARGS__)
42
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070043#define info(_msg, ...) \
Luis Hector Chavez114a9302017-09-05 20:36:58 -070044 do_log(LOG_INFO, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__)
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070045
Mike Frysinger404d2bb2017-01-17 19:29:00 -050046#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040047/* clang-format on */
Mike Frysinger404d2bb2017-01-17 19:29:00 -050048
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070049extern const char *log_syscalls[];
50extern const size_t log_syscalls_len;
51
Luis Hector Chavez114a9302017-09-05 20:36:58 -070052enum 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
60extern void do_log(int priority, const char *format, ...)
61 __attribute__((__format__(__printf__, 2, 3)));
62
Mike Frysinger8c013272017-09-06 19:26:46 -040063static inline int is_android(void)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040064{
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -080065#if defined(__ANDROID__)
66 return 1;
67#else
68 return 0;
69#endif
70}
71
Evgenii Stepanov97ba2b22016-09-15 17:09:38 -070072void __asan_init(void) __attribute__((weak));
Jorge Lucangeli Obes9e35c092016-04-11 13:30:08 -070073
Mike Frysinger8c013272017-09-06 19:26:46 -040074static inline int running_with_asan(void)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040075{
Evgenii Stepanov97ba2b22016-09-15 17:09:38 -070076 return &__asan_init != 0;
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -070077}
78
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070079int lookup_syscall(const char *name);
80const char *lookup_syscall_name(int nr);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040081
Luis Hector Chavez40b25742013-09-22 19:44:06 -070082long int parse_constant(char *constant_str, char **endptr);
Martin Pelikánab9eb442017-01-25 11:53:58 +110083int parse_size(size_t *size, const char *sizespec);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040084
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070085char *strip(char *s);
Mike Frysingerb4c7e772018-01-17 17:40:15 -050086
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 */
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -080098char *tokenize(char **stringp, const char *delim);
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070099
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400100char *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 */
110void *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 */
119char *consumestr(char **buf, size_t *buflength);
120
Luis Hector Chavez114a9302017-09-05 20:36:58 -0700121/*
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 */
129void init_logging(enum logging_system_t logger, int fd, int min_priority);
130
Jorge Lucangeli Obesa67bd6a2016-08-19 15:33:48 -0400131#ifdef __cplusplus
132}; /* extern "C" */
133#endif
134
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -0700135#endif /* _UTIL_H_ */