blob: f019f00df870158c93650ca0d05d2a5e68117c84 [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
12#include <stdlib.h>
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -040013#include <sys/types.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070014#include <syslog.h>
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -040015#include <unistd.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070016
Jorge Lucangeli Obesa67bd6a2016-08-19 15:33:48 -040017#ifdef __cplusplus
18extern "C" {
19#endif
20
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070021#define die(_msg, ...) do { \
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -040022 syslog(LOG_ERR, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__); \
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -070023 abort(); \
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070024} while (0)
25
26#define pdie(_msg, ...) \
Mike Frysingerb5d7b9f2015-01-09 03:50:15 -050027 die(_msg ": %m", ## __VA_ARGS__)
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070028
29#define warn(_msg, ...) \
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -040030 syslog(LOG_WARNING, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__)
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070031
Jorge Lucangeli Obesa2053902016-08-02 12:08:15 -040032#define pwarn(_msg, ...) \
33 warn(_msg ": %m", ## __VA_ARGS__)
34
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070035#define info(_msg, ...) \
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -040036 syslog(LOG_INFO, "libminijail[%d]: " _msg, getpid(), ## __VA_ARGS__)
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070037
Mike Frysinger404d2bb2017-01-17 19:29:00 -050038#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
39
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070040extern const char *log_syscalls[];
41extern const size_t log_syscalls_len;
42
Dylan Reidc4b0fdc2016-01-13 18:28:29 -080043static inline int is_android() {
Jorge Lucangeli Obes4b276a62016-01-07 14:31:33 -080044#if defined(__ANDROID__)
45 return 1;
46#else
47 return 0;
48#endif
49}
50
Evgenii Stepanov97ba2b22016-09-15 17:09:38 -070051void __asan_init(void) __attribute__((weak));
Jorge Lucangeli Obes9e35c092016-04-11 13:30:08 -070052
Evgenii Stepanov97ba2b22016-09-15 17:09:38 -070053static inline int running_with_asan() {
54 return &__asan_init != 0;
Jorge Lucangeli Obes2413f372016-04-06 18:43:10 -070055}
56
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070057int lookup_syscall(const char *name);
58const char *lookup_syscall_name(int nr);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040059
Luis Hector Chavez40b25742013-09-22 19:44:06 -070060long int parse_constant(char *constant_str, char **endptr);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040061
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070062char *strip(char *s);
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -080063char *tokenize(char **stringp, const char *delim);
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070064
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040065char *path_join(const char *external_path, const char *internal_path);
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -040066int write_proc_file(pid_t pid, const char *content, const char *basename);
67int write_pid_to_path(pid_t pid, const char *path);
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040068
69/*
70 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
71 * @length Number of bytes to consume
72 * @buf Buffer to consume from
73 * @buflength Size of @buf
74 *
75 * Returns a pointer to the base of the bytes, or NULL for errors.
76 */
77void *consumebytes(size_t length, char **buf, size_t *buflength);
78
79/*
80 * consumestr: consumes a C string from a buffer @buf of length @length
81 * @buf Buffer to consume
82 * @length Length of buffer
83 *
84 * Returns a pointer to the base of the string, or NULL for errors.
85 */
86char *consumestr(char **buf, size_t *buflength);
87
Jorge Lucangeli Obesa67bd6a2016-08-19 15:33:48 -040088#ifdef __cplusplus
89}; /* extern "C" */
90#endif
91
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070092#endif /* _UTIL_H_ */