@cryptotaxi247 / netdata-1 / commits / e99060f03

SPAWN SERVER: close all open fds on callback (#18764)

close all open fds on callback

Costa Tsaousis committed Oct 11, 2024 at 23:00 UTC e99060f0338010600457aeffc996d330c8cd808b
1 file changed +5 -2
src/libnetdata/spawn_server/spawn_server_nofork.c
+5 -2
@@ -57,6 +57,9 @@ static void spawn_server_run_child(SPAWN_SERVER *server, SPAWN_REQUEST *rq) {
57 if(server->pipe[0] != -1) { close(server->pipe[0]); server->pipe[0] = -1; }
58 if(server->pipe[1] != -1) { close(server->pipe[1]); server->pipe[1] = -1; }
59
60 + // close all open file descriptors of the parent, but keep ours
61 + os_close_all_non_std_open_fds_except(rq->fds, 4, 0);
62 +
63 // set the process name
64 os_setproctitle("spawn-child", server->argc, server->argv);
65
@@ -351,8 +354,6 @@ static bool spawn_server_run_callback(SPAWN_SERVER *server __maybe_unused, SPAWN
354 }
355
356 pid_t pid = fork();
354 - gettid_uncached(); // make sure the logger logs valid pids
355 -
357 if (pid < 0) {
358 // fork failed
359
@@ -362,6 +363,7 @@ static bool spawn_server_run_callback(SPAWN_SERVER *server __maybe_unused, SPAWN
363 else if (pid == 0) {
364 // the child
365
366 + gettid_uncached(); // make sure the logger logs valid pids
367 spawn_server_run_child(server, rq);
368 exit(63);
369 }
@@ -1068,6 +1070,7 @@ SPAWN_SERVER* spawn_server_create(SPAWN_SERVER_OPTIONS options, const char *name
1070 pid_t pid = fork();
1071 if (pid == 0) {
1072 // the child - the spawn server
1073 + gettid_uncached(); // make sure the logger logs valid pids
1074
1075 {
1076 char buf[15];