• W
    use atomic O_CLOEXEC when available (#4328) · d00e5de7
    Wez Furlong 提交于
    Summary:
    In our application we spawn helper child processes concurrently with
    opening rocksdb.  In one situation I observed that the child process had inherited
    the rocksdb lock file as well as directory handles to the rocksdb storage location.
    
    The code in env_posix takes care to set CLOEXEC but doesn't use `O_CLOEXEC` at the
    time that the files are opened which means that there is a window of opportunity
    to leak the descriptors across a fork/exec boundary.
    
    This diff introduces a helper that can conditionally set the `O_CLOEXEC` bit for
    the open call using the same logic as that in the existing helper for setting
    that flag post-open.
    
    I've preserved the post-open logic for systems that don't have `O_CLOEXEC`.
    
    I've introduced setting `O_CLOEXEC` for what appears to be a number of temporary
    or transient files and directory handles; I suspect that none of the files
    opened by Rocks are intended to be inherited by a forked child process.
    
    In one case, `fopen` is used to open a file.  I've added the use of the glibc-specific `e`
    mode to turn on `O_CLOEXEC` for this case.  While this doesn't cover all posix systems,
    it is an improvement for our common deployment system.
    Pull Request resolved: https://github.com/facebook/rocksdb/pull/4328
    
    Reviewed By: ajkr
    
    Differential Revision: D9553046
    
    Pulled By: wez
    
    fbshipit-source-id: acdb89f7a85ca649b22fe3c3bd76f82142bec2bf
    d00e5de7
env_posix.cc 34.0 KB