]> git.proxmox.com Git - mirror_lxcfs.git/blame - macro.h
Merge pull request #336 from brauner/2020-02-27/safe_mount
[mirror_lxcfs.git] / macro.h
CommitLineData
1f5596dd
CB
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
17e0e368
CB
3#ifndef __LXCFS_MACRO_H
4#define __LXCFS_MACRO_H
5
1f5596dd
CB
6#ifndef _GNU_SOURCE
7#define _GNU_SOURCE
8#endif
9
10#ifndef FUSE_USE_VERSION
11#define FUSE_USE_VERSION 26
12#endif
13
14#define _FILE_OFFSET_BITS 64
15
5fbea8a6
CB
16#include <stdio.h>
17
1f5596dd
CB
18#include "config.h"
19
5fbea8a6
CB
20#define BATCH_SIZE 50
21
22/* filesystem magic values */
23#ifndef CGROUP_SUPER_MAGIC
24#define CGROUP_SUPER_MAGIC 0x27e0eb
25#endif
26
27#ifndef CGROUP2_SUPER_MAGIC
28#define CGROUP2_SUPER_MAGIC 0x63677270
29#endif
30
17e0e368
CB
31#define lxcfs_debug_stream(stream, format, ...) \
32 do { \
5fbea8a6 33 fprintf(stream, "%s: %d: %s: " format "\n", __FILE__, __LINE__, \
5faa9af7 34 __func__, ##__VA_ARGS__); \
17e0e368
CB
35 } while (false)
36
5faa9af7 37#define lxcfs_error(format, ...) lxcfs_debug_stream(stderr, format, ##__VA_ARGS__)
17e0e368
CB
38
39#ifdef DEBUG
5faa9af7 40#define lxcfs_debug(format, ...) lxcfs_error(format, ##__VA_ARGS__)
17e0e368
CB
41#else
42#define lxcfs_debug(format, ...)
43#endif /* DEBUG */
44
83dad6a5 45#ifdef VERBOSE
5faa9af7 46#define lxcfs_v(format, ...) lxcfs_error(format, ##__VA_ARGS__);
83dad6a5
SG
47#else
48#define lxcfs_v(format, ...)
49#endif /* VERBOSE */
50
5fbea8a6
CB
51#define log_error_errno(__ret__, __errno__, format, ...) \
52 ({ \
53 errno = __errno__; \
54 lxcfs_error(format, ##__VA_ARGS__); \
55 __ret__; \
56 })
57
58#define STRLITERALLEN(x) (sizeof(""x"") - 1)
59
60/* Calculate the number of chars needed to represent a given integer as a C
61 * string. Include room for '-' to indicate negative numbers and the \0 byte.
62 * This is based on systemd.
63 */
64#define INTTYPE_TO_STRLEN(type) \
65 (2 + (sizeof(type) <= 1 \
66 ? 3 \
67 : sizeof(type) <= 2 \
68 ? 5 \
69 : sizeof(type) <= 4 \
70 ? 10 \
71 : sizeof(type) <= 8 \
72 ? 20 \
73 : sizeof(int[-2 * (sizeof(type) > 8)])))
74
75#define ret_errno(__errno__) \
76 ({ \
77 errno = __errno__; \
78 -__errno__; \
79 })
80
81#define ret_set_errno(__ret__, __errno__) \
82 ({ \
83 errno = __errno__; \
84 __ret__; \
85 })
86
87#define lxc_iterate_parts(__iterator, __splitme, __separators) \
88 for (char *__p = NULL, *__it = strtok_r(__splitme, __separators, &__p); \
89 (__iterator = __it); \
90 __iterator = __it = strtok_r(NULL, __separators, &__p))
91
2243c5a9
CB
92#define log_exit(format, ...) \
93 ({ \
94 fprintf(stderr, format, ##__VA_ARGS__); \
4ec5c9da 95 _exit(EXIT_FAILURE); \
2243c5a9
CB
96 })
97
99b183fb
CB
98#define PTR_TO_UINT64(p) ((uint64_t)((intptr_t)(p)))
99#define INTTYPE_TO_PTR(u) ((void *)((intptr_t)(u)))
100
17e0e368 101#endif /* __LXCFS_MACRO_H */