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

Add O_CLOEXEC to open calls. #624

Merged
merged 1 commit into from
May 6, 2019
Merged

Conversation

adam-azarchs
Copy link
Contributor

This prevents file descriptors from leaking to child processes.

When compiled for older (pre-2.6.23) kernels which lack support for O_CLOEXEC, there is no change in behavior. With newer kernels, child processes will no longer inherit leveldb's file handles, which reduces the changes of accidentally corrupting the database.

Fixes #623

@pwnall
Copy link
Member

pwnall commented Sep 20, 2018

This change seems reasonable to me. Can you please add tests?

@adam-azarchs
Copy link
Contributor Author

Added a test as requested. Sorry about being lazy about it before. Before:

==== Test EnvPosixTest.TestCloseOnExec
Unexpected open file 3
File descriptor 3 is /tmp/leveldbtest-5046/close_on_exec_seq.txt
/mnt/home/adam.azarchs/leveldb/util/env_posix_test.cc:104: failed: 0 == 1

After:

==== Test EnvPosixTest.TestCloseOnExec
==== PASSED 2 tests

@adam-azarchs
Copy link
Contributor Author

Hm, seems that while it passes fine for ./env_posix_test, it fails when running through make (as it does in travis). Probably because make is leaking file descriptors to it as well. Will need to iterate on this a bit more.

@adam-azarchs
Copy link
Contributor Author

Fixed the error. It does seem somewhat concerning that travis considers the build to have passed even if a unit test fails like that, however.

@adam-azarchs
Copy link
Contributor Author

Sorry for the noise earlier. Been spoiled by for Go too long I guess. The PR is now stable. Please take a look and let me know if further changes are desired.

@adam-azarchs
Copy link
Contributor Author

Are there any further changes you'd like to see on this PR?

@adam-azarchs
Copy link
Contributor Author

Rebased to master to resolve conflicts.

@pwnall
Copy link
Member

pwnall commented Nov 3, 2018

@adam-azarchs Thank you for your work, and sorry for the slow response! This looks good, and I'll look at adopting it next time I have a chunk of time for LevelDB. I may try to use CMake for the O_CLOEXEC feature detection, but everything else looks good to me.

This prevents file descriptors from leaking to child processes.

When compiled for older (pre-2.6.23) kernels which lack support for
O_CLOEXEC there is no change in behavior.  With newer kernels, child
processes will no longer inherit leveldb's file handles, which
reduces the changes of accidentally corrupting the database.

Fixes google#623
@adam-azarchs
Copy link
Contributor Author

I've rebased to master and modified things to use cmake for the feature detection. Admittedly I don't use cmake often so I'm cargo culting that bit, but it seems to work.

@pwnall
Copy link
Member

pwnall commented Mar 6, 2019

@adam-azarchs Thank you very much for the revisions!

I expect us to get the Windows CI green over the next couple of days. I plan to look into this PR right after the CI becomes green.

@cmumford cmumford merged commit 75fceae into google:master May 6, 2019
cmumford added a commit that referenced this pull request May 6, 2019
PiperOrigin-RevId: 246903086
@adam-azarchs
Copy link
Contributor Author

This seems to have broken the travis build on mac. I don't have a mac to test on so it's a bit difficult for me to resolve this. It looks like those errors were being inadvertently ignored by travis, previously. I'm guessing the issue is that macs don't have a /proc/self/exe?

@pwnall
Copy link
Member

pwnall commented May 7, 2019

Macs don't have procfs, indeed. So, no /proc/self/exe or /proc/self/fd.

The bigger question is -- why did this PR pass CI in the first place. I think it's fair to say we were all relying on the Travis results here.

I'm trying to see if I can figure out a quick fix for this issue.

@pwnall
Copy link
Member

pwnall commented May 7, 2019

(continued from the previous comment, I accidentally hit Enter)

https://travis-ci.org/google/leveldb/jobs/497244562#L2789 shows that the test failed on Mac, as expected, but the failure didn't bubble up to the Travis status.

Good news: the travis config used by the PR had a bug that was fixed in 808e59e

So, I'll assume that the CI configuration problem was fixed, and we only have to deal with the broken test. This is a good example of the importance of getting that CI config right 😄

@@ -15,6 +15,11 @@
#cmakedefine01 HAVE_FULLFSYNC
#endif // !defined(HAVE_FULLFSYNC)

// Define to 1 if you have a definition for O_CLOEXEC in <fcntl.h>.
#if !defined(HAVE_O_CLOEXEC)
#cmakedefine01 HAVE_O_CLOEXEC
Copy link

@hebasto hebasto Jun 20, 2022

Choose a reason for hiding this comment

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

#cmakedefine01 always defines the HAVE_O_CLOEXEC macro:

Input file lines of the form #cmakedefine01 VAR will be replaced with either #define VAR 1 or #define VAR 0 similarly.

Therefore, it looks wrong to use #if defined(HAVE_O_CLOEXEC) in the code:

#if defined(HAVE_O_CLOEXEC)
and
#if defined(HAVE_O_CLOEXEC)

UPDATE: The latter has been fixed in 27dc99f.

UPDATE 2: Have just noticed that a fix has been suggested in #1028.

maochongxin pushed a commit to maochongxin/leveldb that referenced this pull request Jul 21, 2022
* Use EXPECT_DOUBLE_EQ when comparing doubles in tests.

Fixes google#623

* disable 'float-equal' warning
fanquake added a commit to bitcoin-core/leveldb-subtree that referenced this pull request Jul 27, 2022
1eeb1cb fix macro HAVE_O_CLOEXEC when O_CLOEXEC not found (emptyx.wong)

Pull request description:

  google#624 was [buggy](google#624 (comment)).

  One bug was fixed in google@27dc99f.

  google#1028 addresses another one.

  This PR cherry-picks from google#1028, and is an alternative to bitcoin/bitcoin#25463.

ACKs for top commit:
  fanquake:
    ACK 1eeb1cb - I think the likelyhood of this being merged upstream is quite low, so that shouldn't be a blocker in us merging it here.
  jarolrod:
    ACK 1eeb1cb

Tree-SHA512: 67a4e05372e71ba12abf84c633284afbaa54852b616fc86939231e72d5054d9f7973caad5a18944a84f0fdf924f404a35b50287d53a704c7d320f04068800ebf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Leaking file descriptors to child processes
4 participants