Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Dec 10, 2020
1 parent c626073 commit d1de572
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
31 changes: 17 additions & 14 deletions lib/childprocess.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ class << self
attr_writer :logger

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
require 'childprocess/unix/process_spawn_process'
return Unix::PosixSpawnProcess.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
alias_method :build, :new

Expand Down
47 changes: 47 additions & 0 deletions lib/childprocess/unix/process_spawn_process.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module ChildProcess
module Unix
class PosixSpawnProcess < Process
private

def launch_process
options = {}

options[:out] = io.stdout ? io.stdout.fileno : File::NULL
options[:err] = io.stderr ? io.stderr.fileno : File::NULL

if duplex?
reader, writer = ::IO.pipe
options[:in] = reader.fileno
options[writer.fileno] = :close
end

options[:pgroup] = true if leader?

options[:chdir] = @cwd if @cwd

if @args.size == 1
# When given a single String, Process.spawn would think it should use the shell
# if there is any special character in it. However, ChildProcess should never
# use the shell. So we use the [cmdname, argv0] form to force no shell.
arg = @args[0]
args = [[arg, arg]]
else
args = @args
end

begin
@pid = ::Process.spawn(@environment, *args, **options)
rescue SystemCallError => e
raise LaunchError, e.message
end

if duplex?
io._stdin = writer
reader.close
end

::Process.detach(@pid) if detach?
end
end
end
end

0 comments on commit d1de572

Please sign in to comment.