Spawn server fixes 6 (#18136)
* spawn server reopens the log files after closing all sockets * close all sockets only when we run an external command * the spawn server disables all log methods except daemon and collectors
Costa Tsaousis committed
Jul 13, 2024 at 01:37 UTC
72c13bd405f2272a5b2643018818268bba1222e3
4 files changed
+44
-18
src/daemon/commands.c
+1
-1
@@ -164,7 +164,7 @@ static cmd_status_t cmd_reopen_logs_execute(char *args, char **message)
164
(void)message;
165
166
nd_log_limits_unlimited();
167
- nd_log_reopen_log_files();
167
+ nd_log_reopen_log_files(true);
168
nd_log_limits_reset();
169
170
return CMD_STATUS_SUCCESS;
src/libnetdata/log/log.c
+27
-3
@@ -992,14 +992,38 @@ void nd_log_initialize(void) {
992
nd_log_open(&nd_log.sources[i], i);
993
}
994
995
-void nd_log_reopen_log_files(void) {
996
- netdata_log_info("Reopening all log files.");
995
+void nd_log_reopen_log_files(bool log) {
996
+ if(log)
997
+ netdata_log_info("Reopening all log files.");
998
999
nd_log.std_output.initialized = false;
1000
nd_log.std_error.initialized = false;
1001
nd_log_initialize();
1002
1002
- netdata_log_info("Log files re-opened.");
1003
+ if(log)
1004
+ netdata_log_info("Log files re-opened.");
1005
+}
1006
+
1007
+void nd_log_reopen_log_files_for_spawn_server(void) {
1008
+ if(nd_log.syslog.initialized) {
1009
+ closelog();
1010
+ nd_log.syslog.initialized = false;
1011
+ nd_log_syslog_init();
1012
+ }
1013
+
1014
+ if(nd_log.journal_direct.initialized) {
1015
+ close(nd_log.journal_direct.fd);
1016
+ nd_log.journal_direct.fd = -1;
1017
+ nd_log.journal_direct.initialized = false;
1018
+ nd_log_journal_direct_init(NULL);
1019
+ }
1020
+
1021
+ nd_log.sources[NDLS_UNSET].method = NDLM_DISABLED;
1022
+ nd_log.sources[NDLS_ACCESS].method = NDLM_DISABLED;
1023
+ nd_log.sources[NDLS_ACLK].method = NDLM_DISABLED;
1024
+ nd_log.sources[NDLS_DEBUG].method = NDLM_DISABLED;
1025
+ nd_log.sources[NDLS_HEALTH].method = NDLM_DISABLED;
1026
+ nd_log_reopen_log_files(false);
1027
}
1028
1029
void chown_open_file(int fd, uid_t uid, gid_t gid) {
src/libnetdata/log/log.h
+2
-1
@@ -149,11 +149,12 @@ void nd_log_set_user_settings(ND_LOG_SOURCES source, const char *setting);
149
void nd_log_set_facility(const char *facility);
150
void nd_log_set_priority_level(const char *setting);
151
void nd_log_initialize(void);
152
-void nd_log_reopen_log_files(void);
152
+void nd_log_reopen_log_files(bool log);
153
void chown_open_file(int fd, uid_t uid, gid_t gid);
154
void nd_log_chown_log_files(uid_t uid, gid_t gid);
155
void nd_log_set_flood_protection(size_t logs, time_t period);
156
void nd_log_initialize_for_external_plugins(const char *name);
157
+void nd_log_reopen_log_files_for_spawn_server(void);
158
bool nd_log_journal_socket_available(void);
159
ND_LOG_FIELD_ID nd_log_field_id_by_name(const char *field, size_t len);
160
int nd_log_priority2id(const char *priority);
src/libnetdata/spawn_server/spawn_server.c
+14
-13
@@ -59,12 +59,14 @@ SPAWN_SERVER* spawn_server_create(SPAWN_SERVER_OPTIONS options __maybe_unused, c
59
SPAWN_SERVER* server = callocz(1, sizeof(SPAWN_SERVER));
60
if(name)
61
server->name = strdupz(name);
62
+ else
63
+ server->name = strdupz("unnamed");
64
return server;
65
}
66
67
void spawn_server_destroy(SPAWN_SERVER *server) {
68
if (server) {
67
- if(server->name) freez((void *)server->name);
69
+ freez((void *)server->name);
70
freez(server);
71
}
72
}
@@ -329,20 +331,13 @@ static void spawn_server_run_child(SPAWN_SERVER *server, SPAWN_REQUEST *rq) {
331
if(server->pipe[1] != -1) { close(server->pipe[1]); server->pipe[1] = -1; }
332
333
// set the process name
332
- {
333
- char buf[15];
334
- snprintfz(buf, sizeof(buf), "chld-%zu-r%zu", server->id, rq->request_id);
335
- os_setproctitle(buf, server->argc, server->argv);
336
- }
337
-
338
- // just a precausion in case we have any left-over fds
339
- os_close_all_non_std_open_fds_except(rq->fds, SPAWN_SERVER_TRANSFER_FDS);
334
+ os_setproctitle("spawn-child", server->argc, server->argv);
335
336
// get the fds from the request
337
int stdin_fd = rq->fds[0];
338
int stdout_fd = rq->fds[1];
339
int stderr_fd = rq->fds[2];
345
- int custom_fd = rq->fds[3];
340
+ int custom_fd = rq->fds[3]; (void)custom_fd;
341
342
// change stdio fds to the ones in the request
343
if (dup2(stdin_fd, STDIN_FILENO) == -1) {
@@ -376,11 +371,16 @@ static void spawn_server_run_child(SPAWN_SERVER *server, SPAWN_REQUEST *rq) {
371
switch (rq->type) {
372
373
case SPAWN_INSTANCE_TYPE_EXEC:
379
- if(custom_fd != -1) { close(custom_fd); custom_fd = -1; }
374
+ // close all fds except the ones we need
375
+ os_close_all_non_std_open_fds_except(NULL, 0);
376
+
377
+ // run the command
378
execvp(rq->argv[0], (char **)rq->argv);
379
+
380
nd_log(NDLS_COLLECTORS, NDLP_ERR,
381
"SPAWN SERVER: Failed to execute command of request No %zu: %s",
382
rq->request_id, rq->cmdline);
383
+
384
exit(1);
385
break;
386
@@ -1135,6 +1135,7 @@ static void spawn_server_event_loop(SPAWN_SERVER *server) {
1135
if (spawn_server_sigchld) {
1136
spawn_server_sigchld = false;
1137
spawn_server_process_sigchld();
1138
+ errno_clear();
1139
1140
if(ret == -1)
1141
continue;
@@ -1310,8 +1311,7 @@ SPAWN_SERVER* spawn_server_create(SPAWN_SERVER_OPTIONS options, const char *name
1311
snprintf(path, sizeof(path), "%s/.netdata-spawn-%s.sock", runtime_directory, name);
1312
}
1313
else {
1313
- snprintfz(path, sizeof(path), "%d-%zu", getpid(), server->id);
1314
- server->name = strdupz(path);
1314
+ server->name = strdupz("unnamed");
1315
snprintf(path, sizeof(path), "%s/.netdata-spawn-%d-%zu.sock", runtime_directory, getpid(), server->id);
1316
}
1317
@@ -1337,6 +1337,7 @@ SPAWN_SERVER* spawn_server_create(SPAWN_SERVER_OPTIONS options, const char *name
1337
1338
replace_stdio_with_dev_null();
1339
os_close_all_non_std_open_fds_except((int[]){ server->sock, server->pipe[1] }, 2);
1340
+ nd_log_reopen_log_files_for_spawn_server();
1341
spawn_server_event_loop(server);
1342
}
1343
else if (pid > 0) {