Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fds close logic #4120

Merged
merged 2 commits into from Jan 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions libs/libdaemon/src/BaseDaemon.cpp
Expand Up @@ -788,23 +788,25 @@ void BaseDaemon::closeFDs()
#endif
if (proc_path.isDirectory()) /// Hooray, proc exists
{
Poco::DirectoryIterator itr(proc_path), end;
for (; itr != end; ++itr)
std::vector<std::string> fds;
/// in /proc/self/fd directory filenames are numeric file descriptors
proc_path.list(fds);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm as a temporary fix as this routine looks dodgy anyway :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix!

for (const auto & fd_str : fds)
{
long fd = DB::parse<long>(itr.name());
int fd = DB::parse<int>(fd_str);
if (fd > 2 && fd != signal_pipe.read_fd && fd != signal_pipe.write_fd)
::close(fd);
}
}
else
{
long max_fd = -1;
int max_fd = -1;
#ifdef _SC_OPEN_MAX
max_fd = sysconf(_SC_OPEN_MAX);
if (max_fd == -1)
#endif
max_fd = 256; /// bad fallback
for (long fd = 3; fd < max_fd; ++fd)
for (int fd = 3; fd < max_fd; ++fd)
if (fd != signal_pipe.read_fd && fd != signal_pipe.write_fd)
::close(fd);
}
Expand Down