Class: Kward::NativeShell::NativeProcess::ExecState
- Inherits:
-
Object
- Object
- Kward::NativeShell::NativeProcess::ExecState
- Defined in:
- lib/kward/shell/native_exec_state.rb
Overview
A failed standalone interactive exec must return to a live Ruby process. Shell redirections are persisted separately; these backups protect the process's original IOs (including private descriptors), cwd and signals.
Instance Attribute Summary collapse
-
#signals ⇒ Object
readonly
Returns the value of attribute signals.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(descriptors) ⇒ ExecState
constructor
A new instance of ExecState.
- #restore ⇒ Object
Constructor Details
#initialize(descriptors) ⇒ ExecState
Returns a new instance of ExecState.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/kward/shell/native_exec_state.rb', line 12 def initialize(descriptors) @streams = {} @flags = {} @signals = {} @sync = [STDIN, STDOUT, STDERR].map { |io| io.sync unless io.closed? } @mask = File.umask @directory = Directory.new(".") @directory.protect_descriptors(descriptors) descriptors.each do |fd| flags = FCNTL.call(fd, Fcntl::F_GETFD, 0) if flags < 0 raise SystemCallError.new("fcntl", Fiddle.last_error) unless Fiddle.last_error == Errno::EBADF::Errno @streams[fd] = nil next end copy = FCNTL.call(fd, Fcntl::F_DUPFD, [32, descriptors.max + 1].max) raise SystemCallError.new("dup", Fiddle.last_error) if copy < 0 @streams[fd] = IO.for_fd(copy) @streams[fd].close_on_exec = true @flags[fd] = [flags, FCNTL.call(fd, Fcntl::F_GETFL, 0)] end complete = true ensure close unless complete end |
Instance Attribute Details
#signals ⇒ Object (readonly)
Returns the value of attribute signals.
10 11 12 |
# File 'lib/kward/shell/native_exec_state.rb', line 10 def signals @signals end |
Instance Method Details
#close ⇒ Object
66 67 68 69 |
# File 'lib/kward/shell/native_exec_state.rb', line 66 def close @streams.each_value { |io| io.close if io && !io.closed? } @directory&.close end |
#restore ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/kward/shell/native_exec_state.rb', line 38 def restore @streams.each do |fd, source| if fd < 3 standard = [STDIN, STDOUT, STDERR][fd] if source standard.reopen(source) standard.sync = @sync[fd] else standard.close unless standard.closed? end elsif source raise SystemCallError.new("dup2", Fiddle.last_error) if DUP2.call(source.fileno, fd) < 0 else CLOSE.call(fd) end next unless source descriptor_flags, file_flags = @flags.fetch(fd) raise SystemCallError.new("fcntl", Fiddle.last_error) if FCNTL.call(fd, Fcntl::F_SETFD, descriptor_flags) < 0 raise SystemCallError.new("fcntl", Fiddle.last_error) if FCNTL.call(fd, Fcntl::F_SETFL, file_flags) < 0 end @directory.enter File.umask(@mask) @signals.each { |name, handler| Signal.trap(name, handler || "DEFAULT") } rescue StandardError => e # Continuing with corrupt descriptors could expose private resources. raise SystemExit.new(126, "exec recovery failed: #{e.}") end |