rmux-server 0.7.1

Tokio daemon and request dispatcher 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
#![cfg(unix)]
#![allow(dead_code)]

use std::collections::BTreeSet;
use std::error::Error;
use std::fs::{File, OpenOptions};
use std::io::{self, Read};
use std::os::fd::AsFd;
use std::os::unix::net::UnixListener as StdUnixListener;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use rmux_proto::{
    decode_frame, encode_frame, AttachSessionRequest, AttachSessionResponse, FrameDecoder, Request,
    Response, RmuxError, SessionName, TerminalSize, DEFAULT_MAX_DETACHED_FRAME_LENGTH,
    RMUX_FRAME_MAGIC, RMUX_WIRE_VERSION,
};
use rmux_server::{DaemonConfig, ServerDaemon, ServerHandle};
use rustix::event::{poll, PollFd, PollFlags, Timespec};
use rustix::fs::{flock, FlockOperation};
use rustix::termios::{
    tcgetattr, tcgetwinsize, tcsetattr, OptionalActions, SpecialCodeIndex, Termios,
};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::UnixStream;
use tokio::sync::{Mutex, MutexGuard};

static UNIQUE_ID: AtomicUsize = AtomicUsize::new(0);
static IN_PROCESS_PTY_TEST_LOCK: Mutex<()> = Mutex::const_new(());
pub(crate) static PTY_TEST_LOCK: PtyTestLock = PtyTestLock;

pub(crate) struct PtyTestLock;

pub(crate) struct PtyTestGuard {
    _in_process: MutexGuard<'static, ()>,
    file: File,
}

impl PtyTestLock {
    pub(crate) async fn lock(&'static self) -> PtyTestGuard {
        let in_process = IN_PROCESS_PTY_TEST_LOCK.lock().await;
        let path = std::env::temp_dir().join("rmux-server-pty-tests.lock");
        let file = OpenOptions::new()
            .create(true)
            .truncate(false)
            .read(true)
            .write(true)
            .open(&path)
            .unwrap_or_else(|error| {
                panic!("failed to open PTY test lock '{}': {error}", path.display())
            });
        flock(file.as_fd(), FlockOperation::LockExclusive).unwrap_or_else(|error| {
            panic!(
                "failed to acquire PTY test lock '{}': {error}",
                path.display()
            )
        });
        PtyTestGuard {
            _in_process: in_process,
            file,
        }
    }
}

impl Drop for PtyTestGuard {
    fn drop(&mut self) {
        let _ = flock(self.file.as_fd(), FlockOperation::Unlock);
    }
}

pub(crate) async fn start_server(harness: &TestHarness) -> Result<ServerHandle, Box<dyn Error>> {
    let socket_path = harness.socket_path().to_path_buf();
    ServerDaemon::new(DaemonConfig::new(socket_path))
        .bind()
        .await
        .map_err(Into::into)
}

pub(crate) async fn send_request(
    socket_path: &Path,
    request: &Request,
) -> Result<Response, Box<dyn Error>> {
    let mut client = ClientConnection::connect(socket_path).await?;
    client.send_request(request).await
}

pub(crate) fn session_name(value: &str) -> SessionName {
    SessionName::new(value).expect("valid session name")
}

pub(crate) fn create_stale_socket(socket_path: &Path) -> Result<StdUnixListener, Box<dyn Error>> {
    let parent = socket_path.parent().ok_or_else(|| {
        io::Error::new(
            io::ErrorKind::InvalidInput,
            "socket path must include a parent directory",
        )
    })?;
    std::fs::create_dir_all(parent)?;
    let listener = StdUnixListener::bind(socket_path)?;
    Ok(listener)
}

pub(crate) async fn wait_for_socket_removal(socket_path: &Path) -> Result<(), Box<dyn Error>> {
    for _ in 0..200 {
        if !socket_path.exists() {
            return Ok(());
        }

        tokio::task::yield_now().await;
    }

    Err(io::Error::other(format!(
        "socket '{}' was not removed after drop",
        socket_path.display()
    ))
    .into())
}

pub(crate) fn pane_tty_paths() -> Result<BTreeSet<PathBuf>, Box<dyn Error>> {
    let mut paths = BTreeSet::new();

    for pid in pane_child_pids()? {
        let target = match std::fs::read_link(format!("/proc/{pid}/fd/0")) {
            Ok(target) => target,
            Err(_) => continue,
        };

        if is_pts_device(&target) {
            paths.insert(target);
        }
    }

    Ok(paths)
}

pub(crate) fn pane_child_pids() -> Result<BTreeSet<u32>, Box<dyn Error>> {
    let task_directory = format!("/proc/{}/task", std::process::id());
    let tasks = match std::fs::read_dir(task_directory) {
        Ok(tasks) => tasks,
        Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(BTreeSet::new()),
        Err(error) => return Err(error.into()),
    };

    let mut pids = BTreeSet::new();

    for task in tasks {
        let task = task?;
        let children = match std::fs::read_to_string(task.path().join("children")) {
            Ok(children) => children,
            Err(error) if error.kind() == io::ErrorKind::NotFound => continue,
            Err(error) => return Err(error.into()),
        };

        for pid in children.split_whitespace() {
            pids.insert(pid.parse()?);
        }
    }

    Ok(pids)
}

pub(crate) fn tty_size(path: &Path) -> Result<TerminalSize, Box<dyn Error>> {
    let file = std::fs::File::open(path)?;
    let winsize = tcgetwinsize(&file)?;

    Ok(TerminalSize {
        cols: winsize.ws_col,
        rows: winsize.ws_row,
    })
}

pub(crate) struct RawTty {
    file: std::fs::File,
    original_termios: Termios,
}

impl RawTty {
    pub(crate) fn open(path: &Path) -> Result<Self, Box<dyn Error>> {
        let file = OpenOptions::new().read(true).write(true).open(path)?;
        let original_termios = tcgetattr(&file)?;
        let mut raw_termios = original_termios.clone();
        raw_termios.make_raw();
        raw_termios.special_codes[SpecialCodeIndex::VMIN] = 1;
        raw_termios.special_codes[SpecialCodeIndex::VTIME] = 0;
        tcsetattr(&file, OptionalActions::Now, &raw_termios)?;

        Ok(Self {
            file,
            original_termios,
        })
    }

    pub(crate) fn read_exact(&mut self, len: usize) -> Result<Vec<u8>, Box<dyn Error>> {
        let mut buffer = vec![0; len];
        self.file.read_exact(&mut buffer)?;
        Ok(buffer)
    }

    pub(crate) fn read_exact_with_timeout(
        &mut self,
        len: usize,
        timeout: Duration,
    ) -> Result<Vec<u8>, Box<dyn Error>> {
        let mut fds = [PollFd::new(
            &self.file,
            PollFlags::IN | PollFlags::ERR | PollFlags::HUP,
        )];
        let timeout = Timespec {
            tv_sec: timeout.as_secs() as i64,
            tv_nsec: timeout.subsec_nanos() as i64,
        };

        let ready = poll(&mut fds, Some(&timeout))?;
        if ready == 0 || fds[0].revents().is_empty() {
            return Err(io::Error::new(io::ErrorKind::TimedOut, "tty read timed out").into());
        }

        self.read_exact(len)
    }

    pub(crate) fn write_all(&mut self, bytes: &[u8]) -> Result<(), Box<dyn Error>> {
        use std::io::Write;

        self.file.write_all(bytes)?;
        self.file.flush()?;
        Ok(())
    }
}

impl Drop for RawTty {
    fn drop(&mut self) {
        let _ = tcsetattr(&self.file, OptionalActions::Now, &self.original_termios);
    }
}

fn is_pts_device(path: &Path) -> bool {
    path.parent() == Some(Path::new("/dev/pts"))
        && path
            .file_name()
            .and_then(|name| name.to_str())
            .map(|name| name.chars().all(|character| character.is_ascii_digit()))
            .unwrap_or(false)
}

pub(crate) struct ClientConnection {
    stream: UnixStream,
    decoder: FrameDecoder,
    read_buffer: [u8; 4096],
}

impl ClientConnection {
    pub(crate) async fn connect(socket_path: &Path) -> Result<Self, Box<dyn Error>> {
        Ok(Self {
            stream: UnixStream::connect(socket_path).await?,
            decoder: FrameDecoder::new(),
            read_buffer: [0; 4096],
        })
    }

    pub(crate) async fn send_request(
        &mut self,
        request: &Request,
    ) -> Result<Response, Box<dyn Error>> {
        let frame = encode_frame(request)?;
        self.stream.write_all(&frame).await?;
        self.read_response().await
    }

    async fn read_response(&mut self) -> Result<Response, Box<dyn Error>> {
        loop {
            match self.decoder.next_frame::<Response>() {
                Ok(Some(response)) => return Ok(response),
                Ok(None) => {}
                Err(error) => return Err(Box::new(error)),
            }

            let bytes_read = self.stream.read(&mut self.read_buffer).await?;
            if bytes_read == 0 {
                return Err(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "connection closed before a response frame arrived",
                )
                .into());
            }

            self.decoder.push_bytes(&self.read_buffer[..bytes_read]);
        }
    }

    pub(crate) async fn begin_attach(
        mut self,
        request: AttachSessionRequest,
    ) -> Result<(AttachSessionResponse, UnixStream), Box<dyn Error>> {
        let frame = encode_frame(&Request::AttachSession(request))?;
        self.stream.write_all(&frame).await?;

        match read_response_exact(&mut self.stream).await? {
            Response::AttachSession(response) => Ok((response, self.stream)),
            other => Err(io::Error::other(format!("unexpected attach response: {other:?}")).into()),
        }
    }
}

pub(crate) async fn read_response_exact(
    stream: &mut UnixStream,
) -> Result<Response, Box<dyn Error>> {
    let frame = read_detached_frame_exact(stream).await?;
    decode_frame(&frame).map_err(Into::into)
}

async fn read_detached_frame_exact(stream: &mut UnixStream) -> Result<Vec<u8>, Box<dyn Error>> {
    let mut frame = Vec::new();
    let mut magic = [0_u8; 1];
    stream.read_exact(&mut magic).await?;
    if magic[0] != RMUX_FRAME_MAGIC {
        return Err(RmuxError::BadFrameMagic(magic[0]).into());
    }
    frame.push(magic[0]);

    let version = read_varint_u32_exact(stream, &mut frame).await?;
    if version != RMUX_WIRE_VERSION {
        return Err(RmuxError::UnsupportedWireVersion {
            got: version,
            minimum: RMUX_WIRE_VERSION,
            maximum: RMUX_WIRE_VERSION,
        }
        .into());
    }

    let mut length_bytes = [0_u8; 4];
    stream.read_exact(&mut length_bytes).await?;
    frame.extend_from_slice(&length_bytes);
    let length = u32::from_le_bytes(length_bytes) as usize;
    if length == 0 {
        return Err(RmuxError::EmptyFrame.into());
    }
    if length > DEFAULT_MAX_DETACHED_FRAME_LENGTH {
        return Err(RmuxError::FrameTooLarge {
            length,
            maximum: DEFAULT_MAX_DETACHED_FRAME_LENGTH,
        }
        .into());
    }

    let mut payload = vec![0_u8; length];
    stream.read_exact(&mut payload).await?;
    frame.extend_from_slice(&payload);
    Ok(frame)
}

async fn read_varint_u32_exact(
    stream: &mut UnixStream,
    frame: &mut Vec<u8>,
) -> Result<u32, Box<dyn Error>> {
    let mut value = 0_u32;
    for index in 0..5 {
        let mut byte = [0_u8; 1];
        stream.read_exact(&mut byte).await?;
        let byte = byte[0];
        frame.push(byte);
        value |= u32::from(byte & 0x7f) << (index * 7);
        if byte & 0x80 == 0 {
            return Ok(value);
        }
    }

    Err(RmuxError::Decode("wire-version varint exceeds u32 length".to_owned()).into())
}

pub(crate) struct TestHarness {
    root: PathBuf,
    socket_path: PathBuf,
}

impl TestHarness {
    pub(crate) fn new(label: &str) -> Self {
        let unique_id = UNIQUE_ID.fetch_add(1, Ordering::Relaxed);
        let root = PathBuf::from("/tmp").join(format!(
            "rxs-{}-{}-{unique_id}",
            compact_label(label),
            std::process::id()
        ));
        let socket_path = root.join("s.sock");

        Self { root, socket_path }
    }

    pub(crate) fn socket_path(&self) -> &Path {
        &self.socket_path
    }
}

fn compact_label(label: &str) -> String {
    let compact = label
        .chars()
        .filter(|character| character.is_ascii_alphanumeric())
        .take(16)
        .collect::<String>();
    if compact.is_empty() {
        "x".to_owned()
    } else {
        compact
    }
}

impl Drop for TestHarness {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(&self.socket_path);
        let _ = std::fs::remove_dir_all(&self.root);
    }
}