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/server.rb
CHANGED
|
@@ -99,8 +99,9 @@ module LibTmux
|
|
|
99
99
|
# - Down: {Session#list_windows}, {Session#list_panes}, {Window#list_panes}
|
|
100
100
|
# - Up: {Entity#server}
|
|
101
101
|
#
|
|
102
|
-
# **Changing things.**
|
|
103
|
-
# `command
|
|
102
|
+
# **Changing things.** A new session starts detached. Omit `name:` for a tmux
|
|
103
|
+
# name and `command:` for its default command or shell. Supply an argv
|
|
104
|
+
# array to run a particular program. The call returns the
|
|
104
105
|
# {Session} or, with `receipt: true`, a {CreationReceipt}. Kill stops the tmux
|
|
105
106
|
# server, which closing does only for an owned server. Switching a client
|
|
106
107
|
# points the client named by `client:` at a session.
|
|
@@ -212,10 +213,12 @@ module LibTmux
|
|
|
212
213
|
@executor = Internal::ProcessExecutor.new
|
|
213
214
|
@pin = Internal::SocketIdentity.new(@endpoint)
|
|
214
215
|
begin
|
|
215
|
-
@release = Internal::TmuxVersion.connected(@pin.command_prefix)
|
|
216
|
+
@release = Internal::TmuxVersion.connected(@pin.command_prefix) do |argv|
|
|
217
|
+
@executor.run(argv, **version_probe_options(version_timeout), discard_stdout: true)
|
|
218
|
+
end
|
|
216
219
|
@pin.record_release(@release)
|
|
217
220
|
argv, marker = Internal::TmuxVersion.fence_check_argv
|
|
218
|
-
Internal::TmuxVersion.verify_fence(@executor.run(@pin.command_prefix + argv,
|
|
221
|
+
Internal::TmuxVersion.verify_fence(@executor.run(@pin.command_prefix + argv, **version_probe_options(version_timeout)), marker)
|
|
219
222
|
rescue Exception
|
|
220
223
|
@pin.close
|
|
221
224
|
raise
|
|
@@ -232,6 +235,16 @@ module LibTmux
|
|
|
232
235
|
Internal::Capabilities.capability("control-mode").available?(@release)
|
|
233
236
|
end
|
|
234
237
|
|
|
238
|
+
# Read immutable feature and workaround decisions from the bound daemon.
|
|
239
|
+
# This bounded request fires no user hook and never refreshes the server's
|
|
240
|
+
# original version fence. Closed or unavailable servers raise instead of
|
|
241
|
+
# reporting cached support. Async facades use their fiber-aware dispatch.
|
|
242
|
+
def capabilities(timeout: 5.0, cancel: nil)
|
|
243
|
+
identity = bound_daemon_identity(timeout: timeout, cancel: cancel)
|
|
244
|
+
Capabilities.__send__(:new, binding_key: @pin.key, pid: identity.fetch(:pid),
|
|
245
|
+
start_time: identity.fetch(:start_time), release: @release)
|
|
246
|
+
end
|
|
247
|
+
|
|
235
248
|
# Raw tmux arguments retain tmux's separators and format semantics.
|
|
236
249
|
def run(argv, input: "".b, timeout: 5.0, cancel: nil)
|
|
237
250
|
dispatch(argv, input: input, timeout: timeout, cancel: cancel)
|
|
@@ -290,9 +303,17 @@ module LibTmux
|
|
|
290
303
|
execute_typed(["kill-server"], timeout: timeout, cancel: cancel)
|
|
291
304
|
end
|
|
292
305
|
|
|
293
|
-
|
|
306
|
+
# Create a session and return its handle, or its creation receipt.
|
|
307
|
+
# tmux through 3.6b replaces `:` and `.` in session names with `_`, so
|
|
308
|
+
# different requests can collide. Exact 3.7 refuses both characters
|
|
309
|
+
# before dispatch. tmux 3.7a through 3.8 preserve them.
|
|
310
|
+
# Read `session.snapshot.name` for the effective name and retain
|
|
311
|
+
# `session.ref` for subsequent targeting by ID.
|
|
312
|
+
# @raise [UnsupportedFeatureError] if exact tmux 3.7 refuses punctuation
|
|
313
|
+
def new_session(name: nil, command: nil, width: nil, height: nil, window_name: nil, cwd: nil, environment: {}, receipt: false, timeout: 5.0, cancel: nil)
|
|
294
314
|
budget = operation_budget(timeout, cancel)
|
|
295
|
-
arguments = ["new-session", "-d", "-P", "-F", creation_format(:session, receipt)
|
|
315
|
+
arguments = ["new-session", "-d", "-P", "-F", creation_format(:session, receipt)]
|
|
316
|
+
arguments.concat(["-s", literal_name(name)]) unless name.nil?
|
|
296
317
|
arguments.concat(["-n", literal_name(window_name)]) if window_name
|
|
297
318
|
{"-x" => width, "-y" => height}.each do |flag, value|
|
|
298
319
|
next if value.nil?
|
|
@@ -301,7 +322,7 @@ module LibTmux
|
|
|
301
322
|
arguments.concat([flag, value.to_s])
|
|
302
323
|
end
|
|
303
324
|
arguments.concat(creation_options(cwd: cwd, environment: environment))
|
|
304
|
-
create_entity(:session, arguments +
|
|
325
|
+
create_entity(:session, arguments + creation_command(command), receipt: receipt, **budget.options)
|
|
305
326
|
end
|
|
306
327
|
|
|
307
328
|
def list_sessions(timeout: 5.0, cancel: nil)
|
|
@@ -343,7 +364,7 @@ module LibTmux
|
|
|
343
364
|
if @controls.length >= @max_controls
|
|
344
365
|
raise CapacityError.new("server control capacity is exhausted", phase: :admission)
|
|
345
366
|
end
|
|
346
|
-
connection = ControlConnection.new
|
|
367
|
+
connection = ControlConnection.__send__(:new, binding: @pin, session_id: session_id, **options)
|
|
347
368
|
@controls << connection
|
|
348
369
|
end
|
|
349
370
|
if block_given?
|
|
@@ -391,6 +412,10 @@ module LibTmux
|
|
|
391
412
|
|
|
392
413
|
private
|
|
393
414
|
|
|
415
|
+
def version_probe_options(timeout)
|
|
416
|
+
{timeout: timeout}
|
|
417
|
+
end
|
|
418
|
+
|
|
394
419
|
class OperationBudget
|
|
395
420
|
def initialize(timeout, cancel, clock)
|
|
396
421
|
raise ArgumentError, "timeout must be finite" unless timeout.is_a?(Numeric) && timeout.finite?
|
|
@@ -416,6 +441,11 @@ module LibTmux
|
|
|
416
441
|
end
|
|
417
442
|
private_constant :OperationBudget
|
|
418
443
|
|
|
444
|
+
def self.operation_budget(timeout, cancel)
|
|
445
|
+
OperationBudget.new(timeout, cancel, -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
|
|
446
|
+
end
|
|
447
|
+
private_class_method :operation_budget
|
|
448
|
+
|
|
419
449
|
def operation_budget(timeout, cancel)
|
|
420
450
|
OperationBudget.new(timeout, cancel, method(:monotonic))
|
|
421
451
|
end
|
|
@@ -547,8 +577,19 @@ module LibTmux
|
|
|
547
577
|
end
|
|
548
578
|
end
|
|
549
579
|
|
|
580
|
+
def bound_daemon_identity(timeout:, cancel:)
|
|
581
|
+
arguments, marker = Internal::TmuxVersion.daemon_identity_argv
|
|
582
|
+
result = dispatch(arguments, timeout: timeout, cancel: cancel)
|
|
583
|
+
identity = Internal::TmuxVersion.parse_daemon_identity(result, marker)
|
|
584
|
+
unless identity.fetch(:version) == @release.text
|
|
585
|
+
raise ProtocolError.new("tmux did not report the bound daemon identity", delivery: :observed, phase: :decode)
|
|
586
|
+
end
|
|
587
|
+
|
|
588
|
+
identity
|
|
589
|
+
end
|
|
590
|
+
|
|
550
591
|
# Library operations that repair exact tmux 3.7's break-pane say so here.
|
|
551
|
-
def dispatch(argv, input: "".b, timeout: 5.0, cancel: nil, repaired_break: false)
|
|
592
|
+
def dispatch(argv, input: "".b, timeout: 5.0, cancel: nil, repaired_break: false, stdout_tail: nil)
|
|
552
593
|
ensure_owner
|
|
553
594
|
started = monotonic
|
|
554
595
|
validate_argv(argv)
|
|
@@ -558,15 +599,25 @@ module LibTmux
|
|
|
558
599
|
perform_request(cancel: cancel) do |view|
|
|
559
600
|
fence, marker = Internal::TmuxVersion.fence(@release)
|
|
560
601
|
result = @executor.run(@pin.command_prefix + fence + argv, input: input,
|
|
561
|
-
timeout: timeout - (monotonic - started), cancel: view)
|
|
602
|
+
timeout: timeout - (monotonic - started), cancel: view, stdout_tail: stdout_tail)
|
|
562
603
|
Internal::TmuxVersion.check_fence(result, marker, @release)
|
|
563
604
|
end
|
|
564
605
|
end
|
|
565
606
|
|
|
566
|
-
def execute_typed(arguments, repaired_break: false, **options)
|
|
607
|
+
def execute_typed(arguments, repaired_break: false, stdout_tail: nil, **options)
|
|
567
608
|
arguments = encode_arguments(arguments)
|
|
568
|
-
result =
|
|
569
|
-
|
|
609
|
+
result = if repaired_break || stdout_tail
|
|
610
|
+
dispatch(arguments, repaired_break: repaired_break, stdout_tail: stdout_tail, **options)
|
|
611
|
+
else
|
|
612
|
+
run(arguments, **options)
|
|
613
|
+
end
|
|
614
|
+
unless result.success?
|
|
615
|
+
message = "tmux #{arguments.first} failed"
|
|
616
|
+
line = result.stderr.byteslice(0, 240).partition(/[\r\n]/).first
|
|
617
|
+
reason = line.force_encoding(Encoding::UTF_8).scrub("?").gsub(/[[:cntrl:]\p{Cf}\p{Zl}\p{Zp}]/, "?")
|
|
618
|
+
message += ": #{reason}" unless reason.empty?
|
|
619
|
+
raise CommandError.new(message, result: result, phase: :command)
|
|
620
|
+
end
|
|
570
621
|
|
|
571
622
|
result
|
|
572
623
|
end
|
|
@@ -590,6 +641,10 @@ module LibTmux
|
|
|
590
641
|
name.gsub("#", "##")
|
|
591
642
|
end
|
|
592
643
|
|
|
644
|
+
def creation_command(command)
|
|
645
|
+
command.nil? ? [] : ["--", *pane_command(command)]
|
|
646
|
+
end
|
|
647
|
+
|
|
593
648
|
def pane_command(command)
|
|
594
649
|
validate_argv(command)
|
|
595
650
|
raise ArgumentError, "command executable must not be empty" if command.first.empty?
|
|
@@ -647,10 +702,17 @@ module LibTmux
|
|
|
647
702
|
end
|
|
648
703
|
|
|
649
704
|
def list_entities(kind, options, global: false, timeout: 5.0, cancel: nil)
|
|
650
|
-
|
|
705
|
+
budget = operation_budget(timeout, cancel)
|
|
706
|
+
result = execute_typed(["list-#{kind}s", *options, "-F", id_format(kind)], **budget.options)
|
|
651
707
|
entities = Internal::Metadata.decode(result.stdout, fields: 1).map { |row| build_entity(kind, row.first) }
|
|
652
708
|
entities = entities.uniq.sort_by { |entity| entity.id[1..].to_i } if global
|
|
653
709
|
entities.freeze
|
|
710
|
+
rescue CommandError => error
|
|
711
|
+
# Even with -a, tmux requires a current target for windows and panes.
|
|
712
|
+
raise unless global && kind != :session && error.result.stderr == "no current target\n"
|
|
713
|
+
raise unless list_sessions(**budget.options).empty?
|
|
714
|
+
|
|
715
|
+
[].freeze
|
|
654
716
|
end
|
|
655
717
|
|
|
656
718
|
def creation_options(cwd:, environment:)
|
|
@@ -686,9 +748,10 @@ module LibTmux
|
|
|
686
748
|
|
|
687
749
|
destination = target(ref, :session)
|
|
688
750
|
destination += ":#{index}" unless index.nil?
|
|
689
|
-
arguments = ["new-window", *(focus ? [] : ["-d"]), "-P", "-F", creation_format(:window, receipt), "-t", destination
|
|
751
|
+
arguments = ["new-window", *(focus ? [] : ["-d"]), "-P", "-F", creation_format(:window, receipt), "-t", destination]
|
|
752
|
+
arguments.concat(["-n", literal_name(name)]) unless name.nil?
|
|
690
753
|
arguments.concat(creation_options(cwd: cwd, environment: environment))
|
|
691
|
-
create_entity(:window, arguments +
|
|
754
|
+
create_entity(:window, arguments + creation_command(command), receipt: receipt, **budget.options)
|
|
692
755
|
end
|
|
693
756
|
|
|
694
757
|
def split_window(ref, direction:, command:, size: nil, cwd: nil, environment: {}, focus: false, timeout: 5.0, cancel: nil)
|
|
@@ -708,7 +771,7 @@ module LibTmux
|
|
|
708
771
|
arguments = ["split-window", *(focus ? [] : ["-d"]), flag, "-P", "-F", id_format(:pane), "-t", target(ref, ref.kind)]
|
|
709
772
|
arguments.concat(["-l", size.to_s]) if size
|
|
710
773
|
arguments.concat(creation_options(cwd: cwd, environment: environment))
|
|
711
|
-
create_entity(:pane, arguments +
|
|
774
|
+
create_entity(:pane, arguments + creation_command(command), **budget.options)
|
|
712
775
|
end
|
|
713
776
|
end
|
|
714
777
|
end
|
data/lib/libtmux/terminal.rb
CHANGED
|
@@ -30,6 +30,11 @@ module LibTmux
|
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
class Server
|
|
33
|
+
# Checks a TERM value before opening a server or attaching a client.
|
|
34
|
+
def self.valid_terminal_type?(value)
|
|
35
|
+
value.is_a?(String) && value.ascii_only? && /\A[A-Za-z0-9][A-Za-z0-9_.+-]{0,127}\z/.match?(value)
|
|
36
|
+
end
|
|
37
|
+
|
|
33
38
|
# Resolves the literal selector at dispatch; it is not a client reference.
|
|
34
39
|
def switch_client(client:, session:, timeout: 5.0, cancel: nil)
|
|
35
40
|
budget = operation_budget(timeout, cancel)
|
|
@@ -50,7 +55,7 @@ module LibTmux
|
|
|
50
55
|
unless terminal.is_a?(IO) && !terminal.closed? && terminal.tty?
|
|
51
56
|
raise ArgumentError, "terminal must be an open caller-owned TTY"
|
|
52
57
|
end
|
|
53
|
-
unless
|
|
58
|
+
unless Server.valid_terminal_type?(term)
|
|
54
59
|
raise ArgumentError, "term must name a terminal type"
|
|
55
60
|
end
|
|
56
61
|
raise ArgumentError, "read_only must be Boolean" unless [true, false].include?(read_only)
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "io/wait"
|
|
4
|
+
require "libtmux/entity"
|
|
5
|
+
require "libtmux/tmux_version"
|
|
6
|
+
|
|
7
|
+
module LibTmux
|
|
8
|
+
module TextWait
|
|
9
|
+
# The joined screen and whether its first capture matched the needle.
|
|
10
|
+
Result = Data.define(:text, :matched_at_entry) do
|
|
11
|
+
private_class_method :new, :[]
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class Pane
|
|
16
|
+
# Wait for literal text on the visible screen, including wrapped lines.
|
|
17
|
+
# The first read happens immediately. Each read runs the pane's native
|
|
18
|
+
# after-capture-pane hook. Use a distinct reply to avoid matching input echo.
|
|
19
|
+
# @raise [PaneDied] if the pane or server ends first
|
|
20
|
+
# @raise [DeadlineExceeded] if timeout elapses
|
|
21
|
+
# @raise [Cancelled] if the cancellation token is signaled
|
|
22
|
+
def wait_for_text(text, interval: 0.1, timeout: 5.0, cancel: nil)
|
|
23
|
+
last_text = ""
|
|
24
|
+
unless text.is_a?(String) && !text.empty?
|
|
25
|
+
raise ArgumentError, "text must be a nonempty String"
|
|
26
|
+
end
|
|
27
|
+
unless interval.is_a?(Numeric) && interval.finite? && interval.positive?
|
|
28
|
+
raise ArgumentError, "interval must be positive and finite"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
needle = text.dup.freeze
|
|
32
|
+
budget = server.__send__(:operation_budget, timeout, cancel)
|
|
33
|
+
pane_id = target
|
|
34
|
+
first_read = true
|
|
35
|
+
loop do
|
|
36
|
+
argv, marker = Internal::TmuxVersion.identity_argv(%w[pane_id pane_dead])
|
|
37
|
+
probe = [argv.first, "-t", pane_id, *argv.drop(1)]
|
|
38
|
+
result = server.run(["capture-pane", "-p", "-J", "-t", pane_id, ";", *probe], **budget.options)
|
|
39
|
+
screen, ending = text_wait_frame(result, marker, pane_id)
|
|
40
|
+
last_text = screen if screen
|
|
41
|
+
matched = last_text.include?(needle)
|
|
42
|
+
remaining = budget.options.fetch(:timeout)
|
|
43
|
+
|
|
44
|
+
if matched
|
|
45
|
+
return TextWait::Result.__send__(:new, text: last_text, matched_at_entry: first_read)
|
|
46
|
+
end
|
|
47
|
+
if ending
|
|
48
|
+
raise PaneDied.__send__(:new, ending, last_text)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
text_wait_pause([interval, remaining].min, cancel)
|
|
52
|
+
first_read = false
|
|
53
|
+
end
|
|
54
|
+
rescue DeadlineExceeded, Cancelled => error
|
|
55
|
+
error.__send__(:attach_wait_text, last_text)
|
|
56
|
+
raise
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def text_wait_frame(result, marker, pane_id)
|
|
62
|
+
stderr = result.stderr
|
|
63
|
+
# A capture hook can emit its own diagnostic before the state probe runs.
|
|
64
|
+
state = /^unknown command: #{Regexp.escape(marker)}(%\d+)?,([01])?\n/n.match(stderr)
|
|
65
|
+
if state
|
|
66
|
+
# The parse error in stderr carries state independently of screen bytes.
|
|
67
|
+
unless result.stdout.end_with?("\n") && (state[1] != pane_id || state[2])
|
|
68
|
+
raise ProtocolError.new("tmux did not return a complete pane capture", phase: :read, delivery: :observed)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
ending = if state[1] != pane_id
|
|
72
|
+
:pane_gone
|
|
73
|
+
elsif state[2] == "1"
|
|
74
|
+
:pane_exited
|
|
75
|
+
end
|
|
76
|
+
return [result.text.freeze, ending]
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# A failed capture can print hook output without reading the screen.
|
|
80
|
+
if Internal::TmuxVersion.daemon_loss_delivery(result)
|
|
81
|
+
return [nil, :server_gone]
|
|
82
|
+
end
|
|
83
|
+
if stderr.start_with?("no current target\n") ||
|
|
84
|
+
stderr.match?(/\Acan't find (?:pane|window|session): #{Regexp.escape(pane_id)}\n/n)
|
|
85
|
+
return [nil, :pane_gone]
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
raise CommandError.new("tmux could not read the pane screen", result: result, phase: :read)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def text_wait_pause(duration, cancel)
|
|
92
|
+
if cancel
|
|
93
|
+
cancel.reader.wait_readable(duration)
|
|
94
|
+
else
|
|
95
|
+
sleep(duration)
|
|
96
|
+
end
|
|
97
|
+
rescue IOError
|
|
98
|
+
raise unless cancel&.cancelled?
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
data/lib/libtmux/tmux_version.rb
CHANGED
|
@@ -28,7 +28,8 @@ module LibTmux
|
|
|
28
28
|
# sharing it, refuses the server instead of silently disabling the fence.
|
|
29
29
|
FENCE_COMMAND = "if-shel"
|
|
30
30
|
PROBE_COMMAND = "run-shel"
|
|
31
|
-
ABSENT =
|
|
31
|
+
ABSENT = /\A(?:no server running(?: on [^\n]+)?|error connecting to [^\n]+ \((?:No such file or directory|Connection refused)\))\n?\z/n
|
|
32
|
+
private_constant :ABSENT
|
|
32
33
|
# What a daemon older than 3.2 answers to the probe. run-shell gained -C
|
|
33
34
|
# in 3.2, so 3.1 reports its usage line; a 3.0 daemon speaks a protocol
|
|
34
35
|
# the client refuses before any command runs.
|
|
@@ -117,7 +118,7 @@ module LibTmux
|
|
|
117
118
|
raise UnsupportedFeatureError.new("tmux #{MINIMUM} or newer is required (the connected daemon is older than 3.2); #{REMEDY}",
|
|
118
119
|
phase: :admission, expected: EXPECTED)
|
|
119
120
|
end
|
|
120
|
-
if
|
|
121
|
+
if daemon_loss_delivery(result)
|
|
121
122
|
raise TargetNotFoundError.new("no tmux server is running at the selected endpoint", phase: :admission)
|
|
122
123
|
end
|
|
123
124
|
|
|
@@ -126,6 +127,13 @@ module LibTmux
|
|
|
126
127
|
raise not_sent(error), cause: nil
|
|
127
128
|
end
|
|
128
129
|
|
|
130
|
+
def daemon_loss_delivery(result)
|
|
131
|
+
return if result.success?
|
|
132
|
+
return :not_sent if result.stdout.empty? && ABSENT.match?(result.stderr.b)
|
|
133
|
+
|
|
134
|
+
:possibly_sent if result.stderr == "server exited unexpectedly\n"
|
|
135
|
+
end
|
|
136
|
+
|
|
129
137
|
# Reads the executable that would start a new daemon. tmux 3.3a prints
|
|
130
138
|
# its own file name where later releases print "tmux".
|
|
131
139
|
def executable(path)
|
|
@@ -162,6 +170,20 @@ module LibTmux
|
|
|
162
170
|
values.map(&:freeze)
|
|
163
171
|
end
|
|
164
172
|
|
|
173
|
+
def daemon_identity_argv
|
|
174
|
+
identity_argv(%w[version pid start_time])
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def parse_daemon_identity(result, marker, phase: :decode)
|
|
178
|
+
version, pid, start_time = parse_identity(result, marker, 3)
|
|
179
|
+
unless pid.match?(/\A[1-9][0-9]{0,9}\z/n) && start_time.match?(/\A-?[0-9]{1,19}\z/n)
|
|
180
|
+
raise ProtocolError.new("invalid tmux daemon identity metadata", phase: phase, delivery: :observed)
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
{version: version.dup.force_encoding(Encoding::UTF_8).freeze,
|
|
184
|
+
pid: Integer(pid, 10), start_time: Integer(start_time, 10)}
|
|
185
|
+
end
|
|
186
|
+
|
|
165
187
|
# Leading commands for one dispatch. A daemon that no longer reports
|
|
166
188
|
# +release+ fails the if-shell condition and reaches the private marker,
|
|
167
189
|
# which tmux rejects before the commands that follow.
|
data/lib/libtmux/version.rb
CHANGED
data/lib/libtmux.rb
CHANGED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# tmux starts this supervisor as the foreground process-group leader. Only the
|
|
4
|
+
# original socket peer can release the one-shot argv or keep the program alive.
|
|
5
|
+
require "socket"
|
|
6
|
+
|
|
7
|
+
directory, title = ARGV
|
|
8
|
+
if title == "prepare"
|
|
9
|
+
begin
|
|
10
|
+
UNIXSocket.open(File.join(directory, "prepare.sock")) do |socket|
|
|
11
|
+
socket.puts(Process.pid)
|
|
12
|
+
socket.read(1)
|
|
13
|
+
end
|
|
14
|
+
rescue Errno::ENOENT, Errno::ECONNREFUSED, Errno::ECONNRESET
|
|
15
|
+
# The launching owner has already unwound.
|
|
16
|
+
end
|
|
17
|
+
exit
|
|
18
|
+
end
|
|
19
|
+
exit 1 unless Process.getpgrp == Process.pid
|
|
20
|
+
begin
|
|
21
|
+
owner = UNIXSocket.new(File.join(directory, "owner.sock"))
|
|
22
|
+
rescue Errno::ENOENT, Errno::ECONNREFUSED
|
|
23
|
+
exit 1
|
|
24
|
+
end
|
|
25
|
+
owner.close_on_exec = true
|
|
26
|
+
# Terminal-generated signals also reach the supervisor. Caught dispositions
|
|
27
|
+
# revert to their defaults when the worker execs.
|
|
28
|
+
%w[HUP INT QUIT TSTP].each { |signal| Signal.trap(signal) {} }
|
|
29
|
+
begin
|
|
30
|
+
owner.puts(Process.pid)
|
|
31
|
+
exit unless owner.read(1) == "G"
|
|
32
|
+
command_file = File.join(directory, "argv")
|
|
33
|
+
argv = File.binread(command_file).split("\0".b, -1)
|
|
34
|
+
File.unlink(command_file)
|
|
35
|
+
Dir.rmdir(directory)
|
|
36
|
+
owner.write("A")
|
|
37
|
+
begin
|
|
38
|
+
child = Process.spawn([argv.first, argv.first], *argv.drop(1), close_others: true)
|
|
39
|
+
rescue SystemCallError => error
|
|
40
|
+
owner.puts("spawn #{error.errno}")
|
|
41
|
+
else
|
|
42
|
+
loop do
|
|
43
|
+
if (observed = Process.waitpid2(child, Process::WNOHANG))
|
|
44
|
+
status = observed.last
|
|
45
|
+
# CAN ends any unfinished escape. tmux parses the uppercase title after
|
|
46
|
+
# preceding output, and the title survives PTY uppercase conversion.
|
|
47
|
+
STDOUT.syswrite("\x18\e]2;#{title}\a")
|
|
48
|
+
owner.puts(status.signaled? ? "signal #{status.termsig}" : "exit #{status.exitstatus}")
|
|
49
|
+
break
|
|
50
|
+
end
|
|
51
|
+
exit if IO.select([owner], nil, nil, 0.01)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
owner.read(1) # Keep the process generation live until close or owner death.
|
|
55
|
+
rescue Errno::EPIPE, Errno::ECONNRESET, Errno::EIO
|
|
56
|
+
# The owner or terminal disappeared.
|
|
57
|
+
ensure
|
|
58
|
+
owner.close
|
|
59
|
+
Process.kill("KILL", 0) # Our live process still pins this process group.
|
|
60
|
+
end
|