-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
conmon: store open FDs and close only them
now that we use a delay to call the cleanup program, we might end up in a race where the event fd used by glib is close'd and it causes the glib event handler to keep polling the closed file descriptor in a tight loop. To avoid closing files that are handled by glib, store what FDs are opened when conmon first started and close only them. Closes: #221 Signed-off-by: Giuseppe Scrivano <[email protected]>
- Loading branch information
Showing
7 changed files
with
100 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#define _GNU_SOURCE | ||
#if __STDC_VERSION__ >= 199901L | ||
/* C99 or later */ | ||
#else | ||
#error conmon.c requires C99 or later | ||
#endif | ||
|
||
#include "utils.h" | ||
#include "ctr_logging.h" | ||
#include "cgroup.h" | ||
#include "cli.h" | ||
#include "globals.h" | ||
#include "oom.h" | ||
#include "conn_sock.h" | ||
#include "ctrl.h" | ||
#include "ctr_stdio.h" | ||
#include "config.h" | ||
#include "parent_pipe_fd.h" | ||
#include "ctr_exit.h" | ||
#include "close_fds.h" | ||
#include "runtime_args.h" | ||
|
||
#include <sys/prctl.h> | ||
#include <sys/stat.h> | ||
|
||
static int open_files_max_fd; | ||
static fd_set *open_files_set; | ||
|
||
static void __attribute__((constructor)) init() | ||
{ | ||
struct dirent *ent; | ||
ssize_t size = 0; | ||
DIR *d; | ||
|
||
/* Store how many FDs were open before the Go runtime kicked in. */ | ||
d = opendir("/proc/self/fd"); | ||
if (!d) | ||
return; | ||
|
||
for (ent = readdir(d); ent; ent = readdir(d)) { | ||
int fd; | ||
|
||
if (ent->d_name[0] == '.') | ||
continue; | ||
|
||
fd = atoi(ent->d_name); | ||
if (fd == dirfd(d)) | ||
continue; | ||
|
||
if (fd >= size * FD_SETSIZE) { | ||
int i; | ||
ssize_t new_size; | ||
|
||
new_size = (fd / FD_SETSIZE) + 1; | ||
open_files_set = realloc(open_files_set, new_size * sizeof(fd_set)); | ||
if (open_files_set == NULL) | ||
_exit(EXIT_FAILURE); | ||
|
||
for (i = size; i < new_size; i++) | ||
FD_ZERO(&(open_files_set[i])); | ||
|
||
size = new_size; | ||
} | ||
|
||
if (fd > open_files_max_fd) | ||
open_files_max_fd = fd; | ||
|
||
FD_SET(fd % FD_SETSIZE, &(open_files_set[fd / FD_SETSIZE])); | ||
} | ||
closedir(d); | ||
} | ||
|
||
void close_other_fds() | ||
{ | ||
int fd; | ||
|
||
for (fd = 3; fd < open_files_max_fd; fd++) { | ||
if (open_files_set == NULL || FD_ISSET(fd % FD_SETSIZE, &(open_files_set[fd / FD_SETSIZE]))) | ||
if (fd == sync_pipe_fd || fd == attach_pipe_fd || fd == dev_null_r || fd == dev_null_w || fd == oom_cgroup_fd | ||
|| fd == oom_event_fd) | ||
close(fd); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
void close_other_fds(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters