libnetdata.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_LIB_H
  3. #define NETDATA_LIB_H 1
  4. # ifdef __cplusplus
  5. extern "C" {
  6. # endif
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #define OS_LINUX 1
  11. #define OS_FREEBSD 2
  12. #define OS_MACOS 3
  13. // ----------------------------------------------------------------------------
  14. // system include files for all netdata C programs
  15. /* select the memory allocator, based on autoconf findings */
  16. #if defined(ENABLE_JEMALLOC)
  17. #if defined(HAVE_JEMALLOC_JEMALLOC_H)
  18. #include <jemalloc/jemalloc.h>
  19. #else // !defined(HAVE_JEMALLOC_JEMALLOC_H)
  20. #include <malloc.h>
  21. #endif // !defined(HAVE_JEMALLOC_JEMALLOC_H)
  22. #elif defined(ENABLE_TCMALLOC)
  23. #include <google/tcmalloc.h>
  24. #else /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */
  25. #if !(defined(__FreeBSD__) || defined(__APPLE__))
  26. #include <malloc.h>
  27. #endif /* __FreeBSD__ || __APPLE__ */
  28. #endif /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */
  29. // ----------------------------------------------------------------------------
  30. #if defined(__FreeBSD__)
  31. #include <pthread_np.h>
  32. #define NETDATA_OS_TYPE "freebsd"
  33. #elif defined(__APPLE__)
  34. #define NETDATA_OS_TYPE "macos"
  35. #else
  36. #define NETDATA_OS_TYPE "linux"
  37. #endif /* __FreeBSD__, __APPLE__*/
  38. #include <pthread.h>
  39. #include <errno.h>
  40. #include <stdbool.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <stdarg.h>
  44. #include <stddef.h>
  45. #include <ctype.h>
  46. #include <string.h>
  47. #include <strings.h>
  48. #include <arpa/inet.h>
  49. #include <netinet/tcp.h>
  50. #include <sys/ioctl.h>
  51. #include <libgen.h>
  52. #include <dirent.h>
  53. #include <fcntl.h>
  54. #include <getopt.h>
  55. #include <grp.h>
  56. #include <pwd.h>
  57. #include <limits.h>
  58. #include <locale.h>
  59. #include <net/if.h>
  60. #include <poll.h>
  61. #include <signal.h>
  62. #include <syslog.h>
  63. #include <sys/mman.h>
  64. #include <sys/resource.h>
  65. #include <sys/socket.h>
  66. #include <sys/syscall.h>
  67. #include <sys/time.h>
  68. #include <sys/types.h>
  69. #include <sys/wait.h>
  70. #include <sys/un.h>
  71. #include <time.h>
  72. #include <unistd.h>
  73. #include <uuid/uuid.h>
  74. #include <spawn.h>
  75. #include <uv.h>
  76. #include <assert.h>
  77. // CentOS 7 has older version that doesn't define this
  78. // same goes for MacOS
  79. #ifndef UUID_STR_LEN
  80. #define UUID_STR_LEN (37)
  81. #endif
  82. #ifdef HAVE_NETINET_IN_H
  83. #include <netinet/in.h>
  84. #endif
  85. #ifdef HAVE_RESOLV_H
  86. #include <resolv.h>
  87. #endif
  88. #ifdef HAVE_NETDB_H
  89. #include <netdb.h>
  90. #endif
  91. #ifdef HAVE_SYS_PRCTL_H
  92. #include <sys/prctl.h>
  93. #endif
  94. #ifdef HAVE_SYS_STAT_H
  95. #include <sys/stat.h>
  96. #endif
  97. #ifdef HAVE_SYS_VFS_H
  98. #include <sys/vfs.h>
  99. #endif
  100. #ifdef HAVE_SYS_STATFS_H
  101. #include <sys/statfs.h>
  102. #endif
  103. #ifdef HAVE_LINUX_MAGIC_H
  104. #include <linux/magic.h>
  105. #endif
  106. #ifdef HAVE_SYS_MOUNT_H
  107. #include <sys/mount.h>
  108. #endif
  109. #ifdef HAVE_SYS_STATVFS_H
  110. #include <sys/statvfs.h>
  111. #endif
  112. // #1408
  113. #ifdef MAJOR_IN_MKDEV
  114. #include <sys/mkdev.h>
  115. #endif
  116. #ifdef MAJOR_IN_SYSMACROS
  117. #include <sys/sysmacros.h>
  118. #endif
  119. #ifdef STORAGE_WITH_MATH
  120. #include <math.h>
  121. #include <float.h>
  122. #endif
  123. #if defined(HAVE_INTTYPES_H)
  124. #include <inttypes.h>
  125. #elif defined(HAVE_STDINT_H)
  126. #include <stdint.h>
  127. #endif
  128. #ifdef NETDATA_WITH_ZLIB
  129. #include <zlib.h>
  130. #endif
  131. #ifdef HAVE_CAPABILITY
  132. #include <sys/capability.h>
  133. #endif
  134. // ----------------------------------------------------------------------------
  135. // netdata common definitions
  136. #ifdef __GNUC__
  137. #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  138. #endif // __GNUC__
  139. #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
  140. #define NEVERNULL __attribute__((returns_nonnull))
  141. #else
  142. #define NEVERNULL
  143. #endif
  144. #ifdef HAVE_FUNC_ATTRIBUTE_NOINLINE
  145. #define NOINLINE __attribute__((noinline))
  146. #else
  147. #define NOINLINE
  148. #endif
  149. #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
  150. #define MALLOCLIKE __attribute__((malloc))
  151. #else
  152. #define MALLOCLIKE
  153. #endif
  154. #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
  155. #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
  156. #else
  157. #define PRINTFLIKE(f, a)
  158. #endif
  159. #ifdef HAVE_FUNC_ATTRIBUTE_NORETURN
  160. #define NORETURN __attribute__ ((noreturn))
  161. #else
  162. #define NORETURN
  163. #endif
  164. #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
  165. #define WARNUNUSED __attribute__ ((warn_unused_result))
  166. #else
  167. #define WARNUNUSED
  168. #endif
  169. #define ABS(x) (((x) < 0)? (-(x)) : (x))
  170. #define MIN(a,b) (((a)<(b))?(a):(b))
  171. #define MAX(a,b) (((a)>(b))?(a):(b))
  172. #define GUID_LEN 36
  173. extern void netdata_fix_chart_id(char *s);
  174. extern void netdata_fix_chart_name(char *s);
  175. extern void strreverse(char* begin, char* end);
  176. extern char *mystrsep(char **ptr, char *s);
  177. extern char *trim(char *s); // remove leading and trailing spaces; may return NULL
  178. extern char *trim_all(char *buffer); // like trim(), but also remove duplicate spaces inside the string; may return NULL
  179. extern int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args);
  180. extern int snprintfz(char *dst, size_t n, const char *fmt, ...) PRINTFLIKE(3, 4);
  181. // memory allocation functions that handle failures
  182. #ifdef NETDATA_LOG_ALLOCATIONS
  183. extern __thread size_t log_thread_memory_allocations;
  184. #define strdupz(s) strdupz_int(__FILE__, __FUNCTION__, __LINE__, s)
  185. #define callocz(nmemb, size) callocz_int(__FILE__, __FUNCTION__, __LINE__, nmemb, size)
  186. #define mallocz(size) mallocz_int(__FILE__, __FUNCTION__, __LINE__, size)
  187. #define reallocz(ptr, size) reallocz_int(__FILE__, __FUNCTION__, __LINE__, ptr, size)
  188. #define freez(ptr) freez_int(__FILE__, __FUNCTION__, __LINE__, ptr)
  189. #define log_allocations() log_allocations_int(__FILE__, __FUNCTION__, __LINE__)
  190. extern char *strdupz_int(const char *file, const char *function, const unsigned long line, const char *s);
  191. extern void *callocz_int(const char *file, const char *function, const unsigned long line, size_t nmemb, size_t size);
  192. extern void *mallocz_int(const char *file, const char *function, const unsigned long line, size_t size);
  193. extern void *reallocz_int(const char *file, const char *function, const unsigned long line, void *ptr, size_t size);
  194. extern void freez_int(const char *file, const char *function, const unsigned long line, void *ptr);
  195. extern void log_allocations_int(const char *file, const char *function, const unsigned long line);
  196. #else // NETDATA_LOG_ALLOCATIONS
  197. extern char *strdupz(const char *s) MALLOCLIKE NEVERNULL;
  198. extern void *callocz(size_t nmemb, size_t size) MALLOCLIKE NEVERNULL;
  199. extern void *mallocz(size_t size) MALLOCLIKE NEVERNULL;
  200. extern void *reallocz(void *ptr, size_t size) MALLOCLIKE NEVERNULL;
  201. extern void freez(void *ptr);
  202. #endif // NETDATA_LOG_ALLOCATIONS
  203. extern void json_escape_string(char *dst, const char *src, size_t size);
  204. extern void json_fix_string(char *s);
  205. extern void *netdata_mmap(const char *filename, size_t size, int flags, int ksm);
  206. extern int memory_file_save(const char *filename, void *mem, size_t size);
  207. extern int fd_is_valid(int fd);
  208. extern struct rlimit rlimit_nofile;
  209. extern int enable_ksm;
  210. extern char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len);
  211. extern int verify_netdata_host_prefix();
  212. extern int recursively_delete_dir(const char *path, const char *reason);
  213. extern volatile sig_atomic_t netdata_exit;
  214. extern const char *program_version;
  215. extern char *strdupz_path_subpath(const char *path, const char *subpath);
  216. extern int path_is_dir(const char *path, const char *subpath);
  217. extern int path_is_file(const char *path, const char *subpath);
  218. extern void recursive_config_double_dir_load(
  219. const char *user_path
  220. , const char *stock_path
  221. , const char *subpath
  222. , int (*callback)(const char *filename, void *data)
  223. , void *data
  224. , size_t depth
  225. );
  226. extern char *read_by_filename(char *filename, long *file_size);
  227. extern char *find_and_replace(const char *src, const char *find, const char *replace, const char *where);
  228. /* fix for alpine linux */
  229. #ifndef RUSAGE_THREAD
  230. #ifdef RUSAGE_CHILDREN
  231. #define RUSAGE_THREAD RUSAGE_CHILDREN
  232. #endif
  233. #endif
  234. #define BITS_IN_A_KILOBIT 1000
  235. #define KILOBITS_IN_A_MEGABIT 1000
  236. /* misc. */
  237. #define UNUSED(x) (void)(x)
  238. #ifdef __GNUC__
  239. #define UNUSED_FUNCTION(x) __attribute__((unused)) UNUSED_##x
  240. #else
  241. #define UNUSED_FUNCTION(x) UNUSED_##x
  242. #endif
  243. #define error_report(x, args...) do { errno = 0; error(x, ##args); } while(0)
  244. // Taken from linux kernel
  245. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  246. typedef struct bitmap256 {
  247. uint64_t data[4];
  248. } BITMAP256;
  249. extern bool bitmap256_get_bit(BITMAP256 *ptr, uint8_t idx);
  250. extern void bitmap256_set_bit(BITMAP256 *ptr, uint8_t idx, bool value);
  251. extern void netdata_cleanup_and_exit(int ret) NORETURN;
  252. extern void send_statistics(const char *action, const char *action_result, const char *action_data);
  253. extern char *netdata_configured_host_prefix;
  254. #include "os.h"
  255. #include "storage_number/storage_number.h"
  256. #include "threads/threads.h"
  257. #include "buffer/buffer.h"
  258. #include "locks/locks.h"
  259. #include "circular_buffer/circular_buffer.h"
  260. #include "avl/avl.h"
  261. #include "inlined.h"
  262. #include "clocks/clocks.h"
  263. #include "completion/completion.h"
  264. #include "popen/popen.h"
  265. #include "simple_pattern/simple_pattern.h"
  266. #ifdef ENABLE_HTTPS
  267. # include "socket/security.h"
  268. #endif
  269. #include "socket/socket.h"
  270. #include "config/appconfig.h"
  271. #include "log/log.h"
  272. #include "procfile/procfile.h"
  273. #include "dictionary/dictionary.h"
  274. #if defined(HAVE_LIBBPF) && !defined(__cplusplus)
  275. #include "ebpf/ebpf.h"
  276. #endif
  277. #include "eval/eval.h"
  278. #include "statistical/statistical.h"
  279. #include "adaptive_resortable_list/adaptive_resortable_list.h"
  280. #include "url/url.h"
  281. #include "json/json.h"
  282. #include "health/health.h"
  283. #include "string/utf8.h"
  284. #include "arrayalloc/arrayalloc.h"
  285. #include "onewayalloc/onewayalloc.h"
  286. #include "worker_utilization/worker_utilization.h"
  287. // BEWARE: Outside of the C code this also exists in alarm-notify.sh
  288. #define DEFAULT_CLOUD_BASE_URL "https://app.netdata.cloud"
  289. #define RRD_STORAGE_TIERS 5
  290. # ifdef __cplusplus
  291. }
  292. # endif
  293. #endif // NETDATA_LIB_H