Class: Subprocess::Process
- Inherits:
-
Object
- Object
- Subprocess::Process
- Defined in:
- lib/subprocess.rb
Overview
A child process. The preferred way of spawning a subprocess is through the functions on Subprocess (especially check_call and check_output).
Instance Attribute Summary collapse
-
#command ⇒ Array<String>
readonly
The command this process was invoked with.
-
#pid ⇒ Integer
readonly
The process ID of the spawned process.
-
#status ⇒ ::Process::Status?
readonly
The exit status code of the process.
-
#stderr ⇒ IO
readonly
The
IO
that is connected to this process'sstderr
. -
#stdin ⇒ IO
readonly
The
IO
that is connected to this process'sstdin
. -
#stdout ⇒ IO
readonly
The
IO
that is connected to this process'sstdout
.
Instance Method Summary collapse
-
#communicate(input = nil, timeout_s = nil) {|stdout, stderr| ... } ⇒ Array(String, String)?
Write the (optional) input to the process's
stdin
and read the contents ofstdout
andstderr
. -
#drain_fd(fd, buf = nil) ⇒ true, false
Do nonblocking reads from
fd
, appending all data read intobuf
. -
#initialize(cmd, opts = {}) {|process| ... } ⇒ Process
constructor
Create a new process.
-
#poll ⇒ ::Process::Status?
Poll the child, setting (and returning) its status.
-
#send_signal(signal) ⇒ Integer
Does exactly what it says on the box.
-
#terminate ⇒ Integer
Sends
SIGTERM
to the process. -
#wait ⇒ ::Process::Status
Wait for the child to return, setting and returning the status of the child.
Constructor Details
#initialize(cmd, opts = {}) {|process| ... } ⇒ Process
Create a new process.
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/subprocess.rb', line 269 def initialize(cmd, opts={}, &blk) raise ArgumentError, "cmd must be an Array of strings" unless Array === cmd raise ArgumentError, "cmd cannot be empty" if cmd.empty? @command = cmd # Figure out what file descriptors we should pass on to the child (and # make externally visible ourselves) @child_stdin, @stdin = parse_fd(opts[:stdin], 'r') @child_stdout, @stdout = parse_fd(opts[:stdout], 'w') unless opts[:stderr] == STDOUT @child_stderr, @stderr = parse_fd(opts[:stderr], 'w') end retained_fds = Set.new(opts[:retain_fds] || []) # A control pipe for ferrying errors back from the child control_r, control_w = IO.pipe @pid = fork do begin ::STDIN.reopen(@child_stdin) if @child_stdin ::STDOUT.reopen(@child_stdout) if @child_stdout if opts[:stderr] == STDOUT ::STDERR.reopen(::STDOUT) else ::STDERR.reopen(@child_stderr) if @child_stderr end # Set up a new environment if we're requested to do so. if opts[:env] ENV.clear begin ENV.update(opts[:env]) rescue TypeError => e raise ArgumentError, "`env` option must be a hash where all keys and values are strings (#{e})" end end # Call the user back, maybe? if opts[:preexec_fn] if opts[:cwd] Dir.chdir(opts[:cwd], &opts[:preexec_fn]) else opts[:preexec_fn].call end end = {close_others: true}.merge(opts.fetch(:exec_opts, {})) if opts[:retain_fds] retained_fds.each { |fd| [fd] = fd } end if opts[:cwd] # We use the chdir option to `exec` since wrapping the # `exec` in a Dir.chdir block caused these sporadic errors on macOS: # Too many open files - getcwd (Errno::EMFILE) [:chdir] = opts[:cwd] end begin # Ruby's Kernel#exec will call an exec(3) variant if called with two # or more arguments, but when called with just a single argument will # spawn a subshell with that argument as the command. Since we always # want to call exec(3), we use the third exec form, which passes a # [cmdname, argv0] array as its first argument and never invokes a # subshell. exec([cmd[0], cmd[0]], *cmd[1..-1], ) rescue TypeError => e raise ArgumentError, "cmd must be an Array of strings (#{e})" end rescue Exception => e # Dump all errors up to the parent through the control socket Marshal.dump(e, control_w) control_w.flush end # Something has gone terribly, terribly wrong if we're hitting this :( exit!(1) end # Meanwhile, in the parent process... # First, let's close some things we shouldn't have access to @child_stdin.close if our_fd?(opts[:stdin]) @child_stdout.close if our_fd?(opts[:stdout]) @child_stderr.close if our_fd?(opts[:stderr]) control_w.close # Any errors during the spawn process? We'll get past this point when the # child execs and the OS closes control_w begin e = Marshal.load(control_r) e = "Unknown Failure" unless e.is_a?(Exception) || e.is_a?(String) # Because we're throwing an exception and not returning a # Process, we need to make sure the child gets reaped wait raise e rescue EOFError # Nothing to read? Great! ensure control_r.close end # Everything is okay. Good job, team! blk.call(self) if blk end |
Instance Attribute Details
#command ⇒ Array<String> (readonly)
Returns The command this process was invoked with.
220 221 222 |
# File 'lib/subprocess.rb', line 220 def command @command end |
#pid ⇒ Integer (readonly)
Returns The process ID of the spawned process.
223 224 225 |
# File 'lib/subprocess.rb', line 223 def pid @pid end |
#status ⇒ ::Process::Status? (readonly)
Returns The exit status code of the process. Only set after the process has exited.
227 228 229 |
# File 'lib/subprocess.rb', line 227 def status @status end |
#stderr ⇒ IO (readonly)
Returns The IO
that is connected to this process's stderr
.
217 218 219 |
# File 'lib/subprocess.rb', line 217 def stderr @stderr end |
#stdin ⇒ IO (readonly)
Returns The IO
that is connected to this process's stdin
.
211 212 213 |
# File 'lib/subprocess.rb', line 211 def stdin @stdin end |
#stdout ⇒ IO (readonly)
Returns The IO
that is connected to this process's stdout
.
214 215 216 |
# File 'lib/subprocess.rb', line 214 def stdout @stdout end |
Instance Method Details
#communicate(input = nil, timeout_s = nil) {|stdout, stderr| ... } ⇒ Array(String, String)?
Write the (optional) input to the process's stdin
and read the contents of
stdout
and stderr
. If a block is provided, stdout and stderr are yielded as they
are read. Otherwise they are buffered in memory and returned when the process
exits. Do this all using IO::select
, so we don't deadlock due to full pipe
buffers.
This is only really useful if you set some of :stdin
, :stdout
, and
:stderr
to Subprocess::PIPE.
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
# File 'lib/subprocess.rb', line 429 def communicate(input=nil, timeout_s=nil) raise ArgumentError if !input.nil? && @stdin.nil? stdout, stderr = +"", +"" # NB: Always force encoding to binary so we handle unicode or binary input # correctly across multiple write_nonblock calls, since we manually slice # the input depending on how many bytes were written input = input.dup.force_encoding('BINARY') unless input.nil? @stdin.close if (input.nil? || input.empty?) && !@stdin.nil? timeout_at = Time.now + timeout_s if timeout_s self.class.catching_sigchld(pid) do |global_read, self_read| wait_r = [@stdout, @stderr, self_read, global_read].compact wait_w = [input && @stdin].compact done = false while !done # If the process has exited, we want to drain any remaining output before returning if poll ready_r = wait_r - [self_read, global_read] ready_w = [] done = true else ready_r, ready_w = select_until(wait_r, wait_w, [], timeout_at) raise CommunicateTimeout.new(@command, stdout, stderr) if ready_r.nil? end if ready_r.include?(@stdout) if drain_fd(@stdout, stdout) wait_r.delete(@stdout) end end if ready_r.include?(@stderr) if drain_fd(@stderr, stderr) wait_r.delete(@stderr) end end if ready_r.include?(global_read) if drain_fd(global_read) raise "Unexpected internal error -- someone closed the global self-pipe!" end self.class.wakeup_sigchld end if ready_r.include?(self_read) if drain_fd(self_read) raise "Unexpected internal error -- someone closed our self-pipe!" end end if ready_w.include?(@stdin) written = 0 begin written = @stdin.write_nonblock(input) rescue EOFError # Maybe I shouldn't catch this... rescue Errno::EINTR rescue IO::WaitWritable # On OS X, a pipe can raise EAGAIN even after select indicates # that it is writable. Once the process consumes from the pipe, # the next write should succeed and we should make forward progress. # Until then, treat this as not writing any bytes and continue looping. # For details see: https://github.com/stripe/subprocess/pull/22 nil rescue Errno::EPIPE # The other side of the pipe closed before we could # write all of our input. This can happen if the # process exits prematurely. @stdin.close wait_w.delete(@stdin) end input = input[written..input.length] if input.empty? @stdin.close wait_w.delete(@stdin) end end if block_given? && !(stderr.empty? && stdout.empty?) yield stdout, stderr stdout, stderr = +"", +"" end end end wait if block_given? nil else [stdout, stderr] end end |
#drain_fd(fd, buf = nil) ⇒ true, false
Do nonblocking reads from fd
, appending all data read into buf
.
399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/subprocess.rb', line 399 def drain_fd(fd, buf=nil) loop do tmp = fd.read_nonblock(4096).force_encoding(fd.external_encoding) buf << tmp unless buf.nil? end rescue EOFError, Errno::EPIPE fd.close true rescue Errno::EINTR rescue Errno::EWOULDBLOCK, Errno::EAGAIN false end |
#poll ⇒ ::Process::Status?
Poll the child, setting (and returning) its status. If the child has not terminated, return nil and exit immediately
380 381 382 |
# File 'lib/subprocess.rb', line 380 def poll @status ||= (::Process.waitpid2(@pid, ::Process::WNOHANG) || []).last end |
#send_signal(signal) ⇒ Integer
Does exactly what it says on the box.
536 537 538 |
# File 'lib/subprocess.rb', line 536 def send_signal(signal) ::Process.kill(signal, pid) end |
#terminate ⇒ Integer
Sends SIGTERM
to the process.
545 546 547 |
# File 'lib/subprocess.rb', line 545 def terminate send_signal("TERM") end |
#wait ⇒ ::Process::Status
Wait for the child to return, setting and returning the status of the child.
388 389 390 |
# File 'lib/subprocess.rb', line 388 def wait @status ||= ::Process.waitpid2(@pid).last end |