@cryptotaxi247 / netdata-1 / commits / 2f3ec28b9

Add -W keepopenfds option. (#12211)

When this option is specified the agent will not close any inherited open FDs. This option is useful when starting the agent under profiling tools that maintain their own file descriptors, eg. to save profiling data.

vkalintiris committed Feb 22, 2022 at 13:51 UTC 2f3ec28b98f1a61aae7c58d1eeea31a425e1b1a7
1 file changed +14 -7
daemon/main.c
+14 -7
@@ -690,6 +690,7 @@ int main(int argc, char **argv) {
690 int i;
691 int config_loaded = 0;
692 int dont_fork = 0;
693 + bool close_open_fds = true;
694 size_t default_stacksize;
695 char *user = NULL;
696
@@ -1038,7 +1039,13 @@ int main(int argc, char **argv) {
1039 print_build_info_json();
1040 return 0;
1041 }
1041 - else {
1042 + else if(strcmp(optarg, "keepopenfds") == 0) {
1043 + // Internal dev option to skip closing inherited
1044 + // open FDs. Useful, when we want to run the agent
1045 + // under profiling tools that open/maintain their
1046 + // own FDs.
1047 + close_open_fds = false;
1048 + } else {
1049 fprintf(stderr, "Unknown -W parameter '%s'\n", optarg);
1050 return help(1);
1051 }
@@ -1053,12 +1060,12 @@ int main(int argc, char **argv) {
1060 }
1061
1062 #ifdef _SC_OPEN_MAX
1056 - // close all open file descriptors, except the standard ones
1057 - // the caller may have left open files (lxc-attach has this issue)
1058 - {
1059 - int fd;
1060 - for(fd = (int) (sysconf(_SC_OPEN_MAX) - 1); fd > 2; fd--)
1061 - if(fd_is_valid(fd)) close(fd);
1063 + if (close_open_fds == true) {
1064 + // close all open file descriptors, except the standard ones
1065 + // the caller may have left open files (lxc-attach has this issue)
1066 + for(int fd = (int) (sysconf(_SC_OPEN_MAX) - 1); fd > 2; fd--)
1067 + if(fd_is_valid(fd))
1068 + close(fd);
1069 }
1070 #endif
1071