rmux-client 0.10.0

Blocking local client and attach-mode plumbing for the RMUX terminal multiplexer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
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
37
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
use std::error::Error as StdError;
use std::fmt;
use std::fs::File;
use std::io::{self, Write};
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};
use std::process::{Command, Stdio};
use std::thread;
use std::time::{Duration, Instant};

use rmux_os::process_tree::ProcessTreeChild;
use rmux_proto::AttachShellCommand;
use rustix::fs::{fcntl_getfl, fcntl_setfl, OFlags};
use rustix::process::{kill_process, Pid, Signal};
use rustix::termios::{
    tcflush, tcgetattr, tcsetattr, OptionalActions, QueueSelector, SpecialCodeIndex, Termios,
};

use super::terminal_cleanup::fallback_attach_stop_sequence;
use super::termination;

const TERMINATION_OUTPUT_RETRY: Duration = Duration::from_millis(100);
const TERMINATION_OUTPUT_RETRY_INTERVAL: Duration = Duration::from_millis(1);
const SHELL_CHILD_WAIT_INTERVAL: Duration = Duration::from_millis(10);
const SHELL_CHILD_TERMINATION_GRACE: Duration = Duration::from_millis(250);

pub(super) fn current_process_pid() -> io::Result<Pid> {
    let raw = i32::try_from(std::process::id())
        .map_err(|_| io::Error::other("process id does not fit in i32"))?;
    Pid::from_raw(raw).ok_or_else(|| io::Error::other("process id must be positive"))
}

/// Result type for raw-terminal lifecycle operations.
pub type Result<T> = std::result::Result<T, AttachError>;

/// Errors produced while entering or restoring raw terminal mode.
#[derive(Debug)]
pub enum AttachError {
    /// Duplicating the target file descriptor failed before raw mode was applied.
    Io(io::Error),
    /// A termios syscall failed while applying or restoring raw mode.
    Termios(rustix::io::Errno),
}

impl fmt::Display for AttachError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io(error) => write!(formatter, "terminal descriptor operation failed: {error}"),
            Self::Termios(errno) => write!(formatter, "terminal mode operation failed: {errno}"),
        }
    }
}

impl StdError for AttachError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            Self::Io(error) => Some(error),
            Self::Termios(errno) => Some(errno),
        }
    }
}

impl From<io::Error> for AttachError {
    fn from(error: io::Error) -> Self {
        Self::Io(error)
    }
}

impl From<rustix::io::Errno> for AttachError {
    fn from(errno: rustix::io::Errno) -> Self {
        Self::Termios(errno)
    }
}

/// A drop guard that applies raw mode to a terminal and restores the original
/// settings when dropped.
///
/// The guard duplicates the target file descriptor so restoration still works
/// even if the caller later drops the original handle.
#[derive(Debug)]
#[must_use = "keep the guard alive for as long as raw terminal mode is required"]
pub struct RawTerminal {
    fd: OwnedFd,
    original_termios: Termios,
}

impl RawTerminal {
    /// Enters raw mode for the process stdin file descriptor.
    pub fn enter() -> Result<Self> {
        Self::from_fd(&io::stdin())
    }

    /// Enters raw mode for the provided terminal file descriptor.
    ///
    /// The descriptor must refer to a terminal device. The guard duplicates the
    /// descriptor before applying raw mode so the caller may drop the original
    /// handle after creation.
    pub fn from_fd<Fd>(fd: &Fd) -> Result<Self>
    where
        Fd: AsFd,
    {
        let owned_fd = fd.as_fd().try_clone_to_owned()?;
        let original_termios = tcgetattr(&owned_fd)?;
        let mut raw_termios = original_termios.clone();
        configure_raw_mode(&mut raw_termios);
        tcsetattr(&owned_fd, OptionalActions::Now, &raw_termios)?;

        Ok(Self {
            fd: owned_fd,
            original_termios,
        })
    }

    /// Restores the terminal settings captured when the guard was created.
    ///
    /// This provides explicit restore support for callers that want error
    /// feedback before the guard later runs its drop path.
    pub fn restore(&self) -> Result<()> {
        tcsetattr(&self.fd, OptionalActions::Now, &self.original_termios)?;
        Ok(())
    }

    fn reapply_raw_mode(&self) -> Result<()> {
        let mut raw_termios = self.original_termios.clone();
        configure_raw_mode(&mut raw_termios);
        tcsetattr(&self.fd, OptionalActions::Now, &raw_termios)?;
        Ok(())
    }

    pub(super) fn run_lock_command(&self, command: &str) -> Result<()> {
        self.restore()?;
        let result = run_shell_command_with_terminal(&self.fd, "sh", command, None);
        let reapply_result = self.reapply_raw_mode();
        if let Err(error) = result {
            reapply_result?;
            return Err(error);
        }
        reapply_result?;
        Ok(())
    }

    pub(super) fn run_lock_shell_command(&self, command: &AttachShellCommand) -> Result<()> {
        self.restore()?;
        let result = run_shell_command_with_terminal(
            &self.fd,
            command.shell(),
            command.command(),
            Some(command.cwd()),
        );
        let reapply_result = self.reapply_raw_mode();
        if let Err(error) = result {
            reapply_result?;
            return Err(error);
        }
        reapply_result
    }

    pub(super) fn suspend_self(&self) -> Result<()> {
        self.restore()?;
        kill_process(current_process_pid()?, Signal::TSTP)?;
        self.reapply_raw_mode()?;
        Ok(())
    }

    pub(super) fn run_detach_exec_command(&self, command: &str) -> Result<()> {
        self.restore()?;
        run_shell_command_with_terminal(&self.fd, "sh", command, None)
    }

    pub(super) fn run_detach_exec_shell_command(&self, command: &AttachShellCommand) -> Result<()> {
        self.restore()?;
        run_shell_command_with_terminal(
            &self.fd,
            command.shell(),
            command.command(),
            Some(command.cwd()),
        )
    }

    pub(super) fn restore_attach_terminal_state(&self) -> Result<()> {
        let mut terminal = File::from(self.fd.as_fd().try_clone_to_owned()?);
        let term = std::env::var("TERM").unwrap_or_default();
        terminal.write_all(&fallback_attach_stop_sequence(&term))?;
        terminal.flush()?;
        Ok(())
    }

    pub(super) fn restore_after_termination(&self) -> Result<()> {
        self.restore()?;
        let _flags = self.interrupt_output_writer()?;
        let mut terminal = File::from(self.fd.as_fd().try_clone_to_owned()?);
        write_cleanup_with_deadline(
            &mut terminal,
            &fallback_attach_stop_sequence(&std::env::var("TERM").unwrap_or_default()),
        )
        .map_err(AttachError::Io)
    }

    pub(super) fn interrupt_output_writer(&self) -> Result<FileStatusFlagsGuard<'_>> {
        let flags = FileStatusFlagsGuard::set_nonblocking(self.fd.as_fd())?;
        tcflush(&self.fd, QueueSelector::OFlush)?;
        Ok(flags)
    }

    pub(super) fn flush_pending_input(&self) -> Result<()> {
        tcflush(&self.fd, QueueSelector::IFlush)?;
        Ok(())
    }
}

pub(super) struct FileStatusFlagsGuard<'fd> {
    fd: BorrowedFd<'fd>,
    original: OFlags,
}

impl<'fd> FileStatusFlagsGuard<'fd> {
    fn set_nonblocking(fd: BorrowedFd<'fd>) -> Result<Self> {
        let original = fcntl_getfl(fd).map_err(io::Error::from)?;
        fcntl_setfl(fd, original | OFlags::NONBLOCK).map_err(io::Error::from)?;
        Ok(Self { fd, original })
    }
}

impl Drop for FileStatusFlagsGuard<'_> {
    fn drop(&mut self) {
        let _ = fcntl_setfl(self.fd, self.original);
    }
}

fn write_cleanup_with_deadline(output: &mut File, mut bytes: &[u8]) -> io::Result<()> {
    let deadline = Instant::now() + TERMINATION_OUTPUT_RETRY;
    while !bytes.is_empty() {
        match output.write(bytes) {
            Ok(0) => {
                return Err(io::Error::new(
                    io::ErrorKind::WriteZero,
                    "failed to write terminal cleanup after attach termination",
                ))
            }
            Ok(written) => bytes = &bytes[written..],
            Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
            Err(error)
                if error.kind() == io::ErrorKind::WouldBlock && Instant::now() < deadline =>
            {
                thread::sleep(TERMINATION_OUTPUT_RETRY_INTERVAL);
            }
            Err(error) => return Err(error),
        }
    }
    output.flush()
}

impl Drop for RawTerminal {
    fn drop(&mut self) {
        let _ = self.restore();
    }
}

fn configure_raw_mode(termios: &mut Termios) {
    termios.make_raw();
    termios.special_codes[SpecialCodeIndex::VMIN] = 1;
    termios.special_codes[SpecialCodeIndex::VTIME] = 0;
}

fn run_shell_command_with_terminal(
    fd: &OwnedFd,
    shell: &str,
    command: &str,
    cwd: Option<&str>,
) -> Result<()> {
    let stdin = File::from(fd.as_fd().try_clone_to_owned()?);
    let stdout = File::from(fd.as_fd().try_clone_to_owned()?);
    let stderr = File::from(fd.as_fd().try_clone_to_owned()?);
    let mut process = Command::new(shell);
    process
        .arg("-c")
        .arg(command)
        .stdin(Stdio::from(stdin))
        .stdout(Stdio::from(stdout))
        .stderr(Stdio::from(stderr));
    if let Some(cwd) = cwd {
        process.current_dir(cwd);
    }
    let mut child = ProcessTreeChild::spawn(&mut process).map_err(AttachError::Io)?;
    let foreground = child.foreground_terminal(fd).map_err(AttachError::Io)?;
    let wait_result = wait_for_shell_child(&mut child, termination::requested_signal);
    let restore_result = foreground.restore();
    if let Err(error) = wait_result {
        restore_result?;
        return Err(AttachError::Io(error));
    }
    restore_result?;
    Ok(())
}

fn wait_for_shell_child(
    child: &mut ProcessTreeChild,
    requested_signal: impl Fn() -> Option<i32>,
) -> io::Result<()> {
    loop {
        if child.has_exited()? {
            child.wait()?;
            return Ok(());
        }
        if child.has_stopped()? {
            terminate_stopped_shell_tree(child)?;
            return Ok(());
        }
        let Some(signal) = requested_signal() else {
            thread::sleep(SHELL_CHILD_WAIT_INTERVAL);
            continue;
        };

        child.forward_signal(signal).map_err(|error| {
            io::Error::new(
                error.kind(),
                format!("failed to forward attach interruption to shell tree: {error}"),
            )
        })?;
        let _ = wait_for_child_exit_until(child, Instant::now() + SHELL_CHILD_TERMINATION_GRACE)?;

        // Keep the exited Unix group leader waitable until after the force
        // signal. That prevents its PGID from being recycled while also
        // terminating descendants that ignored the forwarded signal.
        child.terminate().map_err(|error| {
            io::Error::new(
                error.kind(),
                format!("failed to terminate interrupted shell tree: {error}"),
            )
        })?;
        let _ = wait_for_child_exit_until(child, Instant::now() + SHELL_CHILD_TERMINATION_GRACE);
        child.wait().map_err(|error| {
            io::Error::new(
                error.kind(),
                format!("failed to reap interrupted shell tree: {error}"),
            )
        })?;
        return Err(termination::interruption_error());
    }
}

fn terminate_stopped_shell_tree(child: &mut ProcessTreeChild) -> io::Result<()> {
    child.terminate().map_err(|error| {
        io::Error::new(
            error.kind(),
            format!("failed to terminate stopped shell tree: {error}"),
        )
    })?;
    child.wait().map_err(|error| {
        io::Error::new(
            error.kind(),
            format!("failed to reap stopped shell tree: {error}"),
        )
    })?;
    Ok(())
}

fn wait_for_child_exit_until(child: &mut ProcessTreeChild, deadline: Instant) -> io::Result<bool> {
    while Instant::now() < deadline {
        if child.has_exited()? {
            return Ok(true);
        }
        thread::sleep(SHELL_CHILD_WAIT_INTERVAL);
    }
    child.has_exited()
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;
    use std::process::{Command, Stdio};
    use std::sync::atomic::{AtomicI32, Ordering};
    use std::sync::Arc;
    use std::time::{Duration, Instant};

    use rmux_os::process_tree::ProcessTreeChild;

    use super::wait_for_shell_child;

    fn spawn_shell(script: &str) -> ProcessTreeChild {
        let mut command = Command::new("sh");
        command
            .args(["-c", script])
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null());
        ProcessTreeChild::spawn(&mut command).expect("spawn isolated shell child")
    }

    #[test]
    fn shell_child_wait_is_bounded_after_hup_or_term() {
        for signal in [libc::SIGHUP, libc::SIGTERM] {
            let mut child = spawn_shell("exec sleep 60");
            let requested = Arc::new(AtomicI32::new(0));
            let request_from_thread = Arc::clone(&requested);
            std::thread::spawn(move || {
                std::thread::sleep(Duration::from_millis(40));
                request_from_thread.store(signal, Ordering::SeqCst);
            });

            let started = Instant::now();
            let error = wait_for_shell_child(&mut child, || {
                let signal = requested.load(Ordering::SeqCst);
                (signal != 0).then_some(signal)
            })
            .expect_err("termination must interrupt the shell wait");

            assert_eq!(
                error.kind(),
                std::io::ErrorKind::Interrupted,
                "unexpected shell wait error after signal {signal}: {error}"
            );
            assert!(
                started.elapsed() < Duration::from_secs(2),
                "shell wait should not remain blocked after signal {signal}"
            );
            assert!(
                child.has_exited().expect("query child status"),
                "the interrupted shell child should be reaped"
            );
        }
    }

    #[test]
    fn shell_child_wait_force_kills_a_child_that_ignores_term() {
        let mut child = spawn_shell("trap '' TERM; exec sleep 60");
        std::thread::sleep(Duration::from_millis(40));

        let started = Instant::now();
        let error = wait_for_shell_child(&mut child, || Some(libc::SIGTERM))
            .expect_err("ignored termination must still bound the shell wait");

        assert_eq!(error.kind(), std::io::ErrorKind::Interrupted);
        assert!(started.elapsed() < Duration::from_secs(2));
        assert!(
            child.has_exited().expect("query child status"),
            "the force-killed shell child should be reaped"
        );
    }

    #[test]
    fn shell_child_wait_terminates_signal_ignoring_descendants() {
        let pid_file = unique_descendant_pid_file();
        let mut command = Command::new("sh");
        command
            .args([
                "-c",
                "sh -c 'trap \"\" TERM; exec sleep 60' & printf '%s' \"$!\" > \"$RMUX_DESCENDANT_PID\"; wait",
            ])
            .env("RMUX_DESCENDANT_PID", &pid_file)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null());
        let mut child = ProcessTreeChild::spawn(&mut command).expect("spawn shell process tree");

        let descendant_pid = wait_for_descendant_pid(&pid_file);
        let cleanup = DescendantCleanup {
            pid: descendant_pid,
            pid_file,
        };
        let error = wait_for_shell_child(&mut child, || Some(libc::SIGTERM))
            .expect_err("termination must interrupt the complete shell process tree");

        assert_eq!(error.kind(), std::io::ErrorKind::Interrupted);
        let deadline = Instant::now() + Duration::from_secs(2);
        while process_exists(descendant_pid) && Instant::now() < deadline {
            std::thread::sleep(Duration::from_millis(10));
        }
        assert!(
            !process_exists(descendant_pid),
            "signal-ignoring descendant {descendant_pid} survived attach interruption"
        );
        drop(cleanup);
    }

    #[test]
    fn shell_child_wait_preserves_background_descendants_after_normal_exit() {
        let pid_file = unique_descendant_pid_file();
        let mut command = Command::new("sh");
        command
            .args([
                "-c",
                "sleep 60 </dev/null >/dev/null 2>&1 & printf '%s' \"$!\" > \"$RMUX_DESCENDANT_PID\"",
            ])
            .env("RMUX_DESCENDANT_PID", &pid_file)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null());
        let mut child = ProcessTreeChild::spawn(&mut command).expect("spawn shell process tree");

        let descendant_pid = wait_for_descendant_pid(&pid_file);
        let cleanup = DescendantCleanup {
            pid: descendant_pid,
            pid_file,
        };
        wait_for_shell_child(&mut child, || None).expect("foreground shell should exit normally");

        assert!(
            process_exists(descendant_pid),
            "normal shell completion must preserve an intentional background descendant"
        );
        drop(cleanup);
    }

    struct DescendantCleanup {
        pid: i32,
        pid_file: PathBuf,
    }

    impl Drop for DescendantCleanup {
        fn drop(&mut self) {
            unsafe {
                // SAFETY: The test owns the spawned descendant PID and uses a
                // force signal only as failure-path cleanup.
                libc::kill(self.pid, libc::SIGKILL);
            }
            let _ = std::fs::remove_file(&self.pid_file);
        }
    }

    fn unique_descendant_pid_file() -> PathBuf {
        std::env::temp_dir().join(format!(
            "rmux-attach-descendant-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .expect("system clock after Unix epoch")
                .as_nanos()
        ))
    }

    fn wait_for_descendant_pid(pid_file: &PathBuf) -> i32 {
        let deadline = Instant::now() + Duration::from_secs(2);
        loop {
            if let Ok(contents) = std::fs::read_to_string(pid_file) {
                if let Ok(pid) = contents.parse() {
                    return pid;
                }
            }
            assert!(
                Instant::now() < deadline,
                "shell did not publish its descendant pid"
            );
            std::thread::sleep(Duration::from_millis(10));
        }
    }

    fn process_exists(pid: i32) -> bool {
        let result = unsafe {
            // SAFETY: Signal zero performs an existence check without changing
            // the target process.
            libc::kill(pid, 0)
        };
        result == 0 || std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH)
    }
}

#[cfg(all(
    test,
    not(any(
        target_os = "cygwin",
        target_os = "horizon",
        target_os = "openbsd",
        target_os = "redox",
        target_os = "wasi"
    ))
))]
#[path = "terminal_stopped_tests.rs"]
mod stopped_tests;