]> git.proxmox.com Git - mirror_lxcfs.git/blame - src/macro.h
proc_loadavg: replace malloc() with asprintf() in calc_pid()
[mirror_lxcfs.git] / src / macro.h
CommitLineData
db0463bf 1/* SPDX-License-Identifier: LGPL-2.1+ */
1f5596dd 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
cc42d0c7
CB
51#define lxcfs_info(format, ...) \
52 do { \
53 fprintf(stderr, format "\n", ##__VA_ARGS__); \
54 } while (false)
55
5fbea8a6
CB
56#define log_error_errno(__ret__, __errno__, format, ...) \
57 ({ \
58 errno = __errno__; \
59 lxcfs_error(format, ##__VA_ARGS__); \
60 __ret__; \
61 })
62
971aa868
CB
63#define log_error(__ret__, format, ...) \
64 ({ \
65 lxcfs_error(format, ##__VA_ARGS__); \
66 __ret__; \
67 })
68
5fbea8a6
CB
69#define STRLITERALLEN(x) (sizeof(""x"") - 1)
70
71/* Calculate the number of chars needed to represent a given integer as a C
72 * string. Include room for '-' to indicate negative numbers and the \0 byte.
73 * This is based on systemd.
74 */
75#define INTTYPE_TO_STRLEN(type) \
76 (2 + (sizeof(type) <= 1 \
77 ? 3 \
78 : sizeof(type) <= 2 \
79 ? 5 \
80 : sizeof(type) <= 4 \
81 ? 10 \
82 : sizeof(type) <= 8 \
83 ? 20 \
84 : sizeof(int[-2 * (sizeof(type) > 8)])))
85
05b7a16d
CB
86#define move_ptr(ptr) \
87 ({ \
88 typeof(ptr) __internal_ptr__ = (ptr); \
89 (ptr) = NULL; \
90 __internal_ptr__; \
91 })
92
93#define move_fd(fd) \
94 ({ \
95 int __internal_fd__ = (fd); \
96 (fd) = -EBADF; \
97 __internal_fd__; \
98 })
99
5fbea8a6
CB
100#define ret_errno(__errno__) \
101 ({ \
102 errno = __errno__; \
103 -__errno__; \
104 })
105
106#define ret_set_errno(__ret__, __errno__) \
107 ({ \
108 errno = __errno__; \
109 __ret__; \
110 })
111
112#define lxc_iterate_parts(__iterator, __splitme, __separators) \
113 for (char *__p = NULL, *__it = strtok_r(__splitme, __separators, &__p); \
114 (__iterator = __it); \
115 __iterator = __it = strtok_r(NULL, __separators, &__p))
116
2243c5a9
CB
117#define log_exit(format, ...) \
118 ({ \
119 fprintf(stderr, format, ##__VA_ARGS__); \
4ec5c9da 120 _exit(EXIT_FAILURE); \
2243c5a9
CB
121 })
122
b456d40d 123#ifdef DEBUG
6ba365d9
CB
124#define log_debug(__ret__, format, ...) \
125 ({ \
126 lxcfs_debug_stream(stderr, format, ##__VA_ARGS__); \
127 __ret__; \
b456d40d
CB
128 })
129#else
130#define log_debug(__ret__, format, ...) ({ __ret__; })
131#endif
132
f1744de4
CB
133#define PTR_TO_INT(p) ((int)((intptr_t)(p)))
134#define INT_TO_PTR(u) ((void *)((intptr_t)(u)))
99b183fb
CB
135#define PTR_TO_UINT64(p) ((uint64_t)((intptr_t)(p)))
136#define INTTYPE_TO_PTR(u) ((void *)((intptr_t)(u)))
137
b0a2dc08
CB
138#define __visible __attribute__((visibility("default")))
139
2d7bcab7
CB
140#define __lxcfs_fuse_ops
141
17e0e368 142#endif /* __LXCFS_MACRO_H */