Skip to content

Commit

Permalink
Spawn server improvements 5 (netdata#18131)
Browse files Browse the repository at this point in the history
spawn server feedback is always sent from the spawn server itself
  • Loading branch information
ktsaou authored Jul 12, 2024
1 parent 078015b commit fa8c704
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 199 deletions.
23 changes: 17 additions & 6 deletions src/libnetdata/os/close_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,31 @@ static int compare_ints(const void *a, const void *b) {
return (int_a > int_b) - (int_a < int_b);
}

void os_close_all_non_std_open_fds_except(int fds[], size_t fds_num) {
void os_close_all_non_std_open_fds_except(const int fds[], size_t fds_num) {
if (fds_num == 0 || fds == NULL) {
os_close_range(STDERR_FILENO + 1, CLOSE_RANGE_FD_MAX);
return;
}

qsort(fds, fds_num, sizeof(int), compare_ints);
// copy the fds array to ensure we will not alter them
int fds_copy[fds_num];
memcpy(fds_copy, fds, sizeof(fds_copy));

qsort(fds_copy, fds_num, sizeof(int), compare_ints);

int start = STDERR_FILENO + 1;
for (size_t i = 0; i < fds_num; i++) {
if (fds[i] > start)
os_close_range(start, fds[i] - 1);
size_t i = 0;

// filter out all fds with a number smaller than our start
for (; i < fds_num; i++)
if(fds_copy[i] >= start) break;

// call os_close_range() as many times as needed
for (; i < fds_num; i++) {
if (fds_copy[i] > start)
os_close_range(start, fds_copy[i] - 1);

start = fds[i] + 1;
start = fds_copy[i] + 1;
}

os_close_range(start, CLOSE_RANGE_FD_MAX);
Expand Down
2 changes: 1 addition & 1 deletion src/libnetdata/os/close_range.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

int os_get_fd_open_max(void);
void os_close_range(int first, int last);
void os_close_all_non_std_open_fds_except(int fds[], size_t fds_num);
void os_close_all_non_std_open_fds_except(const int fds[], size_t fds_num);

#endif //CLOSE_RANGE_H
Loading

0 comments on commit fa8c704

Please sign in to comment.