Reduce netdatacli size (#15024)
* Reduce netdatacli size * Terminate string * Dummy functions to handle building with NETDATA_INTERNAL_CHECKS * Formatting * Fix formatting / properly initialize args * Fix compilation errors where using NETDATA_TRACE_ALLOCATIONS
Stelios Fragkakis committed
May 12, 2023 at 09:58 UTC
89568295b62c65f848dbee013a0d6677a0aa24e9
2 files changed
+107
-3
Makefile.am
+2
-1
@@ -1126,7 +1126,8 @@ endif
1126
1127
NETDATACLI_FILES = \
1128
daemon/commands.h \
1129
- $(LIBNETDATA_FILES) \
1129
+ libnetdata/buffer/buffer.c \
1130
+ libnetdata/buffer/buffer.h \
1131
cli/cli.c \
1132
cli/cli.h \
1133
$(NULL)
cli/cli.c
+105
-2
@@ -1,7 +1,109 @@
1
// SPDX-License-Identifier: GPL-3.0-or-later
2
3
#include "cli.h"
4
-#include "libnetdata/required_dummies.h"
4
+
5
+void error_int(int is_collector __maybe_unused, const char *prefix __maybe_unused, const char *file __maybe_unused, const char *function __maybe_unused, const unsigned long line __maybe_unused, const char *fmt, ... ) {
6
+ FILE *fp = stderr;
7
+
8
+ va_list args;
9
+ va_start( args, fmt );
10
+ vfprintf(fp, fmt, args );
11
+ va_end( args );
12
+}
13
+
14
+#ifdef NETDATA_INTERNAL_CHECKS
15
+
16
+uint64_t debug_flags;
17
+
18
+void debug_int( const char *file __maybe_unused , const char *function __maybe_unused , const unsigned long line __maybe_unused, const char *fmt __maybe_unused, ... )
19
+{
20
+
21
+}
22
+
23
+void fatal_int( const char *file __maybe_unused, const char *function __maybe_unused, const unsigned long line __maybe_unused, const char *fmt __maybe_unused, ... )
24
+{
25
+ abort();
26
+};
27
+#endif
28
+
29
+#ifdef NETDATA_TRACE_ALLOCATIONS
30
+void *callocz_int(size_t nmemb, size_t size, const char *file __maybe_unused, const char *function __maybe_unused, size_t line __maybe_unused)
31
+{
32
+ void *p = calloc(nmemb, size);
33
+ if (unlikely(!p)) {
34
+ error("Cannot allocate %zu bytes of memory.", nmemb * size);
35
+ exit(1);
36
+ }
37
+ return p;
38
+}
39
+
40
+void *mallocz_int(size_t size, const char *file __maybe_unused, const char *function __maybe_unused, size_t line __maybe_unused)
41
+{
42
+ void *p = malloc(size);
43
+ if (unlikely(!p)) {
44
+ error("Cannot allocate %zu bytes of memory.", size);
45
+ exit(1);
46
+ }
47
+ return p;
48
+}
49
+
50
+void *reallocz_int(void *ptr, size_t size, const char *file __maybe_unused, const char *function __maybe_unused, size_t line __maybe_unused)
51
+{
52
+ void *p = realloc(ptr, size);
53
+ if (unlikely(!p)) {
54
+ error("Cannot allocate %zu bytes of memory.", size);
55
+ exit(1);
56
+ }
57
+ return p;
58
+}
59
+
60
+void freez_int(void *ptr, const char *file __maybe_unused, const char *function __maybe_unused, size_t line __maybe_unused)
61
+{
62
+ free(ptr);
63
+}
64
+#else
65
+void freez(void *ptr) {
66
+ free(ptr);
67
+}
68
+
69
+void *mallocz(size_t size) {
70
+ void *p = malloc(size);
71
+ if (unlikely(!p)) {
72
+ error("Cannot allocate %zu bytes of memory.", size);
73
+ exit(1);
74
+ }
75
+ return p;
76
+}
77
+
78
+void *callocz(size_t nmemb, size_t size) {
79
+ void *p = calloc(nmemb, size);
80
+ if (unlikely(!p)) {
81
+ error("Cannot allocate %zu bytes of memory.", nmemb * size);
82
+ exit(1);
83
+ }
84
+ return p;
85
+}
86
+
87
+void *reallocz(void *ptr, size_t size) {
88
+ void *p = realloc(ptr, size);
89
+ if (unlikely(!p)) {
90
+ error("Cannot allocate %zu bytes of memory.", size);
91
+ exit(1);
92
+ }
93
+ return p;
94
+}
95
+#endif
96
+
97
+int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
98
+ if(unlikely(!n)) return 0;
99
+
100
+ int size = vsnprintf(dst, n, fmt, args);
101
+ dst[n - 1] = '\0';
102
+
103
+ if (unlikely((size_t) size > n)) size = (int)n;
104
+
105
+ return size;
106
+}
107
108
static uv_pipe_t client_pipe;
109
static uv_write_t write_req;
@@ -174,8 +276,9 @@ int main(int argc, char **argv)
276
size_t to_copy;
277
278
to_copy = MIN(strlen(argv[i]), MAX_COMMAND_LENGTH - 1 - command_string_size);
177
- strncpyz(command_string + command_string_size, argv[i], to_copy);
279
+ strncpy(command_string + command_string_size, argv[i], to_copy);
280
command_string_size += to_copy;
281
+ command_string[command_string_size] = '\0';
282
283
if (command_string_size < MAX_COMMAND_LENGTH - 1) {
284
command_string[command_string_size++] = ' ';