libnetdata.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_LIB_H
  3. #define NETDATA_LIB_H 1
  4. #ifdef HAVE_CONFIG_H
  5. #include <config.h>
  6. #endif
  7. #define OS_LINUX 1
  8. #define OS_FREEBSD 2
  9. #define OS_MACOS 3
  10. // ----------------------------------------------------------------------------
  11. // system include files for all netdata C programs
  12. /* select the memory allocator, based on autoconf findings */
  13. #if defined(ENABLE_JEMALLOC)
  14. #if defined(HAVE_JEMALLOC_JEMALLOC_H)
  15. #include <jemalloc/jemalloc.h>
  16. #else // !defined(HAVE_JEMALLOC_JEMALLOC_H)
  17. #include <malloc.h>
  18. #endif // !defined(HAVE_JEMALLOC_JEMALLOC_H)
  19. #elif defined(ENABLE_TCMALLOC)
  20. #include <google/tcmalloc.h>
  21. #else /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */
  22. #if !(defined(__FreeBSD__) || defined(__APPLE__))
  23. #include <malloc.h>
  24. #endif /* __FreeBSD__ || __APPLE__ */
  25. #endif /* !defined(ENABLE_JEMALLOC) && !defined(ENABLE_TCMALLOC) */
  26. // ----------------------------------------------------------------------------
  27. #if defined(__FreeBSD__)
  28. #include <pthread_np.h>
  29. #define NETDATA_OS_TYPE "freebsd"
  30. #elif defined(__APPLE__)
  31. #define NETDATA_OS_TYPE "macos"
  32. #else
  33. #define NETDATA_OS_TYPE "linux"
  34. #endif /* __FreeBSD__, __APPLE__*/
  35. #include <pthread.h>
  36. #include <errno.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <stdarg.h>
  40. #include <stddef.h>
  41. #include <ctype.h>
  42. #include <string.h>
  43. #include <strings.h>
  44. #include <arpa/inet.h>
  45. #include <netinet/tcp.h>
  46. #include <sys/ioctl.h>
  47. #include <libgen.h>
  48. #include <dirent.h>
  49. #include <fcntl.h>
  50. #include <getopt.h>
  51. #include <grp.h>
  52. #include <pwd.h>
  53. #include <locale.h>
  54. #include <net/if.h>
  55. #include <poll.h>
  56. #include <signal.h>
  57. #include <syslog.h>
  58. #include <sys/mman.h>
  59. #include <sys/resource.h>
  60. #include <sys/socket.h>
  61. #include <sys/syscall.h>
  62. #include <sys/time.h>
  63. #include <sys/types.h>
  64. #include <sys/wait.h>
  65. #include <sys/un.h>
  66. #include <time.h>
  67. #include <unistd.h>
  68. #include <uuid/uuid.h>
  69. #ifdef HAVE_NETINET_IN_H
  70. #include <netinet/in.h>
  71. #endif
  72. #ifdef HAVE_RESOLV_H
  73. #include <resolv.h>
  74. #endif
  75. #ifdef HAVE_NETDB_H
  76. #include <netdb.h>
  77. #endif
  78. #ifdef HAVE_SYS_PRCTL_H
  79. #include <sys/prctl.h>
  80. #endif
  81. #ifdef HAVE_SYS_STAT_H
  82. #include <sys/stat.h>
  83. #endif
  84. #ifdef HAVE_SYS_VFS_H
  85. #include <sys/vfs.h>
  86. #endif
  87. #ifdef HAVE_SYS_STATFS_H
  88. #include <sys/statfs.h>
  89. #endif
  90. #ifdef HAVE_SYS_MOUNT_H
  91. #include <sys/mount.h>
  92. #endif
  93. #ifdef HAVE_SYS_STATVFS_H
  94. #include <sys/statvfs.h>
  95. #endif
  96. // #1408
  97. #ifdef MAJOR_IN_MKDEV
  98. #include <sys/mkdev.h>
  99. #endif
  100. #ifdef MAJOR_IN_SYSMACROS
  101. #include <sys/sysmacros.h>
  102. #endif
  103. #ifdef STORAGE_WITH_MATH
  104. #include <math.h>
  105. #include <float.h>
  106. #endif
  107. #if defined(HAVE_INTTYPES_H)
  108. #include <inttypes.h>
  109. #elif defined(HAVE_STDINT_H)
  110. #include <stdint.h>
  111. #endif
  112. #ifdef NETDATA_WITH_ZLIB
  113. #include <zlib.h>
  114. #endif
  115. #ifdef HAVE_CAPABILITY
  116. #include <sys/capability.h>
  117. #endif
  118. // ----------------------------------------------------------------------------
  119. // netdata common definitions
  120. #if (SIZEOF_VOID_P == 8)
  121. #define ENVIRONMENT64
  122. #elif (SIZEOF_VOID_P == 4)
  123. #define ENVIRONMENT32
  124. #else
  125. #error "Cannot detect if this is a 32 or 64 bit CPU"
  126. #endif
  127. #ifdef __GNUC__
  128. #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  129. #endif // __GNUC__
  130. #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
  131. #define NEVERNULL __attribute__((returns_nonnull))
  132. #else
  133. #define NEVERNULL
  134. #endif
  135. #ifdef HAVE_FUNC_ATTRIBUTE_NOINLINE
  136. #define NOINLINE __attribute__((noinline))
  137. #else
  138. #define NOINLINE
  139. #endif
  140. #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
  141. #define MALLOCLIKE __attribute__((malloc))
  142. #else
  143. #define MALLOCLIKE
  144. #endif
  145. #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
  146. #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
  147. #else
  148. #define PRINTFLIKE(f, a)
  149. #endif
  150. #ifdef HAVE_FUNC_ATTRIBUTE_NORETURN
  151. #define NORETURN __attribute__ ((noreturn))
  152. #else
  153. #define NORETURN
  154. #endif
  155. #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
  156. #define WARNUNUSED __attribute__ ((warn_unused_result))
  157. #else
  158. #define WARNUNUSED
  159. #endif
  160. #ifdef abs
  161. #undef abs
  162. #endif
  163. #define abs(x) (((x) < 0)? (-(x)) : (x))
  164. #define GUID_LEN 36
  165. extern void netdata_fix_chart_id(char *s);
  166. extern void netdata_fix_chart_name(char *s);
  167. extern void strreverse(char* begin, char* end);
  168. extern char *mystrsep(char **ptr, char *s);
  169. extern char *trim(char *s); // remove leading and trailing spaces; may return NULL
  170. extern char *trim_all(char *buffer); // like trim(), but also remove duplicate spaces inside the string; may return NULL
  171. extern int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args);
  172. extern int snprintfz(char *dst, size_t n, const char *fmt, ...) PRINTFLIKE(3, 4);
  173. // memory allocation functions that handle failures
  174. #ifdef NETDATA_LOG_ALLOCATIONS
  175. extern __thread size_t log_thread_memory_allocations;
  176. #define strdupz(s) strdupz_int(__FILE__, __FUNCTION__, __LINE__, s)
  177. #define callocz(nmemb, size) callocz_int(__FILE__, __FUNCTION__, __LINE__, nmemb, size)
  178. #define mallocz(size) mallocz_int(__FILE__, __FUNCTION__, __LINE__, size)
  179. #define reallocz(ptr, size) reallocz_int(__FILE__, __FUNCTION__, __LINE__, ptr, size)
  180. #define freez(ptr) freez_int(__FILE__, __FUNCTION__, __LINE__, ptr)
  181. extern char *strdupz_int(const char *file, const char *function, const unsigned long line, const char *s);
  182. extern void *callocz_int(const char *file, const char *function, const unsigned long line, size_t nmemb, size_t size);
  183. extern void *mallocz_int(const char *file, const char *function, const unsigned long line, size_t size);
  184. extern void *reallocz_int(const char *file, const char *function, const unsigned long line, void *ptr, size_t size);
  185. extern void freez_int(const char *file, const char *function, const unsigned long line, void *ptr);
  186. #else // NETDATA_LOG_ALLOCATIONS
  187. extern char *strdupz(const char *s) MALLOCLIKE NEVERNULL;
  188. extern void *callocz(size_t nmemb, size_t size) MALLOCLIKE NEVERNULL;
  189. extern void *mallocz(size_t size) MALLOCLIKE NEVERNULL;
  190. extern void *reallocz(void *ptr, size_t size) MALLOCLIKE NEVERNULL;
  191. extern void freez(void *ptr);
  192. #endif // NETDATA_LOG_ALLOCATIONS
  193. extern void json_escape_string(char *dst, const char *src, size_t size);
  194. extern void json_fix_string(char *s);
  195. extern void *mymmap(const char *filename, size_t size, int flags, int ksm);
  196. extern int memory_file_save(const char *filename, void *mem, size_t size);
  197. extern int fd_is_valid(int fd);
  198. extern struct rlimit rlimit_nofile;
  199. extern int enable_ksm;
  200. extern char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len);
  201. extern int verify_netdata_host_prefix();
  202. extern int recursively_delete_dir(const char *path, const char *reason);
  203. extern volatile sig_atomic_t netdata_exit;
  204. extern const char *os_type;
  205. extern const char *program_version;
  206. extern char *strdupz_path_subpath(const char *path, const char *subpath);
  207. extern int path_is_dir(const char *path, const char *subpath);
  208. extern int path_is_file(const char *path, const char *subpath);
  209. extern void recursive_config_double_dir_load(
  210. const char *user_path
  211. , const char *stock_path
  212. , const char *subpath
  213. , int (*callback)(const char *filename, void *data)
  214. , void *data
  215. , size_t depth
  216. );
  217. /* fix for alpine linux */
  218. #ifndef RUSAGE_THREAD
  219. #ifdef RUSAGE_CHILDREN
  220. #define RUSAGE_THREAD RUSAGE_CHILDREN
  221. #endif
  222. #endif
  223. #define BITS_IN_A_KILOBIT 1000
  224. extern void netdata_cleanup_and_exit(int ret) NORETURN;
  225. extern char *netdata_configured_host_prefix;
  226. #include "os.h"
  227. #include "storage_number/storage_number.h"
  228. #include "threads/threads.h"
  229. #include "buffer/buffer.h"
  230. #include "locks/locks.h"
  231. #include "avl/avl.h"
  232. #include "inlined.h"
  233. #include "clocks/clocks.h"
  234. #include "popen/popen.h"
  235. #include "simple_pattern/simple_pattern.h"
  236. #include "socket/socket.h"
  237. #include "config/appconfig.h"
  238. #include "log/log.h"
  239. #include "procfile/procfile.h"
  240. #include "dictionary/dictionary.h"
  241. #include "eval/eval.h"
  242. #include "statistical/statistical.h"
  243. #include "adaptive_resortable_list/adaptive_resortable_list.h"
  244. #include "url/url.h"
  245. #endif // NETDATA_LIB_H