| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | /* |
| 4 | * This file should include everything from the operating system needed to compile Netdata, |
| 5 | * without any Netdata specific includes. |
| 6 | * |
| 7 | * It should be the baseline of includes (operating system and common libraries related). |
| 8 | */ |
| 9 | |
| 10 | #ifndef LIBNETDATA_COMMON_H |
| 11 | #define LIBNETDATA_COMMON_H |
| 12 | |
| 13 | # ifdef __cplusplus |
| 14 | extern "C" { |
| 15 | # endif |
| 16 | |
| 17 | #include "libnetdata-base.h" |
| 18 | #include "libnetdata-types.h" |
| 19 | #include "libnetdata-platform-fwd.h" |
| 20 | |
| 21 | // -------------------------------------------------------------------------------------------------------------------- |
| 22 | // NETDATA_OS_TYPE |
| 23 | |
| 24 | // -------------------------------------------------------------------------------------------------------------------- |
| 25 | // memory allocators |
| 26 | |
| 27 | /* select the memory allocator, based on autoconf findings */ |
| 28 | #if defined(ENABLE_JEMALLOC) |
| 29 | |
| 30 | #if defined(HAVE_JEMALLOC_JEMALLOC_H) |
| 31 | #include <jemalloc/jemalloc.h> |
| 32 | #else // !defined(HAVE_JEMALLOC_JEMALLOC_H) |
| 33 | #include <malloc.h> |
| 34 | #endif // !defined(HAVE_JEMALLOC_JEMALLOC_H) |
| 35 | |
| 36 | #elif defined(ENABLE_TCMALLOC) |
| 37 | |
| 38 | #include <google/tcmalloc.h> |
| 39 | |
| 40 | #else /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */ |
| 41 | |
| 42 | #if !(defined(__FreeBSD__) || defined(__APPLE__)) |
| 43 | #include <malloc.h> |
| 44 | #endif /* __FreeBSD__ || __APPLE__ */ |
| 45 | |
| 46 | #endif /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */ |
| 47 | |
| 48 | // -------------------------------------------------------------------------------------------------------------------- |
| 49 | |
| 50 | #include <pthread.h> |
| 51 | #include <errno.h> |
| 52 | #include <stdbool.h> |
| 53 | #include <stdio.h> |
| 54 | #include <stdlib.h> |
| 55 | #include <stdarg.h> |
| 56 | #include <stddef.h> |
| 57 | #include <ctype.h> |
| 58 | #include <string.h> |
| 59 | #include <strings.h> |
| 60 | #include <libgen.h> |
| 61 | #include <dirent.h> |
| 62 | #include <fcntl.h> |
| 63 | #include <getopt.h> |
| 64 | #include <limits.h> |
| 65 | #include <locale.h> |
| 66 | #include <signal.h> |
| 67 | #include <sys/time.h> |
| 68 | #include <sys/types.h> |
| 69 | #include <time.h> |
| 70 | #include <unistd.h> |
| 71 | #include <uv.h> |
| 72 | #include <assert.h> |
| 73 | |
| 74 | #ifdef HAVE_ARPA_INET_H |
| 75 | #include <arpa/inet.h> |
| 76 | #endif |
| 77 | |
| 78 | #ifdef HAVE_NETINET_TCP_H |
| 79 | #include <netinet/tcp.h> |
| 80 | #endif |
| 81 | |
| 82 | #ifdef HAVE_SYS_IOCTL_H |
| 83 | #include <sys/ioctl.h> |
| 84 | #endif |
| 85 | |
| 86 | #ifdef HAVE_GRP_H |
| 87 | #include <grp.h> |
| 88 | #else |
| 89 | typedef uint32_t gid_t; |
| 90 | #endif |
| 91 | |
| 92 | #ifdef HAVE_PWD_H |
| 93 | #include <pwd.h> |
| 94 | #else |
| 95 | typedef uint32_t uid_t; |
| 96 | #endif |
| 97 | |
| 98 | #ifdef HAVE_NET_IF_H |
| 99 | #include <net/if.h> |
| 100 | #endif |
| 101 | |
| 102 | #ifdef HAVE_POLL_H |
| 103 | #include <poll.h> |
| 104 | #endif |
| 105 | |
| 106 | #ifdef HAVE_SYSLOG_H |
| 107 | #include <syslog.h> |
| 108 | #else |
| 109 | /* priorities */ |
| 110 | #define LOG_EMERG 0 /* system is unusable */ |
| 111 | #define LOG_ALERT 1 /* action must be taken immediately */ |
| 112 | #define LOG_CRIT 2 /* critical conditions */ |
| 113 | #define LOG_ERR 3 /* error conditions */ |
| 114 | #define LOG_WARNING 4 /* warning conditions */ |
| 115 | #define LOG_NOTICE 5 /* normal but significant condition */ |
| 116 | #define LOG_INFO 6 /* informational */ |
| 117 | #define LOG_DEBUG 7 /* debug-level messages */ |
| 118 | |
| 119 | /* facility codes */ |
| 120 | #define LOG_KERN (0<<3) /* kernel messages */ |
| 121 | #define LOG_USER (1<<3) /* random user-level messages */ |
| 122 | #define LOG_MAIL (2<<3) /* mail system */ |
| 123 | #define LOG_DAEMON (3<<3) /* system daemons */ |
| 124 | #define LOG_AUTH (4<<3) /* security/authorization messages */ |
| 125 | #define LOG_SYSLOG (5<<3) /* messages generated internally by syslogd */ |
| 126 | #define LOG_LPR (6<<3) /* line printer subsystem */ |
| 127 | #define LOG_NEWS (7<<3) /* network news subsystem */ |
| 128 | #define LOG_UUCP (8<<3) /* UUCP subsystem */ |
| 129 | #define LOG_CRON (9<<3) /* clock daemon */ |
| 130 | #define LOG_AUTHPRIV (10<<3) /* security/authorization messages (private) */ |
| 131 | #define LOG_FTP (11<<3) /* ftp daemon */ |
| 132 | |
| 133 | /* other codes through 15 reserved for system use */ |
| 134 | #define LOG_LOCAL0 (16<<3) /* reserved for local use */ |
| 135 | #define LOG_LOCAL1 (17<<3) /* reserved for local use */ |
| 136 | #define LOG_LOCAL2 (18<<3) /* reserved for local use */ |
| 137 | #define LOG_LOCAL3 (19<<3) /* reserved for local use */ |
| 138 | #define LOG_LOCAL4 (20<<3) /* reserved for local use */ |
| 139 | #define LOG_LOCAL5 (21<<3) /* reserved for local use */ |
| 140 | #define LOG_LOCAL6 (22<<3) /* reserved for local use */ |
| 141 | #define LOG_LOCAL7 (23<<3) /* reserved for local use */ |
| 142 | #endif |
| 143 | |
| 144 | #ifdef HAVE_SYS_MMAN_H |
| 145 | #include <sys/mman.h> |
| 146 | #endif |
| 147 | |
| 148 | #ifdef HAVE_SYS_RESOURCE_H |
| 149 | #include <sys/resource.h> |
| 150 | #endif |
| 151 | |
| 152 | #ifdef HAVE_SYS_SOCKET_H |
| 153 | #include <sys/socket.h> |
| 154 | #endif |
| 155 | |
| 156 | #ifdef HAVE_SYS_WAIT_H |
| 157 | #include <sys/wait.h> |
| 158 | #endif |
| 159 | |
| 160 | #ifdef HAVE_SYS_UN_H |
| 161 | #include <sys/un.h> |
| 162 | #endif |
| 163 | |
| 164 | #ifdef HAVE_SPAWN_H |
| 165 | #include <spawn.h> |
| 166 | #endif |
| 167 | |
| 168 | #ifdef HAVE_NETINET_IN_H |
| 169 | #include <netinet/in.h> |
| 170 | #endif |
| 171 | |
| 172 | #ifdef HAVE_RESOLV_H |
| 173 | #include <resolv.h> |
| 174 | #endif |
| 175 | |
| 176 | #ifdef HAVE_NETDB_H |
| 177 | #include <netdb.h> |
| 178 | #endif |
| 179 | |
| 180 | #ifdef HAVE_SYS_PRCTL_H |
| 181 | #include <sys/prctl.h> |
| 182 | #endif |
| 183 | |
| 184 | #ifdef HAVE_SYS_STAT_H |
| 185 | #include <sys/stat.h> |
| 186 | #endif |
| 187 | |
| 188 | #ifdef HAVE_SYS_VFS_H |
| 189 | #include <sys/vfs.h> |
| 190 | #endif |
| 191 | |
| 192 | #ifdef HAVE_SYS_STATFS_H |
| 193 | #include <sys/statfs.h> |
| 194 | #endif |
| 195 | |
| 196 | #ifdef HAVE_LINUX_MAGIC_H |
| 197 | #include <linux/magic.h> |
| 198 | #endif |
| 199 | |
| 200 | #ifdef HAVE_SYS_MOUNT_H |
| 201 | #include <sys/mount.h> |
| 202 | #endif |
| 203 | |
| 204 | #ifdef HAVE_SYS_STATVFS_H |
| 205 | #include <sys/statvfs.h> |
| 206 | #endif |
| 207 | |
| 208 | // #1408 |
| 209 | #ifdef MAJOR_IN_MKDEV |
| 210 | #include <sys/mkdev.h> |
| 211 | #endif |
| 212 | #ifdef MAJOR_IN_SYSMACROS |
| 213 | #include <sys/sysmacros.h> |
| 214 | #endif |
| 215 | |
| 216 | #include <math.h> |
| 217 | #include <float.h> |
| 218 | |
| 219 | #include <zlib.h> |
| 220 | |
| 221 | #ifdef HAVE_CAPABILITY |
| 222 | #include <sys/capability.h> |
| 223 | #endif |
| 224 | |
| 225 | #define XXH_INLINE_ALL |
| 226 | #include "libnetdata/xxHash/xxhash.h" |
| 227 | |
| 228 | // -------------------------------------------------------------------------------------------------------------------- |
| 229 | // OpenSSL |
| 230 | |
| 231 | #define OPENSSL_VERSION_095 0x00905100L |
| 232 | #define OPENSSL_VERSION_097 0x0907000L |
| 233 | #define OPENSSL_VERSION_110 0x10100000L |
| 234 | #define OPENSSL_VERSION_111 0x10101000L |
| 235 | #define OPENSSL_VERSION_300 0x30000000L |
| 236 | |
| 237 | #include <openssl/ssl.h> |
| 238 | #include <openssl/rand.h> |
| 239 | #include <openssl/err.h> |
| 240 | #include <openssl/evp.h> |
| 241 | #include <openssl/pem.h> |
| 242 | #if (SSLEAY_VERSION_NUMBER >= OPENSSL_VERSION_097) && (OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110) |
| 243 | #include <openssl/conf.h> |
| 244 | #endif |
| 245 | |
| 246 | #if OPENSSL_VERSION_NUMBER >= OPENSSL_VERSION_300 |
| 247 | #include <openssl/core_names.h> |
| 248 | #include <openssl/decoder.h> |
| 249 | #endif |
| 250 | |
| 251 | // -------------------------------------------------------------------------------------------------------------------- |
| 252 | |
| 253 | #ifndef O_CLOEXEC |
| 254 | #define O_CLOEXEC (0) |
| 255 | #endif |
| 256 | |
| 257 | // -------------------------------------------------------------------------------------------------------------------- |
| 258 | // fix for alpine linux |
| 259 | |
| 260 | #if !defined(RUSAGE_THREAD) && defined(RUSAGE_CHILDREN) |
| 261 | #define RUSAGE_THREAD RUSAGE_CHILDREN |
| 262 | #endif |
| 263 | |
| 264 | // -------------------------------------------------------------------------------------------------------------------- |
| 265 | // NETDATA CLOUD |
| 266 | |
| 267 | // BEWARE: this exists in alarm-notify.sh |
| 268 | #define DEFAULT_CLOUD_BASE_URL "https://app.netdata.cloud" |
| 269 | |
| 270 | // -------------------------------------------------------------------------------------------------------------------- |
| 271 | // DBENGINE |
| 272 | |
| 273 | #define RRD_STORAGE_TIERS 5 |
| 274 | |
| 275 | // -------------------------------------------------------------------------------------------------------------------- |
| 276 | // UUIDs |
| 277 | |
| 278 | #define GUID_LEN 36 |
| 279 | |
| 280 | // -------------------------------------------------------------------------------------------------------------------- |
| 281 | // Macro-only includes |
| 282 | |
| 283 | #include "libnetdata/linked_lists/linked_lists.h" |
| 284 | |
| 285 | // -------------------------------------------------------------------------------------------------------------------- |
| 286 | |
| 287 | #define FUNCTION_RUN_ONCE() { \ |
| 288 | static bool __run_once = false; \ |
| 289 | if (!__sync_bool_compare_and_swap(&__run_once, false, true)) { \ |
| 290 | return; \ |
| 291 | } \ |
| 292 | } |
| 293 | |
| 294 | #define FUNCTION_RUN_ONCE_RET(ret) { \ |
| 295 | static bool __run_once = false; \ |
| 296 | if (!__sync_bool_compare_and_swap(&__run_once, false, true)) { \ |
| 297 | return (ret); \ |
| 298 | } \ |
| 299 | } |
| 300 | |
| 301 | // -------------------------------------------------------------------------------------------------------------------- |
| 302 | |
| 303 | #ifndef HOST_NAME_MAX |
| 304 | #define HOST_NAME_MAX 256 |
| 305 | #endif |
| 306 | |
| 307 | // -------------------------------------------------------------------------------------------------------------------- |
| 308 | |
| 309 | #if defined(OS_WINDOWS) |
| 310 | #include <windows.h> |
| 311 | #include <wctype.h> |
| 312 | #include <wchar.h> |
| 313 | #include <tchar.h> |
| 314 | #include <guiddef.h> |
| 315 | #include <io.h> |
| 316 | #include <fcntl.h> |
| 317 | #include <process.h> |
| 318 | #include <tlhelp32.h> |
| 319 | #include <sys/cygwin.h> |
| 320 | #include <winevt.h> |
| 321 | #include <evntprov.h> |
| 322 | #include <wbemidl.h> |
| 323 | #include <sddl.h> |
| 324 | // #include <winternl.h> // conflicts on STRING, |
| 325 | |
| 326 | // wincrypt.h (included via windows.h) defines macros that conflict with OpenSSL |
| 327 | #ifdef X509_NAME |
| 328 | #undef X509_NAME |
| 329 | #endif |
| 330 | #ifdef X509_EXTENSIONS |
| 331 | #undef X509_EXTENSIONS |
| 332 | #endif |
| 333 | #ifdef PKCS7_SIGNER_INFO |
| 334 | #undef PKCS7_SIGNER_INFO |
| 335 | #endif |
| 336 | #ifdef OCSP_REQUEST |
| 337 | #undef OCSP_REQUEST |
| 338 | #endif |
| 339 | #ifdef OCSP_RESPONSE |
| 340 | #undef OCSP_RESPONSE |
| 341 | #endif |
| 342 | #endif |
| 343 | |
| 344 | // -------------------------------------------------------------------------------------------------------------------- |
| 345 | |
| 346 | /* Define a portable way to access st_mtim across Unix variants */ |
| 347 | #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L |
| 348 | /* POSIX.1-2008 compliant systems have st_mtim */ |
| 349 | #define STAT_GET_MTIME_SEC(st) ((st).st_mtim.tv_sec) |
| 350 | #define STAT_GET_MTIME_NSEC(st) ((st).st_mtim.tv_nsec) |
| 351 | #elif defined(__APPLE__) || defined(__darwin__) || defined(__MACH__) |
| 352 | /* macOS has st_mtimespec */ |
| 353 | #define STAT_GET_MTIME_SEC(st) ((st).st_mtimespec.tv_sec) |
| 354 | #define STAT_GET_MTIME_NSEC(st) ((st).st_mtimespec.tv_nsec) |
| 355 | #elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) |
| 356 | /* BSD systems typically have st_mtim or provide a compatibility layer */ |
| 357 | #define STAT_GET_MTIME_SEC(st) ((st).st_mtim.tv_sec) |
| 358 | #define STAT_GET_MTIME_NSEC(st) ((st).st_mtim.tv_nsec) |
| 359 | #else |
| 360 | /* Fallback for systems with only second precision */ |
| 361 | #define STAT_GET_MTIME_SEC(st) ((st).st_mtime) |
| 362 | #define STAT_GET_MTIME_NSEC(st) (0) |
| 363 | #endif |
| 364 | |
| 365 | # ifdef __cplusplus |
| 366 | } |
| 367 | # endif |
| 368 | |
| 369 | #endif //LIBNETDATA_COMMON_H |