childprocess 0.8.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.document +6 -6
- data/.gitignore +28 -28
- data/.rspec +1 -1
- data/.travis.yml +42 -36
- data/CHANGELOG.md +67 -44
- data/Gemfile +18 -15
- data/LICENSE +20 -20
- data/README.md +216 -192
- data/Rakefile +61 -61
- data/appveyor.yml +42 -43
- data/childprocess.gemspec +32 -30
- data/ext/mkrf_conf.rb +24 -0
- data/lib/childprocess/abstract_io.rb +36 -36
- data/lib/childprocess/abstract_process.rb +192 -192
- data/lib/childprocess/errors.rb +37 -26
- data/lib/childprocess/jruby/io.rb +16 -16
- data/lib/childprocess/jruby/process.rb +184 -159
- data/lib/childprocess/jruby/pump.rb +53 -53
- data/lib/childprocess/jruby.rb +56 -56
- data/lib/childprocess/tools/generator.rb +145 -145
- data/lib/childprocess/unix/fork_exec_process.rb +78 -70
- data/lib/childprocess/unix/io.rb +21 -21
- data/lib/childprocess/unix/lib.rb +186 -186
- data/lib/childprocess/unix/platform/i386-linux.rb +12 -12
- data/lib/childprocess/unix/platform/i386-solaris.rb +11 -11
- data/lib/childprocess/unix/platform/x86_64-linux.rb +12 -12
- data/lib/childprocess/unix/platform/x86_64-macosx.rb +11 -11
- data/lib/childprocess/unix/posix_spawn_process.rb +134 -134
- data/lib/childprocess/unix/process.rb +90 -89
- data/lib/childprocess/unix.rb +9 -9
- data/lib/childprocess/version.rb +3 -3
- data/lib/childprocess/windows/handle.rb +91 -91
- data/lib/childprocess/windows/io.rb +25 -25
- data/lib/childprocess/windows/lib.rb +416 -416
- data/lib/childprocess/windows/process.rb +130 -130
- data/lib/childprocess/windows/process_builder.rb +178 -175
- data/lib/childprocess/windows/structs.rb +148 -148
- data/lib/childprocess/windows.rb +33 -33
- data/lib/childprocess.rb +210 -205
- data/spec/abstract_io_spec.rb +12 -12
- data/spec/childprocess_spec.rb +447 -391
- data/spec/get_env.ps1 +13 -0
- data/spec/io_spec.rb +228 -228
- data/spec/jruby_spec.rb +24 -24
- data/spec/pid_behavior.rb +12 -12
- data/spec/platform_detection_spec.rb +86 -86
- data/spec/spec_helper.rb +270 -261
- data/spec/unix_spec.rb +57 -57
- data/spec/windows_spec.rb +23 -23
- metadata +18 -33
data/lib/childprocess.rb
CHANGED
@@ -1,205 +1,210 @@
|
|
1
|
-
require 'childprocess/version'
|
2
|
-
require 'childprocess/errors'
|
3
|
-
require 'childprocess/abstract_process'
|
4
|
-
require 'childprocess/abstract_io'
|
5
|
-
require "fcntl"
|
6
|
-
require 'logger'
|
7
|
-
|
8
|
-
module ChildProcess
|
9
|
-
|
10
|
-
@posix_spawn = false
|
11
|
-
|
12
|
-
class << self
|
13
|
-
attr_writer :logger
|
14
|
-
|
15
|
-
def new(*args)
|
16
|
-
case os
|
17
|
-
when :macosx, :linux, :solaris, :bsd, :cygwin, :aix
|
18
|
-
if posix_spawn?
|
19
|
-
Unix::PosixSpawnProcess.new(args)
|
20
|
-
elsif jruby?
|
21
|
-
JRuby::Process.new(args)
|
22
|
-
else
|
23
|
-
Unix::ForkExecProcess.new(args)
|
24
|
-
end
|
25
|
-
when :windows
|
26
|
-
Windows::Process.new(args)
|
27
|
-
else
|
28
|
-
raise Error, "unsupported platform #{platform_name.inspect}"
|
29
|
-
end
|
30
|
-
end
|
31
|
-
alias_method :build, :new
|
32
|
-
|
33
|
-
def logger
|
34
|
-
return @logger if defined?(@logger) and @logger
|
35
|
-
|
36
|
-
@logger = Logger.new($stderr)
|
37
|
-
@logger.level = $DEBUG ? Logger::DEBUG : Logger::INFO
|
38
|
-
|
39
|
-
@logger
|
40
|
-
end
|
41
|
-
|
42
|
-
def platform
|
43
|
-
if RUBY_PLATFORM == "java"
|
44
|
-
:jruby
|
45
|
-
elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == "ironruby"
|
46
|
-
:ironruby
|
47
|
-
else
|
48
|
-
os
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def platform_name
|
53
|
-
@platform_name ||= "#{arch}-#{os}"
|
54
|
-
end
|
55
|
-
|
56
|
-
def unix?
|
57
|
-
!windows?
|
58
|
-
end
|
59
|
-
|
60
|
-
def linux?
|
61
|
-
os == :linux
|
62
|
-
end
|
63
|
-
|
64
|
-
def jruby?
|
65
|
-
platform == :jruby
|
66
|
-
end
|
67
|
-
|
68
|
-
def windows?
|
69
|
-
os == :windows
|
70
|
-
end
|
71
|
-
|
72
|
-
def posix_spawn?
|
73
|
-
enabled = @posix_spawn || %w[1 true].include?(ENV['CHILDPROCESS_POSIX_SPAWN'])
|
74
|
-
return false unless enabled
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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
|
-
|
113
|
-
|
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
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
#
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
#
|
194
|
-
def
|
195
|
-
|
196
|
-
end
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
1
|
+
require 'childprocess/version'
|
2
|
+
require 'childprocess/errors'
|
3
|
+
require 'childprocess/abstract_process'
|
4
|
+
require 'childprocess/abstract_io'
|
5
|
+
require "fcntl"
|
6
|
+
require 'logger'
|
7
|
+
|
8
|
+
module ChildProcess
|
9
|
+
|
10
|
+
@posix_spawn = false
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_writer :logger
|
14
|
+
|
15
|
+
def new(*args)
|
16
|
+
case os
|
17
|
+
when :macosx, :linux, :solaris, :bsd, :cygwin, :aix
|
18
|
+
if posix_spawn?
|
19
|
+
Unix::PosixSpawnProcess.new(args)
|
20
|
+
elsif jruby?
|
21
|
+
JRuby::Process.new(args)
|
22
|
+
else
|
23
|
+
Unix::ForkExecProcess.new(args)
|
24
|
+
end
|
25
|
+
when :windows
|
26
|
+
Windows::Process.new(args)
|
27
|
+
else
|
28
|
+
raise Error, "unsupported platform #{platform_name.inspect}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
alias_method :build, :new
|
32
|
+
|
33
|
+
def logger
|
34
|
+
return @logger if defined?(@logger) and @logger
|
35
|
+
|
36
|
+
@logger = Logger.new($stderr)
|
37
|
+
@logger.level = $DEBUG ? Logger::DEBUG : Logger::INFO
|
38
|
+
|
39
|
+
@logger
|
40
|
+
end
|
41
|
+
|
42
|
+
def platform
|
43
|
+
if RUBY_PLATFORM == "java"
|
44
|
+
:jruby
|
45
|
+
elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == "ironruby"
|
46
|
+
:ironruby
|
47
|
+
else
|
48
|
+
os
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def platform_name
|
53
|
+
@platform_name ||= "#{arch}-#{os}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def unix?
|
57
|
+
!windows?
|
58
|
+
end
|
59
|
+
|
60
|
+
def linux?
|
61
|
+
os == :linux
|
62
|
+
end
|
63
|
+
|
64
|
+
def jruby?
|
65
|
+
platform == :jruby
|
66
|
+
end
|
67
|
+
|
68
|
+
def windows?
|
69
|
+
os == :windows
|
70
|
+
end
|
71
|
+
|
72
|
+
def posix_spawn?
|
73
|
+
enabled = @posix_spawn || %w[1 true].include?(ENV['CHILDPROCESS_POSIX_SPAWN'])
|
74
|
+
return false unless enabled
|
75
|
+
|
76
|
+
begin
|
77
|
+
require 'ffi'
|
78
|
+
rescue LoadError
|
79
|
+
raise ChildProcess::MissingFFIError
|
80
|
+
end
|
81
|
+
|
82
|
+
begin
|
83
|
+
require "childprocess/unix/platform/#{ChildProcess.platform_name}"
|
84
|
+
rescue LoadError
|
85
|
+
raise ChildProcess::MissingPlatformError
|
86
|
+
end
|
87
|
+
|
88
|
+
require "childprocess/unix/lib"
|
89
|
+
require 'childprocess/unix/posix_spawn_process'
|
90
|
+
|
91
|
+
true
|
92
|
+
rescue ChildProcess::MissingPlatformError => ex
|
93
|
+
warn_once ex.message
|
94
|
+
false
|
95
|
+
end
|
96
|
+
|
97
|
+
#
|
98
|
+
# Set this to true to enable experimental use of posix_spawn.
|
99
|
+
#
|
100
|
+
|
101
|
+
def posix_spawn=(bool)
|
102
|
+
@posix_spawn = bool
|
103
|
+
end
|
104
|
+
|
105
|
+
def os
|
106
|
+
@os ||= (
|
107
|
+
require "rbconfig"
|
108
|
+
host_os = RbConfig::CONFIG['host_os'].downcase
|
109
|
+
|
110
|
+
case host_os
|
111
|
+
when /linux/
|
112
|
+
:linux
|
113
|
+
when /darwin|mac os/
|
114
|
+
:macosx
|
115
|
+
when /mswin|msys|mingw32/
|
116
|
+
:windows
|
117
|
+
when /cygwin/
|
118
|
+
:cygwin
|
119
|
+
when /solaris|sunos/
|
120
|
+
:solaris
|
121
|
+
when /bsd|dragonfly/
|
122
|
+
:bsd
|
123
|
+
when /aix/
|
124
|
+
:aix
|
125
|
+
else
|
126
|
+
raise Error, "unknown os: #{host_os.inspect}"
|
127
|
+
end
|
128
|
+
)
|
129
|
+
end
|
130
|
+
|
131
|
+
def arch
|
132
|
+
@arch ||= (
|
133
|
+
host_cpu = RbConfig::CONFIG['host_cpu'].downcase
|
134
|
+
case host_cpu
|
135
|
+
when /i[3456]86/
|
136
|
+
if workaround_older_macosx_misreported_cpu?
|
137
|
+
# Workaround case: older 64-bit Darwin Rubies misreported as i686
|
138
|
+
"x86_64"
|
139
|
+
else
|
140
|
+
"i386"
|
141
|
+
end
|
142
|
+
when /amd64|x86_64/
|
143
|
+
"x86_64"
|
144
|
+
when /ppc|powerpc/
|
145
|
+
"powerpc"
|
146
|
+
else
|
147
|
+
host_cpu
|
148
|
+
end
|
149
|
+
)
|
150
|
+
end
|
151
|
+
|
152
|
+
#
|
153
|
+
# By default, a child process will inherit open file descriptors from the
|
154
|
+
# parent process. This helper provides a cross-platform way of making sure
|
155
|
+
# that doesn't happen for the given file/io.
|
156
|
+
#
|
157
|
+
|
158
|
+
def close_on_exec(file)
|
159
|
+
if file.respond_to?(:close_on_exec=)
|
160
|
+
file.close_on_exec = true
|
161
|
+
elsif file.respond_to?(:fcntl) && defined?(Fcntl::FD_CLOEXEC)
|
162
|
+
file.fcntl Fcntl::F_SETFD, Fcntl::FD_CLOEXEC
|
163
|
+
|
164
|
+
if jruby? && posix_spawn?
|
165
|
+
# on JRuby, the fcntl call above apparently isn't enough when
|
166
|
+
# we're launching the process through posix_spawn.
|
167
|
+
fileno = JRuby.posix_fileno_for(file)
|
168
|
+
Unix::Lib.fcntl fileno, Fcntl::F_SETFD, Fcntl::FD_CLOEXEC
|
169
|
+
end
|
170
|
+
elsif windows?
|
171
|
+
Windows::Lib.dont_inherit file
|
172
|
+
else
|
173
|
+
raise Error, "not sure how to set close-on-exec for #{file.inspect} on #{platform_name.inspect}"
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
private
|
178
|
+
|
179
|
+
def warn_once(msg)
|
180
|
+
@warnings ||= {}
|
181
|
+
|
182
|
+
unless @warnings[msg]
|
183
|
+
@warnings[msg] = true
|
184
|
+
logger.warn msg
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
# Workaround: detect the situation that an older Darwin Ruby is actually
|
189
|
+
# 64-bit, but is misreporting cpu as i686, which would imply 32-bit.
|
190
|
+
#
|
191
|
+
# @return [Boolean] `true` if:
|
192
|
+
# (a) on Mac OS X
|
193
|
+
# (b) actually running in 64-bit mode
|
194
|
+
def workaround_older_macosx_misreported_cpu?
|
195
|
+
os == :macosx && is_64_bit?
|
196
|
+
end
|
197
|
+
|
198
|
+
# @return [Boolean] `true` if this Ruby represents `1` in 64 bits (8 bytes).
|
199
|
+
def is_64_bit?
|
200
|
+
1.size == 8
|
201
|
+
end
|
202
|
+
|
203
|
+
end # class << self
|
204
|
+
end # ChildProcess
|
205
|
+
|
206
|
+
require 'jruby' if ChildProcess.jruby?
|
207
|
+
|
208
|
+
require 'childprocess/unix' if ChildProcess.unix?
|
209
|
+
require 'childprocess/windows' if ChildProcess.windows?
|
210
|
+
require 'childprocess/jruby' if ChildProcess.jruby?
|
data/spec/abstract_io_spec.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
require File.expand_path('../spec_helper', __FILE__)
|
2
|
-
|
3
|
-
describe ChildProcess::AbstractIO do
|
4
|
-
let(:io) { ChildProcess::AbstractIO.new }
|
5
|
-
|
6
|
-
it "inherits the parent's IO streams" do
|
7
|
-
io.inherit!
|
8
|
-
|
9
|
-
expect(io.stdout).to eq STDOUT
|
10
|
-
expect(io.stderr).to eq STDERR
|
11
|
-
end
|
12
|
-
end
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe ChildProcess::AbstractIO do
|
4
|
+
let(:io) { ChildProcess::AbstractIO.new }
|
5
|
+
|
6
|
+
it "inherits the parent's IO streams" do
|
7
|
+
io.inherit!
|
8
|
+
|
9
|
+
expect(io.stdout).to eq STDOUT
|
10
|
+
expect(io.stderr).to eq STDERR
|
11
|
+
end
|
12
|
+
end
|