libtmux 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -1
- data/README.md +270 -93
- data/lib/libtmux/capabilities.rb +86 -2
- data/lib/libtmux/capture.rb +6 -11
- data/lib/libtmux/control.rb +3 -0
- data/lib/libtmux/entity.rb +176 -3
- data/lib/libtmux/errors.rb +20 -1
- data/lib/libtmux/operations.rb +7 -0
- data/lib/libtmux/option_catalog.rb +54 -0
- data/lib/libtmux/options.rb +39 -11
- data/lib/libtmux/owned.rb +11 -0
- data/lib/libtmux/process.rb +40 -11
- data/lib/libtmux/program.rb +478 -0
- data/lib/libtmux/server.rb +80 -17
- data/lib/libtmux/terminal.rb +6 -1
- data/lib/libtmux/text_wait.rb +101 -0
- data/lib/libtmux/tmux_version.rb +24 -2
- data/lib/libtmux/version.rb +1 -1
- data/lib/libtmux.rb +6 -0
- data/libexec/program_child.rb +60 -0
- data/schema/option-catalog.json +1723 -0
- data/sig/control.rbs +2 -1
- data/sig/libtmux.rbs +81 -6
- data/sig/option_catalog.rbs +26 -0
- data/sig/program.rbs +47 -0
- data/sig/terminal.rbs +1 -0
- metadata +17 -4
data/lib/libtmux/process.rb
CHANGED
|
@@ -91,6 +91,35 @@ module LibTmux
|
|
|
91
91
|
module Internal
|
|
92
92
|
Cancellation = LibTmux::Cancellation
|
|
93
93
|
|
|
94
|
+
class CommandOutput
|
|
95
|
+
attr_reader :bytes
|
|
96
|
+
|
|
97
|
+
def initialize(limit, tail: nil)
|
|
98
|
+
@bytes = +"".b
|
|
99
|
+
@limit = @retained_limit = limit
|
|
100
|
+
if tail && tail.fetch(:retain) <= limit
|
|
101
|
+
@limit = tail.fetch(:read)
|
|
102
|
+
@retained_limit = tail.fetch(:retain)
|
|
103
|
+
end
|
|
104
|
+
@read = 0
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def read_size(chunk_size)
|
|
108
|
+
[chunk_size, @limit - @read + 1].min
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def append(chunk)
|
|
112
|
+
@read += chunk.bytesize
|
|
113
|
+
return false if @read > @limit
|
|
114
|
+
|
|
115
|
+
discard = [@bytes.bytesize + chunk.bytesize - @retained_limit, 0].max
|
|
116
|
+
yield chunk.bytesize - discard if block_given?
|
|
117
|
+
@bytes << chunk
|
|
118
|
+
@bytes.slice!(0, discard) if discard.positive?
|
|
119
|
+
true
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
94
123
|
class ProcessExecutor
|
|
95
124
|
def initialize(stdout_limit: 1 << 20, stderr_limit: 1 << 18, input_limit: 1 << 20, argv_limit: 1 << 18, cleanup_timeout: 0.5, drain_timeout: 0.5)
|
|
96
125
|
[stdout_limit, stderr_limit, input_limit, argv_limit].each do |limit|
|
|
@@ -107,14 +136,15 @@ module LibTmux
|
|
|
107
136
|
# +discard_stdout+ gives the child the null device for stdout, so the
|
|
108
137
|
# result reports no stdout and the call does not wait for a descriptor
|
|
109
138
|
# that a daemon older than tmux 3.2 keeps open after its client exits.
|
|
110
|
-
def run(argv, input: "".b, env: {}, timeout: 5.0, cancel: nil, discard_stdout: false)
|
|
111
|
-
Execution.new(argv, input, env, timeout, cancel, @limits, @cleanup_timeout, @drain_timeout,
|
|
139
|
+
def run(argv, input: "".b, env: {}, timeout: 5.0, cancel: nil, discard_stdout: false, stdout_tail: nil)
|
|
140
|
+
Execution.new(argv, input, env, timeout, cancel, @limits, @cleanup_timeout, @drain_timeout,
|
|
141
|
+
discard_stdout, stdout_tail).call
|
|
112
142
|
end
|
|
113
143
|
|
|
114
144
|
class Execution
|
|
115
145
|
CHUNK_BYTES = 16_384
|
|
116
146
|
|
|
117
|
-
def initialize(argv, input, env, timeout, cancel, limits, cleanup_timeout, drain_timeout, discard_stdout)
|
|
147
|
+
def initialize(argv, input, env, timeout, cancel, limits, cleanup_timeout, drain_timeout, discard_stdout, stdout_tail)
|
|
118
148
|
@started = monotonic
|
|
119
149
|
unless argv.is_a?(Array) && !argv.empty? && argv.all? { |argument| argument.is_a?(String) && !argument.include?("\0") }
|
|
120
150
|
raise ArgumentError, "argv must contain strings without NUL"
|
|
@@ -136,7 +166,8 @@ module LibTmux
|
|
|
136
166
|
@discard_stdout = discard_stdout
|
|
137
167
|
@process_wait = ProcessWait.new
|
|
138
168
|
@owned = []
|
|
139
|
-
@buffers = {stdout:
|
|
169
|
+
@buffers = {stdout: CommandOutput.new(limits.fetch(:stdout), tail: stdout_tail),
|
|
170
|
+
stderr: CommandOutput.new(limits.fetch(:stderr))}
|
|
140
171
|
@offset = 0
|
|
141
172
|
end
|
|
142
173
|
|
|
@@ -249,15 +280,12 @@ module LibTmux
|
|
|
249
280
|
def read_output(io, reading)
|
|
250
281
|
stream = reading.fetch(io)
|
|
251
282
|
buffer = @buffers.fetch(stream)
|
|
252
|
-
|
|
253
|
-
bytes = io.read_nonblock([CHUNK_BYTES, available + 1].min, exception: false)
|
|
283
|
+
bytes = io.read_nonblock(buffer.read_size(CHUNK_BYTES), exception: false)
|
|
254
284
|
if bytes.nil?
|
|
255
285
|
reading.delete(io)
|
|
256
286
|
io.close
|
|
257
|
-
elsif bytes.is_a?(String)
|
|
258
|
-
raise CapacityError.new("command #{stream} exceeded its byte limit", **details(:read))
|
|
259
|
-
|
|
260
|
-
buffer << bytes
|
|
287
|
+
elsif bytes.is_a?(String) && !buffer.append(bytes)
|
|
288
|
+
raise CapacityError.new("command #{stream} exceeded its byte limit", **details(:read))
|
|
261
289
|
end
|
|
262
290
|
end
|
|
263
291
|
|
|
@@ -286,7 +314,8 @@ module LibTmux
|
|
|
286
314
|
end
|
|
287
315
|
|
|
288
316
|
def result
|
|
289
|
-
CommandResult.new(**@buffers, status: @status,
|
|
317
|
+
CommandResult.new(**@buffers.transform_values(&:bytes), status: @status,
|
|
318
|
+
elapsed_seconds: monotonic - @started, pid: @pid, argv: @argv)
|
|
290
319
|
end
|
|
291
320
|
|
|
292
321
|
def retire
|
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "io/wait"
|
|
5
|
+
require "socket"
|
|
6
|
+
require "tmpdir"
|
|
7
|
+
require "rbconfig"
|
|
8
|
+
|
|
9
|
+
module LibTmux
|
|
10
|
+
# Start literal argv in a terminal on a new private server or a supplied server.
|
|
11
|
+
def self.start_program(argv, **options, &block)
|
|
12
|
+
Program.__send__(:start_with, nil, argv, **options, &block)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Start a private program, await completion, and close the program.
|
|
16
|
+
def self.run_program(argv, **options)
|
|
17
|
+
Program.__send__(:run_with, self, argv, **options)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# A terminal program tied to one process generation on a bound daemon.
|
|
21
|
+
# Use the block form for automatic cleanup, or explicitly close the owner.
|
|
22
|
+
class Program
|
|
23
|
+
# The pane's current geometry exceeds the owned screen limits.
|
|
24
|
+
class ScreenGeometryError < CapacityError; end
|
|
25
|
+
|
|
26
|
+
# A UTF-8 capture of the retained rows. Truncation records the byte cap.
|
|
27
|
+
class Screen
|
|
28
|
+
attr_reader :text
|
|
29
|
+
|
|
30
|
+
def initialize(text, truncated)
|
|
31
|
+
@text, @truncated = text.freeze, truncated
|
|
32
|
+
freeze
|
|
33
|
+
end
|
|
34
|
+
private_class_method :new
|
|
35
|
+
|
|
36
|
+
def truncated?
|
|
37
|
+
@truncated
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def inspect
|
|
41
|
+
"#<#{self.class} bytes=#{text.bytesize} truncated=#{truncated?}>"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# A completed process and the retained terminal screen, including history.
|
|
46
|
+
class Result
|
|
47
|
+
attr_reader :exit_status, :signal, :screen
|
|
48
|
+
|
|
49
|
+
def initialize(exit_status, signal, capture)
|
|
50
|
+
@exit_status, @signal, @screen = exit_status, signal, capture.text
|
|
51
|
+
@screen_truncated = capture.truncated?
|
|
52
|
+
freeze
|
|
53
|
+
end
|
|
54
|
+
private_class_method :new
|
|
55
|
+
|
|
56
|
+
def screen_truncated?
|
|
57
|
+
@screen_truncated
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def inspect
|
|
61
|
+
"#<#{self.class} exit_status=#{exit_status.inspect} signal=#{signal.inspect} screen_bytes=#{screen.bytesize}>"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
HISTORY_LINES = 100
|
|
66
|
+
SCREEN_BYTES = 65_536
|
|
67
|
+
MAX_WIDTH = 256
|
|
68
|
+
MAX_HEIGHT = 100
|
|
69
|
+
# A tmux grid cell holds up to 32 UTF-8 bytes, including combining marks.
|
|
70
|
+
SCREEN_TAIL = {retain: SCREEN_BYTES + 1, read: (MAX_HEIGHT + HISTORY_LINES) * (MAX_WIDTH * 32 + 1)}.freeze
|
|
71
|
+
CHILD = File.expand_path("../../libexec/program_child.rb", __dir__)
|
|
72
|
+
private_constant :HISTORY_LINES, :SCREEN_TAIL, :CHILD
|
|
73
|
+
|
|
74
|
+
def self.run_with(factory, argv, timeout: 5.0, cancel: nil, **options)
|
|
75
|
+
budget = Server.__send__(:operation_budget, timeout, cancel)
|
|
76
|
+
factory.start_program(argv, **options, **budget.options) { |program| program.wait(**budget.options) }
|
|
77
|
+
end
|
|
78
|
+
private_class_method :run_with
|
|
79
|
+
|
|
80
|
+
def self.start_with(adapter, argv, server: nil, executable: "tmux", cwd: nil, environment: {}, width: 80, height: 24,
|
|
81
|
+
timeout: 5.0, cancel: nil, history_lines: HISTORY_LINES)
|
|
82
|
+
validate_size(width, height)
|
|
83
|
+
raise ArgumentError, "server must be a LibTmux::Server" unless server.nil? || server.is_a?(Server)
|
|
84
|
+
unless history_lines.is_a?(Integer) && (0..HISTORY_LINES).cover?(history_lines)
|
|
85
|
+
raise ArgumentError, "history_lines must be between 0 and #{HISTORY_LINES}"
|
|
86
|
+
end
|
|
87
|
+
budget = Server.__send__(:operation_budget, timeout, cancel)
|
|
88
|
+
owned = Server.start(executable: executable, **budget.options) unless server
|
|
89
|
+
source = server || owned
|
|
90
|
+
scope = adapter&.call(source)
|
|
91
|
+
source = scope.server if scope
|
|
92
|
+
source.__send__(:require_program_support, **budget.options)
|
|
93
|
+
|
|
94
|
+
program = new(owned, scope, source, history_lines)
|
|
95
|
+
program.__send__(:launch, argv, cwd, environment, width, height, budget)
|
|
96
|
+
return program unless block_given?
|
|
97
|
+
|
|
98
|
+
yield program
|
|
99
|
+
rescue Exception => failure
|
|
100
|
+
raise
|
|
101
|
+
ensure
|
|
102
|
+
if (program || owned || scope) && (failure || block_given?)
|
|
103
|
+
begin
|
|
104
|
+
program ? program.close : close_owned(scope, owned)
|
|
105
|
+
rescue Exception => cleanup
|
|
106
|
+
raise unless failure
|
|
107
|
+
|
|
108
|
+
attach_cleanup(failure, cleanup)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
private_class_method :start_with
|
|
113
|
+
|
|
114
|
+
def self.validate_size(width, height)
|
|
115
|
+
unless [width, height].zip([MAX_WIDTH, MAX_HEIGHT]).all? { |value, limit| value.is_a?(Integer) && (1..limit).cover?(value) }
|
|
116
|
+
raise ArgumentError, "geometry must fit #{MAX_WIDTH} columns by #{MAX_HEIGHT} rows"
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
private_class_method :validate_size
|
|
120
|
+
|
|
121
|
+
def self.close_owned(scope, owned)
|
|
122
|
+
failure = nil
|
|
123
|
+
begin
|
|
124
|
+
scope&.close
|
|
125
|
+
rescue Exception => error
|
|
126
|
+
failure = error
|
|
127
|
+
ensure
|
|
128
|
+
begin
|
|
129
|
+
owned&.close
|
|
130
|
+
rescue Exception => cleanup
|
|
131
|
+
attach_cleanup(failure, cleanup)
|
|
132
|
+
failure ||= cleanup
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
raise failure if failure
|
|
136
|
+
end
|
|
137
|
+
private_class_method :close_owned
|
|
138
|
+
|
|
139
|
+
def self.attach_cleanup(failure, cleanup)
|
|
140
|
+
return unless failure.is_a?(Error)
|
|
141
|
+
|
|
142
|
+
details = ["program cleanup failed (#{cleanup.class})"]
|
|
143
|
+
details.concat(cleanup.cleanup_errors) if cleanup.is_a?(Error)
|
|
144
|
+
failure.__send__(:attach_cleanup_errors, details)
|
|
145
|
+
end
|
|
146
|
+
private_class_method :attach_cleanup
|
|
147
|
+
|
|
148
|
+
def initialize(owned, scope, server, history_lines)
|
|
149
|
+
@history_lines = history_lines
|
|
150
|
+
@close_lock = Mutex.new
|
|
151
|
+
@owned, @scope, @server = owned, scope, server
|
|
152
|
+
@result_lock = Mutex.new
|
|
153
|
+
@status_bytes = +"".b
|
|
154
|
+
@creator = Process.pid
|
|
155
|
+
end
|
|
156
|
+
private_class_method :new
|
|
157
|
+
|
|
158
|
+
# Each wait has its own deadline. All successful waits return the same result.
|
|
159
|
+
def wait(timeout: 5.0, cancel: nil)
|
|
160
|
+
ensure_open
|
|
161
|
+
budget = @server.__send__(:operation_budget, timeout, cancel)
|
|
162
|
+
loop do
|
|
163
|
+
options = budget.options
|
|
164
|
+
result = @result_lock.synchronize { @result }
|
|
165
|
+
return result if result
|
|
166
|
+
|
|
167
|
+
# Also check identity while the worker is running, so a respawn has
|
|
168
|
+
# a definite outcome even if its predecessor never reported an exit.
|
|
169
|
+
title = guarded("display-message", ["-p", "-t", @pane.id, "\#{pane_title}"], **options).text.chomp
|
|
170
|
+
status = read_status
|
|
171
|
+
if title == @final_title && status
|
|
172
|
+
result = Result.__send__(:new, *status, capture_screen(**budget.options))
|
|
173
|
+
budget.options
|
|
174
|
+
return @result_lock.synchronize { @result ||= result }
|
|
175
|
+
end
|
|
176
|
+
remaining = budget.options.fetch(:timeout)
|
|
177
|
+
pause = [remaining, 0.02].min
|
|
178
|
+
cancel ? cancel.reader.wait_readable(pause) : sleep(pause)
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Capture screen and bounded scrollback as UTF-8 text.
|
|
183
|
+
def screen(timeout: 5.0, cancel: nil)
|
|
184
|
+
capture_screen(timeout: timeout, cancel: cancel).text
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Capture the current screen and whether the 64 KiB limit dropped bytes.
|
|
188
|
+
def capture_screen(timeout: 5.0, cancel: nil)
|
|
189
|
+
ensure_open
|
|
190
|
+
text = capture_within_geometry(timeout: timeout, cancel: cancel)
|
|
191
|
+
offset = [text.bytesize - SCREEN_BYTES, 0].max
|
|
192
|
+
tail = text.byteslice(offset..).force_encoding(Encoding::UTF_8).scrub("")
|
|
193
|
+
Screen.__send__(:new, tail, offset.positive?)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def send_text(text, timeout: 5.0, cancel: nil)
|
|
197
|
+
ensure_open
|
|
198
|
+
raise ArgumentError, "text must be a String" unless text.is_a?(String)
|
|
199
|
+
guarded("send-keys", ["-l", "-t", @pane.id, "--", text], timeout: timeout, cancel: cancel)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def send_keys(*keys, timeout: 5.0, cancel: nil)
|
|
203
|
+
ensure_open
|
|
204
|
+
raise ArgumentError, "provide at least one key name" if keys.empty?
|
|
205
|
+
guarded("send-keys", ["-t", @pane.id, "--", *keys], timeout: timeout, cancel: cancel)
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def resize(width:, height:, timeout: 5.0, cancel: nil)
|
|
209
|
+
ensure_open
|
|
210
|
+
self.class.__send__(:validate_size, width, height)
|
|
211
|
+
guarded("resize-window", ["-t", @window.id, "-x", width.to_s, "-y", height.to_s],
|
|
212
|
+
window: @window.id, timeout: timeout, cancel: cancel)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def close
|
|
216
|
+
if @creator != Process.pid
|
|
217
|
+
# Only release descriptor copies inherited by this fork.
|
|
218
|
+
close_sockets
|
|
219
|
+
@closed = true
|
|
220
|
+
return nil
|
|
221
|
+
end
|
|
222
|
+
@close_lock.synchronize do
|
|
223
|
+
return nil if @closed
|
|
224
|
+
@scope&.__send__(:ensure_owner)
|
|
225
|
+
@server.__send__(:ensure_owner)
|
|
226
|
+
@result_lock.synchronize { @closing = true }
|
|
227
|
+
begin
|
|
228
|
+
retire_borrowed_panes unless @owned
|
|
229
|
+
ensure
|
|
230
|
+
close_sockets
|
|
231
|
+
begin
|
|
232
|
+
FileUtils.remove_entry_secure(@directory) if @directory
|
|
233
|
+
rescue Errno::ENOENT
|
|
234
|
+
# The supervisor removes its one-shot directory before launching.
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
self.class.__send__(:close_owned, @scope, @owned)
|
|
238
|
+
@closed = true
|
|
239
|
+
end
|
|
240
|
+
nil
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def inspect
|
|
244
|
+
"#<#{self.class} #{@closed ? "closed" : "open"}>"
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
private
|
|
248
|
+
|
|
249
|
+
def capture_within_geometry(timeout:, cancel:)
|
|
250
|
+
native = @server.method(:tmux_command)
|
|
251
|
+
marker = "libtmux-screen-geometry-#{SecureRandom.hex(12)}"
|
|
252
|
+
fits = "\#{&&:\#{e|<=:\#{pane_width},#{MAX_WIDTH}},\#{e|<=:\#{pane_height},#{MAX_HEIGHT}}}"
|
|
253
|
+
capture = native.call([@names.fetch("capture-pane"), "-p", "-S", (-@history_lines).to_s, "-t", @pane.id])
|
|
254
|
+
refuse = native.call([@names.fetch("display-message"), "-p", "--", marker])
|
|
255
|
+
result = guarded("if-shell", ["-F", "-t", @pane.id, fits, capture, refuse],
|
|
256
|
+
timeout: timeout, cancel: cancel, stdout_tail: SCREEN_TAIL)
|
|
257
|
+
return result.stdout unless result.stdout == "#{marker}\n"
|
|
258
|
+
|
|
259
|
+
raise ScreenGeometryError.new("Terminal program screen requires at most #{MAX_WIDTH} columns and #{MAX_HEIGHT} rows. " \
|
|
260
|
+
"Restore the pane geometry in tmux and retry.", phase: :admission)
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def retire_pane
|
|
264
|
+
if @pending_listener
|
|
265
|
+
cleanup = @server.__send__(:operation_budget, 0.5, nil)
|
|
266
|
+
claim_pending(cleanup)
|
|
267
|
+
retire_guarded_pane(@pending_target, pid: @pending_pid, **cleanup.options)
|
|
268
|
+
elsif @pid
|
|
269
|
+
retire_guarded_pane(@pane.id)
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def retire_preparation(**options)
|
|
274
|
+
return unless @prepare_pane
|
|
275
|
+
|
|
276
|
+
retire_guarded_pane(@prepare_pane, pid: @prepare_pid, **options)
|
|
277
|
+
@prepare_pane = nil
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def retire_guarded_pane(pane, **options)
|
|
281
|
+
guarded("kill-pane", ["-t", pane], pane: pane, **options)
|
|
282
|
+
rescue TargetNotFoundError => error
|
|
283
|
+
raise if error.delivery == :possibly_sent
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
def retire_borrowed_panes
|
|
287
|
+
failure = nil
|
|
288
|
+
begin
|
|
289
|
+
retire_preparation(timeout: 0.5)
|
|
290
|
+
rescue Exception => error
|
|
291
|
+
failure = error
|
|
292
|
+
ensure
|
|
293
|
+
begin
|
|
294
|
+
retire_pane unless @pane_retired
|
|
295
|
+
@pane_retired = true
|
|
296
|
+
rescue Exception => cleanup
|
|
297
|
+
raise unless failure
|
|
298
|
+
|
|
299
|
+
self.class.__send__(:attach_cleanup, failure, cleanup)
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
raise failure if failure
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
def close_sockets
|
|
306
|
+
@pending_peer&.close
|
|
307
|
+
@pending_listener&.close
|
|
308
|
+
@prepare_peer&.close
|
|
309
|
+
@peer&.close
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
def launch(argv, cwd, environment, width, height, budget)
|
|
313
|
+
@server.__send__(:pane_command, argv)
|
|
314
|
+
@server.__send__(:creation_options, cwd: cwd, environment: environment)
|
|
315
|
+
if argv.sum { |argument| argument.bytesize + 1 } > 262_144
|
|
316
|
+
raise ArgumentError, "program argv exceeds 256 KiB"
|
|
317
|
+
end
|
|
318
|
+
@nonce = SecureRandom.hex(12).upcase
|
|
319
|
+
@final_title = "LIBTMUX-FINISHED-#{@nonce}"
|
|
320
|
+
temporary_root = (Dir.tmpdir.bytesize <= 40) ? Dir.tmpdir : "/tmp"
|
|
321
|
+
@directory = Dir.mktmpdir("libtmux-ruby-program-", temporary_root)
|
|
322
|
+
File.open(File.join(@directory, "argv"), "wb", 0o600) { |io| io.write(argv.map(&:b).join("\0".b)) }
|
|
323
|
+
@names = @server.__send__(:builtin_spellings, "if-shell", "display-message", "set-option", "kill-pane",
|
|
324
|
+
"capture-pane", "send-keys", "resize-window", budget: budget)
|
|
325
|
+
helper = [RbConfig.ruby, "--disable=rubyopt,gems", CHILD, @directory]
|
|
326
|
+
session_name = "program-#{@nonce}"
|
|
327
|
+
# A grid created with zero history stays disabled even if the session's
|
|
328
|
+
# history-limit increases. Set the limit before creating the real pane.
|
|
329
|
+
setup, @prepare_peer, @prepare_pid = open_candidate("prepare", "=#{session_name}:", budget) do
|
|
330
|
+
@server.new_session(name: session_name, command: [*helper, "prepare", @nonce],
|
|
331
|
+
width: width, height: height, receipt: true, **budget.options)
|
|
332
|
+
end
|
|
333
|
+
@prepare_pane = setup.pane.id
|
|
334
|
+
setup.entity.options.set("history-limit", @history_lines, **budget.options)
|
|
335
|
+
receipt, @peer, @pid = open_candidate("owner", "=#{session_name}:=program", budget) do
|
|
336
|
+
setup.entity.new_window(name: "program", command: [*helper, @final_title],
|
|
337
|
+
cwd: cwd, environment: environment, focus: true, receipt: true, **budget.options)
|
|
338
|
+
end
|
|
339
|
+
@pane, @window = receipt.pane, receipt.window
|
|
340
|
+
retire_preparation(**budget.options)
|
|
341
|
+
@prepare_peer.close
|
|
342
|
+
guarded("set-option", ["-p", "-t", @pane.id, "allow-set-title", "on"], **budget.options)
|
|
343
|
+
@server.options.set("exit-empty", true, **budget.options) if @owned
|
|
344
|
+
guarded("display-message", ["-p", "-t", @pane.id, "\#{pane_id}"], **budget.options)
|
|
345
|
+
@peer.write("G")
|
|
346
|
+
wait_readable(@peer, budget)
|
|
347
|
+
unless @peer.read_nonblock(1) == "A"
|
|
348
|
+
raise ProtocolError.new("program supervisor did not acknowledge launch", phase: :spawn, delivery: :possibly_sent)
|
|
349
|
+
end
|
|
350
|
+
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE
|
|
351
|
+
raise OutcomeUnknown.new("program supervisor disconnected during launch", phase: :spawn, delivery: :possibly_sent)
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def open_candidate(name, target, budget)
|
|
355
|
+
path = File.join(@directory, "#{name}.sock")
|
|
356
|
+
@pending_target, @pending_listener = target, UNIXServer.new(path)
|
|
357
|
+
@pending_peer = @pending_pid = nil
|
|
358
|
+
@handshake = +"".b
|
|
359
|
+
begin
|
|
360
|
+
receipt = yield
|
|
361
|
+
rescue Error => failure
|
|
362
|
+
if failure.delivery == :not_sent
|
|
363
|
+
@pending_listener.close
|
|
364
|
+
@pending_listener = nil
|
|
365
|
+
end
|
|
366
|
+
raise
|
|
367
|
+
end
|
|
368
|
+
@pending_target = receipt.pane.id
|
|
369
|
+
claim_pending(budget)
|
|
370
|
+
guarded("set-option", ["-p", "-t", @pending_target, "remain-on-exit", "off"],
|
|
371
|
+
pane: @pending_target, pid: @pending_pid, **budget.options)
|
|
372
|
+
@pending_listener.close
|
|
373
|
+
File.unlink(path)
|
|
374
|
+
@pending_listener = nil
|
|
375
|
+
[receipt, @pending_peer, @pending_pid]
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
def claim_pending(budget)
|
|
379
|
+
unless @pending_peer
|
|
380
|
+
wait_readable(@pending_listener, budget)
|
|
381
|
+
@pending_peer = @pending_listener.accept_nonblock
|
|
382
|
+
end
|
|
383
|
+
until @handshake.end_with?("\n")
|
|
384
|
+
wait_readable(@pending_peer, budget)
|
|
385
|
+
@handshake << @pending_peer.read_nonblock(32)
|
|
386
|
+
raise ProtocolError.new("invalid program helper identity", phase: :admission) if @handshake.bytesize > 24
|
|
387
|
+
end
|
|
388
|
+
unless @handshake.match?(/\A[1-9]\d*\n\z/)
|
|
389
|
+
raise ProtocolError.new("invalid program helper PID", phase: :admission)
|
|
390
|
+
end
|
|
391
|
+
@pending_pid = Integer(@handshake, 10)
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
def wait_readable(io, budget)
|
|
395
|
+
loop do
|
|
396
|
+
options = budget.options
|
|
397
|
+
timeout = options.fetch(:timeout)
|
|
398
|
+
timeout = [timeout, 0.02].min if options[:cancel]
|
|
399
|
+
if io.wait_readable(timeout)
|
|
400
|
+
budget.options
|
|
401
|
+
return
|
|
402
|
+
end
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
def read_status
|
|
407
|
+
@result_lock.synchronize do
|
|
408
|
+
raise ClosedError.new("program is closing", phase: :retire) if @closing
|
|
409
|
+
raise @spawn_error if @spawn_error
|
|
410
|
+
return @exit if @exit
|
|
411
|
+
part = @peer.read_nonblock(48, exception: false)
|
|
412
|
+
return if part == :wait_readable
|
|
413
|
+
raise OutcomeUnknown.new("program supervisor closed before reporting exit", phase: :command, delivery: :possibly_sent) unless part
|
|
414
|
+
@status_bytes << part
|
|
415
|
+
raise ProtocolError.new("program supervisor sent an oversized result", phase: :decode) if @status_bytes.bytesize > 48
|
|
416
|
+
return unless @status_bytes.end_with?("\n")
|
|
417
|
+
match = /\A(exit|signal|spawn) (\d+)\n\z/.match(@status_bytes)
|
|
418
|
+
raise ProtocolError.new("invalid program supervisor result", phase: :decode) unless match
|
|
419
|
+
value = Integer(match[2], 10)
|
|
420
|
+
if match[1] == "spawn"
|
|
421
|
+
@spawn_error = TransportError.new("terminal program could not start (errno #{value})", phase: :spawn, delivery: :not_sent)
|
|
422
|
+
raise @spawn_error
|
|
423
|
+
end
|
|
424
|
+
@exit = (match[1] == "exit") ? [value, nil] : [nil, value]
|
|
425
|
+
end
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
def guarded(command, arguments, pane: @pane.id, pid: @pid, window: nil, **options)
|
|
429
|
+
# New sessions run a supervisor whose startup command includes a fresh
|
|
430
|
+
# directory. That directory accepts one peer and one launch only.
|
|
431
|
+
native = @server.method(:tmux_command)
|
|
432
|
+
nonce = "\#{m:*#{@nonce}*,\#{pane_start_command}}"
|
|
433
|
+
guard = "\#{&&:\#{==:\#{pane_pid},#{pid}},#{nonce}}"
|
|
434
|
+
guard = "\#{&&:#{guard},\#{==:\#{window_id},#{window}}}" if window
|
|
435
|
+
marker = "libtmux-replaced-#{SecureRandom.hex(12)}"
|
|
436
|
+
yes = native.call([@names.fetch(command), *arguments])
|
|
437
|
+
no = native.call([@names.fetch("display-message"), "-p", "--", marker])
|
|
438
|
+
result = @server.__send__(:execute_typed, [@names.fetch("if-shell"), "-F", "-t", pane, guard, yes, no], **options)
|
|
439
|
+
return result unless result.stdout == "#{marker}\n"
|
|
440
|
+
|
|
441
|
+
raise TargetNotFoundError.new("terminal program target changed", phase: :command, delivery: :observed)
|
|
442
|
+
rescue CommandError => error
|
|
443
|
+
if (delivery = Internal::TmuxVersion.daemon_loss_delivery(error.result))
|
|
444
|
+
raise TargetNotFoundError.new("the terminal program's tmux daemon stopped", phase: :command, delivery: delivery)
|
|
445
|
+
end
|
|
446
|
+
raise unless error.result.stderr.match?(/\A(?:can't find pane|no current target)/)
|
|
447
|
+
raise TargetNotFoundError.new("terminal program pane is gone", phase: :command, delivery: :observed)
|
|
448
|
+
end
|
|
449
|
+
|
|
450
|
+
def ensure_open
|
|
451
|
+
raise ClosedError.new("program is closed", phase: :admission) if @closing || @closed || @creator != Process.pid
|
|
452
|
+
@server.__send__(:with_bound_endpoint) { nil }
|
|
453
|
+
end
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
class Server
|
|
457
|
+
# Read the owned-program requirement and support decision from the bound daemon.
|
|
458
|
+
def program_support(timeout: 5.0, cancel: nil)
|
|
459
|
+
identity = bound_daemon_identity(timeout: timeout, cancel: cancel)
|
|
460
|
+
capability = Internal::Capabilities.capability("owned-terminal-program")
|
|
461
|
+
identity.merge(binding_key: @pin.key, since: capability.since, supported: capability.available?(@release)).freeze
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
def start_program(argv, **options, &block)
|
|
465
|
+
LibTmux.start_program(argv, **options, server: self, &block)
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
private
|
|
469
|
+
|
|
470
|
+
def require_program_support(**options)
|
|
471
|
+
support = program_support(**options)
|
|
472
|
+
return if support.fetch(:supported)
|
|
473
|
+
|
|
474
|
+
raise UnsupportedFeatureError.new("owned terminal programs require tmux #{support.fetch(:since)} or newer",
|
|
475
|
+
phase: :admission, delivery: :not_sent, expected: "owned-terminal-program")
|
|
476
|
+
end
|
|
477
|
+
end
|
|
478
|
+
end
|