Module: SubSpawn::POSIX::PtyHelper

Defined in:
lib/subspawn/posix/pty.rb

Defined Under Namespace

Classes: PtyIO

Class Method Summary collapse

Class Method Details

.openObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/subspawn/posix/pty.rb', line 45

def self.open
	*files, name = open_internal(true)
	return files unless block_given?

	begin
		return yield files.dup # Array, not splatted
	ensure
		files.reject(&:closed?).each(&:close)
	end
end

.open_internal(chmod_for_open = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/subspawn/posix/pty.rb', line 25

def self.open_internal(chmod_for_open = false)
	m, s, name = Internal::OpenPTY.call

	# chmod the slave path, but only if were were called as ::open, not as ::spawn
	# I don't understand why, but doing this just to mirror MRI
	File.chmod(0o600, name) if chmod_for_open

	master = PtyIO.for_fd(m, IO::RDWR | IO::SYNC)
	master.send(:__subspawn_init, name)

	# we could shim the slave to be a fake file, or we could just re-open the path
	# which fixes #inspect, #tty?, #path, and #close_on_exec, all in one go
	# https://bugs.ruby-lang.org/issues/19036
	slave = File.open(name, IO::RDWR | IO::SYNC)
	slave.sync = true # still must do this manually though, as IO::SYNC seems to be mostly ignored
	Internal::OpenPTY.close(s)

	[master, slave, name]
end