Minor changes when handling systemd integration. (#20006)
* Add statics and remove unused variable. * Add systemd_watcher_thread in static threads only when systemd is available. * Conditionaly include systemd units source file.
vkalintiris committed
Mar 31, 2025 at 19:07 UTC
66580aed1079f30b93aaf8ea9c8dc8ba2051e0ee
4 files changed
+31
-23
CMakeLists.txt
+18
-8
@@ -739,6 +739,11 @@ endif()
739
740
netdata_detect_protobuf()
741
742
+if(OS_LINUX)
743
+ include(NetdataDetectSystemd)
744
+ detect_systemd()
745
+endif()
746
+
747
#
748
# source files
749
#
@@ -1275,8 +1280,6 @@ set(DAEMON_FILES
1280
src/daemon/status-file.h
1281
src/daemon/config/netdata-conf-ssl.c
1282
src/daemon/config/netdata-conf-ssl.h
1278
- src/daemon/daemon-systemd-watcher.c
1279
- src/daemon/daemon-systemd-watcher.h
1283
src/daemon/machine-guid.c
1284
src/daemon/machine-guid.h
1285
src/daemon/status-file-dedup.c
@@ -1285,6 +1288,15 @@ set(DAEMON_FILES
1288
src/daemon/status-file-io.h
1289
)
1290
1291
+set(DAEMON_SYSTEMD_WATCHER_FILES
1292
+ src/daemon/daemon-systemd-watcher.c
1293
+ src/daemon/daemon-systemd-watcher.h
1294
+)
1295
+
1296
+if(ENABLE_SYSTEMD_DBUS)
1297
+ list(APPEND DAEMON_FILES ${DAEMON_SYSTEMD_WATCHER_FILES})
1298
+endif()
1299
+
1300
set(H2O_FILES
1301
src/web/server/h2o/http_server.c
1302
src/web/server/h2o/http_server.h
@@ -1692,7 +1704,6 @@ set(SYSTEMD_JOURNAL_PLUGIN_FILES
1704
src/collectors/systemd-journal.plugin/systemd-journal.c
1705
src/collectors/systemd-journal.plugin/systemd-internals.h
1706
src/collectors/systemd-journal.plugin/systemd-main.c
1695
- src/collectors/systemd-journal.plugin/systemd-units.c
1707
src/collectors/systemd-journal.plugin/systemd-journal.c
1708
src/collectors/systemd-journal.plugin/systemd-journal-annotations.c
1709
src/collectors/systemd-journal.plugin/systemd-journal-files.c
@@ -1703,6 +1714,10 @@ set(SYSTEMD_JOURNAL_PLUGIN_FILES
1714
src/collectors/systemd-journal.plugin/systemd-journal-sampling.h
1715
)
1716
1717
+if(ENABLE_SYSTEMD_DBUS)
1718
+ list(APPEND SYSTEMD_JOURNAL_PLUGIN_FILES src/collectors/systemd-journal.plugin/systemd-units.c)
1719
+endif()
1720
+
1721
set(STREAMING_PLUGIN_FILES
1722
src/streaming/stream.h
1723
src/streaming/stream-compression/compression.c
@@ -2164,11 +2179,6 @@ set_source_files_properties(JudyLTables.c PROPERTIES COMPILE_OPTIONS "-I${CMAKE_
2179
# build libnetdata
2180
#
2181
2167
-if(OS_LINUX)
2168
- include(NetdataDetectSystemd)
2169
- detect_systemd()
2170
-endif()
2171
-
2182
add_library(libnetdata STATIC ${LIBNETDATA_FILES})
2183
2184
target_include_directories(libnetdata BEFORE PUBLIC ${CONFIG_H_DIR} ${CMAKE_SOURCE_DIR}/src)
src/collectors/systemd-journal.plugin/systemd-units.c
+7
-9
@@ -49,7 +49,7 @@ static inline bool streq_ptr(const sd_char *a, const sd_char *b) {
49
return strcmp_ptr(a, b) == 0;
50
}
51
52
-ssize_t string_table_lookup(const char * const *table, size_t len, const char *key) {
52
+static ssize_t string_table_lookup(const char * const *table, size_t len, const char *key) {
53
if (!key || !*key)
54
return -EINVAL;
55
@@ -487,7 +487,7 @@ typedef void (*attribute_handler_t)(struct UnitInfo *u, UnitAttribute *ua);
487
488
static void update_freezer_state(struct UnitInfo *u, UnitAttribute *ua);
489
490
-struct {
490
+static const struct {
491
const char *member;
492
char value_type;
493
@@ -855,10 +855,8 @@ static int systemd_unit_get_all_properties(sd_bus *bus, UnitInfo *u) {
855
return r;
856
}
857
858
- int c = 0;
858
while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
859
const char *member, *contents;
861
- c++;
860
861
r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
862
if (r < 0) {
@@ -916,7 +914,7 @@ static void systemd_units_get_all_properties(sd_bus *bus, UnitInfo *base) {
914
// ----------------------------------------------------------------------------
915
// main unit info
916
919
-int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u) {
917
+static int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u) {
918
assert(message);
919
assert(u);
920
@@ -1105,7 +1103,7 @@ static UnitInfo *systemd_units_get_all(void) {
1103
return base;
1104
}
1105
1108
-void systemd_units_free_all(UnitInfo *base) {
1106
+static void systemd_units_free_all(UnitInfo *base) {
1107
while(base) {
1108
UnitInfo *u = base;
1109
DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(base, u, prev, next);
@@ -1227,7 +1225,7 @@ static inline FACET_ROW_SEVERITY if_normal(FACET_ROW_SEVERITY current, FACET_ROW
1225
return wanted;
1226
}
1227
1230
-FACET_ROW_SEVERITY system_unit_severity(UnitInfo *u) {
1228
+static FACET_ROW_SEVERITY system_unit_severity(UnitInfo *u) {
1229
FACET_ROW_SEVERITY severity, max_severity;
1230
1231
switch(u->UnitLoadState) {
@@ -1577,14 +1575,14 @@ FACET_ROW_SEVERITY system_unit_severity(UnitInfo *u) {
1575
return severity;
1576
}
1577
1580
-int unit_info_compar(const void *a, const void *b) {
1578
+static int unit_info_compar(const void *a, const void *b) {
1579
UnitInfo *u1 = *((UnitInfo **)a);
1580
UnitInfo *u2 = *((UnitInfo **)b);
1581
1582
return strcasecmp(u1->id, u2->id);
1583
}
1584
1587
-void systemd_units_assign_priority(UnitInfo *base) {
1585
+static void systemd_units_assign_priority(UnitInfo *base) {
1586
size_t units = 0, c = 0, prio = 0;
1587
for(UnitInfo *u = base; u ; u = u->next)
1588
units++;
src/daemon/daemon-systemd-watcher.c
-6
@@ -5,8 +5,6 @@
5
#include "daemon-service.h"
6
#include "daemon-shutdown.h"
7
8
-#ifdef ENABLE_SYSTEMD_DBUS
9
-
8
#include <systemd/sd-bus.h>
9
10
/* Callback function to handle the PrepareForShutdown signal.
@@ -137,16 +135,12 @@ finish:
135
sd_bus_unref(bus);
136
}
137
140
-#endif
141
-
138
void *systemd_watcher_thread(void *arg) {
139
struct netdata_static_thread *static_thread = arg;
140
141
service_register(SERVICE_THREAD_TYPE_NETDATA, NULL, NULL, NULL, false);
142
147
-#ifdef ENABLE_SYSTEMD_DBUS
143
listen_for_systemd_dbus_events();
149
-#endif
144
145
service_exits();
146
worker_unregister();
src/daemon/static_threads.c
+6
@@ -2,7 +2,10 @@
2
3
#include "common.h"
4
#include "web/api/queries/backfill.h"
5
+
6
+#ifdef ENABLE_SYSTEMD_DBUS
7
#include "daemon-systemd-watcher.h"
8
+#endif
9
10
void *aclk_main(void *ptr);
11
void *analytics_main(void *ptr);
@@ -202,6 +205,8 @@ const struct netdata_static_thread static_threads_common[] = {
205
.init_routine = NULL,
206
.start_routine = backfill_thread
207
},
208
+
209
+#ifdef ENABLE_SYSTEMD_DBUS
210
{
211
.name = "SDBUSWATCHER",
212
.config_section = NULL,
@@ -212,6 +217,7 @@ const struct netdata_static_thread static_threads_common[] = {
217
.init_routine = NULL,
218
.start_routine = systemd_watcher_thread
219
},
220
+#endif
221
222
// terminator
223
{