Method: ChildProcess.close_on_exec

Defined in:
lib/childprocess.rb

.close_on_exec(file) ⇒ Object

By default, a child process will inherit open file descriptors from the parent process. This helper provides a cross-platform way of making sure that doesn’t happen for the given file/io.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/childprocess.rb', line 140

def close_on_exec(file)
  if file.respond_to?(:close_on_exec=)
    file.close_on_exec = true
  elsif file.respond_to?(:fcntl) && defined?(Fcntl::FD_CLOEXEC)
    file.fcntl Fcntl::F_SETFD, Fcntl::FD_CLOEXEC

    if jruby? && posix_spawn?
      # on JRuby, the fcntl call above apparently isn't enough when
      # we're launching the process through posix_spawn.
      fileno = JRuby.posix_fileno_for(file)
      Unix::Lib.fcntl fileno, Fcntl::F_SETFD, Fcntl::FD_CLOEXEC
    end
  elsif windows?
    Windows::Lib.dont_inherit file
  else
    raise Error, "not sure how to set close-on-exec for #{file.inspect} on #{platform_name.inspect}"
  end
end