Module: ChildProcess

Defined in:
lib/childprocess.rb,
lib/childprocess/unix.rb,
lib/childprocess/errors.rb,
lib/childprocess/unix/io.rb,
lib/childprocess/version.rb,
lib/childprocess/windows.rb,
lib/childprocess/jruby/io.rb,
lib/childprocess/unix/lib.rb,
lib/childprocess/jruby/pump.rb,
lib/childprocess/windows/io.rb,
lib/childprocess/abstract_io.rb,
lib/childprocess/windows/lib.rb,
lib/childprocess/unix/process.rb,
lib/childprocess/jruby/process.rb,
lib/childprocess/windows/handle.rb,
lib/childprocess/tools/generator.rb,
lib/childprocess/windows/process.rb,
lib/childprocess/windows/structs.rb,
lib/childprocess/abstract_process.rb,
lib/childprocess/unix/fork_exec_process.rb,
lib/childprocess/windows/process_builder.rb,
lib/childprocess/unix/posix_spawn_process.rb,
lib/childprocess/jruby.rb

Defined Under Namespace

Modules: JRuby, Tools, Unix, Windows Classes: AbstractIO, AbstractProcess, Error, InvalidEnvironmentVariable, LaunchError, MissingPlatformError, SubclassResponsibility, TimeoutError

Constant Summary collapse

VERSION =
'0.6.1'

Class Method Summary collapse

Class Method Details

.archObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/childprocess.rb', line 114

def arch
  @arch ||= (
    host_cpu = RbConfig::CONFIG['host_cpu'].downcase
    case host_cpu
    when /i[3456]86/

      # Ruby 2.4 unifies Bignum and Fixnum into Integer. Also, I've heard that Darwin no
      # longer has this issue, so on newer Ruby/Darwin combos this check shouldn't be
      # needed anyway. Leaving it here for older combinations, however.

      # Check for Ruby older version of Ruby
      if (RUBY_VERSION =~ /^[123]\./) && (os == :macosx) && (0xfee1deadbeef.is_a?(Fixnum))
        # Darwin always reports i686, even when running in 64bit mod
        "x86_64"
      else
        "i386"
      end
    when /amd64|x86_64/
      "x86_64"
    when /ppc|powerpc/
      "powerpc"
    else
      host_cpu
    end
  )
end

.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.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/childprocess.rb', line 147

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

.jruby?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/childprocess.rb', line 52

def jruby?
  platform == :jruby
end

.linux?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/childprocess.rb', line 48

def linux?
  os == :linux
end

.new(*args) ⇒ Object Also known as: build



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/childprocess.rb', line 12

def new(*args)
  case os
  when :macosx, :linux, :solaris, :bsd, :cygwin, :aix
    if posix_spawn?
      Unix::PosixSpawnProcess.new(args)
    elsif jruby?
      JRuby::Process.new(args)
    else
      Unix::ForkExecProcess.new(args)
    end
  when :windows
    Windows::Process.new(args)
  else
    raise Error, "unsupported platform #{platform_name.inspect}"
  end
end

.osObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/childprocess.rb', line 88

def os
  @os ||= (
    require "rbconfig"
    host_os = RbConfig::CONFIG['host_os'].downcase

    case host_os
    when /linux/
      :linux
    when /darwin|mac os/
      :macosx
    when /mswin|msys|mingw32/
      :windows
    when /cygwin/
      :cygwin
    when /solaris|sunos/
      :solaris
    when /bsd/
      :bsd
    when /aix/
      :aix
    else
      raise Error, "unknown os: #{host_os.inspect}"
    end
  )
end

.platformObject



30
31
32
33
34
35
36
37
38
# File 'lib/childprocess.rb', line 30

def platform
  if RUBY_PLATFORM == "java"
    :jruby
  elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == "ironruby"
    :ironruby
  else
    os
  end
end

.platform_nameObject



40
41
42
# File 'lib/childprocess.rb', line 40

def platform_name
  @platform_name ||= "#{arch}-#{os}"
end

.posix_spawn=(bool) ⇒ Object

Set this to true to enable experimental use of posix_spawn.



84
85
86
# File 'lib/childprocess.rb', line 84

def posix_spawn=(bool)
  @posix_spawn = bool
end

.posix_spawn?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/childprocess.rb', line 60

def posix_spawn?
  enabled = @posix_spawn || %w[1 true].include?(ENV['CHILDPROCESS_POSIX_SPAWN'])
  return false unless enabled

  require 'ffi'
  begin
    require "childprocess/unix/platform/#{ChildProcess.platform_name}"
  rescue LoadError
    raise ChildProcess::MissingPlatformError
  end

  require "childprocess/unix/lib"
  require 'childprocess/unix/posix_spawn_process'

  true
rescue ChildProcess::MissingPlatformError => ex
  warn_once ex.message
  false
end

.unix?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/childprocess.rb', line 44

def unix?
  !windows?
end

.windows?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/childprocess.rb', line 56

def windows?
  os == :windows
end