]> arthur.barton.de Git - netdata.git/blob - src/common.h
3d864f4244d6b31e2f1499ae2023baf432d601ce
[netdata.git] / src / common.h
1 #ifndef NETDATA_COMMON_H
2 #define NETDATA_COMMON_H 1
3
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 // ----------------------------------------------------------------------------
9 // system include files for all netdata C programs
10
11 /* select the memory allocator, based on autoconf findings */
12 #if defined(ENABLE_JEMALLOC)
13
14 #if defined(HAVE_JEMALLOC_JEMALLOC_H)
15 #include <jemalloc/jemalloc.h>
16 #else
17 #include <malloc.h>
18 #endif
19
20 #elif defined(ENABLE_TCMALLOC)
21
22 #include <google/tcmalloc.h>
23
24 #else /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */
25
26 #if !(defined(__FreeBSD__) || defined(__APPLE__))
27 #include <malloc.h>
28 #endif /* __FreeBSD__ || __APPLE__ */
29
30 #endif
31
32 #include <pthread.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <stddef.h>
38 #include <ctype.h>
39 #include <string.h>
40 #include <strings.h>
41 #include <arpa/inet.h>
42 #include <netinet/tcp.h>
43
44 #ifdef HAVE_NETINET_IN_H
45 #include <netinet/in.h>
46 #endif
47
48 #ifdef HAVE_RESOLV_H
49 #include <resolv.h>
50 #endif
51
52 #include <dirent.h>
53 #include <fcntl.h>
54 #include <getopt.h>
55 #include <grp.h>
56 #include <pwd.h>
57 #include <locale.h>
58
59 #ifdef HAVE_NETDB_H
60 #include <netdb.h>
61 #endif
62
63 #include <net/if.h>
64
65 #include <poll.h>
66 #include <signal.h>
67 #include <syslog.h>
68 #include <sys/mman.h>
69
70 #ifdef HAVE_SYS_PRCTL_H
71 #include <sys/prctl.h>
72 #endif
73
74 #include <sys/resource.h>
75 #include <sys/socket.h>
76 #include <sys/stat.h>
77 #include <sys/statvfs.h>
78 #include <sys/syscall.h>
79 #include <sys/time.h>
80 #include <sys/types.h>
81 #include <sys/wait.h>
82 #include <time.h>
83 #include <unistd.h>
84 #include <uuid/uuid.h>
85
86 // #1408
87 #ifdef MAJOR_IN_MKDEV
88 #include <sys/mkdev.h>
89 #endif
90 #ifdef MAJOR_IN_SYSMACROS
91 #include <sys/sysmacros.h>
92 #endif
93
94 /*
95 #include <mntent.h>
96 */
97
98 #ifdef STORAGE_WITH_MATH
99 #include <math.h>
100 #endif
101
102 #if defined(HAVE_INTTYPES_H)
103 #include <inttypes.h>
104 #elif defined(HAVE_STDINT_H)
105 #include <stdint.h>
106 #endif
107
108 #ifdef NETDATA_WITH_ZLIB
109 #include <zlib.h>
110 #endif
111
112 #ifdef HAVE_CAPABILITY
113 #include <sys/capability.h>
114 #endif
115
116 // ----------------------------------------------------------------------------
117 // netdata common definitions
118
119 #if (SIZEOF_VOID_P == 8)
120 #define ENVIRONMENT64
121 #elif (SIZEOF_VOID_P == 4)
122 #define ENVIRONMENT32
123 #else
124 #error "Cannot detect if this is a 32 or 64 bit CPU"
125 #endif
126
127 #ifdef __GNUC__
128 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
129 #endif // __GNUC__
130
131 #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
132 #define NEVERNULL __attribute__((returns_nonnull))
133 #else
134 #define NEVERNULL
135 #endif
136
137 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
138 #define MALLOCLIKE __attribute__((malloc))
139 #else
140 #define MALLOCLIKE
141 #endif
142
143 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
144 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
145 #else
146 #define PRINTFLIKE(f, a)
147 #endif
148
149 #ifdef HAVE_FUNC_ATTRIBUTE_NORETURN
150 #define NORETURN __attribute__ ((noreturn))
151 #else
152 #define NORETURN
153 #endif
154
155 #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
156 #define WARNUNUSED __attribute__ ((warn_unused_result))
157 #else
158 #define WARNUNUSED
159 #endif
160
161 #ifdef abs
162 #undef abs
163 #endif
164 #define abs(x) ((x < 0)? -x : x)
165
166 #define GUID_LEN 36
167
168 // ----------------------------------------------------------------------------
169 // netdata include files
170
171 #include "simple_pattern.h"
172 #include "avl.h"
173 #include "clocks.h"
174 #include "log.h"
175 #include "global_statistics.h"
176 #include "storage_number.h"
177 #include "web_buffer.h"
178 #include "web_buffer_svg.h"
179 #include "url.h"
180 #include "popen.h"
181
182 #include "procfile.h"
183 #include "appconfig.h"
184 #include "dictionary.h"
185 #include "proc_self_mountinfo.h"
186 #include "plugin_checks.h"
187 #include "plugin_idlejitter.h"
188 #include "plugin_nfacct.h"
189
190 #if defined(__FreeBSD__)
191 #include "plugin_freebsd.h"
192 #define NETDATA_OS_TYPE "freebsd"
193 #elif defined(__APPLE__)
194 #include "plugin_macos.h"
195 #define NETDATA_OS_TYPE "macos"
196 #else
197 #include "plugin_proc.h"
198 #include "plugin_proc_diskspace.h"
199 #define NETDATA_OS_TYPE "linux"
200 #endif /* __FreeBSD__, __APPLE__*/
201
202 #include "socket.h"
203 #include "eval.h"
204 #include "health.h"
205 #include "rrd.h"
206 #include "plugin_tc.h"
207 #include "plugins_d.h"
208 #include "rrd2json.h"
209 #include "web_client.h"
210 #include "web_server.h"
211 #include "registry.h"
212 #include "daemon.h"
213 #include "main.h"
214 #include "unit_test.h"
215 #include "ipc.h"
216 #include "backends.h"
217 #include "inlined.h"
218 #include "adaptive_resortable_list.h"
219 #include "rrdpush.h"
220 #include "web_api_v1.h"
221 #include "web_api_old.h"
222
223 extern char *netdata_configured_hostname;
224 extern char *netdata_configured_config_dir;
225 extern char *netdata_configured_log_dir;
226 extern char *netdata_configured_plugins_dir;
227 extern char *netdata_configured_web_dir;
228 extern char *netdata_configured_cache_dir;
229 extern char *netdata_configured_varlib_dir;
230 extern char *netdata_configured_home_dir;
231 extern char *netdata_configured_host_prefix;
232
233 extern void netdata_fix_chart_id(char *s);
234 extern void netdata_fix_chart_name(char *s);
235
236 extern void strreverse(char* begin, char* end);
237 extern char *mystrsep(char **ptr, char *s);
238 extern char *trim(char *s);
239
240 extern int  vsnprintfz(char *dst, size_t n, const char *fmt, va_list args);
241 extern int  snprintfz(char *dst, size_t n, const char *fmt, ...) PRINTFLIKE(3, 4);
242
243 // memory allocation functions that handle failures
244 #ifdef NETDATA_LOG_ALLOCATIONS
245 #define strdupz(s) strdupz_int(__FILE__, __FUNCTION__, __LINE__, s)
246 #define callocz(nmemb, size) callocz_int(__FILE__, __FUNCTION__, __LINE__, nmemb, size)
247 #define mallocz(size) mallocz_int(__FILE__, __FUNCTION__, __LINE__, size)
248 #define reallocz(ptr, size) reallocz_int(__FILE__, __FUNCTION__, __LINE__, ptr, size)
249 #define freez(ptr) freez_int(__FILE__, __FUNCTION__, __LINE__, ptr)
250
251 extern char *strdupz_int(const char *file, const char *function, const unsigned long line, const char *s);
252 extern void *callocz_int(const char *file, const char *function, const unsigned long line, size_t nmemb, size_t size);
253 extern void *mallocz_int(const char *file, const char *function, const unsigned long line, size_t size);
254 extern void *reallocz_int(const char *file, const char *function, const unsigned long line, void *ptr, size_t size);
255 extern void freez_int(const char *file, const char *function, const unsigned long line, void *ptr);
256 #else
257 extern char *strdupz(const char *s) MALLOCLIKE NEVERNULL;
258 extern void *callocz(size_t nmemb, size_t size) MALLOCLIKE NEVERNULL;
259 extern void *mallocz(size_t size) MALLOCLIKE NEVERNULL;
260 extern void *reallocz(void *ptr, size_t size) MALLOCLIKE NEVERNULL;
261 extern void freez(void *ptr);
262 #endif
263
264 extern void json_escape_string(char *dst, const char *src, size_t size);
265
266 extern void *mymmap(const char *filename, size_t size, int flags, int ksm);
267 extern int savememory(const char *filename, void *mem, size_t size);
268
269 extern int fd_is_valid(int fd);
270
271 extern int enable_ksm;
272
273 extern pid_t gettid(void);
274
275 extern int sleep_usec(usec_t usec);
276
277 extern char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len);
278
279 extern int processors;
280 extern long get_system_cpus(void);
281
282 extern pid_t pid_max;
283 extern pid_t get_system_pid_max(void);
284
285 /* Number of ticks per second */
286 extern unsigned int hz;
287 extern void get_system_HZ(void);
288
289 extern volatile sig_atomic_t netdata_exit;
290 extern const char *os_type;
291
292 extern const char *program_version;
293
294 /* fix for alpine linux */
295 #ifndef RUSAGE_THREAD
296 #ifdef RUSAGE_CHILDREN
297 #define RUSAGE_THREAD RUSAGE_CHILDREN
298 #endif
299 #endif
300
301 #endif /* NETDATA_COMMON_H */